From 50760c693510894ca368e90369b0cc2dabfd07f3 Mon Sep 17 00:00:00 2001 From: Pretty Printer Date: Fri, 17 Apr 2020 09:56:34 -0500 Subject: [PATCH] Format first-party source according to .clang-format --- .../app/consensus/RCLCensorshipDetector.h | 52 +- src/ripple/app/consensus/RCLConsensus.cpp | 159 +- src/ripple/app/consensus/RCLConsensus.h | 41 +- src/ripple/app/consensus/RCLCxLedger.h | 2 +- src/ripple/app/consensus/RCLCxPeerPos.cpp | 8 +- src/ripple/app/consensus/RCLCxPeerPos.h | 6 +- src/ripple/app/consensus/RCLCxTx.h | 6 +- src/ripple/app/consensus/RCLValidations.cpp | 30 +- src/ripple/app/consensus/RCLValidations.h | 8 +- .../app/ledger/AbstractFetchPackContainer.h | 7 +- src/ripple/app/ledger/AcceptedLedger.cpp | 27 +- src/ripple/app/ledger/AcceptedLedger.h | 31 +- src/ripple/app/ledger/AcceptedLedgerTx.cpp | 90 +- src/ripple/app/ledger/AcceptedLedgerTx.h | 55 +- src/ripple/app/ledger/AccountStateSF.cpp | 13 +- src/ripple/app/ledger/AccountStateSF.h | 17 +- src/ripple/app/ledger/BookListeners.cpp | 2 +- src/ripple/app/ledger/BookListeners.h | 4 +- src/ripple/app/ledger/BuildLedger.h | 3 +- src/ripple/app/ledger/ConsensusTransSetSF.cpp | 58 +- src/ripple/app/ledger/ConsensusTransSetSF.h | 19 +- src/ripple/app/ledger/InboundLedger.h | 148 +- src/ripple/app/ledger/InboundLedgers.h | 65 +- src/ripple/app/ledger/InboundTransactions.h | 58 +- src/ripple/app/ledger/Ledger.cpp | 834 +- src/ripple/app/ledger/Ledger.h | 181 +- src/ripple/app/ledger/LedgerCleaner.h | 24 +- src/ripple/app/ledger/LedgerHistory.cpp | 370 +- src/ripple/app/ledger/LedgerHistory.h | 55 +- src/ripple/app/ledger/LedgerHolder.h | 21 +- src/ripple/app/ledger/LedgerMaster.h | 304 +- src/ripple/app/ledger/LedgerReplay.h | 6 +- src/ripple/app/ledger/LedgerToJson.h | 31 +- src/ripple/app/ledger/LocalTxs.h | 18 +- src/ripple/app/ledger/OpenLedger.h | 154 +- src/ripple/app/ledger/OrderBookDB.cpp | 174 +- src/ripple/app/ledger/OrderBookDB.h | 49 +- src/ripple/app/ledger/PendingSaves.h | 34 +- src/ripple/app/ledger/TransactionMaster.h | 41 +- src/ripple/app/ledger/TransactionStateSF.cpp | 17 +- src/ripple/app/ledger/TransactionStateSF.h | 17 +- src/ripple/app/ledger/impl/BuildLedger.cpp | 55 +- src/ripple/app/ledger/impl/InboundLedger.cpp | 918 +- src/ripple/app/ledger/impl/InboundLedgers.cpp | 245 +- .../app/ledger/impl/InboundTransactions.cpp | 199 +- src/ripple/app/ledger/impl/LedgerCleaner.cpp | 188 +- src/ripple/app/ledger/impl/LedgerMaster.cpp | 1052 ++- src/ripple/app/ledger/impl/LedgerToJson.cpp | 101 +- src/ripple/app/ledger/impl/LocalTxs.cpp | 94 +- src/ripple/app/ledger/impl/OpenLedger.cpp | 129 +- .../app/ledger/impl/TransactionAcquire.cpp | 208 +- .../app/ledger/impl/TransactionAcquire.h | 66 +- .../app/ledger/impl/TransactionMaster.cpp | 79 +- src/ripple/app/main/Application.cpp | 1454 ++-- src/ripple/app/main/Application.h | 221 +- src/ripple/app/main/BasicApp.cpp | 15 +- src/ripple/app/main/CollectorManager.cpp | 39 +- src/ripple/app/main/CollectorManager.h | 15 +- src/ripple/app/main/DBInit.h | 167 +- src/ripple/app/main/GRPCServer.cpp | 70 +- src/ripple/app/main/LoadManager.cpp | 93 +- src/ripple/app/main/LoadManager.h | 47 +- src/ripple/app/main/Main.cpp | 421 +- src/ripple/app/main/NodeIdentity.cpp | 45 +- src/ripple/app/main/NodeIdentity.h | 4 +- src/ripple/app/main/NodeStoreScheduler.cpp | 50 +- src/ripple/app/main/NodeStoreScheduler.h | 38 +- src/ripple/app/main/Tuning.h | 2 +- src/ripple/app/misc/AmendmentTable.h | 87 +- src/ripple/app/misc/CanonicalTXSet.cpp | 99 +- src/ripple/app/misc/CanonicalTXSet.h | 70 +- src/ripple/app/misc/FeeVote.h | 40 +- src/ripple/app/misc/FeeVoteImpl.cpp | 141 +- src/ripple/app/misc/HashRouter.cpp | 77 +- src/ripple/app/misc/HashRouter.h | 126 +- src/ripple/app/misc/LoadFeeTrack.h | 94 +- src/ripple/app/misc/Manifest.h | 97 +- src/ripple/app/misc/NetworkOPs.cpp | 2661 +++--- src/ripple/app/misc/NetworkOPs.h | 215 +- src/ripple/app/misc/OrderBook.h | 28 +- src/ripple/app/misc/SHAMapStore.h | 38 +- src/ripple/app/misc/SHAMapStoreImp.cpp | 394 +- src/ripple/app/misc/SHAMapStoreImp.h | 106 +- src/ripple/app/misc/Transaction.h | 153 +- src/ripple/app/misc/TxQ.h | 149 +- src/ripple/app/misc/ValidatorKeys.h | 5 +- src/ripple/app/misc/ValidatorList.h | 87 +- src/ripple/app/misc/ValidatorSite.h | 37 +- src/ripple/app/misc/detail/Work.h | 10 +- src/ripple/app/misc/detail/WorkBase.h | 104 +- src/ripple/app/misc/detail/WorkFile.h | 35 +- src/ripple/app/misc/detail/WorkPlain.h | 24 +- src/ripple/app/misc/detail/WorkSSL.h | 18 +- src/ripple/app/misc/impl/AccountTxPaging.cpp | 22 +- src/ripple/app/misc/impl/AccountTxPaging.h | 9 +- src/ripple/app/misc/impl/AmendmentTable.cpp | 371 +- src/ripple/app/misc/impl/LoadFeeTrack.cpp | 45 +- src/ripple/app/misc/impl/Manifest.cpp | 339 +- src/ripple/app/misc/impl/Transaction.cpp | 133 +- src/ripple/app/misc/impl/TxQ.cpp | 713 +- src/ripple/app/misc/impl/ValidatorKeys.cpp | 15 +- src/ripple/app/misc/impl/ValidatorList.cpp | 383 +- src/ripple/app/misc/impl/ValidatorSite.cpp | 426 +- src/ripple/app/paths/AccountCurrencies.cpp | 42 +- src/ripple/app/paths/AccountCurrencies.h | 10 +- src/ripple/app/paths/Credit.cpp | 41 +- src/ripple/app/paths/Credit.h | 10 +- src/ripple/app/paths/Flow.cpp | 149 +- src/ripple/app/paths/Flow.h | 19 +- src/ripple/app/paths/NodeDirectory.h | 38 +- src/ripple/app/paths/PathRequest.cpp | 393 +- src/ripple/app/paths/PathRequest.h | 94 +- src/ripple/app/paths/PathRequests.cpp | 159 +- src/ripple/app/paths/PathRequests.h | 65 +- src/ripple/app/paths/Pathfinder.cpp | 843 +- src/ripple/app/paths/Pathfinder.h | 64 +- src/ripple/app/paths/RippleLineCache.cpp | 18 +- src/ripple/app/paths/RippleLineCache.h | 39 +- src/ripple/app/paths/RippleState.cpp | 69 +- src/ripple/app/paths/RippleState.h | 79 +- src/ripple/app/paths/Tuning.h | 2 +- src/ripple/app/paths/Types.h | 6 +- src/ripple/app/paths/impl/AmountSpec.h | 93 +- src/ripple/app/paths/impl/BookStep.cpp | 674 +- src/ripple/app/paths/impl/DirectStep.cpp | 590 +- src/ripple/app/paths/impl/FlatSets.h | 3 +- src/ripple/app/paths/impl/FlowDebugInfo.h | 279 +- src/ripple/app/paths/impl/PaySteps.cpp | 382 +- src/ripple/app/paths/impl/StepChecks.h | 36 +- src/ripple/app/paths/impl/Steps.h | 250 +- src/ripple/app/paths/impl/StrandFlow.h | 341 +- src/ripple/app/paths/impl/XRPEndpointStep.cpp | 231 +- src/ripple/app/tx/apply.h | 40 +- src/ripple/app/tx/applySteps.h | 64 +- src/ripple/app/tx/impl/ApplyContext.cpp | 83 +- src/ripple/app/tx/impl/ApplyContext.h | 49 +- src/ripple/app/tx/impl/BookTip.cpp | 27 +- src/ripple/app/tx/impl/BookTip.h | 8 +- src/ripple/app/tx/impl/CancelCheck.cpp | 54 +- src/ripple/app/tx/impl/CancelCheck.h | 21 +- src/ripple/app/tx/impl/CancelOffer.cpp | 38 +- src/ripple/app/tx/impl/CancelOffer.h | 19 +- src/ripple/app/tx/impl/CancelTicket.cpp | 44 +- src/ripple/app/tx/impl/CancelTicket.h | 16 +- src/ripple/app/tx/impl/CashCheck.cpp | 177 +- src/ripple/app/tx/impl/CashCheck.h | 21 +- src/ripple/app/tx/impl/Change.cpp | 114 +- src/ripple/app/tx/impl/Change.h | 37 +- src/ripple/app/tx/impl/CreateCheck.cpp | 134 +- src/ripple/app/tx/impl/CreateCheck.h | 21 +- src/ripple/app/tx/impl/CreateOffer.cpp | 966 ++- src/ripple/app/tx/impl/CreateOffer.h | 77 +- src/ripple/app/tx/impl/CreateTicket.cpp | 71 +- src/ripple/app/tx/impl/CreateTicket.h | 16 +- src/ripple/app/tx/impl/DeleteAccount.cpp | 207 +- src/ripple/app/tx/impl/DeleteAccount.h | 33 +- src/ripple/app/tx/impl/DepositPreauth.cpp | 126 +- src/ripple/app/tx/impl/DepositPreauth.h | 30 +- src/ripple/app/tx/impl/Escrow.cpp | 186 +- src/ripple/app/tx/impl/Escrow.h | 48 +- src/ripple/app/tx/impl/InvariantCheck.cpp | 180 +- src/ripple/app/tx/impl/InvariantCheck.h | 12 +- src/ripple/app/tx/impl/Offer.h | 177 +- src/ripple/app/tx/impl/OfferStream.cpp | 130 +- src/ripple/app/tx/impl/OfferStream.h | 52 +- src/ripple/app/tx/impl/PayChan.cpp | 215 +- src/ripple/app/tx/impl/PayChan.h | 43 +- src/ripple/app/tx/impl/Payment.cpp | 232 +- src/ripple/app/tx/impl/Payment.h | 22 +- src/ripple/app/tx/impl/SetAccount.cpp | 181 +- src/ripple/app/tx/impl/SetAccount.h | 22 +- src/ripple/app/tx/impl/SetRegularKey.cpp | 54 +- src/ripple/app/tx/impl/SetRegularKey.h | 27 +- src/ripple/app/tx/impl/SetSignerList.cpp | 201 +- src/ripple/app/tx/impl/SetSignerList.h | 75 +- src/ripple/app/tx/impl/SetTrust.cpp | 330 +- src/ripple/app/tx/impl/SetTrust.h | 22 +- src/ripple/app/tx/impl/SignerEntries.cpp | 32 +- src/ripple/app/tx/impl/SignerEntries.h | 33 +- src/ripple/app/tx/impl/Taker.cpp | 602 +- src/ripple/app/tx/impl/Taker.h | 184 +- src/ripple/app/tx/impl/Transactor.cpp | 355 +- src/ripple/app/tx/impl/Transactor.h | 113 +- src/ripple/app/tx/impl/apply.cpp | 90 +- src/ripple/app/tx/impl/applySteps.cpp | 551 +- src/ripple/basics/Archive.h | 2 +- src/ripple/basics/BasicConfig.h | 135 +- src/ripple/basics/Blob.h | 4 +- src/ripple/basics/Buffer.h | 58 +- src/ripple/basics/ByteUtilities.h | 30 +- src/ripple/basics/CompressionAlgorithms.h | 60 +- src/ripple/basics/CountedObject.h | 80 +- src/ripple/basics/DecayingSample.h | 45 +- src/ripple/basics/FeeUnits.h | 343 +- src/ripple/basics/FileUtilities.h | 15 +- src/ripple/basics/IOUAmount.h | 46 +- src/ripple/basics/KeyCache.h | 187 +- src/ripple/basics/LocalValue.h | 44 +- src/ripple/basics/Log.h | 153 +- src/ripple/basics/PerfLog.h | 58 +- src/ripple/basics/RangeSet.h | 63 +- src/ripple/basics/Resolver.h | 29 +- src/ripple/basics/ResolverAsio.h | 7 +- src/ripple/basics/Slice.h | 69 +- src/ripple/basics/StringUtilities.h | 52 +- src/ripple/basics/Sustain.h | 11 +- src/ripple/basics/TaggedCache.h | 354 +- src/ripple/basics/ToString.h | 17 +- src/ripple/basics/UnorderedContainers.h | 111 +- src/ripple/basics/UptimeClock.h | 19 +- src/ripple/basics/XRPAmount.h | 135 +- src/ripple/basics/algorithm.h | 45 +- src/ripple/basics/base64.h | 12 +- src/ripple/basics/base_uint.h | 379 +- src/ripple/basics/chrono.h | 21 +- src/ripple/basics/contract.h | 33 +- src/ripple/basics/hardened_hash.h | 18 +- src/ripple/basics/impl/Archive.cpp | 39 +- src/ripple/basics/impl/BasicConfig.cpp | 102 +- src/ripple/basics/impl/CountedObject.cpp | 40 +- src/ripple/basics/impl/FileUtilities.cpp | 24 +- src/ripple/basics/impl/IOUAmount.cpp | 156 +- src/ripple/basics/impl/Log.cpp | 287 +- src/ripple/basics/impl/PerfLogImp.cpp | 100 +- src/ripple/basics/impl/PerfLogImp.h | 128 +- src/ripple/basics/impl/ResolverAsio.cpp | 282 +- src/ripple/basics/impl/StringUtilities.cpp | 50 +- src/ripple/basics/impl/Sustain.cpp | 93 +- src/ripple/basics/impl/UptimeClock.cpp | 35 +- src/ripple/basics/impl/base64.cpp | 137 +- src/ripple/basics/impl/contract.cpp | 18 +- src/ripple/basics/impl/make_SSLContext.cpp | 271 +- src/ripple/basics/impl/mulDiv.cpp | 11 +- src/ripple/basics/impl/strHex.cpp | 18 +- src/ripple/basics/make_SSLContext.h | 8 +- src/ripple/basics/mulDiv.h | 5 +- src/ripple/basics/qalloc.h | 196 +- src/ripple/basics/random.h | 104 +- src/ripple/basics/safe_cast.h | 96 +- src/ripple/basics/strHex.h | 33 +- src/ripple/basics/tagged_integer.h | 35 +- src/ripple/beast/asio/io_latency_probe.h | 155 +- src/ripple/beast/clock/abstract_clock.h | 12 +- src/ripple/beast/clock/basic_seconds_clock.h | 80 +- src/ripple/beast/clock/manual_clock.h | 20 +- src/ripple/beast/container/aged_container.h | 5 +- .../beast/container/aged_container_utility.h | 21 +- src/ripple/beast/container/aged_map.h | 9 +- src/ripple/beast/container/aged_multimap.h | 9 +- src/ripple/beast/container/aged_multiset.h | 9 +- src/ripple/beast/container/aged_set.h | 9 +- .../beast/container/aged_unordered_map.h | 18 +- .../beast/container/aged_unordered_multimap.h | 18 +- .../beast/container/aged_unordered_multiset.h | 18 +- .../beast/container/aged_unordered_set.h | 18 +- .../detail/aged_associative_container.h | 12 +- .../detail/aged_container_iterator.h | 105 +- .../container/detail/aged_ordered_container.h | 2113 +++-- .../detail/aged_unordered_container.h | 3521 +++++--- .../detail/empty_base_optimization.h | 66 +- src/ripple/beast/core/CurrentThreadName.cpp | 44 +- src/ripple/beast/core/CurrentThreadName.h | 10 +- src/ripple/beast/core/LexicalCast.h | 136 +- src/ripple/beast/core/List.h | 249 +- src/ripple/beast/core/LockFreeStack.h | 198 +- src/ripple/beast/core/SemanticVersion.cpp | 191 +- src/ripple/beast/core/SemanticVersion.h | 49 +- src/ripple/beast/crypto/detail/mac_facade.h | 32 +- .../beast/crypto/detail/ripemd_context.h | 492 +- src/ripple/beast/crypto/detail/sha2_context.h | 315 +- src/ripple/beast/crypto/ripemd.h | 8 +- src/ripple/beast/crypto/secure_erase.h | 30 +- src/ripple/beast/crypto/sha2.h | 14 +- src/ripple/beast/hash/endian.h | 20 +- src/ripple/beast/hash/hash_append.h | 317 +- src/ripple/beast/hash/impl/xxhash.cpp | 576 +- src/ripple/beast/hash/impl/xxhash.h | 88 +- src/ripple/beast/hash/meta.h | 44 +- src/ripple/beast/hash/tests/hash_metrics.h | 75 +- src/ripple/beast/hash/uhash.h | 4 +- src/ripple/beast/hash/xxhasher.h | 34 +- src/ripple/beast/insight/Collector.h | 62 +- src/ripple/beast/insight/Counter.h | 39 +- src/ripple/beast/insight/CounterImpl.h | 12 +- src/ripple/beast/insight/Event.h | 24 +- src/ripple/beast/insight/EventImpl.h | 12 +- src/ripple/beast/insight/Gauge.h | 57 +- src/ripple/beast/insight/GaugeImpl.h | 15 +- src/ripple/beast/insight/Group.h | 9 +- src/ripple/beast/insight/Groups.h | 16 +- src/ripple/beast/insight/Hook.h | 20 +- src/ripple/beast/insight/HookImpl.h | 11 +- src/ripple/beast/insight/Insight.h | 2 +- src/ripple/beast/insight/Meter.h | 40 +- src/ripple/beast/insight/MeterImpl.h | 12 +- src/ripple/beast/insight/NullCollector.h | 7 +- src/ripple/beast/insight/StatsDCollector.h | 14 +- src/ripple/beast/insight/impl/Collector.cpp | 2 +- src/ripple/beast/insight/impl/Groups.cpp | 77 +- src/ripple/beast/insight/impl/Hook.cpp | 2 +- src/ripple/beast/insight/impl/Metric.cpp | 4 +- .../beast/insight/impl/NullCollector.cpp | 62 +- .../beast/insight/impl/StatsDCollector.cpp | 579 +- src/ripple/beast/net/IPAddress.h | 74 +- src/ripple/beast/net/IPAddressConversion.h | 46 +- src/ripple/beast/net/IPAddressV4.h | 15 +- src/ripple/beast/net/IPAddressV6.h | 12 +- src/ripple/beast/net/IPEndpoint.h | 179 +- .../beast/net/impl/IPAddressConversion.cpp | 22 +- src/ripple/beast/net/impl/IPAddressV4.cpp | 29 +- src/ripple/beast/net/impl/IPAddressV6.cpp | 22 +- src/ripple/beast/net/impl/IPEndpoint.cpp | 76 +- src/ripple/beast/rfc2616.h | 223 +- src/ripple/beast/type_name.h | 28 +- src/ripple/beast/unit_test.h | 3 +- src/ripple/beast/utility/Journal.h | 286 +- src/ripple/beast/utility/PropertyStream.h | 313 +- src/ripple/beast/utility/WrappedSink.h | 44 +- src/ripple/beast/utility/Zero.h | 48 +- src/ripple/beast/utility/hash_pair.h | 36 +- src/ripple/beast/utility/maybe_const.h | 11 +- src/ripple/beast/utility/rngfill.h | 26 +- .../beast/utility/src/beast_Journal.cpp | 83 +- .../utility/src/beast_PropertyStream.cpp | 395 +- src/ripple/beast/utility/temp_dir.h | 18 +- src/ripple/beast/xor_shift_engine.h | 42 +- src/ripple/conditions/Condition.h | 36 +- src/ripple/conditions/Fulfillment.h | 63 +- src/ripple/conditions/impl/Condition.cpp | 73 +- src/ripple/conditions/impl/Fulfillment.cpp | 75 +- src/ripple/conditions/impl/PreimageSha256.h | 27 +- src/ripple/conditions/impl/error.cpp | 106 +- src/ripple/conditions/impl/error.h | 16 +- src/ripple/conditions/impl/utils.h | 40 +- src/ripple/consensus/Consensus.h | 56 +- src/ripple/consensus/ConsensusParms.h | 27 +- src/ripple/consensus/ConsensusProposal.h | 2 +- src/ripple/consensus/DisputedTx.h | 11 +- src/ripple/consensus/LedgerTiming.h | 18 +- src/ripple/consensus/LedgerTrie.h | 9 +- src/ripple/consensus/Validations.h | 24 +- src/ripple/core/ClosureCounter.h | 94 +- src/ripple/core/Config.h | 139 +- src/ripple/core/ConfigSections.h | 96 +- src/ripple/core/Coro.ipp | 85 +- src/ripple/core/DatabaseCon.h | 57 +- src/ripple/core/Job.h | 137 +- src/ripple/core/JobQueue.h | 206 +- src/ripple/core/JobTypeData.h | 59 +- src/ripple/core/JobTypeInfo.h | 48 +- src/ripple/core/JobTypes.h | 157 +- src/ripple/core/LoadEvent.h | 19 +- src/ripple/core/LoadMonitor.h | 45 +- src/ripple/core/SociDB.h | 66 +- src/ripple/core/Stoppable.h | 132 +- src/ripple/core/TimeKeeper.h | 32 +- src/ripple/core/impl/Config.cpp | 462 +- src/ripple/core/impl/DatabaseCon.cpp | 23 +- src/ripple/core/impl/Job.cpp | 81 +- src/ripple/core/impl/JobQueue.cpp | 302 +- src/ripple/core/impl/LoadEvent.cpp | 42 +- src/ripple/core/impl/LoadMonitor.cpp | 90 +- src/ripple/core/impl/SNTPClock.cpp | 318 +- src/ripple/core/impl/SNTPClock.h | 18 +- src/ripple/core/impl/SociDB.cpp | 204 +- src/ripple/core/impl/Stoppable.cpp | 138 +- src/ripple/core/impl/TimeKeeper.cpp | 40 +- src/ripple/core/impl/Workers.cpp | 120 +- src/ripple/core/impl/Workers.h | 96 +- src/ripple/core/impl/semaphore.h | 21 +- src/ripple/crypto/GenerateDeterministicKey.h | 19 +- src/ripple/crypto/RFC1751.h | 31 +- src/ripple/crypto/csprng.h | 32 +- .../crypto/impl/GenerateDeterministicKey.cpp | 107 +- src/ripple/crypto/impl/RFC1751.cpp | 599 +- src/ripple/crypto/impl/csprng.cpp | 57 +- src/ripple/crypto/impl/ec_key.cpp | 24 +- src/ripple/crypto/impl/ec_key.h | 28 +- src/ripple/crypto/impl/openssl.cpp | 95 +- src/ripple/crypto/impl/openssl.h | 153 +- src/ripple/json/JsonPropertyStream.h | 78 +- src/ripple/json/Object.h | 179 +- src/ripple/json/Output.h | 18 +- src/ripple/json/Writer.h | 84 +- src/ripple/json/impl/JsonPropertyStream.cpp | 125 +- src/ripple/json/impl/Object.cpp | 206 +- src/ripple/json/impl/Output.cpp | 129 +- src/ripple/json/impl/Writer.cpp | 229 +- src/ripple/json/impl/json_assert.h | 9 +- src/ripple/json/impl/json_reader.cpp | 828 +- src/ripple/json/impl/json_value.cpp | 1084 ++- src/ripple/json/impl/json_valueiterator.cpp | 99 +- src/ripple/json/impl/json_writer.cpp | 689 +- src/ripple/json/impl/to_string.cpp | 15 +- src/ripple/json/json_errors.h | 8 +- src/ripple/json/json_forwards.h | 8 +- src/ripple/json/json_reader.h | 181 +- src/ripple/json/json_value.h | 505 +- src/ripple/json/json_writer.h | 167 +- src/ripple/json/to_string.h | 15 +- src/ripple/ledger/ApplyView.h | 128 +- src/ripple/ledger/ApplyViewImpl.h | 39 +- src/ripple/ledger/BookDirs.h | 30 +- src/ripple/ledger/CachedSLEs.h | 38 +- src/ripple/ledger/CachedView.h | 56 +- src/ripple/ledger/CashDiff.h | 89 +- src/ripple/ledger/Directory.h | 26 +- src/ripple/ledger/OpenView.h | 94 +- src/ripple/ledger/PaymentSandbox.h | 101 +- src/ripple/ledger/RawView.h | 38 +- src/ripple/ledger/ReadView.h | 189 +- src/ripple/ledger/Sandbox.h | 23 +- src/ripple/ledger/TxMeta.h | 120 +- src/ripple/ledger/View.h | 258 +- src/ripple/ledger/detail/ApplyStateTable.h | 119 +- src/ripple/ledger/detail/ApplyViewBase.h | 70 +- src/ripple/ledger/detail/RawStateTable.h | 57 +- src/ripple/ledger/detail/ReadViewFwdRange.h | 70 +- src/ripple/ledger/detail/ReadViewFwdRange.ipp | 88 +- src/ripple/ledger/impl/ApplyStateTable.cpp | 575 +- src/ripple/ledger/impl/ApplyView.cpp | 103 +- src/ripple/ledger/impl/ApplyViewBase.cpp | 80 +- src/ripple/ledger/impl/ApplyViewImpl.cpp | 27 +- src/ripple/ledger/impl/BookDirs.cpp | 41 +- src/ripple/ledger/impl/CachedSLEs.cpp | 14 +- src/ripple/ledger/impl/CachedView.cpp | 14 +- src/ripple/ledger/impl/CashDiff.cpp | 575 +- src/ripple/ledger/impl/Directory.cpp | 25 +- src/ripple/ledger/impl/OpenView.cpp | 142 +- src/ripple/ledger/impl/PaymentSandbox.cpp | 141 +- src/ripple/ledger/impl/RawStateTable.cpp | 224 +- src/ripple/ledger/impl/ReadView.cpp | 73 +- src/ripple/ledger/impl/TxMeta.cpp | 221 +- src/ripple/ledger/impl/View.cpp | 1148 +-- src/ripple/net/AutoSocket.h | 236 +- src/ripple/net/HTTPClient.h | 44 +- src/ripple/net/HTTPClientSSLContext.h | 110 +- src/ripple/net/InfoSub.h | 163 +- src/ripple/net/RPCCall.h | 38 +- src/ripple/net/RPCErr.h | 9 +- src/ripple/net/RPCSub.h | 24 +- src/ripple/net/RegisterSSLCerts.h | 2 +- src/ripple/net/SSLHTTPDownloader.h | 32 +- src/ripple/net/impl/DatabaseDownloader.cpp | 38 +- src/ripple/net/impl/HTTPClient.cpp | 464 +- src/ripple/net/impl/InfoSub.cpp | 80 +- src/ripple/net/impl/RPCCall.cpp | 1148 +-- src/ripple/net/impl/RPCErr.cpp | 12 +- src/ripple/net/impl/RPCSub.cpp | 185 +- src/ripple/net/impl/RegisterSSLCerts.cpp | 12 +- src/ripple/net/impl/SSLHTTPDownloader.cpp | 142 +- src/ripple/nodestore/Backend.h | 45 +- src/ripple/nodestore/Database.h | 147 +- src/ripple/nodestore/DatabaseRotating.h | 19 +- src/ripple/nodestore/DatabaseShard.h | 77 +- src/ripple/nodestore/DummyScheduler.h | 20 +- src/ripple/nodestore/Factory.h | 22 +- src/ripple/nodestore/Manager.h | 67 +- src/ripple/nodestore/NodeObject.h | 39 +- src/ripple/nodestore/Scheduler.h | 17 +- src/ripple/nodestore/Task.h | 7 +- src/ripple/nodestore/Types.h | 14 +- src/ripple/nodestore/VisitCallback.h | 7 +- .../nodestore/backend/MemoryFactory.cpp | 70 +- src/ripple/nodestore/backend/NuDBFactory.cpp | 197 +- src/ripple/nodestore/backend/NullFactory.cpp | 31 +- .../nodestore/backend/RocksDBFactory.cpp | 230 +- src/ripple/nodestore/impl/BatchWriter.cpp | 75 +- src/ripple/nodestore/impl/BatchWriter.h | 31 +- src/ripple/nodestore/impl/Database.cpp | 149 +- src/ripple/nodestore/impl/DatabaseNodeImp.cpp | 19 +- src/ripple/nodestore/impl/DatabaseNodeImp.h | 36 +- .../nodestore/impl/DatabaseRotatingImp.cpp | 34 +- .../nodestore/impl/DatabaseRotatingImp.h | 61 +- .../nodestore/impl/DatabaseShardImp.cpp | 459 +- src/ripple/nodestore/impl/DatabaseShardImp.h | 46 +- src/ripple/nodestore/impl/DecodedBlob.cpp | 37 +- src/ripple/nodestore/impl/DecodedBlob.h | 15 +- src/ripple/nodestore/impl/DummyScheduler.cpp | 12 +- src/ripple/nodestore/impl/EncodedBlob.cpp | 19 +- src/ripple/nodestore/impl/EncodedBlob.h | 16 +- src/ripple/nodestore/impl/ManagerImp.cpp | 59 +- src/ripple/nodestore/impl/ManagerImp.h | 24 +- src/ripple/nodestore/impl/NodeObject.cpp | 23 +- src/ripple/nodestore/impl/Shard.cpp | 476 +- src/ripple/nodestore/impl/Shard.h | 42 +- src/ripple/nodestore/impl/TaskQueue.cpp | 8 +- src/ripple/nodestore/impl/TaskQueue.h | 13 +- src/ripple/nodestore/impl/Tuning.h | 12 +- src/ripple/nodestore/impl/codec.h | 322 +- src/ripple/nodestore/impl/varint.h | 57 +- src/ripple/overlay/Cluster.h | 31 +- src/ripple/overlay/ClusterNode.h | 29 +- src/ripple/overlay/Compression.h | 57 +- src/ripple/overlay/Message.h | 40 +- src/ripple/overlay/Overlay.h | 192 +- src/ripple/overlay/Peer.h | 69 +- src/ripple/overlay/PeerReservationTable.h | 17 +- src/ripple/overlay/PeerSet.h | 84 +- src/ripple/overlay/impl/Cluster.cpp | 67 +- src/ripple/overlay/impl/ConnectAttempt.cpp | 266 +- src/ripple/overlay/impl/ConnectAttempt.h | 71 +- src/ripple/overlay/impl/Handshake.cpp | 101 +- src/ripple/overlay/impl/Handshake.h | 20 +- src/ripple/overlay/impl/Message.cpp | 65 +- src/ripple/overlay/impl/OverlayImpl.cpp | 640 +- src/ripple/overlay/impl/OverlayImpl.h | 240 +- src/ripple/overlay/impl/PeerImp.cpp | 1793 ++-- src/ripple/overlay/impl/PeerImp.h | 385 +- .../overlay/impl/PeerReservationTable.cpp | 6 +- src/ripple/overlay/impl/PeerSet.cpp | 108 +- src/ripple/overlay/impl/ProtocolMessage.h | 233 +- src/ripple/overlay/impl/ProtocolVersion.cpp | 56 +- src/ripple/overlay/impl/ProtocolVersion.h | 8 +- src/ripple/overlay/impl/TrafficCount.cpp | 108 +- src/ripple/overlay/impl/TrafficCount.h | 148 +- src/ripple/overlay/impl/Tuning.h | 39 +- src/ripple/overlay/impl/ZeroCopyStream.h | 74 +- src/ripple/overlay/make_Overlay.h | 14 +- src/ripple/overlay/predicates.h | 94 +- src/ripple/peerfinder/PeerfinderManager.h | 117 +- src/ripple/peerfinder/Slot.h | 43 +- src/ripple/peerfinder/impl/Bootcache.cpp | 230 +- src/ripple/peerfinder/impl/Bootcache.h | 100 +- src/ripple/peerfinder/impl/Checker.h | 92 +- src/ripple/peerfinder/impl/Counts.h | 207 +- src/ripple/peerfinder/impl/Endpoint.cpp | 15 +- src/ripple/peerfinder/impl/Fixed.h | 27 +- src/ripple/peerfinder/impl/Handouts.h | 174 +- src/ripple/peerfinder/impl/Livecache.h | 402 +- src/ripple/peerfinder/impl/Logic.h | 790 +- .../peerfinder/impl/PeerfinderConfig.cpp | 53 +- .../peerfinder/impl/PeerfinderManager.cpp | 179 +- src/ripple/peerfinder/impl/Reporting.h | 4 +- src/ripple/peerfinder/impl/SlotImp.cpp | 98 +- src/ripple/peerfinder/impl/SlotImp.h | 93 +- src/ripple/peerfinder/impl/Source.h | 19 +- src/ripple/peerfinder/impl/SourceStrings.cpp | 36 +- src/ripple/peerfinder/impl/SourceStrings.h | 8 +- src/ripple/peerfinder/impl/Store.h | 16 +- src/ripple/peerfinder/impl/StoreSqdb.h | 327 +- src/ripple/peerfinder/impl/Tuning.h | 57 +- src/ripple/peerfinder/impl/iosformat.h | 170 +- src/ripple/peerfinder/make_Manager.h | 13 +- src/ripple/peerfinder/sim/FunctionQueue.h | 47 +- src/ripple/peerfinder/sim/GraphAlgorithms.h | 54 +- src/ripple/peerfinder/sim/Message.h | 20 +- src/ripple/peerfinder/sim/NodeSnapshot.h | 6 +- src/ripple/peerfinder/sim/Params.h | 15 +- src/ripple/peerfinder/sim/Predicates.h | 41 +- src/ripple/protocol/AccountID.h | 65 +- src/ripple/protocol/AmountConversions.h | 81 +- src/ripple/protocol/Book.h | 86 +- src/ripple/protocol/BuildInfo.h | 5 +- src/ripple/protocol/ErrorCodes.h | 276 +- src/ripple/protocol/Feature.h | 157 +- src/ripple/protocol/HashPrefix.h | 38 +- src/ripple/protocol/Indexes.h | 183 +- src/ripple/protocol/InnerObjectFormats.h | 14 +- src/ripple/protocol/Issue.h | 37 +- src/ripple/protocol/KeyType.h | 23 +- src/ripple/protocol/Keylet.h | 11 +- src/ripple/protocol/KnownFormats.h | 68 +- src/ripple/protocol/LedgerFormats.h | 131 +- src/ripple/protocol/PayChan.h | 15 +- src/ripple/protocol/Protocol.h | 4 +- src/ripple/protocol/PublicKey.h | 96 +- src/ripple/protocol/Quality.h | 138 +- src/ripple/protocol/Rate.h | 48 +- src/ripple/protocol/SField.h | 164 +- src/ripple/protocol/SOTemplate.h | 58 +- src/ripple/protocol/STAccount.h | 48 +- src/ripple/protocol/STAmount.h | 280 +- src/ripple/protocol/STArray.h | 140 +- src/ripple/protocol/STBase.h | 79 +- src/ripple/protocol/STBitString.h | 89 +- src/ripple/protocol/STBlob.h | 59 +- src/ripple/protocol/STExchange.h | 113 +- src/ripple/protocol/STInteger.h | 58 +- src/ripple/protocol/STLedgerEntry.h | 62 +- src/ripple/protocol/STObject.h | 663 +- src/ripple/protocol/STParsedJSON.h | 30 +- src/ripple/protocol/STPathSet.h | 249 +- src/ripple/protocol/STTx.h | 115 +- src/ripple/protocol/STValidation.h | 22 +- src/ripple/protocol/STVector256.h | 92 +- src/ripple/protocol/SecretKey.h | 65 +- src/ripple/protocol/Seed.h | 26 +- src/ripple/protocol/Serializer.h | 286 +- src/ripple/protocol/Sign.h | 27 +- src/ripple/protocol/SystemParameters.h | 24 +- src/ripple/protocol/TER.h | 362 +- src/ripple/protocol/TxFlags.h | 89 +- src/ripple/protocol/TxFormats.h | 64 +- src/ripple/protocol/UintTypes.h | 40 +- src/ripple/protocol/digest.h | 103 +- src/ripple/protocol/impl/AccountID.cpp | 67 +- src/ripple/protocol/impl/Book.cpp | 48 +- src/ripple/protocol/impl/BuildInfo.cpp | 24 +- src/ripple/protocol/impl/ErrorCodes.cpp | 207 +- src/ripple/protocol/impl/Feature.cpp | 66 +- src/ripple/protocol/impl/Indexes.cpp | 240 +- .../protocol/impl/InnerObjectFormats.cpp | 24 +- src/ripple/protocol/impl/Issue.cpp | 46 +- src/ripple/protocol/impl/Keylet.cpp | 4 +- src/ripple/protocol/impl/LedgerFormats.cpp | 275 +- src/ripple/protocol/impl/PublicKey.cpp | 132 +- src/ripple/protocol/impl/Quality.cpp | 95 +- src/ripple/protocol/impl/Rate2.cpp | 79 +- src/ripple/protocol/impl/SField.cpp | 398 +- src/ripple/protocol/impl/SOTemplate.cpp | 27 +- src/ripple/protocol/impl/STAccount.cpp | 33 +- src/ripple/protocol/impl/STAmount.cpp | 759 +- src/ripple/protocol/impl/STArray.cpp | 94 +- src/ripple/protocol/impl/STBase.cpp | 58 +- src/ripple/protocol/impl/STBlob.cpp | 17 +- src/ripple/protocol/impl/STInteger.cpp | 96 +- src/ripple/protocol/impl/STLedgerEntry.cpp | 114 +- src/ripple/protocol/impl/STObject.cpp | 510 +- src/ripple/protocol/impl/STParsedJSON.cpp | 1223 +-- src/ripple/protocol/impl/STPathSet.cpp | 125 +- src/ripple/protocol/impl/STTx.cpp | 303 +- src/ripple/protocol/impl/STValidation.cpp | 98 +- src/ripple/protocol/impl/STVar.cpp | 153 +- src/ripple/protocol/impl/STVar.h | 94 +- src/ripple/protocol/impl/STVector256.cpp | 40 +- src/ripple/protocol/impl/SecretKey.cpp | 239 +- src/ripple/protocol/impl/Seed.cpp | 69 +- src/ripple/protocol/impl/Serializer.cpp | 464 +- src/ripple/protocol/impl/Sign.cpp | 38 +- src/ripple/protocol/impl/TER.cpp | 386 +- src/ripple/protocol/impl/TxFormats.cpp | 255 +- src/ripple/protocol/impl/UintTypes.cpp | 63 +- src/ripple/protocol/impl/digest.cpp | 59 +- src/ripple/protocol/impl/secp256k1.h | 7 +- src/ripple/protocol/impl/tokens.cpp | 118 +- src/ripple/protocol/jss.h | 1078 +-- src/ripple/protocol/messages.h | 1 - src/ripple/protocol/tokens.h | 39 +- src/ripple/resource/Charge.h | 26 +- src/ripple/resource/Consumer.h | 45 +- src/ripple/resource/Disposition.h | 13 +- src/ripple/resource/Fees.h | 52 +- src/ripple/resource/Gossip.h | 6 +- src/ripple/resource/ResourceManager.h | 46 +- src/ripple/resource/Types.h | 4 +- src/ripple/resource/impl/Charge.cpp | 27 +- src/ripple/resource/impl/Consumer.cpp | 74 +- src/ripple/resource/impl/Entry.h | 46 +- src/ripple/resource/impl/Fees.cpp | 34 +- src/ripple/resource/impl/Import.h | 11 +- src/ripple/resource/impl/Key.h | 25 +- src/ripple/resource/impl/Kind.h | 11 +- src/ripple/resource/impl/Logic.h | 344 +- src/ripple/resource/impl/ResourceManager.cpp | 104 +- src/ripple/resource/impl/Tuning.h | 18 +- src/ripple/rpc/Context.h | 6 +- src/ripple/rpc/DeliveredAmount.h | 12 +- src/ripple/rpc/GRPCHandlers.h | 13 +- src/ripple/rpc/Output.h | 12 +- src/ripple/rpc/RPCHandler.h | 10 +- src/ripple/rpc/Request.h | 21 +- src/ripple/rpc/Role.h | 33 +- src/ripple/rpc/ServerHandler.h | 22 +- src/ripple/rpc/ShardArchiveHandler.h | 41 +- src/ripple/rpc/Status.h | 75 +- src/ripple/rpc/handlers/AccountChannels.cpp | 103 +- .../rpc/handlers/AccountCurrenciesHandler.cpp | 57 +- src/ripple/rpc/handlers/AccountInfo.cpp | 58 +- src/ripple/rpc/handlers/AccountLines.cpp | 163 +- src/ripple/rpc/handlers/AccountObjects.cpp | 80 +- src/ripple/rpc/handlers/AccountOffers.cpp | 104 +- src/ripple/rpc/handlers/AccountTx.cpp | 17 +- src/ripple/rpc/handlers/AccountTxOld.cpp | 174 +- src/ripple/rpc/handlers/AccountTxSwitch.cpp | 11 +- src/ripple/rpc/handlers/BlackList.cpp | 5 +- src/ripple/rpc/handlers/BookOffers.cpp | 155 +- src/ripple/rpc/handlers/CanDelete.cpp | 35 +- src/ripple/rpc/handlers/Connect.cpp | 29 +- src/ripple/rpc/handlers/ConsensusInfo.cpp | 9 +- src/ripple/rpc/handlers/CrawlShards.cpp | 12 +- src/ripple/rpc/handlers/DepositAuthorized.cpp | 50 +- src/ripple/rpc/handlers/DownloadShard.cpp | 76 +- src/ripple/rpc/handlers/Feature1.cpp | 36 +- src/ripple/rpc/handlers/Fee1.cpp | 26 +- src/ripple/rpc/handlers/FetchInfo.cpp | 10 +- src/ripple/rpc/handlers/GatewayBalances.cpp | 102 +- src/ripple/rpc/handlers/GetCounts.cpp | 61 +- src/ripple/rpc/handlers/GetCounts.h | 5 +- src/ripple/rpc/handlers/Handlers.h | 200 +- src/ripple/rpc/handlers/LedgerAccept.cpp | 11 +- .../rpc/handlers/LedgerCleanerHandler.cpp | 10 +- src/ripple/rpc/handlers/LedgerClosed.cpp | 11 +- src/ripple/rpc/handlers/LedgerCurrent.cpp | 7 +- src/ripple/rpc/handlers/LedgerData.cpp | 43 +- src/ripple/rpc/handlers/LedgerEntry.cpp | 184 +- src/ripple/rpc/handlers/LedgerHandler.cpp | 46 +- src/ripple/rpc/handlers/LedgerHandler.h | 49 +- src/ripple/rpc/handlers/LedgerHeader.cpp | 16 +- src/ripple/rpc/handlers/LedgerRequest.cpp | 59 +- src/ripple/rpc/handlers/LogLevel.cpp | 31 +- src/ripple/rpc/handlers/LogRotate.cpp | 7 +- src/ripple/rpc/handlers/Manifest.cpp | 7 +- src/ripple/rpc/handlers/NoRippleCheck.cpp | 111 +- src/ripple/rpc/handlers/OwnerInfo.cpp | 37 +- src/ripple/rpc/handlers/PathFind.cpp | 37 +- src/ripple/rpc/handlers/PayChanClaim.cpp | 60 +- src/ripple/rpc/handlers/Peers.cpp | 42 +- src/ripple/rpc/handlers/Ping.cpp | 12 +- src/ripple/rpc/handlers/Print.cpp | 14 +- src/ripple/rpc/handlers/Random.cpp | 18 +- src/ripple/rpc/handlers/Reservations.cpp | 2 +- src/ripple/rpc/handlers/RipplePathFind.cpp | 47 +- src/ripple/rpc/handlers/ServerInfo.cpp | 14 +- src/ripple/rpc/handlers/ServerState.cpp | 14 +- src/ripple/rpc/handlers/SignFor.cpp | 27 +- src/ripple/rpc/handlers/SignHandler.cpp | 32 +- src/ripple/rpc/handlers/Stop.cpp | 9 +- src/ripple/rpc/handlers/Submit.cpp | 139 +- src/ripple/rpc/handlers/SubmitMultiSigned.cpp | 11 +- src/ripple/rpc/handlers/Subscribe.cpp | 247 +- src/ripple/rpc/handlers/TransactionEntry.cpp | 28 +- src/ripple/rpc/handlers/Tx.cpp | 49 +- src/ripple/rpc/handlers/TxHistory.cpp | 46 +- src/ripple/rpc/handlers/UnlList.cpp | 22 +- src/ripple/rpc/handlers/Unsubscribe.cpp | 138 +- src/ripple/rpc/handlers/ValidationCreate.cpp | 36 +- src/ripple/rpc/handlers/ValidatorInfo.cpp | 9 +- src/ripple/rpc/handlers/Version.h | 25 +- src/ripple/rpc/handlers/WalletPropose.cpp | 60 +- src/ripple/rpc/handlers/WalletPropose.h | 5 +- src/ripple/rpc/impl/Handler.cpp | 252 +- src/ripple/rpc/impl/Handler.h | 109 +- src/ripple/rpc/impl/LegacyPathFind.cpp | 17 +- src/ripple/rpc/impl/LegacyPathFind.h | 13 +- src/ripple/rpc/impl/RPCHandler.cpp | 103 +- src/ripple/rpc/impl/RPCHelpers.cpp | 379 +- src/ripple/rpc/impl/RPCHelpers.h | 82 +- src/ripple/rpc/impl/Role.cpp | 110 +- src/ripple/rpc/impl/ServerHandlerImp.cpp | 650 +- src/ripple/rpc/impl/ServerHandlerImp.h | 61 +- src/ripple/rpc/impl/ShardArchiveHandler.cpp | 167 +- src/ripple/rpc/impl/Status.cpp | 40 +- src/ripple/rpc/impl/TransactionSign.cpp | 862 +- src/ripple/rpc/impl/TransactionSign.h | 40 +- src/ripple/rpc/impl/Tuning.h | 11 +- src/ripple/rpc/impl/WSInfoSub.h | 31 +- src/ripple/rpc/json_body.h | 32 +- src/ripple/server/Handoff.h | 7 +- src/ripple/server/Port.h | 25 +- src/ripple/server/Server.h | 17 +- src/ripple/server/Session.h | 73 +- src/ripple/server/SimpleWriter.h | 13 +- src/ripple/server/WSSession.h | 59 +- src/ripple/server/Writer.h | 19 +- src/ripple/server/impl/BaseHTTPPeer.h | 262 +- src/ripple/server/impl/BasePeer.h | 23 +- src/ripple/server/impl/BaseWSPeer.h | 213 +- src/ripple/server/impl/Door.h | 245 +- src/ripple/server/impl/JSONRPCUtil.cpp | 126 +- src/ripple/server/impl/JSONRPCUtil.h | 12 +- src/ripple/server/impl/PlainHTTPPeer.h | 68 +- src/ripple/server/impl/PlainWSPeer.h | 13 +- src/ripple/server/impl/Port.cpp | 144 +- src/ripple/server/impl/SSLHTTPPeer.h | 101 +- src/ripple/server/impl/SSLWSPeer.h | 18 +- src/ripple/server/impl/ServerImpl.h | 83 +- src/ripple/server/impl/io_list.h | 66 +- src/ripple/shamap/Family.h | 45 +- src/ripple/shamap/FullBelowCache.h | 71 +- src/ripple/shamap/SHAMap.h | 534 +- src/ripple/shamap/SHAMapAddNode.h | 139 +- src/ripple/shamap/SHAMapItem.h | 46 +- src/ripple/shamap/SHAMapMissingNode.h | 46 +- src/ripple/shamap/SHAMapNodeID.h | 141 +- src/ripple/shamap/SHAMapSyncFilter.h | 22 +- src/ripple/shamap/SHAMapTreeNode.h | 385 +- src/ripple/shamap/TreeNodeCache.h | 6 +- src/ripple/shamap/impl/SHAMap.cpp | 603 +- src/ripple/shamap/impl/SHAMapDelta.cpp | 195 +- src/ripple/shamap/impl/SHAMapItem.cpp | 17 +- src/ripple/shamap/impl/SHAMapNodeID.cpp | 94 +- src/ripple/shamap/impl/SHAMapSync.cpp | 539 +- src/ripple/shamap/impl/SHAMapTreeNode.cpp | 372 +- src/test/app/AccountDelete_test.cpp | 496 +- src/test/app/AccountTxPaging_test.cpp | 161 +- src/test/app/AmendmentTable_test.cpp | 550 +- src/test/app/Check_test.cpp | 1589 ++-- src/test/app/CrossingLimits_test.cpp | 203 +- src/test/app/DeliverMin_test.cpp | 69 +- src/test/app/DepositAuth_test.cpp | 525 +- src/test/app/Discrepancy_test.cpp | 86 +- src/test/app/Escrow_test.cpp | 1013 ++- src/test/app/FeeVote_test.cpp | 63 +- src/test/app/Flow_test.cpp | 1095 +-- src/test/app/Freeze_test.cpp | 169 +- src/test/app/HashRouter_test.cpp | 21 +- src/test/app/LedgerHistory_test.cpp | 32 +- src/test/app/LedgerLoad_test.cpp | 147 +- src/test/app/LedgerReplay_test.cpp | 15 +- src/test/app/LoadFeeTrack_test.cpp | 41 +- src/test/app/Manifest_test.cpp | 656 +- src/test/app/MultiSign_test.cpp | 1135 +-- src/test/app/OfferStream_test.cpp | 4 +- src/test/app/Offer_test.cpp | 4495 +++++----- src/test/app/OversizeMeta_test.cpp | 35 +- src/test/app/Path_test.cpp | 525 +- src/test/app/PayChan_test.cpp | 1343 +-- src/test/app/PayStrand_test.cpp | 133 +- src/test/app/PseudoTx_test.cpp | 9 +- src/test/app/RCLCensorshipDetector_test.cpp | 76 +- src/test/app/RCLValidations_test.cpp | 70 +- src/test/app/Regression_test.cpp | 121 +- src/test/app/SHAMapStore_test.cpp | 174 +- src/test/app/SetAuth_test.cpp | 37 +- src/test/app/SetRegularKey_test.cpp | 67 +- src/test/app/SetTrust_test.cpp | 133 +- src/test/app/Taker_test.cpp | 975 ++- src/test/app/Ticket_test.cpp | 322 +- src/test/app/Transaction_ordering_test.cpp | 64 +- src/test/app/TrustAndBalance_test.cpp | 404 +- src/test/app/TxQ_test.cpp | 1094 +-- src/test/app/ValidatorKeys_test.cpp | 12 +- src/test/app/ValidatorList_test.cpp | 983 ++- src/test/app/ValidatorSite_test.cpp | 443 +- src/test/app/tx/apply_test.cpp | 51 +- src/test/basics/Buffer_test.cpp | 241 +- src/test/basics/DetectCrash_test.cpp | 14 +- src/test/basics/FeeUnits_test.cpp | 121 +- src/test/basics/FileUtilities_test.cpp | 18 +- src/test/basics/IOUAmount_test.cpp | 253 +- src/test/basics/KeyCache_test.cpp | 79 +- src/test/basics/PerfLog_test.cpp | 658 +- src/test/basics/RangeSet_test.cpp | 3 +- src/test/basics/Slice_test.cpp | 58 +- src/test/basics/StringUtilities_test.cpp | 136 +- src/test/basics/TaggedCache_test.cpp | 61 +- src/test/basics/XRPAmount_test.cpp | 236 +- src/test/basics/base64_test.cpp | 49 +- src/test/basics/base_uint_test.cpp | 49 +- src/test/basics/contract_test.cpp | 7 +- src/test/basics/hardened_hash_test.cpp | 157 +- src/test/basics/mulDiv_test.cpp | 3 +- src/test/basics/tagged_integer_test.cpp | 105 +- src/test/beast/IPEndpointCommon.h | 62 +- src/test/beast/IPEndpoint_test.cpp | 447 +- src/test/beast/LexicalCast_test.cpp | 242 +- src/test/beast/SemanticVersion_test.cpp | 78 +- .../beast/aged_associative_container_test.cpp | 1487 ++-- .../beast/beast_CurrentThreadName_test.cpp | 35 +- src/test/beast/beast_Journal_test.cpp | 15 +- src/test/beast/beast_PropertyStream_test.cpp | 60 +- src/test/beast/beast_Zero_test.cpp | 87 +- src/test/beast/beast_abstract_clock_test.cpp | 57 +- .../beast/beast_basic_seconds_clock_test.cpp | 9 +- .../beast/beast_io_latency_probe_test.cpp | 116 +- src/test/beast/define_print.cpp | 16 +- src/test/conditions/PreimageSha256_test.cpp | 167 +- .../consensus/ByzantineFailureSim_test.cpp | 7 +- src/test/consensus/Consensus_test.cpp | 80 +- .../DistributedValidatorsSim_test.cpp | 121 +- src/test/consensus/LedgerTiming_test.cpp | 56 +- src/test/consensus/ScaleFreeSim_test.cpp | 29 +- src/test/consensus/Validations_test.cpp | 23 +- src/test/core/ClosureCounter_test.cpp | 252 +- src/test/core/Config_test.cpp | 629 +- src/test/core/Coroutine_test.cpp | 90 +- src/test/core/CryptoPRNG_test.cpp | 15 +- src/test/core/JobQueue_test.cpp | 94 +- src/test/core/SociDB_test.cpp | 238 +- src/test/core/Stoppable_test.cpp | 375 +- src/test/core/Workers_test.cpp | 106 +- src/test/csf.h | 2 +- src/test/csf/BasicNetwork.h | 21 +- src/test/csf/BasicNetwork_test.cpp | 5 +- src/test/csf/CollectorRef.h | 36 +- src/test/csf/Digraph.h | 10 +- src/test/csf/Digraph_test.cpp | 23 +- src/test/csf/Histogram.h | 15 +- src/test/csf/Histogram_test.cpp | 8 +- src/test/csf/Peer.h | 118 +- src/test/csf/PeerGroup.h | 76 +- src/test/csf/Proposal.h | 2 +- src/test/csf/Scheduler.h | 11 +- src/test/csf/Scheduler_test.cpp | 23 +- src/test/csf/Sim.h | 29 +- src/test/csf/TrustGraph.h | 25 +- src/test/csf/Tx.h | 18 +- src/test/csf/Validation.h | 17 +- src/test/csf/collectors.h | 396 +- src/test/csf/events.h | 8 +- src/test/csf/impl/Sim.cpp | 18 +- src/test/csf/impl/ledgers.cpp | 31 +- src/test/csf/ledgers.h | 63 +- src/test/csf/random.h | 28 +- src/test/csf/submitters.h | 24 +- src/test/csf/timers.h | 25 +- src/test/json/Object_test.cpp | 182 +- src/test/json/Output_test.cpp | 44 +- src/test/json/TestOutputSuite.h | 24 +- src/test/json/Writer_test.cpp | 251 +- src/test/json/json_value_test.cpp | 1744 ++-- src/test/jtx.h | 6 +- src/test/jtx/AbstractClient.h | 16 +- src/test/jtx/Account.h | 70 +- src/test/jtx/Env.h | 253 +- src/test/jtx/Env_ss.h | 31 +- src/test/jtx/Env_test.cpp | 375 +- src/test/jtx/JSONRPCClient.h | 6 +- src/test/jtx/JTx.h | 48 +- src/test/jtx/ManualTimeKeeper.h | 17 +- src/test/jtx/PathSet.h | 114 +- src/test/jtx/TestSuite.h | 57 +- src/test/jtx/TrustedPublisherServer.h | 164 +- src/test/jtx/WSClient.h | 25 +- src/test/jtx/WSClient_test.cpp | 13 +- src/test/jtx/account_txn_id.h | 21 +- src/test/jtx/acctdelete.h | 10 +- src/test/jtx/amount.h | 193 +- src/test/jtx/balance.h | 29 +- src/test/jtx/basic_prop.h | 32 +- src/test/jtx/check.h | 32 +- src/test/jtx/delivermin.h | 11 +- src/test/jtx/deposit.h | 14 +- src/test/jtx/envconfig.h | 18 +- src/test/jtx/fee.h | 32 +- src/test/jtx/flags.h | 85 +- src/test/jtx/impl/Account.cpp | 56 +- src/test/jtx/impl/Env.cpp | 226 +- src/test/jtx/impl/JSONRPCClient.cpp | 62 +- src/test/jtx/impl/ManualTimeKeeper.cpp | 34 +- src/test/jtx/impl/WSClient.cpp | 123 +- src/test/jtx/impl/account_txn_id.cpp | 6 +- src/test/jtx/impl/acctdelete.cpp | 10 +- src/test/jtx/impl/amount.cpp | 52 +- src/test/jtx/impl/balance.cpp | 25 +- src/test/jtx/impl/check.cpp | 39 +- src/test/jtx/impl/delivermin.cpp | 8 +- src/test/jtx/impl/deposit.cpp | 14 +- src/test/jtx/impl/envconfig.cpp | 29 +- src/test/jtx/impl/fee.cpp | 13 +- src/test/jtx/impl/flags.cpp | 17 +- src/test/jtx/impl/invoice_id.cpp | 6 +- src/test/jtx/impl/jtx_json.cpp | 26 +- src/test/jtx/impl/last_ledger_sequence.cpp | 10 +- src/test/jtx/impl/memo.cpp | 9 +- src/test/jtx/impl/multisign.cpp | 46 +- src/test/jtx/impl/offer.cpp | 17 +- src/test/jtx/impl/owners.cpp | 31 +- src/test/jtx/impl/paths.cpp | 37 +- src/test/jtx/impl/pay.cpp | 14 +- src/test/jtx/impl/quality2.cpp | 32 +- src/test/jtx/impl/rate.cpp | 18 +- src/test/jtx/impl/regkey.cpp | 14 +- src/test/jtx/impl/sendmax.cpp | 8 +- src/test/jtx/impl/seq.cpp | 10 +- src/test/jtx/impl/sig.cpp | 15 +- src/test/jtx/impl/tag.cpp | 8 +- src/test/jtx/impl/ticket.cpp | 19 +- src/test/jtx/impl/trust.cpp | 24 +- src/test/jtx/impl/txflags.cpp | 11 +- src/test/jtx/impl/utility.cpp | 62 +- src/test/jtx/invoice_id.h | 21 +- src/test/jtx/jtx_json.h | 21 +- src/test/jtx/last_ledger_sequence.h | 21 +- src/test/jtx/memo.h | 44 +- src/test/jtx/multisign.h | 61 +- src/test/jtx/noop.h | 11 +- src/test/jtx/offer.h | 17 +- src/test/jtx/owners.h | 37 +- src/test/jtx/paths.h | 30 +- src/test/jtx/pay.h | 11 +- src/test/jtx/prop.h | 13 +- src/test/jtx/quality.h | 16 +- src/test/jtx/rate.h | 11 +- src/test/jtx/regkey.h | 14 +- src/test/jtx/require.h | 30 +- src/test/jtx/requires.h | 6 +- src/test/jtx/sendmax.h | 11 +- src/test/jtx/seq.h | 19 +- src/test/jtx/sig.h | 19 +- src/test/jtx/tag.h | 14 +- src/test/jtx/tags.h | 27 +- src/test/jtx/ter.h | 13 +- src/test/jtx/ticket.h | 53 +- src/test/jtx/trust.h | 15 +- src/test/jtx/txflags.h | 10 +- src/test/jtx/utility.h | 25 +- src/test/ledger/BookDirs_test.cpp | 27 +- src/test/ledger/CashDiff_test.cpp | 51 +- src/test/ledger/Directory_test.cpp | 148 +- src/test/ledger/Invariants_test.cpp | 460 +- src/test/ledger/PaymentSandbox_test.cpp | 366 +- src/test/ledger/PendingSaves_test.cpp | 38 +- src/test/ledger/SkipList_test.cpp | 76 +- src/test/ledger/View_test.cpp | 475 +- src/test/net/SSLHTTPDownloader_test.cpp | 109 +- src/test/nodestore/Backend_test.cpp | 77 +- src/test/nodestore/Basics_test.cpp | 55 +- src/test/nodestore/Database_test.cpp | 153 +- src/test/nodestore/TestBase.h | 133 +- src/test/nodestore/Timing_test.cpp | 446 +- src/test/nodestore/import_test.cpp | 280 +- src/test/nodestore/varint_test.cpp | 40 +- src/test/overlay/ProtocolVersion_test.cpp | 52 +- src/test/overlay/cluster_test.cpp | 195 +- src/test/overlay/compression_test.cpp | 249 +- src/test/overlay/short_read_test.cpp | 109 +- src/test/peerfinder/Livecache_test.cpp | 153 +- src/test/peerfinder/PeerFinder_test.cpp | 61 +- src/test/protocol/InnerObjectFormats_test.cpp | 82 +- src/test/protocol/Issue_test.cpp | 648 +- src/test/protocol/PublicKey_test.cpp | 385 +- src/test/protocol/Quality_test.cpp | 385 +- src/test/protocol/STAccount_test.cpp | 87 +- src/test/protocol/STAmount_test.cpp | 711 +- src/test/protocol/STObject_test.cpp | 593 +- src/test/protocol/STTx_test.cpp | 2843 ++++--- src/test/protocol/STValidation_test.cpp | 101 +- src/test/protocol/SecretKey_test.cpp | 232 +- src/test/protocol/Seed_test.cpp | 267 +- src/test/protocol/TER_test.cpp | 241 +- src/test/protocol/digest_test.cpp | 110 +- src/test/protocol/types_test.cpp | 12 +- src/test/resource/Logic_test.cpp | 183 +- src/test/rpc/AccountCurrencies_test.cpp | 216 +- src/test/rpc/AccountInfo_test.cpp | 297 +- src/test/rpc/AccountLinesRPC_test.cpp | 798 +- src/test/rpc/AccountObjects_test.cpp | 300 +- src/test/rpc/AccountOffers_test.cpp | 215 +- src/test/rpc/AccountSet_test.cpp | 268 +- src/test/rpc/AccountTx_test.cpp | 347 +- src/test/rpc/AmendmentBlocked_test.cpp | 186 +- src/test/rpc/Book_test.cpp | 970 +-- src/test/rpc/DeliveredAmount_test.cpp | 9 +- src/test/rpc/DepositAuthorized_test.cpp | 243 +- src/test/rpc/Feature_test.cpp | 187 +- src/test/rpc/Fee_test.cpp | 3 +- src/test/rpc/GRPCTestClientBase.h | 13 +- src/test/rpc/GatewayBalances_test.cpp | 73 +- src/test/rpc/GetCounts_test.cpp | 59 +- src/test/rpc/JSONRPC_test.cpp | 1624 ++-- src/test/rpc/KeyGeneration_test.cpp | 543 +- src/test/rpc/LedgerClosed_test.cpp | 32 +- src/test/rpc/LedgerData_test.cpp | 369 +- src/test/rpc/LedgerRPC_test.cpp | 811 +- src/test/rpc/LedgerRequestRPC_test.cpp | 316 +- src/test/rpc/ManifestRPC_test.cpp | 54 +- src/test/rpc/NoRippleCheck_test.cpp | 332 +- src/test/rpc/NoRipple_test.cpp | 72 +- src/test/rpc/OwnerInfo_test.cpp | 162 +- src/test/rpc/Peers_test.cpp | 53 +- src/test/rpc/RPCCall_test.cpp | 7472 +++++++---------- src/test/rpc/RPCOverload_test.cpp | 52 +- src/test/rpc/RobustTransaction_test.cpp | 257 +- src/test/rpc/Roles_test.cpp | 38 +- src/test/rpc/ServerInfo_test.cpp | 33 +- src/test/rpc/ShardArchiveHandler_test.cpp | 25 +- src/test/rpc/Status_test.cpp | 168 +- src/test/rpc/Subscribe_test.cpp | 381 +- src/test/rpc/TransactionEntry_test.cpp | 173 +- src/test/rpc/TransactionHistory_test.cpp | 64 +- src/test/rpc/Transaction_test.cpp | 28 +- src/test/rpc/ValidatorInfo_test.cpp | 82 +- src/test/rpc/ValidatorRPC_test.cpp | 54 +- src/test/rpc/Version_test.cpp | 172 +- src/test/server/ServerStatus_test.cpp | 754 +- src/test/server/Server_test.cpp | 379 +- src/test/shamap/FetchPack_test.cpp | 162 +- src/test/shamap/SHAMapSync_test.cpp | 109 +- src/test/shamap/SHAMap_test.cpp | 325 +- src/test/shamap/common.h | 27 +- src/test/unit_test/FileDirGuard.h | 92 +- src/test/unit_test/SuiteJournal.h | 80 +- src/test/unit_test/multi_runner.cpp | 17 +- src/test/unit_test/multi_runner.h | 34 +- 1076 files changed, 86161 insertions(+), 77449 deletions(-) diff --git a/src/ripple/app/consensus/RCLCensorshipDetector.h b/src/ripple/app/consensus/RCLCensorshipDetector.h index 3639039272..f661a80362 100644 --- a/src/ripple/app/consensus/RCLCensorshipDetector.h +++ b/src/ripple/app/consensus/RCLCensorshipDetector.h @@ -37,25 +37,28 @@ public: TxID txid; Sequence seq; - TxIDSeq (TxID const& txid_, Sequence const& seq_) - : txid (txid_) - , seq (seq_) - { } + TxIDSeq(TxID const& txid_, Sequence const& seq_) + : txid(txid_), seq(seq_) + { + } }; - friend bool operator< (TxIDSeq const& lhs, TxIDSeq const& rhs) + friend bool + operator<(TxIDSeq const& lhs, TxIDSeq const& rhs) { if (lhs.txid != rhs.txid) return lhs.txid < rhs.txid; return lhs.seq < rhs.seq; } - friend bool operator< (TxIDSeq const& lhs, TxID const& rhs) + friend bool + operator<(TxIDSeq const& lhs, TxID const& rhs) { return lhs.txid < rhs; } - friend bool operator< (TxID const& lhs, TxIDSeq const& rhs) + friend bool + operator<(TxID const& lhs, TxIDSeq const& rhs) { return lhs < rhs.txid; } @@ -73,17 +76,21 @@ public: @param proposed The set of transactions that we are initially proposing for this round. */ - void propose(TxIDSeqVec proposed) + void + propose(TxIDSeqVec proposed) { // We want to remove any entries that we proposed in a previous round // that did not make it in yet if we are no longer proposing them. // And we also want to preserve the Sequence of entries that we proposed // in the last round and want to propose again. std::sort(proposed.begin(), proposed.end()); - generalized_set_intersection(proposed.begin(), proposed.end(), - tracker_.cbegin(), tracker_.cend(), - [](auto& x, auto const& y) {x.seq = y.seq;}, - [](auto const& x, auto const& y) {return x.txid < y.txid;}); + generalized_set_intersection( + proposed.begin(), + proposed.end(), + tracker_.cbegin(), + tracker_.cend(), + [](auto& x, auto const& y) { x.seq = y.seq; }, + [](auto const& x, auto const& y) { return x.txid < y.txid; }); tracker_ = std::move(proposed); } @@ -101,9 +108,8 @@ public: It must return true for entries that should be removed. */ template - void check( - std::vector accepted, - Predicate&& pred) + void + check(std::vector accepted, Predicate&& pred) { auto acceptTxid = accepted.begin(); auto const ae = accepted.end(); @@ -112,10 +118,13 @@ public: // We want to remove all tracking entries for transactions that were // accepted as well as those which match the predicate. - auto i = remove_if_intersect_or_match(tracker_.begin(), tracker_.end(), - accepted.begin(), accepted.end(), - [&pred](auto const& x) {return pred(x.txid, x.seq);}, - std::less{}); + auto i = remove_if_intersect_or_match( + tracker_.begin(), + tracker_.end(), + accepted.begin(), + accepted.end(), + [&pred](auto const& x) { return pred(x.txid, x.seq); }, + std::less{}); tracker_.erase(i, tracker_.end()); } @@ -124,12 +133,13 @@ public: Typically, this function might be called after we reconnect to the network following an outage, or after we start tracking the network. */ - void reset() + void + reset() { tracker_.clear(); } }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/consensus/RCLConsensus.cpp b/src/ripple/app/consensus/RCLConsensus.cpp index 862be4cfc7..06c6bbec0a 100644 --- a/src/ripple/app/consensus/RCLConsensus.cpp +++ b/src/ripple/app/consensus/RCLConsensus.cpp @@ -77,14 +77,14 @@ RCLConsensus::Adaptor::Adaptor( ValidatorKeys const& validatorKeys, beast::Journal journal) : app_(app) - , feeVote_(std::move(feeVote)) - , ledgerMaster_(ledgerMaster) - , localTxs_(localTxs) - , inboundTransactions_{inboundTransactions} - , j_(journal) - , nodeID_{validatorKeys.nodeID} - , valPublic_{validatorKeys.publicKey} - , valSecret_{validatorKeys.secretKey} + , feeVote_(std::move(feeVote)) + , ledgerMaster_(ledgerMaster) + , localTxs_(localTxs) + , inboundTransactions_{inboundTransactions} + , j_(journal) + , nodeID_{validatorKeys.nodeID} + , valPublic_{validatorKeys.publicKey} + , valSecret_{validatorKeys.secretKey} { } @@ -103,11 +103,12 @@ RCLConsensus::Adaptor::acquireLedger(LedgerHash const& hash) // Tell the ledger acquire system that we need the consensus ledger acquiringLedger_ = hash; - app_.getJobQueue().addJob(jtADVANCE, "getConsensusLedger", - [id = hash, &app = app_](Job&) - { - app.getInboundLedgers().acquire(id, 0, - InboundLedger::Reason::CONSENSUS); + app_.getJobQueue().addJob( + jtADVANCE, + "getConsensusLedger", + [id = hash, &app = app_](Job&) { + app.getInboundLedgers().acquire( + id, 0, InboundLedger::Reason::CONSENSUS); }); } return boost::none; @@ -205,7 +206,7 @@ RCLConsensus::Adaptor::propose(RCLCxPeerPos::Proposal const& proposal) valPublic_, sig); - app_.getHashRouter ().addSuppression (suppression); + app_.getHashRouter().addSuppression(suppression); app_.overlay().send(prop); } @@ -264,7 +265,7 @@ RCLConsensus::Adaptor::getPrevLedger( if (mode != ConsensusMode::wrongLedger) app_.getOPs().consensusViewChange(); - JLOG(j_.debug())<< Json::Compact(app_.getValidations().getJsonTrie()); + JLOG(j_.debug()) << Json::Compact(app_.getValidations().getJsonTrie()); } return netLgr; @@ -289,15 +290,15 @@ RCLConsensus::Adaptor::onClose( auto initialLedger = app_.openLedger().current(); - auto initialSet = std::make_shared( - SHAMapType::TRANSACTION, app_.family()); + auto initialSet = + std::make_shared(SHAMapType::TRANSACTION, app_.family()); initialSet->setUnbacked(); // Build SHAMap containing all transactions in our open ledger for (auto const& tx : initialLedger->txs) { - JLOG(j_.trace()) << "Adding open ledger TX " << - tx.first->getTransactionID(); + JLOG(j_.trace()) << "Adding open ledger TX " + << tx.first->getTransactionID(); Serializer s(2048); tx.first->add(s); initialSet->addItem( @@ -311,11 +312,10 @@ RCLConsensus::Adaptor::onClose( ((prevLedger->info().seq % 256) == 0)) { // previous ledger was flag ledger, add pseudo-transactions - auto const validations = - app_.getValidations().getTrustedForLedger ( - prevLedger->info().parentHash); + auto const validations = app_.getValidations().getTrustedForLedger( + prevLedger->info().parentHash); - if (validations.size() >= app_.validators ().quorum ()) + if (validations.size() >= app_.validators().quorum()) { feeVote_->doVoting(prevLedger, validations, initialSet); app_.getAmendmentTable().doVoting( @@ -332,8 +332,7 @@ RCLConsensus::Adaptor::onClose( RCLCensorshipDetector::TxIDSeqVec proposed; initialSet->visitLeaves( - [&proposed, seq](std::shared_ptr const& item) - { + [&proposed, seq](std::shared_ptr const& item) { proposed.emplace_back(item->key(), seq); }); @@ -361,7 +360,7 @@ RCLConsensus::Adaptor::onForceAccept( NetClock::duration const& closeResolution, ConsensusCloseTimes const& rawCloseTimes, ConsensusMode const& mode, - Json::Value && consensusJson) + Json::Value&& consensusJson) { doAccept( result, @@ -379,12 +378,12 @@ RCLConsensus::Adaptor::onAccept( NetClock::duration const& closeResolution, ConsensusCloseTimes const& rawCloseTimes, ConsensusMode const& mode, - Json::Value && consensusJson) + Json::Value&& consensusJson) { app_.getJobQueue().addJob( jtACCEPT, "acceptLedger", - [=, cj = std::move(consensusJson) ](auto&) mutable { + [=, cj = std::move(consensusJson)](auto&) mutable { // Note that no lock is held or acquired during this job. // This is because generic Consensus guarantees that once a ledger // is accepted, the consensus results and capture by reference state @@ -408,7 +407,7 @@ RCLConsensus::Adaptor::doAccept( NetClock::duration closeResolution, ConsensusCloseTimes const& rawCloseTimes, ConsensusMode const& mode, - Json::Value && consensusJson) + Json::Value&& consensusJson) { prevProposers_ = result.proposers; prevRoundTime_ = result.roundTime.read(); @@ -450,7 +449,7 @@ RCLConsensus::Adaptor::doAccept( // we use the hash of the set. // // FIXME: Use a std::vector and a custom sorter instead of CanonicalTXSet? - CanonicalTXSet retriableTxs{ result.txns.map_->getHash().as_uint256() }; + CanonicalTXSet retriableTxs{result.txns.map_->getHash().as_uint256()}; JLOG(j_.debug()) << "Building canonical tx set: " << retriableTxs.key(); @@ -458,7 +457,8 @@ RCLConsensus::Adaptor::doAccept( { try { - retriableTxs.insert(std::make_shared(SerialIter{item.slice()})); + retriableTxs.insert( + std::make_shared(SerialIter{item.slice()})); JLOG(j_.debug()) << " Tx: " << item.key(); } catch (std::exception const&) @@ -468,8 +468,14 @@ RCLConsensus::Adaptor::doAccept( } } - auto built = buildLCL(prevLedger, retriableTxs, consensusCloseTime, - closeTimeCorrect, closeResolution, result.roundTime.read(), failed); + auto built = buildLCL( + prevLedger, + retriableTxs, + consensusCloseTime, + closeTimeCorrect, + closeResolution, + result.roundTime.read(), + failed); auto const newLCLHash = built.id(); JLOG(j_.debug()) << "Built ledger #" << built.seq() << ": " << newLCLHash; @@ -484,20 +490,20 @@ RCLConsensus::Adaptor::doAccept( { std::vector accepted; - result.txns.map_->visitLeaves ( - [&accepted](std::shared_ptr const& item) - { + result.txns.map_->visitLeaves( + [&accepted](std::shared_ptr const& item) { accepted.push_back(item->key()); }); // Track all the transactions which failed or were marked as retriable for (auto const& r : retriableTxs) - failed.insert (r.first.getTXID()); + failed.insert(r.first.getTXID()); - censorshipDetector_.check(std::move(accepted), - [curr = built.seq(), j = app_.journal("CensorshipDetector"), &failed] - (uint256 const& id, LedgerIndex seq) - { + censorshipDetector_.check( + std::move(accepted), + [curr = built.seq(), + j = app_.journal("CensorshipDetector"), + &failed](uint256 const& id, LedgerIndex seq) { if (failed.count(id)) return true; @@ -508,8 +514,7 @@ RCLConsensus::Adaptor::doAccept( std::ostringstream ss; ss << "Potential Censorship: Eligible tx " << id << ", which we are tracking since ledger " << seq - << " has not been included as of ledger " << curr - << "."; + << " has not been included as of ledger " << curr << "."; JLOG(j.warn()) << ss.str(); } @@ -617,15 +622,16 @@ RCLConsensus::Adaptor::doAccept( // Do these need to exist? assert(ledgerMaster_.getClosedLedger()->info().hash == built.id()); - assert( - app_.openLedger().current()->info().parentHash == built.id()); + assert(app_.openLedger().current()->info().parentHash == built.id()); } //------------------------------------------------------------------------- // we entered the round with the network, // see how close our close time is to other node's // close time reports, and update our clock. - if ((mode == ConsensusMode::proposing || mode == ConsensusMode::observing) && !consensusFail) + if ((mode == ConsensusMode::proposing || + mode == ConsensusMode::observing) && + !consensusFail) { auto closeTime = rawCloseTimes.self; @@ -638,9 +644,8 @@ RCLConsensus::Adaptor::doAccept( for (auto const& [t, v] : rawCloseTimes.peers) { - JLOG(j_.info()) - << std::to_string(v) << " time votes for " - << std::to_string(t.time_since_epoch().count()); + JLOG(j_.info()) << std::to_string(v) << " time votes for " + << std::to_string(t.time_since_epoch().count()); closeCount += v; closeTotal += std::chrono::duration_cast(t.time_since_epoch()) * v; @@ -695,10 +700,9 @@ RCLConsensus::Adaptor::notify( } s.set_firstseq(uMin); s.set_lastseq(uMax); - app_.overlay ().foreach (send_always ( - std::make_shared ( - s, protocol::mtSTATUS_CHANGE))); - JLOG (j_.trace()) << "send status change to peer"; + app_.overlay().foreach ( + send_always(std::make_shared(s, protocol::mtSTATUS_CHANGE))); + JLOG(j_.trace()) << "send status change to peer"; } RCLCxLedger @@ -711,15 +715,21 @@ RCLConsensus::Adaptor::buildLCL( std::chrono::milliseconds roundTime, std::set& failedTxs) { - std::shared_ptr built = [&]() - { + std::shared_ptr built = [&]() { if (auto const replayData = ledgerMaster_.releaseReplay()) { assert(replayData->parent()->info().hash == previousLedger.id()); return buildLedger(*replayData, tapNONE, app_, j_); } - return buildLedger(previousLedger.ledger_, closeTime, closeTimeCorrect, - closeResolution, app_, retriableTxs, failedTxs, j_); + return buildLedger( + previousLedger.ledger_, + closeTime, + closeTimeCorrect, + closeResolution, + app_, + retriableTxs, + failedTxs, + j_); }(); // Update fee computations based on accepted txs @@ -737,7 +747,8 @@ RCLConsensus::Adaptor::buildLCL( } void -RCLConsensus::Adaptor::validate(RCLCxLedger const& ledger, +RCLConsensus::Adaptor::validate( + RCLCxLedger const& ledger, RCLTxSet const& txns, bool proposing) { @@ -762,7 +773,8 @@ RCLConsensus::Adaptor::validate(RCLCxLedger const& ledger, { // Suggest fee changes and new features feeVote_->doValidation(ledger.ledger_, fees); - amendments = app_.getAmendmentTable().doValidation (getEnabledAmendments(*ledger.ledger_)); + amendments = app_.getAmendmentTable().doValidation( + getEnabledAmendments(*ledger.ledger_)); } auto v = std::make_shared( @@ -789,16 +801,16 @@ RCLConsensus::Adaptor::validate(RCLCxLedger const& ledger, } void -RCLConsensus::Adaptor::onModeChange( - ConsensusMode before, - ConsensusMode after) +RCLConsensus::Adaptor::onModeChange(ConsensusMode before, ConsensusMode after) { JLOG(j_.info()) << "Consensus mode change before=" << to_string(before) << ", after=" << to_string(after); // If we were proposing but aren't any longer, we need to reset the // censorship tracking to avoid bogus warnings. - if ((before == ConsensusMode::proposing || before == ConsensusMode::observing) && before != after) + if ((before == ConsensusMode::proposing || + before == ConsensusMode::observing) && + before != after) censorshipDetector_.reset(); mode_ = after; @@ -809,8 +821,8 @@ RCLConsensus::getJson(bool full) const { Json::Value ret; { - std::lock_guard _{mutex_}; - ret = consensus_.getJson(full); + std::lock_guard _{mutex_}; + ret = consensus_.getJson(full); } ret["validating"] = adaptor_.validating(); return ret; @@ -848,7 +860,6 @@ RCLConsensus::gotTxSet(NetClock::time_point const& now, RCLTxSet const& txSet) } } - //! @see Consensus::simulate void @@ -870,13 +881,13 @@ RCLConsensus::peerProposal( } bool -RCLConsensus::Adaptor::preStartRound(RCLCxLedger const & prevLgr) +RCLConsensus::Adaptor::preStartRound(RCLCxLedger const& prevLgr) { // We have a key, we do not want out of sync validations after a restart // and are not amendment blocked. validating_ = valPublic_.size() != 0 && - prevLgr.seq() >= app_.getMaxDisallowedLedger() && - !app_.getOPs().isAmendmentBlocked(); + prevLgr.seq() >= app_.getMaxDisallowedLedger() && + !app_.getOPs().isAmendmentBlocked(); // If we are not running in standalone mode and there's a configured UNL, // check to make sure that it's not expired. @@ -892,8 +903,7 @@ RCLConsensus::Adaptor::preStartRound(RCLCxLedger const & prevLgr) } } - const bool synced = app_.getOPs().getOperatingMode() == - OperatingMode::FULL; + const bool synced = app_.getOPs().getOperatingMode() == OperatingMode::FULL; if (validating_) { @@ -933,7 +943,8 @@ RCLConsensus::Adaptor::getQuorumKeys() const } std::size_t -RCLConsensus::Adaptor::laggards(Ledger_t::Seq const seq, +RCLConsensus::Adaptor::laggards( + Ledger_t::Seq const seq, hash_set& trustedKeys) const { return app_.getValidations().laggards(seq, trustedKeys); @@ -948,7 +959,7 @@ RCLConsensus::Adaptor::validator() const void RCLConsensus::Adaptor::updateOperatingMode(std::size_t const positions) const { - if (! positions && app_.getOPs().isFull()) + if (!positions && app_.getOPs().isFull()) app_.getOPs().setMode(OperatingMode::CONNECTED); } @@ -963,4 +974,4 @@ RCLConsensus::startRound( consensus_.startRound( now, prevLgrId, prevLgr, nowUntrusted, adaptor_.preStartRound(prevLgr)); } -} +} // namespace ripple diff --git a/src/ripple/app/consensus/RCLConsensus.h b/src/ripple/app/consensus/RCLConsensus.h index 61f3531ce1..2a932ed608 100644 --- a/src/ripple/app/consensus/RCLConsensus.h +++ b/src/ripple/app/consensus/RCLConsensus.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED #define RIPPLE_APP_CONSENSUS_RCLCONSENSUS_H_INCLUDED +#include #include #include #include -#include #include #include #include @@ -45,10 +45,11 @@ class LedgerMaster; class ValidatorKeys; /** Manages the generic consensus algorithm for use by the RCL. -*/ + */ class RCLConsensus { - /** Warn for transactions that haven't been included every so many ledgers. */ + /** Warn for transactions that haven't been included every so many ledgers. + */ constexpr static unsigned int censorshipWarnInternal = 15; // Implements the Adaptor template interface required by Consensus. @@ -97,7 +98,7 @@ class RCLConsensus LedgerMaster& ledgerMaster, LocalTxs& localTxs, InboundTransactions& inboundTransactions, - ValidatorKeys const & validatorKeys, + ValidatorKeys const& validatorKeys, beast::Journal journal); bool @@ -130,7 +131,7 @@ class RCLConsensus @return Whether we enter the round proposing */ bool - preStartRound(RCLCxLedger const & prevLedger); + preStartRound(RCLCxLedger const& prevLedger); bool haveValidated() const; @@ -142,8 +143,8 @@ class RCLConsensus getQuorumKeys() const; std::size_t - laggards(Ledger_t::Seq const seq, - hash_set& trustedKeys) const; + laggards(Ledger_t::Seq const seq, hash_set& trustedKeys) + const; /** Whether I am a validator. * @@ -159,7 +160,8 @@ class RCLConsensus * * @param positions Number of current peer positions. */ - void updateOperatingMode(std::size_t const positions) const; + void + updateOperatingMode(std::size_t const positions) const; /** Consensus simulation parameters */ @@ -173,9 +175,9 @@ class RCLConsensus //--------------------------------------------------------------------- // The following members implement the generic Consensus requirements // and are marked private to indicate ONLY Consensus will call - // them (via friendship). Since they are called only from Consensus - // methods and since RCLConsensus::consensus_ should only be accessed - // under lock, these will only be called under lock. + // them (via friendship). Since they are called only from + // Consensus methods and since RCLConsensus::consensus_ should + // only be accessed under lock, these will only be called under lock. // // In general, the idea is that there is only ONE thread that is running // consensus code at anytime. The only special case is the dispatched @@ -242,7 +244,7 @@ class RCLConsensus descended from the preferred working ledger. */ std::size_t - proposersFinished(RCLCxLedger const & ledger, LedgerHash const& h) const; + proposersFinished(RCLCxLedger const& ledger, LedgerHash const& h) const; /** Propose the given position to my peers. @@ -281,9 +283,7 @@ class RCLConsensus @param after The new consensus mode */ void - onModeChange( - ConsensusMode before, - ConsensusMode after); + onModeChange(ConsensusMode before, ConsensusMode after); /** Close the open ledger and return initial consensus position. @@ -400,7 +400,10 @@ class RCLConsensus but are still around and trying to catch up. */ void - validate(RCLCxLedger const& ledger, RCLTxSet const& txns, bool proposing); + validate( + RCLCxLedger const& ledger, + RCLTxSet const& txns, + bool proposing); }; public: @@ -412,7 +415,7 @@ public: LocalTxs& localTxs, InboundTransactions& inboundTransactions, Consensus::clock_type const& clock, - ValidatorKeys const & validatorKeys, + ValidatorKeys const& validatorKeys, beast::Journal journal); RCLConsensus(RCLConsensus const&) = delete; @@ -501,7 +504,7 @@ public: NetClock::time_point const& now, RCLCxPeerPos const& newProposal); - ConsensusParms const & + ConsensusParms const& parms() const { return adaptor_.parms(); @@ -517,6 +520,6 @@ private: Consensus consensus_; beast::Journal const j_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/consensus/RCLCxLedger.h b/src/ripple/app/consensus/RCLCxLedger.h index 58bc7a4d48..678529351f 100644 --- a/src/ripple/app/consensus/RCLCxLedger.h +++ b/src/ripple/app/consensus/RCLCxLedger.h @@ -119,5 +119,5 @@ public: */ std::shared_ptr ledger_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/consensus/RCLCxPeerPos.cpp b/src/ripple/app/consensus/RCLCxPeerPos.cpp index cccf6d9a13..c9ae9c2eb0 100644 --- a/src/ripple/app/consensus/RCLCxPeerPos.cpp +++ b/src/ripple/app/consensus/RCLCxPeerPos.cpp @@ -20,9 +20,9 @@ #include #include #include -#include #include #include +#include namespace ripple { @@ -54,11 +54,9 @@ RCLCxPeerPos::signingHash() const bool RCLCxPeerPos::checkSign() const { - return verifyDigest( - publicKey(), signingHash(), signature(), false); + return verifyDigest(publicKey(), signingHash(), signature(), false); } - Json::Value RCLCxPeerPos::getJson() const { @@ -102,4 +100,4 @@ RCLCxPeerPos::Data::Data( { } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/consensus/RCLCxPeerPos.h b/src/ripple/app/consensus/RCLCxPeerPos.h index 773c4cafd1..83456a101f 100644 --- a/src/ripple/app/consensus/RCLCxPeerPos.h +++ b/src/ripple/app/consensus/RCLCxPeerPos.h @@ -90,7 +90,7 @@ public: return data_->suppression_; } - Proposal const & + Proposal const& proposal() const { return data_->proposal_; @@ -101,7 +101,6 @@ public: getJson() const; private: - struct Data : public CountedObject { PublicKey publicKey_; @@ -135,7 +134,6 @@ private: hash_append(h, proposal().prevLedger()); hash_append(h, proposal().position()); } - }; /** Calculate a unique identifier for a signed proposal. @@ -163,6 +161,6 @@ proposalUniqueId( Slice const& publicKey, Slice const& signature); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/consensus/RCLCxTx.h b/src/ripple/app/consensus/RCLCxTx.h index eedeb99620..14c61a6023 100644 --- a/src/ripple/app/consensus/RCLCxTx.h +++ b/src/ripple/app/consensus/RCLCxTx.h @@ -178,9 +178,7 @@ public: std::map ret; for (auto const& [k, v] : delta) { - assert( - (v.first && !v.second) || - (v.second && !v.first)); + assert((v.first && !v.second) || (v.second && !v.first)); ret[k] = static_cast(v.first); } @@ -190,5 +188,5 @@ public: //! The SHAMap representing the transactions. std::shared_ptr map_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/consensus/RCLValidations.cpp b/src/ripple/app/consensus/RCLValidations.cpp index ff007acb2c..9940f2a54c 100644 --- a/src/ripple/app/consensus/RCLValidations.cpp +++ b/src/ripple/app/consensus/RCLValidations.cpp @@ -75,7 +75,8 @@ RCLValidatedLedger::id() const -> ID return ledgerID_; } -auto RCLValidatedLedger::operator[](Seq const& s) const -> ID +auto +RCLValidatedLedger::operator[](Seq const& s) const -> ID { if (s >= minSeq() && s <= seq()) { @@ -112,7 +113,7 @@ mismatch(RCLValidatedLedger const& a, RCLValidatedLedger const& b) } RCLValidationsAdaptor::RCLValidationsAdaptor(Application& app, beast::Journal j) - : app_(app), j_(j) + : app_(app), j_(j) { } @@ -123,7 +124,7 @@ RCLValidationsAdaptor::now() const } boost::optional -RCLValidationsAdaptor::acquire(LedgerHash const & hash) +RCLValidationsAdaptor::acquire(LedgerHash const& hash) { auto ledger = app_.getLedgerMaster().getLedgerByHash(hash); if (!ledger) @@ -131,11 +132,11 @@ RCLValidationsAdaptor::acquire(LedgerHash const & hash) JLOG(j_.debug()) << "Need validated ledger for preferred ledger analysis " << hash; - Application * pApp = &app_; + Application* pApp = &app_; app_.getJobQueue().addJob( jtADVANCE, "getConsensusLedger", [pApp, hash](Job&) { - pApp ->getInboundLedgers().acquire( + pApp->getInboundLedgers().acquire( hash, 0, InboundLedger::Reason::CONSENSUS); }); return boost::none; @@ -148,7 +149,8 @@ RCLValidationsAdaptor::acquire(LedgerHash const & hash) } bool -handleNewValidation(Application& app, +handleNewValidation( + Application& app, STValidation::ref val, std::string const& source) { @@ -175,14 +177,13 @@ handleNewValidation(Application& app, << (val->isFull() ? "full" : "partial") << " from " << (masterKey ? toBase58(TokenType::NodePublic, *masterKey) : "unknown") - << " signing key " - << toBase58(TokenType::NodePublic, signingKey) << " " << msg - << " src=" << source; + << " signing key " << toBase58(TokenType::NodePublic, signingKey) + << " " << msg << " src=" << source; }; - if(!val->isFieldPresent(sfLedgerSequence)) + if (!val->isFieldPresent(sfLedgerSequence)) { - if(j.error()) + if (j.error()) dmp(j.error(), "missing ledger sequence field"); return false; } @@ -191,7 +192,7 @@ handleNewValidation(Application& app, if (masterKey) { ValStatus const outcome = validations.add(calcNodeID(*masterKey), val); - if(j.debug()) + if (j.debug()) dmp(j.debug(), to_string(outcome)); if (outcome == ValStatus::badSeq && j.warn()) @@ -211,8 +212,8 @@ handleNewValidation(Application& app, else { JLOG(j.debug()) << "Val for " << hash << " from " - << toBase58(TokenType::NodePublic, signingKey) - << " not added UNlisted"; + << toBase58(TokenType::NodePublic, signingKey) + << " not added UNlisted"; } // This currently never forwards untrusted validations, though we may @@ -226,5 +227,4 @@ handleNewValidation(Application& app, return shouldRelay; } - } // namespace ripple diff --git a/src/ripple/app/consensus/RCLValidations.h b/src/ripple/app/consensus/RCLValidations.h index 191a6516c0..4cce72f035 100644 --- a/src/ripple/app/consensus/RCLValidations.h +++ b/src/ripple/app/consensus/RCLValidations.h @@ -39,6 +39,7 @@ class Application; class RCLValidation { STValidation::pointer val_; + public: using NodeKey = ripple::PublicKey; using NodeID = ripple::NodeID; @@ -132,7 +133,6 @@ public: { return val_; } - }; /** Wraps a ledger instance for use in generic Validations LedgerTrie. @@ -174,7 +174,8 @@ public: @return The ID of this ledger's ancestor with that sequence number or ID{0} if one was not determined */ - ID operator[](Seq const& s) const; + ID + operator[](Seq const& s) const; /// Find the sequence number of the earliest mismatching ancestor friend Seq @@ -212,7 +213,7 @@ public: /** Attempt to acquire the ledger with given id from the network */ boost::optional - acquire(LedgerHash const & id); + acquire(LedgerHash const& id); beast::Journal journal() const @@ -228,7 +229,6 @@ private: /// Alias for RCL-specific instantiation of generic Validations using RCLValidations = Validations; - /** Handle a new validation Also sets the trust status of a validation based on the validating node's diff --git a/src/ripple/app/ledger/AbstractFetchPackContainer.h b/src/ripple/app/ledger/AbstractFetchPackContainer.h index 819b03e3cf..75ae084476 100644 --- a/src/ripple/app/ledger/AbstractFetchPackContainer.h +++ b/src/ripple/app/ledger/AbstractFetchPackContainer.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_APP_LEDGER_ABSTRACTFETCHPACKCONTAINER_H_INCLUDED #define RIPPLE_APP_LEDGER_ABSTRACTFETCHPACKCONTAINER_H_INCLUDED -#include #include +#include #include namespace ripple { @@ -40,9 +40,10 @@ public: @return `boost::none` if the hash isn't cached, otherwise, the hash associated data. */ - virtual boost::optional getFetchPack(uint256 const& nodeHash) = 0; + virtual boost::optional + getFetchPack(uint256 const& nodeHash) = 0; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/AcceptedLedger.cpp b/src/ripple/app/ledger/AcceptedLedger.cpp index ec2cfde198..d097e8884a 100644 --- a/src/ripple/app/ledger/AcceptedLedger.cpp +++ b/src/ripple/app/ledger/AcceptedLedger.cpp @@ -23,32 +23,35 @@ namespace ripple { -AcceptedLedger::AcceptedLedger ( +AcceptedLedger::AcceptedLedger( std::shared_ptr const& ledger, - AccountIDCache const& accountCache, Logs& logs) - : mLedger (ledger) + AccountIDCache const& accountCache, + Logs& logs) + : mLedger(ledger) { for (auto const& item : ledger->txs) { - insert (std::make_shared( + insert(std::make_shared( ledger, item.first, item.second, accountCache, logs)); } } -void AcceptedLedger::insert (AcceptedLedgerTx::ref at) +void +AcceptedLedger::insert(AcceptedLedgerTx::ref at) { - assert (mMap.find (at->getIndex ()) == mMap.end ()); - mMap.insert (std::make_pair (at->getIndex (), at)); + assert(mMap.find(at->getIndex()) == mMap.end()); + mMap.insert(std::make_pair(at->getIndex(), at)); } -AcceptedLedgerTx::pointer AcceptedLedger::getTxn (int i) const +AcceptedLedgerTx::pointer +AcceptedLedger::getTxn(int i) const { - map_t::const_iterator it = mMap.find (i); + map_t::const_iterator it = mMap.find(i); - if (it == mMap.end ()) - return AcceptedLedgerTx::pointer (); + if (it == mMap.end()) + return AcceptedLedgerTx::pointer(); return it->second; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/AcceptedLedger.h b/src/ripple/app/ledger/AcceptedLedger.h index db2dee3bd3..2fe22efc2d 100644 --- a/src/ripple/app/ledger/AcceptedLedger.h +++ b/src/ripple/app/ledger/AcceptedLedger.h @@ -44,41 +44,46 @@ namespace ripple { class AcceptedLedger { public: - using pointer = std::shared_ptr; - using ret = const pointer&; - using map_t = std::map; + using pointer = std::shared_ptr; + using ret = const pointer&; + using map_t = std::map; // mapt_t must be an ordered map! - using value_type = map_t::value_type; + using value_type = map_t::value_type; using const_iterator = map_t::const_iterator; public: - std::shared_ptr const& getLedger () const + std::shared_ptr const& + getLedger() const { return mLedger; } - const map_t& getMap () const + const map_t& + getMap() const { return mMap; } - int getTxnCount () const + int + getTxnCount() const { - return mMap.size (); + return mMap.size(); } - AcceptedLedgerTx::pointer getTxn (int) const; + AcceptedLedgerTx::pointer + getTxn(int) const; - AcceptedLedger ( + AcceptedLedger( std::shared_ptr const& ledger, - AccountIDCache const& accountCache, Logs& logs); + AccountIDCache const& accountCache, + Logs& logs); private: - void insert (AcceptedLedgerTx::ref); + void insert(AcceptedLedgerTx::ref); std::shared_ptr mLedger; map_t mMap; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/AcceptedLedgerTx.cpp b/src/ripple/app/ledger/AcceptedLedgerTx.cpp index 20a2c3137d..4c0150d4a8 100644 --- a/src/ripple/app/ledger/AcceptedLedgerTx.cpp +++ b/src/ripple/app/ledger/AcceptedLedgerTx.cpp @@ -17,96 +17,104 @@ */ //============================================================================== -#include #include +#include #include #include -#include #include +#include namespace ripple { -AcceptedLedgerTx::AcceptedLedgerTx ( +AcceptedLedgerTx::AcceptedLedgerTx( std::shared_ptr const& ledger, std::shared_ptr const& txn, std::shared_ptr const& met, AccountIDCache const& accountCache, Logs& logs) - : mLedger (ledger) - , mTxn (txn) - , mMeta (std::make_shared ( - txn->getTransactionID(), ledger->seq(), *met)) - , mAffected (mMeta->getAffectedAccounts (logs.journal("View"))) - , accountCache_ (accountCache) - , logs_ (logs) + : mLedger(ledger) + , mTxn(txn) + , mMeta(std::make_shared( + txn->getTransactionID(), + ledger->seq(), + *met)) + , mAffected(mMeta->getAffectedAccounts(logs.journal("View"))) + , accountCache_(accountCache) + , logs_(logs) { - assert (! ledger->open()); + assert(!ledger->open()); - mResult = mMeta->getResultTER (); + mResult = mMeta->getResultTER(); Serializer s; met->add(s); - mRawMeta = std::move (s.modData()); + mRawMeta = std::move(s.modData()); - buildJson (); + buildJson(); } -AcceptedLedgerTx::AcceptedLedgerTx ( +AcceptedLedgerTx::AcceptedLedgerTx( std::shared_ptr const& ledger, std::shared_ptr const& txn, TER result, AccountIDCache const& accountCache, Logs& logs) - : mLedger (ledger) - , mTxn (txn) - , mResult (result) - , mAffected (txn->getMentionedAccounts ()) - , accountCache_ (accountCache) - , logs_ (logs) + : mLedger(ledger) + , mTxn(txn) + , mResult(result) + , mAffected(txn->getMentionedAccounts()) + , accountCache_(accountCache) + , logs_(logs) { - assert (ledger->open()); - buildJson (); + assert(ledger->open()); + buildJson(); } -std::string AcceptedLedgerTx::getEscMeta () const +std::string +AcceptedLedgerTx::getEscMeta() const { - assert (!mRawMeta.empty ()); - return sqlEscape (mRawMeta); + assert(!mRawMeta.empty()); + return sqlEscape(mRawMeta); } -void AcceptedLedgerTx::buildJson () +void +AcceptedLedgerTx::buildJson() { mJson = Json::objectValue; - mJson[jss::transaction] = mTxn->getJson (JsonOptions::none); + mJson[jss::transaction] = mTxn->getJson(JsonOptions::none); if (mMeta) { - mJson[jss::meta] = mMeta->getJson (JsonOptions::none); - mJson[jss::raw_meta] = strHex (mRawMeta); + mJson[jss::meta] = mMeta->getJson(JsonOptions::none); + mJson[jss::raw_meta] = strHex(mRawMeta); } - mJson[jss::result] = transHuman (mResult); + mJson[jss::result] = transHuman(mResult); - if (! mAffected.empty ()) + if (!mAffected.empty()) { Json::Value& affected = (mJson[jss::affected] = Json::arrayValue); - for (auto const& account: mAffected) - affected.append (accountCache_.toBase58(account)); + for (auto const& account : mAffected) + affected.append(accountCache_.toBase58(account)); } - if (mTxn->getTxnType () == ttOFFER_CREATE) + if (mTxn->getTxnType() == ttOFFER_CREATE) { auto const& account = mTxn->getAccountID(sfAccount); - auto const amount = mTxn->getFieldAmount (sfTakerGets); + auto const amount = mTxn->getFieldAmount(sfTakerGets); // If the offer create is not self funded then add the owner balance - if (account != amount.issue ().account) + if (account != amount.issue().account) { - auto const ownerFunds = accountFunds(*mLedger, - account, amount, fhIGNORE_FREEZE, logs_.journal ("View")); - mJson[jss::transaction][jss::owner_funds] = ownerFunds.getText (); + auto const ownerFunds = accountFunds( + *mLedger, + account, + amount, + fhIGNORE_FREEZE, + logs_.journal("View")); + mJson[jss::transaction][jss::owner_funds] = ownerFunds.getText(); } } } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/AcceptedLedgerTx.h b/src/ripple/app/ledger/AcceptedLedgerTx.h index b3e4c2f9a4..712e085b6c 100644 --- a/src/ripple/app/ledger/AcceptedLedgerTx.h +++ b/src/ripple/app/ledger/AcceptedLedgerTx.h @@ -50,28 +50,30 @@ class Logs; class AcceptedLedgerTx { public: - using pointer = std::shared_ptr ; + using pointer = std::shared_ptr; using ref = const pointer&; public: - AcceptedLedgerTx ( + AcceptedLedgerTx( std::shared_ptr const& ledger, std::shared_ptr const&, std::shared_ptr const&, AccountIDCache const&, Logs&); - AcceptedLedgerTx ( + AcceptedLedgerTx( std::shared_ptr const&, std::shared_ptr const&, TER, AccountIDCache const&, Logs&); - std::shared_ptr const& getTxn () const + std::shared_ptr const& + getTxn() const { return mTxn; } - std::shared_ptr const& getMeta () const + std::shared_ptr const& + getMeta() const { return mMeta; } @@ -82,33 +84,41 @@ public: return mAffected; } - TxID getTransactionID () const + TxID + getTransactionID() const { - return mTxn->getTransactionID (); + return mTxn->getTransactionID(); } - TxType getTxnType () const + TxType + getTxnType() const { - return mTxn->getTxnType (); + return mTxn->getTxnType(); } - TER getResult () const + TER + getResult() const { return mResult; } - std::uint32_t getTxnSeq () const + std::uint32_t + getTxnSeq() const { - return mMeta->getIndex (); + return mMeta->getIndex(); } - bool isApplied () const + bool + isApplied() const { return bool(mMeta); } - int getIndex () const + int + getIndex() const { - return mMeta ? mMeta->getIndex () : 0; + return mMeta ? mMeta->getIndex() : 0; } - std::string getEscMeta () const; - Json::Value getJson () const + std::string + getEscMeta() const; + Json::Value + getJson() const { return mJson; } @@ -117,16 +127,17 @@ private: std::shared_ptr mLedger; std::shared_ptr mTxn; std::shared_ptr mMeta; - TER mResult; + TER mResult; boost::container::flat_set mAffected; - Blob mRawMeta; - Json::Value mJson; + Blob mRawMeta; + Json::Value mJson; AccountIDCache const& accountCache_; Logs& logs_; - void buildJson (); + void + buildJson(); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/AccountStateSF.cpp b/src/ripple/app/ledger/AccountStateSF.cpp index 142c7886fc..5d3456a9fc 100644 --- a/src/ripple/app/ledger/AccountStateSF.cpp +++ b/src/ripple/app/ledger/AccountStateSF.cpp @@ -22,12 +22,15 @@ namespace ripple { void -AccountStateSF::gotNode(bool, SHAMapHash const& nodeHash, - std::uint32_t ledgerSeq, Blob&& nodeData, +AccountStateSF::gotNode( + bool, + SHAMapHash const& nodeHash, + std::uint32_t ledgerSeq, + Blob&& nodeData, SHAMapTreeNode::TNType) const { - db_.store(hotACCOUNT_NODE, std::move(nodeData), - nodeHash.as_uint256(), ledgerSeq); + db_.store( + hotACCOUNT_NODE, std::move(nodeData), nodeHash.as_uint256(), ledgerSeq); } boost::optional @@ -36,4 +39,4 @@ AccountStateSF::getNode(SHAMapHash const& nodeHash) const return fp_.getFetchPack(nodeHash.as_uint256()); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/AccountStateSF.h b/src/ripple/app/ledger/AccountStateSF.h index 0b5b83b428..aa1557f98d 100644 --- a/src/ripple/app/ledger/AccountStateSF.h +++ b/src/ripple/app/ledger/AccountStateSF.h @@ -32,14 +32,17 @@ class AccountStateSF : public SHAMapSyncFilter { public: AccountStateSF(NodeStore::Database& db, AbstractFetchPackContainer& fp) - : db_(db) - , fp_(fp) - {} + : db_(db), fp_(fp) + { + } void - gotNode(bool fromFilter, SHAMapHash const& nodeHash, - std::uint32_t ledgerSeq, Blob&& nodeData, - SHAMapTreeNode::TNType type) const override; + gotNode( + bool fromFilter, + SHAMapHash const& nodeHash, + std::uint32_t ledgerSeq, + Blob&& nodeData, + SHAMapTreeNode::TNType type) const override; boost::optional getNode(SHAMapHash const& nodeHash) const override; @@ -49,6 +52,6 @@ private: AbstractFetchPackContainer& fp_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/BookListeners.cpp b/src/ripple/app/ledger/BookListeners.cpp index f2e1fe4652..885ca4d322 100644 --- a/src/ripple/app/ledger/BookListeners.cpp +++ b/src/ripple/app/ledger/BookListeners.cpp @@ -52,7 +52,7 @@ BookListeners::publish( if (p) { // Only publish jvObj if this is the first occurence - if(havePublished.emplace(p->getSeq()).second) + if (havePublished.emplace(p->getSeq()).second) { p->send(jvObj, true); } diff --git a/src/ripple/app/ledger/BookListeners.h b/src/ripple/app/ledger/BookListeners.h index fc45595dc1..2f668f3404 100644 --- a/src/ripple/app/ledger/BookListeners.h +++ b/src/ripple/app/ledger/BookListeners.h @@ -37,12 +37,12 @@ public: } /** Add a new subscription for this book - */ + */ void addSubscriber(InfoSub::ref sub); /** Stop publishing to a subscriber - */ + */ void removeSubscriber(std::uint64_t sub); diff --git a/src/ripple/app/ledger/BuildLedger.h b/src/ripple/app/ledger/BuildLedger.h index 1eb6b38dfa..acefee24e7 100644 --- a/src/ripple/app/ledger/BuildLedger.h +++ b/src/ripple/app/ledger/BuildLedger.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_APP_LEDGER_BUILD_LEDGER_H_INCLUDED #define RIPPLE_APP_LEDGER_BUILD_LEDGER_H_INCLUDED -#include #include #include +#include #include #include @@ -34,7 +34,6 @@ class Ledger; class LedgerReplay; class SHAMap; - /** Build a new ledger by applying consensus transactions Build a new ledger by applying a set of transactions accepted as part of diff --git a/src/ripple/app/ledger/ConsensusTransSetSF.cpp b/src/ripple/app/ledger/ConsensusTransSetSF.cpp index 9bb962ca1c..3368751216 100644 --- a/src/ripple/app/ledger/ConsensusTransSetSF.cpp +++ b/src/ripple/app/ledger/ConsensusTransSetSF.cpp @@ -23,80 +23,80 @@ #include #include #include -#include #include #include #include +#include namespace ripple { -ConsensusTransSetSF::ConsensusTransSetSF (Application& app, NodeCache& nodeCache) - : app_ (app) - , m_nodeCache (nodeCache) - , j_ (app.journal ("TransactionAcquire")) +ConsensusTransSetSF::ConsensusTransSetSF(Application& app, NodeCache& nodeCache) + : app_(app), m_nodeCache(nodeCache), j_(app.journal("TransactionAcquire")) { } void -ConsensusTransSetSF::gotNode(bool fromFilter, SHAMapHash const& nodeHash, - std::uint32_t, Blob&& nodeData, SHAMapTreeNode::TNType type) const +ConsensusTransSetSF::gotNode( + bool fromFilter, + SHAMapHash const& nodeHash, + std::uint32_t, + Blob&& nodeData, + SHAMapTreeNode::TNType type) const { if (fromFilter) return; - m_nodeCache.insert (nodeHash, nodeData); + m_nodeCache.insert(nodeHash, nodeData); - if ((type == SHAMapTreeNode::tnTRANSACTION_NM) && (nodeData.size () > 16)) + if ((type == SHAMapTreeNode::tnTRANSACTION_NM) && (nodeData.size() > 16)) { // this is a transaction, and we didn't have it - JLOG (j_.debug()) - << "Node on our acquiring TX set is TXN we may not have"; + JLOG(j_.debug()) + << "Node on our acquiring TX set is TXN we may not have"; try { // skip prefix - Serializer s (nodeData.data() + 4, nodeData.size() - 4); - SerialIter sit (s.slice()); - auto stx = std::make_shared (std::ref (sit)); - assert (stx->getTransactionID () == nodeHash.as_uint256()); + Serializer s(nodeData.data() + 4, nodeData.size() - 4); + SerialIter sit(s.slice()); + auto stx = std::make_shared(std::ref(sit)); + assert(stx->getTransactionID() == nodeHash.as_uint256()); auto const pap = &app_; - app_.getJobQueue ().addJob ( - jtTRANSACTION, "TXS->TXN", - [pap, stx] (Job&) { + app_.getJobQueue().addJob( + jtTRANSACTION, "TXS->TXN", [pap, stx](Job&) { pap->getOPs().submitTransaction(stx); }); } catch (std::exception const&) { - JLOG (j_.warn()) - << "Fetched invalid transaction in proposed set"; + JLOG(j_.warn()) << "Fetched invalid transaction in proposed set"; } } } boost::optional -ConsensusTransSetSF::getNode (SHAMapHash const& nodeHash) const +ConsensusTransSetSF::getNode(SHAMapHash const& nodeHash) const { Blob nodeData; - if (m_nodeCache.retrieve (nodeHash, nodeData)) + if (m_nodeCache.retrieve(nodeHash, nodeData)) return nodeData; - auto txn = app_.getMasterTransaction().fetch_from_cache (nodeHash.as_uint256()); + auto txn = + app_.getMasterTransaction().fetch_from_cache(nodeHash.as_uint256()); if (txn) { // this is a transaction, and we have it - JLOG (j_.trace()) - << "Node in our acquiring TX set is TXN we have"; + JLOG(j_.trace()) << "Node in our acquiring TX set is TXN we have"; Serializer s; - s.add32 (HashPrefix::transactionID); - txn->getSTransaction ()->add (s); + s.add32(HashPrefix::transactionID); + txn->getSTransaction()->add(s); assert(sha512Half(s.slice()) == nodeHash.as_uint256()); - nodeData = s.peekData (); + nodeData = s.peekData(); return nodeData; } return boost::none; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/ConsensusTransSetSF.h b/src/ripple/app/ledger/ConsensusTransSetSF.h index 21c138c03b..c413caffa6 100644 --- a/src/ripple/app/ledger/ConsensusTransSetSF.h +++ b/src/ripple/app/ledger/ConsensusTransSetSF.h @@ -21,8 +21,8 @@ #define RIPPLE_APP_LEDGER_CONSENSUSTRANSSETSF_H_INCLUDED #include -#include #include +#include namespace ripple { @@ -34,18 +34,21 @@ namespace ripple { class ConsensusTransSetSF : public SHAMapSyncFilter { public: - using NodeCache = TaggedCache ; + using NodeCache = TaggedCache; - ConsensusTransSetSF (Application& app, NodeCache& nodeCache); + ConsensusTransSetSF(Application& app, NodeCache& nodeCache); // Note that the nodeData is overwritten by this call void - gotNode(bool fromFilter, SHAMapHash const& nodeHash, - std::uint32_t ledgerSeq, Blob&& nodeData, - SHAMapTreeNode::TNType type) const override; + gotNode( + bool fromFilter, + SHAMapHash const& nodeHash, + std::uint32_t ledgerSeq, + Blob&& nodeData, + SHAMapTreeNode::TNType type) const override; boost::optional - getNode (SHAMapHash const& nodeHash) const override; + getNode(SHAMapHash const& nodeHash) const override; private: Application& app_; @@ -53,6 +56,6 @@ private: beast::Journal const j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/InboundLedger.h b/src/ripple/app/ledger/InboundLedger.h index a878f805b5..05ddec2bf9 100644 --- a/src/ripple/app/ledger/InboundLedger.h +++ b/src/ripple/app/ledger/InboundLedger.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_APP_LEDGER_INBOUNDLEDGER_H_INCLUDED #define RIPPLE_APP_LEDGER_INBOUNDLEDGER_H_INCLUDED -#include #include -#include +#include #include +#include #include #include #include @@ -31,37 +31,44 @@ namespace ripple { // A ledger we are trying to acquire -class InboundLedger - : public PeerSet - , public std::enable_shared_from_this - , public CountedObject +class InboundLedger : public PeerSet, + public std::enable_shared_from_this, + public CountedObject { public: - static char const* getCountedObjectName () { return "InboundLedger"; } + static char const* + getCountedObjectName() + { + return "InboundLedger"; + } - using PeerDataPairType = std::pair< - std::weak_ptr, - std::shared_ptr>; + using PeerDataPairType = + std::pair, std::shared_ptr>; // These are the reasons we might acquire a ledger - enum class Reason - { - HISTORY, // Acquiring past ledger - SHARD, // Acquiring for shard - GENERIC, // Generic other reasons - CONSENSUS // We believe the consensus round requires this ledger + enum class Reason { + HISTORY, // Acquiring past ledger + SHARD, // Acquiring for shard + GENERIC, // Generic other reasons + CONSENSUS // We believe the consensus round requires this ledger }; - InboundLedger(Application& app, uint256 const& hash, - std::uint32_t seq, Reason reason, clock_type&); + InboundLedger( + Application& app, + uint256 const& hash, + std::uint32_t seq, + Reason reason, + clock_type&); - ~InboundLedger (); + ~InboundLedger(); // Called when the PeerSet timer expires - void execute () override; + void + execute() override; // Called when another attempt is made to fetch this same ledger - void update (std::uint32_t seq); + void + update(std::uint32_t seq); std::shared_ptr getLedger() const @@ -69,7 +76,8 @@ public: return mLedger; } - std::uint32_t getSeq () const + std::uint32_t + getSeq() const { return mSeq; } @@ -80,82 +88,96 @@ public: return mReason; } - bool checkLocal (); - void init (ScopedLockType& collectionLock); + bool + checkLocal(); + void + init(ScopedLockType& collectionLock); bool - gotData(std::weak_ptr, + gotData( + std::weak_ptr, std::shared_ptr const&); using neededHash_t = - std::pair ; + std::pair; /** Return a Json::objectValue. */ - Json::Value getJson (int); + Json::Value + getJson(int); - void runData (); + void + runData(); - static - LedgerInfo + static LedgerInfo deserializeHeader(Slice data, bool hasPrefix); private: - enum class TriggerReason - { - added, - reply, - timeout - }; + enum class TriggerReason { added, reply, timeout }; - void filterNodes ( + void + filterNodes( std::vector>& nodes, TriggerReason reason); - void trigger (std::shared_ptr const&, TriggerReason); + void + trigger(std::shared_ptr const&, TriggerReason); - std::vector getNeededHashes (); + std::vector + getNeededHashes(); - void addPeers (); - void tryDB (Family& f); + void + addPeers(); + void + tryDB(Family& f); - void done (); + void + done(); - void onTimer (bool progress, ScopedLockType& peerSetLock) override; + void + onTimer(bool progress, ScopedLockType& peerSetLock) override; - void newPeer (std::shared_ptr const& peer) override + void + newPeer(std::shared_ptr const& peer) override { // For historical nodes, do not trigger too soon // since a fetch pack is probably coming if (mReason != Reason::HISTORY) - trigger (peer, TriggerReason::added); + trigger(peer, TriggerReason::added); } - std::weak_ptr pmDowncast () override; + std::weak_ptr + pmDowncast() override; - int processData (std::shared_ptr peer, protocol::TMLedgerData& data); + int + processData(std::shared_ptr peer, protocol::TMLedgerData& data); - bool takeHeader (std::string const& data); - bool takeTxNode (const std::vector& IDs, - const std::vector& data, - SHAMapAddNode&); - bool takeTxRootNode (Slice const& data, SHAMapAddNode&); + bool + takeHeader(std::string const& data); + bool + takeTxNode( + const std::vector& IDs, + const std::vector& data, + SHAMapAddNode&); + bool + takeTxRootNode(Slice const& data, SHAMapAddNode&); // VFALCO TODO Rename to receiveAccountStateNode // Don't use acronyms, but if we are going to use them at least // capitalize them correctly. // - bool takeAsNode (const std::vector& IDs, - const std::vector& data, - SHAMapAddNode&); - bool takeAsRootNode (Slice const& data, SHAMapAddNode&); + bool + takeAsNode( + const std::vector& IDs, + const std::vector& data, + SHAMapAddNode&); + bool + takeAsRootNode(Slice const& data, SHAMapAddNode&); std::vector - neededTxHashes ( - int max, SHAMapSyncFilter* filter) const; + neededTxHashes(int max, SHAMapSyncFilter* filter) const; std::vector - neededStateHashes ( - int max, SHAMapSyncFilter* filter) const; + neededStateHashes(int max, SHAMapSyncFilter* filter) const; std::shared_ptr mLedger; bool mHaveHeader; @@ -166,16 +188,16 @@ private: std::uint32_t mSeq; Reason const mReason; - std::set mRecentNodes; + std::set mRecentNodes; SHAMapAddNode mStats; // Data we have received from peers std::mutex mReceivedDataLock; - std::vector mReceivedData; + std::vector mReceivedData; bool mReceiveDispatched; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/InboundLedgers.h b/src/ripple/app/ledger/InboundLedgers.h index 9988e6a6bc..f57b2616cb 100644 --- a/src/ripple/app/ledger/InboundLedgers.h +++ b/src/ripple/app/ledger/InboundLedgers.h @@ -21,8 +21,8 @@ #define RIPPLE_APP_LEDGER_INBOUNDLEDGERS_H_INCLUDED #include -#include #include +#include #include namespace ripple { @@ -34,58 +34,71 @@ namespace ripple { class InboundLedgers { public: - using clock_type = beast::abstract_clock ; + using clock_type = beast::abstract_clock; virtual ~InboundLedgers() = 0; // VFALCO TODO Should this be called findOrAdd ? // - virtual - std::shared_ptr - acquire (uint256 const& hash, - std::uint32_t seq, InboundLedger::Reason) = 0; + virtual std::shared_ptr + acquire(uint256 const& hash, std::uint32_t seq, InboundLedger::Reason) = 0; - virtual std::shared_ptr find (LedgerHash const& hash) = 0; + virtual std::shared_ptr + find(LedgerHash const& hash) = 0; // VFALCO TODO Remove the dependency on the Peer object. // - virtual bool gotLedgerData (LedgerHash const& ledgerHash, + virtual bool + gotLedgerData( + LedgerHash const& ledgerHash, std::shared_ptr, - std::shared_ptr ) = 0; + std::shared_ptr) = 0; - virtual void doLedgerData (LedgerHash hash) = 0; + virtual void + doLedgerData(LedgerHash hash) = 0; - virtual void gotStaleData ( - std::shared_ptr packet) = 0; + virtual void + gotStaleData(std::shared_ptr packet) = 0; - virtual int getFetchCount (int& timeoutCount) = 0; + virtual int + getFetchCount(int& timeoutCount) = 0; - virtual void logFailure (uint256 const& h, std::uint32_t seq) = 0; + virtual void + logFailure(uint256 const& h, std::uint32_t seq) = 0; - virtual bool isFailure (uint256 const& h) = 0; + virtual bool + isFailure(uint256 const& h) = 0; - virtual void clearFailures() = 0; + virtual void + clearFailures() = 0; - virtual Json::Value getInfo() = 0; + virtual Json::Value + getInfo() = 0; /** Returns the rate of historical ledger fetches per minute. */ - virtual std::size_t fetchRate() = 0; + virtual std::size_t + fetchRate() = 0; /** Called when a complete ledger is obtained. */ - virtual void onLedgerFetched() = 0; + virtual void + onLedgerFetched() = 0; - virtual void gotFetchPack () = 0; - virtual void sweep () = 0; + virtual void + gotFetchPack() = 0; + virtual void + sweep() = 0; - virtual void onStop() = 0; + virtual void + onStop() = 0; }; std::unique_ptr -make_InboundLedgers (Application& app, - InboundLedgers::clock_type& clock, Stoppable& parent, +make_InboundLedgers( + Application& app, + InboundLedgers::clock_type& clock, + Stoppable& parent, beast::insight::Collector::ptr const& collector); - -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/InboundTransactions.h b/src/ripple/app/ledger/InboundTransactions.h index 867db11dcd..0d6e883411 100644 --- a/src/ripple/app/ledger/InboundTransactions.h +++ b/src/ripple/app/ledger/InboundTransactions.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_APP_LEDGER_INBOUNDTRANSACTIONS_H_INCLUDED #define RIPPLE_APP_LEDGER_INBOUNDTRANSACTIONS_H_INCLUDED -#include -#include #include #include +#include +#include #include namespace ripple { @@ -31,57 +31,61 @@ namespace ripple { class Application; /** Manages the acquisition and lifetime of transaction sets. -*/ + */ class InboundTransactions { public: - using clock_type = beast::abstract_clock ; + using clock_type = beast::abstract_clock; InboundTransactions() = default; InboundTransactions(InboundTransactions const&) = delete; - InboundTransactions& operator=(InboundTransactions const&) = delete; + InboundTransactions& + operator=(InboundTransactions const&) = delete; virtual ~InboundTransactions() = 0; /** Retrieves a transaction set by hash - */ - virtual std::shared_ptr getSet ( - uint256 const& setHash, - bool acquire) = 0; + */ + virtual std::shared_ptr + getSet(uint256 const& setHash, bool acquire) = 0; /** Gives data to an inbound transaction set - */ - virtual void gotData (uint256 const& setHash, - std::shared_ptr , - std::shared_ptr ) = 0; + */ + virtual void + gotData( + uint256 const& setHash, + std::shared_ptr, + std::shared_ptr) = 0; /** Gives set to the container - */ - virtual void giveSet (uint256 const& setHash, - std::shared_ptr const& set, + */ + virtual void + giveSet( + uint256 const& setHash, + std::shared_ptr const& set, bool acquired) = 0; /** Informs the container if a new consensus round - */ - virtual void newRound (std::uint32_t seq) = 0; + */ + virtual void + newRound(std::uint32_t seq) = 0; - virtual Json::Value getInfo() = 0; + virtual Json::Value + getInfo() = 0; - virtual void onStop() = 0; + virtual void + onStop() = 0; }; -std::unique_ptr -make_InboundTransactions ( +std::unique_ptr +make_InboundTransactions( Application& app, InboundTransactions::clock_type& clock, Stoppable& parent, beast::insight::Collector::ptr const& collector, - std::function - const&, - bool)> gotSet); + std::function const&, bool)> gotSet); - -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 0c5cc4cf1d..0f95bdfb85 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -17,11 +17,10 @@ */ //============================================================================== -#include #include #include +#include #include -#include #include #include #include @@ -30,40 +29,40 @@ #include #include #include -#include #include #include +#include +#include +#include #include #include #include #include #include #include -#include +#include #include -#include #include #include -#include #include -#include +#include +#include #include #include #include namespace ripple { -create_genesis_t const create_genesis {}; +create_genesis_t const create_genesis{}; -static -uint256 -calculateLedgerHash (LedgerInfo const& info) +static uint256 +calculateLedgerHash(LedgerInfo const& info) { // VFALCO This has to match addRaw in View.h. return sha512Half( HashPrefix::ledgerMaster, std::uint32_t(info.seq), - std::uint64_t(info.drops.drops ()), + std::uint64_t(info.drops.drops()), info.parentHash, info.txHash, info.accountHash, @@ -75,35 +74,32 @@ calculateLedgerHash (LedgerInfo const& info) //------------------------------------------------------------------------------ -class Ledger::sles_iter_impl - : public sles_type::iter_base +class Ledger::sles_iter_impl : public sles_type::iter_base { private: SHAMap::const_iterator iter_; public: sles_iter_impl() = delete; - sles_iter_impl& operator= (sles_iter_impl const&) = delete; + sles_iter_impl& + operator=(sles_iter_impl const&) = delete; - sles_iter_impl (sles_iter_impl const&) = default; + sles_iter_impl(sles_iter_impl const&) = default; - sles_iter_impl (SHAMap::const_iterator iter) - : iter_ (iter) + sles_iter_impl(SHAMap::const_iterator iter) : iter_(iter) { } std::unique_ptr copy() const override { - return std::make_unique< - sles_iter_impl>(*this); + return std::make_unique(*this); } bool - equal (base_type const& impl) const override + equal(base_type const& impl) const override { - auto const& other = dynamic_cast< - sles_iter_impl const&>(impl); + auto const& other = dynamic_cast(impl); return iter_ == other.iter_; } @@ -118,15 +114,13 @@ public: { auto const item = *iter_; SerialIter sit(item.slice()); - return std::make_shared( - sit, item.key()); + return std::make_shared(sit, item.key()); } }; //------------------------------------------------------------------------------ -class Ledger::txs_iter_impl - : public txs_type::iter_base +class Ledger::txs_iter_impl : public txs_type::iter_base { private: bool metadata_; @@ -134,12 +128,12 @@ private: public: txs_iter_impl() = delete; - txs_iter_impl& operator= (txs_iter_impl const&) = delete; + txs_iter_impl& + operator=(txs_iter_impl const&) = delete; - txs_iter_impl (txs_iter_impl const&) = default; + txs_iter_impl(txs_iter_impl const&) = default; - txs_iter_impl(bool metadata, - SHAMap::const_iterator iter) + txs_iter_impl(bool metadata, SHAMap::const_iterator iter) : metadata_(metadata), iter_(iter) { } @@ -147,15 +141,13 @@ public: std::unique_ptr copy() const override { - return std::make_unique< - txs_iter_impl>(*this); + return std::make_unique(*this); } bool - equal (base_type const& impl) const override + equal(base_type const& impl) const override { - auto const& other = dynamic_cast< - txs_iter_impl const&>(impl); + auto const& other = dynamic_cast(impl); return iter_ == other.iter_; } @@ -171,22 +163,20 @@ public: auto const item = *iter_; if (metadata_) return deserializeTxPlusMeta(item); - return { deserializeTx(item), nullptr }; + return {deserializeTx(item), nullptr}; } }; //------------------------------------------------------------------------------ -Ledger::Ledger ( - create_genesis_t, - Config const& config, - std::vector const& amendments, - Family& family) - : mImmutable (false) - , txMap_ (std::make_shared (SHAMapType::TRANSACTION, - family)) - , stateMap_ (std::make_shared (SHAMapType::STATE, - family)) +Ledger::Ledger( + create_genesis_t, + Config const& config, + std::vector const& amendments, + Family& family) + : mImmutable(false) + , txMap_(std::make_shared(SHAMapType::TRANSACTION, family)) + , stateMap_(std::make_shared(SHAMapType::STATE, family)) , rules_{config.features} { info_.seq = 1; @@ -194,8 +184,8 @@ Ledger::Ledger ( info_.closeTimeResolution = ledgerDefaultTimeResolution; static auto const id = calcAccountID( - generateKeyPair(KeyType::secp256k1, - generateSeed("masterpassphrase")).first); + generateKeyPair(KeyType::secp256k1, generateSeed("masterpassphrase")) + .first); { auto const sle = std::make_shared(keylet::account(id)); sle->setFieldU32(sfSequence, 1); @@ -204,83 +194,84 @@ Ledger::Ledger ( rawInsert(sle); } - if (! amendments.empty()) + if (!amendments.empty()) { auto const sle = std::make_shared(keylet::amendments()); - sle->setFieldV256 (sfAmendments, STVector256{amendments}); + sle->setFieldV256(sfAmendments, STVector256{amendments}); rawInsert(sle); } - stateMap_->flushDirty (hotACCOUNT_NODE, info_.seq); + stateMap_->flushDirty(hotACCOUNT_NODE, info_.seq); setImmutable(config); } -Ledger::Ledger ( - LedgerInfo const& info, - bool& loaded, - bool acquire, - Config const& config, - Family& family, - beast::Journal j) - : mImmutable (true) - , txMap_ (std::make_shared (SHAMapType::TRANSACTION, - info.txHash, family)) - , stateMap_ (std::make_shared (SHAMapType::STATE, - info.accountHash, family)) - , rules_ (config.features) - , info_ (info) +Ledger::Ledger( + LedgerInfo const& info, + bool& loaded, + bool acquire, + Config const& config, + Family& family, + beast::Journal j) + : mImmutable(true) + , txMap_(std::make_shared( + SHAMapType::TRANSACTION, + info.txHash, + family)) + , stateMap_( + std::make_shared(SHAMapType::STATE, info.accountHash, family)) + , rules_(config.features) + , info_(info) { loaded = true; - if (info_.txHash.isNonZero () && - !txMap_->fetchRoot (SHAMapHash{info_.txHash}, nullptr)) + if (info_.txHash.isNonZero() && + !txMap_->fetchRoot(SHAMapHash{info_.txHash}, nullptr)) { loaded = false; - JLOG (j.warn()) << "Don't have TX root for ledger"; + JLOG(j.warn()) << "Don't have TX root for ledger"; } - if (info_.accountHash.isNonZero () && - !stateMap_->fetchRoot (SHAMapHash{info_.accountHash}, nullptr)) + if (info_.accountHash.isNonZero() && + !stateMap_->fetchRoot(SHAMapHash{info_.accountHash}, nullptr)) { loaded = false; - JLOG (j.warn()) << "Don't have AS root for ledger"; + JLOG(j.warn()) << "Don't have AS root for ledger"; } - txMap_->setImmutable (); - stateMap_->setImmutable (); + txMap_->setImmutable(); + stateMap_->setImmutable(); - if (! setup(config)) + if (!setup(config)) loaded = false; - if (! loaded) + if (!loaded) { info_.hash = calculateLedgerHash(info_); if (acquire) - family.missing_node (info_.hash, info_.seq); + family.missing_node(info_.hash, info_.seq); } } // Create a new ledger that follows this one -Ledger::Ledger (Ledger const& prevLedger, - NetClock::time_point closeTime) - : mImmutable (false) - , txMap_ (std::make_shared ( - SHAMapType::TRANSACTION, - prevLedger.stateMap_->family())) - , stateMap_ (prevLedger.stateMap_->snapShot (true)) +Ledger::Ledger(Ledger const& prevLedger, NetClock::time_point closeTime) + : mImmutable(false) + , txMap_(std::make_shared( + SHAMapType::TRANSACTION, + prevLedger.stateMap_->family())) + , stateMap_(prevLedger.stateMap_->snapShot(true)) , fees_(prevLedger.fees_) , rules_(prevLedger.rules_) { info_.seq = prevLedger.info_.seq + 1; - info_.parentCloseTime = - prevLedger.info_.closeTime; + info_.parentCloseTime = prevLedger.info_.closeTime; info_.hash = prevLedger.info().hash + uint256(1); info_.drops = prevLedger.info().drops; info_.closeTimeResolution = prevLedger.info_.closeTimeResolution; info_.parentHash = prevLedger.info().hash; info_.closeTimeResolution = getNextLedgerTimeResolution( prevLedger.info_.closeTimeResolution, - getCloseAgree(prevLedger.info()), info_.seq); + getCloseAgree(prevLedger.info()), + info_.seq); if (prevLedger.info_.closeTime == NetClock::time_point{}) { @@ -293,29 +284,28 @@ Ledger::Ledger (Ledger const& prevLedger, } } -Ledger::Ledger ( - LedgerInfo const& info, - Config const& config, - Family& family) - : mImmutable (true) - , txMap_ (std::make_shared (SHAMapType::TRANSACTION, - info.txHash, family)) - , stateMap_ (std::make_shared (SHAMapType::STATE, - info.accountHash, family)) +Ledger::Ledger(LedgerInfo const& info, Config const& config, Family& family) + : mImmutable(true) + , txMap_(std::make_shared( + SHAMapType::TRANSACTION, + info.txHash, + family)) + , stateMap_( + std::make_shared(SHAMapType::STATE, info.accountHash, family)) , rules_{config.features} - , info_ (info) + , info_(info) { - info_.hash = calculateLedgerHash (info_); + info_.hash = calculateLedgerHash(info_); } -Ledger::Ledger (std::uint32_t ledgerSeq, - NetClock::time_point closeTime, Config const& config, - Family& family) - : mImmutable (false) - , txMap_ (std::make_shared ( - SHAMapType::TRANSACTION, family)) - , stateMap_ (std::make_shared ( - SHAMapType::STATE, family)) +Ledger::Ledger( + std::uint32_t ledgerSeq, + NetClock::time_point closeTime, + Config const& config, + Family& family) + : mImmutable(false) + , txMap_(std::make_shared(SHAMapType::TRANSACTION, family)) + , stateMap_(std::make_shared(SHAMapType::STATE, family)) , rules_{config.features} { info_.seq = ledgerSeq; @@ -324,73 +314,70 @@ Ledger::Ledger (std::uint32_t ledgerSeq, setup(config); } -void Ledger::setImmutable (Config const& config) +void +Ledger::setImmutable(Config const& config) { // Force update, since this is the only // place the hash transitions to valid - if (! mImmutable) + if (!mImmutable) { - info_.txHash = txMap_->getHash ().as_uint256(); - info_.accountHash = stateMap_->getHash ().as_uint256(); + info_.txHash = txMap_->getHash().as_uint256(); + info_.accountHash = stateMap_->getHash().as_uint256(); } - info_.hash = calculateLedgerHash (info_); + info_.hash = calculateLedgerHash(info_); mImmutable = true; - txMap_->setImmutable (); - stateMap_->setImmutable (); + txMap_->setImmutable(); + stateMap_->setImmutable(); setup(config); } void -Ledger::setAccepted(NetClock::time_point closeTime, - NetClock::duration closeResolution, - bool correctCloseTime, Config const& config) +Ledger::setAccepted( + NetClock::time_point closeTime, + NetClock::duration closeResolution, + bool correctCloseTime, + Config const& config) { // Used when we witnessed the consensus. - assert (!open()); + assert(!open()); info_.closeTime = closeTime; info_.closeTimeResolution = closeResolution; info_.closeFlags = correctCloseTime ? 0 : sLCF_NoConsensusTime; - setImmutable (config); + setImmutable(config); } -bool Ledger::addSLE (SLE const& sle) +bool +Ledger::addSLE(SLE const& sle) { - SHAMapItem item (sle.key(), sle.getSerializer()); + SHAMapItem item(sle.key(), sle.getSerializer()); return stateMap_->addItem(std::move(item), false, false); } //------------------------------------------------------------------------------ std::shared_ptr -deserializeTx (SHAMapItem const& item) +deserializeTx(SHAMapItem const& item) { SerialIter sit(item.slice()); return std::make_shared(sit); } -std::pair, std::shared_ptr< - STObject const>> -deserializeTxPlusMeta (SHAMapItem const& item) +std::pair, std::shared_ptr> +deserializeTxPlusMeta(SHAMapItem const& item) { - std::pair, std::shared_ptr< - STObject const>> result; + std::pair, std::shared_ptr> + result; SerialIter sit(item.slice()); { - SerialIter s(sit.getSlice( - sit.getVLDataLength())); - result.first = std::make_shared< - STTx const>(s); + SerialIter s(sit.getSlice(sit.getVLDataLength())); + result.first = std::make_shared(s); } { - SerialIter s(sit.getSlice( - sit.getVLDataLength())); - result.second = std::make_shared< - STObject const>(s, sfMetadata); + SerialIter s(sit.getSlice(sit.getVLDataLength())); + result.second = std::make_shared(s, sfMetadata); } return result; } @@ -398,15 +385,14 @@ deserializeTxPlusMeta (SHAMapItem const& item) //------------------------------------------------------------------------------ bool -Ledger::exists (Keylet const& k) const +Ledger::exists(Keylet const& k) const { // VFALCO NOTE Perhaps check the type for debug builds? return stateMap_->hasItem(k.key); } boost::optional -Ledger::succ (uint256 const& key, - boost::optional const& last) const +Ledger::succ(uint256 const& key, boost::optional const& last) const { auto item = stateMap_->upper_bound(key); if (item == stateMap_->end()) @@ -417,21 +403,19 @@ Ledger::succ (uint256 const& key, } std::shared_ptr -Ledger::read (Keylet const& k) const +Ledger::read(Keylet const& k) const { if (k.key == beast::zero) { assert(false); return nullptr; } - auto const& item = - stateMap_->peekItem(k.key); - if (! item) + auto const& item = stateMap_->peekItem(k.key); + if (!item) return nullptr; auto sle = std::make_shared( - SerialIter{item->data(), - item->size()}, item->key()); - if (! k.check(*sle)) + SerialIter{item->data(), item->size()}, item->key()); + if (!k.check(*sle)) return nullptr; return sle; } @@ -439,73 +423,63 @@ Ledger::read (Keylet const& k) const //------------------------------------------------------------------------------ auto -Ledger::slesBegin() const -> - std::unique_ptr +Ledger::slesBegin() const -> std::unique_ptr { return std::make_unique(stateMap_->begin()); } auto -Ledger::slesEnd() const -> - std::unique_ptr +Ledger::slesEnd() const -> std::unique_ptr { return std::make_unique(stateMap_->end()); } auto -Ledger::slesUpperBound(uint256 const& key) const -> - std::unique_ptr +Ledger::slesUpperBound(uint256 const& key) const + -> std::unique_ptr { return std::make_unique(stateMap_->upper_bound(key)); } auto -Ledger::txsBegin() const -> - std::unique_ptr +Ledger::txsBegin() const -> std::unique_ptr { return std::make_unique(!open(), txMap_->begin()); } auto -Ledger::txsEnd() const -> - std::unique_ptr +Ledger::txsEnd() const -> std::unique_ptr { return std::make_unique(!open(), txMap_->end()); } bool -Ledger::txExists (uint256 const& key) const +Ledger::txExists(uint256 const& key) const { - return txMap_->hasItem (key); + return txMap_->hasItem(key); } auto -Ledger::txRead( - key_type const& key) const -> - tx_type +Ledger::txRead(key_type const& key) const -> tx_type { - auto const& item = - txMap_->peekItem(key); - if (! item) + auto const& item = txMap_->peekItem(key); + if (!item) return {}; if (!open()) { - auto result = - deserializeTxPlusMeta(*item); - return { std::move(result.first), - std::move(result.second) }; + auto result = deserializeTxPlusMeta(*item); + return {std::move(result.first), std::move(result.second)}; } - return { deserializeTx(*item), nullptr }; + return {deserializeTx(*item), nullptr}; } auto -Ledger::digest (key_type const& key) const -> - boost::optional +Ledger::digest(key_type const& key) const -> boost::optional { SHAMapHash digest; // VFALCO Unfortunately this loads the item // from the NodeStore needlessly. - if (! stateMap_->peekItem(key, digest)) + if (!stateMap_->peekItem(key, digest)) return boost::none; return digest.as_uint256(); } @@ -515,7 +489,7 @@ Ledger::digest (key_type const& key) const -> void Ledger::rawErase(std::shared_ptr const& sle) { - if (! stateMap_->delItem(sle->key())) + if (!stateMap_->delItem(sle->key())) LogicError("Ledger::rawErase: key not found"); } @@ -524,12 +498,9 @@ Ledger::rawInsert(std::shared_ptr const& sle) { Serializer ss; sle->add(ss); - auto item = std::make_shared< - SHAMapItem const>(sle->key(), - std::move(ss)); + auto item = std::make_shared(sle->key(), std::move(ss)); // VFALCO NOTE addGiveItem should take ownership - if (! stateMap_->addGiveItem( - std::move(item), false, false)) + if (!stateMap_->addGiveItem(std::move(item), false, false)) LogicError("Ledger::rawInsert: key already exists"); } @@ -538,37 +509,31 @@ Ledger::rawReplace(std::shared_ptr const& sle) { Serializer ss; sle->add(ss); - auto item = std::make_shared< - SHAMapItem const>(sle->key(), - std::move(ss)); + auto item = std::make_shared(sle->key(), std::move(ss)); // VFALCO NOTE updateGiveItem should take ownership - if (! stateMap_->updateGiveItem( - std::move(item), false, false)) + if (!stateMap_->updateGiveItem(std::move(item), false, false)) LogicError("Ledger::rawReplace: key not found"); } void -Ledger::rawTxInsert (uint256 const& key, - std::shared_ptr const& txn, std::shared_ptr< - Serializer const> const& metaData) +Ledger::rawTxInsert( + uint256 const& key, + std::shared_ptr const& txn, + std::shared_ptr const& metaData) { - assert (metaData); + assert(metaData); // low-level - just add to table - Serializer s(txn->getDataLength () + - metaData->getDataLength () + 16); - s.addVL (txn->peekData ()); - s.addVL (metaData->peekData ()); - auto item = std::make_shared< - SHAMapItem const> (key, std::move(s)); - if (! txMap().addGiveItem - (std::move(item), true, true)) + Serializer s(txn->getDataLength() + metaData->getDataLength() + 16); + s.addVL(txn->peekData()); + s.addVL(metaData->peekData()); + auto item = std::make_shared(key, std::move(s)); + if (!txMap().addGiveItem(std::move(item), true, true)) LogicError("duplicate_tx: " + to_string(key)); } bool -Ledger::setup (Config const& config) +Ledger::setup(Config const& config) { bool ret = true; @@ -583,17 +548,17 @@ Ledger::setup (Config const& config) { // VFALCO NOTE Why getFieldIndex and not isFieldPresent? - if (sle->getFieldIndex (sfBaseFee) != -1) - fees_.base = sle->getFieldU64 (sfBaseFee); + if (sle->getFieldIndex(sfBaseFee) != -1) + fees_.base = sle->getFieldU64(sfBaseFee); - if (sle->getFieldIndex (sfReferenceFeeUnits) != -1) - fees_.units = sle->getFieldU32 (sfReferenceFeeUnits); + if (sle->getFieldIndex(sfReferenceFeeUnits) != -1) + fees_.units = sle->getFieldU32(sfReferenceFeeUnits); - if (sle->getFieldIndex (sfReserveBase) != -1) - fees_.reserve = sle->getFieldU32 (sfReserveBase); + if (sle->getFieldIndex(sfReserveBase) != -1) + fees_.reserve = sle->getFieldU32(sfReserveBase); - if (sle->getFieldIndex (sfReserveIncrement) != -1) - fees_.increment = sle->getFieldU32 (sfReserveIncrement); + if (sle->getFieldIndex(sfReserveIncrement) != -1) + fees_.increment = sle->getFieldU32(sfReserveIncrement); } } catch (SHAMapMissingNode const&) @@ -622,96 +587,95 @@ Ledger::setup (Config const& config) } std::shared_ptr -Ledger::peek (Keylet const& k) const +Ledger::peek(Keylet const& k) const { - auto const& value = - stateMap_->peekItem(k.key); - if (! value) + auto const& value = stateMap_->peekItem(k.key); + if (!value) return nullptr; auto sle = std::make_shared( SerialIter{value->data(), value->size()}, value->key()); - if (! k.check(*sle)) + if (!k.check(*sle)) return nullptr; return sle; } //------------------------------------------------------------------------------ -bool Ledger::walkLedger (beast::Journal j) const +bool +Ledger::walkLedger(beast::Journal j) const { - std::vector missingNodes1; - std::vector missingNodes2; + std::vector missingNodes1; + std::vector missingNodes2; - if (stateMap_->getHash().isZero() && - ! info_.accountHash.isZero() && - ! stateMap_->fetchRoot (SHAMapHash{info_.accountHash}, nullptr)) + if (stateMap_->getHash().isZero() && !info_.accountHash.isZero() && + !stateMap_->fetchRoot(SHAMapHash{info_.accountHash}, nullptr)) { - missingNodes1.emplace_back (SHAMapType::STATE, SHAMapHash{info_.accountHash}); + missingNodes1.emplace_back( + SHAMapType::STATE, SHAMapHash{info_.accountHash}); } else { - stateMap_->walkMap (missingNodes1, 32); + stateMap_->walkMap(missingNodes1, 32); } - if (!missingNodes1.empty ()) + if (!missingNodes1.empty()) { if (auto stream = j.info()) { - stream << missingNodes1.size () << " missing account node(s)"; + stream << missingNodes1.size() << " missing account node(s)"; stream << "First: " << missingNodes1[0].what(); } } - if (txMap_->getHash().isZero() && - info_.txHash.isNonZero() && - ! txMap_->fetchRoot (SHAMapHash{info_.txHash}, nullptr)) + if (txMap_->getHash().isZero() && info_.txHash.isNonZero() && + !txMap_->fetchRoot(SHAMapHash{info_.txHash}, nullptr)) { - missingNodes2.emplace_back (SHAMapType::TRANSACTION, SHAMapHash{info_.txHash}); + missingNodes2.emplace_back( + SHAMapType::TRANSACTION, SHAMapHash{info_.txHash}); } else { - txMap_->walkMap (missingNodes2, 32); + txMap_->walkMap(missingNodes2, 32); } - if (!missingNodes2.empty ()) + if (!missingNodes2.empty()) { if (auto stream = j.info()) { - stream << missingNodes2.size () << " missing transaction node(s)"; + stream << missingNodes2.size() << " missing transaction node(s)"; stream << "First: " << missingNodes2[0].what(); } } - return missingNodes1.empty () && missingNodes2.empty (); + return missingNodes1.empty() && missingNodes2.empty(); } -bool Ledger::assertSane (beast::Journal ledgerJ) const +bool +Ledger::assertSane(beast::Journal ledgerJ) const { - if (info_.hash.isNonZero () && - info_.accountHash.isNonZero () && - stateMap_ && - txMap_ && - (info_.accountHash == stateMap_->getHash ().as_uint256()) && - (info_.txHash == txMap_->getHash ().as_uint256())) + if (info_.hash.isNonZero() && info_.accountHash.isNonZero() && stateMap_ && + txMap_ && (info_.accountHash == stateMap_->getHash().as_uint256()) && + (info_.txHash == txMap_->getHash().as_uint256())) { return true; } - Json::Value j = getJson (*this); + Json::Value j = getJson(*this); - j [jss::accountTreeHash] = to_string (info_.accountHash); - j [jss::transTreeHash] = to_string (info_.txHash); + j[jss::accountTreeHash] = to_string(info_.accountHash); + j[jss::transTreeHash] = to_string(info_.txHash); - JLOG (ledgerJ.fatal()) << "ledger is not sane" << j; + JLOG(ledgerJ.fatal()) << "ledger is not sane" << j; - assert (false); + assert(false); return false; } // update the skip list with the information from our previous ledger // VFALCO TODO Document this skip list concept -void Ledger::updateSkipList () +void +Ledger::updateSkipList() { - if (info_.seq == 0) // genesis ledger has no previous ledger + if (info_.seq == 0) // genesis ledger has no previous ledger return; std::uint32_t prevIndex = info_.seq - 1; @@ -724,22 +688,21 @@ void Ledger::updateSkipList () std::vector hashes; bool created; - if (! sle) + if (!sle) { sle = std::make_shared(k); created = true; } else { - hashes = static_cast( - sle->getFieldV256(sfHashes)); + hashes = static_cast(sle->getFieldV256(sfHashes)); created = false; } - assert (hashes.size () <= 256); - hashes.push_back (info_.parentHash); - sle->setFieldV256 (sfHashes, STVector256 (hashes)); - sle->setFieldU32 (sfLastLedgerSequence, prevIndex); + assert(hashes.size() <= 256); + hashes.push_back(info_.parentHash); + sle->setFieldV256(sfHashes, STVector256(hashes)); + sle->setFieldU32(sfLastLedgerSequence, prevIndex); if (created) rawInsert(sle); else @@ -749,98 +712,98 @@ void Ledger::updateSkipList () // update record of past 256 ledger auto const k = keylet::skip(); auto sle = peek(k); - std::vector hashes; + std::vector hashes; bool created; - if (! sle) + if (!sle) { sle = std::make_shared(k); created = true; } else { - hashes = static_cast( - sle->getFieldV256 (sfHashes)); + hashes = static_cast(sle->getFieldV256(sfHashes)); created = false; } - assert (hashes.size () <= 256); - if (hashes.size () == 256) - hashes.erase (hashes.begin ()); - hashes.push_back (info_.parentHash); - sle->setFieldV256 (sfHashes, STVector256 (hashes)); - sle->setFieldU32 (sfLastLedgerSequence, prevIndex); + assert(hashes.size() <= 256); + if (hashes.size() == 256) + hashes.erase(hashes.begin()); + hashes.push_back(info_.parentHash); + sle->setFieldV256(sfHashes, STVector256(hashes)); + sle->setFieldU32(sfLastLedgerSequence, prevIndex); if (created) rawInsert(sle); else rawReplace(sle); } -static bool saveValidatedLedger ( +static bool +saveValidatedLedger( Application& app, std::shared_ptr const& ledger, bool current) { - auto j = app.journal ("Ledger"); + auto j = app.journal("Ledger"); auto seq = ledger->info().seq; - if (! app.pendingSaves().startWork (seq)) + if (!app.pendingSaves().startWork(seq)) { // The save was completed synchronously - JLOG (j.debug()) << "Save aborted"; + JLOG(j.debug()) << "Save aborted"; return true; } // TODO(tom): Fix this hard-coded SQL! - JLOG (j.trace()) - << "saveValidatedLedger " - << (current ? "" : "fromAcquire ") << seq; - static boost::format deleteLedger ( + JLOG(j.trace()) << "saveValidatedLedger " << (current ? "" : "fromAcquire ") + << seq; + static boost::format deleteLedger( "DELETE FROM Ledgers WHERE LedgerSeq = %u;"); - static boost::format deleteTrans1 ( + static boost::format deleteTrans1( "DELETE FROM Transactions WHERE LedgerSeq = %u;"); - static boost::format deleteTrans2 ( + static boost::format deleteTrans2( "DELETE FROM AccountTransactions WHERE LedgerSeq = %u;"); - static boost::format deleteAcctTrans ( + static boost::format deleteAcctTrans( "DELETE FROM AccountTransactions WHERE TransID = '%s';"); - if (! ledger->info().accountHash.isNonZero ()) + if (!ledger->info().accountHash.isNonZero()) { - JLOG (j.fatal()) << "AH is zero: " - << getJson (*ledger); - assert (false); + JLOG(j.fatal()) << "AH is zero: " << getJson(*ledger); + assert(false); } - if (ledger->info().accountHash != ledger->stateMap().getHash ().as_uint256()) + if (ledger->info().accountHash != ledger->stateMap().getHash().as_uint256()) { - JLOG (j.fatal()) << "sAL: " << ledger->info().accountHash - << " != " << ledger->stateMap().getHash (); - JLOG (j.fatal()) << "saveAcceptedLedger: seq=" - << seq << ", current=" << current; - assert (false); + JLOG(j.fatal()) << "sAL: " << ledger->info().accountHash + << " != " << ledger->stateMap().getHash(); + JLOG(j.fatal()) << "saveAcceptedLedger: seq=" << seq + << ", current=" << current; + assert(false); } - assert (ledger->info().txHash == ledger->txMap().getHash ().as_uint256()); + assert(ledger->info().txHash == ledger->txMap().getHash().as_uint256()); // Save the ledger header in the hashed object store { - Serializer s (128); - s.add32 (HashPrefix::ledgerMaster); + Serializer s(128); + s.add32(HashPrefix::ledgerMaster); addRaw(ledger->info(), s); - app.getNodeStore().store(hotLEDGER, - std::move(s.modData()), ledger->info().hash, seq); + app.getNodeStore().store( + hotLEDGER, std::move(s.modData()), ledger->info().hash, seq); } AcceptedLedger::pointer aLedger; try { - aLedger = app.getAcceptedLedgerCache().fetch (ledger->info().hash); - if (! aLedger) + aLedger = app.getAcceptedLedgerCache().fetch(ledger->info().hash); + if (!aLedger) { - aLedger = std::make_shared(ledger, app.accountIDCache(), app.logs()); - app.getAcceptedLedgerCache().canonicalize_replace_client(ledger->info().hash, aLedger); + aLedger = std::make_shared( + ledger, app.accountIDCache(), app.logs()); + app.getAcceptedLedgerCache().canonicalize_replace_client( + ledger->info().hash, aLedger); } } catch (std::exception const&) { - JLOG (j.warn()) << "An accepted ledger was missing nodes"; + JLOG(j.warn()) << "An accepted ledger was missing nodes"; app.getLedgerMaster().failedSave(seq, ledger->info().hash); // Clients can now trust the database for information about this // ledger sequence. @@ -849,45 +812,45 @@ static bool saveValidatedLedger ( } { - auto db = app.getLedgerDB ().checkoutDb(); - *db << boost::str (deleteLedger % seq); + auto db = app.getLedgerDB().checkoutDb(); + *db << boost::str(deleteLedger % seq); } { - auto db = app.getTxnDB ().checkoutDb (); + auto db = app.getTxnDB().checkoutDb(); soci::transaction tr(*db); - *db << boost::str (deleteTrans1 % seq); - *db << boost::str (deleteTrans2 % seq); + *db << boost::str(deleteTrans1 % seq); + *db << boost::str(deleteTrans2 % seq); - std::string const ledgerSeq (std::to_string (seq)); + std::string const ledgerSeq(std::to_string(seq)); - for (auto const& [_, acceptedLedgerTx] : aLedger->getMap ()) + for (auto const& [_, acceptedLedgerTx] : aLedger->getMap()) { (void)_; - uint256 transactionID = acceptedLedgerTx->getTransactionID (); + uint256 transactionID = acceptedLedgerTx->getTransactionID(); - app.getMasterTransaction ().inLedger ( - transactionID, seq); + app.getMasterTransaction().inLedger(transactionID, seq); - std::string const txnId (to_string (transactionID)); - std::string const txnSeq (std::to_string (acceptedLedgerTx->getTxnSeq ())); + std::string const txnId(to_string(transactionID)); + std::string const txnSeq( + std::to_string(acceptedLedgerTx->getTxnSeq())); - *db << boost::str (deleteAcctTrans % transactionID); + *db << boost::str(deleteAcctTrans % transactionID); - auto const& accts = acceptedLedgerTx->getAffected (); + auto const& accts = acceptedLedgerTx->getAffected(); - if (!accts.empty ()) + if (!accts.empty()) { - std::string sql ( + std::string sql( "INSERT INTO AccountTransactions " "(TransID, Account, LedgerSeq, TxnSeq) VALUES "); // Try to make an educated guess on how much space we'll need // for our arguments. In argument order we have: // 64 + 34 + 10 + 10 = 118 + 10 extra = 128 bytes - sql.reserve (sql.length () + (accts.size () * 128)); + sql.reserve(sql.length() + (accts.size() * 128)); bool first = true; for (auto const& account : accts) @@ -910,25 +873,25 @@ static bool saveValidatedLedger ( sql += ")"; } sql += ";"; - JLOG (j.trace()) << "ActTx: " << sql; + JLOG(j.trace()) << "ActTx: " << sql; *db << sql; } else { - JLOG (j.warn()) - << "Transaction in ledger " << seq - << " affects no accounts"; - JLOG (j.warn()) + JLOG(j.warn()) << "Transaction in ledger " << seq + << " affects no accounts"; + JLOG(j.warn()) << acceptedLedgerTx->getTxn()->getJson(JsonOptions::none); } - *db << - (STTx::getMetaSQLInsertReplaceHeader () + - acceptedLedgerTx->getTxn ()->getMetaSQL ( - seq, acceptedLedgerTx->getEscMeta ()) + ";"); + *db + << (STTx::getMetaSQLInsertReplaceHeader() + + acceptedLedgerTx->getTxn()->getMetaSQL( + seq, acceptedLedgerTx->getEscMeta()) + + ";"); } - tr.commit (); + tr.commit(); } { @@ -940,13 +903,13 @@ static bool saveValidatedLedger ( (:ledgerHash,:ledgerSeq,:prevHash,:totalCoins,:closingTime,:prevClosingTime, :closeTimeRes,:closeFlags,:accountSetHash,:transSetHash);)sql"); - auto db (app.getLedgerDB ().checkoutDb ()); + auto db(app.getLedgerDB().checkoutDb()); soci::transaction tr(*db); - auto const hash = to_string (ledger->info().hash); - auto const parentHash = to_string (ledger->info().parentHash); - auto const drops = to_string (ledger->info().drops); + auto const hash = to_string(ledger->info().hash); + auto const parentHash = to_string(ledger->info().parentHash); + auto const drops = to_string(ledger->info().drops); auto const closeTime = ledger->info().closeTime.time_since_epoch().count(); auto const parentCloseTime = @@ -954,20 +917,13 @@ static bool saveValidatedLedger ( auto const closeTimeResolution = ledger->info().closeTimeResolution.count(); auto const closeFlags = ledger->info().closeFlags; - auto const accountHash = to_string (ledger->info().accountHash); - auto const txHash = to_string (ledger->info().txHash); + auto const accountHash = to_string(ledger->info().accountHash); + auto const txHash = to_string(ledger->info().txHash); - *db << addLedger, - soci::use(hash), - soci::use(seq), - soci::use(parentHash), - soci::use(drops), - soci::use(closeTime), - soci::use(parentCloseTime), - soci::use(closeTimeResolution), - soci::use(closeFlags), - soci::use(accountHash), - soci::use(txHash); + *db << addLedger, soci::use(hash), soci::use(seq), + soci::use(parentHash), soci::use(drops), soci::use(closeTime), + soci::use(parentCloseTime), soci::use(closeTimeResolution), + soci::use(closeFlags), soci::use(accountHash), soci::use(txHash); tr.commit(); } @@ -981,21 +937,20 @@ static bool saveValidatedLedger ( /** Save, or arrange to save, a fully-validated ledger Returns false on error */ -bool pendSaveValidated ( +bool +pendSaveValidated( Application& app, std::shared_ptr const& ledger, bool isSynchronous, bool isCurrent) { - if (! app.getHashRouter ().setFlags (ledger->info().hash, SF_SAVED)) + if (!app.getHashRouter().setFlags(ledger->info().hash, SF_SAVED)) { // We have tried to save this ledger recently - auto stream = app.journal ("Ledger").debug(); - JLOG (stream) << "Double pend save for " - << ledger->info().seq; + auto stream = app.journal("Ledger").debug(); + JLOG(stream) << "Double pend save for " << ledger->info().seq; - if (! isSynchronous || - ! app.pendingSaves().pending (ledger->info().seq)) + if (!isSynchronous || !app.pendingSaves().pending(ledger->info().seq)) { // Either we don't need it to be finished // or it is finished @@ -1003,28 +958,27 @@ bool pendSaveValidated ( } } - assert (ledger->isImmutable ()); + assert(ledger->isImmutable()); - if (! app.pendingSaves().shouldWork (ledger->info().seq, isSynchronous)) + if (!app.pendingSaves().shouldWork(ledger->info().seq, isSynchronous)) { - auto stream = app.journal ("Ledger").debug(); - JLOG (stream) - << "Pend save with seq in pending saves " - << ledger->info().seq; + auto stream = app.journal("Ledger").debug(); + JLOG(stream) << "Pend save with seq in pending saves " + << ledger->info().seq; return true; } - JobType const jobType {isCurrent ? jtPUBLEDGER : jtPUBOLDLEDGER}; - char const* const jobName { + JobType const jobType{isCurrent ? jtPUBLEDGER : jtPUBOLDLEDGER}; + char const* const jobName{ isCurrent ? "Ledger::pendSave" : "Ledger::pendOldSave"}; // See if we can use the JobQueue. if (!isSynchronous && - app.getJobQueue().addJob (jobType, jobName, - [&app, ledger, isCurrent] (Job&) { - saveValidatedLedger(app, ledger, isCurrent); - })) + app.getJobQueue().addJob( + jobType, jobName, [&app, ledger, isCurrent](Job&) { + saveValidatedLedger(app, ledger, isCurrent); + })) { return true; } @@ -1058,13 +1012,12 @@ Ledger::invariants() const * @return The ledger, ledger sequence, and ledger hash. */ std::tuple, std::uint32_t, uint256> -loadLedgerHelper(std::string const& sqlSuffix, - Application& app, bool acquire) +loadLedgerHelper(std::string const& sqlSuffix, Application& app, bool acquire) { uint256 ledgerHash{}; std::uint32_t ledgerSeq{0}; - auto db = app.getLedgerDB ().checkoutDb (); + auto db = app.getLedgerDB().checkoutDb(); boost::optional sLedgerHash, sPrevHash, sAccountHash, sTransHash; @@ -1072,47 +1025,38 @@ loadLedgerHelper(std::string const& sqlSuffix, closeResolution, closeFlags, ledgerSeq64; std::string const sql = - "SELECT " - "LedgerHash, PrevHash, AccountSetHash, TransSetHash, " - "TotalCoins," - "ClosingTime, PrevClosingTime, CloseTimeRes, CloseFlags," - "LedgerSeq from Ledgers " + - sqlSuffix + ";"; + "SELECT " + "LedgerHash, PrevHash, AccountSetHash, TransSetHash, " + "TotalCoins," + "ClosingTime, PrevClosingTime, CloseTimeRes, CloseFlags," + "LedgerSeq from Ledgers " + + sqlSuffix + ";"; - *db << sql, - soci::into(sLedgerHash), - soci::into(sPrevHash), - soci::into(sAccountHash), - soci::into(sTransHash), - soci::into(totDrops), - soci::into(closingTime), - soci::into(prevClosingTime), - soci::into(closeResolution), - soci::into(closeFlags), - soci::into(ledgerSeq64); + *db << sql, soci::into(sLedgerHash), soci::into(sPrevHash), + soci::into(sAccountHash), soci::into(sTransHash), soci::into(totDrops), + soci::into(closingTime), soci::into(prevClosingTime), + soci::into(closeResolution), soci::into(closeFlags), + soci::into(ledgerSeq64); - if (!db->got_data ()) + if (!db->got_data()) { auto stream = app.journal("Ledger").debug(); - JLOG (stream) << "Ledger not found: " << sqlSuffix; - return std::make_tuple ( - std::shared_ptr(), - ledgerSeq, - ledgerHash); + JLOG(stream) << "Ledger not found: " << sqlSuffix; + return std::make_tuple( + std::shared_ptr(), ledgerSeq, ledgerHash); } - ledgerSeq = - rangeCheckedCast(ledgerSeq64.value_or (0)); + ledgerSeq = rangeCheckedCast(ledgerSeq64.value_or(0)); uint256 prevHash{}, accountHash{}, transHash{}; if (sLedgerHash) - ledgerHash.SetHexExact (*sLedgerHash); + ledgerHash.SetHexExact(*sLedgerHash); if (sPrevHash) - prevHash.SetHexExact (*sPrevHash); + prevHash.SetHexExact(*sPrevHash); if (sAccountHash) - accountHash.SetHexExact (*sAccountHash); + accountHash.SetHexExact(*sAccountHash); if (sTransHash) - transHash.SetHexExact (*sTransHash); + transHash.SetHexExact(*sTransHash); using time_point = NetClock::time_point; using duration = NetClock::duration; @@ -1140,11 +1084,11 @@ loadLedgerHelper(std::string const& sqlSuffix, if (!loaded) ledger.reset(); - return std::make_tuple (ledger, ledgerSeq, ledgerHash); + return std::make_tuple(ledger, ledgerSeq, ledgerHash); } -static -void finishLoadByIndexOrHash( +static void +finishLoadByIndexOrHash( std::shared_ptr const& ledger, Config const& config, beast::Journal j) @@ -1152,153 +1096,141 @@ void finishLoadByIndexOrHash( if (!ledger) return; - ledger->setImmutable (config); + ledger->setImmutable(config); - JLOG (j.trace()) - << "Loaded ledger: " << to_string (ledger->info().hash); + JLOG(j.trace()) << "Loaded ledger: " << to_string(ledger->info().hash); - ledger->setFull (); + ledger->setFull(); } std::shared_ptr -loadByIndex (std::uint32_t ledgerIndex, - Application& app, bool acquire) +loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire) { std::shared_ptr ledger; { std::ostringstream s; s << "WHERE LedgerSeq = " << ledgerIndex; - std::tie (ledger, std::ignore, std::ignore) = - loadLedgerHelper (s.str (), app, acquire); + std::tie(ledger, std::ignore, std::ignore) = + loadLedgerHelper(s.str(), app, acquire); } - finishLoadByIndexOrHash (ledger, app.config(), - app.journal ("Ledger")); + finishLoadByIndexOrHash(ledger, app.config(), app.journal("Ledger")); return ledger; } std::shared_ptr -loadByHash (uint256 const& ledgerHash, - Application& app, bool acquire) +loadByHash(uint256 const& ledgerHash, Application& app, bool acquire) { std::shared_ptr ledger; { std::ostringstream s; s << "WHERE LedgerHash = '" << ledgerHash << "'"; - std::tie (ledger, std::ignore, std::ignore) = - loadLedgerHelper (s.str (), app, acquire); + std::tie(ledger, std::ignore, std::ignore) = + loadLedgerHelper(s.str(), app, acquire); } - finishLoadByIndexOrHash (ledger, app.config(), - app.journal ("Ledger")); + finishLoadByIndexOrHash(ledger, app.config(), app.journal("Ledger")); - assert (!ledger || ledger->info().hash == ledgerHash); + assert(!ledger || ledger->info().hash == ledgerHash); return ledger; } uint256 -getHashByIndex (std::uint32_t ledgerIndex, Application& app) +getHashByIndex(std::uint32_t ledgerIndex, Application& app) { uint256 ret; std::string sql = "SELECT LedgerHash FROM Ledgers INDEXED BY SeqLedger WHERE LedgerSeq='"; - sql.append (beast::lexicalCastThrow (ledgerIndex)); - sql.append ("';"); + sql.append(beast::lexicalCastThrow(ledgerIndex)); + sql.append("';"); std::string hash; { - auto db = app.getLedgerDB ().checkoutDb (); + auto db = app.getLedgerDB().checkoutDb(); boost::optional lh; - *db << sql, - soci::into (lh); + *db << sql, soci::into(lh); - if (!db->got_data () || !lh) + if (!db->got_data() || !lh) return ret; hash = *lh; - if (hash.empty ()) + if (hash.empty()) return ret; } - ret.SetHexExact (hash); + ret.SetHexExact(hash); return ret; } bool -getHashesByIndex(std::uint32_t ledgerIndex, - uint256& ledgerHash, uint256& parentHash, - Application& app) +getHashesByIndex( + std::uint32_t ledgerIndex, + uint256& ledgerHash, + uint256& parentHash, + Application& app) { - auto db = app.getLedgerDB ().checkoutDb (); + auto db = app.getLedgerDB().checkoutDb(); - boost::optional lhO, phO; + boost::optional lhO, phO; *db << "SELECT LedgerHash,PrevHash FROM Ledgers " - "INDEXED BY SeqLedger Where LedgerSeq = :ls;", - soci::into (lhO), - soci::into (phO), - soci::use (ledgerIndex); + "INDEXED BY SeqLedger Where LedgerSeq = :ls;", + soci::into(lhO), soci::into(phO), soci::use(ledgerIndex); if (!lhO || !phO) { - auto stream = app.journal ("Ledger").trace(); - JLOG (stream) - << "Don't have ledger " << ledgerIndex; + auto stream = app.journal("Ledger").trace(); + JLOG(stream) << "Don't have ledger " << ledgerIndex; return false; } - ledgerHash.SetHexExact (*lhO); - parentHash.SetHexExact (*phO); + ledgerHash.SetHexExact(*lhO); + parentHash.SetHexExact(*phO); return true; } -std::map< std::uint32_t, std::pair > -getHashesByIndex (std::uint32_t minSeq, std::uint32_t maxSeq, - Application& app) +std::map> +getHashesByIndex(std::uint32_t minSeq, std::uint32_t maxSeq, Application& app) { - std::map< std::uint32_t, std::pair > ret; + std::map> ret; std::string sql = "SELECT LedgerSeq,LedgerHash,PrevHash FROM Ledgers WHERE LedgerSeq >= "; - sql.append (beast::lexicalCastThrow (minSeq)); - sql.append (" AND LedgerSeq <= "); - sql.append (beast::lexicalCastThrow (maxSeq)); - sql.append (";"); + sql.append(beast::lexicalCastThrow(minSeq)); + sql.append(" AND LedgerSeq <= "); + sql.append(beast::lexicalCastThrow(maxSeq)); + sql.append(";"); - auto db = app.getLedgerDB ().checkoutDb (); + auto db = app.getLedgerDB().checkoutDb(); std::uint64_t ls; std::string lh; boost::optional ph; soci::statement st = - (db->prepare << sql, - soci::into (ls), - soci::into (lh), - soci::into (ph)); + (db->prepare << sql, soci::into(ls), soci::into(lh), soci::into(ph)); - st.execute (); - while (st.fetch ()) + st.execute(); + while (st.fetch()) { std::pair& hashes = - ret[rangeCheckedCast(ls)]; - hashes.first.SetHexExact (lh); + ret[rangeCheckedCast(ls)]; + hashes.first.SetHexExact(lh); if (ph) - hashes.second.SetHexExact (*ph); + hashes.second.SetHexExact(*ph); else - hashes.second.zero (); + hashes.second.zero(); if (!ph) { - auto stream = app.journal ("Ledger").warn(); - JLOG (stream) - << "Null prev hash for ledger seq: " << ls; + auto stream = app.journal("Ledger").warn(); + JLOG(stream) << "Null prev hash for ledger seq: " << ls; } } return ret; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/Ledger.h b/src/ripple/app/ledger/Ledger.h index fa60a8a1e6..baf4e2f409 100644 --- a/src/ripple/app/ledger/Ledger.h +++ b/src/ripple/app/ledger/Ledger.h @@ -20,17 +20,17 @@ #ifndef RIPPLE_APP_LEDGER_LEDGER_H_INCLUDED #define RIPPLE_APP_LEDGER_LEDGER_H_INCLUDED +#include +#include +#include +#include #include #include -#include -#include -#include +#include #include #include #include -#include #include -#include #include #include @@ -74,17 +74,21 @@ extern create_genesis_t const create_genesis; @note Presented to clients as ReadView @note Calls virtuals in the constructor, so marked as final */ -class Ledger final - : public std::enable_shared_from_this - , public DigestAwareReadView - , public TxsRawView - , public CountedObject +class Ledger final : public std::enable_shared_from_this, + public DigestAwareReadView, + public TxsRawView, + public CountedObject { public: - static char const* getCountedObjectName () { return "Ledger"; } + static char const* + getCountedObjectName() + { + return "Ledger"; + } - Ledger (Ledger const&) = delete; - Ledger& operator= (Ledger const&) = delete; + Ledger(Ledger const&) = delete; + Ledger& + operator=(Ledger const&) = delete; /** Create the Genesis ledger. @@ -100,22 +104,19 @@ public: Amendments specified are enabled in the genesis ledger */ - Ledger ( + Ledger( create_genesis_t, Config const& config, std::vector const& amendments, Family& family); - Ledger ( - LedgerInfo const& info, - Config const& config, - Family& family); + Ledger(LedgerInfo const& info, Config const& config, Family& family); /** Used for ledgers loaded from JSON files @param acquire If true, acquires the ledger if not found locally */ - Ledger ( + Ledger( LedgerInfo const& info, bool& loaded, bool acquire, @@ -129,13 +130,14 @@ public: follows previous, and have parentCloseTime == previous.closeTime. */ - Ledger (Ledger const& previous, - NetClock::time_point closeTime); + Ledger(Ledger const& previous, NetClock::time_point closeTime); // used for database ledgers - Ledger (std::uint32_t ledgerSeq, - NetClock::time_point closeTime, Config const& config, - Family& family); + Ledger( + std::uint32_t ledgerSeq, + NetClock::time_point closeTime, + Config const& config, + Family& family); ~Ledger() = default; @@ -168,14 +170,14 @@ public: } bool - exists (Keylet const& k) const override; + exists(Keylet const& k) const override; boost::optional - succ (uint256 const& key, boost::optional< - uint256> const& last = boost::none) const override; + succ(uint256 const& key, boost::optional const& last = boost::none) + const override; std::shared_ptr - read (Keylet const& k) const override; + read(Keylet const& k) const override; std::unique_ptr slesBegin() const override; @@ -193,36 +195,33 @@ public: txsEnd() const override; bool - txExists (uint256 const& key) const override; + txExists(uint256 const& key) const override; tx_type - txRead (key_type const& key) const override; + txRead(key_type const& key) const override; // // DigestAwareReadView // boost::optional - digest (key_type const& key) const override; + digest(key_type const& key) const override; // // RawView // void - rawErase (std::shared_ptr< - SLE> const& sle) override; + rawErase(std::shared_ptr const& sle) override; void - rawInsert (std::shared_ptr< - SLE> const& sle) override; + rawInsert(std::shared_ptr const& sle) override; void - rawReplace (std::shared_ptr< - SLE> const& sle) override; + rawReplace(std::shared_ptr const& sle) override; void - rawDestroyXRP (XRPAmount const& fee) override + rawDestroyXRP(XRPAmount const& fee) override { info_.drops -= fee; } @@ -232,25 +231,31 @@ public: // void - rawTxInsert (uint256 const& key, - std::shared_ptr const& txn, std::shared_ptr< - Serializer const> const& metaData) override; + rawTxInsert( + uint256 const& key, + std::shared_ptr const& txn, + std::shared_ptr const& metaData) override; //-------------------------------------------------------------------------- - void setValidated() const + void + setValidated() const { info_.validated = true; } - void setAccepted (NetClock::time_point closeTime, - NetClock::duration closeResolution, bool correctCloseTime, - Config const& config); + void + setAccepted( + NetClock::time_point closeTime, + NetClock::duration closeResolution, + bool correctCloseTime, + Config const& config); - void setImmutable (Config const& config); + void + setImmutable(Config const& config); - bool isImmutable () const + bool + isImmutable() const { return mImmutable; } @@ -274,7 +279,8 @@ public: stateMap_->setLedgerSeq(info_.seq); } - void setTotalDrops (std::uint64_t totDrops) + void + setTotalDrops(std::uint64_t totDrops) { info_.drops = totDrops; } @@ -304,27 +310,34 @@ public: } // returns false on error - bool addSLE (SLE const& sle); + bool + addSLE(SLE const& sle); //-------------------------------------------------------------------------- - void updateSkipList (); + void + updateSkipList(); - bool walkLedger (beast::Journal j) const; + bool + walkLedger(beast::Journal j) const; - bool assertSane (beast::Journal ledgerJ) const; + bool + assertSane(beast::Journal ledgerJ) const; + + void + invariants() const; + void + unshare() const; - void invariants() const; - void unshare() const; private: class sles_iter_impl; class txs_iter_impl; bool - setup (Config const& config); + setup(Config const& config); std::shared_ptr - peek (Keylet const& k) const; + peek(Keylet const& k) const; bool mImmutable; @@ -348,44 +361,38 @@ using CachedLedger = CachedView; // //------------------------------------------------------------------------------ -extern -bool +extern bool pendSaveValidated( Application& app, std::shared_ptr const& ledger, bool isSynchronous, bool isCurrent); -extern -std::shared_ptr -loadByIndex (std::uint32_t ledgerIndex, - Application& app, bool acquire = true); +extern std::shared_ptr +loadByIndex(std::uint32_t ledgerIndex, Application& app, bool acquire = true); -extern -std::tuple, std::uint32_t, uint256> -loadLedgerHelper(std::string const& sqlSuffix, - Application& app, bool acquire = true); +extern std::tuple, std::uint32_t, uint256> +loadLedgerHelper( + std::string const& sqlSuffix, + Application& app, + bool acquire = true); -extern -std::shared_ptr -loadByHash (uint256 const& ledgerHash, - Application& app, bool acquire = true); +extern std::shared_ptr +loadByHash(uint256 const& ledgerHash, Application& app, bool acquire = true); -extern -uint256 +extern uint256 getHashByIndex(std::uint32_t index, Application& app); -extern -bool -getHashesByIndex(std::uint32_t index, - uint256 &ledgerHash, uint256& parentHash, - Application& app); - -extern -std::map< std::uint32_t, std::pair> -getHashesByIndex (std::uint32_t minSeq, std::uint32_t maxSeq, +extern bool +getHashesByIndex( + std::uint32_t index, + uint256& ledgerHash, + uint256& parentHash, Application& app); +extern std::map> +getHashesByIndex(std::uint32_t minSeq, std::uint32_t maxSeq, Application& app); + /** Deserialize a SHAMapItem containing a single STTx Throw: @@ -393,7 +400,7 @@ getHashesByIndex (std::uint32_t minSeq, std::uint32_t maxSeq, May throw on deserializaton error */ std::shared_ptr -deserializeTx (SHAMapItem const& item); +deserializeTx(SHAMapItem const& item); /** Deserialize a SHAMapItem containing STTx + STObject metadata @@ -404,11 +411,9 @@ deserializeTx (SHAMapItem const& item); May throw on deserializaton error */ -std::pair, std::shared_ptr< - STObject const>> -deserializeTxPlusMeta (SHAMapItem const& item); +std::pair, std::shared_ptr> +deserializeTxPlusMeta(SHAMapItem const& item); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LedgerCleaner.h b/src/ripple/app/ledger/LedgerCleaner.h index eb482b314c..d3ee4add06 100644 --- a/src/ripple/app/ledger/LedgerCleaner.h +++ b/src/ripple/app/ledger/LedgerCleaner.h @@ -21,26 +21,24 @@ #define RIPPLE_APP_LEDGER_LEDGERCLEANER_H_INCLUDED #include -#include -#include -#include #include +#include +#include +#include #include namespace ripple { namespace detail { /** Check the ledger/transaction databases to make sure they have continuity */ -class LedgerCleaner - : public Stoppable - , public beast::PropertyStream::Source +class LedgerCleaner : public Stoppable, public beast::PropertyStream::Source { protected: - explicit LedgerCleaner (Stoppable& parent); + explicit LedgerCleaner(Stoppable& parent); public: /** Destroy the object. */ - virtual ~LedgerCleaner () = 0; + virtual ~LedgerCleaner() = 0; /** Start a long running task to clean the ledger. The ledger is cleaned asynchronously, on an implementation defined @@ -52,14 +50,14 @@ public: @param parameters A Json object with configurable parameters. */ - virtual void doClean (Json::Value const& parameters) = 0; + virtual void + doClean(Json::Value const& parameters) = 0; }; std::unique_ptr -make_LedgerCleaner (Application& app, - Stoppable& parent, beast::Journal journal); +make_LedgerCleaner(Application& app, Stoppable& parent, beast::Journal journal); -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LedgerHistory.cpp b/src/ripple/app/ledger/LedgerHistory.cpp index aa07e8ad40..98a5e43451 100644 --- a/src/ripple/app/ledger/LedgerHistory.cpp +++ b/src/ripple/app/ledger/LedgerHistory.cpp @@ -36,31 +36,37 @@ std::chrono::seconds constexpr CachedLedgerAge = std::chrono::minutes{2}; // FIXME: Need to clean up ledgers by index at some point -LedgerHistory::LedgerHistory ( +LedgerHistory::LedgerHistory( beast::insight::Collector::ptr const& collector, - Application& app) - : app_ (app) - , collector_ (collector) - , mismatch_counter_ (collector->make_counter ("ledger.history", "mismatch")) - , m_ledgers_by_hash ("LedgerCache", CACHED_LEDGER_NUM, CachedLedgerAge, - stopwatch(), app_.journal("TaggedCache")) - , m_consensus_validated ("ConsensusValidated", 64, std::chrono::minutes {5}, - stopwatch(), app_.journal("TaggedCache")) - , j_ (app.journal ("LedgerHistory")) + Application& app) + : app_(app) + , collector_(collector) + , mismatch_counter_(collector->make_counter("ledger.history", "mismatch")) + , m_ledgers_by_hash( + "LedgerCache", + CACHED_LEDGER_NUM, + CachedLedgerAge, + stopwatch(), + app_.journal("TaggedCache")) + , m_consensus_validated( + "ConsensusValidated", + 64, + std::chrono::minutes{5}, + stopwatch(), + app_.journal("TaggedCache")) + , j_(app.journal("LedgerHistory")) { } bool -LedgerHistory::insert( - std::shared_ptr ledger, - bool validated) +LedgerHistory::insert(std::shared_ptr ledger, bool validated) { - if(! ledger->isImmutable()) + if (!ledger->isImmutable()) LogicError("mutable Ledger in insert"); - assert (ledger->stateMap().getHash ().isNonZero ()); + assert(ledger->stateMap().getHash().isNonZero()); - std::unique_lock sl (m_ledgers_by_hash.peekMutex ()); + std::unique_lock sl(m_ledgers_by_hash.peekMutex()); const bool alreadyHad = m_ledgers_by_hash.canonicalize_replace_cache( ledger->info().hash, ledger); @@ -70,44 +76,45 @@ LedgerHistory::insert( return alreadyHad; } -LedgerHash LedgerHistory::getLedgerHash (LedgerIndex index) +LedgerHash +LedgerHistory::getLedgerHash(LedgerIndex index) { - std::unique_lock sl (m_ledgers_by_hash.peekMutex ()); - auto it = mLedgersByIndex.find (index); + std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + auto it = mLedgersByIndex.find(index); - if (it != mLedgersByIndex.end ()) + if (it != mLedgersByIndex.end()) return it->second; - return uint256 (); + return uint256(); } std::shared_ptr -LedgerHistory::getLedgerBySeq (LedgerIndex index) +LedgerHistory::getLedgerBySeq(LedgerIndex index) { { - std::unique_lock sl (m_ledgers_by_hash.peekMutex ()); - auto it = mLedgersByIndex.find (index); + std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + auto it = mLedgersByIndex.find(index); - if (it != mLedgersByIndex.end ()) + if (it != mLedgersByIndex.end()) { uint256 hash = it->second; - sl.unlock (); - return getLedgerByHash (hash); + sl.unlock(); + return getLedgerByHash(hash); } } - std::shared_ptr ret = loadByIndex (index, app_); + std::shared_ptr ret = loadByIndex(index, app_); if (!ret) return ret; - assert (ret->info().seq == index); + assert(ret->info().seq == index); { // Add this ledger to the local tracking by index - std::unique_lock sl (m_ledgers_by_hash.peekMutex ()); + std::unique_lock sl(m_ledgers_by_hash.peekMutex()); - assert (ret->isImmutable ()); + assert(ret->isImmutable()); m_ledgers_by_hash.canonicalize_replace_client(ret->info().hash, ret); mLedgersByIndex[ret->info().seq] = ret->info().hash; return (ret->info().seq == index) ? ret : nullptr; @@ -115,32 +122,31 @@ LedgerHistory::getLedgerBySeq (LedgerIndex index) } std::shared_ptr -LedgerHistory::getLedgerByHash (LedgerHash const& hash) +LedgerHistory::getLedgerByHash(LedgerHash const& hash) { - auto ret = m_ledgers_by_hash.fetch (hash); + auto ret = m_ledgers_by_hash.fetch(hash); if (ret) { - assert (ret->isImmutable ()); - assert (ret->info().hash == hash); + assert(ret->isImmutable()); + assert(ret->info().hash == hash); return ret; } - ret = loadByHash (hash, app_); + ret = loadByHash(hash, app_); if (!ret) return ret; - assert (ret->isImmutable ()); - assert (ret->info().hash == hash); + assert(ret->isImmutable()); + assert(ret->info().hash == hash); m_ledgers_by_hash.canonicalize_replace_client(ret->info().hash, ret); - assert (ret->info().hash == hash); + assert(ret->info().hash == hash); return ret; } -static -void +static void log_one( ReadView const& ledger, uint256 const& tx, @@ -151,19 +157,18 @@ log_one( if (metaData != nullptr) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": " << msg << " is missing this transaction:\n" << - metaData->getJson (JsonOptions::none); + JLOG(j.debug()) << "MISMATCH on TX " << tx << ": " << msg + << " is missing this transaction:\n" + << metaData->getJson(JsonOptions::none); } else { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": " << msg << " is missing this transaction."; + JLOG(j.debug()) << "MISMATCH on TX " << tx << ": " << msg + << " is missing this transaction."; } } -static -void +static void log_metadata_difference( ReadView const& builtLedger, ReadView const& validLedger, @@ -171,35 +176,34 @@ log_metadata_difference( beast::Journal j) { auto getMeta = [](ReadView const& ledger, - uint256 const& txID) -> std::shared_ptr - { + uint256 const& txID) -> std::shared_ptr { auto meta = ledger.txRead(txID).second; if (!meta) return {}; - return std::make_shared (txID, ledger.seq(), *meta); + return std::make_shared(txID, ledger.seq(), *meta); }; - auto validMetaData = getMeta (validLedger, tx); - auto builtMetaData = getMeta (builtLedger, tx); + auto validMetaData = getMeta(validLedger, tx); + auto builtMetaData = getMeta(builtLedger, tx); assert(validMetaData != nullptr || builtMetaData != nullptr); if (validMetaData != nullptr && builtMetaData != nullptr) { - auto const& validNodes = validMetaData->getNodes (); - auto const& builtNodes = builtMetaData->getNodes (); + auto const& validNodes = validMetaData->getNodes(); + auto const& builtNodes = builtMetaData->getNodes(); bool const result_diff = - validMetaData->getResultTER () != builtMetaData->getResultTER (); + validMetaData->getResultTER() != builtMetaData->getResultTER(); bool const index_diff = - validMetaData->getIndex() != builtMetaData->getIndex (); + validMetaData->getIndex() != builtMetaData->getIndex(); bool const nodes_diff = validNodes != builtNodes; if (!result_diff && !index_diff && !nodes_diff) { - JLOG (j.error()) << "MISMATCH on TX " << tx << - ": No apparent mismatches detected!"; + JLOG(j.error()) << "MISMATCH on TX " << tx + << ": No apparent mismatches detected!"; return; } @@ -207,105 +211,111 @@ log_metadata_difference( { if (result_diff && index_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different result and index!"; - JLOG (j.debug()) << " Built:" << - " Result: " << builtMetaData->getResult () << - " Index: " << builtMetaData->getIndex (); - JLOG (j.debug()) << " Valid:" << - " Result: " << validMetaData->getResult () << - " Index: " << validMetaData->getIndex (); + JLOG(j.debug()) << "MISMATCH on TX " << tx + << ": Different result and index!"; + JLOG(j.debug()) << " Built:" + << " Result: " << builtMetaData->getResult() + << " Index: " << builtMetaData->getIndex(); + JLOG(j.debug()) << " Valid:" + << " Result: " << validMetaData->getResult() + << " Index: " << validMetaData->getIndex(); } else if (result_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different result!"; - JLOG (j.debug()) << " Built:" << - " Result: " << builtMetaData->getResult (); - JLOG (j.debug()) << " Valid:" << - " Result: " << validMetaData->getResult (); + JLOG(j.debug()) + << "MISMATCH on TX " << tx << ": Different result!"; + JLOG(j.debug()) << " Built:" + << " Result: " << builtMetaData->getResult(); + JLOG(j.debug()) << " Valid:" + << " Result: " << validMetaData->getResult(); } else if (index_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different index!"; - JLOG (j.debug()) << " Built:" << - " Index: " << builtMetaData->getIndex (); - JLOG (j.debug()) << " Valid:" << - " Index: " << validMetaData->getIndex (); + JLOG(j.debug()) + << "MISMATCH on TX " << tx << ": Different index!"; + JLOG(j.debug()) << " Built:" + << " Index: " << builtMetaData->getIndex(); + JLOG(j.debug()) << " Valid:" + << " Index: " << validMetaData->getIndex(); } } else { if (result_diff && index_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different result, index and nodes!"; - JLOG (j.debug()) << " Built:\n" << - builtMetaData->getJson (JsonOptions::none); - JLOG (j.debug()) << " Valid:\n" << - validMetaData->getJson (JsonOptions::none); + JLOG(j.debug()) << "MISMATCH on TX " << tx + << ": Different result, index and nodes!"; + JLOG(j.debug()) << " Built:\n" + << builtMetaData->getJson(JsonOptions::none); + JLOG(j.debug()) << " Valid:\n" + << validMetaData->getJson(JsonOptions::none); } else if (result_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different result and nodes!"; - JLOG (j.debug()) << " Built:" << - " Result: " << builtMetaData->getResult () << - " Nodes:\n" << builtNodes.getJson (JsonOptions::none); - JLOG (j.debug()) << " Valid:" << - " Result: " << validMetaData->getResult () << - " Nodes:\n" << validNodes.getJson (JsonOptions::none); + JLOG(j.debug()) << "MISMATCH on TX " << tx + << ": Different result and nodes!"; + JLOG(j.debug()) + << " Built:" + << " Result: " << builtMetaData->getResult() << " Nodes:\n" + << builtNodes.getJson(JsonOptions::none); + JLOG(j.debug()) + << " Valid:" + << " Result: " << validMetaData->getResult() << " Nodes:\n" + << validNodes.getJson(JsonOptions::none); } else if (index_diff) { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different index and nodes!"; - JLOG (j.debug()) << " Built:" << - " Index: " << builtMetaData->getIndex () << - " Nodes:\n" << builtNodes.getJson (JsonOptions::none); - JLOG (j.debug()) << " Valid:" << - " Index: " << validMetaData->getIndex () << - " Nodes:\n" << validNodes.getJson (JsonOptions::none); + JLOG(j.debug()) << "MISMATCH on TX " << tx + << ": Different index and nodes!"; + JLOG(j.debug()) + << " Built:" + << " Index: " << builtMetaData->getIndex() << " Nodes:\n" + << builtNodes.getJson(JsonOptions::none); + JLOG(j.debug()) + << " Valid:" + << " Index: " << validMetaData->getIndex() << " Nodes:\n" + << validNodes.getJson(JsonOptions::none); } - else // nodes_diff + else // nodes_diff { - JLOG (j.debug()) << "MISMATCH on TX " << tx << - ": Different nodes!"; - JLOG (j.debug()) << " Built:" << - " Nodes:\n" << builtNodes.getJson (JsonOptions::none); - JLOG (j.debug()) << " Valid:" << - " Nodes:\n" << validNodes.getJson (JsonOptions::none); + JLOG(j.debug()) + << "MISMATCH on TX " << tx << ": Different nodes!"; + JLOG(j.debug()) << " Built:" + << " Nodes:\n" + << builtNodes.getJson(JsonOptions::none); + JLOG(j.debug()) << " Valid:" + << " Nodes:\n" + << validNodes.getJson(JsonOptions::none); } } } else if (validMetaData != nullptr) { - JLOG (j.error()) << "MISMATCH on TX " << tx << - ": Metadata Difference (built has none)\n" << - validMetaData->getJson (JsonOptions::none); + JLOG(j.error()) << "MISMATCH on TX " << tx + << ": Metadata Difference (built has none)\n" + << validMetaData->getJson(JsonOptions::none); } - else // builtMetaData != nullptr + else // builtMetaData != nullptr { - JLOG (j.error()) << "MISMATCH on TX " << tx << - ": Metadata Difference (valid has none)\n" << - builtMetaData->getJson (JsonOptions::none); + JLOG(j.error()) << "MISMATCH on TX " << tx + << ": Metadata Difference (valid has none)\n" + << builtMetaData->getJson(JsonOptions::none); } } //------------------------------------------------------------------------------ // Return list of leaves sorted by key -static -std::vector -leaves (SHAMap const& sm) +static std::vector +leaves(SHAMap const& sm) { std::vector v; for (auto const& item : sm) v.push_back(&item); - std::sort(v.begin(), v.end(), - [](SHAMapItem const* lhs, SHAMapItem const* rhs) - { return lhs->key() < rhs->key(); }); + std::sort( + v.begin(), v.end(), [](SHAMapItem const* lhs, SHAMapItem const* rhs) { + return lhs->key() < rhs->key(); + }); return v; } @@ -317,26 +327,27 @@ LedgerHistory::handleMismatch( boost::optional const& validatedConsensusHash, Json::Value const& consensus) { - assert (built != valid); + assert(built != valid); ++mismatch_counter_; - auto builtLedger = getLedgerByHash (built); - auto validLedger = getLedgerByHash (valid); + auto builtLedger = getLedgerByHash(built); + auto validLedger = getLedgerByHash(valid); if (!builtLedger || !validLedger) { - JLOG (j_.error()) << "MISMATCH cannot be analyzed:" << - " builtLedger: " << to_string (built) << " -> " << builtLedger << - " validLedger: " << to_string (valid) << " -> " << validLedger; + JLOG(j_.error()) << "MISMATCH cannot be analyzed:" + << " builtLedger: " << to_string(built) << " -> " + << builtLedger << " validLedger: " << to_string(valid) + << " -> " << validLedger; return; } - assert (builtLedger->info().seq == validLedger->info().seq); + assert(builtLedger->info().seq == validLedger->info().seq); if (auto stream = j_.debug()) { - stream << "Built: " << getJson (*builtLedger); - stream << "Valid: " << getJson (*validLedger); + stream << "Built: " << getJson(*builtLedger); + stream << "Valid: " << getJson(*validLedger); stream << "Consensus: " << consensus; } @@ -346,14 +357,14 @@ LedgerHistory::handleMismatch( // Disagreement over prior ledger indicates sync issue if (builtLedger->info().parentHash != validLedger->info().parentHash) { - JLOG (j_.error()) << "MISMATCH on prior ledger"; + JLOG(j_.error()) << "MISMATCH on prior ledger"; return; } // Disagreement over close time indicates Byzantine failure if (builtLedger->info().closeTime != validLedger->info().closeTime) { - JLOG (j_.error()) << "MISMATCH on close time"; + JLOG(j_.error()) << "MISMATCH on close time"; return; } @@ -374,25 +385,23 @@ LedgerHistory::handleMismatch( auto const validTx = leaves(validLedger->txMap()); if (builtTx == validTx) - JLOG (j_.error()) << - "MISMATCH with same " << builtTx.size() << - " transactions"; + JLOG(j_.error()) << "MISMATCH with same " << builtTx.size() + << " transactions"; else - JLOG (j_.error()) << "MISMATCH with " << - builtTx.size() << " built and " << - validTx.size() << " valid transactions."; + JLOG(j_.error()) << "MISMATCH with " << builtTx.size() << " built and " + << validTx.size() << " valid transactions."; - JLOG (j_.error()) << "built\n" << getJson(*builtLedger); - JLOG (j_.error()) << "valid\n" << getJson(*validLedger); + JLOG(j_.error()) << "built\n" << getJson(*builtLedger); + JLOG(j_.error()) << "valid\n" << getJson(*validLedger); // Log all differences between built and valid ledgers auto b = builtTx.begin(); auto v = validTx.begin(); - while(b != builtTx.end() && v != validTx.end()) + while (b != builtTx.end() && v != validTx.end()) { if ((*b)->key() < (*v)->key()) { - log_one (*builtLedger, (*b)->key(), "valid", j_); + log_one(*builtLedger, (*b)->key(), "valid", j_); ++b; } else if ((*b)->key() > (*v)->key()) @@ -406,41 +415,40 @@ LedgerHistory::handleMismatch( { // Same transaction with different metadata log_metadata_difference( - *builtLedger, - *validLedger, (*b)->key(), j_); + *builtLedger, *validLedger, (*b)->key(), j_); } ++b; ++v; } } for (; b != builtTx.end(); ++b) - log_one (*builtLedger, (*b)->key(), "valid", j_); + log_one(*builtLedger, (*b)->key(), "valid", j_); for (; v != validTx.end(); ++v) - log_one (*validLedger, (*v)->key(), "built", j_); + log_one(*validLedger, (*v)->key(), "built", j_); } -void LedgerHistory::builtLedger ( +void +LedgerHistory::builtLedger( std::shared_ptr const& ledger, uint256 const& consensusHash, Json::Value consensus) { LedgerIndex index = ledger->info().seq; LedgerHash hash = ledger->info().hash; - assert (!hash.isZero()); + assert(!hash.isZero()); - std::unique_lock sl ( - m_consensus_validated.peekMutex()); + std::unique_lock sl(m_consensus_validated.peekMutex()); auto entry = std::make_shared(); m_consensus_validated.canonicalize_replace_client(index, entry); - if (entry->validated && ! entry->built) + if (entry->validated && !entry->built) { if (entry->validated.get() != hash) { - JLOG (j_.error()) << "MISMATCH: seq=" << index - << " validated:" << entry->validated.get() - << " then:" << hash; + JLOG(j_.error()) + << "MISMATCH: seq=" << index + << " validated:" << entry->validated.get() << " then:" << hash; handleMismatch( hash, entry->validated.get(), @@ -451,35 +459,35 @@ void LedgerHistory::builtLedger ( else { // We validated a ledger and then built it locally - JLOG (j_.debug()) << "MATCH: seq=" << index << " late"; + JLOG(j_.debug()) << "MATCH: seq=" << index << " late"; } } - entry->built.emplace (hash); + entry->built.emplace(hash); entry->builtConsensusHash.emplace(consensusHash); - entry->consensus.emplace (std::move (consensus)); + entry->consensus.emplace(std::move(consensus)); } -void LedgerHistory::validatedLedger ( +void +LedgerHistory::validatedLedger( std::shared_ptr const& ledger, boost::optional const& consensusHash) { LedgerIndex index = ledger->info().seq; LedgerHash hash = ledger->info().hash; - assert (!hash.isZero()); + assert(!hash.isZero()); - std::unique_lock sl ( - m_consensus_validated.peekMutex()); + std::unique_lock sl(m_consensus_validated.peekMutex()); auto entry = std::make_shared(); m_consensus_validated.canonicalize_replace_client(index, entry); - if (entry->built && ! entry->validated) + if (entry->built && !entry->validated) { if (entry->built.get() != hash) { - JLOG (j_.error()) << "MISMATCH: seq=" << index - << " built:" << entry->built.get() + JLOG(j_.error()) + << "MISMATCH: seq=" << index << " built:" << entry->built.get() << " then:" << hash; handleMismatch( entry->built.get(), @@ -491,23 +499,23 @@ void LedgerHistory::validatedLedger ( else { // We built a ledger locally and then validated it - JLOG (j_.debug()) << "MATCH: seq=" << index; + JLOG(j_.debug()) << "MATCH: seq=" << index; } } - entry->validated.emplace (hash); + entry->validated.emplace(hash); entry->validatedConsensusHash = consensusHash; } /** Ensure m_ledgers_by_hash doesn't have the wrong hash for a particular index -*/ -bool LedgerHistory::fixIndex ( - LedgerIndex ledgerIndex, LedgerHash const& ledgerHash) + */ +bool +LedgerHistory::fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash) { - std::unique_lock sl (m_ledgers_by_hash.peekMutex ()); - auto it = mLedgersByIndex.find (ledgerIndex); + std::unique_lock sl(m_ledgers_by_hash.peekMutex()); + auto it = mLedgersByIndex.find(ledgerIndex); - if ((it != mLedgersByIndex.end ()) && (it->second != ledgerHash) ) + if ((it != mLedgersByIndex.end()) && (it->second != ledgerHash)) { it->second = ledgerHash; return false; @@ -515,20 +523,22 @@ bool LedgerHistory::fixIndex ( return true; } -void LedgerHistory::tune (int size, std::chrono::seconds age) +void +LedgerHistory::tune(int size, std::chrono::seconds age) { - m_ledgers_by_hash.setTargetSize (size); - m_ledgers_by_hash.setTargetAge (age); + m_ledgers_by_hash.setTargetSize(size); + m_ledgers_by_hash.setTargetAge(age); } -void LedgerHistory::clearLedgerCachePrior (LedgerIndex seq) +void +LedgerHistory::clearLedgerCachePrior(LedgerIndex seq) { - for (LedgerHash it: m_ledgers_by_hash.getKeys()) + for (LedgerHash it : m_ledgers_by_hash.getKeys()) { - auto const ledger = getLedgerByHash (it); + auto const ledger = getLedgerByHash(it); if (!ledger || ledger->info().seq < seq) - m_ledgers_by_hash.del (it, false); + m_ledgers_by_hash.del(it, false); } } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/LedgerHistory.h b/src/ripple/app/ledger/LedgerHistory.h index c3d0f9731e..56a50e9ea0 100644 --- a/src/ripple/app/ledger/LedgerHistory.h +++ b/src/ripple/app/ledger/LedgerHistory.h @@ -22,9 +22,9 @@ #include #include -#include #include #include +#include namespace ripple { @@ -34,59 +34,66 @@ namespace ripple { class LedgerHistory { public: - LedgerHistory (beast::insight::Collector::ptr const& collector, + LedgerHistory( + beast::insight::Collector::ptr const& collector, Application& app); /** Track a ledger @return `true` if the ledger was already tracked */ - bool insert (std::shared_ptr ledger, - bool validated); + bool + insert(std::shared_ptr ledger, bool validated); /** Get the ledgers_by_hash cache hit rate @return the hit rate */ - float getCacheHitRate () + float + getCacheHitRate() { - return m_ledgers_by_hash.getHitRate (); + return m_ledgers_by_hash.getHitRate(); } /** Get a ledger given its sequence number */ std::shared_ptr - getLedgerBySeq (LedgerIndex ledgerIndex); + getLedgerBySeq(LedgerIndex ledgerIndex); /** Retrieve a ledger given its hash */ std::shared_ptr - getLedgerByHash (LedgerHash const& ledgerHash); + getLedgerByHash(LedgerHash const& ledgerHash); /** Get a ledger's hash given its sequence number @param ledgerIndex The sequence number of the desired ledger @return The hash of the specified ledger */ - LedgerHash getLedgerHash (LedgerIndex ledgerIndex); + LedgerHash + getLedgerHash(LedgerIndex ledgerIndex); /** Set the history cache's parameters @param size The target size of the cache @param age The target age of the cache, in seconds */ - void tune (int size, std::chrono::seconds age); + void + tune(int size, std::chrono::seconds age); /** Remove stale cache entries - */ - void sweep () + */ + void + sweep() { - m_ledgers_by_hash.sweep (); - m_consensus_validated.sweep (); + m_ledgers_by_hash.sweep(); + m_consensus_validated.sweep(); } /** Report that we have locally built a particular ledger */ - void builtLedger ( + void + builtLedger( std::shared_ptr const&, uint256 const& consensusHash, Json::Value); /** Report that we have validated a particular ledger */ - void validatedLedger ( + void + validatedLedger( std::shared_ptr const&, boost::optional const& consensusHash); @@ -95,12 +102,13 @@ public: @param ledgerHash The hash it is to be mapped to @return `true` if the mapping was repaired */ - bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash); + bool + fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash); - void clearLedgerCachePrior (LedgerIndex seq); + void + clearLedgerCachePrior(LedgerIndex seq); private: - /** Log details in the case where we build one ledger but validate a different one. @param built The hash of the ledger we built @@ -123,7 +131,7 @@ private: beast::insight::Collector::ptr collector_; beast::insight::Counter mismatch_counter_; - using LedgersByHash = TaggedCache ; + using LedgersByHash = TaggedCache; LedgersByHash m_ledgers_by_hash; @@ -142,16 +150,15 @@ private: // Consensus metadata of built ledger boost::optional consensus; }; - using ConsensusValidated = TaggedCache ; + using ConsensusValidated = TaggedCache; ConsensusValidated m_consensus_validated; - // Maps ledger indexes to the corresponding hash. - std::map mLedgersByIndex; // validated ledgers + std::map mLedgersByIndex; // validated ledgers beast::Journal j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LedgerHolder.h b/src/ripple/app/ledger/LedgerHolder.h index a0831ccf47..449cff9ab0 100644 --- a/src/ripple/app/ledger/LedgerHolder.h +++ b/src/ripple/app/ledger/LedgerHolder.h @@ -39,26 +39,29 @@ class LedgerHolder { public: // Update the held ledger - void set (std::shared_ptr ledger) + void + set(std::shared_ptr ledger) { - if(! ledger) + if (!ledger) LogicError("LedgerHolder::set with nullptr"); - if(! ledger->isImmutable()) + if (!ledger->isImmutable()) LogicError("LedgerHolder::set with mutable Ledger"); - std::lock_guard sl (m_lock); + std::lock_guard sl(m_lock); m_heldLedger = std::move(ledger); } // Return the (immutable) held ledger - std::shared_ptr get () + std::shared_ptr + get() { - std::lock_guard sl (m_lock); + std::lock_guard sl(m_lock); return m_heldLedger; } - bool empty () + bool + empty() { - std::lock_guard sl (m_lock); + std::lock_guard sl(m_lock); return m_heldLedger == nullptr; } @@ -67,6 +70,6 @@ private: std::shared_ptr m_heldLedger; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LedgerMaster.h b/src/ripple/app/ledger/LedgerMaster.h index 0eb303207f..1c60476e7f 100644 --- a/src/ripple/app/ledger/LedgerMaster.h +++ b/src/ripple/app/ledger/LedgerMaster.h @@ -20,7 +20,6 @@ #ifndef RIPPLE_APP_LEDGER_LEDGERMASTER_H_INCLUDED #define RIPPLE_APP_LEDGER_LEDGERMASTER_H_INCLUDED -#include #include #include #include @@ -28,17 +27,18 @@ #include #include #include +#include #include -#include #include #include +#include #include #include #include -#include #include #include #include +#include #include #include @@ -48,8 +48,6 @@ namespace ripple { class Peer; class Transaction; - - // Tracks the current ledger and any ledgers in the process of closing // Tracks ledger history // Tracks held transactions @@ -57,28 +55,28 @@ class Transaction; // VFALCO TODO Rename to Ledgers // It sounds like this holds all the ledgers... // -class LedgerMaster - : public Stoppable - , public AbstractFetchPackContainer +class LedgerMaster : public Stoppable, public AbstractFetchPackContainer { public: - explicit - LedgerMaster(Application& app, Stopwatch& stopwatch, + explicit LedgerMaster( + Application& app, + Stopwatch& stopwatch, Stoppable& parent, - beast::insight::Collector::ptr const& collector, - beast::Journal journal); + beast::insight::Collector::ptr const& collector, + beast::Journal journal); - virtual ~LedgerMaster () = default; + virtual ~LedgerMaster() = default; - LedgerIndex getCurrentLedgerIndex (); - LedgerIndex getValidLedgerIndex (); + LedgerIndex + getCurrentLedgerIndex(); + LedgerIndex + getValidLedgerIndex(); - bool isCompatible ( - ReadView const&, - beast::Journal::Stream, - char const* reason); + bool + isCompatible(ReadView const&, beast::Journal::Stream, char const* reason); - std::recursive_mutex& peekMutex (); + std::recursive_mutex& + peekMutex(); // The current ledger is the ledger we believe new transactions should go in std::shared_ptr @@ -93,49 +91,62 @@ public: // The validated ledger is the last fully validated ledger std::shared_ptr - getValidatedLedger () + getValidatedLedger() { return mValidLedger.get(); } // The Rules are in the last fully validated ledger if there is one. - Rules getValidatedRules(); + Rules + getValidatedRules(); // This is the last ledger we published to clients and can lag the validated // ledger std::shared_ptr getPublishedLedger(); - std::chrono::seconds getPublishedLedgerAge (); - std::chrono::seconds getValidatedLedgerAge (); - bool isCaughtUp(std::string& reason); + std::chrono::seconds + getPublishedLedgerAge(); + std::chrono::seconds + getValidatedLedgerAge(); + bool + isCaughtUp(std::string& reason); - std::uint32_t getEarliestFetch (); + std::uint32_t + getEarliestFetch(); - bool storeLedger (std::shared_ptr ledger); + bool + storeLedger(std::shared_ptr ledger); - void setFullLedger ( + void + setFullLedger( std::shared_ptr const& ledger, - bool isSynchronous, bool isCurrent); + bool isSynchronous, + bool isCurrent); /** Check the sequence number and parent close time of a ledger against our clock and last validated ledger to see if it can be the network's current ledger */ - bool canBeCurrent (std::shared_ptr const& ledger); + bool + canBeCurrent(std::shared_ptr const& ledger); - void switchLCL (std::shared_ptr const& lastClosed); + void + switchLCL(std::shared_ptr const& lastClosed); - void failedSave(std::uint32_t seq, uint256 const& hash); + void + failedSave(std::uint32_t seq, uint256 const& hash); - std::string getCompleteLedgers (); + std::string + getCompleteLedgers(); /** Apply held transactions to the open ledger This is normally called as we close the ledger. The open ledger remains open to handle new transactions until a new open ledger is built. */ - void applyHeldTransactions (); + void + applyHeldTransactions(); /** Get all the transactions held for a particular account. This is normally called when a transaction for that @@ -144,15 +155,16 @@ public: waiting for ledger close. */ std::vector> - pruneHeldTransactions(AccountID const& account, - std::uint32_t const seq); + pruneHeldTransactions(AccountID const& account, std::uint32_t const seq); /** Get a ledger's hash by sequence number using the cache - */ - uint256 getHashBySeq (std::uint32_t index); + */ + uint256 + getHashBySeq(std::uint32_t index); /** Walk to a ledger's hash using the skip list */ - boost::optional walkHashBySeq (std::uint32_t index); + boost::optional + walkHashBySeq(std::uint32_t index); /** Walk the chain of ledger hashes to determine the hash of the ledger with the specified index. The referenceLedger is used as @@ -161,92 +173,115 @@ public: from the reference ledger or any prior ledger are not present in the node store. */ - boost::optional walkHashBySeq ( + boost::optional + walkHashBySeq( std::uint32_t index, std::shared_ptr const& referenceLedger); std::shared_ptr - getLedgerBySeq (std::uint32_t index); + getLedgerBySeq(std::uint32_t index); std::shared_ptr - getLedgerByHash (uint256 const& hash); + getLedgerByHash(uint256 const& hash); - void setLedgerRangePresent ( - std::uint32_t minV, std::uint32_t maxV); + void + setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV); - boost::optional getLedgerHash( + boost::optional + getLedgerHash( std::uint32_t desiredSeq, std::shared_ptr const& knownGoodLedger); - boost::optional getCloseTimeBySeq ( - LedgerIndex ledgerIndex); + boost::optional + getCloseTimeBySeq(LedgerIndex ledgerIndex); - boost::optional getCloseTimeByHash ( - LedgerHash const& ledgerHash, LedgerIndex ledgerIndex); + boost::optional + getCloseTimeByHash(LedgerHash const& ledgerHash, LedgerIndex ledgerIndex); - void addHeldTransaction (std::shared_ptr const& trans); - void fixMismatch (ReadView const& ledger); + void + addHeldTransaction(std::shared_ptr const& trans); + void + fixMismatch(ReadView const& ledger); - bool haveLedger (std::uint32_t seq); - void clearLedger (std::uint32_t seq); - bool getValidatedRange ( - std::uint32_t& minVal, std::uint32_t& maxVal); - bool getFullValidatedRange ( - std::uint32_t& minVal, std::uint32_t& maxVal); + bool + haveLedger(std::uint32_t seq); + void + clearLedger(std::uint32_t seq); + bool + getValidatedRange(std::uint32_t& minVal, std::uint32_t& maxVal); + bool + getFullValidatedRange(std::uint32_t& minVal, std::uint32_t& maxVal); - void tune (int size, std::chrono::seconds age); - void sweep (); - float getCacheHitRate (); + void + tune(int size, std::chrono::seconds age); + void + sweep(); + float + getCacheHitRate(); - void checkAccept (std::shared_ptr const& ledger); - void checkAccept (uint256 const& hash, std::uint32_t seq); + void + checkAccept(std::shared_ptr const& ledger); + void + checkAccept(uint256 const& hash, std::uint32_t seq); void consensusBuilt( std::shared_ptr const& ledger, uint256 const& consensusHash, Json::Value consensus); - LedgerIndex getBuildingLedger (); - void setBuildingLedger (LedgerIndex index); + LedgerIndex + getBuildingLedger(); + void + setBuildingLedger(LedgerIndex index); - void tryAdvance (); - bool newPathRequest (); // Returns true if path request successfully placed. - bool isNewPathRequest (); - bool newOrderBookDB (); // Returns true if able to fulfill request. + void + tryAdvance(); + bool + newPathRequest(); // Returns true if path request successfully placed. + bool + isNewPathRequest(); + bool + newOrderBookDB(); // Returns true if able to fulfill request. - bool fixIndex ( - LedgerIndex ledgerIndex, LedgerHash const& ledgerHash); - void doLedgerCleaner(Json::Value const& parameters); + bool + fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash); + void + doLedgerCleaner(Json::Value const& parameters); - beast::PropertyStream::Source& getPropertySource (); + beast::PropertyStream::Source& + getPropertySource(); - void clearPriorLedgers (LedgerIndex seq); + void + clearPriorLedgers(LedgerIndex seq); - void clearLedgerCachePrior (LedgerIndex seq); + void + clearLedgerCachePrior(LedgerIndex seq); // ledger replay - void takeReplay (std::unique_ptr replay); - std::unique_ptr releaseReplay (); + void + takeReplay(std::unique_ptr replay); + std::unique_ptr + releaseReplay(); // Fetch Packs - void gotFetchPack ( - bool progress, - std::uint32_t seq); + void + gotFetchPack(bool progress, std::uint32_t seq); - void addFetchPack ( - uint256 const& hash, - std::shared_ptr& data); + void + addFetchPack(uint256 const& hash, std::shared_ptr& data); boost::optional - getFetchPack (uint256 const& hash) override; + getFetchPack(uint256 const& hash) override; - void makeFetchPack ( + void + makeFetchPack( std::weak_ptr const& wPeer, std::shared_ptr const& request, uint256 haveLedgerHash, UptimeClock::time_point uptime); - std::size_t getFetchPackCacheSize () const; + std::size_t + getFetchPackCacheSize() const; //! Whether we have ever fully validated a ledger. bool @@ -256,40 +291,45 @@ public: } private: - void setValidLedger( - std::shared_ptr const& l); - void setPubLedger( - std::shared_ptr const& l); + void + setValidLedger(std::shared_ptr const& l); + void + setPubLedger(std::shared_ptr const& l); - void tryFill( - Job& job, - std::shared_ptr ledger); + void + tryFill(Job& job, std::shared_ptr ledger); - void getFetchPack( - LedgerIndex missing, InboundLedger::Reason reason); + void + getFetchPack(LedgerIndex missing, InboundLedger::Reason reason); - boost::optional getLedgerHashForHistory( - LedgerIndex index, InboundLedger::Reason reason); + boost::optional + getLedgerHashForHistory(LedgerIndex index, InboundLedger::Reason reason); - std::size_t getNeededValidations(); - void advanceThread(); - void fetchForHistory( + std::size_t + getNeededValidations(); + void + advanceThread(); + void + fetchForHistory( std::uint32_t missing, bool& progress, InboundLedger::Reason reason, std::unique_lock&); // Try to publish ledgers, acquire missing ledgers. Always called with // m_mutex locked. The passed lock is a reminder to callers. - void doAdvance(std::unique_lock&); + void + doAdvance(std::unique_lock&); std::vector> findNewLedgersToPublish(std::unique_lock&); - void updatePaths(Job& job); + void + updatePaths(Job& job); // Returns true if work started. Always called with m_mutex locked. // The passed lock is a reminder to callers. - bool newPFWork(const char *name, std::unique_lock&); + bool + newPFWork(const char* name, std::unique_lock&); Application& app_; beast::Journal m_journal; @@ -315,11 +355,11 @@ private: std::shared_ptr mShardLedger; // Fully validated ledger, whether or not we have the ledger resident. - std::pair mLastValidLedger {uint256(), 0}; + std::pair mLastValidLedger{uint256(), 0}; LedgerHistory mLedgerHistory; - CanonicalTXSet mHeldTransactions {uint256()}; + CanonicalTXSet mHeldTransactions{uint256()}; // A set of transactions to replay during the next close std::unique_ptr replayData; @@ -327,25 +367,26 @@ private: std::recursive_mutex mCompleteLock; RangeSet mCompleteLedgers; - std::unique_ptr mLedgerCleaner; + std::unique_ptr mLedgerCleaner; // Publish thread is running. - bool mAdvanceThread {false}; + bool mAdvanceThread{false}; // Publish thread has work to do. - bool mAdvanceWork {false}; - int mFillInProgress {0}; + bool mAdvanceWork{false}; + int mFillInProgress{0}; - int mPathFindThread {0}; // Pathfinder jobs dispatched - bool mPathFindNewRequest {false}; + int mPathFindThread{0}; // Pathfinder jobs dispatched + bool mPathFindNewRequest{false}; - std::atomic_flag mGotFetchPackThread = ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched + std::atomic_flag mGotFetchPackThread = + ATOMIC_FLAG_INIT; // GotFetchPack jobs dispatched - std::atomic mPubLedgerClose {0}; - std::atomic mPubLedgerSeq {0}; - std::atomic mValidLedgerSign {0}; - std::atomic mValidLedgerSeq {0}; - std::atomic mBuildingLedgerSeq {0}; + std::atomic mPubLedgerClose{0}; + std::atomic mPubLedgerSeq{0}; + std::atomic mValidLedgerSign{0}; + std::atomic mValidLedgerSeq{0}; + std::atomic mBuildingLedgerSeq{0}; // The server is in standalone mode bool const standalone_; @@ -360,21 +401,26 @@ private: TaggedCache fetch_packs_; - std::uint32_t fetch_seq_ {0}; + std::uint32_t fetch_seq_{0}; // Try to keep a validator from switching from test to live network // without first wiping the database. - LedgerIndex const max_ledger_difference_ {1000000}; + LedgerIndex const max_ledger_difference_{1000000}; private: struct Stats { template - Stats (Handler const& handler, beast::insight::Collector::ptr const& collector) - : hook (collector->make_hook (handler)) - , validatedLedgerAge (collector->make_gauge ("LedgerMaster", "Validated_Ledger_Age")) - , publishedLedgerAge (collector->make_gauge ("LedgerMaster", "Published_Ledger_Age")) - { } + Stats( + Handler const& handler, + beast::insight::Collector::ptr const& collector) + : hook(collector->make_hook(handler)) + , validatedLedgerAge( + collector->make_gauge("LedgerMaster", "Validated_Ledger_Age")) + , publishedLedgerAge( + collector->make_gauge("LedgerMaster", "Published_Ledger_Age")) + { + } beast::insight::Hook hook; beast::insight::Gauge validatedLedgerAge; @@ -384,19 +430,15 @@ private: Stats m_stats; private: - void collect_metrics() + void + collect_metrics() { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_stats.validatedLedgerAge.set(getValidatedLedgerAge().count()); m_stats.publishedLedgerAge.set(getPublishedLedgerAge().count()); } - - - - - }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LedgerReplay.h b/src/ripple/app/ledger/LedgerReplay.h index 4491aca9ef..5d5fa6dc96 100644 --- a/src/ripple/app/ledger/LedgerReplay.h +++ b/src/ripple/app/ledger/LedgerReplay.h @@ -41,7 +41,7 @@ public: std::shared_ptr replay); /** @return The parent of the ledger to replay - */ + */ std::shared_ptr const& parent() const { @@ -49,7 +49,7 @@ public: } /** @return The ledger to replay - */ + */ std::shared_ptr const& replay() const { @@ -57,7 +57,7 @@ public: } /** @return Transactions in the order they should be replayed - */ + */ std::map> const& orderedTxns() const { diff --git a/src/ripple/app/ledger/LedgerToJson.h b/src/ripple/app/ledger/LedgerToJson.h index 77b0c863a1..909e185fd0 100644 --- a/src/ripple/app/ledger/LedgerToJson.h +++ b/src/ripple/app/ledger/LedgerToJson.h @@ -23,20 +23,20 @@ #include #include #include -#include -#include #include +#include +#include namespace ripple { struct LedgerFill { - LedgerFill (ReadView const& l, int o = 0, std::vector q = {}, - LedgerEntryType t = ltINVALID) - : ledger (l) - , options (o) - , txQueue(std::move(q)) - , type (t) + LedgerFill( + ReadView const& l, + int o = 0, + std::vector q = {}, + LedgerEntryType t = ltINVALID) + : ledger(l), options(o), txQueue(std::move(q)), type(t) { } @@ -60,14 +60,17 @@ struct LedgerFill description of the ledger. */ -void addJson(Json::Value&, LedgerFill const&); +void +addJson(Json::Value&, LedgerFill const&); /** Return a new Json::Value representing the ledger with given options.*/ -Json::Value getJson (LedgerFill const&); +Json::Value +getJson(LedgerFill const&); /** Serialize an object to a blob. */ template -Blob serializeBlob(Object const& o) +Blob +serializeBlob(Object const& o) { Serializer s; o.add(s); @@ -75,11 +78,11 @@ Blob serializeBlob(Object const& o) } /** Serialize an object to a hex string. */ -inline -std::string serializeHex(STObject const& o) +inline std::string +serializeHex(STObject const& o) { return strHex(serializeBlob(o)); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/LocalTxs.h b/src/ripple/app/ledger/LocalTxs.h index be8bffd1ee..f427a5e047 100644 --- a/src/ripple/app/ledger/LocalTxs.h +++ b/src/ripple/app/ledger/LocalTxs.h @@ -33,23 +33,27 @@ namespace ripple { class LocalTxs { public: - virtual ~LocalTxs () = default; + virtual ~LocalTxs() = default; // Add a new local transaction - virtual void push_back (LedgerIndex index, std::shared_ptr const& txn) = 0; + virtual void + push_back(LedgerIndex index, std::shared_ptr const& txn) = 0; // Return the set of local transactions to a new open ledger - virtual CanonicalTXSet getTxSet () = 0; + virtual CanonicalTXSet + getTxSet() = 0; // Remove obsolete transactions based on a new fully-valid ledger - virtual void sweep (ReadView const& view) = 0; + virtual void + sweep(ReadView const& view) = 0; - virtual std::size_t size () = 0; + virtual std::size_t + size() = 0; }; std::unique_ptr -make_LocalTxs (); +make_LocalTxs(); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/OpenLedger.h b/src/ripple/app/ledger/OpenLedger.h index 4b28294e00..e9aba34fba 100644 --- a/src/ripple/app/ledger/OpenLedger.h +++ b/src/ripple/app/ledger/OpenLedger.h @@ -21,13 +21,13 @@ #define RIPPLE_APP_LEDGER_OPENLEDGER_H_INCLUDED #include -#include -#include #include #include #include -#include #include +#include +#include +#include #include #include @@ -67,22 +67,21 @@ public: `true` won't cause harm, but it may be sub-optimal. */ - using modify_type = std::function< - bool(OpenView&, beast::Journal)>; + using modify_type = std::function; OpenLedger() = delete; - OpenLedger (OpenLedger const&) = delete; - OpenLedger& operator= (OpenLedger const&) = delete; + OpenLedger(OpenLedger const&) = delete; + OpenLedger& + operator=(OpenLedger const&) = delete; /** Create a new open ledger object. @param ledger A closed ledger */ - explicit - OpenLedger(std::shared_ptr< - Ledger const> const& ledger, - CachedSLEs& cache, - beast::Journal journal); + explicit OpenLedger( + std::shared_ptr const& ledger, + CachedSLEs& cache, + beast::Journal journal); /** Returns `true` if there are no transactions. @@ -124,7 +123,7 @@ public: @return `true` if the open view was changed */ bool - modify (modify_type const& f); + modify(modify_type const& f); /** Accept a new ledger. @@ -160,12 +159,16 @@ public: @param ledger A new closed ledger */ void - accept (Application& app, Rules const& rules, + accept( + Application& app, + Rules const& rules, std::shared_ptr const& ledger, - OrderedTxs const& locals, bool retriesFirst, - OrderedTxs& retries, ApplyFlags flags, - std::string const& suffix = "", - modify_type const& f = {}); + OrderedTxs const& locals, + bool retriesFirst, + OrderedTxs& retries, + ApplyFlags flags, + std::string const& suffix = "", + modify_type const& f = {}); private: /** Algorithm for applying transactions. @@ -174,45 +177,48 @@ private: used for consensus and building the open ledger. */ template - static - void - apply (Application& app, OpenView& view, - ReadView const& check, FwdRange const& txs, - OrderedTxs& retries, ApplyFlags flags, - std::map& shouldRecover, - beast::Journal j); + static void + apply( + Application& app, + OpenView& view, + ReadView const& check, + FwdRange const& txs, + OrderedTxs& retries, + ApplyFlags flags, + std::map& shouldRecover, + beast::Journal j); - enum Result - { - success, - failure, - retry - }; + enum Result { success, failure, retry }; std::shared_ptr - create (Rules const& rules, - std::shared_ptr const& ledger); + create(Rules const& rules, std::shared_ptr const& ledger); - static - Result - apply_one (Application& app, OpenView& view, - std::shared_ptr< STTx const> const& tx, - bool retry, ApplyFlags flags, - bool shouldRecover, beast::Journal j); + static Result + apply_one( + Application& app, + OpenView& view, + std::shared_ptr const& tx, + bool retry, + ApplyFlags flags, + bool shouldRecover, + beast::Journal j); }; //------------------------------------------------------------------------------ template void -OpenLedger::apply (Application& app, OpenView& view, - ReadView const& check, FwdRange const& txs, - OrderedTxs& retries, ApplyFlags flags, - std::map& shouldRecover, - beast::Journal j) +OpenLedger::apply( + Application& app, + OpenView& view, + ReadView const& check, + FwdRange const& txs, + OrderedTxs& retries, + ApplyFlags flags, + std::map& shouldRecover, + beast::Journal j) { - for (auto iter = txs.begin(); - iter != txs.end(); ++iter) + for (auto iter = txs.begin(); iter != txs.end(); ++iter) { try { @@ -222,51 +228,53 @@ OpenLedger::apply (Application& app, OpenView& view, auto const txId = tx->getTransactionID(); if (check.txExists(txId)) continue; - auto const result = apply_one(app, view, - tx, true, flags, shouldRecover[txId], j); + auto const result = + apply_one(app, view, tx, true, flags, shouldRecover[txId], j); if (result == Result::retry) retries.insert(tx); } - catch(std::exception const&) + catch (std::exception const&) { - JLOG(j.error()) << - "Caught exception"; + JLOG(j.error()) << "Caught exception"; } } bool retry = true; - for (int pass = 0; - pass < LEDGER_TOTAL_PASSES; - ++pass) + for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass) { int changes = 0; auto iter = retries.begin(); while (iter != retries.end()) { - switch (apply_one(app, view, - iter->second, retry, flags, - shouldRecover[iter->second->getTransactionID()], j)) + switch (apply_one( + app, + view, + iter->second, + retry, + flags, + shouldRecover[iter->second->getTransactionID()], + j)) { - case Result::success: - ++changes; - [[fallthrough]]; - case Result::failure: - iter = retries.erase (iter); - break; - case Result::retry: - ++iter; + case Result::success: + ++changes; + [[fallthrough]]; + case Result::failure: + iter = retries.erase(iter); + break; + case Result::retry: + ++iter; } } // A non-retry pass made no changes - if (! changes && ! retry) + if (!changes && !retry) return; // Stop retriable passes - if (! changes || (pass >= LEDGER_RETRY_PASSES)) + if (!changes || (pass >= LEDGER_RETRY_PASSES)) retry = false; } // If there are any transactions left, we must have // tried them in at least one final pass - assert (retries.empty() || ! retry); + assert(retries.empty() || !retry); } //------------------------------------------------------------------------------ @@ -274,17 +282,17 @@ OpenLedger::apply (Application& app, OpenView& view, // For debug logging std::string -debugTxstr (std::shared_ptr const& tx); +debugTxstr(std::shared_ptr const& tx); std::string -debugTostr (OrderedTxs const& set); +debugTostr(OrderedTxs const& set); std::string -debugTostr (SHAMap const& set); +debugTostr(SHAMap const& set); std::string -debugTostr (std::shared_ptr const& view); +debugTostr(std::shared_ptr const& view); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/OrderBookDB.cpp b/src/ripple/app/ledger/OrderBookDB.cpp index c050bb317c..98cb306d5b 100644 --- a/src/ripple/app/ledger/OrderBookDB.cpp +++ b/src/ripple/app/ledger/OrderBookDB.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -27,25 +27,26 @@ namespace ripple { -OrderBookDB::OrderBookDB (Application& app, Stoppable& parent) - : Stoppable ("OrderBookDB", parent) - , app_ (app) - , mSeq (0) - , j_ (app.journal ("OrderBookDB")) +OrderBookDB::OrderBookDB(Application& app, Stoppable& parent) + : Stoppable("OrderBookDB", parent) + , app_(app) + , mSeq(0) + , j_(app.journal("OrderBookDB")) { } -void OrderBookDB::invalidate () +void +OrderBookDB::invalidate() { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); mSeq = 0; } -void OrderBookDB::setup( - std::shared_ptr const& ledger) +void +OrderBookDB::setup(std::shared_ptr const& ledger) { { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); auto seq = ledger->info().seq; // Do a full update every 256 ledgers @@ -59,8 +60,7 @@ void OrderBookDB::setup( return; } - JLOG (j_.debug()) - << "Advancing from " << mSeq << " to " << seq; + JLOG(j_.debug()) << "Advancing from " << mSeq << " to " << seq; mSeq = seq; } @@ -73,19 +73,20 @@ void OrderBookDB::setup( update(ledger); else app_.getJobQueue().addJob( - jtUPDATE_PF, "OrderBookDB::update", - [this, ledger] (Job&) { update(ledger); }); + jtUPDATE_PF, "OrderBookDB::update", [this, ledger](Job&) { + update(ledger); + }); } -void OrderBookDB::update( - std::shared_ptr const& ledger) +void +OrderBookDB::update(std::shared_ptr const& ledger) { - hash_set< uint256 > seen; + hash_set seen; OrderBookDB::IssueToOrderBook destMap; OrderBookDB::IssueToOrderBook sourceMap; - hash_set< Issue > XRPBooks; + hash_set XRPBooks; - JLOG (j_.debug()) << "OrderBookDB::update>"; + JLOG(j_.debug()) << "OrderBookDB::update>"; if (app_.config().PATH_SEARCH_MAX == 0) { @@ -98,20 +99,20 @@ void OrderBookDB::update( try { - for(auto& sle : ledger->sles) + for (auto& sle : ledger->sles) { if (isStopping()) { - JLOG (j_.info()) + JLOG(j_.info()) << "OrderBookDB::update exiting due to isStopping"; - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); mSeq = 0; return; } - if (sle->getType () == ltDIR_NODE && - sle->isFieldPresent (sfExchangeRate) && - sle->getFieldH256 (sfRootIndex) == sle->key()) + if (sle->getType() == ltDIR_NODE && + sle->isFieldPresent(sfExchangeRate) && + sle->getFieldH256(sfRootIndex) == sle->key()) { Book book; book.in.currency = sle->getFieldH160(sfTakerPaysCurrency); @@ -119,12 +120,12 @@ void OrderBookDB::update( book.out.account = sle->getFieldH160(sfTakerGetsIssuer); book.out.currency = sle->getFieldH160(sfTakerGetsCurrency); - uint256 index = getBookBase (book); - if (seen.insert (index).second) + uint256 index = getBookBase(book); + if (seen.insert(index).second) { - auto orderBook = std::make_shared (index, book); - sourceMap[book.in].push_back (orderBook); - destMap[book.out].push_back (orderBook); + auto orderBook = std::make_shared(index, book); + sourceMap[book.in].push_back(orderBook); + destMap[book.out].push_back(orderBook); if (isXRP(book.out)) XRPBooks.insert(book.in); ++books; @@ -134,17 +135,15 @@ void OrderBookDB::update( } catch (SHAMapMissingNode const& mn) { - JLOG (j_.info()) - << "OrderBookDB::update: " << mn.what(); - std::lock_guard sl (mLock); + JLOG(j_.info()) << "OrderBookDB::update: " << mn.what(); + std::lock_guard sl(mLock); mSeq = 0; return; } - JLOG (j_.debug()) - << "OrderBookDB::update< " << books << " books found"; + JLOG(j_.debug()) << "OrderBookDB::update< " << books << " books found"; { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); mXRPBooks.swap(XRPBooks); mSourceMap.swap(sourceMap); @@ -153,24 +152,25 @@ void OrderBookDB::update( app_.getLedgerMaster().newOrderBookDB(); } -void OrderBookDB::addOrderBook(Book const& book) +void +OrderBookDB::addOrderBook(Book const& book) { - bool toXRP = isXRP (book.out); - std::lock_guard sl (mLock); + bool toXRP = isXRP(book.out); + std::lock_guard sl(mLock); if (toXRP) { // We don't want to search through all the to-XRP or from-XRP order // books! - for (auto ob: mSourceMap[book.in]) + for (auto ob : mSourceMap[book.in]) { - if (isXRP (ob->getCurrencyOut ())) // also to XRP + if (isXRP(ob->getCurrencyOut())) // also to XRP return; } } else { - for (auto ob: mDestMap[book.out]) + for (auto ob : mDestMap[book.out]) { if (ob->getCurrencyIn() == book.in.currency && ob->getIssuerIn() == book.in.account) @@ -180,57 +180,63 @@ void OrderBookDB::addOrderBook(Book const& book) } } uint256 index = getBookBase(book); - auto orderBook = std::make_shared (index, book); + auto orderBook = std::make_shared(index, book); - mSourceMap[book.in].push_back (orderBook); - mDestMap[book.out].push_back (orderBook); + mSourceMap[book.in].push_back(orderBook); + mDestMap[book.out].push_back(orderBook); if (toXRP) mXRPBooks.insert(book.in); } // return list of all orderbooks that want this issuerID and currencyID -OrderBook::List OrderBookDB::getBooksByTakerPays (Issue const& issue) +OrderBook::List +OrderBookDB::getBooksByTakerPays(Issue const& issue) { - std::lock_guard sl (mLock); - auto it = mSourceMap.find (issue); - return it == mSourceMap.end () ? OrderBook::List() : it->second; + std::lock_guard sl(mLock); + auto it = mSourceMap.find(issue); + return it == mSourceMap.end() ? OrderBook::List() : it->second; } -int OrderBookDB::getBookSize(Issue const& issue) { - std::lock_guard sl (mLock); - auto it = mSourceMap.find (issue); - return it == mSourceMap.end () ? 0 : it->second.size(); +int +OrderBookDB::getBookSize(Issue const& issue) +{ + std::lock_guard sl(mLock); + auto it = mSourceMap.find(issue); + return it == mSourceMap.end() ? 0 : it->second.size(); } -bool OrderBookDB::isBookToXRP(Issue const& issue) +bool +OrderBookDB::isBookToXRP(Issue const& issue) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); return mXRPBooks.count(issue) > 0; } -BookListeners::pointer OrderBookDB::makeBookListeners (Book const& book) +BookListeners::pointer +OrderBookDB::makeBookListeners(Book const& book) { - std::lock_guard sl (mLock); - auto ret = getBookListeners (book); + std::lock_guard sl(mLock); + auto ret = getBookListeners(book); if (!ret) { - ret = std::make_shared (); + ret = std::make_shared(); - mListeners [book] = ret; - assert (getBookListeners (book) == ret); + mListeners[book] = ret; + assert(getBookListeners(book) == ret); } return ret; } -BookListeners::pointer OrderBookDB::getBookListeners (Book const& book) +BookListeners::pointer +OrderBookDB::getBookListeners(Book const& book) { BookListeners::pointer ret; - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - auto it0 = mListeners.find (book); - if (it0 != mListeners.end ()) + auto it0 = mListeners.find(book); + if (it0 != mListeners.end()) ret = it0->second; return ret; @@ -238,12 +244,14 @@ BookListeners::pointer OrderBookDB::getBookListeners (Book const& book) // Based on the meta, send the meta to the streams that are listening. // We need to determine which streams a given meta effects. -void OrderBookDB::processTxn ( +void +OrderBookDB::processTxn( std::shared_ptr const& ledger, - const AcceptedLedgerTx& alTx, Json::Value const& jvObj) + const AcceptedLedgerTx& alTx, + Json::Value const& jvObj) { - std::lock_guard sl (mLock); - if (alTx.getResult () == tesSUCCESS) + std::lock_guard sl(mLock); + if (alTx.getResult() == tesSUCCESS) { // For this particular transaction, maintain the set of unique // subscriptions that have already published it. This prevents sending @@ -255,34 +263,34 @@ void OrderBookDB::processTxn ( // Check if this is an offer or an offer cancel or a payment that // consumes an offer. // Check to see what the meta looks like. - for (auto& node : alTx.getMeta ()->getNodes ()) + for (auto& node : alTx.getMeta()->getNodes()) { try { - if (node.getFieldU16 (sfLedgerEntryType) == ltOFFER) + if (node.getFieldU16(sfLedgerEntryType) == ltOFFER) { SField const* field = nullptr; // We need a field that contains the TakerGets and TakerPays // parameters. - if (node.getFName () == sfModifiedNode) + if (node.getFName() == sfModifiedNode) field = &sfPreviousFields; - else if (node.getFName () == sfCreatedNode) + else if (node.getFName() == sfCreatedNode) field = &sfNewFields; - else if (node.getFName () == sfDeletedNode) + else if (node.getFName() == sfDeletedNode) field = &sfFinalFields; if (field) { - auto data = dynamic_cast ( - node.peekAtPField (*field)); + auto data = dynamic_cast( + node.peekAtPField(*field)); - if (data && - data->isFieldPresent (sfTakerPays) && - data->isFieldPresent (sfTakerGets)) + if (data && data->isFieldPresent(sfTakerPays) && + data->isFieldPresent(sfTakerGets)) { // determine the OrderBook - Book b{data->getFieldAmount(sfTakerGets).issue(), + Book b{ + data->getFieldAmount(sfTakerGets).issue(), data->getFieldAmount(sfTakerPays).issue()}; auto listeners = getBookListeners(b); @@ -296,11 +304,11 @@ void OrderBookDB::processTxn ( } catch (std::exception const&) { - JLOG (j_.info()) + JLOG(j_.info()) << "Fields not found in OrderBookDB::processTxn"; } } } } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/OrderBookDB.h b/src/ripple/app/ledger/OrderBookDB.h index 8ce0ed7dac..4a79bc1293 100644 --- a/src/ripple/app/ledger/OrderBookDB.h +++ b/src/ripple/app/ledger/OrderBookDB.h @@ -28,40 +28,51 @@ namespace ripple { -class OrderBookDB - : public Stoppable +class OrderBookDB : public Stoppable { public: - OrderBookDB (Application& app, Stoppable& parent); + OrderBookDB(Application& app, Stoppable& parent); - void setup (std::shared_ptr const& ledger); - void update (std::shared_ptr const& ledger); - void invalidate (); + void + setup(std::shared_ptr const& ledger); + void + update(std::shared_ptr const& ledger); + void + invalidate(); - void addOrderBook(Book const&); + void + addOrderBook(Book const&); /** @return a list of all orderbooks that want this issuerID and currencyID. */ - OrderBook::List getBooksByTakerPays (Issue const&); + OrderBook::List + getBooksByTakerPays(Issue const&); /** @return a count of all orderbooks that want this issuerID and currencyID. */ - int getBookSize(Issue const&); + int + getBookSize(Issue const&); - bool isBookToXRP (Issue const&); + bool + isBookToXRP(Issue const&); - BookListeners::pointer getBookListeners (Book const&); - BookListeners::pointer makeBookListeners (Book const&); + BookListeners::pointer + getBookListeners(Book const&); + BookListeners::pointer + makeBookListeners(Book const&); // see if this txn effects any orderbook - void processTxn ( + void + processTxn( std::shared_ptr const& ledger, - const AcceptedLedgerTx& alTx, Json::Value const& jvObj); + const AcceptedLedgerTx& alTx, + Json::Value const& jvObj); - using IssueToOrderBook = hash_map ; + using IssueToOrderBook = hash_map; private: - void rawAddBook(Book const&); + void + rawAddBook(Book const&); Application& app_; @@ -72,11 +83,11 @@ private: IssueToOrderBook mDestMap; // does an order book to XRP exist - hash_set mXRPBooks; + hash_set mXRPBooks; std::recursive_mutex mLock; - using BookToListenersMap = hash_map ; + using BookToListenersMap = hash_map; BookToListenersMap mListeners; @@ -85,6 +96,6 @@ private: beast::Journal const j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/PendingSaves.h b/src/ripple/app/ledger/PendingSaves.h index 17145f5935..f031baf4a9 100644 --- a/src/ripple/app/ledger/PendingSaves.h +++ b/src/ripple/app/ledger/PendingSaves.h @@ -21,9 +21,9 @@ #define RIPPLE_APP_PENDINGSAVES_H_INCLUDED #include +#include #include #include -#include namespace ripple { @@ -37,11 +37,10 @@ class PendingSaves { private: std::mutex mutable mutex_; - std::map map_; + std::map map_; std::condition_variable await_; public: - /** Start working on a ledger This is called prior to updating the SQLite indexes. @@ -49,11 +48,11 @@ public: @return 'true' if work should be done */ bool - startWork (LedgerIndex seq) + startWork(LedgerIndex seq) { std::lock_guard lock(mutex_); - auto it = map_.find (seq); + auto it = map_.find(seq); if ((it == map_.end()) || it->second) { @@ -72,17 +71,17 @@ public: threads awaiting completion are notified. */ void - finishWork (LedgerIndex seq) + finishWork(LedgerIndex seq) { std::lock_guard lock(mutex_); - map_.erase (seq); + map_.erase(seq); await_.notify_all(); } /** Return `true` if a ledger is in the progress of being saved. */ bool - pending (LedgerIndex seq) + pending(LedgerIndex seq) { std::lock_guard lock(mutex_); return map_.find(seq) != map_.end(); @@ -97,12 +96,12 @@ public: @return 'true' if work should be done or dispatched */ bool - shouldWork (LedgerIndex seq, bool isSynchronous) + shouldWork(LedgerIndex seq, bool isSynchronous) { - std::unique_lock lock(mutex_); + std::unique_lock lock(mutex_); do { - auto it = map_.find (seq); + auto it = map_.find(seq); if (it == map_.end()) { @@ -110,20 +109,20 @@ public: return true; } - if (! isSynchronous) + if (!isSynchronous) { // Already dispatched return false; } - if (! it->second) + if (!it->second) { // Scheduled, but not dispatched return true; } // Already in progress, just need to wait - await_.wait (lock); + await_.wait(lock); } while (true); } @@ -134,16 +133,15 @@ public: that is in progress or dispatched. The boolean indicates whether work is currently in progress. */ - std::map - getSnapshot () const + std::map + getSnapshot() const { std::lock_guard lock(mutex_); return map_; } - }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/TransactionMaster.h b/src/ripple/app/ledger/TransactionMaster.h index 5357aac6c4..9d3e064668 100644 --- a/src/ripple/app/ledger/TransactionMaster.h +++ b/src/ripple/app/ledger/TransactionMaster.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_APP_LEDGER_TRANSACTIONMASTER_H_INCLUDED #define RIPPLE_APP_LEDGER_TRANSACTIONMASTER_H_INCLUDED +#include +#include #include #include #include -#include -#include namespace ripple { @@ -36,15 +36,16 @@ class STTx; class TransactionMaster { public: - TransactionMaster (Application& app); - TransactionMaster (TransactionMaster const&) = delete; - TransactionMaster& operator= (TransactionMaster const&) = delete; + TransactionMaster(Application& app); + TransactionMaster(TransactionMaster const&) = delete; + TransactionMaster& + operator=(TransactionMaster const&) = delete; std::shared_ptr - fetch_from_cache (uint256 const&); + fetch_from_cache(uint256 const&); std::shared_ptr - fetch (uint256 const& , error_code_i& ec); + fetch(uint256 const&, error_code_i& ec); /** * Fetch transaction from the cache or database. @@ -56,27 +57,35 @@ public: * while the search was conducted. */ boost::variant - fetch (uint256 const& , ClosedInterval const& range, error_code_i& ec); + fetch( + uint256 const&, + ClosedInterval const& range, + error_code_i& ec); std::shared_ptr - fetch (std::shared_ptr const& item, - SHAMapTreeNode::TNType type, std::uint32_t uCommitLedger); + fetch( + std::shared_ptr const& item, + SHAMapTreeNode::TNType type, + std::uint32_t uCommitLedger); // return value: true = we had the transaction already - bool inLedger (uint256 const& hash, std::uint32_t ledger); + bool + inLedger(uint256 const& hash, std::uint32_t ledger); - void canonicalize (std::shared_ptr* pTransaction); + void + canonicalize(std::shared_ptr* pTransaction); - void sweep (void); + void + sweep(void); - TaggedCache & + TaggedCache& getCache(); private: Application& mApp; - TaggedCache mCache; + TaggedCache mCache; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/TransactionStateSF.cpp b/src/ripple/app/ledger/TransactionStateSF.cpp index 527f8d1bb9..5ac9b44fe9 100644 --- a/src/ripple/app/ledger/TransactionStateSF.cpp +++ b/src/ripple/app/ledger/TransactionStateSF.cpp @@ -22,13 +22,20 @@ namespace ripple { void -TransactionStateSF::gotNode(bool, SHAMapHash const& nodeHash, - std::uint32_t ledgerSeq, Blob&& nodeData, SHAMapTreeNode::TNType type) const +TransactionStateSF::gotNode( + bool, + SHAMapHash const& nodeHash, + std::uint32_t ledgerSeq, + Blob&& nodeData, + SHAMapTreeNode::TNType type) const { assert(type != SHAMapTreeNode::tnTRANSACTION_NM); - db_.store(hotTRANSACTION_NODE, std::move(nodeData), - nodeHash.as_uint256(), ledgerSeq); + db_.store( + hotTRANSACTION_NODE, + std::move(nodeData), + nodeHash.as_uint256(), + ledgerSeq); } boost::optional @@ -37,4 +44,4 @@ TransactionStateSF::getNode(SHAMapHash const& nodeHash) const return fp_.getFetchPack(nodeHash.as_uint256()); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/TransactionStateSF.h b/src/ripple/app/ledger/TransactionStateSF.h index 928977b8f6..bfe33dbe51 100644 --- a/src/ripple/app/ledger/TransactionStateSF.h +++ b/src/ripple/app/ledger/TransactionStateSF.h @@ -32,14 +32,17 @@ class TransactionStateSF : public SHAMapSyncFilter { public: TransactionStateSF(NodeStore::Database& db, AbstractFetchPackContainer& fp) - : db_(db) - , fp_(fp) - {} + : db_(db), fp_(fp) + { + } void - gotNode(bool fromFilter, SHAMapHash const& nodeHash, - std::uint32_t ledgerSeq, Blob&& nodeData, - SHAMapTreeNode::TNType type) const override; + gotNode( + bool fromFilter, + SHAMapHash const& nodeHash, + std::uint32_t ledgerSeq, + Blob&& nodeData, + SHAMapTreeNode::TNType type) const override; boost::optional getNode(SHAMapHash const& nodeHash) const override; @@ -49,6 +52,6 @@ private: AbstractFetchPackContainer& fp_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/impl/BuildLedger.cpp b/src/ripple/app/ledger/impl/BuildLedger.cpp index acde44478b..62aab96415 100644 --- a/src/ripple/app/ledger/impl/BuildLedger.cpp +++ b/src/ripple/app/ledger/impl/BuildLedger.cpp @@ -62,10 +62,10 @@ buildLedgerImpl( // Write the final version of all modified SHAMap // nodes to the node store to preserve the new LCL - int const asf = built->stateMap().flushDirty( - hotACCOUNT_NODE, built->info().seq); - int const tmf = built->txMap().flushDirty( - hotTRANSACTION_NODE, built->info().seq); + int const asf = + built->stateMap().flushDirty(hotACCOUNT_NODE, built->info().seq); + int const tmf = + built->txMap().flushDirty(hotTRANSACTION_NODE, built->info().seq); JLOG(j.debug()) << "Flushed " << asf << " accounts and " << tmf << " transaction nodes"; } @@ -103,9 +103,8 @@ applyTransactions( // Attempt to apply all of the retriable transactions for (int pass = 0; pass < LEDGER_TOTAL_PASSES; ++pass) { - JLOG(j.debug()) - << (certainRetry ? "Pass: " : "Final pass: ") << pass - << " begins (" << txns.size() << " transactions)"; + JLOG(j.debug()) << (certainRetry ? "Pass: " : "Final pass: ") << pass + << " begins (" << txns.size() << " transactions)"; int changes = 0; auto it = txns.begin(); @@ -147,9 +146,8 @@ applyTransactions( } } - JLOG(j.debug()) - << (certainRetry ? "Pass: " : "Final pass: ") << pass - << " completed (" << changes << " changes)"; + JLOG(j.debug()) << (certainRetry ? "Pass: " : "Final pass: ") << pass + << " completed (" << changes << " changes)"; // Accumulate changes. count += changes; @@ -181,30 +179,30 @@ buildLedger( std::set& failedTxns, beast::Journal j) { - JLOG(j.debug()) << "Report: Transaction Set = " << txns.key() - << ", close " << closeTime.time_since_epoch().count() + JLOG(j.debug()) << "Report: Transaction Set = " << txns.key() << ", close " + << closeTime.time_since_epoch().count() << (closeTimeCorrect ? "" : " (incorrect)"); - return buildLedgerImpl(parent, closeTime, closeTimeCorrect, - closeResolution, app, j, - [&](OpenView& accum, std::shared_ptr const& built) - { + return buildLedgerImpl( + parent, + closeTime, + closeTimeCorrect, + closeResolution, + app, + j, + [&](OpenView& accum, std::shared_ptr const& built) { JLOG(j.debug()) - << "Attempting to apply " << txns.size() - << " transactions"; + << "Attempting to apply " << txns.size() << " transactions"; - auto const applied = applyTransactions(app, built, txns, - failedTxns, accum, j); + auto const applied = + applyTransactions(app, built, txns, failedTxns, accum, j); if (!txns.empty() || !failedTxns.empty()) - JLOG(j.debug()) - << "Applied " << applied << " transactions; " - << failedTxns.size() << " failed and " - << txns.size() << " will be retried."; + JLOG(j.debug()) << "Applied " << applied << " transactions; " + << failedTxns.size() << " failed and " + << txns.size() << " will be retried."; else - JLOG(j.debug()) - << "Applied " << applied - << " transactions."; + JLOG(j.debug()) << "Applied " << applied << " transactions."; }); } @@ -227,8 +225,7 @@ buildLedger( replayLedger->info().closeTimeResolution, app, j, - [&](OpenView& accum, std::shared_ptr const& built) - { + [&](OpenView& accum, std::shared_ptr const& built) { for (auto& tx : replayData.orderedTxns()) applyTransaction(app, accum, *tx.second, false, applyFlags, j); }); diff --git a/src/ripple/app/ledger/impl/InboundLedger.cpp b/src/ripple/app/ledger/impl/InboundLedger.cpp index db5465593d..7a2c02fb6c 100644 --- a/src/ripple/app/ledger/impl/InboundLedger.cpp +++ b/src/ripple/app/ledger/impl/InboundLedger.cpp @@ -17,9 +17,8 @@ */ //============================================================================== -#include -#include #include +#include #include #include #include @@ -27,11 +26,12 @@ #include #include #include +#include #include -#include #include #include -#include +#include +#include #include @@ -39,66 +39,79 @@ namespace ripple { using namespace std::chrono_literals; -enum -{ +enum { // Number of peers to start with peerCountStart = 4 // Number of peers to add on a timeout - ,peerCountAdd = 2 + , + peerCountAdd = 2 // how many timeouts before we giveup - ,ledgerTimeoutRetriesMax = 10 + , + ledgerTimeoutRetriesMax = 10 // how many timeouts before we get aggressive - ,ledgerBecomeAggressiveThreshold = 6 + , + ledgerBecomeAggressiveThreshold = 6 // Number of nodes to find initially - ,missingNodesFind = 256 + , + missingNodesFind = 256 // Number of nodes to request for a reply - ,reqNodesReply = 128 + , + reqNodesReply = 128 // Number of nodes to request blindly - ,reqNodes = 8 + , + reqNodes = 8 }; // millisecond for each ledger timeout auto constexpr ledgerAcquireTimeout = 2500ms; -InboundLedger::InboundLedger(Application& app, uint256 const& hash, - std::uint32_t seq, Reason reason, clock_type& clock) - : PeerSet (app, hash, ledgerAcquireTimeout, clock, - app.journal("InboundLedger")) - , mHaveHeader (false) - , mHaveState (false) - , mHaveTransactions (false) - , mSignaled (false) - , mByHash (true) - , mSeq (seq) - , mReason (reason) - , mReceiveDispatched (false) +InboundLedger::InboundLedger( + Application& app, + uint256 const& hash, + std::uint32_t seq, + Reason reason, + clock_type& clock) + : PeerSet( + app, + hash, + ledgerAcquireTimeout, + clock, + app.journal("InboundLedger")) + , mHaveHeader(false) + , mHaveState(false) + , mHaveTransactions(false) + , mSignaled(false) + , mByHash(true) + , mSeq(seq) + , mReason(reason) + , mReceiveDispatched(false) { - JLOG (m_journal.trace()) << "Acquiring ledger " << mHash; + JLOG(m_journal.trace()) << "Acquiring ledger " << mHash; } void InboundLedger::init(ScopedLockType& collectionLock) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); collectionLock.unlock(); tryDB(app_.family()); if (mFailed) return; - if (! mComplete) + if (!mComplete) { auto shardStore = app_.getShardStore(); if (mReason == Reason::SHARD) { - if (! shardStore || ! app_.shardFamily()) + if (!shardStore || !app_.shardFamily()) { - JLOG(m_journal.error()) << - "Acquiring shard with no shard store available"; + JLOG(m_journal.error()) + << "Acquiring shard with no shard store available"; mFailed = true; return; } @@ -122,16 +135,15 @@ InboundLedger::init(ScopedLockType& collectionLock) } } } - if (! mComplete) + if (!mComplete) { addPeers(); execute(); return; } - JLOG (m_journal.debug()) << - "Acquiring ledger we already have in " << - " local store. " << mHash; + JLOG(m_journal.debug()) << "Acquiring ledger we already have in " + << " local store. " << mHash; mLedger->setImmutable(app_.config()); if (mReason == Reason::HISTORY || mReason == Reason::SHARD) @@ -144,43 +156,43 @@ InboundLedger::init(ScopedLockType& collectionLock) app_.getLedgerMaster().checkAccept(mLedger); } -void InboundLedger::execute () +void +InboundLedger::execute() { - if (app_.getJobQueue ().getJobCountTotal (jtLEDGER_DATA) > 4) + if (app_.getJobQueue().getJobCountTotal(jtLEDGER_DATA) > 4) { - JLOG (m_journal.debug()) << - "Deferring InboundLedger timer due to load"; - setTimer (); + JLOG(m_journal.debug()) << "Deferring InboundLedger timer due to load"; + setTimer(); return; } - app_.getJobQueue ().addJob ( - jtLEDGER_DATA, "InboundLedger", - [ptr = shared_from_this()] (Job&) - { - ptr->invokeOnTimer (); + app_.getJobQueue().addJob( + jtLEDGER_DATA, "InboundLedger", [ptr = shared_from_this()](Job&) { + ptr->invokeOnTimer(); }); } -void InboundLedger::update (std::uint32_t seq) +void +InboundLedger::update(std::uint32_t seq) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); // If we didn't know the sequence number, but now do, save it if ((seq != 0) && (mSeq == 0)) mSeq = seq; // Prevent this from being swept - touch (); + touch(); } -bool InboundLedger::checkLocal () +bool +InboundLedger::checkLocal() { - ScopedLockType sl (mLock); - if (! isDone()) + ScopedLockType sl(mLock); + if (!isDone()) { if (mLedger) tryDB(mLedger->stateMap().family()); - else if(mReason == Reason::SHARD) + else if (mReason == Reason::SHARD) tryDB(*app_.shardFamily()); else tryDB(app_.family()); @@ -193,81 +205,78 @@ bool InboundLedger::checkLocal () return false; } -InboundLedger::~InboundLedger () +InboundLedger::~InboundLedger() { // Save any received AS data not processed. It could be useful // for populating a different ledger for (auto& entry : mReceivedData) { - if (entry.second->type () == protocol::liAS_NODE) + if (entry.second->type() == protocol::liAS_NODE) app_.getInboundLedgers().gotStaleData(entry.second); } - if (! isDone()) + if (!isDone()) { - JLOG (m_journal.debug()) << - "Acquire " << mHash << " abort " << - ((getTimeouts () == 0) ? std::string() : - (std::string ("timeouts:") + - std::to_string (getTimeouts ()) + " ")) << - mStats.get (); + JLOG(m_journal.debug()) + << "Acquire " << mHash << " abort " + << ((getTimeouts() == 0) ? std::string() + : (std::string("timeouts:") + + std::to_string(getTimeouts()) + " ")) + << mStats.get(); } } std::vector -InboundLedger::neededTxHashes ( - int max, SHAMapSyncFilter* filter) const +InboundLedger::neededTxHashes(int max, SHAMapSyncFilter* filter) const { std::vector ret; - if (mLedger->info().txHash.isNonZero ()) + if (mLedger->info().txHash.isNonZero()) { - if (mLedger->txMap().getHash().isZero ()) - ret.push_back (mLedger->info().txHash); + if (mLedger->txMap().getHash().isZero()) + ret.push_back(mLedger->info().txHash); else - ret = mLedger->txMap().getNeededHashes (max, filter); + ret = mLedger->txMap().getNeededHashes(max, filter); } return ret; } std::vector -InboundLedger::neededStateHashes ( - int max, SHAMapSyncFilter* filter) const +InboundLedger::neededStateHashes(int max, SHAMapSyncFilter* filter) const { std::vector ret; - if (mLedger->info().accountHash.isNonZero ()) + if (mLedger->info().accountHash.isNonZero()) { - if (mLedger->stateMap().getHash().isZero ()) - ret.push_back (mLedger->info().accountHash); + if (mLedger->stateMap().getHash().isZero()) + ret.push_back(mLedger->info().accountHash); else - ret = mLedger->stateMap().getNeededHashes (max, filter); + ret = mLedger->stateMap().getNeededHashes(max, filter); } return ret; } LedgerInfo -InboundLedger::deserializeHeader ( - Slice data, - bool hasPrefix) +InboundLedger::deserializeHeader(Slice data, bool hasPrefix) { - SerialIter sit (data.data(), data.size()); + SerialIter sit(data.data(), data.size()); if (hasPrefix) - sit.get32 (); + sit.get32(); LedgerInfo info; - info.seq = sit.get32 (); - info.drops = sit.get64 (); - info.parentHash = sit.get256 (); - info.txHash = sit.get256 (); - info.accountHash = sit.get256 (); - info.parentCloseTime = NetClock::time_point{NetClock::duration{sit.get32()}}; + info.seq = sit.get32(); + info.drops = sit.get64(); + info.parentHash = sit.get256(); + info.txHash = sit.get256(); + info.accountHash = sit.get256(); + info.parentCloseTime = + NetClock::time_point{NetClock::duration{sit.get32()}}; info.closeTime = NetClock::time_point{NetClock::duration{sit.get32()}}; info.closeTimeResolution = NetClock::duration{sit.get8()}; - info.closeFlags = sit.get8 (); + info.closeFlags = sit.get8(); return info; } @@ -277,46 +286,40 @@ InboundLedger::deserializeHeader ( void InboundLedger::tryDB(Family& f) { - if (! mHaveHeader) + if (!mHaveHeader) { - auto makeLedger = [&, this](Blob const& data) + auto makeLedger = [&, this](Blob const& data) { + JLOG(m_journal.trace()) << "Ledger header found in fetch pack"; + mLedger = std::make_shared( + deserializeHeader(makeSlice(data), true), app_.config(), f); + if (mLedger->info().hash != mHash || + (mSeq != 0 && mSeq != mLedger->info().seq)) { - JLOG(m_journal.trace()) << - "Ledger header found in fetch pack"; - mLedger = std::make_shared( - deserializeHeader(makeSlice(data), true), - app_.config(), f); - if (mLedger->info().hash != mHash || - (mSeq != 0 && mSeq != mLedger->info().seq)) - { - // We know for a fact the ledger can never be acquired - JLOG(m_journal.warn()) << - "hash " << mHash << - " seq " << std::to_string(mSeq) << - " cannot be a ledger"; - mLedger.reset(); - mFailed = true; - } - }; + // We know for a fact the ledger can never be acquired + JLOG(m_journal.warn()) + << "hash " << mHash << " seq " << std::to_string(mSeq) + << " cannot be a ledger"; + mLedger.reset(); + mFailed = true; + } + }; // Try to fetch the ledger header from the DB auto node = f.db().fetch(mHash, mSeq); - if (! node) + if (!node) { auto data = app_.getLedgerMaster().getFetchPack(mHash); - if (! data) + if (!data) return; - JLOG (m_journal.trace()) << - "Ledger header found in fetch pack"; + JLOG(m_journal.trace()) << "Ledger header found in fetch pack"; makeLedger(*data); if (mLedger) - f.db().store(hotLEDGER, std::move(*data), - mHash, mLedger->info().seq); + f.db().store( + hotLEDGER, std::move(*data), mHash, mLedger->info().seq); } else { - JLOG (m_journal.trace()) << - "Ledger header found in node store"; + JLOG(m_journal.trace()) << "Ledger header found in node store"; makeLedger(node->getData()); } if (mFailed) @@ -328,48 +331,46 @@ InboundLedger::tryDB(Family& f) mHaveHeader = true; } - if (! mHaveTransactions) + if (!mHaveTransactions) { if (mLedger->info().txHash.isZero()) { - JLOG (m_journal.trace()) << "No TXNs to fetch"; + JLOG(m_journal.trace()) << "No TXNs to fetch"; mHaveTransactions = true; } else { - TransactionStateSF filter(mLedger->txMap().family().db(), - app_.getLedgerMaster()); + TransactionStateSF filter( + mLedger->txMap().family().db(), app_.getLedgerMaster()); if (mLedger->txMap().fetchRoot( - SHAMapHash{mLedger->info().txHash}, &filter)) + SHAMapHash{mLedger->info().txHash}, &filter)) { if (neededTxHashes(1, &filter).empty()) { - JLOG(m_journal.trace()) << - "Had full txn map locally"; + JLOG(m_journal.trace()) << "Had full txn map locally"; mHaveTransactions = true; } } } } - if (! mHaveState) + if (!mHaveState) { if (mLedger->info().accountHash.isZero()) { - JLOG (m_journal.fatal()) << - "We are acquiring a ledger with a zero account hash"; + JLOG(m_journal.fatal()) + << "We are acquiring a ledger with a zero account hash"; mFailed = true; return; } - AccountStateSF filter(mLedger->stateMap().family().db(), - app_.getLedgerMaster()); + AccountStateSF filter( + mLedger->stateMap().family().db(), app_.getLedgerMaster()); if (mLedger->stateMap().fetchRoot( - SHAMapHash{mLedger->info().accountHash}, &filter)) + SHAMapHash{mLedger->info().accountHash}, &filter)) { if (neededStateHashes(1, &filter).empty()) { - JLOG(m_journal.trace()) << - "Had full AS map locally"; + JLOG(m_journal.trace()) << "Had full AS map locally"; mHaveState = true; } } @@ -377,40 +378,39 @@ InboundLedger::tryDB(Family& f) if (mHaveTransactions && mHaveState) { - JLOG(m_journal.debug()) << - "Had everything locally"; + JLOG(m_journal.debug()) << "Had everything locally"; mComplete = true; mLedger->setImmutable(app_.config()); } } /** Called with a lock by the PeerSet when the timer expires -*/ -void InboundLedger::onTimer (bool wasProgress, ScopedLockType&) + */ +void +InboundLedger::onTimer(bool wasProgress, ScopedLockType&) { - mRecentNodes.clear (); + mRecentNodes.clear(); if (isDone()) { - JLOG (m_journal.info()) << - "Already done " << mHash; + JLOG(m_journal.info()) << "Already done " << mHash; return; } - if (getTimeouts () > ledgerTimeoutRetriesMax) + if (getTimeouts() > ledgerTimeoutRetriesMax) { if (mSeq != 0) { - JLOG (m_journal.warn()) << - getTimeouts() << " timeouts for ledger " << mSeq; + JLOG(m_journal.warn()) + << getTimeouts() << " timeouts for ledger " << mSeq; } else { - JLOG (m_journal.warn()) << - getTimeouts() << " timeouts for ledger " << mHash; + JLOG(m_journal.warn()) + << getTimeouts() << " timeouts for ledger " << mHash; } - setFailed (); - done (); + setFailed(); + done(); return; } @@ -420,187 +420,179 @@ void InboundLedger::onTimer (bool wasProgress, ScopedLockType&) mByHash = true; - std::size_t pc = getPeerCount (); - JLOG (m_journal.debug()) << - "No progress(" << pc << - ") for ledger " << mHash; + std::size_t pc = getPeerCount(); + JLOG(m_journal.debug()) + << "No progress(" << pc << ") for ledger " << mHash; // addPeers triggers if the reason is not HISTORY // So if the reason IS HISTORY, need to trigger after we add // otherwise, we need to trigger before we add // so each peer gets triggered once if (mReason != Reason::HISTORY) - trigger (nullptr, TriggerReason::timeout); - addPeers (); + trigger(nullptr, TriggerReason::timeout); + addPeers(); if (mReason == Reason::HISTORY) - trigger (nullptr, TriggerReason::timeout); + trigger(nullptr, TriggerReason::timeout); } } /** Add more peers to the set, if possible */ -void InboundLedger::addPeers () +void +InboundLedger::addPeers() { - app_.overlay().selectPeers (*this, + app_.overlay().selectPeers( + *this, (getPeerCount() == 0) ? peerCountStart : peerCountAdd, - ScoreHasLedger (getHash(), mSeq)); + ScoreHasLedger(getHash(), mSeq)); } -std::weak_ptr InboundLedger::pmDowncast () +std::weak_ptr +InboundLedger::pmDowncast() { - return std::dynamic_pointer_cast (shared_from_this ()); + return std::dynamic_pointer_cast(shared_from_this()); } -void InboundLedger::done () +void +InboundLedger::done() { if (mSignaled) return; mSignaled = true; - touch (); + touch(); - JLOG (m_journal.debug()) << - "Acquire " << mHash << - (mFailed ? " fail " : " ") << - ((getTimeouts () == 0) ? std::string() : - (std::string ("timeouts:") + - std::to_string (getTimeouts ()) + " ")) << - mStats.get (); + JLOG(m_journal.debug()) + << "Acquire " << mHash << (mFailed ? " fail " : " ") + << ((getTimeouts() == 0) ? std::string() + : (std::string("timeouts:") + + std::to_string(getTimeouts()) + " ")) + << mStats.get(); - assert (mComplete || mFailed); + assert(mComplete || mFailed); - if (mComplete && ! mFailed && mLedger) + if (mComplete && !mFailed && mLedger) { - mLedger->setImmutable (app_.config()); + mLedger->setImmutable(app_.config()); switch (mReason) { - case Reason::SHARD: - app_.getShardStore()->setStored(mLedger); - [[fallthrough]]; - case Reason::HISTORY: - app_.getInboundLedgers().onLedgerFetched(); - break; - default: - app_.getLedgerMaster().storeLedger(mLedger); - break; + case Reason::SHARD: + app_.getShardStore()->setStored(mLedger); + [[fallthrough]]; + case Reason::HISTORY: + app_.getInboundLedgers().onLedgerFetched(); + break; + default: + app_.getLedgerMaster().storeLedger(mLedger); + break; } } // We hold the PeerSet lock, so must dispatch - app_.getJobQueue ().addJob ( - jtLEDGER_DATA, "AcquisitionDone", - [self = shared_from_this()](Job&) - { + app_.getJobQueue().addJob( + jtLEDGER_DATA, "AcquisitionDone", [self = shared_from_this()](Job&) { if (self->mComplete && !self->mFailed) { - self->app().getLedgerMaster().checkAccept( - self->getLedger()); + self->app().getLedgerMaster().checkAccept(self->getLedger()); self->app().getLedgerMaster().tryAdvance(); } else - self->app().getInboundLedgers().logFailure ( + self->app().getInboundLedgers().logFailure( self->getHash(), self->getSeq()); }); } /** Request more nodes, perhaps from a specific peer -*/ -void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason reason) + */ +void +InboundLedger::trigger(std::shared_ptr const& peer, TriggerReason reason) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - if (isDone ()) + if (isDone()) { - JLOG (m_journal.debug()) << - "Trigger on ledger: " << mHash << - (mComplete ? " completed" : "") << - (mFailed ? " failed" : ""); + JLOG(m_journal.debug()) + << "Trigger on ledger: " << mHash << (mComplete ? " completed" : "") + << (mFailed ? " failed" : ""); return; } if (auto stream = m_journal.trace()) { if (peer) - stream << - "Trigger acquiring ledger " << mHash << " from " << peer; + stream << "Trigger acquiring ledger " << mHash << " from " << peer; else - stream << - "Trigger acquiring ledger " << mHash; + stream << "Trigger acquiring ledger " << mHash; if (mComplete || mFailed) - stream << - "complete=" << mComplete << " failed=" << mFailed; + stream << "complete=" << mComplete << " failed=" << mFailed; else - stream << - "header=" << mHaveHeader << " tx=" << mHaveTransactions << - " as=" << mHaveState; + stream << "header=" << mHaveHeader << " tx=" << mHaveTransactions + << " as=" << mHaveState; } - if (! mHaveHeader) + if (!mHaveHeader) { - tryDB(mReason == Reason::SHARD ? - *app_.shardFamily() : app_.family()); + tryDB(mReason == Reason::SHARD ? *app_.shardFamily() : app_.family()); if (mFailed) { - JLOG (m_journal.warn()) << - " failed local for " << mHash; + JLOG(m_journal.warn()) << " failed local for " << mHash; return; } } protocol::TMGetLedger tmGL; - tmGL.set_ledgerhash (mHash.begin (), mHash.size ()); + tmGL.set_ledgerhash(mHash.begin(), mHash.size()); - if (getTimeouts () != 0) - { // Be more aggressive if we've timed out at least once - tmGL.set_querytype (protocol::qtINDIRECT); + if (getTimeouts() != 0) + { // Be more aggressive if we've timed out at least once + tmGL.set_querytype(protocol::qtINDIRECT); - if (! isProgress () && ! mFailed && mByHash && - (getTimeouts () > ledgerBecomeAggressiveThreshold)) + if (!isProgress() && !mFailed && mByHash && + (getTimeouts() > ledgerBecomeAggressiveThreshold)) { - auto need = getNeededHashes (); + auto need = getNeededHashes(); - if (!need.empty ()) + if (!need.empty()) { protocol::TMGetObjectByHash tmBH; bool typeSet = false; - tmBH.set_query (true); - tmBH.set_ledgerhash (mHash.begin (), mHash.size ()); + tmBH.set_query(true); + tmBH.set_ledgerhash(mHash.begin(), mHash.size()); for (auto const& p : need) { - JLOG (m_journal.warn()) << - "Want: " << p.second; + JLOG(m_journal.warn()) << "Want: " << p.second; if (!typeSet) { - tmBH.set_type (p.first); + tmBH.set_type(p.first); typeSet = true; } - if (p.first == tmBH.type ()) + if (p.first == tmBH.type()) { - protocol::TMIndexedObject* io = tmBH.add_objects (); - io->set_hash (p.second.begin (), p.second.size ()); + protocol::TMIndexedObject* io = tmBH.add_objects(); + io->set_hash(p.second.begin(), p.second.size()); if (mSeq != 0) io->set_ledgerseq(mSeq); } } - auto packet = std::make_shared ( - tmBH, protocol::mtGET_OBJECTS); + auto packet = + std::make_shared(tmBH, protocol::mtGET_OBJECTS); for (auto id : mPeers) { - if (auto p = app_.overlay ().findPeerByShortID (id)) + if (auto p = app_.overlay().findPeerByShortID(id)) { mByHash = false; - p->send (packet); + p->send(packet); } } } else { - JLOG (m_journal.info()) << - "getNeededHashes says acquire is complete"; + JLOG(m_journal.info()) + << "getNeededHashes says acquire is complete"; mHaveHeader = true; mHaveTransactions = true; mHaveState = true; @@ -613,70 +605,68 @@ void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason re // state or transaction root hashes. if (!mHaveHeader && !mFailed) { - tmGL.set_itype (protocol::liBASE); + tmGL.set_itype(protocol::liBASE); if (mSeq != 0) - tmGL.set_ledgerseq (mSeq); - JLOG (m_journal.trace()) << - "Sending header request to " << - (peer ? "selected peer" : "all peers"); - sendRequest (tmGL, peer); + tmGL.set_ledgerseq(mSeq); + JLOG(m_journal.trace()) << "Sending header request to " + << (peer ? "selected peer" : "all peers"); + sendRequest(tmGL, peer); return; } if (mLedger) - tmGL.set_ledgerseq (mLedger->info().seq); + tmGL.set_ledgerseq(mLedger->info().seq); if (reason != TriggerReason::reply) { // If we're querying blind, don't query deep - tmGL.set_querydepth (0); + tmGL.set_querydepth(0); } - else if (peer && peer->isHighLatency ()) + else if (peer && peer->isHighLatency()) { // If the peer has high latency, query extra deep - tmGL.set_querydepth (2); + tmGL.set_querydepth(2); } else - tmGL.set_querydepth (1); + tmGL.set_querydepth(1); // Get the state data first because it's the most likely to be useful // if we wind up abandoning this fetch. if (mHaveHeader && !mHaveState && !mFailed) { - assert (mLedger); + assert(mLedger); - if (!mLedger->stateMap().isValid ()) + if (!mLedger->stateMap().isValid()) { mFailed = true; } - else if (mLedger->stateMap().getHash ().isZero ()) + else if (mLedger->stateMap().getHash().isZero()) { // we need the root node - tmGL.set_itype (protocol::liAS_NODE); - *tmGL.add_nodeids () = SHAMapNodeID ().getRawString (); - JLOG (m_journal.trace()) << - "Sending AS root request to " << - (peer ? "selected peer" : "all peers"); - sendRequest (tmGL, peer); + tmGL.set_itype(protocol::liAS_NODE); + *tmGL.add_nodeids() = SHAMapNodeID().getRawString(); + JLOG(m_journal.trace()) << "Sending AS root request to " + << (peer ? "selected peer" : "all peers"); + sendRequest(tmGL, peer); return; } else { - AccountStateSF filter(mLedger->stateMap().family().db(), - app_.getLedgerMaster()); + AccountStateSF filter( + mLedger->stateMap().family().db(), app_.getLedgerMaster()); // Release the lock while we process the large state map sl.unlock(); - auto nodes = mLedger->stateMap().getMissingNodes ( - missingNodesFind, &filter); + auto nodes = + mLedger->stateMap().getMissingNodes(missingNodesFind, &filter); sl.lock(); // Make sure nothing happened while we released the lock if (!mFailed && !mComplete && !mHaveState) { - if (nodes.empty ()) + if (nodes.empty()) { - if (!mLedger->stateMap().isValid ()) + if (!mLedger->stateMap().isValid()) mFailed = true; else { @@ -688,27 +678,26 @@ void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason re } else { - filterNodes (nodes, reason); + filterNodes(nodes, reason); - if (!nodes.empty ()) + if (!nodes.empty()) { - tmGL.set_itype (protocol::liAS_NODE); + tmGL.set_itype(protocol::liAS_NODE); for (auto const& id : nodes) { - * (tmGL.add_nodeids ()) = id.first.getRawString (); + *(tmGL.add_nodeids()) = id.first.getRawString(); } - JLOG (m_journal.trace()) << - "Sending AS node request (" << - nodes.size () << ") to " << - (peer ? "selected peer" : "all peers"); - sendRequest (tmGL, peer); + JLOG(m_journal.trace()) + << "Sending AS node request (" << nodes.size() + << ") to " + << (peer ? "selected peer" : "all peers"); + sendRequest(tmGL, peer); return; } else { - JLOG (m_journal.trace()) << - "All AS nodes filtered"; + JLOG(m_journal.trace()) << "All AS nodes filtered"; } } } @@ -717,34 +706,33 @@ void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason re if (mHaveHeader && !mHaveTransactions && !mFailed) { - assert (mLedger); + assert(mLedger); - if (!mLedger->txMap().isValid ()) + if (!mLedger->txMap().isValid()) { mFailed = true; } - else if (mLedger->txMap().getHash ().isZero ()) + else if (mLedger->txMap().getHash().isZero()) { // we need the root node - tmGL.set_itype (protocol::liTX_NODE); - * (tmGL.add_nodeids ()) = SHAMapNodeID ().getRawString (); - JLOG (m_journal.trace()) << - "Sending TX root request to " << ( - peer ? "selected peer" : "all peers"); - sendRequest (tmGL, peer); + tmGL.set_itype(protocol::liTX_NODE); + *(tmGL.add_nodeids()) = SHAMapNodeID().getRawString(); + JLOG(m_journal.trace()) << "Sending TX root request to " + << (peer ? "selected peer" : "all peers"); + sendRequest(tmGL, peer); return; } else { - TransactionStateSF filter(mLedger->txMap().family().db(), - app_.getLedgerMaster()); + TransactionStateSF filter( + mLedger->txMap().family().db(), app_.getLedgerMaster()); - auto nodes = mLedger->txMap().getMissingNodes ( - missingNodesFind, &filter); + auto nodes = + mLedger->txMap().getMissingNodes(missingNodesFind, &filter); - if (nodes.empty ()) + if (nodes.empty()) { - if (!mLedger->txMap().isValid ()) + if (!mLedger->txMap().isValid()) mFailed = true; else { @@ -756,26 +744,24 @@ void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason re } else { - filterNodes (nodes, reason); + filterNodes(nodes, reason); - if (!nodes.empty ()) + if (!nodes.empty()) { - tmGL.set_itype (protocol::liTX_NODE); + tmGL.set_itype(protocol::liTX_NODE); for (auto const& n : nodes) { - * (tmGL.add_nodeids ()) = n.first.getRawString (); + *(tmGL.add_nodeids()) = n.first.getRawString(); } - JLOG (m_journal.trace()) << - "Sending TX node request (" << - nodes.size () << ") to " << - (peer ? "selected peer" : "all peers"); - sendRequest (tmGL, peer); + JLOG(m_journal.trace()) + << "Sending TX node request (" << nodes.size() + << ") to " << (peer ? "selected peer" : "all peers"); + sendRequest(tmGL, peer); return; } else { - JLOG (m_journal.trace()) << - "All TX nodes filtered"; + JLOG(m_journal.trace()) << "All TX nodes filtered"; } } } @@ -783,85 +769,79 @@ void InboundLedger::trigger (std::shared_ptr const& peer, TriggerReason re if (mComplete || mFailed) { - JLOG (m_journal.debug()) << - "Done:" << (mComplete ? " complete" : "") << - (mFailed ? " failed " : " ") << - mLedger->info().seq; - sl.unlock (); - done (); + JLOG(m_journal.debug()) + << "Done:" << (mComplete ? " complete" : "") + << (mFailed ? " failed " : " ") << mLedger->info().seq; + sl.unlock(); + done(); } } -void InboundLedger::filterNodes ( +void +InboundLedger::filterNodes( std::vector>& nodes, TriggerReason reason) { // Sort nodes so that the ones we haven't recently // requested come before the ones we have. - auto dup = std::stable_partition ( - nodes.begin(), nodes.end(), - [this](auto const& item) - { - return mRecentNodes.count (item.second) == 0; + auto dup = std::stable_partition( + nodes.begin(), nodes.end(), [this](auto const& item) { + return mRecentNodes.count(item.second) == 0; }); // If everything is a duplicate we don't want to send // any query at all except on a timeout where we need // to query everyone: - if (dup == nodes.begin ()) + if (dup == nodes.begin()) { - JLOG (m_journal.trace()) << - "filterNodes: all duplicates"; + JLOG(m_journal.trace()) << "filterNodes: all duplicates"; if (reason != TriggerReason::timeout) { - nodes.clear (); + nodes.clear(); return; } } else { - JLOG (m_journal.trace()) << - "filterNodes: pruning duplicates"; + JLOG(m_journal.trace()) << "filterNodes: pruning duplicates"; - nodes.erase (dup, nodes.end()); + nodes.erase(dup, nodes.end()); } - std::size_t const limit = (reason == TriggerReason::reply) - ? reqNodesReply - : reqNodes; + std::size_t const limit = + (reason == TriggerReason::reply) ? reqNodesReply : reqNodes; - if (nodes.size () > limit) - nodes.resize (limit); + if (nodes.size() > limit) + nodes.resize(limit); for (auto const& n : nodes) - mRecentNodes.insert (n.second); + mRecentNodes.insert(n.second); } /** Take ledger header data Call with a lock */ // data must not have hash prefix -bool InboundLedger::takeHeader (std::string const& data) +bool +InboundLedger::takeHeader(std::string const& data) { // Return value: true=normal, false=bad data - JLOG (m_journal.trace()) << - "got header acquiring ledger " << mHash; + JLOG(m_journal.trace()) << "got header acquiring ledger " << mHash; if (mComplete || mFailed || mHaveHeader) return true; - auto* f = mReason == Reason::SHARD ? - app_.shardFamily() : &app_.family(); - mLedger = std::make_shared(deserializeHeader( - makeSlice(data), false), app_.config(), *f); + auto* f = mReason == Reason::SHARD ? app_.shardFamily() : &app_.family(); + mLedger = std::make_shared( + deserializeHeader(makeSlice(data), false), app_.config(), *f); if (mLedger->info().hash != mHash || (mSeq != 0 && mSeq != mLedger->info().seq)) { - JLOG (m_journal.warn()) << - "Acquire hash mismatch: " << mLedger->info().hash << - "!=" << mHash; - mLedger.reset (); + JLOG(m_journal.warn()) + << "Acquire hash mismatch: " << mLedger->info().hash + << "!=" << mHash; + mLedger.reset(); return false; } if (mSeq == 0) @@ -870,19 +850,19 @@ bool InboundLedger::takeHeader (std::string const& data) mLedger->txMap().setLedgerSeq(mSeq); mHaveHeader = true; - Serializer s (data.size () + 4); - s.add32 (HashPrefix::ledgerMaster); - s.addRaw (data.data(), data.size()); - f->db().store(hotLEDGER, std::move (s.modData ()), mHash, mSeq); + Serializer s(data.size() + 4); + s.add32(HashPrefix::ledgerMaster); + s.addRaw(data.data(), data.size()); + f->db().store(hotLEDGER, std::move(s.modData()), mHash, mSeq); - if (mLedger->info().txHash.isZero ()) + if (mLedger->info().txHash.isZero()) mHaveTransactions = true; - if (mLedger->info().accountHash.isZero ()) + if (mLedger->info().accountHash.isZero()) mHaveState = true; - mLedger->txMap().setSynching (); - mLedger->stateMap().setSynching (); + mLedger->txMap().setSynching(); + mLedger->stateMap().setSynching(); return true; } @@ -890,13 +870,15 @@ bool InboundLedger::takeHeader (std::string const& data) /** Process TX data received from a peer Call with a lock */ -bool InboundLedger::takeTxNode (const std::vector& nodeIDs, - const std::vector< Blob >& data, SHAMapAddNode& san) +bool +InboundLedger::takeTxNode( + const std::vector& nodeIDs, + const std::vector& data, + SHAMapAddNode& san) { if (!mHaveHeader) { - JLOG (m_journal.warn()) << - "TX node without header"; + JLOG(m_journal.warn()) << "TX node without header"; san.incInvalid(); return false; } @@ -907,24 +889,26 @@ bool InboundLedger::takeTxNode (const std::vector& nodeIDs, return true; } - auto nodeIDit = nodeIDs.cbegin (); - auto nodeDatait = data.begin (); - TransactionStateSF filter(mLedger->txMap().family().db(), - app_.getLedgerMaster()); + auto nodeIDit = nodeIDs.cbegin(); + auto nodeDatait = data.begin(); + TransactionStateSF filter( + mLedger->txMap().family().db(), app_.getLedgerMaster()); - while (nodeIDit != nodeIDs.cend ()) + while (nodeIDit != nodeIDs.cend()) { - if (nodeIDit->isRoot ()) + if (nodeIDit->isRoot()) { - san += mLedger->txMap().addRootNode ( + san += mLedger->txMap().addRootNode( SHAMapHash{mLedger->info().txHash}, - makeSlice(*nodeDatait), snfWIRE, &filter); + makeSlice(*nodeDatait), + snfWIRE, + &filter); if (!san.isGood()) return false; } else { - san += mLedger->txMap().addKnownNode ( + san += mLedger->txMap().addKnownNode( *nodeIDit, makeSlice(*nodeDatait), &filter); if (!san.isGood()) return false; @@ -934,14 +918,14 @@ bool InboundLedger::takeTxNode (const std::vector& nodeIDs, ++nodeDatait; } - if (!mLedger->txMap().isSynching ()) + if (!mLedger->txMap().isSynching()) { mHaveTransactions = true; if (mHaveState) { mComplete = true; - done (); + done(); } } @@ -951,24 +935,24 @@ bool InboundLedger::takeTxNode (const std::vector& nodeIDs, /** Process AS data received from a peer Call with a lock */ -bool InboundLedger::takeAsNode (const std::vector& nodeIDs, - const std::vector< Blob >& data, SHAMapAddNode& san) +bool +InboundLedger::takeAsNode( + const std::vector& nodeIDs, + const std::vector& data, + SHAMapAddNode& san) { - JLOG (m_journal.trace()) << - "got ASdata (" << nodeIDs.size () << - ") acquiring ledger " << mHash; - if (nodeIDs.size () == 1) + JLOG(m_journal.trace()) + << "got ASdata (" << nodeIDs.size() << ") acquiring ledger " << mHash; + if (nodeIDs.size() == 1) { - JLOG(m_journal.trace()) << - "got AS node: " << nodeIDs.front (); + JLOG(m_journal.trace()) << "got AS node: " << nodeIDs.front(); } - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); if (!mHaveHeader) { - JLOG (m_journal.warn()) << - "Don't have ledger header"; + JLOG(m_journal.warn()) << "Don't have ledger header"; san.incInvalid(); return false; } @@ -979,33 +963,33 @@ bool InboundLedger::takeAsNode (const std::vector& nodeIDs, return true; } - auto nodeIDit = nodeIDs.cbegin (); - auto nodeDatait = data.begin (); - AccountStateSF filter(mLedger->stateMap().family().db(), - app_.getLedgerMaster()); + auto nodeIDit = nodeIDs.cbegin(); + auto nodeDatait = data.begin(); + AccountStateSF filter( + mLedger->stateMap().family().db(), app_.getLedgerMaster()); - while (nodeIDit != nodeIDs.cend ()) + while (nodeIDit != nodeIDs.cend()) { - if (nodeIDit->isRoot ()) + if (nodeIDit->isRoot()) { - san += mLedger->stateMap().addRootNode ( + san += mLedger->stateMap().addRootNode( SHAMapHash{mLedger->info().accountHash}, - makeSlice(*nodeDatait), snfWIRE, &filter); - if (!san.isGood ()) + makeSlice(*nodeDatait), + snfWIRE, + &filter); + if (!san.isGood()) { - JLOG (m_journal.warn()) << - "Bad ledger header"; + JLOG(m_journal.warn()) << "Bad ledger header"; return false; } } else { - san += mLedger->stateMap().addKnownNode ( + san += mLedger->stateMap().addKnownNode( *nodeIDit, makeSlice(*nodeDatait), &filter); - if (!san.isGood ()) + if (!san.isGood()) { - JLOG (m_journal.warn()) << - "Unable to add AS node"; + JLOG(m_journal.warn()) << "Unable to add AS node"; return false; } } @@ -1014,14 +998,14 @@ bool InboundLedger::takeAsNode (const std::vector& nodeIDs, ++nodeDatait; } - if (!mLedger->stateMap().isSynching ()) + if (!mLedger->stateMap().isSynching()) { mHaveState = true; if (mHaveTransactions) { mComplete = true; - done (); + done(); } } @@ -1031,7 +1015,8 @@ bool InboundLedger::takeAsNode (const std::vector& nodeIDs, /** Process AS root node received from a peer Call with a lock */ -bool InboundLedger::takeAsRootNode (Slice const& data, SHAMapAddNode& san) +bool +InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san) { if (mFailed || mHaveState) { @@ -1045,9 +1030,9 @@ bool InboundLedger::takeAsRootNode (Slice const& data, SHAMapAddNode& san) return false; } - AccountStateSF filter(mLedger->stateMap().family().db(), - app_.getLedgerMaster()); - san += mLedger->stateMap().addRootNode ( + AccountStateSF filter( + mLedger->stateMap().family().db(), app_.getLedgerMaster()); + san += mLedger->stateMap().addRootNode( SHAMapHash{mLedger->info().accountHash}, data, snfWIRE, &filter); return san.isGood(); } @@ -1055,7 +1040,8 @@ bool InboundLedger::takeAsRootNode (Slice const& data, SHAMapAddNode& san) /** Process AS root node received from a peer Call with a lock */ -bool InboundLedger::takeTxRootNode (Slice const& data, SHAMapAddNode& san) +bool +InboundLedger::takeTxRootNode(Slice const& data, SHAMapAddNode& san) { if (mFailed || mHaveTransactions) { @@ -1069,43 +1055,43 @@ bool InboundLedger::takeTxRootNode (Slice const& data, SHAMapAddNode& san) return false; } - TransactionStateSF filter(mLedger->txMap().family().db(), - app_.getLedgerMaster()); - san += mLedger->txMap().addRootNode ( + TransactionStateSF filter( + mLedger->txMap().family().db(), app_.getLedgerMaster()); + san += mLedger->txMap().addRootNode( SHAMapHash{mLedger->info().txHash}, data, snfWIRE, &filter); return san.isGood(); } std::vector -InboundLedger::getNeededHashes () +InboundLedger::getNeededHashes() { std::vector ret; if (!mHaveHeader) { - ret.push_back (std::make_pair ( - protocol::TMGetObjectByHash::otLEDGER, mHash)); + ret.push_back( + std::make_pair(protocol::TMGetObjectByHash::otLEDGER, mHash)); return ret; } if (!mHaveState) { - AccountStateSF filter(mLedger->stateMap().family().db(), - app_.getLedgerMaster()); - for (auto const& h : neededStateHashes (4, &filter)) + AccountStateSF filter( + mLedger->stateMap().family().db(), app_.getLedgerMaster()); + for (auto const& h : neededStateHashes(4, &filter)) { - ret.push_back (std::make_pair ( - protocol::TMGetObjectByHash::otSTATE_NODE, h)); + ret.push_back( + std::make_pair(protocol::TMGetObjectByHash::otSTATE_NODE, h)); } } if (!mHaveTransactions) { - TransactionStateSF filter(mLedger->txMap().family().db(), - app_.getLedgerMaster()); - for (auto const& h : neededTxHashes (4, &filter)) + TransactionStateSF filter( + mLedger->txMap().family().db(), app_.getLedgerMaster()); + for (auto const& h : neededTxHashes(4, &filter)) { - ret.push_back (std::make_pair ( + ret.push_back(std::make_pair( protocol::TMGetObjectByHash::otTRANSACTION_NODE, h)); } } @@ -1117,15 +1103,16 @@ InboundLedger::getNeededHashes () Returns 'true' if we need to dispatch */ bool -InboundLedger::gotData(std::weak_ptr peer, +InboundLedger::gotData( + std::weak_ptr peer, std::shared_ptr const& data) { - std::lock_guard sl (mReceivedDataLock); + std::lock_guard sl(mReceivedDataLock); - if (isDone ()) + if (isDone()) return false; - mReceivedData.emplace_back (peer, data); + mReceivedData.emplace_back(peer, data); if (mReceiveDispatched) return false; @@ -1142,18 +1129,19 @@ InboundLedger::gotData(std::weak_ptr peer, // // TODO Change peer to Consumer // -int InboundLedger::processData (std::shared_ptr peer, +int +InboundLedger::processData( + std::shared_ptr peer, protocol::TMLedgerData& packet) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - if (packet.type () == protocol::liBASE) + if (packet.type() == protocol::liBASE) { - if (packet.nodes_size () < 1) + if (packet.nodes_size() < 1) { - JLOG (m_journal.warn()) << - "Got empty header data"; - peer->charge (Resource::feeInvalidRequest); + JLOG(m_journal.warn()) << "Got empty header data"; + peer->charge(Resource::feeInvalidRequest); return -1; } @@ -1161,93 +1149,85 @@ int InboundLedger::processData (std::shared_ptr peer, if (!mHaveHeader) { - if (takeHeader (packet.nodes (0).nodedata ())) - san.incUseful (); + if (takeHeader(packet.nodes(0).nodedata())) + san.incUseful(); else { - JLOG (m_journal.warn()) << - "Got invalid header data"; - peer->charge (Resource::feeInvalidRequest); + JLOG(m_journal.warn()) << "Got invalid header data"; + peer->charge(Resource::feeInvalidRequest); return -1; } } - - if (!mHaveState && (packet.nodes ().size () > 1) && - !takeAsRootNode (makeSlice(packet.nodes(1).nodedata ()), san)) + if (!mHaveState && (packet.nodes().size() > 1) && + !takeAsRootNode(makeSlice(packet.nodes(1).nodedata()), san)) { - JLOG (m_journal.warn()) << - "Included AS root invalid"; + JLOG(m_journal.warn()) << "Included AS root invalid"; } - if (!mHaveTransactions && (packet.nodes ().size () > 2) && - !takeTxRootNode (makeSlice(packet.nodes(2).nodedata ()), san)) + if (!mHaveTransactions && (packet.nodes().size() > 2) && + !takeTxRootNode(makeSlice(packet.nodes(2).nodedata()), san)) { - JLOG (m_journal.warn()) << - "Included TX root invalid"; + JLOG(m_journal.warn()) << "Included TX root invalid"; } - if (san.isUseful ()) - progress (); + if (san.isUseful()) + progress(); mStats += san; - return san.getGood (); + return san.getGood(); } - if ((packet.type () == protocol::liTX_NODE) || ( - packet.type () == protocol::liAS_NODE)) + if ((packet.type() == protocol::liTX_NODE) || + (packet.type() == protocol::liAS_NODE)) { - if (packet.nodes ().size () == 0) + if (packet.nodes().size() == 0) { - JLOG (m_journal.info()) << - "Got response with no nodes"; - peer->charge (Resource::feeInvalidRequest); + JLOG(m_journal.info()) << "Got response with no nodes"; + peer->charge(Resource::feeInvalidRequest); return -1; } std::vector nodeIDs; nodeIDs.reserve(packet.nodes().size()); - std::vector< Blob > nodeData; + std::vector nodeData; nodeData.reserve(packet.nodes().size()); - for (int i = 0; i < packet.nodes ().size (); ++i) + for (int i = 0; i < packet.nodes().size(); ++i) { - const protocol::TMLedgerNode& node = packet.nodes (i); + const protocol::TMLedgerNode& node = packet.nodes(i); - if (!node.has_nodeid () || !node.has_nodedata ()) + if (!node.has_nodeid() || !node.has_nodedata()) { - JLOG (m_journal.warn()) << - "Got bad node"; - peer->charge (Resource::feeInvalidRequest); + JLOG(m_journal.warn()) << "Got bad node"; + peer->charge(Resource::feeInvalidRequest); return -1; } - nodeIDs.push_back (SHAMapNodeID (node.nodeid ().data (), - node.nodeid ().size ())); - nodeData.push_back (Blob (node.nodedata ().begin (), - node.nodedata ().end ())); + nodeIDs.push_back( + SHAMapNodeID(node.nodeid().data(), node.nodeid().size())); + nodeData.push_back( + Blob(node.nodedata().begin(), node.nodedata().end())); } SHAMapAddNode san; - if (packet.type () == protocol::liTX_NODE) + if (packet.type() == protocol::liTX_NODE) { - takeTxNode (nodeIDs, nodeData, san); - JLOG (m_journal.debug()) << - "Ledger TX node stats: " << san.get(); + takeTxNode(nodeIDs, nodeData, san); + JLOG(m_journal.debug()) << "Ledger TX node stats: " << san.get(); } else { - takeAsNode (nodeIDs, nodeData, san); - JLOG (m_journal.debug()) << - "Ledger AS node stats: " << san.get(); + takeAsNode(nodeIDs, nodeData, san); + JLOG(m_journal.debug()) << "Ledger AS node stats: " << san.get(); } - if (san.isUseful ()) - progress (); + if (san.isUseful()) + progress(); mStats += san; - return san.getGood (); + return san.getGood(); } return -1; @@ -1256,20 +1236,21 @@ int InboundLedger::processData (std::shared_ptr peer, /** Process pending TMLedgerData Query the 'best' peer */ -void InboundLedger::runData () +void +InboundLedger::runData() { std::shared_ptr chosenPeer; int chosenPeerCount = -1; - std::vector data; + std::vector data; for (;;) { data.clear(); { - std::lock_guard sl (mReceivedDataLock); + std::lock_guard sl(mReceivedDataLock); - if (mReceivedData.empty ()) + if (mReceivedData.empty()) { mReceiveDispatched = false; break; @@ -1284,27 +1265,28 @@ void InboundLedger::runData () { if (auto peer = entry.first.lock()) { - int count = processData (peer, *(entry.second)); + int count = processData(peer, *(entry.second)); if (count > chosenPeerCount) { chosenPeerCount = count; - chosenPeer = std::move (peer); + chosenPeer = std::move(peer); } } } } if (chosenPeer) - trigger (chosenPeer, TriggerReason::reply); + trigger(chosenPeer, TriggerReason::reply); } -Json::Value InboundLedger::getJson (int) +Json::Value +InboundLedger::getJson(int) { - Json::Value ret (Json::objectValue); + Json::Value ret(Json::objectValue); - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - ret[jss::hash] = to_string (mHash); + ret[jss::hash] = to_string(mHash); if (mComplete) ret[jss::complete] = true; @@ -1323,24 +1305,24 @@ Json::Value InboundLedger::getJson (int) ret[jss::have_transactions] = mHaveTransactions; } - ret[jss::timeouts] = getTimeouts (); + ret[jss::timeouts] = getTimeouts(); if (mHaveHeader && !mHaveState) { - Json::Value hv (Json::arrayValue); - for (auto const& h : neededStateHashes (16, nullptr)) + Json::Value hv(Json::arrayValue); + for (auto const& h : neededStateHashes(16, nullptr)) { - hv.append (to_string (h)); + hv.append(to_string(h)); } ret[jss::needed_state_hashes] = hv; } if (mHaveHeader && !mHaveTransactions) { - Json::Value hv (Json::arrayValue); - for (auto const& h : neededTxHashes (16, nullptr)) + Json::Value hv(Json::arrayValue); + for (auto const& h : neededTxHashes(16, nullptr)) { - hv.append (to_string (h)); + hv.append(to_string(h)); } ret[jss::needed_transaction_hashes] = hv; } @@ -1348,4 +1330,4 @@ Json::Value InboundLedger::getJson (int) return ret; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/InboundLedgers.cpp b/src/ripple/app/ledger/impl/InboundLedgers.cpp index cba1301afb..0ff028d8a3 100644 --- a/src/ripple/app/ledger/impl/InboundLedgers.cpp +++ b/src/ripple/app/ledger/impl/InboundLedgers.cpp @@ -23,19 +23,17 @@ #include #include #include +#include +#include #include #include #include -#include -#include #include #include namespace ripple { -class InboundLedgersImp - : public InboundLedgers - , public Stoppable +class InboundLedgersImp : public InboundLedgers, public Stoppable { private: Application& app_; @@ -45,32 +43,36 @@ private: beast::Journal const j_; public: - using u256_acq_pair = std::pair< - uint256, - std::shared_ptr >; + using u256_acq_pair = std::pair>; // How long before we try again to acquire the same ledger static const std::chrono::minutes kReacquireInterval; - InboundLedgersImp (Application& app, clock_type& clock, Stoppable& parent, - beast::insight::Collector::ptr const& collector) - : Stoppable ("InboundLedgers", parent) - , app_ (app) + InboundLedgersImp( + Application& app, + clock_type& clock, + Stoppable& parent, + beast::insight::Collector::ptr const& collector) + : Stoppable("InboundLedgers", parent) + , app_(app) , fetchRate_(clock.now()) - , j_ (app.journal ("InboundLedger")) - , m_clock (clock) - , mRecentFailures (clock) + , j_(app.journal("InboundLedger")) + , m_clock(clock) + , mRecentFailures(clock) , mCounter(collector->make_counter("ledger_fetches")) { } /** @callgraph */ std::shared_ptr - acquire(uint256 const& hash, std::uint32_t seq, + acquire( + uint256 const& hash, + std::uint32_t seq, InboundLedger::Reason reason) override { assert(hash.isNonZero()); - assert(reason != InboundLedger::Reason::SHARD || + assert( + reason != InboundLedger::Reason::SHARD || (seq != 0 && app_.getShardStore())); if (isStopping()) return {}; @@ -87,7 +89,7 @@ public: } else { - inbound = std::make_shared ( + inbound = std::make_shared( app_, hash, seq, reason, std::ref(m_clock)); mLedgers.emplace(hash, inbound); inbound->init(sl); @@ -98,10 +100,10 @@ public: if (inbound->isFailed()) return {}; - if (! isNew) + if (!isNew) inbound->update(seq); - if (! inbound->isComplete()) + if (!inbound->isComplete()) return {}; if (reason == InboundLedger::Reason::HISTORY) @@ -114,8 +116,8 @@ public: auto shardStore = app_.getShardStore(); if (!shardStore) { - JLOG(j_.error()) << - "Acquiring shard with no shard store available"; + JLOG(j_.error()) + << "Acquiring shard with no shard store available"; return {}; } if (inbound->getLedger()->stateMap().family().isShardBacked()) @@ -126,17 +128,18 @@ public: return inbound->getLedger(); } - std::shared_ptr find (uint256 const& hash) override + std::shared_ptr + find(uint256 const& hash) override { - assert (hash.isNonZero ()); + assert(hash.isNonZero()); std::shared_ptr ret; { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - auto it = mLedgers.find (hash); - if (it != mLedgers.end ()) + auto it = mLedgers.find(hash); + if (it != mLedgers.end()) { ret = it->second; } @@ -158,31 +161,32 @@ public: // VFALCO TODO Remove the dependency on the Peer object. /** We received a TMLedgerData from a peer. - */ - bool gotLedgerData (LedgerHash const& hash, - std::shared_ptr peer, - std::shared_ptr packet_ptr) override + */ + bool + gotLedgerData( + LedgerHash const& hash, + std::shared_ptr peer, + std::shared_ptr packet_ptr) override { protocol::TMLedgerData& packet = *packet_ptr; - JLOG (j_.trace()) - << "Got data (" << packet.nodes ().size () - << ") for acquiring ledger: " << hash; + JLOG(j_.trace()) << "Got data (" << packet.nodes().size() + << ") for acquiring ledger: " << hash; - auto ledger = find (hash); + auto ledger = find(hash); if (!ledger) { - JLOG (j_.trace()) - << "Got data for ledger we're no longer acquiring"; + JLOG(j_.trace()) << "Got data for ledger we're no longer acquiring"; // If it's state node data, stash it because it still might be // useful. - if (packet.type () == protocol::liAS_NODE) + if (packet.type() == protocol::liAS_NODE) { app_.getJobQueue().addJob( - jtLEDGER_DATA, "gotStaleData", - [this, packet_ptr] (Job&) { gotStaleData(packet_ptr); }); + jtLEDGER_DATA, "gotStaleData", [this, packet_ptr](Job&) { + gotStaleData(packet_ptr); + }); } return false; @@ -190,14 +194,16 @@ public: // Stash the data for later processing and see if we need to dispatch if (ledger->gotData(std::weak_ptr(peer), packet_ptr)) - app_.getJobQueue().addJob ( - jtLEDGER_DATA, "processLedgerData", - [this, hash] (Job&) { doLedgerData(hash); }); + app_.getJobQueue().addJob( + jtLEDGER_DATA, "processLedgerData", [this, hash](Job&) { + doLedgerData(hash); + }); return true; } - int getFetchCount (int& timeoutCount) override + int + getFetchCount(int& timeoutCount) override { timeoutCount = 0; int ret = 0; @@ -205,7 +211,7 @@ public: std::vector inboundLedgers; { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); inboundLedgers.reserve(mLedgers.size()); for (auto const& it : mLedgers) @@ -216,34 +222,37 @@ public: for (auto const& it : inboundLedgers) { - if (it.second->isActive ()) + if (it.second->isActive()) { ++ret; - timeoutCount += it.second->getTimeouts (); + timeoutCount += it.second->getTimeouts(); } } return ret; } - void logFailure (uint256 const& h, std::uint32_t seq) override + void + logFailure(uint256 const& h, std::uint32_t seq) override { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); mRecentFailures.emplace(h, seq); } - bool isFailure (uint256 const& h) override + bool + isFailure(uint256 const& h) override { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - beast::expire (mRecentFailures, kReacquireInterval); - return mRecentFailures.find (h) != mRecentFailures.end(); + beast::expire(mRecentFailures, kReacquireInterval); + return mRecentFailures.find(h) != mRecentFailures.end(); } - void doLedgerData (LedgerHash hash) override + void + doLedgerData(LedgerHash hash) override { - if (auto ledger = find (hash)) - ledger->runData (); + if (auto ledger = find(hash)) + ledger->runData(); } /** We got some data for a ledger we are no longer acquiring Since we paid @@ -252,23 +261,28 @@ public: Nodes are received in wire format and must be stashed/hashed in prefix format */ - void gotStaleData (std::shared_ptr packet_ptr) override + void + gotStaleData(std::shared_ptr packet_ptr) override { const uint256 uZero; Serializer s; try { - for (int i = 0; i < packet_ptr->nodes ().size (); ++i) + for (int i = 0; i < packet_ptr->nodes().size(); ++i) { - auto const& node = packet_ptr->nodes (i); + auto const& node = packet_ptr->nodes(i); - if (!node.has_nodeid () || !node.has_nodedata ()) + if (!node.has_nodeid() || !node.has_nodedata()) return; auto id_string = node.nodeid(); auto newNode = SHAMapAbstractNode::make( makeSlice(node.nodedata()), - 0, snfWIRE, SHAMapHash{uZero}, false, app_.journal ("SHAMapNodeID"), + 0, + snfWIRE, + SHAMapHash{uZero}, + false, + app_.journal("SHAMapNodeID"), SHAMapNodeID(id_string.data(), id_string.size())); if (!newNode) @@ -277,7 +291,7 @@ public: s.erase(); newNode->addRaw(s, snfPREFIX); - auto blob = std::make_shared (s.begin(), s.end()); + auto blob = std::make_shared(s.begin(), s.end()); app_.getLedgerMaster().addFetchPack( newNode->getNodeHash().as_uint256(), blob); @@ -288,50 +302,53 @@ public: } } - void clearFailures () override + void + clearFailures() override { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); mRecentFailures.clear(); mLedgers.clear(); } - std::size_t fetchRate() override + std::size_t + fetchRate() override { std::lock_guard lock(fetchRateMutex_); - return 60 * fetchRate_.value( - m_clock.now()); + return 60 * fetchRate_.value(m_clock.now()); } // Should only be called with an inboundledger that has // a reason of history or shard - void onLedgerFetched() override + void + onLedgerFetched() override { std::lock_guard lock(fetchRateMutex_); fetchRate_.add(1, m_clock.now()); } - Json::Value getInfo() override + Json::Value + getInfo() override { Json::Value ret(Json::objectValue); std::vector acquires; { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - acquires.reserve (mLedgers.size ()); + acquires.reserve(mLedgers.size()); for (auto const& it : mLedgers) { - assert (it.second); - acquires.push_back (it); + assert(it.second); + acquires.push_back(it); } for (auto const& it : mRecentFailures) { if (it.second > 1) - ret[beast::lexicalCastThrow ( - it.second)][jss::failed] = true; + ret[beast::lexicalCastThrow(it.second)] + [jss::failed] = true; else - ret[to_string (it.first)][jss::failed] = true; + ret[to_string(it.first)][jss::failed] = true; } } @@ -342,59 +359,62 @@ public: if (seq > 1) ret[std::to_string(seq)] = it.second->getJson(0); else - ret[to_string (it.first)] = it.second->getJson(0); + ret[to_string(it.first)] = it.second->getJson(0); } return ret; } - void gotFetchPack () override + void + gotFetchPack() override { std::vector> acquires; { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - acquires.reserve (mLedgers.size ()); + acquires.reserve(mLedgers.size()); for (auto const& it : mLedgers) { - assert (it.second); - acquires.push_back (it.second); + assert(it.second); + acquires.push_back(it.second); } } for (auto const& acquire : acquires) { - acquire->checkLocal (); + acquire->checkLocal(); } } - void sweep () override + void + sweep() override { - clock_type::time_point const now (m_clock.now()); + clock_type::time_point const now(m_clock.now()); // Make a list of things to sweep, while holding the lock - std::vector stuffToSweep; + std::vector stuffToSweep; std::size_t total; { - ScopedLockType sl (mLock); - MapType::iterator it (mLedgers.begin ()); - total = mLedgers.size (); - stuffToSweep.reserve (total); + ScopedLockType sl(mLock); + MapType::iterator it(mLedgers.begin()); + total = mLedgers.size(); + stuffToSweep.reserve(total); - while (it != mLedgers.end ()) + while (it != mLedgers.end()) { - if (it->second->getLastAction () > now) + if (it->second->getLastAction() > now) { - it->second->touch (); + it->second->touch(); ++it; } - else if ((it->second->getLastAction () + - std::chrono::minutes (1)) < now) + else if ( + (it->second->getLastAction() + std::chrono::minutes(1)) < + now) { - stuffToSweep.push_back (it->second); + stuffToSweep.push_back(it->second); // shouldn't cause the actual final delete // since we are holding a reference in the vector. - it = mLedgers.erase (it); + it = mLedgers.erase(it); } else { @@ -402,18 +422,17 @@ public: } } - beast::expire (mRecentFailures, kReacquireInterval); - + beast::expire(mRecentFailures, kReacquireInterval); } - JLOG (j_.debug()) << - "Swept " << stuffToSweep.size () << - " out of " << total << " inbound ledgers."; + JLOG(j_.debug()) << "Swept " << stuffToSweep.size() << " out of " + << total << " inbound ledgers."; } - void onStop () override + void + onStop() override { - ScopedLockType lock (mLock); + ScopedLockType lock(mLock); mLedgers.clear(); mRecentFailures.clear(); @@ -424,13 +443,13 @@ public: private: clock_type& m_clock; - using ScopedLockType = std::unique_lock ; + using ScopedLockType = std::unique_lock; std::recursive_mutex mLock; - using MapType = hash_map >; + using MapType = hash_map>; MapType mLedgers; - beast::aged_map mRecentFailures; + beast::aged_map mRecentFailures; beast::insight::Counter mCounter; }; @@ -438,16 +457,18 @@ private: //------------------------------------------------------------------------------ decltype(InboundLedgersImp::kReacquireInterval) -InboundLedgersImp::kReacquireInterval{5}; + InboundLedgersImp::kReacquireInterval{5}; InboundLedgers::~InboundLedgers() = default; std::unique_ptr -make_InboundLedgers (Application& app, - InboundLedgers::clock_type& clock, Stoppable& parent, +make_InboundLedgers( + Application& app, + InboundLedgers::clock_type& clock, + Stoppable& parent, beast::insight::Collector::ptr const& collector) { - return std::make_unique (app, clock, parent, collector); + return std::make_unique(app, clock, parent, collector); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/InboundTransactions.cpp b/src/ripple/app/ledger/impl/InboundTransactions.cpp index c7047c228d..5f7a1bc6a9 100644 --- a/src/ripple/app/ledger/impl/InboundTransactions.cpp +++ b/src/ripple/app/ledger/impl/InboundTransactions.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -31,8 +31,7 @@ namespace ripple { -enum -{ +enum { // Ideal number of peers to start with startPeers = 2, @@ -42,152 +41,155 @@ enum class InboundTransactionSet { -// A transaction set we generated, acquired, or are acquiring + // A transaction set we generated, acquired, or are acquiring public: - std::uint32_t mSeq; + std::uint32_t mSeq; TransactionAcquire::pointer mAcquire; - std::shared_ptr mSet; + std::shared_ptr mSet; - InboundTransactionSet ( - std::uint32_t seq, - std::shared_ptr const& set) : - mSeq (seq), mSet (set) - { ; } - InboundTransactionSet () : mSeq (0) - { ; } + InboundTransactionSet(std::uint32_t seq, std::shared_ptr const& set) + : mSeq(seq), mSet(set) + { + ; + } + InboundTransactionSet() : mSeq(0) + { + ; + } }; -class InboundTransactionsImp - : public InboundTransactions - , public Stoppable +class InboundTransactionsImp : public InboundTransactions, public Stoppable { public: Application& app_; - InboundTransactionsImp ( - Application& app, - clock_type& clock, - Stoppable& parent, - beast::insight::Collector::ptr const& collector, - std::function const&, - bool)> gotSet) - : Stoppable ("InboundTransactions", parent) - , app_ (app) - , m_clock (clock) - , m_seq (0) - , m_zeroSet (m_map[uint256()]) - , m_gotSet (std::move (gotSet)) + InboundTransactionsImp( + Application& app, + clock_type& clock, + Stoppable& parent, + beast::insight::Collector::ptr const& collector, + std::function const&, bool)> gotSet) + : Stoppable("InboundTransactions", parent) + , app_(app) + , m_clock(clock) + , m_seq(0) + , m_zeroSet(m_map[uint256()]) + , m_gotSet(std::move(gotSet)) { - m_zeroSet.mSet = std::make_shared ( - SHAMapType::TRANSACTION, uint256(), - app_.family()); + m_zeroSet.mSet = std::make_shared( + SHAMapType::TRANSACTION, uint256(), app_.family()); m_zeroSet.mSet->setUnbacked(); } - TransactionAcquire::pointer getAcquire (uint256 const& hash) + TransactionAcquire::pointer + getAcquire(uint256 const& hash) { { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - auto it = m_map.find (hash); + auto it = m_map.find(hash); - if (it != m_map.end ()) + if (it != m_map.end()) return it->second.mAcquire; } return {}; } - std::shared_ptr getSet ( - uint256 const& hash, - bool acquire) override + std::shared_ptr + getSet(uint256 const& hash, bool acquire) override { TransactionAcquire::pointer ta; { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - auto it = m_map.find (hash); + auto it = m_map.find(hash); - if (it != m_map.end ()) + if (it != m_map.end()) { if (acquire) { it->second.mSeq = m_seq; if (it->second.mAcquire) { - it->second.mAcquire->stillNeed (); + it->second.mAcquire->stillNeed(); } } return it->second.mSet; } - if (!acquire || isStopping ()) - return std::shared_ptr (); + if (!acquire || isStopping()) + return std::shared_ptr(); - ta = std::make_shared (app_, hash, m_clock); + ta = std::make_shared(app_, hash, m_clock); - auto &obj = m_map[hash]; + auto& obj = m_map[hash]; obj.mAcquire = ta; obj.mSeq = m_seq; } - - ta->init (startPeers); + ta->init(startPeers); return {}; } /** We received a TMLedgerData from a peer. - */ - void gotData (LedgerHash const& hash, - std::shared_ptr peer, - std::shared_ptr packet_ptr) override + */ + void + gotData( + LedgerHash const& hash, + std::shared_ptr peer, + std::shared_ptr packet_ptr) override { protocol::TMLedgerData& packet = *packet_ptr; - JLOG (app_.journal("InboundLedger").trace()) << - "Got data (" << packet.nodes ().size () << ") " - "for acquiring ledger: " << hash; + JLOG(app_.journal("InboundLedger").trace()) + << "Got data (" << packet.nodes().size() + << ") " + "for acquiring ledger: " + << hash; - TransactionAcquire::pointer ta = getAcquire (hash); + TransactionAcquire::pointer ta = getAcquire(hash); if (ta == nullptr) { - peer->charge (Resource::feeUnwantedData); + peer->charge(Resource::feeUnwantedData); return; } std::list nodeIDs; - std::list< Blob > nodeData; - for (auto const &node : packet.nodes()) + std::list nodeData; + for (auto const& node : packet.nodes()) { - if (!node.has_nodeid () || !node.has_nodedata () || ( - node.nodeid ().size () != 33)) + if (!node.has_nodeid() || !node.has_nodedata() || + (node.nodeid().size() != 33)) { - peer->charge (Resource::feeInvalidRequest); + peer->charge(Resource::feeInvalidRequest); return; } - nodeIDs.emplace_back (node.nodeid ().data (), - static_cast(node.nodeid ().size ())); - nodeData.emplace_back (node.nodedata ().begin (), - node.nodedata ().end ()); + nodeIDs.emplace_back( + node.nodeid().data(), static_cast(node.nodeid().size())); + nodeData.emplace_back( + node.nodedata().begin(), node.nodedata().end()); } - if (! ta->takeNodes (nodeIDs, nodeData, peer).isUseful ()) - peer->charge (Resource::feeUnwantedData); + if (!ta->takeNodes(nodeIDs, nodeData, peer).isUseful()) + peer->charge(Resource::feeUnwantedData); } - void giveSet (uint256 const& hash, - std::shared_ptr const& set, + void + giveSet( + uint256 const& hash, + std::shared_ptr const& set, bool fromAcquire) override { bool isNew = true; { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - auto& inboundSet = m_map [hash]; + auto& inboundSet = m_map[hash]; if (inboundSet.mSeq < m_seq) inboundSet.mSeq = m_seq; @@ -197,28 +199,28 @@ public: else inboundSet.mSet = set; - inboundSet.mAcquire.reset (); - + inboundSet.mAcquire.reset(); } if (isNew) - m_gotSet (set, fromAcquire); + m_gotSet(set, fromAcquire); } - Json::Value getInfo() override + Json::Value + getInfo() override { - Json::Value ret (Json::objectValue); + Json::Value ret(Json::objectValue); Json::Value& sets = (ret["sets"] = Json::arrayValue); { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); ret["seq"] = m_seq; for (auto const& it : m_map) { - Json::Value& set = sets [to_string (it.first)]; + Json::Value& set = sets[to_string(it.first)]; set["seq"] = it.second.mSeq; if (it.second.mSet) set["state"] = "complete"; @@ -227,45 +229,45 @@ public: else set["state"] = "dead"; } - } return ret; } - void newRound (std::uint32_t seq) override + void + newRound(std::uint32_t seq) override { - std::lock_guard lock (mLock); + std::lock_guard lock(mLock); // Protect zero set from expiration m_zeroSet.mSeq = seq; if (m_seq != seq) { - m_seq = seq; - auto it = m_map.begin (); + auto it = m_map.begin(); std::uint32_t const minSeq = (seq < setKeepRounds) ? 0 : (seq - setKeepRounds); std::uint32_t maxSeq = seq + setKeepRounds; - while (it != m_map.end ()) + while (it != m_map.end()) { if (it->second.mSeq < minSeq || it->second.mSeq > maxSeq) - it = m_map.erase (it); + it = m_map.erase(it); else ++it; } } } - void onStop () override + void + onStop() override { - std::lock_guard lock (mLock); + std::lock_guard lock(mLock); - m_map.clear (); + m_map.clear(); stopped(); } @@ -273,7 +275,7 @@ public: private: clock_type& m_clock; - using MapType = hash_map ; + using MapType = hash_map; std::recursive_mutex mLock; @@ -283,24 +285,23 @@ private: // The empty transaction set whose hash is zero InboundTransactionSet& m_zeroSet; - std::function const&, bool)> m_gotSet; + std::function const&, bool)> m_gotSet; }; //------------------------------------------------------------------------------ InboundTransactions::~InboundTransactions() = default; -std::unique_ptr -make_InboundTransactions ( +std::unique_ptr +make_InboundTransactions( Application& app, InboundLedgers::clock_type& clock, Stoppable& parent, beast::insight::Collector::ptr const& collector, - std::function const&, - bool)> gotSet) + std::function const&, bool)> gotSet) { - return std::make_unique - (app, clock, parent, collector, std::move (gotSet)); + return std::make_unique( + app, clock, parent, collector, std::move(gotSet)); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/LedgerCleaner.cpp b/src/ripple/app/ledger/impl/LedgerCleaner.cpp index 382aca3778..fad97590e4 100644 --- a/src/ripple/app/ledger/impl/LedgerCleaner.cpp +++ b/src/ripple/app/ledger/impl/LedgerCleaner.cpp @@ -17,12 +17,12 @@ */ //============================================================================== -#include #include +#include #include #include -#include #include +#include namespace ripple { namespace detail { @@ -51,19 +51,15 @@ class LedgerCleanerImp : public LedgerCleaner std::thread thread_; - enum class State : char { - readyToClean = 0, - startCleaning, - cleaning - }; + enum class State : char { readyToClean = 0, startCleaning, cleaning }; State state_ = State::readyToClean; bool shouldExit_ = false; // The lowest ledger in the range we're checking. - LedgerIndex minRange_ = 0; + LedgerIndex minRange_ = 0; // The highest ledger in the range we're checking - LedgerIndex maxRange_ = 0; + LedgerIndex maxRange_ = 0; // Check all state/transaction nodes bool checkNodes_ = false; @@ -76,20 +72,18 @@ class LedgerCleanerImp : public LedgerCleaner //-------------------------------------------------------------------------- public: - LedgerCleanerImp ( + LedgerCleanerImp( Application& app, Stoppable& stoppable, beast::Journal journal) - : LedgerCleaner (stoppable) - , app_ (app) - , j_ (journal) + : LedgerCleaner(stoppable), app_(app), j_(journal) { } - ~LedgerCleanerImp () override + ~LedgerCleanerImp() override { if (thread_.joinable()) - LogicError ("LedgerCleanerImp::onStop not called."); + LogicError("LedgerCleanerImp::onStop not called."); } //-------------------------------------------------------------------------- @@ -98,20 +92,23 @@ public: // //-------------------------------------------------------------------------- - void onPrepare () override + void + onPrepare() override { } - void onStart () override + void + onStart() override { - thread_ = std::thread {&LedgerCleanerImp::run, this}; + thread_ = std::thread{&LedgerCleanerImp::run, this}; } - void onStop () override + void + onStop() override { - JLOG (j_.info()) << "Stopping"; + JLOG(j_.info()) << "Stopping"; { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); shouldExit_ = true; wakeup_.notify_one(); } @@ -124,9 +121,10 @@ public: // //-------------------------------------------------------------------------- - void onWrite (beast::PropertyStream::Map& map) override + void + onWrite(beast::PropertyStream::Map& map) override { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); if (maxRange_ == 0) map["status"] = "idle"; @@ -148,14 +146,15 @@ public: // //-------------------------------------------------------------------------- - void doClean (Json::Value const& params) override + void + doClean(Json::Value const& params) override { LedgerIndex minRange = 0; LedgerIndex maxRange = 0; - app_.getLedgerMaster().getFullValidatedRange (minRange, maxRange); + app_.getLedgerMaster().getFullValidatedRange(minRange, maxRange); { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); maxRange_ = maxRange; minRange_ = minRange; @@ -203,7 +202,7 @@ public: } if (params.isMember(jss::max_ledger)) - maxRange_ = params[jss::max_ledger].asUInt(); + maxRange_ = params[jss::max_ledger].asUInt(); if (params.isMember(jss::min_ledger)) minRange_ = params[jss::min_ledger].asUInt(); @@ -234,28 +233,27 @@ public: // //-------------------------------------------------------------------------- private: - void init () + void + init() { - JLOG (j_.debug()) << "Initializing"; + JLOG(j_.debug()) << "Initializing"; } - void run () + void + run() { - beast::setCurrentThreadName ("LedgerCleaner"); - JLOG (j_.debug()) << "Started"; + beast::setCurrentThreadName("LedgerCleaner"); + JLOG(j_.debug()) << "Started"; init(); while (true) { { - std::unique_lock lock (mutex_); - wakeup_.wait(lock, [this]() - { - return ( - shouldExit_ || - state_ == State::startCleaning); - }); + std::unique_lock lock(mutex_); + wakeup_.wait(lock, [this]() { + return (shouldExit_ || state_ == State::startCleaning); + }); if (shouldExit_) break; @@ -268,9 +266,8 @@ private: } // VFALCO TODO This should return boost::optional - LedgerHash getLedgerHash( - std::shared_ptr& ledger, - LedgerIndex index) + LedgerHash + getLedgerHash(std::shared_ptr& ledger, LedgerIndex index) { boost::optional hash; try @@ -279,13 +276,14 @@ private: } catch (SHAMapMissingNode const& mn) { - JLOG (j_.warn()) << - "Ledger #" << ledger->info().seq << ": " << mn.what(); - app_.getInboundLedgers().acquire ( - ledger->info().hash, ledger->info().seq, + JLOG(j_.warn()) + << "Ledger #" << ledger->info().seq << ": " << mn.what(); + app_.getInboundLedgers().acquire( + ledger->info().hash, + ledger->info().seq, InboundLedger::Reason::GENERIC); } - return hash ? *hash : beast::zero; // kludge + return hash ? *hash : beast::zero; // kludge } /** Process a single ledger @@ -295,45 +293,45 @@ private: @param doTxns Reprocess (account) transactions to SQL databases. @return `true` if the ledger was cleaned. */ - bool doLedger( + bool + doLedger( LedgerIndex const& ledgerIndex, LedgerHash const& ledgerHash, bool doNodes, bool doTxns) { - auto nodeLedger = app_.getInboundLedgers().acquire ( + auto nodeLedger = app_.getInboundLedgers().acquire( ledgerHash, ledgerIndex, InboundLedger::Reason::GENERIC); if (!nodeLedger) { - JLOG (j_.debug()) << "Ledger " << ledgerIndex << " not available"; - app_.getLedgerMaster().clearLedger (ledgerIndex); + JLOG(j_.debug()) << "Ledger " << ledgerIndex << " not available"; + app_.getLedgerMaster().clearLedger(ledgerIndex); app_.getInboundLedgers().acquire( ledgerHash, ledgerIndex, InboundLedger::Reason::GENERIC); return false; } auto dbLedger = loadByIndex(ledgerIndex, app_); - if (! dbLedger || - (dbLedger->info().hash != ledgerHash) || + if (!dbLedger || (dbLedger->info().hash != ledgerHash) || (dbLedger->info().parentHash != nodeLedger->info().parentHash)) { // Ideally we'd also check for more than one ledger with that index - JLOG (j_.debug()) << - "Ledger " << ledgerIndex << " mismatches SQL DB"; + JLOG(j_.debug()) + << "Ledger " << ledgerIndex << " mismatches SQL DB"; doTxns = true; } - if(! app_.getLedgerMaster().fixIndex(ledgerIndex, ledgerHash)) + if (!app_.getLedgerMaster().fixIndex(ledgerIndex, ledgerHash)) { - JLOG (j_.debug()) << "ledger " << ledgerIndex - << " had wrong entry in history"; + JLOG(j_.debug()) + << "ledger " << ledgerIndex << " had wrong entry in history"; doTxns = true; } - if (doNodes && !nodeLedger->walkLedger(app_.journal ("Ledger"))) + if (doNodes && !nodeLedger->walkLedger(app_.journal("Ledger"))) { - JLOG (j_.debug()) << "Ledger " << ledgerIndex << " is missing nodes"; - app_.getLedgerMaster().clearLedger (ledgerIndex); + JLOG(j_.debug()) << "Ledger " << ledgerIndex << " is missing nodes"; + app_.getLedgerMaster().clearLedger(ledgerIndex); app_.getInboundLedgers().acquire( ledgerHash, ledgerIndex, InboundLedger::Reason::GENERIC); return false; @@ -341,7 +339,7 @@ private: if (doTxns && !pendSaveValidated(app_, nodeLedger, true, false)) { - JLOG (j_.debug()) << "Failed to save ledger " << ledgerIndex; + JLOG(j_.debug()) << "Failed to save ledger " << ledgerIndex; return false; } @@ -353,7 +351,8 @@ private: @param referenceLedger [out] An optional known good subsequent ledger. @return The hash of the ledger. This will be all-bits-zero if not found. */ - LedgerHash getHash( + LedgerHash + getHash( LedgerIndex const& ledgerIndex, std::shared_ptr& referenceLedger) { @@ -364,8 +363,8 @@ private: referenceLedger = app_.getLedgerMaster().getValidatedLedger(); if (!referenceLedger) { - JLOG (j_.warn()) << "No validated ledger"; - return ledgerHash; // Nothing we can do. No validated ledger. + JLOG(j_.warn()) << "No validated ledger"; + return ledgerHash; // Nothing we can do. No validated ledger. } } @@ -378,42 +377,41 @@ private: // No. Try to get another ledger that might have the hash we // need: compute the index and hash of a ledger that will have // the hash we need. - LedgerIndex refIndex = getCandidateLedger (ledgerIndex); - LedgerHash refHash = getLedgerHash (referenceLedger, refIndex); + LedgerIndex refIndex = getCandidateLedger(ledgerIndex); + LedgerHash refHash = getLedgerHash(referenceLedger, refIndex); - bool const nonzero (refHash.isNonZero ()); - assert (nonzero); + bool const nonzero(refHash.isNonZero()); + assert(nonzero); if (nonzero) { // We found the hash and sequence of a better reference // ledger. - referenceLedger = - app_.getInboundLedgers().acquire( - refHash, refIndex, InboundLedger::Reason::GENERIC); + referenceLedger = app_.getInboundLedgers().acquire( + refHash, refIndex, InboundLedger::Reason::GENERIC); if (referenceLedger) - ledgerHash = getLedgerHash( - referenceLedger, ledgerIndex); + ledgerHash = + getLedgerHash(referenceLedger, ledgerIndex); } } } else - JLOG (j_.warn()) << "Validated ledger is prior to target ledger"; + JLOG(j_.warn()) << "Validated ledger is prior to target ledger"; return ledgerHash; } /** Run the ledger cleaner. */ - void doLedgerCleaner() + void + doLedgerCleaner() { - auto shouldExit = [this]() - { + auto shouldExit = [this]() { std::lock_guard lock(mutex_); return shouldExit_; }; std::shared_ptr goodLedger; - while (! shouldExit()) + while (!shouldExit()) { LedgerIndex ledgerIndex; LedgerHash ledgerHash; @@ -422,16 +420,16 @@ private: while (app_.getFeeTrack().isLoadedLocal()) { - JLOG (j_.debug()) << "Waiting for load to subside"; + JLOG(j_.debug()) << "Waiting for load to subside"; std::this_thread::sleep_for(std::chrono::seconds(5)); if (shouldExit()) return; } { - std::lock_guard lock (mutex_); - if ((minRange_ > maxRange_) || - (maxRange_ == 0) || (minRange_ == 0)) + std::lock_guard lock(mutex_); + if ((minRange_ > maxRange_) || (maxRange_ == 0) || + (minRange_ == 0)) { minRange_ = maxRange_ = 0; state_ = State::readyToClean; @@ -447,20 +445,20 @@ private: bool fail = false; if (ledgerHash.isZero()) { - JLOG (j_.info()) << "Unable to get hash for ledger " - << ledgerIndex; + JLOG(j_.info()) + << "Unable to get hash for ledger " << ledgerIndex; fail = true; } else if (!doLedger(ledgerIndex, ledgerHash, doNodes, doTxns)) { - JLOG (j_.info()) << "Failed to process ledger " << ledgerIndex; + JLOG(j_.info()) << "Failed to process ledger " << ledgerIndex; fail = true; } if (fail) { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); ++failures_; } // Wait for acquiring to catch up to us @@ -469,7 +467,7 @@ private: else { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); if (ledgerIndex == minRange_) ++minRange_; if (ledgerIndex == maxRange_) @@ -479,27 +477,25 @@ private: // Reduce I/O pressure and wait for acquiring to catch up to us std::this_thread::sleep_for(std::chrono::milliseconds(100)); } - } } }; //------------------------------------------------------------------------------ -LedgerCleaner::LedgerCleaner (Stoppable& parent) - : Stoppable ("LedgerCleaner", parent) - , beast::PropertyStream::Source ("ledgercleaner") +LedgerCleaner::LedgerCleaner(Stoppable& parent) + : Stoppable("LedgerCleaner", parent) + , beast::PropertyStream::Source("ledgercleaner") { } LedgerCleaner::~LedgerCleaner() = default; std::unique_ptr -make_LedgerCleaner (Application& app, - Stoppable& parent, beast::Journal journal) +make_LedgerCleaner(Application& app, Stoppable& parent, beast::Journal journal) { return std::make_unique(app, parent, journal); } -} // detail -} // ripple +} // namespace detail +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/LedgerMaster.cpp b/src/ripple/app/ledger/impl/LedgerMaster.cpp index 8762ed0844..3cee63b104 100644 --- a/src/ripple/app/ledger/impl/LedgerMaster.cpp +++ b/src/ripple/app/ledger/impl/LedgerMaster.cpp @@ -17,11 +17,11 @@ */ //============================================================================== +#include #include #include #include #include -#include #include #include #include @@ -30,21 +30,21 @@ #include #include #include -#include #include #include -#include -#include +#include #include #include #include +#include +#include #include #include #include #include #include -#include #include +#include #include #include #include @@ -60,8 +60,8 @@ namespace { /** Automatically unlocks and re-locks a unique_lock object. - This is the reverse of a std::unique_lock object - instead of locking the mutex - for the lifetime of this object, it unlocks it. + This is the reverse of a std::unique_lock object - instead of locking the + mutex for the lifetime of this object, it unlocks it. Make sure you don't try to unlock mutexes that aren't actually locked! @@ -97,6 +97,7 @@ template class ScopedUnlock { std::unique_lock& lock_; + public: /** Creates a ScopedUnlock. @@ -108,15 +109,15 @@ public: otherwise there are no guarantees what will happen! Best just to use it as a local stack object, rather than creating on the heap. */ - explicit ScopedUnlock (std::unique_lock& lock) - : lock_ (lock) + explicit ScopedUnlock(std::unique_lock& lock) : lock_(lock) { assert(lock_.owns_lock()); lock_.unlock(); } - ScopedUnlock (ScopedUnlock const&) = delete; - ScopedUnlock& operator= (ScopedUnlock const&) = delete; + ScopedUnlock(ScopedUnlock const&) = delete; + ScopedUnlock& + operator=(ScopedUnlock const&) = delete; /** Destructor. @@ -131,16 +132,16 @@ public: } }; -} +} // namespace // Don't catch up more than 100 ledgers (cannot exceed 256) -static constexpr int MAX_LEDGER_GAP {100}; +static constexpr int MAX_LEDGER_GAP{100}; // Don't acquire history if ledger is too old -static constexpr std::chrono::minutes MAX_LEDGER_AGE_ACQUIRE {1}; +static constexpr std::chrono::minutes MAX_LEDGER_AGE_ACQUIRE{1}; // Don't acquire history if write load is too high -static constexpr int MAX_WRITE_LOAD_ACQUIRE {8192}; +static constexpr int MAX_WRITE_LOAD_ACQUIRE{8192}; // Helper function for LedgerMaster::doAdvance() // Returns the minimum ledger sequence in SQL database, if any. @@ -156,7 +157,7 @@ minSqlSeq(Application& app) // Helper function for LedgerMaster::doAdvance() // Return true if candidateLedger should be fetched from the network. static bool -shouldAcquire ( +shouldAcquire( std::uint32_t const currentLedger, std::uint32_t const ledgerHistory, boost::optional minSeq, @@ -164,8 +165,7 @@ shouldAcquire ( std::uint32_t const candidateLedger, beast::Journal j) { - bool ret = [&]() - { + bool ret = [&]() { // Fetch ledger if it may be the current ledger if (candidateLedger >= currentLedger) return true; @@ -179,72 +179,79 @@ shouldAcquire ( // sequence value, if no persisted ledger, and // - minimum ledger that will be persisted as of the next online // deletion interval, or 1 if online deletion is disabled. - return - candidateLedger >= std::max( - minSeq.value_or(std::numeric_limits::max()), - lastRotated + 1); + return candidateLedger >= + std::max( + minSeq.value_or(std::numeric_limits::max()), + lastRotated + 1); }(); - JLOG (j.trace()) - << "Missing ledger " - << candidateLedger - << (ret ? " should" : " should NOT") - << " be acquired"; + JLOG(j.trace()) << "Missing ledger " << candidateLedger + << (ret ? " should" : " should NOT") << " be acquired"; return ret; } -LedgerMaster::LedgerMaster (Application& app, Stopwatch& stopwatch, +LedgerMaster::LedgerMaster( + Application& app, + Stopwatch& stopwatch, Stoppable& parent, - beast::insight::Collector::ptr const& collector, beast::Journal journal) - : Stoppable ("LedgerMaster", parent) - , app_ (app) - , m_journal (journal) - , mLedgerHistory (collector, app) - , mLedgerCleaner (detail::make_LedgerCleaner ( - app, *this, app_.journal("LedgerCleaner"))) - , standalone_ (app_.config().standalone()) - , fetch_depth_ (app_.getSHAMapStore ().clampFetchDepth ( - app_.config().FETCH_DEPTH)) - , ledger_history_ (app_.config().LEDGER_HISTORY) - , ledger_fetch_size_ (app_.config().getValueFor(SizedItem::ledgerFetch)) - , fetch_packs_ ("FetchPack", 65536, std::chrono::seconds {45}, stopwatch, - app_.journal("TaggedCache")) - , m_stats(std::bind (&LedgerMaster::collect_metrics, this),collector) + beast::insight::Collector::ptr const& collector, + beast::Journal journal) + : Stoppable("LedgerMaster", parent) + , app_(app) + , m_journal(journal) + , mLedgerHistory(collector, app) + , mLedgerCleaner( + detail::make_LedgerCleaner(app, *this, app_.journal("LedgerCleaner"))) + , standalone_(app_.config().standalone()) + , fetch_depth_( + app_.getSHAMapStore().clampFetchDepth(app_.config().FETCH_DEPTH)) + , ledger_history_(app_.config().LEDGER_HISTORY) + , ledger_fetch_size_(app_.config().getValueFor(SizedItem::ledgerFetch)) + , fetch_packs_( + "FetchPack", + 65536, + std::chrono::seconds{45}, + stopwatch, + app_.journal("TaggedCache")) + , m_stats(std::bind(&LedgerMaster::collect_metrics, this), collector) { } LedgerIndex -LedgerMaster::getCurrentLedgerIndex () +LedgerMaster::getCurrentLedgerIndex() { return app_.openLedger().current()->info().seq; } LedgerIndex -LedgerMaster::getValidLedgerIndex () +LedgerMaster::getValidLedgerIndex() { return mValidLedgerSeq; } bool -LedgerMaster::isCompatible ( +LedgerMaster::isCompatible( ReadView const& view, beast::Journal::Stream s, char const* reason) { auto validLedger = getValidatedLedger(); - if (validLedger && - ! areCompatible (*validLedger, view, s, reason)) + if (validLedger && !areCompatible(*validLedger, view, s, reason)) { return false; } { - std::lock_guard sl (m_mutex); + std::lock_guard sl(m_mutex); if ((mLastValidLedger.second != 0) && - ! areCompatible (mLastValidLedger.first, - mLastValidLedger.second, view, s, reason)) + !areCompatible( + mLastValidLedger.first, + mLastValidLedger.second, + view, + s, + reason)) { return false; } @@ -260,7 +267,7 @@ LedgerMaster::getPublishedLedgerAge() std::chrono::seconds pubClose{mPubLedgerClose.load()}; if (pubClose == 0s) { - JLOG (m_journal.debug()) << "No published ledger"; + JLOG(m_journal.debug()) << "No published ledger"; return weeks{2}; } @@ -268,7 +275,7 @@ LedgerMaster::getPublishedLedgerAge() ret -= pubClose; ret = (ret > 0s) ? ret : 0s; - JLOG (m_journal.trace()) << "Published ledger age is " << ret.count(); + JLOG(m_journal.trace()) << "Published ledger age is " << ret.count(); return ret; } @@ -279,7 +286,7 @@ LedgerMaster::getValidatedLedgerAge() std::chrono::seconds valClose{mValidLedgerSign.load()}; if (valClose == 0s) { - JLOG (m_journal.debug()) << "No validated ledger"; + JLOG(m_journal.debug()) << "No validated ledger"; return weeks{2}; } @@ -287,7 +294,7 @@ LedgerMaster::getValidatedLedgerAge() ret -= valClose; ret = (ret > 0s) ? ret : 0s; - JLOG (m_journal.trace()) << "Validated ledger age is " << ret.count(); + JLOG(m_journal.trace()) << "Validated ledger age is " << ret.count(); return ret; } @@ -307,7 +314,7 @@ LedgerMaster::isCaughtUp(std::string& reason) reason = "No published ledger"; return false; } - if (validClose > (pubClose + 90)) + if (validClose > (pubClose + 90)) { reason = "Published ledger lags validated ledger"; return false; @@ -316,58 +323,56 @@ LedgerMaster::isCaughtUp(std::string& reason) } void -LedgerMaster::setValidLedger( - std::shared_ptr const& l) +LedgerMaster::setValidLedger(std::shared_ptr const& l) { - - std::vector times; + std::vector times; boost::optional consensusHash; - if (! standalone_) + if (!standalone_) { - auto const vals = app_.getValidations().getTrustedForLedger(l->info().hash); + auto const vals = + app_.getValidations().getTrustedForLedger(l->info().hash); times.reserve(vals.size()); - for(auto const& val: vals) + for (auto const& val : vals) times.push_back(val->getSignTime()); - if(!vals.empty()) + if (!vals.empty()) consensusHash = vals.front()->getConsensusHash(); } NetClock::time_point signTime; - if (! times.empty () && times.size() >= app_.validators ().quorum ()) + if (!times.empty() && times.size() >= app_.validators().quorum()) { // Calculate the sample median - std::sort (times.begin (), times.end ()); + std::sort(times.begin(), times.end()); auto const t0 = times[(times.size() - 1) / 2]; auto const t1 = times[times.size() / 2]; - signTime = t0 + (t1 - t0)/2; + signTime = t0 + (t1 - t0) / 2; } else { signTime = l->info().closeTime; } - mValidLedger.set (l); + mValidLedger.set(l); mValidLedgerSign = signTime.time_since_epoch().count(); - assert (mValidLedgerSeq || - !app_.getMaxDisallowedLedger() || - l->info().seq + max_ledger_difference_ > - app_.getMaxDisallowedLedger()); - (void) max_ledger_difference_; + assert( + mValidLedgerSeq || !app_.getMaxDisallowedLedger() || + l->info().seq + max_ledger_difference_ > app_.getMaxDisallowedLedger()); + (void)max_ledger_difference_; mValidLedgerSeq = l->info().seq; - app_.getOPs().updateLocalTx (*l); - app_.getSHAMapStore().onLedgerClosed (getValidatedLedger()); - mLedgerHistory.validatedLedger (l, consensusHash); - app_.getAmendmentTable().doValidatedLedger (l); + app_.getOPs().updateLocalTx(*l); + app_.getSHAMapStore().onLedgerClosed(getValidatedLedger()); + mLedgerHistory.validatedLedger(l, consensusHash); + app_.getAmendmentTable().doValidatedLedger(l); if (!app_.getOPs().isAmendmentBlocked()) { if (app_.getAmendmentTable().hasUnsupportedEnabled()) { JLOG(m_journal.error()) << "One or more unsupported amendments " - "activated: server blocked."; + "activated: server blocked."; app_.getOPs().setAmendmentBlocked(); } else if (!app_.getOPs().isAmendmentWarned() || ((l->seq() % 256) == 0)) @@ -398,8 +403,7 @@ LedgerMaster::setValidLedger( } void -LedgerMaster::setPubLedger( - std::shared_ptr const& l) +LedgerMaster::setPubLedger(std::shared_ptr const& l) { mPubLedger = l; mPubLedgerClose = l->info().closeTime.time_since_epoch().count(); @@ -407,30 +411,30 @@ LedgerMaster::setPubLedger( } void -LedgerMaster::addHeldTransaction ( +LedgerMaster::addHeldTransaction( std::shared_ptr const& transaction) { - std::lock_guard ml (m_mutex); - mHeldTransactions.insert (transaction->getSTransaction ()); + std::lock_guard ml(m_mutex); + mHeldTransactions.insert(transaction->getSTransaction()); } // Validate a ledger's close time and sequence number if we're considering // jumping to that ledger. This helps defend agains some rare hostile or // insane majority scenarios. bool -LedgerMaster::canBeCurrent (std::shared_ptr const& ledger) +LedgerMaster::canBeCurrent(std::shared_ptr const& ledger) { - assert (ledger); + assert(ledger); // Never jump to a candidate ledger that precedes our // last validated ledger auto validLedger = getValidatedLedger(); - if (validLedger && - (ledger->info().seq < validLedger->info().seq)) + if (validLedger && (ledger->info().seq < validLedger->info().seq)) { - JLOG (m_journal.trace()) << "Candidate for current ledger has low seq " - << ledger->info().seq << " < " << validLedger->info().seq; + JLOG(m_journal.trace()) + << "Candidate for current ledger has low seq " << ledger->info().seq + << " < " << validLedger->info().seq; return false; } @@ -444,10 +448,11 @@ LedgerMaster::canBeCurrent (std::shared_ptr const& ledger) using namespace std::chrono_literals; if ((validLedger || (ledger->info().seq > 10)) && - ((std::max (closeTime, ledgerClose) - std::min (closeTime, ledgerClose)) - > 5min)) + ((std::max(closeTime, ledgerClose) - std::min(closeTime, ledgerClose)) > + 5min)) { - JLOG (m_journal.warn()) << "Candidate for current ledger has close time " + JLOG(m_journal.warn()) + << "Candidate for current ledger has close time " << to_string(ledgerClose) << " at network time " << to_string(closeTime) << " seq " << ledger->info().seq; return false; @@ -464,18 +469,21 @@ LedgerMaster::canBeCurrent (std::shared_ptr const& ledger) if (closeTime > validLedger->info().parentCloseTime) maxSeq += std::chrono::duration_cast( - closeTime - validLedger->info().parentCloseTime).count() / 2; + closeTime - validLedger->info().parentCloseTime) + .count() / + 2; if (ledger->info().seq > maxSeq) { - JLOG (m_journal.warn()) << "Candidate for current ledger has high seq " + JLOG(m_journal.warn()) + << "Candidate for current ledger has high seq " << ledger->info().seq << " > " << maxSeq; return false; } - JLOG (m_journal.trace()) << "Acceptable seq range: " << - validLedger->info().seq << " <= " << - ledger->info().seq << " <= " << maxSeq; + JLOG(m_journal.trace()) + << "Acceptable seq range: " << validLedger->info().seq + << " <= " << ledger->info().seq << " <= " << maxSeq; } return true; @@ -484,37 +492,37 @@ LedgerMaster::canBeCurrent (std::shared_ptr const& ledger) void LedgerMaster::switchLCL(std::shared_ptr const& lastClosed) { - assert (lastClosed); - if(! lastClosed->isImmutable()) + assert(lastClosed); + if (!lastClosed->isImmutable()) LogicError("mutable ledger in switchLCL"); if (lastClosed->open()) - LogicError ("The new last closed ledger is open!"); + LogicError("The new last closed ledger is open!"); { - std::lock_guard ml (m_mutex); - mClosedLedger.set (lastClosed); + std::lock_guard ml(m_mutex); + mClosedLedger.set(lastClosed); } if (standalone_) { - setFullLedger (lastClosed, true, false); + setFullLedger(lastClosed, true, false); tryAdvance(); } else { - checkAccept (lastClosed); + checkAccept(lastClosed); } } bool -LedgerMaster::fixIndex (LedgerIndex ledgerIndex, LedgerHash const& ledgerHash) +LedgerMaster::fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash) { - return mLedgerHistory.fixIndex (ledgerIndex, ledgerHash); + return mLedgerHistory.fixIndex(ledgerIndex, ledgerHash); } bool -LedgerMaster::storeLedger (std::shared_ptr ledger) +LedgerMaster::storeLedger(std::shared_ptr ledger) { // Returns true if we already had the ledger return mLedgerHistory.insert(std::move(ledger), false); @@ -526,35 +534,33 @@ LedgerMaster::storeLedger (std::shared_ptr ledger) until a new open ledger is built. */ void -LedgerMaster::applyHeldTransactions () +LedgerMaster::applyHeldTransactions() { - std::lock_guard sl (m_mutex); + std::lock_guard sl(m_mutex); - app_.openLedger().modify( - [&](OpenView& view, beast::Journal j) + app_.openLedger().modify([&](OpenView& view, beast::Journal j) { + bool any = false; + for (auto const& it : mHeldTransactions) { - bool any = false; - for (auto const& it : mHeldTransactions) - { - ApplyFlags flags = tapNONE; - auto const result = app_.getTxQ().apply( - app_, view, it.second, flags, j); - if (result.second) - any = true; - } - return any; - }); + ApplyFlags flags = tapNONE; + auto const result = + app_.getTxQ().apply(app_, view, it.second, flags, j); + if (result.second) + any = true; + } + return any; + }); // VFALCO TODO recreate the CanonicalTxSet object instead of resetting // it. // VFALCO NOTE The hash for an open ledger is undefined so we use // something that is a reasonable substitute. - mHeldTransactions.reset ( - app_.openLedger().current()->info().parentHash); + mHeldTransactions.reset(app_.openLedger().current()->info().parentHash); } std::vector> -LedgerMaster::pruneHeldTransactions(AccountID const& account, +LedgerMaster::pruneHeldTransactions( + AccountID const& account, std::uint32_t const seq) { std::lock_guard sl(m_mutex); @@ -563,35 +569,37 @@ LedgerMaster::pruneHeldTransactions(AccountID const& account, } LedgerIndex -LedgerMaster::getBuildingLedger () +LedgerMaster::getBuildingLedger() { // The ledger we are currently building, 0 of none - return mBuildingLedgerSeq.load (); + return mBuildingLedgerSeq.load(); } void -LedgerMaster::setBuildingLedger (LedgerIndex i) +LedgerMaster::setBuildingLedger(LedgerIndex i) { - mBuildingLedgerSeq.store (i); + mBuildingLedgerSeq.store(i); } bool -LedgerMaster::haveLedger (std::uint32_t seq) +LedgerMaster::haveLedger(std::uint32_t seq) { - std::lock_guard sl (mCompleteLock); + std::lock_guard sl(mCompleteLock); return boost::icl::contains(mCompleteLedgers, seq); } void -LedgerMaster::clearLedger (std::uint32_t seq) +LedgerMaster::clearLedger(std::uint32_t seq) { - std::lock_guard sl (mCompleteLock); - mCompleteLedgers.erase (seq); + std::lock_guard sl(mCompleteLock); + mCompleteLedgers.erase(seq); } // returns Ledgers we have all the nodes for bool -LedgerMaster::getFullValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVal) +LedgerMaster::getFullValidatedRange( + std::uint32_t& minVal, + std::uint32_t& maxVal) { // Validated ledger is likely not stored in the DB yet so we use the // published ledger which is. @@ -602,7 +610,7 @@ LedgerMaster::getFullValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVa boost::optional maybeMin; { - std::lock_guard sl (mCompleteLock); + std::lock_guard sl(mCompleteLock); maybeMin = prevMissing(mCompleteLedgers, maxVal); } @@ -616,7 +624,7 @@ LedgerMaster::getFullValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVa // Returns Ledgers we have all the nodes for and are indexed bool -LedgerMaster::getValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVal) +LedgerMaster::getValidatedRange(std::uint32_t& minVal, std::uint32_t& maxVal) { if (!getFullValidatedRange(minVal, maxVal)) return false; @@ -624,8 +632,7 @@ LedgerMaster::getValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVal) // Remove from the validated range any ledger sequences that may not be // fully updated in the database yet - auto const pendingSaves = - app_.pendingSaves().getSnapshot(); + auto const pendingSaves = app_.pendingSaves().getSnapshot(); if (!pendingSaves.empty() && ((minVal != 0) || (maxVal != 0))) { @@ -638,7 +645,7 @@ LedgerMaster::getValidatedRange (std::uint32_t& minVal, std::uint32_t& maxVal) ++minVal; // Best effort for remaining exclusions - for(auto v : pendingSaves) + for (auto v : pendingSaves) { if ((v.first >= minVal) && (v.first <= maxVal)) { @@ -672,35 +679,33 @@ LedgerMaster::getEarliestFetch() } void -LedgerMaster::tryFill ( - Job& job, - std::shared_ptr ledger) +LedgerMaster::tryFill(Job& job, std::shared_ptr ledger) { std::uint32_t seq = ledger->info().seq; uint256 prevHash = ledger->info().parentHash; - std::map< std::uint32_t, std::pair > ledgerHashes; + std::map> ledgerHashes; std::uint32_t minHas = seq; std::uint32_t maxHas = seq; - NodeStore::Database& nodeStore {app_.getNodeStore()}; - while (! job.shouldCancel() && seq > 0) + NodeStore::Database& nodeStore{app_.getNodeStore()}; + while (!job.shouldCancel() && seq > 0) { { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); minHas = seq; --seq; - if (haveLedger (seq)) + if (haveLedger(seq)) break; } - auto it (ledgerHashes.find (seq)); + auto it(ledgerHashes.find(seq)); - if (it == ledgerHashes.end ()) + if (it == ledgerHashes.end()) { - if (app_.isShutdown ()) + if (app_.isShutdown()) return; { @@ -708,21 +713,20 @@ LedgerMaster::tryFill ( mCompleteLedgers.insert(range(minHas, maxHas)); } maxHas = minHas; - ledgerHashes = getHashesByIndex ((seq < 500) - ? 0 - : (seq - 499), seq, app_); - it = ledgerHashes.find (seq); + ledgerHashes = + getHashesByIndex((seq < 500) ? 0 : (seq - 499), seq, app_); + it = ledgerHashes.find(seq); - if (it == ledgerHashes.end ()) + if (it == ledgerHashes.end()) break; - if (!nodeStore.fetch(ledgerHashes.begin()->second.first, - ledgerHashes.begin()->first)) + if (!nodeStore.fetch( + ledgerHashes.begin()->second.first, + ledgerHashes.begin()->first)) { // The ledger is not backed by the node store - JLOG(m_journal.warn()) << - "SQL DB ledger sequence " << seq << - " mismatches node store"; + JLOG(m_journal.warn()) << "SQL DB ledger sequence " << seq + << " mismatches node store"; break; } } @@ -734,32 +738,31 @@ LedgerMaster::tryFill ( } { - std::lock_guard ml (mCompleteLock); + std::lock_guard ml(mCompleteLock); mCompleteLedgers.insert(range(minHas, maxHas)); } { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); mFillInProgress = 0; tryAdvance(); } } /** Request a fetch pack to get to the specified ledger -*/ + */ void -LedgerMaster::getFetchPack (LedgerIndex missing, - InboundLedger::Reason reason) +LedgerMaster::getFetchPack(LedgerIndex missing, InboundLedger::Reason reason) { - auto haveHash {getLedgerHashForHistory(missing + 1, reason)}; + auto haveHash{getLedgerHashForHistory(missing + 1, reason)}; if (!haveHash || haveHash->isZero()) { if (reason == InboundLedger::Reason::SHARD) { - auto const shardStore {app_.getShardStore()}; - auto const shardIndex {shardStore->seqToShardIndex(missing)}; + auto const shardStore{app_.getShardStore()}; + auto const shardIndex{shardStore->seqToShardIndex(missing)}; if (missing < shardStore->lastLedgerSeq(shardIndex)) { - JLOG(m_journal.error()) + JLOG(m_journal.error()) << "No hash for fetch pack. " << "Missing ledger sequence " << missing << " while acquiring shard " << shardIndex; @@ -767,8 +770,8 @@ LedgerMaster::getFetchPack (LedgerIndex missing, } else { - JLOG(m_journal.error()) << - "No hash for fetch pack. Missing Index " << missing; + JLOG(m_journal.error()) + << "No hash for fetch pack. Missing Index " << missing; } return; } @@ -778,13 +781,13 @@ LedgerMaster::getFetchPack (LedgerIndex missing, std::shared_ptr target; { int maxScore = 0; - auto peerList = app_.overlay ().getActivePeers(); + auto peerList = app_.overlay().getActivePeers(); for (auto const& peer : peerList) { - if (peer->hasRange (missing, missing + 1)) + if (peer->hasRange(missing, missing + 1)) { - int score = peer->getScore (true); - if (! target || (score > maxScore)) + int score = peer->getScore(true); + if (!target || (score > maxScore)) { target = peer; maxScore = score; @@ -796,28 +799,27 @@ LedgerMaster::getFetchPack (LedgerIndex missing, if (target) { protocol::TMGetObjectByHash tmBH; - tmBH.set_query (true); - tmBH.set_type (protocol::TMGetObjectByHash::otFETCH_PACK); - tmBH.set_ledgerhash (haveHash->begin(), 32); - auto packet = std::make_shared ( - tmBH, protocol::mtGET_OBJECTS); + tmBH.set_query(true); + tmBH.set_type(protocol::TMGetObjectByHash::otFETCH_PACK); + tmBH.set_ledgerhash(haveHash->begin(), 32); + auto packet = std::make_shared(tmBH, protocol::mtGET_OBJECTS); - target->send (packet); + target->send(packet); JLOG(m_journal.trace()) << "Requested fetch pack for " << missing; } else - JLOG (m_journal.debug()) << "No peer for fetch pack"; + JLOG(m_journal.debug()) << "No peer for fetch pack"; } void -LedgerMaster::fixMismatch (ReadView const& ledger) +LedgerMaster::fixMismatch(ReadView const& ledger) { int invalidate = 0; boost::optional hash; for (std::uint32_t lSeq = ledger.info().seq - 1; lSeq > 0; --lSeq) { - if (haveLedger (lSeq)) + if (haveLedger(lSeq)) { try { @@ -825,8 +827,8 @@ LedgerMaster::fixMismatch (ReadView const& ledger) } catch (std::exception const&) { - JLOG (m_journal.warn()) << - "fixMismatch encounters partial ledger"; + JLOG(m_journal.warn()) + << "fixMismatch encounters partial ledger"; clearLedger(lSeq); return; } @@ -834,16 +836,15 @@ LedgerMaster::fixMismatch (ReadView const& ledger) if (hash) { // try to close the seam - auto otherLedger = getLedgerBySeq (lSeq); + auto otherLedger = getLedgerBySeq(lSeq); if (otherLedger && (otherLedger->info().hash == *hash)) { // we closed the seam if (invalidate != 0) { - JLOG (m_journal.warn()) - << "Match at " << lSeq - << ", " << invalidate + JLOG(m_journal.warn()) + << "Match at " << lSeq << ", " << invalidate << " prior ledgers invalidated"; } @@ -851,7 +852,7 @@ LedgerMaster::fixMismatch (ReadView const& ledger) } } - clearLedger (lSeq); + clearLedger(lSeq); ++invalidate; } } @@ -859,21 +860,21 @@ LedgerMaster::fixMismatch (ReadView const& ledger) // all prior ledgers invalidated if (invalidate != 0) { - JLOG (m_journal.warn()) << - "All " << invalidate << " prior ledgers invalidated"; + JLOG(m_journal.warn()) + << "All " << invalidate << " prior ledgers invalidated"; } } void -LedgerMaster::setFullLedger ( +LedgerMaster::setFullLedger( std::shared_ptr const& ledger, - bool isSynchronous, bool isCurrent) + bool isSynchronous, + bool isCurrent) { // A new ledger has been accepted as part of the trusted chain - JLOG (m_journal.debug()) << - "Ledger " << ledger->info().seq << - " accepted :" << ledger->info().hash; - assert (ledger->stateMap().getHash ().isNonZero ()); + JLOG(m_journal.debug()) << "Ledger " << ledger->info().seq + << " accepted :" << ledger->info().hash; + assert(ledger->stateMap().getHash().isNonZero()); ledger->setValidated(); ledger->setFull(); @@ -884,21 +885,20 @@ LedgerMaster::setFullLedger ( { // Check the SQL database's entry for the sequence before this // ledger, if it's not this ledger's parent, invalidate it - uint256 prevHash = getHashByIndex (ledger->info().seq - 1, app_); - if (prevHash.isNonZero () && prevHash != ledger->info().parentHash) - clearLedger (ledger->info().seq - 1); + uint256 prevHash = getHashByIndex(ledger->info().seq - 1, app_); + if (prevHash.isNonZero() && prevHash != ledger->info().parentHash) + clearLedger(ledger->info().seq - 1); } - - pendSaveValidated (app_, ledger, isSynchronous, isCurrent); + pendSaveValidated(app_, ledger, isSynchronous, isCurrent); { - std::lock_guard ml (mCompleteLock); - mCompleteLedgers.insert (ledger->info().seq); + std::lock_guard ml(mCompleteLock); + mCompleteLedgers.insert(ledger->info().seq); } { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); if (ledger->info().seq > mValidLedgerSeq) setValidLedger(ledger); @@ -908,18 +908,18 @@ LedgerMaster::setFullLedger ( app_.getOrderBookDB().setup(ledger); } - if (ledger->info().seq != 0 && haveLedger (ledger->info().seq - 1)) + if (ledger->info().seq != 0 && haveLedger(ledger->info().seq - 1)) { // we think we have the previous ledger, double check - auto prevLedger = getLedgerBySeq (ledger->info().seq - 1); + auto prevLedger = getLedgerBySeq(ledger->info().seq - 1); if (!prevLedger || (prevLedger->info().hash != ledger->info().parentHash)) { - JLOG (m_journal.warn()) + JLOG(m_journal.warn()) << "Acquired ledger invalidates previous ledger: " << (prevLedger ? "hashMismatch" : "missingLedger"); - fixMismatch (*ledger); + fixMismatch(*ledger); } } } @@ -929,14 +929,13 @@ void LedgerMaster::failedSave(std::uint32_t seq, uint256 const& hash) { clearLedger(seq); - app_.getInboundLedgers().acquire( - hash, seq, InboundLedger::Reason::GENERIC); + app_.getInboundLedgers().acquire(hash, seq, InboundLedger::Reason::GENERIC); } // Check if the specified ledger can become the new last fully-validated // ledger. void -LedgerMaster::checkAccept (uint256 const& hash, std::uint32_t seq) +LedgerMaster::checkAccept(uint256 const& hash, std::uint32_t seq) { std::size_t valCount = 0; @@ -946,14 +945,13 @@ LedgerMaster::checkAccept (uint256 const& hash, std::uint32_t seq) if (seq < mValidLedgerSeq) return; - valCount = - app_.getValidations().numTrustedForLedger (hash); + valCount = app_.getValidations().numTrustedForLedger(hash); - if (valCount >= app_.validators ().quorum ()) + if (valCount >= app_.validators().quorum()) { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); if (seq > mLastValidLedger.second) - mLastValidLedger = std::make_pair (hash, seq); + mLastValidLedger = std::make_pair(hash, seq); } if (seq == mValidLedgerSeq) @@ -964,15 +962,15 @@ LedgerMaster::checkAccept (uint256 const& hash, std::uint32_t seq) return; } - auto ledger = mLedgerHistory.getLedgerByHash (hash); + auto ledger = mLedgerHistory.getLedgerByHash(hash); if (!ledger) { if ((seq != 0) && (getValidLedgerIndex() == 0)) { // Set peers sane early if we can - if (valCount >= app_.validators ().quorum ()) - app_.overlay().checkSanity (seq); + if (valCount >= app_.validators().quorum()) + app_.overlay().checkSanity(seq); } // FIXME: We may not want to fetch a ledger with just one @@ -982,49 +980,48 @@ LedgerMaster::checkAccept (uint256 const& hash, std::uint32_t seq) } if (ledger) - checkAccept (ledger); + checkAccept(ledger); } /** - * Determines how many validations are needed to fully validate a ledger - * - * @return Number of validations needed - */ + * Determines how many validations are needed to fully validate a ledger + * + * @return Number of validations needed + */ std::size_t -LedgerMaster::getNeededValidations () +LedgerMaster::getNeededValidations() { - return standalone_ ? 0 : app_.validators().quorum (); + return standalone_ ? 0 : app_.validators().quorum(); } void -LedgerMaster::checkAccept ( - std::shared_ptr const& ledger) +LedgerMaster::checkAccept(std::shared_ptr const& ledger) { // Can we accept this ledger as our new last fully-validated ledger - if (! canBeCurrent (ledger)) + if (!canBeCurrent(ledger)) return; // Can we advance the last fully-validated ledger? If so, can we // publish? - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); if (ledger->info().seq <= mValidLedgerSeq) return; auto const minVal = getNeededValidations(); - auto const tvc = app_.getValidations().numTrustedForLedger(ledger->info().hash); - if (tvc < minVal) // nothing we can do + auto const tvc = + app_.getValidations().numTrustedForLedger(ledger->info().hash); + if (tvc < minVal) // nothing we can do { - JLOG (m_journal.trace()) << - "Only " << tvc << - " validations for " << ledger->info().hash; + JLOG(m_journal.trace()) + << "Only " << tvc << " validations for " << ledger->info().hash; return; } - JLOG (m_journal.info()) - << "Advancing accepted ledger to " << ledger->info().seq - << " with >= " << minVal << " validations"; + JLOG(m_journal.info()) << "Advancing accepted ledger to " + << ledger->info().seq << " with >= " << minVal + << " validations"; ledger->setValidated(); ledger->setFull(); @@ -1037,18 +1034,18 @@ LedgerMaster::checkAccept ( } std::uint32_t const base = app_.getFeeTrack().getLoadBase(); - auto fees = app_.getValidations().fees (ledger->info().hash, base); + auto fees = app_.getValidations().fees(ledger->info().hash, base); { - auto fees2 = app_.getValidations().fees ( - ledger->info(). parentHash, base); - fees.reserve (fees.size() + fees2.size()); - std::copy (fees2.begin(), fees2.end(), std::back_inserter(fees)); + auto fees2 = + app_.getValidations().fees(ledger->info().parentHash, base); + fees.reserve(fees.size() + fees2.size()); + std::copy(fees2.begin(), fees2.end(), std::back_inserter(fees)); } std::uint32_t fee; - if (! fees.empty()) + if (!fees.empty()) { - std::sort (fees.begin(), fees.end()); - fee = fees[fees.size() / 2]; // median + std::sort(fees.begin(), fees.end()); + fee = fees[fees.size() / 2]; // median } else { @@ -1057,7 +1054,7 @@ LedgerMaster::checkAccept ( app_.getFeeTrack().setRemoteFee(fee); - tryAdvance (); + tryAdvance(); } /** Report that the consensus process built a particular ledger */ @@ -1067,50 +1064,49 @@ LedgerMaster::consensusBuilt( uint256 const& consensusHash, Json::Value consensus) { - // Because we just built a ledger, we are no longer building one - setBuildingLedger (0); + setBuildingLedger(0); // No need to process validations in standalone mode if (standalone_) return; - mLedgerHistory.builtLedger (ledger, consensusHash, std::move (consensus)); + mLedgerHistory.builtLedger(ledger, consensusHash, std::move(consensus)); if (ledger->info().seq <= mValidLedgerSeq) { - auto stream = app_.journal ("LedgerConsensus").info(); - JLOG (stream) - << "Consensus built old ledger: " - << ledger->info().seq << " <= " << mValidLedgerSeq; + auto stream = app_.journal("LedgerConsensus").info(); + JLOG(stream) << "Consensus built old ledger: " << ledger->info().seq + << " <= " << mValidLedgerSeq; return; } // See if this ledger can be the new fully-validated ledger - checkAccept (ledger); + checkAccept(ledger); if (ledger->info().seq <= mValidLedgerSeq) { - auto stream = app_.journal ("LedgerConsensus").debug(); - JLOG (stream) - << "Consensus ledger fully validated"; + auto stream = app_.journal("LedgerConsensus").debug(); + JLOG(stream) << "Consensus ledger fully validated"; return; } // This ledger cannot be the new fully-validated ledger, but // maybe we saved up validations for some other ledger that can be - auto const val = - app_.getValidations().currentTrusted(); + auto const val = app_.getValidations().currentTrusted(); // Track validation counts with sequence numbers class valSeq { - public: + public: + valSeq() : valCount_(0), ledgerSeq_(0) + { + ; + } - valSeq () : valCount_ (0), ledgerSeq_ (0) { ; } - - void mergeValidation (LedgerIndex seq) + void + mergeValidation(LedgerIndex seq) { valCount_++; @@ -1124,14 +1120,14 @@ LedgerMaster::consensusBuilt( }; // Count the number of current, trusted validations - hash_map count; + hash_map count; for (auto const& v : val) { valSeq& vs = count[v->getLedgerHash()]; - vs.mergeValidation (v->getFieldU32 (sfLedgerSequence)); + vs.mergeValidation(v->getFieldU32(sfLedgerSequence)); } - auto const neededValidations = getNeededValidations (); + auto const neededValidations = getNeededValidations(); auto maxSeq = mValidLedgerSeq.load(); auto maxLedger = ledger->info().hash; @@ -1143,7 +1139,7 @@ LedgerMaster::consensusBuilt( // If we still don't know the sequence, get it if (v.second.ledgerSeq_ == 0) { - if (auto l = getLedgerByHash (v.first)) + if (auto l = getLedgerByHash(v.first)) v.second.ledgerSeq_ = l->info().seq; } @@ -1156,20 +1152,19 @@ LedgerMaster::consensusBuilt( if (maxSeq > mValidLedgerSeq) { - auto stream = app_.journal ("LedgerConsensus").debug(); - JLOG (stream) - << "Consensus triggered check of ledger"; - checkAccept (maxLedger, maxSeq); + auto stream = app_.journal("LedgerConsensus").debug(); + JLOG(stream) << "Consensus triggered check of ledger"; + checkAccept(maxLedger, maxSeq); } } void LedgerMaster::advanceThread() { - std::unique_lock sl (m_mutex); - assert (!mValidLedger.empty () && mAdvanceThread); + std::unique_lock sl(m_mutex); + assert(!mValidLedger.empty() && mAdvanceThread); - JLOG (m_journal.trace()) << "advanceThread<"; + JLOG(m_journal.trace()) << "advanceThread<"; try { @@ -1177,82 +1172,81 @@ LedgerMaster::advanceThread() } catch (std::exception const&) { - JLOG (m_journal.fatal()) << "doAdvance throws an exception"; + JLOG(m_journal.fatal()) << "doAdvance throws an exception"; } mAdvanceThread = false; - JLOG (m_journal.trace()) << "advanceThread>"; + JLOG(m_journal.trace()) << "advanceThread>"; } boost::optional LedgerMaster::getLedgerHashForHistory( - LedgerIndex index, InboundLedger::Reason reason) + LedgerIndex index, + InboundLedger::Reason reason) { // Try to get the hash of a ledger we need to fetch for history boost::optional ret; - auto const& l {reason == InboundLedger::Reason::SHARD ? - mShardLedger : mHistLedger}; + auto const& l{ + reason == InboundLedger::Reason::SHARD ? mShardLedger : mHistLedger}; if (l && l->info().seq >= index) { ret = hashOfSeq(*l, index, m_journal); - if (! ret) - ret = walkHashBySeq (index, l); + if (!ret) + ret = walkHashBySeq(index, l); } - if (! ret) - ret = walkHashBySeq (index); + if (!ret) + ret = walkHashBySeq(index); return ret; } std::vector> -LedgerMaster::findNewLedgersToPublish(std::unique_lock& sl) +LedgerMaster::findNewLedgersToPublish( + std::unique_lock& sl) { std::vector> ret; - JLOG (m_journal.trace()) << "findNewLedgersToPublish<"; + JLOG(m_journal.trace()) << "findNewLedgersToPublish<"; // No valid ledger, nothing to do - if (mValidLedger.empty ()) + if (mValidLedger.empty()) { - JLOG(m_journal.trace()) << - "No valid journal, nothing to publish."; + JLOG(m_journal.trace()) << "No valid journal, nothing to publish."; return {}; } - if (! mPubLedger) + if (!mPubLedger) { - JLOG(m_journal.info()) << - "First published ledger will be " << mValidLedgerSeq; - return { mValidLedger.get () }; + JLOG(m_journal.info()) + << "First published ledger will be " << mValidLedgerSeq; + return {mValidLedger.get()}; } if (mValidLedgerSeq > (mPubLedgerSeq + MAX_LEDGER_GAP)) { - JLOG(m_journal.warn()) << - "Gap in validated ledger stream " << mPubLedgerSeq << - " - " << mValidLedgerSeq - 1; + JLOG(m_journal.warn()) << "Gap in validated ledger stream " + << mPubLedgerSeq << " - " << mValidLedgerSeq - 1; - auto valLedger = mValidLedger.get (); - ret.push_back (valLedger); - setPubLedger (valLedger); + auto valLedger = mValidLedger.get(); + ret.push_back(valLedger); + setPubLedger(valLedger); app_.getOrderBookDB().setup(valLedger); - return { valLedger }; + return {valLedger}; } if (mValidLedgerSeq <= mPubLedgerSeq) { - JLOG(m_journal.trace()) << - "No valid journal, nothing to publish."; + JLOG(m_journal.trace()) << "No valid journal, nothing to publish."; return {}; } int acqCount = 0; - auto pubSeq = mPubLedgerSeq + 1; // Next sequence to publish - auto valLedger = mValidLedger.get (); + auto pubSeq = mPubLedgerSeq + 1; // Next sequence to publish + auto valLedger = mValidLedger.get(); std::uint32_t valSeq = valLedger->info().seq; ScopedUnlock sul{sl}; @@ -1268,8 +1262,8 @@ LedgerMaster::findNewLedgersToPublish(std::unique_lock& sl auto hash = hashOfSeq(*valLedger, seq, m_journal); // VFALCO TODO Restructure this code so that zero is not // used. - if (! hash) - hash = beast::zero; // kludge + if (!hash) + hash = beast::zero; // kludge if (seq == valSeq) { // We need to publish the ledger we just fully validated @@ -1277,37 +1271,36 @@ LedgerMaster::findNewLedgersToPublish(std::unique_lock& sl } else if (hash->isZero()) { - JLOG (m_journal.fatal()) - << "Ledger: " << valSeq - << " does not have hash for " << seq; - assert (false); + JLOG(m_journal.fatal()) << "Ledger: " << valSeq + << " does not have hash for " << seq; + assert(false); } else { - ledger = mLedgerHistory.getLedgerByHash (*hash); + ledger = mLedgerHistory.getLedgerByHash(*hash); } // Can we try to acquire the ledger we need? - if (! ledger && (++acqCount < ledger_fetch_size_)) - ledger = app_.getInboundLedgers ().acquire( + if (!ledger && (++acqCount < ledger_fetch_size_)) + ledger = app_.getInboundLedgers().acquire( *hash, seq, InboundLedger::Reason::GENERIC); // Did we acquire the next ledger we need to publish? if (ledger && (ledger->info().seq == pubSeq)) { ledger->setValidated(); - ret.push_back (ledger); + ret.push_back(ledger); ++pubSeq; } } - JLOG(m_journal.trace()) << - "ready to publish " << ret.size() << " ledgers."; + JLOG(m_journal.trace()) + << "ready to publish " << ret.size() << " ledgers."; } catch (std::exception const&) { - JLOG(m_journal.error()) << - "Exception while trying to find ledgers to publish."; + JLOG(m_journal.error()) + << "Exception while trying to find ledgers to publish."; } return ret; @@ -1316,16 +1309,15 @@ LedgerMaster::findNewLedgersToPublish(std::unique_lock& sl void LedgerMaster::tryAdvance() { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); // Can't advance without at least one fully-valid ledger mAdvanceWork = true; - if (!mAdvanceThread && !mValidLedger.empty ()) + if (!mAdvanceThread && !mValidLedger.empty()) { mAdvanceThread = true; - app_.getJobQueue ().addJob ( - jtADVANCE, "advanceLedger", - [this] (Job&) { advanceThread(); }); + app_.getJobQueue().addJob( + jtADVANCE, "advanceLedger", [this](Job&) { advanceThread(); }); } } @@ -1341,7 +1333,7 @@ LedgerMaster::getLedgerHash( auto hash = hashOfSeq(*knownGoodLedger, desiredSeq, m_journal); // Not directly in the given ledger - if (! hash) + if (!hash) { std::uint32_t seq = (desiredSeq + 255) % 256; assert(seq < desiredSeq); @@ -1352,7 +1344,7 @@ LedgerMaster::getLedgerHash( if (auto l = getLedgerByHash(*hash)) { hash = hashOfSeq(*l, desiredSeq, m_journal); - assert (hash); + assert(hash); } } else @@ -1365,10 +1357,10 @@ LedgerMaster::getLedgerHash( } void -LedgerMaster::updatePaths (Job& job) +LedgerMaster::updatePaths(Job& job) { { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); if (app_.getOPs().isNeedNetworkLedger()) { --mPathFindThread; @@ -1376,41 +1368,39 @@ LedgerMaster::updatePaths (Job& job) } } - - while (! job.shouldCancel()) + while (!job.shouldCancel()) { std::shared_ptr lastLedger; { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); if (!mValidLedger.empty() && - (!mPathLedger || - (mPathLedger->info().seq != mValidLedgerSeq))) - { // We have a new valid ledger since the last full pathfinding - mPathLedger = mValidLedger.get (); + (!mPathLedger || (mPathLedger->info().seq != mValidLedgerSeq))) + { // We have a new valid ledger since the last full pathfinding + mPathLedger = mValidLedger.get(); lastLedger = mPathLedger; } else if (mPathFindNewRequest) - { // We have a new request but no new ledger + { // We have a new request but no new ledger lastLedger = app_.openLedger().current(); } else - { // Nothing to do + { // Nothing to do --mPathFindThread; return; } } if (!standalone_) - { // don't pathfind with a ledger that's more than 60 seconds old + { // don't pathfind with a ledger that's more than 60 seconds old using namespace std::chrono; - auto age = time_point_cast(app_.timeKeeper().closeTime()) - - lastLedger->info().closeTime; + auto age = time_point_cast(app_.timeKeeper().closeTime()) - + lastLedger->info().closeTime; if (age > 1min) { - JLOG (m_journal.debug()) + JLOG(m_journal.debug()) << "Published ledger too old for updating paths"; - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); --mPathFindThread; return; } @@ -1423,8 +1413,7 @@ LedgerMaster::updatePaths (Job& job) } catch (SHAMapMissingNode const& mn) { - JLOG (m_journal.info()) - << "During pathfinding: " << mn.what(); + JLOG(m_journal.info()) << "During pathfinding: " << mn.what(); if (lastLedger->open()) { // our parent is the problem @@ -1446,17 +1435,17 @@ LedgerMaster::updatePaths (Job& job) } bool -LedgerMaster::newPathRequest () +LedgerMaster::newPathRequest() { - std::unique_lock ml (m_mutex); + std::unique_lock ml(m_mutex); mPathFindNewRequest = newPFWork("pf:newRequest", ml); return mPathFindNewRequest; } bool -LedgerMaster::isNewPathRequest () +LedgerMaster::isNewPathRequest() { - std::lock_guard ml (m_mutex); + std::lock_guard ml(m_mutex); bool const ret = mPathFindNewRequest; mPathFindNewRequest = false; return ret; @@ -1465,24 +1454,25 @@ LedgerMaster::isNewPathRequest () // If the order book is radically updated, we need to reprocess all // pathfinding requests. bool -LedgerMaster::newOrderBookDB () +LedgerMaster::newOrderBookDB() { - std::unique_lock ml (m_mutex); + std::unique_lock ml(m_mutex); mPathLedger.reset(); return newPFWork("pf:newOBDB", ml); } /** A thread needs to be dispatched to handle pathfinding work of some kind. -*/ + */ bool -LedgerMaster::newPFWork (const char *name, std::unique_lock&) +LedgerMaster::newPFWork( + const char* name, + std::unique_lock&) { if (mPathFindThread < 2) { - if (app_.getJobQueue().addJob ( - jtUPDATE_PF, name, - [this] (Job& j) { updatePaths(j); })) + if (app_.getJobQueue().addJob( + jtUPDATE_PF, name, [this](Job& j) { updatePaths(j); })) { ++mPathFindThread; } @@ -1493,20 +1483,20 @@ LedgerMaster::newPFWork (const char *name, std::unique_lock -LedgerMaster::getCurrentLedger () +LedgerMaster::getCurrentLedger() { return app_.openLedger().current(); } Rules -LedgerMaster::getValidatedRules () +LedgerMaster::getValidatedRules() { // Once we have a guarantee that there's always a last validated // ledger then we can dispense with the if. @@ -1521,41 +1511,41 @@ LedgerMaster::getValidatedRules () // This is the last ledger we published to clients and can lag the validated // ledger. std::shared_ptr -LedgerMaster::getPublishedLedger () +LedgerMaster::getPublishedLedger() { std::lock_guard lock(m_mutex); return mPubLedger; } std::string -LedgerMaster::getCompleteLedgers () +LedgerMaster::getCompleteLedgers() { - std::lock_guard sl (mCompleteLock); + std::lock_guard sl(mCompleteLock); return to_string(mCompleteLedgers); } -boost::optional -LedgerMaster::getCloseTimeBySeq (LedgerIndex ledgerIndex) +boost::optional +LedgerMaster::getCloseTimeBySeq(LedgerIndex ledgerIndex) { - uint256 hash = getHashBySeq (ledgerIndex); - return hash.isNonZero() ? getCloseTimeByHash( - hash, ledgerIndex) : boost::none; + uint256 hash = getHashBySeq(ledgerIndex); + return hash.isNonZero() ? getCloseTimeByHash(hash, ledgerIndex) + : boost::none; } -boost::optional -LedgerMaster::getCloseTimeByHash(LedgerHash const& ledgerHash, +boost::optional +LedgerMaster::getCloseTimeByHash( + LedgerHash const& ledgerHash, std::uint32_t index) { auto node = app_.getNodeStore().fetch(ledgerHash, index); - if (node && - (node->getData().size() >= 120)) + if (node && (node->getData().size() >= 120)) { - SerialIter it (node->getData().data(), node->getData().size()); + SerialIter it(node->getData().data(), node->getData().size()); if (safe_cast(it.get32()) == HashPrefix::ledgerMaster) { - it.skip ( - 4+8+32+ // seq drops parentHash - 32+32+4); // txHash acctHash parentClose + it.skip( + 4 + 8 + 32 + // seq drops parentHash + 32 + 32 + 4); // txHash acctHash parentClose return NetClock::time_point{NetClock::duration{it.get32()}}; } } @@ -1564,29 +1554,29 @@ LedgerMaster::getCloseTimeByHash(LedgerHash const& ledgerHash, } uint256 -LedgerMaster::getHashBySeq (std::uint32_t index) +LedgerMaster::getHashBySeq(std::uint32_t index) { - uint256 hash = mLedgerHistory.getLedgerHash (index); + uint256 hash = mLedgerHistory.getLedgerHash(index); - if (hash.isNonZero ()) + if (hash.isNonZero()) return hash; - return getHashByIndex (index, app_); + return getHashByIndex(index, app_); } boost::optional -LedgerMaster::walkHashBySeq (std::uint32_t index) +LedgerMaster::walkHashBySeq(std::uint32_t index) { boost::optional ledgerHash; - if (auto referenceLedger = mValidLedger.get ()) - ledgerHash = walkHashBySeq (index, referenceLedger); + if (auto referenceLedger = mValidLedger.get()) + ledgerHash = walkHashBySeq(index, referenceLedger); return ledgerHash; } boost::optional -LedgerMaster::walkHashBySeq ( +LedgerMaster::walkHashBySeq( std::uint32_t index, std::shared_ptr const& referenceLedger) { @@ -1609,7 +1599,7 @@ LedgerMaster::walkHashBySeq ( if (refHash) { // Try the hash and sequence of a better reference ledger just found - auto ledger = mLedgerHistory.getLedgerByHash (*refHash); + auto ledger = mLedgerHistory.getLedgerByHash(*refHash); if (ledger) { @@ -1617,7 +1607,7 @@ LedgerMaster::walkHashBySeq ( { ledgerHash = hashOfSeq(*ledger, index, m_journal); } - catch(SHAMapMissingNode const&) + catch (SHAMapMissingNode const&) { ledger.reset(); } @@ -1626,11 +1616,11 @@ LedgerMaster::walkHashBySeq ( // Try to acquire the complete ledger if (!ledger) { - if (auto const l = app_.getInboundLedgers().acquire ( + if (auto const l = app_.getInboundLedgers().acquire( *refHash, refIndex, InboundLedger::Reason::GENERIC)) { ledgerHash = hashOfSeq(*l, index, m_journal); - assert (ledgerHash); + assert(ledgerHash); } } } @@ -1638,12 +1628,12 @@ LedgerMaster::walkHashBySeq ( } std::shared_ptr -LedgerMaster::getLedgerBySeq (std::uint32_t index) +LedgerMaster::getLedgerBySeq(std::uint32_t index) { if (index <= mValidLedgerSeq) { // Always prefer a validated ledger - if (auto valid = mValidLedger.get ()) + if (auto valid = mValidLedger.get()) { if (valid->info().seq == index) return valid; @@ -1653,7 +1643,7 @@ LedgerMaster::getLedgerBySeq (std::uint32_t index) auto const hash = hashOfSeq(*valid, index, m_journal); if (hash) - return mLedgerHistory.getLedgerByHash (*hash); + return mLedgerHistory.getLedgerByHash(*hash); } catch (std::exception const&) { @@ -1662,24 +1652,24 @@ LedgerMaster::getLedgerBySeq (std::uint32_t index) } } - if (auto ret = mLedgerHistory.getLedgerBySeq (index)) + if (auto ret = mLedgerHistory.getLedgerBySeq(index)) return ret; - auto ret = mClosedLedger.get (); + auto ret = mClosedLedger.get(); if (ret && (ret->info().seq == index)) return ret; - clearLedger (index); + clearLedger(index); return {}; } std::shared_ptr -LedgerMaster::getLedgerByHash (uint256 const& hash) +LedgerMaster::getLedgerByHash(uint256 const& hash) { - if (auto ret = mLedgerHistory.getLedgerByHash (hash)) + if (auto ret = mLedgerHistory.getLedgerByHash(hash)) return ret; - auto ret = mClosedLedger.get (); + auto ret = mClosedLedger.get(); if (ret && (ret->info().hash == hash)) return ret; @@ -1689,43 +1679,43 @@ LedgerMaster::getLedgerByHash (uint256 const& hash) void LedgerMaster::doLedgerCleaner(Json::Value const& parameters) { - mLedgerCleaner->doClean (parameters); + mLedgerCleaner->doClean(parameters); } void -LedgerMaster::setLedgerRangePresent (std::uint32_t minV, std::uint32_t maxV) +LedgerMaster::setLedgerRangePresent(std::uint32_t minV, std::uint32_t maxV) { - std::lock_guard sl (mCompleteLock); + std::lock_guard sl(mCompleteLock); mCompleteLedgers.insert(range(minV, maxV)); } void -LedgerMaster::tune (int size, std::chrono::seconds age) +LedgerMaster::tune(int size, std::chrono::seconds age) { - mLedgerHistory.tune (size, age); + mLedgerHistory.tune(size, age); } void -LedgerMaster::sweep () +LedgerMaster::sweep() { - mLedgerHistory.sweep (); - fetch_packs_.sweep (); + mLedgerHistory.sweep(); + fetch_packs_.sweep(); } float -LedgerMaster::getCacheHitRate () +LedgerMaster::getCacheHitRate() { - return mLedgerHistory.getCacheHitRate (); + return mLedgerHistory.getCacheHitRate(); } beast::PropertyStream::Source& -LedgerMaster::getPropertySource () +LedgerMaster::getPropertySource() { return *mLedgerCleaner; } void -LedgerMaster::clearPriorLedgers (LedgerIndex seq) +LedgerMaster::clearPriorLedgers(LedgerIndex seq) { std::lock_guard sl(mCompleteLock); if (seq > 0) @@ -1733,21 +1723,21 @@ LedgerMaster::clearPriorLedgers (LedgerIndex seq) } void -LedgerMaster::clearLedgerCachePrior (LedgerIndex seq) +LedgerMaster::clearLedgerCachePrior(LedgerIndex seq) { - mLedgerHistory.clearLedgerCachePrior (seq); + mLedgerHistory.clearLedgerCachePrior(seq); } void -LedgerMaster::takeReplay (std::unique_ptr replay) +LedgerMaster::takeReplay(std::unique_ptr replay) { - replayData = std::move (replay); + replayData = std::move(replay); } std::unique_ptr -LedgerMaster::releaseReplay () +LedgerMaster::releaseReplay() { - return std::move (replayData); + return std::move(replayData); } void @@ -1762,14 +1752,13 @@ LedgerMaster::fetchForHistory( { assert(hash->isNonZero()); auto ledger = getLedgerByHash(*hash); - if (! ledger) + if (!ledger) { if (!app_.getInboundLedgers().isFailure(*hash)) { - ledger = app_.getInboundLedgers().acquire( - *hash, missing, reason); - if (!ledger && - missing != fetch_seq_ && + ledger = + app_.getInboundLedgers().acquire(*hash, missing, reason); + if (!ledger && missing != fetch_seq_ && missing > app_.getNodeStore().earliestLedgerSeq()) { JLOG(m_journal.trace()) @@ -1789,8 +1778,7 @@ LedgerMaster::fetchForHistory( { auto seq = ledger->info().seq; assert(seq == missing); - JLOG(m_journal.trace()) << - "fetchForHistory acquired " << seq; + JLOG(m_journal.trace()) << "fetchForHistory acquired " << seq; if (reason == InboundLedger::Reason::SHARD) { ledger->setFull(); @@ -1818,8 +1806,10 @@ LedgerMaster::fetchForHistory( std::lock_guard lock(m_mutex); mFillInProgress = seq; } - app_.getJobQueue().addJob(jtADVANCE, "tryFill", - [this, ledger](Job& j) { tryFill(j, ledger); }); + app_.getJobQueue().addJob( + jtADVANCE, "tryFill", [this, ledger](Job& j) { + tryFill(j, ledger); + }); } } progress = true; @@ -1836,8 +1826,9 @@ LedgerMaster::fetchForHistory( // Do not fetch ledger sequences lower // than the earliest ledger sequence fetchSz = app_.getNodeStore().earliestLedgerSeq(); - fetchSz = missing >= fetchSz ? - std::min(ledger_fetch_size_, (missing - fetchSz) + 1) : 0; + fetchSz = missing >= fetchSz + ? std::min(ledger_fetch_size_, (missing - fetchSz) + 1) + : 0; try { for (std::uint32_t i = 0; i < fetchSz; ++i) @@ -1858,13 +1849,14 @@ LedgerMaster::fetchForHistory( } else { - JLOG(m_journal.fatal()) << "Can't find ledger following prevMissing " - << missing; - JLOG(m_journal.fatal()) << "Pub:" << mPubLedgerSeq - << " Val:" << mValidLedgerSeq; - JLOG(m_journal.fatal()) << "Ledgers: " - << app_.getLedgerMaster().getCompleteLedgers(); - JLOG(m_journal.fatal()) << "Acquire reason: " + JLOG(m_journal.fatal()) + << "Can't find ledger following prevMissing " << missing; + JLOG(m_journal.fatal()) + << "Pub:" << mPubLedgerSeq << " Val:" << mValidLedgerSeq; + JLOG(m_journal.fatal()) + << "Ledgers: " << app_.getLedgerMaster().getCompleteLedgers(); + JLOG(m_journal.fatal()) + << "Acquire reason: " << (reason == InboundLedger::Reason::HISTORY ? "HISTORY" : "SHARD"); clearLedger(missing + 1); progress = true; @@ -1872,14 +1864,15 @@ LedgerMaster::fetchForHistory( } // Try to publish ledgers, acquire missing ledgers -void LedgerMaster::doAdvance (std::unique_lock& sl) +void +LedgerMaster::doAdvance(std::unique_lock& sl) { do { - mAdvanceWork = false; // If there's work to do, we'll make progress + mAdvanceWork = false; // If there's work to do, we'll make progress bool progress = false; - auto const pubLedgers = findNewLedgersToPublish (sl); + auto const pubLedgers = findNewLedgersToPublish(sl); if (pubLedgers.empty()) { if (!standalone_ && !app_.getFeeTrack().isLoadedLocal() && @@ -1893,27 +1886,31 @@ void LedgerMaster::doAdvance (std::unique_lock& sl) boost::optional missing; { std::lock_guard sll(mCompleteLock); - missing = prevMissing(mCompleteLedgers, + missing = prevMissing( + mCompleteLedgers, mPubLedger->info().seq, app_.getNodeStore().earliestLedgerSeq()); } if (missing) { - JLOG(m_journal.trace()) << - "tryAdvance discovered missing " << *missing; + JLOG(m_journal.trace()) + << "tryAdvance discovered missing " << *missing; if ((mFillInProgress == 0 || *missing > mFillInProgress) && - shouldAcquire(mValidLedgerSeq, ledger_history_, + shouldAcquire( + mValidLedgerSeq, + ledger_history_, minSqlSeq(app_), - app_.getSHAMapStore().getLastRotated(), *missing, + app_.getSHAMapStore().getLastRotated(), + *missing, m_journal)) { - JLOG(m_journal.trace()) << - "advanceThread should acquire"; + JLOG(m_journal.trace()) + << "advanceThread should acquire"; } else missing = boost::none; } - if (! missing && mFillInProgress == 0) + if (!missing && mFillInProgress == 0) { if (auto shardStore = app_.getShardStore()) { @@ -1922,13 +1919,13 @@ void LedgerMaster::doAdvance (std::unique_lock& sl) reason = InboundLedger::Reason::SHARD; } } - if(missing) + if (missing) { fetchForHistory(*missing, progress, reason, sl); if (mValidLedgerSeq != mPubLedgerSeq) { - JLOG (m_journal.debug()) << - "tryAdvance found last valid changed"; + JLOG(m_journal.debug()) + << "tryAdvance found last valid changed"; progress = true; } } @@ -1937,21 +1934,19 @@ void LedgerMaster::doAdvance (std::unique_lock& sl) { mHistLedger.reset(); mShardLedger.reset(); - JLOG (m_journal.trace()) << - "tryAdvance not fetching history"; + JLOG(m_journal.trace()) << "tryAdvance not fetching history"; } } else { - JLOG (m_journal.trace()) << - "tryAdvance found " << pubLedgers.size() << - " ledgers to publish"; - for(auto ledger : pubLedgers) + JLOG(m_journal.trace()) << "tryAdvance found " << pubLedgers.size() + << " ledgers to publish"; + for (auto ledger : pubLedgers) { { ScopedUnlock sul{sl}; - JLOG (m_journal.debug()) << - "tryAdvance publishing seq " << ledger->info().seq; + JLOG(m_journal.debug()) + << "tryAdvance publishing seq " << ledger->info().seq; setFullLedger(ledger, true, true); } @@ -1964,7 +1959,7 @@ void LedgerMaster::doAdvance (std::unique_lock& sl) } app_.getOPs().clearNeedNetworkLedger(); - progress = newPFWork ("pf:newLedger", sl); + progress = newPFWork("pf:newLedger", sl); } if (progress) mAdvanceWork = true; @@ -1972,16 +1967,13 @@ void LedgerMaster::doAdvance (std::unique_lock& sl) } void -LedgerMaster::addFetchPack ( - uint256 const& hash, - std::shared_ptr< Blob >& data) +LedgerMaster::addFetchPack(uint256 const& hash, std::shared_ptr& data) { fetch_packs_.canonicalize_replace_client(hash, data); } boost::optional -LedgerMaster::getFetchPack ( - uint256 const& hash) +LedgerMaster::getFetchPack(uint256 const& hash) { Blob data; if (fetch_packs_.retrieve(hash, data)) @@ -1994,28 +1986,23 @@ LedgerMaster::getFetchPack ( } void -LedgerMaster::gotFetchPack ( - bool progress, - std::uint32_t seq) +LedgerMaster::gotFetchPack(bool progress, std::uint32_t seq) { if (!mGotFetchPackThread.test_and_set(std::memory_order_acquire)) { - app_.getJobQueue().addJob ( - jtLEDGER_DATA, "gotFetchPack", - [&] (Job&) - { - app_.getInboundLedgers().gotFetchPack(); - mGotFetchPackThread.clear(std::memory_order_release); - }); + app_.getJobQueue().addJob(jtLEDGER_DATA, "gotFetchPack", [&](Job&) { + app_.getInboundLedgers().gotFetchPack(); + mGotFetchPackThread.clear(std::memory_order_release); + }); } } void -LedgerMaster::makeFetchPack ( +LedgerMaster::makeFetchPack( std::weak_ptr const& wPeer, std::shared_ptr const& request, uint256 haveLedgerHash, - UptimeClock::time_point uptime) + UptimeClock::time_point uptime) { using namespace std::chrono_literals; if (UptimeClock::now() > uptime + 1s) @@ -2024,80 +2011,74 @@ LedgerMaster::makeFetchPack ( return; } - if (app_.getFeeTrack ().isLoadedLocal () || - (getValidatedLedgerAge() > 40s)) + if (app_.getFeeTrack().isLoadedLocal() || (getValidatedLedgerAge() > 40s)) { JLOG(m_journal.info()) << "Too busy to make fetch pack"; return; } - auto peer = wPeer.lock (); + auto peer = wPeer.lock(); if (!peer) return; - auto haveLedger = getLedgerByHash (haveLedgerHash); + auto haveLedger = getLedgerByHash(haveLedgerHash); if (!haveLedger) { JLOG(m_journal.info()) << "Peer requests fetch pack for ledger we don't have: " << haveLedger; - peer->charge (Resource::feeRequestNoReply); + peer->charge(Resource::feeRequestNoReply); return; } if (haveLedger->open()) { JLOG(m_journal.warn()) - << "Peer requests fetch pack from open ledger: " - << haveLedger; - peer->charge (Resource::feeInvalidRequest); + << "Peer requests fetch pack from open ledger: " << haveLedger; + peer->charge(Resource::feeInvalidRequest); return; } if (haveLedger->info().seq < getEarliestFetch()) { - JLOG(m_journal.debug()) - << "Peer requests fetch pack that is too early"; - peer->charge (Resource::feeInvalidRequest); + JLOG(m_journal.debug()) << "Peer requests fetch pack that is too early"; + peer->charge(Resource::feeInvalidRequest); return; } - auto wantLedger = getLedgerByHash (haveLedger->info().parentHash); + auto wantLedger = getLedgerByHash(haveLedger->info().parentHash); if (!wantLedger) { JLOG(m_journal.info()) << "Peer requests fetch pack for ledger whose predecessor we " << "don't have: " << haveLedger; - peer->charge (Resource::feeRequestNoReply); + peer->charge(Resource::feeRequestNoReply); return; } - - auto fpAppender = []( - protocol::TMGetObjectByHash* reply, - std::uint32_t ledgerSeq, - SHAMapHash const& hash, - const Blob& blob) - { - protocol::TMIndexedObject& newObj = * (reply->add_objects ()); - newObj.set_ledgerseq (ledgerSeq); - newObj.set_hash (hash.as_uint256().begin (), 256 / 8); - newObj.set_data (&blob[0], blob.size ()); + auto fpAppender = [](protocol::TMGetObjectByHash* reply, + std::uint32_t ledgerSeq, + SHAMapHash const& hash, + const Blob& blob) { + protocol::TMIndexedObject& newObj = *(reply->add_objects()); + newObj.set_ledgerseq(ledgerSeq); + newObj.set_hash(hash.as_uint256().begin(), 256 / 8); + newObj.set_data(&blob[0], blob.size()); }; try { protocol::TMGetObjectByHash reply; - reply.set_query (false); + reply.set_query(false); - if (request->has_seq ()) - reply.set_seq (request->seq ()); + if (request->has_seq()) + reply.set_seq(request->seq()); - reply.set_ledgerhash (request->ledgerhash ()); - reply.set_type (protocol::TMGetObjectByHash::otFETCH_PACK); + reply.set_ledgerhash(request->ledgerhash()); + reply.set_type(protocol::TMGetObjectByHash::otFETCH_PACK); // Building a fetch pack: // 1. Add the header for the requested ledger. @@ -2112,40 +2093,49 @@ LedgerMaster::makeFetchPack ( { std::uint32_t lSeq = wantLedger->info().seq; - protocol::TMIndexedObject& newObj = *reply.add_objects (); - newObj.set_hash ( - wantLedger->info().hash.data(), 256 / 8); - Serializer s (256); - s.add32 (HashPrefix::ledgerMaster); + protocol::TMIndexedObject& newObj = *reply.add_objects(); + newObj.set_hash(wantLedger->info().hash.data(), 256 / 8); + Serializer s(256); + s.add32(HashPrefix::ledgerMaster); addRaw(wantLedger->info(), s); - newObj.set_data (s.getDataPtr (), s.getLength ()); - newObj.set_ledgerseq (lSeq); + newObj.set_data(s.getDataPtr(), s.getLength()); + newObj.set_ledgerseq(lSeq); - wantLedger->stateMap().getFetchPack - (&haveLedger->stateMap(), true, 16384, - std::bind (fpAppender, &reply, lSeq, std::placeholders::_1, - std::placeholders::_2)); + wantLedger->stateMap().getFetchPack( + &haveLedger->stateMap(), + true, + 16384, + std::bind( + fpAppender, + &reply, + lSeq, + std::placeholders::_1, + std::placeholders::_2)); - if (wantLedger->info().txHash.isNonZero ()) - wantLedger->txMap().getFetchPack ( - nullptr, true, 512, - std::bind (fpAppender, &reply, lSeq, std::placeholders::_1, - std::placeholders::_2)); + if (wantLedger->info().txHash.isNonZero()) + wantLedger->txMap().getFetchPack( + nullptr, + true, + 512, + std::bind( + fpAppender, + &reply, + lSeq, + std::placeholders::_1, + std::placeholders::_2)); - if (reply.objects ().size () >= 512) + if (reply.objects().size() >= 512) break; // move may save a ref/unref - haveLedger = std::move (wantLedger); - wantLedger = getLedgerByHash (haveLedger->info().parentHash); - } - while (wantLedger && - UptimeClock::now() <= uptime + 1s); + haveLedger = std::move(wantLedger); + wantLedger = getLedgerByHash(haveLedger->info().parentHash); + } while (wantLedger && UptimeClock::now() <= uptime + 1s); JLOG(m_journal.info()) - << "Built fetch pack with " << reply.objects ().size () << " nodes"; - auto msg = std::make_shared (reply, protocol::mtGET_OBJECTS); - peer->send (msg); + << "Built fetch pack with " << reply.objects().size() << " nodes"; + auto msg = std::make_shared(reply, protocol::mtGET_OBJECTS); + peer->send(msg); } catch (std::exception const&) { @@ -2154,9 +2144,9 @@ LedgerMaster::makeFetchPack ( } std::size_t -LedgerMaster::getFetchPackCacheSize () const +LedgerMaster::getFetchPackCacheSize() const { - return fetch_packs_.getCacheSize (); + return fetch_packs_.getCacheSize(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/LedgerToJson.cpp b/src/ripple/app/ledger/impl/LedgerToJson.cpp index 711ae2ec91..c681f74061 100644 --- a/src/ripple/app/ledger/impl/LedgerToJson.cpp +++ b/src/ripple/app/ledger/impl/LedgerToJson.cpp @@ -29,74 +29,80 @@ namespace ripple { namespace { -bool isFull(LedgerFill const& fill) +bool +isFull(LedgerFill const& fill) { return fill.options & LedgerFill::full; } -bool isExpanded(LedgerFill const& fill) +bool +isExpanded(LedgerFill const& fill) { return isFull(fill) || (fill.options & LedgerFill::expand); } -bool isBinary(LedgerFill const& fill) +bool +isBinary(LedgerFill const& fill) { return fill.options & LedgerFill::binary; } template -void fillJson(Object& json, bool closed, LedgerInfo const& info, bool bFull) +void +fillJson(Object& json, bool closed, LedgerInfo const& info, bool bFull) { - json[jss::parent_hash] = to_string (info.parentHash); - json[jss::ledger_index] = to_string (info.seq); - json[jss::seqNum] = to_string (info.seq); // DEPRECATED + json[jss::parent_hash] = to_string(info.parentHash); + json[jss::ledger_index] = to_string(info.seq); + json[jss::seqNum] = to_string(info.seq); // DEPRECATED if (closed) { json[jss::closed] = true; } - else if (! bFull) + else if (!bFull) { json[jss::closed] = false; return; } - json[jss::ledger_hash] = to_string (info.hash); - json[jss::transaction_hash] = to_string (info.txHash); - json[jss::account_hash] = to_string (info.accountHash); - json[jss::total_coins] = to_string (info.drops); + json[jss::ledger_hash] = to_string(info.hash); + json[jss::transaction_hash] = to_string(info.txHash); + json[jss::account_hash] = to_string(info.accountHash); + json[jss::total_coins] = to_string(info.drops); // These next three are DEPRECATED. - json[jss::hash] = to_string (info.hash); - json[jss::totalCoins] = to_string (info.drops); + json[jss::hash] = to_string(info.hash); + json[jss::totalCoins] = to_string(info.drops); json[jss::accepted] = closed; json[jss::close_flags] = info.closeFlags; // Always show fields that contribute to the ledger hash - json[jss::parent_close_time] = info.parentCloseTime.time_since_epoch().count(); + json[jss::parent_close_time] = + info.parentCloseTime.time_since_epoch().count(); json[jss::close_time] = info.closeTime.time_since_epoch().count(); json[jss::close_time_resolution] = info.closeTimeResolution.count(); if (info.closeTime != NetClock::time_point{}) { json[jss::close_time_human] = to_string(info.closeTime); - if (! getCloseAgree(info)) + if (!getCloseAgree(info)) json[jss::close_time_estimated] = true; } } template -void fillJsonBinary(Object& json, bool closed, LedgerInfo const& info) +void +fillJsonBinary(Object& json, bool closed, LedgerInfo const& info) { - if (! closed) + if (!closed) json[jss::closed] = false; else { json[jss::closed] = true; Serializer s; - addRaw (info, s); - json[jss::ledger_data] = strHex (s.peekData ()); + addRaw(info, s); + json[jss::ledger_data] = strHex(s.peekData()); } } @@ -160,17 +166,19 @@ fillJsonTx( } template -void fillJsonTx (Object& json, LedgerFill const& fill) +void +fillJsonTx(Object& json, LedgerFill const& fill) { - auto&& txns = setArray (json, jss::transactions); + auto&& txns = setArray(json, jss::transactions); auto bBinary = isBinary(fill); auto bExpanded = isExpanded(fill); try { - for (auto& i: fill.ledger.txs) + for (auto& i : fill.ledger.txs) { - txns.append(fillJsonTx(fill, bBinary, bExpanded, i.first, i.second)); + txns.append( + fillJsonTx(fill, bBinary, bExpanded, i.first, i.second)); } } catch (std::exception const&) @@ -180,16 +188,17 @@ void fillJsonTx (Object& json, LedgerFill const& fill) } template -void fillJsonState(Object& json, LedgerFill const& fill) +void +fillJsonState(Object& json, LedgerFill const& fill) { auto& ledger = fill.ledger; - auto&& array = Json::setArray (json, jss::accountState); + auto&& array = Json::setArray(json, jss::accountState); auto expanded = isExpanded(fill); auto binary = isBinary(fill); - for(auto const& sle : ledger.sles) + for (auto const& sle : ledger.sles) { - if (fill.type == ltINVALID || sle->getType () == fill.type) + if (fill.type == ltINVALID || sle->getType() == fill.type) { if (binary) { @@ -206,7 +215,8 @@ void fillJsonState(Object& json, LedgerFill const& fill) } template -void fillJsonQueue(Object& json, LedgerFill const& fill) +void +fillJsonQueue(Object& json, LedgerFill const& fill) { auto&& queueData = Json::setArray(json, jss::queue_data); auto bBinary = isBinary(fill); @@ -220,13 +230,11 @@ void fillJsonQueue(Object& json, LedgerFill const& fill) txJson[jss::LastLedgerSequence] = *tx.lastValid; if (tx.consequences) { - txJson[jss::fee] = to_string( - tx.consequences->fee); - auto spend = tx.consequences->potentialSpend + - tx.consequences->fee; + txJson[jss::fee] = to_string(tx.consequences->fee); + auto spend = tx.consequences->potentialSpend + tx.consequences->fee; txJson[jss::max_spend_drops] = to_string(spend); - auto authChanged = tx.consequences->category == - TxConsequences::blocker; + auto authChanged = + tx.consequences->category == TxConsequences::blocker; txJson[jss::auth_change] = authChanged; } @@ -241,15 +249,16 @@ void fillJsonQueue(Object& json, LedgerFill const& fill) } template -void fillJson (Object& json, LedgerFill const& fill) +void +fillJson(Object& json, LedgerFill const& fill) { // TODO: what happens if bBinary and bExtracted are both set? // Is there a way to report this back? auto bFull = isFull(fill); if (isBinary(fill)) - fillJsonBinary(json, ! fill.ledger.open(), fill.ledger.info()); + fillJsonBinary(json, !fill.ledger.open(), fill.ledger.info()); else - fillJson(json, ! fill.ledger.open(), fill.ledger.info(), bFull); + fillJson(json, !fill.ledger.open(), fill.ledger.info(), bFull); if (bFull || fill.options & LedgerFill::dumpTxrp) fillJsonTx(json, fill); @@ -258,22 +267,24 @@ void fillJson (Object& json, LedgerFill const& fill) fillJsonState(json, fill); } -} // namespace +} // namespace -void addJson (Json::Value& json, LedgerFill const& fill) +void +addJson(Json::Value& json, LedgerFill const& fill) { - auto&& object = Json::addObject (json, jss::ledger); - fillJson (object, fill); + auto&& object = Json::addObject(json, jss::ledger); + fillJson(object, fill); if ((fill.options & LedgerFill::dumpQueue) && !fill.txQueue.empty()) fillJsonQueue(json, fill); } -Json::Value getJson (LedgerFill const& fill) +Json::Value +getJson(LedgerFill const& fill) { Json::Value json; - fillJson (json, fill); + fillJson(json, fill); return json; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/LocalTxs.cpp b/src/ripple/app/ledger/impl/LocalTxs.cpp index 2265559f61..d7b9cf5488 100644 --- a/src/ripple/app/ledger/impl/LocalTxs.cpp +++ b/src/ripple/app/ledger/impl/LocalTxs.cpp @@ -53,85 +53,90 @@ namespace ripple { class LocalTx { public: - // The number of ledgers to hold a transaction is essentially // arbitrary. It should be sufficient to allow the transaction to // get into a fully-validated ledger. static int const holdLedgers = 5; - LocalTx (LedgerIndex index, std::shared_ptr const& txn) - : m_txn (txn) - , m_expire (index + holdLedgers) - , m_id (txn->getTransactionID ()) - , m_account (txn->getAccountID(sfAccount)) - , m_seq (txn->getSequence()) + LocalTx(LedgerIndex index, std::shared_ptr const& txn) + : m_txn(txn) + , m_expire(index + holdLedgers) + , m_id(txn->getTransactionID()) + , m_account(txn->getAccountID(sfAccount)) + , m_seq(txn->getSequence()) { - if (txn->isFieldPresent (sfLastLedgerSequence)) - m_expire = std::min (m_expire, txn->getFieldU32 (sfLastLedgerSequence) + 1); + if (txn->isFieldPresent(sfLastLedgerSequence)) + m_expire = + std::min(m_expire, txn->getFieldU32(sfLastLedgerSequence) + 1); } - uint256 const& getID () const + uint256 const& + getID() const { return m_id; } - std::uint32_t getSeq () const + std::uint32_t + getSeq() const { return m_seq; } - bool isExpired (LedgerIndex i) const + bool + isExpired(LedgerIndex i) const { return i > m_expire; } - std::shared_ptr const& getTX () const + std::shared_ptr const& + getTX() const { return m_txn; } - AccountID const& getAccount () const + AccountID const& + getAccount() const { return m_account; } private: - std::shared_ptr m_txn; - LedgerIndex m_expire; - uint256 m_id; - AccountID m_account; - std::uint32_t m_seq; + LedgerIndex m_expire; + uint256 m_id; + AccountID m_account; + std::uint32_t m_seq; }; //------------------------------------------------------------------------------ -class LocalTxsImp - : public LocalTxs +class LocalTxsImp : public LocalTxs { public: LocalTxsImp() = default; // Add a new transaction to the set of local transactions - void push_back (LedgerIndex index, std::shared_ptr const& txn) override + void + push_back(LedgerIndex index, std::shared_ptr const& txn) + override { - std::lock_guard lock (m_lock); + std::lock_guard lock(m_lock); - m_txns.emplace_back (index, txn); + m_txns.emplace_back(index, txn); } CanonicalTXSet - getTxSet () override + getTxSet() override { - CanonicalTXSet tset (uint256 {}); + CanonicalTXSet tset(uint256{}); // Get the set of local transactions as a canonical // set (so they apply in a valid order) { - std::lock_guard lock (m_lock); + std::lock_guard lock(m_lock); for (auto const& it : m_txns) - tset.insert (it.getTX()); + tset.insert(it.getTX()); } return tset; @@ -140,41 +145,42 @@ public: // Remove transactions that have either been accepted // into a fully-validated ledger, are (now) impossible, // or have expired - void sweep (ReadView const& view) override + void + sweep(ReadView const& view) override { - std::lock_guard lock (m_lock); + std::lock_guard lock(m_lock); - m_txns.remove_if ([&view](auto const& txn) - { - if (txn.isExpired (view.info().seq)) + m_txns.remove_if([&view](auto const& txn) { + if (txn.isExpired(view.info().seq)) return true; if (view.txExists(txn.getID())) return true; - std::shared_ptr sle = view.read( - keylet::account(txn.getAccount())); - if (! sle) + std::shared_ptr sle = + view.read(keylet::account(txn.getAccount())); + if (!sle) return false; - return sle->getFieldU32 (sfSequence) > txn.getSeq (); + return sle->getFieldU32(sfSequence) > txn.getSeq(); }); } - std::size_t size () override + std::size_t + size() override { - std::lock_guard lock (m_lock); + std::lock_guard lock(m_lock); - return m_txns.size (); + return m_txns.size(); } private: std::mutex m_lock; - std::list m_txns; + std::list m_txns; }; std::unique_ptr -make_LocalTxs () +make_LocalTxs() { - return std::make_unique (); + return std::make_unique(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/OpenLedger.cpp b/src/ripple/app/ledger/impl/OpenLedger.cpp index 92cd053f66..9f20e50f2e 100644 --- a/src/ripple/app/ledger/impl/OpenLedger.cpp +++ b/src/ripple/app/ledger/impl/OpenLedger.cpp @@ -31,13 +31,11 @@ namespace ripple { -OpenLedger::OpenLedger(std::shared_ptr< - Ledger const> const& ledger, - CachedSLEs& cache, - beast::Journal journal) - : j_ (journal) - , cache_ (cache) - , current_ (create(ledger->rules(), ledger)) +OpenLedger::OpenLedger( + std::shared_ptr const& ledger, + CachedSLEs& cache, + beast::Journal journal) + : j_(journal), cache_(cache), current_(create(ledger->rules(), ledger)) { } @@ -56,11 +54,10 @@ OpenLedger::current() const } bool -OpenLedger::modify (modify_type const& f) +OpenLedger::modify(modify_type const& f) { std::lock_guard lock1(modify_mutex_); - auto next = std::make_shared< - OpenView>(*current_); + auto next = std::make_shared(*current_); auto const changed = f(*next, j_); if (changed) { @@ -71,15 +68,18 @@ OpenLedger::modify (modify_type const& f) } void -OpenLedger::accept(Application& app, Rules const& rules, +OpenLedger::accept( + Application& app, + Rules const& rules, std::shared_ptr const& ledger, - OrderedTxs const& locals, bool retriesFirst, - OrderedTxs& retries, ApplyFlags flags, - std::string const& suffix, - modify_type const& f) + OrderedTxs const& locals, + bool retriesFirst, + OrderedTxs& retries, + ApplyFlags flags, + std::string const& suffix, + modify_type const& f) { - JLOG(j_.trace()) << - "accept ledger " << ledger->seq() << " " << suffix; + JLOG(j_.trace()) << "accept ledger " << ledger->seq() << " " << suffix; auto next = create(rules, ledger); std::map shouldRecover; if (retriesFirst) @@ -90,49 +90,49 @@ OpenLedger::accept(Application& app, Rules const& rules, shouldRecover[txID] = app.getHashRouter().shouldRecover(txID); } // Handle disputed tx, outside lock - using empty = - std::vector>; - apply (app, *next, *ledger, empty{}, - retries, flags, shouldRecover, j_); + using empty = std::vector>; + apply(app, *next, *ledger, empty{}, retries, flags, shouldRecover, j_); } // Block calls to modify, otherwise // new tx going into the open ledger // would get lost. std::lock_guard lock1(modify_mutex_); // Apply tx from the current open view - if (! current_->txs.empty()) + if (!current_->txs.empty()) { for (auto const& tx : current_->txs) { auto const txID = tx.first->getTransactionID(); auto iter = shouldRecover.lower_bound(txID); - if (iter != shouldRecover.end() - && iter->first == txID) + if (iter != shouldRecover.end() && iter->first == txID) // already had a chance via disputes iter->second = false; else - shouldRecover.emplace_hint(iter, txID, - app.getHashRouter().shouldRecover(txID)); + shouldRecover.emplace_hint( + iter, txID, app.getHashRouter().shouldRecover(txID)); } - apply (app, *next, *ledger, + apply( + app, + *next, + *ledger, boost::adaptors::transform( current_->txs, - [](std::pair, std::shared_ptr< - STObject const>> const& p) - { - return p.first; - }), - retries, flags, shouldRecover, j_); + [](std::pair< + std::shared_ptr, + std::shared_ptr> const& p) { + return p.first; + }), + retries, + flags, + shouldRecover, + j_); } // Call the modifier if (f) f(*next, j_); // Apply local tx for (auto const& item : locals) - app.getTxQ().apply(app, *next, - item.second, flags, j_); + app.getTxQ().apply(app, *next, item.second, flags, j_); // If we didn't relay this transaction recently, relay it to all peers for (auto const& txpair : next->txs) @@ -150,7 +150,7 @@ OpenLedger::accept(Application& app, Rules const& rules, msg.set_status(protocol::tsNEW); msg.set_receivetimestamp( app.timeKeeper().now().time_since_epoch().count()); - app.overlay().foreach(send_if_not( + app.overlay().foreach (send_if_not( std::make_shared(msg, protocol::mtTRANSACTION), peer_in_set(*toSkip))); } @@ -164,27 +164,31 @@ OpenLedger::accept(Application& app, Rules const& rules, //------------------------------------------------------------------------------ std::shared_ptr -OpenLedger::create (Rules const& rules, +OpenLedger::create( + Rules const& rules, std::shared_ptr const& ledger) { return std::make_shared( - open_ledger, rules, std::make_shared< - CachedLedger const>(ledger, - cache_)); + open_ledger, + rules, + std::make_shared(ledger, cache_)); } auto -OpenLedger::apply_one (Application& app, OpenView& view, +OpenLedger::apply_one( + Application& app, + OpenView& view, std::shared_ptr const& tx, - bool retry, ApplyFlags flags, bool shouldRecover, - beast::Journal j) -> Result + bool retry, + ApplyFlags flags, + bool shouldRecover, + beast::Journal j) -> Result { if (retry) flags = flags | tapRETRY; - auto const result = [&] - { - auto const queueResult = app.getTxQ().apply( - app, view, tx, flags | tapPREFER_QUEUE, j); + auto const result = [&] { + auto const queueResult = + app.getTxQ().apply(app, view, tx, flags | tapPREFER_QUEUE, j); // If the transaction can't get into the queue for intrinsic // reasons, and it can still be recovered, try to put it // directly into the open ledger, else drop it. @@ -192,12 +196,10 @@ OpenLedger::apply_one (Application& app, OpenView& view, return ripple::apply(app, view, *tx, flags, j); return queueResult; }(); - if (result.second || - result.first == terQUEUED) + if (result.second || result.first == terQUEUED) return Result::success; - if (isTefFailure (result.first) || - isTemMalformed (result.first) || - isTelLocal (result.first)) + if (isTefFailure(result.first) || isTemMalformed(result.first) || + isTelLocal(result.first)) return Result::failure; return Result::retry; } @@ -205,7 +207,7 @@ OpenLedger::apply_one (Application& app, OpenView& view, //------------------------------------------------------------------------------ std::string -debugTxstr (std::shared_ptr const& tx) +debugTxstr(std::shared_ptr const& tx) { std::stringstream ss; ss << tx->getTransactionID(); @@ -213,16 +215,16 @@ debugTxstr (std::shared_ptr const& tx) } std::string -debugTostr (OrderedTxs const& set) +debugTostr(OrderedTxs const& set) { std::stringstream ss; - for(auto const& item : set) + for (auto const& item : set) ss << debugTxstr(item.second) << ", "; return ss.str(); } std::string -debugTostr (SHAMap const& set) +debugTostr(SHAMap const& set) { std::stringstream ss; for (auto const& item : set) @@ -230,11 +232,10 @@ debugTostr (SHAMap const& set) try { SerialIter sit(item.slice()); - auto const tx = std::make_shared< - STTx const>(sit); + auto const tx = std::make_shared(sit); ss << debugTxstr(tx) << ", "; } - catch(std::exception const&) + catch (std::exception const&) { ss << "THRO, "; } @@ -243,12 +244,12 @@ debugTostr (SHAMap const& set) } std::string -debugTostr (std::shared_ptr const& view) +debugTostr(std::shared_ptr const& view) { std::stringstream ss; - for(auto const& item : view->txs) + for (auto const& item : view->txs) ss << debugTxstr(item.first) << ", "; return ss.str(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/TransactionAcquire.cpp b/src/ripple/app/ledger/impl/TransactionAcquire.cpp index a650bc9398..cf2d7e0a84 100644 --- a/src/ripple/app/ledger/impl/TransactionAcquire.cpp +++ b/src/ripple/app/ledger/impl/TransactionAcquire.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include #include #include #include +#include #include #include #include @@ -34,235 +34,253 @@ using namespace std::chrono_literals; // Timeout interval in milliseconds auto constexpr TX_ACQUIRE_TIMEOUT = 250ms; -enum -{ +enum { NORM_TIMEOUTS = 4, MAX_TIMEOUTS = 20, }; -TransactionAcquire::TransactionAcquire (Application& app, uint256 const& hash, clock_type& clock) - : PeerSet (app, hash, TX_ACQUIRE_TIMEOUT, clock, - app.journal("TransactionAcquire")) - , mHaveRoot (false) +TransactionAcquire::TransactionAcquire( + Application& app, + uint256 const& hash, + clock_type& clock) + : PeerSet( + app, + hash, + TX_ACQUIRE_TIMEOUT, + clock, + app.journal("TransactionAcquire")) + , mHaveRoot(false) , j_(app.journal("TransactionAcquire")) { - mMap = std::make_shared (SHAMapType::TRANSACTION, hash, - app_.family()); - mMap->setUnbacked (); + mMap = + std::make_shared(SHAMapType::TRANSACTION, hash, app_.family()); + mMap->setUnbacked(); } -void TransactionAcquire::execute () +void +TransactionAcquire::execute() { - app_.getJobQueue ().addJob ( - jtTXN_DATA, "TransactionAcquire", - [ptr = shared_from_this()](Job&) - { - ptr->invokeOnTimer (); + app_.getJobQueue().addJob( + jtTXN_DATA, "TransactionAcquire", [ptr = shared_from_this()](Job&) { + ptr->invokeOnTimer(); }); } -void TransactionAcquire::done () +void +TransactionAcquire::done() { // We hold a PeerSet lock and so cannot do real work here if (mFailed) { - JLOG (j_.warn()) << "Failed to acquire TX set " << mHash; + JLOG(j_.warn()) << "Failed to acquire TX set " << mHash; } else { - JLOG (j_.debug()) << "Acquired TX set " << mHash; - mMap->setImmutable (); + JLOG(j_.debug()) << "Acquired TX set " << mHash; + mMap->setImmutable(); - uint256 const& hash (mHash); - std::shared_ptr const& map (mMap); + uint256 const& hash(mHash); + std::shared_ptr const& map(mMap); auto const pap = &app_; // Note that, when we're in the process of shutting down, addJob() // may reject the request. If that happens then giveSet() will // not be called. That's fine. According to David the giveSet() call // just updates the consensus and related structures when we acquire // a transaction set. No need to update them if we're shutting down. - app_.getJobQueue().addJob (jtTXN_DATA, "completeAcquire", - [pap, hash, map](Job&) - { - pap->getInboundTransactions().giveSet ( - hash, map, true); + app_.getJobQueue().addJob( + jtTXN_DATA, "completeAcquire", [pap, hash, map](Job&) { + pap->getInboundTransactions().giveSet(hash, map, true); }); } - } -void TransactionAcquire::onTimer (bool progress, ScopedLockType& psl) +void +TransactionAcquire::onTimer(bool progress, ScopedLockType& psl) { bool aggressive = false; - if (getTimeouts () >= NORM_TIMEOUTS) + if (getTimeouts() >= NORM_TIMEOUTS) { aggressive = true; - if (getTimeouts () > MAX_TIMEOUTS) + if (getTimeouts() > MAX_TIMEOUTS) { mFailed = true; - done (); + done(); return; } } if (aggressive) - trigger (nullptr); + trigger(nullptr); - addPeers (1); + addPeers(1); } -std::weak_ptr TransactionAcquire::pmDowncast () +std::weak_ptr +TransactionAcquire::pmDowncast() { - return std::dynamic_pointer_cast (shared_from_this ()); + return std::dynamic_pointer_cast(shared_from_this()); } -void TransactionAcquire::trigger (std::shared_ptr const& peer) +void +TransactionAcquire::trigger(std::shared_ptr const& peer) { if (mComplete) { - JLOG (j_.info()) << "trigger after complete"; + JLOG(j_.info()) << "trigger after complete"; return; } if (mFailed) { - JLOG (j_.info()) << "trigger after fail"; + JLOG(j_.info()) << "trigger after fail"; return; } if (!mHaveRoot) { - JLOG (j_.trace()) << "TransactionAcquire::trigger " << (peer ? "havePeer" : "noPeer") << " no root"; + JLOG(j_.trace()) << "TransactionAcquire::trigger " + << (peer ? "havePeer" : "noPeer") << " no root"; protocol::TMGetLedger tmGL; - tmGL.set_ledgerhash (mHash.begin (), mHash.size ()); - tmGL.set_itype (protocol::liTS_CANDIDATE); - tmGL.set_querydepth (3); // We probably need the whole thing + tmGL.set_ledgerhash(mHash.begin(), mHash.size()); + tmGL.set_itype(protocol::liTS_CANDIDATE); + tmGL.set_querydepth(3); // We probably need the whole thing - if (getTimeouts () != 0) - tmGL.set_querytype (protocol::qtINDIRECT); + if (getTimeouts() != 0) + tmGL.set_querytype(protocol::qtINDIRECT); - * (tmGL.add_nodeids ()) = SHAMapNodeID ().getRawString (); - sendRequest (tmGL, peer); + *(tmGL.add_nodeids()) = SHAMapNodeID().getRawString(); + sendRequest(tmGL, peer); } - else if (!mMap->isValid ()) + else if (!mMap->isValid()) { mFailed = true; - done (); + done(); } else { - ConsensusTransSetSF sf (app_, app_.getTempNodeCache ()); - auto nodes = mMap->getMissingNodes (256, &sf); + ConsensusTransSetSF sf(app_, app_.getTempNodeCache()); + auto nodes = mMap->getMissingNodes(256, &sf); - if (nodes.empty ()) + if (nodes.empty()) { - if (mMap->isValid ()) + if (mMap->isValid()) mComplete = true; else mFailed = true; - done (); + done(); return; } protocol::TMGetLedger tmGL; - tmGL.set_ledgerhash (mHash.begin (), mHash.size ()); - tmGL.set_itype (protocol::liTS_CANDIDATE); + tmGL.set_ledgerhash(mHash.begin(), mHash.size()); + tmGL.set_itype(protocol::liTS_CANDIDATE); - if (getTimeouts () != 0) - tmGL.set_querytype (protocol::qtINDIRECT); + if (getTimeouts() != 0) + tmGL.set_querytype(protocol::qtINDIRECT); for (auto const& node : nodes) { - *tmGL.add_nodeids () = node.first.getRawString (); + *tmGL.add_nodeids() = node.first.getRawString(); } - sendRequest (tmGL, peer); + sendRequest(tmGL, peer); } } -SHAMapAddNode TransactionAcquire::takeNodes (const std::list& nodeIDs, - const std::list< Blob >& data, std::shared_ptr const& peer) +SHAMapAddNode +TransactionAcquire::takeNodes( + const std::list& nodeIDs, + const std::list& data, + std::shared_ptr const& peer) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); if (mComplete) { - JLOG (j_.trace()) << "TX set complete"; - return SHAMapAddNode (); + JLOG(j_.trace()) << "TX set complete"; + return SHAMapAddNode(); } if (mFailed) { - JLOG (j_.trace()) << "TX set failed"; - return SHAMapAddNode (); + JLOG(j_.trace()) << "TX set failed"; + return SHAMapAddNode(); } try { - if (nodeIDs.empty ()) - return SHAMapAddNode::invalid (); + if (nodeIDs.empty()) + return SHAMapAddNode::invalid(); - std::list::const_iterator nodeIDit = nodeIDs.begin (); - std::list< Blob >::const_iterator nodeDatait = data.begin (); - ConsensusTransSetSF sf (app_, app_.getTempNodeCache ()); + std::list::const_iterator nodeIDit = nodeIDs.begin(); + std::list::const_iterator nodeDatait = data.begin(); + ConsensusTransSetSF sf(app_, app_.getTempNodeCache()); - while (nodeIDit != nodeIDs.end ()) + while (nodeIDit != nodeIDs.end()) { - if (nodeIDit->isRoot ()) + if (nodeIDit->isRoot()) { if (mHaveRoot) - JLOG (j_.debug()) << "Got root TXS node, already have it"; - else if (!mMap->addRootNode (SHAMapHash{getHash ()}, - makeSlice(*nodeDatait), snfWIRE, nullptr).isGood()) + JLOG(j_.debug()) << "Got root TXS node, already have it"; + else if (!mMap->addRootNode( + SHAMapHash{getHash()}, + makeSlice(*nodeDatait), + snfWIRE, + nullptr) + .isGood()) { - JLOG (j_.warn()) << "TX acquire got bad root node"; + JLOG(j_.warn()) << "TX acquire got bad root node"; } else mHaveRoot = true; } - else if (!mMap->addKnownNode (*nodeIDit, makeSlice(*nodeDatait), &sf).isGood()) + else if (!mMap->addKnownNode(*nodeIDit, makeSlice(*nodeDatait), &sf) + .isGood()) { - JLOG (j_.warn()) << "TX acquire got bad non-root node"; - return SHAMapAddNode::invalid (); + JLOG(j_.warn()) << "TX acquire got bad non-root node"; + return SHAMapAddNode::invalid(); } ++nodeIDit; ++nodeDatait; } - trigger (peer); - progress (); - return SHAMapAddNode::useful (); + trigger(peer); + progress(); + return SHAMapAddNode::useful(); } catch (std::exception const&) { - JLOG (j_.error()) << "Peer sends us junky transaction node data"; - return SHAMapAddNode::invalid (); + JLOG(j_.error()) << "Peer sends us junky transaction node data"; + return SHAMapAddNode::invalid(); } } -void TransactionAcquire::addPeers (int numPeers) +void +TransactionAcquire::addPeers(int numPeers) { - app_.overlay().selectPeers (*this, numPeers, ScoreHasTxSet (getHash())); + app_.overlay().selectPeers(*this, numPeers, ScoreHasTxSet(getHash())); } -void TransactionAcquire::init (int numPeers) +void +TransactionAcquire::init(int numPeers) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - addPeers (numPeers); + addPeers(numPeers); - setTimer (); + setTimer(); } -void TransactionAcquire::stillNeed () +void +TransactionAcquire::stillNeed() { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); if (mTimeouts > NORM_TIMEOUTS) mTimeouts = NORM_TIMEOUTS; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/ledger/impl/TransactionAcquire.h b/src/ripple/app/ledger/impl/TransactionAcquire.h index 81e03c9a5b..9ac06a067b 100644 --- a/src/ripple/app/ledger/impl/TransactionAcquire.h +++ b/src/ripple/app/ledger/impl/TransactionAcquire.h @@ -29,56 +29,74 @@ namespace ripple { // VFALCO TODO rename to PeerTxRequest // A transaction set we are trying to acquire class TransactionAcquire - : public PeerSet - , public std::enable_shared_from_this - , public CountedObject + : public PeerSet, + public std::enable_shared_from_this, + public CountedObject { public: - static char const* getCountedObjectName () { return "TransactionAcquire"; } + static char const* + getCountedObjectName() + { + return "TransactionAcquire"; + } using pointer = std::shared_ptr; public: - TransactionAcquire (Application& app, uint256 const& hash, clock_type& clock); - ~TransactionAcquire () = default; + TransactionAcquire( + Application& app, + uint256 const& hash, + clock_type& clock); + ~TransactionAcquire() = default; - std::shared_ptr const& getMap () + std::shared_ptr const& + getMap() { return mMap; } - SHAMapAddNode takeNodes (const std::list& IDs, - const std::list< Blob >& data, std::shared_ptr const&); + SHAMapAddNode + takeNodes( + const std::list& IDs, + const std::list& data, + std::shared_ptr const&); - void init (int startPeers); + void + init(int startPeers); - void stillNeed (); + void + stillNeed(); private: - std::shared_ptr mMap; - bool mHaveRoot; - beast::Journal j_; + bool mHaveRoot; + beast::Journal j_; - void execute () override; + void + execute() override; - void onTimer (bool progress, ScopedLockType& peerSetLock) override; + void + onTimer(bool progress, ScopedLockType& peerSetLock) override; - - void newPeer (std::shared_ptr const& peer) override + void + newPeer(std::shared_ptr const& peer) override { - trigger (peer); + trigger(peer); } - void done (); + void + done(); // Tries to add the specified number of peers - void addPeers (int num); + void + addPeers(int num); - void trigger (std::shared_ptr const&); - std::weak_ptr pmDowncast () override; + void + trigger(std::shared_ptr const&); + std::weak_ptr + pmDowncast() override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/ledger/impl/TransactionMaster.cpp b/src/ripple/app/ledger/impl/TransactionMaster.cpp index 0bfc250070..7138a3cd9e 100644 --- a/src/ripple/app/ledger/impl/TransactionMaster.cpp +++ b/src/ripple/app/ledger/impl/TransactionMaster.cpp @@ -18,46 +18,51 @@ //============================================================================== #include -#include #include -#include +#include #include +#include namespace ripple { -TransactionMaster::TransactionMaster (Application& app) - : mApp (app) - , mCache ("TransactionCache", 65536, std::chrono::minutes {30}, stopwatch(), - mApp.journal("TaggedCache")) +TransactionMaster::TransactionMaster(Application& app) + : mApp(app) + , mCache( + "TransactionCache", + 65536, + std::chrono::minutes{30}, + stopwatch(), + mApp.journal("TaggedCache")) { } -bool TransactionMaster::inLedger (uint256 const& hash, std::uint32_t ledger) +bool +TransactionMaster::inLedger(uint256 const& hash, std::uint32_t ledger) { - auto txn = mCache.fetch (hash); + auto txn = mCache.fetch(hash); if (!txn) return false; - txn->setStatus (COMMITTED, ledger); + txn->setStatus(COMMITTED, ledger); return true; } std::shared_ptr -TransactionMaster::fetch_from_cache (uint256 const& txnID) +TransactionMaster::fetch_from_cache(uint256 const& txnID) { - return mCache.fetch (txnID); + return mCache.fetch(txnID); } std::shared_ptr -TransactionMaster::fetch (uint256 const& txnID, error_code_i& ec) +TransactionMaster::fetch(uint256 const& txnID, error_code_i& ec) { - auto txn = fetch_from_cache (txnID); + auto txn = fetch_from_cache(txnID); if (txn) return txn; - txn = Transaction::load (txnID, mApp, ec); + txn = Transaction::load(txnID, mApp, ec); if (!txn) return txn; @@ -68,52 +73,56 @@ TransactionMaster::fetch (uint256 const& txnID, error_code_i& ec) } boost::variant -TransactionMaster::fetch (uint256 const& txnID, ClosedInterval const& range, +TransactionMaster::fetch( + uint256 const& txnID, + ClosedInterval const& range, error_code_i& ec) { using pointer = Transaction::pointer; - auto txn = mCache.fetch (txnID); + auto txn = mCache.fetch(txnID); if (txn) return txn; - boost::variant v = Transaction::load ( - txnID, mApp, range, ec); + boost::variant v = + Transaction::load(txnID, mApp, range, ec); - if (v.which () == 0 && boost::get (v)) - mCache.canonicalize_replace_client(txnID, boost::get (v)); + if (v.which() == 0 && boost::get(v)) + mCache.canonicalize_replace_client(txnID, boost::get(v)); return v; } std::shared_ptr -TransactionMaster::fetch (std::shared_ptr const& item, - SHAMapTreeNode::TNType type, std::uint32_t uCommitLedger) +TransactionMaster::fetch( + std::shared_ptr const& item, + SHAMapTreeNode::TNType type, + std::uint32_t uCommitLedger) { - std::shared_ptr txn; - auto iTx = fetch_from_cache (item->key()); + std::shared_ptr txn; + auto iTx = fetch_from_cache(item->key()); if (!iTx) { - if (type == SHAMapTreeNode::tnTRANSACTION_NM) { - SerialIter sit (item->slice()); - txn = std::make_shared (std::ref (sit)); + SerialIter sit(item->slice()); + txn = std::make_shared(std::ref(sit)); } else if (type == SHAMapTreeNode::tnTRANSACTION_MD) { auto blob = SerialIter{item->data(), item->size()}.getVL(); - txn = std::make_shared(SerialIter{blob.data(), blob.size()}); + txn = std::make_shared( + SerialIter{blob.data(), blob.size()}); } } else { if (uCommitLedger) - iTx->setStatus (COMMITTED, uCommitLedger); + iTx->setStatus(COMMITTED, uCommitLedger); - txn = iTx->getSTransaction (); + txn = iTx->getSTransaction(); } return txn; @@ -132,14 +141,16 @@ TransactionMaster::canonicalize(std::shared_ptr* pTransaction) } } -void TransactionMaster::sweep (void) +void +TransactionMaster::sweep(void) { - mCache.sweep (); + mCache.sweep(); } -TaggedCache & TransactionMaster::getCache() +TaggedCache& +TransactionMaster::getCache() { return mCache; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 61b4cc0ca5..3b8a4ad902 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -17,57 +17,57 @@ */ //============================================================================== -#include -#include -#include #include -#include -#include -#include #include +#include #include #include #include #include #include -#include #include +#include +#include +#include #include #include #include +#include #include #include #include #include #include #include -#include #include +#include #include #include #include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include #include -#include #include +#include #include #include #include #include #include -#include #include +#include #include -#include -#include -#include #include +#include -#include #include +#include #include #include @@ -81,7 +81,7 @@ namespace ripple { // 204/256 about 80% -static int const MAJORITY_FRACTION (204); +static int const MAJORITY_FRACTION(204); //------------------------------------------------------------------------------ @@ -101,40 +101,48 @@ private: LedgerIndex maxSeq = 0; std::mutex maxSeqLock; - void acquire ( - uint256 const& hash, - std::uint32_t seq) + void + acquire(uint256 const& hash, std::uint32_t seq) { if (hash.isNonZero()) { - auto j = app_.journal ("Ledger"); + auto j = app_.journal("Ledger"); - JLOG (j.error()) << - "Missing node in " << to_string (hash); + JLOG(j.error()) << "Missing node in " << to_string(hash); - app_.getInboundLedgers ().acquire ( - hash, seq, shardBacked_ ? - InboundLedger::Reason::SHARD : - InboundLedger::Reason::GENERIC); + app_.getInboundLedgers().acquire( + hash, + seq, + shardBacked_ ? InboundLedger::Reason::SHARD + : InboundLedger::Reason::GENERIC); } } public: - AppFamily (AppFamily const&) = delete; - AppFamily& operator= (AppFamily const&) = delete; + AppFamily(AppFamily const&) = delete; + AppFamily& + operator=(AppFamily const&) = delete; - AppFamily (Application& app, NodeStore::Database& db, - CollectorManager& collectorManager) - : app_ (app) - , treecache_ ("TreeNodeCache", 65536, std::chrono::minutes {1}, - stopwatch(), app.journal("TaggedCache")) - , fullbelow_ ("full_below", stopwatch(), - collectorManager.collector(), - fullBelowTargetSize, fullBelowExpiration) - , db_ (db) - , shardBacked_ ( - dynamic_cast(&db) != nullptr) - , j_ (app.journal("SHAMap")) + AppFamily( + Application& app, + NodeStore::Database& db, + CollectorManager& collectorManager) + : app_(app) + , treecache_( + "TreeNodeCache", + 65536, + std::chrono::minutes{1}, + stopwatch(), + app.journal("TaggedCache")) + , fullbelow_( + "full_below", + stopwatch(), + collectorManager.collector(), + fullBelowTargetSize, + fullBelowExpiration) + , db_(db) + , shardBacked_(dynamic_cast(&db) != nullptr) + , j_(app.journal("SHAMap")) { } @@ -187,15 +195,14 @@ public: } void - missing_node (std::uint32_t seq) override + missing_node(std::uint32_t seq) override { - auto j = app_.journal ("Ledger"); + auto j = app_.journal("Ledger"); - JLOG (j.error()) << - "Missing node in " << seq; + JLOG(j.error()) << "Missing node in " << seq; // prevent recursive invocation - std::unique_lock lock (maxSeqLock); + std::unique_lock lock(maxSeqLock); if (maxSeq == 0) { @@ -209,13 +216,10 @@ public: lock.unlock(); // This can invoke the missing node handler - acquire ( - app_.getLedgerMaster().getHashBySeq (seq), - seq); + acquire(app_.getLedgerMaster().getHashBySeq(seq), seq); lock.lock(); - } - while (maxSeq != seq); + } while (maxSeq != seq); } else if (maxSeq < seq) { @@ -226,13 +230,13 @@ public: } void - missing_node (uint256 const& hash, std::uint32_t seq) override + missing_node(uint256 const& hash, std::uint32_t seq) override { - acquire (hash, seq); + acquire(hash, seq); } void - reset () override + reset() override { { std::lock_guard lock(maxSeqLock); @@ -243,15 +247,12 @@ public: } }; -} // detail +} // namespace detail //------------------------------------------------------------------------------ // VFALCO TODO Move the function definitions into the class declaration -class ApplicationImp - : public Application - , public RootStoppable - , public BasicApp +class ApplicationImp : public Application, public RootStoppable, public BasicApp { private: class io_latency_sampler @@ -259,30 +260,31 @@ private: private: beast::insight::Event m_event; beast::Journal m_journal; - beast::io_latency_probe m_probe; + beast::io_latency_probe m_probe; std::atomic lastSample_; public: - io_latency_sampler ( + io_latency_sampler( beast::insight::Event ev, beast::Journal journal, std::chrono::milliseconds interval, boost::asio::io_service& ios) - : m_event (ev) - , m_journal (journal) - , m_probe (interval, ios) - , lastSample_ {} + : m_event(ev) + , m_journal(journal) + , m_probe(interval, ios) + , lastSample_{} { } void start() { - m_probe.sample (std::ref(*this)); + m_probe.sample(std::ref(*this)); } template - void operator() (Duration const& elapsed) + void + operator()(Duration const& elapsed) { using namespace std::chrono; auto const lastSample = date::ceil(elapsed); @@ -290,29 +292,30 @@ private: lastSample_ = lastSample; if (lastSample >= 10ms) - m_event.notify (lastSample); + m_event.notify(lastSample); if (lastSample >= 500ms) { - JLOG(m_journal.warn()) << - "io_service latency = " << lastSample.count(); + JLOG(m_journal.warn()) + << "io_service latency = " << lastSample.count(); } } std::chrono::milliseconds - get () const + get() const { return lastSample_.load(); } void - cancel () + cancel() { - m_probe.cancel (); + m_probe.cancel(); } - void cancel_async () + void + cancel_async() { - m_probe.cancel_async (); + m_probe.cancel_async(); } }; @@ -329,57 +332,57 @@ public: TransactionMaster m_txMaster; NodeStoreScheduler m_nodeStoreScheduler; - std::unique_ptr m_shaMapStore; + std::unique_ptr m_shaMapStore; PendingSaves pendingSaves_; AccountIDCache accountIDCache_; boost::optional openLedger_; // These are not Stoppable-derived NodeCache m_tempNodeCache; - std::unique_ptr m_collectorManager; + std::unique_ptr m_collectorManager; CachedSLEs cachedSLEs_; std::pair nodeIdentity_; ValidatorKeys const validatorKeys_; - std::unique_ptr m_resourceManager; + std::unique_ptr m_resourceManager; // These are Stoppable-related - std::unique_ptr m_jobQueue; - std::unique_ptr m_nodeStore; + std::unique_ptr m_jobQueue; + std::unique_ptr m_nodeStore; detail::AppFamily family_; - std::unique_ptr shardStore_; - std::unique_ptr shardFamily_; + std::unique_ptr shardStore_; + std::unique_ptr shardFamily_; // VFALCO TODO Make OrderBookDB abstract OrderBookDB m_orderBookDB; - std::unique_ptr m_pathRequests; - std::unique_ptr m_ledgerMaster; - std::unique_ptr m_inboundLedgers; - std::unique_ptr m_inboundTransactions; - TaggedCache m_acceptedLedgerCache; - std::unique_ptr m_networkOPs; - std::unique_ptr cluster_; - std::unique_ptr peerReservations_; - std::unique_ptr validatorManifests_; - std::unique_ptr publisherManifests_; - std::unique_ptr validators_; - std::unique_ptr validatorSites_; - std::unique_ptr serverHandler_; - std::unique_ptr m_amendmentTable; - std::unique_ptr mFeeTrack; - std::unique_ptr hashRouter_; + std::unique_ptr m_pathRequests; + std::unique_ptr m_ledgerMaster; + std::unique_ptr m_inboundLedgers; + std::unique_ptr m_inboundTransactions; + TaggedCache m_acceptedLedgerCache; + std::unique_ptr m_networkOPs; + std::unique_ptr cluster_; + std::unique_ptr peerReservations_; + std::unique_ptr validatorManifests_; + std::unique_ptr publisherManifests_; + std::unique_ptr validators_; + std::unique_ptr validatorSites_; + std::unique_ptr serverHandler_; + std::unique_ptr m_amendmentTable; + std::unique_ptr mFeeTrack; + std::unique_ptr hashRouter_; RCLValidations mValidations; - std::unique_ptr m_loadManager; - std::unique_ptr txQ_; + std::unique_ptr m_loadManager; + std::unique_ptr txQ_; ClosureCounter waitHandlerCounter_; boost::asio::steady_timer sweepTimer_; boost::asio::steady_timer entropyTimer_; bool startTimers_; - std::unique_ptr mTxnDB; - std::unique_ptr mLedgerDB; - std::unique_ptr mWalletDB; - std::unique_ptr overlay_; - std::vector > websocketServers_; + std::unique_ptr mTxnDB; + std::unique_ptr mLedgerDB; + std::unique_ptr mWalletDB; + std::unique_ptr overlay_; + std::vector> websocketServers_; boost::asio::signal_set m_signals; @@ -389,7 +392,7 @@ public: std::atomic checkSigs_; - std::unique_ptr m_resolver; + std::unique_ptr m_resolver; io_latency_sampler m_io_latency_sampler; @@ -397,13 +400,12 @@ public: //-------------------------------------------------------------------------- - static - std::size_t + static std::size_t numberOfThreads(Config const& config) { - #if RIPPLE_SINGLE_IO_SERVICE_THREAD +#if RIPPLE_SINGLE_IO_SERVICE_THREAD return 1; - #else +#else auto const cores = std::thread::hardware_concurrency(); // Use a single thread when running on under-provisioned systems @@ -413,153 +415,205 @@ public: // Otherwise, prefer two threads. return 2; - #endif +#endif } //-------------------------------------------------------------------------- - ApplicationImp ( - std::unique_ptr config, - std::unique_ptr logs, - std::unique_ptr timeKeeper) - : RootStoppable ("Application") - , BasicApp (numberOfThreads(*config)) - , config_ (std::move(config)) - , logs_ (std::move(logs)) - , timeKeeper_ (std::move(timeKeeper)) - , m_journal (logs_->journal("Application")) + ApplicationImp( + std::unique_ptr config, + std::unique_ptr logs, + std::unique_ptr timeKeeper) + : RootStoppable("Application") + , BasicApp(numberOfThreads(*config)) + , config_(std::move(config)) + , logs_(std::move(logs)) + , timeKeeper_(std::move(timeKeeper)) + , m_journal(logs_->journal("Application")) // PerfLog must be started before any other threads are launched. - , perfLog_ (perf::make_PerfLog( - perf::setup_PerfLog(config_->section("perf"), config_->CONFIG_DIR), - *this, logs_->journal("PerfLog"), [this] () { signalStop(); })) + , perfLog_(perf::make_PerfLog( + perf::setup_PerfLog( + config_->section("perf"), + config_->CONFIG_DIR), + *this, + logs_->journal("PerfLog"), + [this]() { signalStop(); })) - , m_txMaster (*this) + , m_txMaster(*this) - , m_nodeStoreScheduler (*this) + , m_nodeStoreScheduler(*this) , m_shaMapStore(make_SHAMapStore( - *this, - *this, - m_nodeStoreScheduler, - logs_->journal("SHAMapStore"))) + *this, + *this, + m_nodeStoreScheduler, + logs_->journal("SHAMapStore"))) , accountIDCache_(128000) - , m_tempNodeCache ("NodeCache", 16384, std::chrono::seconds {90}, - stopwatch(), logs_->journal("TaggedCache")) + , m_tempNodeCache( + "NodeCache", + 16384, + std::chrono::seconds{90}, + stopwatch(), + logs_->journal("TaggedCache")) - , m_collectorManager (CollectorManager::New ( - config_->section (SECTION_INSIGHT), logs_->journal("Collector"))) - , cachedSLEs_ (std::chrono::minutes(1), stopwatch()) + , m_collectorManager(CollectorManager::New( + config_->section(SECTION_INSIGHT), + logs_->journal("Collector"))) + , cachedSLEs_(std::chrono::minutes(1), stopwatch()) , validatorKeys_(*config_, m_journal) - , m_resourceManager (Resource::make_Manager ( - m_collectorManager->collector(), logs_->journal("Resource"))) + , m_resourceManager(Resource::make_Manager( + m_collectorManager->collector(), + logs_->journal("Resource"))) // The JobQueue has to come pretty early since // almost everything is a Stoppable child of the JobQueue. // - , m_jobQueue (std::make_unique( - m_collectorManager->group ("jobq"), m_nodeStoreScheduler, - logs_->journal("JobQueue"), *logs_, *perfLog_)) + , m_jobQueue(std::make_unique( + m_collectorManager->group("jobq"), + m_nodeStoreScheduler, + logs_->journal("JobQueue"), + *logs_, + *perfLog_)) - , m_nodeStore (m_shaMapStore->makeNodeStore ("NodeStore.main", 4)) + , m_nodeStore(m_shaMapStore->makeNodeStore("NodeStore.main", 4)) - , family_ (*this, *m_nodeStore, *m_collectorManager) + , family_(*this, *m_nodeStore, *m_collectorManager) // The shard store is optional and make_ShardStore can return null. - , shardStore_ (make_ShardStore ( - *this, - *m_jobQueue, - m_nodeStoreScheduler, - 4, - logs_->journal("ShardStore"))) + , shardStore_(make_ShardStore( + *this, + *m_jobQueue, + m_nodeStoreScheduler, + 4, + logs_->journal("ShardStore"))) - , m_orderBookDB (*this, *m_jobQueue) + , m_orderBookDB(*this, *m_jobQueue) - , m_pathRequests (std::make_unique ( - *this, logs_->journal("PathRequest"), m_collectorManager->collector ())) + , m_pathRequests(std::make_unique( + *this, + logs_->journal("PathRequest"), + m_collectorManager->collector())) - , m_ledgerMaster (std::make_unique (*this, stopwatch (), - *m_jobQueue, m_collectorManager->collector (), - logs_->journal("LedgerMaster"))) + , m_ledgerMaster(std::make_unique( + *this, + stopwatch(), + *m_jobQueue, + m_collectorManager->collector(), + logs_->journal("LedgerMaster"))) // VFALCO NOTE must come before NetworkOPs to prevent a crash due // to dependencies in the destructor. // - , m_inboundLedgers (make_InboundLedgers (*this, stopwatch(), - *m_jobQueue, m_collectorManager->collector ())) + , m_inboundLedgers(make_InboundLedgers( + *this, + stopwatch(), + *m_jobQueue, + m_collectorManager->collector())) - , m_inboundTransactions (make_InboundTransactions - ( *this, stopwatch() - , *m_jobQueue - , m_collectorManager->collector () - , [this](std::shared_ptr const& set, - bool fromAcquire) - { - gotTXSet (set, fromAcquire); - })) + , m_inboundTransactions(make_InboundTransactions( + *this, + stopwatch(), + *m_jobQueue, + m_collectorManager->collector(), + [this](std::shared_ptr const& set, bool fromAcquire) { + gotTXSet(set, fromAcquire); + })) - , m_acceptedLedgerCache ("AcceptedLedger", 4, std::chrono::minutes {1}, - stopwatch(), logs_->journal("TaggedCache")) + , m_acceptedLedgerCache( + "AcceptedLedger", + 4, + std::chrono::minutes{1}, + stopwatch(), + logs_->journal("TaggedCache")) - , m_networkOPs (make_NetworkOPs (*this, stopwatch(), - config_->standalone(), config_->NETWORK_QUORUM, config_->START_VALID, - *m_jobQueue, *m_ledgerMaster, *m_jobQueue, validatorKeys_, - get_io_service(), logs_->journal("NetworkOPs"), m_collectorManager->collector())) + , m_networkOPs(make_NetworkOPs( + *this, + stopwatch(), + config_->standalone(), + config_->NETWORK_QUORUM, + config_->START_VALID, + *m_jobQueue, + *m_ledgerMaster, + *m_jobQueue, + validatorKeys_, + get_io_service(), + logs_->journal("NetworkOPs"), + m_collectorManager->collector())) - , cluster_ (std::make_unique ( - logs_->journal("Overlay"))) + , cluster_(std::make_unique(logs_->journal("Overlay"))) - , peerReservations_(std::make_unique(logs_->journal("PeerReservationTable"))) + , peerReservations_(std::make_unique( + logs_->journal("PeerReservationTable"))) - , validatorManifests_ (std::make_unique ( - logs_->journal("ManifestCache"))) + , validatorManifests_( + std::make_unique(logs_->journal("ManifestCache"))) - , publisherManifests_ (std::make_unique ( - logs_->journal("ManifestCache"))) + , publisherManifests_( + std::make_unique(logs_->journal("ManifestCache"))) - , validators_ (std::make_unique ( - *validatorManifests_, *publisherManifests_, - *timeKeeper_, config_->legacy("database_path"), - logs_->journal("ValidatorList"), config_->VALIDATION_QUORUM)) + , validators_(std::make_unique( + *validatorManifests_, + *publisherManifests_, + *timeKeeper_, + config_->legacy("database_path"), + logs_->journal("ValidatorList"), + config_->VALIDATION_QUORUM)) - , validatorSites_ (std::make_unique (*this)) + , validatorSites_(std::make_unique(*this)) - , serverHandler_ (make_ServerHandler (*this, *m_networkOPs, get_io_service (), - *m_jobQueue, *m_networkOPs, *m_resourceManager, - *m_collectorManager)) + , serverHandler_(make_ServerHandler( + *this, + *m_networkOPs, + get_io_service(), + *m_jobQueue, + *m_networkOPs, + *m_resourceManager, + *m_collectorManager)) - , mFeeTrack (std::make_unique(logs_->journal("LoadManager"))) + , mFeeTrack( + std::make_unique(logs_->journal("LoadManager"))) - , hashRouter_ (std::make_unique( - stopwatch(), HashRouter::getDefaultHoldTime (), - HashRouter::getDefaultRecoverLimit ())) + , hashRouter_(std::make_unique( + stopwatch(), + HashRouter::getDefaultHoldTime(), + HashRouter::getDefaultRecoverLimit())) - , mValidations (ValidationParms(),stopwatch(), *this, logs_->journal("Validations")) + , mValidations( + ValidationParms(), + stopwatch(), + *this, + logs_->journal("Validations")) - , m_loadManager (make_LoadManager (*this, *this, logs_->journal("LoadManager"))) + , m_loadManager( + make_LoadManager(*this, *this, logs_->journal("LoadManager"))) , txQ_(make_TxQ(setup_TxQ(*config_), logs_->journal("TxQ"))) - , sweepTimer_ (get_io_service()) + , sweepTimer_(get_io_service()) - , entropyTimer_ (get_io_service()) + , entropyTimer_(get_io_service()) - , startTimers_ (false) + , startTimers_(false) - , m_signals (get_io_service()) + , m_signals(get_io_service()) , checkSigs_(true) - , m_resolver (ResolverAsio::New (get_io_service(), logs_->journal("Resolver"))) + , m_resolver( + ResolverAsio::New(get_io_service(), logs_->journal("Resolver"))) - , m_io_latency_sampler (m_collectorManager->collector()->make_event ("ios_latency"), - logs_->journal("Application"), std::chrono::milliseconds (100), get_io_service()) + , m_io_latency_sampler( + m_collectorManager->collector()->make_event("ios_latency"), + logs_->journal("Application"), + std::chrono::milliseconds(100), + get_io_service()) , grpcServer_(std::make_unique(*this)) { - add (m_resourceManager.get ()); + add(m_resourceManager.get()); // // VFALCO - READ THIS! @@ -577,21 +631,29 @@ public: // // VFALCO HACK - m_nodeStoreScheduler.setJobQueue (*m_jobQueue); + m_nodeStoreScheduler.setJobQueue(*m_jobQueue); - add (m_ledgerMaster->getPropertySource ()); + add(m_ledgerMaster->getPropertySource()); } //-------------------------------------------------------------------------- - bool setup() override; - void doStart(bool withTimers) override; - void run() override; - bool isShutdown() override; - void signalStop() override; - bool checkSigs() const override; - void checkSigs(bool) override; - int fdRequired() const override; + bool + setup() override; + void + doStart(bool withTimers) override; + void + run() override; + bool + isShutdown() override; + void + signalStop() override; + bool + checkSigs() const override; + void + checkSigs(bool) override; + int + fdRequired() const override; //-------------------------------------------------------------------------- @@ -607,17 +669,20 @@ public: return *config_; } - CollectorManager& getCollectorManager () override + CollectorManager& + getCollectorManager() override { return *m_collectorManager; } - Family& family() override + Family& + family() override { return family_; } - Family* shardFamily() override + Family* + shardFamily() override { return shardFamily_.get(); } @@ -628,110 +693,129 @@ public: return *timeKeeper_; } - JobQueue& getJobQueue () override + JobQueue& + getJobQueue() override { return *m_jobQueue; } std::pair const& - nodeIdentity () override + nodeIdentity() override { return nodeIdentity_; } - PublicKey const & + PublicKey const& getValidationPublicKey() const override { return validatorKeys_.publicKey; } - NetworkOPs& getOPs () override + NetworkOPs& + getOPs() override { return *m_networkOPs; } - boost::asio::io_service& getIOService () override + boost::asio::io_service& + getIOService() override { return get_io_service(); } - std::chrono::milliseconds getIOLatency () override + std::chrono::milliseconds + getIOLatency() override { - return m_io_latency_sampler.get (); + return m_io_latency_sampler.get(); } - LedgerMaster& getLedgerMaster () override + LedgerMaster& + getLedgerMaster() override { return *m_ledgerMaster; } - InboundLedgers& getInboundLedgers () override + InboundLedgers& + getInboundLedgers() override { return *m_inboundLedgers; } - InboundTransactions& getInboundTransactions () override + InboundTransactions& + getInboundTransactions() override { return *m_inboundTransactions; } - TaggedCache & getAcceptedLedgerCache () override + TaggedCache& + getAcceptedLedgerCache() override { return m_acceptedLedgerCache; } - void gotTXSet (std::shared_ptr const& set, bool fromAcquire) + void + gotTXSet(std::shared_ptr const& set, bool fromAcquire) { if (set) - m_networkOPs->mapComplete (set, fromAcquire); + m_networkOPs->mapComplete(set, fromAcquire); } - TransactionMaster& getMasterTransaction () override + TransactionMaster& + getMasterTransaction() override { return m_txMaster; } - perf::PerfLog& getPerfLog () override + perf::PerfLog& + getPerfLog() override { return *perfLog_; } - NodeCache& getTempNodeCache () override + NodeCache& + getTempNodeCache() override { return m_tempNodeCache; } - NodeStore::Database& getNodeStore () override + NodeStore::Database& + getNodeStore() override { return *m_nodeStore; } - NodeStore::DatabaseShard* getShardStore () override + NodeStore::DatabaseShard* + getShardStore() override { return shardStore_.get(); } - Application::MutexType& getMasterMutex () override + Application::MutexType& + getMasterMutex() override { return m_masterMutex; } - LoadManager& getLoadManager () override + LoadManager& + getLoadManager() override { return *m_loadManager; } - Resource::Manager& getResourceManager () override + Resource::Manager& + getResourceManager() override { return *m_resourceManager; } - OrderBookDB& getOrderBookDB () override + OrderBookDB& + getOrderBookDB() override { return m_orderBookDB; } - PathRequests& getPathRequests () override + PathRequests& + getPathRequests() override { return *m_pathRequests; } @@ -742,62 +826,74 @@ public: return cachedSLEs_; } - AmendmentTable& getAmendmentTable() override + AmendmentTable& + getAmendmentTable() override { return *m_amendmentTable; } - LoadFeeTrack& getFeeTrack () override + LoadFeeTrack& + getFeeTrack() override { return *mFeeTrack; } - HashRouter& getHashRouter () override + HashRouter& + getHashRouter() override { return *hashRouter_; } - RCLValidations& getValidations () override + RCLValidations& + getValidations() override { return mValidations; } - ValidatorList& validators () override + ValidatorList& + validators() override { return *validators_; } - ValidatorSite& validatorSites () override + ValidatorSite& + validatorSites() override { return *validatorSites_; } - ManifestCache& validatorManifests() override + ManifestCache& + validatorManifests() override { return *validatorManifests_; } - ManifestCache& publisherManifests() override + ManifestCache& + publisherManifests() override { return *publisherManifests_; } - Cluster& cluster () override + Cluster& + cluster() override { return *cluster_; } - PeerReservationTable& peerReservations () override + PeerReservationTable& + peerReservations() override { return *peerReservations_; } - SHAMapStore& getSHAMapStore () override + SHAMapStore& + getSHAMapStore() override { return *m_shaMapStore; } - PendingSaves& pendingSaves() override + PendingSaves& + pendingSaves() override { return pendingSaves_; } @@ -820,64 +916,67 @@ public: return *openLedger_; } - Overlay& overlay () override + Overlay& + overlay() override { assert(overlay_); return *overlay_; } - TxQ& getTxQ() override + TxQ& + getTxQ() override { assert(txQ_.get() != nullptr); return *txQ_; } - DatabaseCon& getTxnDB () override + DatabaseCon& + getTxnDB() override { - assert (mTxnDB.get() != nullptr); + assert(mTxnDB.get() != nullptr); return *mTxnDB; } - DatabaseCon& getLedgerDB () override + DatabaseCon& + getLedgerDB() override { - assert (mLedgerDB.get() != nullptr); + assert(mLedgerDB.get() != nullptr); return *mLedgerDB; } - DatabaseCon& getWalletDB () override + DatabaseCon& + getWalletDB() override { - assert (mWalletDB.get() != nullptr); + assert(mWalletDB.get() != nullptr); return *mWalletDB; } - bool serverOkay (std::string& reason) override; + bool + serverOkay(std::string& reason) override; - beast::Journal journal (std::string const& name) override; + beast::Journal + journal(std::string const& name) override; //-------------------------------------------------------------------------- bool initSQLiteDBs() { - assert (mTxnDB.get () == nullptr); - assert (mLedgerDB.get () == nullptr); - assert (mWalletDB.get () == nullptr); + assert(mTxnDB.get() == nullptr); + assert(mLedgerDB.get() == nullptr); + assert(mWalletDB.get() == nullptr); try { auto const setup = setup_DatabaseCon(*config_); // transaction database - mTxnDB = std::make_unique ( - setup, - TxDBName, - TxDBPragma, - TxDBInit); - mTxnDB->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % + mTxnDB = std::make_unique( + setup, TxDBName, TxDBPragma, TxDBInit); + mTxnDB->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % kilobytes(config_->getValueFor(SizedItem::txnDBCache))); mTxnDB->setupCheckpointing(m_jobQueue.get(), logs()); - if (!setup.standAlone || - setup.startUp == Config::LOAD || + if (!setup.standAlone || setup.startUp == Config::LOAD || setup.startUp == Config::LOAD_FILE || setup.startUp == Config::REPLAY) { @@ -885,41 +984,39 @@ public: std::string cid, name, type; std::size_t notnull, dflt_value, pk; soci::indicator ind; - soci::statement st = (mTxnDB->getSession().prepare << - ("PRAGMA table_info(AccountTransactions);"), - soci::into(cid), - soci::into(name), - soci::into(type), - soci::into(notnull), - soci::into(dflt_value, ind), - soci::into(pk)); + soci::statement st = + (mTxnDB->getSession().prepare + << ("PRAGMA table_info(AccountTransactions);"), + soci::into(cid), + soci::into(name), + soci::into(type), + soci::into(notnull), + soci::into(dflt_value, ind), + soci::into(pk)); st.execute(); while (st.fetch()) { if (pk == 1) { - JLOG(m_journal.fatal()) << - "AccountTransactions database " - "should not have a primary key"; + JLOG(m_journal.fatal()) + << "AccountTransactions database " + "should not have a primary key"; return false; } } } // ledger database - mLedgerDB = std::make_unique ( - setup, - LgrDBName, - LgrDBPragma, - LgrDBInit); - mLedgerDB->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % + mLedgerDB = std::make_unique( + setup, LgrDBName, LgrDBPragma, LgrDBInit); + mLedgerDB->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % kilobytes(config_->getValueFor(SizedItem::lgrDBCache))); mLedgerDB->setupCheckpointing(m_jobQueue.get(), logs()); // wallet database - mWalletDB = std::make_unique ( + mWalletDB = std::make_unique( setup, WalletDBName, std::array(), @@ -927,8 +1024,8 @@ public: } catch (std::exception const& e) { - JLOG(m_journal.fatal()) << - "Failed to initialize SQLite databases: " << e.what(); + JLOG(m_journal.fatal()) + << "Failed to initialize SQLite databases: " << e.what(); return false; } @@ -942,8 +1039,8 @@ public: { auto j = logs_->journal("NodeObject"); NodeStore::DummyScheduler dummyScheduler; - RootStoppable dummyRoot {"DummyRoot"}; - std::unique_ptr source = + RootStoppable dummyRoot{"DummyRoot"}; + std::unique_ptr source = NodeStore::Manager::instance().make_Database( "NodeStore.import", dummyScheduler, @@ -952,20 +1049,18 @@ public: config_->section(ConfigSection::importNodeDatabase()), j); - JLOG(j.warn()) << - "Starting node import from '" << source->getName() << - "' to '" << m_nodeStore->getName() << "'."; + JLOG(j.warn()) << "Starting node import from '" << source->getName() + << "' to '" << m_nodeStore->getName() << "'."; using namespace std::chrono; auto const start = steady_clock::now(); m_nodeStore->import(*source); - auto const elapsed = duration_cast - (steady_clock::now() - start); - JLOG(j.warn()) << - "Node import from '" << source->getName() << - "' took " << elapsed.count() << " seconds."; + auto const elapsed = + duration_cast(steady_clock::now() - start); + JLOG(j.warn()) << "Node import from '" << source->getName() + << "' took " << elapsed.count() << " seconds."; } // tune caches @@ -986,7 +1081,8 @@ public: return true; } - void signalled(const boost::system::error_code& ec, int signal_number) + void + signalled(const boost::system::error_code& ec, int signal_number) { if (ec == boost::asio::error::operation_aborted) { @@ -996,7 +1092,7 @@ public: else if (ec) { JLOG(m_journal.error()) << "Received signal: " << signal_number - << " with error: " << ec.message(); + << " with error: " << ec.message(); } else { @@ -1010,17 +1106,19 @@ public: // Stoppable // - void onPrepare() override + void + onPrepare() override { } - void onStart () override + void + onStart() override { - JLOG(m_journal.info()) - << "Application starting. Version is " << BuildInfo::getVersionString(); + JLOG(m_journal.info()) << "Application starting. Version is " + << BuildInfo::getVersionString(); using namespace std::chrono_literals; - if(startTimers_) + if (startTimers_) { setSweepTimer(); setEntropyTimer(); @@ -1028,15 +1126,16 @@ public: m_io_latency_sampler.start(); - m_resolver->start (); + m_resolver->start(); } // Called to indicate shutdown. - void onStop () override + void + onStop() override { JLOG(m_journal.debug()) << "Application stopping"; - m_io_latency_sampler.cancel_async (); + m_io_latency_sampler.cancel_async(); // VFALCO Enormous hack, we have to force the probe to cancel // before we stop the io_service queue or else it never @@ -1044,30 +1143,29 @@ public: // io_objects gracefully handle exit so that we can // naturally return from io_service::run() instead of // forcing a call to io_service::stop() - m_io_latency_sampler.cancel (); + m_io_latency_sampler.cancel(); - m_resolver->stop_async (); + m_resolver->stop_async(); // NIKB This is a hack - we need to wait for the resolver to // stop. before we stop the io_server_queue or weird // things will happen. - m_resolver->stop (); + m_resolver->stop(); { boost::system::error_code ec; - sweepTimer_.cancel (ec); + sweepTimer_.cancel(ec); if (ec) { - JLOG (m_journal.error()) - << "Application: sweepTimer cancel error: " - << ec.message(); + JLOG(m_journal.error()) + << "Application: sweepTimer cancel error: " << ec.message(); } ec.clear(); - entropyTimer_.cancel (ec); + entropyTimer_.cancel(ec); if (ec) { - JLOG (m_journal.error()) + JLOG(m_journal.error()) << "Application: entropyTimer cancel error: " << ec.message(); } @@ -1077,24 +1175,26 @@ public: using namespace std::chrono_literals; waitHandlerCounter_.join("Application", 1s, m_journal); - mValidations.flush (); + mValidations.flush(); - validatorSites_->stop (); + validatorSites_->stop(); // TODO Store manifests in manifests.sqlite instead of wallet.db - validatorManifests_->save (getWalletDB (), "ValidatorManifests", - [this](PublicKey const& pubKey) - { - return validators().listed (pubKey); + validatorManifests_->save( + getWalletDB(), + "ValidatorManifests", + [this](PublicKey const& pubKey) { + return validators().listed(pubKey); }); - publisherManifests_->save (getWalletDB (), "PublisherManifests", - [this](PublicKey const& pubKey) - { - return validators().trustedPublisher (pubKey); + publisherManifests_->save( + getWalletDB(), + "PublisherManifests", + [this](PublicKey const& pubKey) { + return validators().trustedPublisher(pubKey); }); - stopped (); + stopped(); } //-------------------------------------------------------------------------- @@ -1102,84 +1202,86 @@ public: // PropertyStream // - void onWrite (beast::PropertyStream::Map& stream) override + void + onWrite(beast::PropertyStream::Map& stream) override { } //-------------------------------------------------------------------------- - void setSweepTimer () + void + setSweepTimer() { // Only start the timer if waitHandlerCounter_ is not yet joined. - if (auto optionalCountedHandler = waitHandlerCounter_.wrap ( - [this] (boost::system::error_code const& e) - { - if ((e.value() == boost::system::errc::success) && - (! m_jobQueue->isStopped())) - { - m_jobQueue->addJob( - jtSWEEP, "sweep", [this] (Job&) { doSweep(); }); - } - // Recover as best we can if an unexpected error occurs. - if (e.value() != boost::system::errc::success && - e.value() != boost::asio::error::operation_aborted) - { - // Try again later and hope for the best. - JLOG (m_journal.error()) - << "Sweep timer got error '" << e.message() - << "'. Restarting timer."; - setSweepTimer(); - } - })) + if (auto optionalCountedHandler = waitHandlerCounter_.wrap( + [this](boost::system::error_code const& e) { + if ((e.value() == boost::system::errc::success) && + (!m_jobQueue->isStopped())) + { + m_jobQueue->addJob( + jtSWEEP, "sweep", [this](Job&) { doSweep(); }); + } + // Recover as best we can if an unexpected error occurs. + if (e.value() != boost::system::errc::success && + e.value() != boost::asio::error::operation_aborted) + { + // Try again later and hope for the best. + JLOG(m_journal.error()) + << "Sweep timer got error '" << e.message() + << "'. Restarting timer."; + setSweepTimer(); + } + })) { using namespace std::chrono; sweepTimer_.expires_from_now( seconds{config_->getValueFor(SizedItem::sweepInterval)}); - sweepTimer_.async_wait (std::move (*optionalCountedHandler)); + sweepTimer_.async_wait(std::move(*optionalCountedHandler)); } } - void setEntropyTimer () + void + setEntropyTimer() { // Only start the timer if waitHandlerCounter_ is not yet joined. - if (auto optionalCountedHandler = waitHandlerCounter_.wrap ( - [this] (boost::system::error_code const& e) - { - if (e.value() == boost::system::errc::success) - { - crypto_prng().mix_entropy(); - setEntropyTimer(); - } - // Recover as best we can if an unexpected error occurs. - if (e.value() != boost::system::errc::success && - e.value() != boost::asio::error::operation_aborted) - { - // Try again later and hope for the best. - JLOG (m_journal.error()) - << "Entropy timer got error '" << e.message() - << "'. Restarting timer."; - setEntropyTimer(); - } - })) + if (auto optionalCountedHandler = waitHandlerCounter_.wrap( + [this](boost::system::error_code const& e) { + if (e.value() == boost::system::errc::success) + { + crypto_prng().mix_entropy(); + setEntropyTimer(); + } + // Recover as best we can if an unexpected error occurs. + if (e.value() != boost::system::errc::success && + e.value() != boost::asio::error::operation_aborted) + { + // Try again later and hope for the best. + JLOG(m_journal.error()) + << "Entropy timer got error '" << e.message() + << "'. Restarting timer."; + setEntropyTimer(); + } + })) { using namespace std::chrono_literals; - entropyTimer_.expires_from_now (5min); - entropyTimer_.async_wait (std::move (*optionalCountedHandler)); + entropyTimer_.expires_from_now(5min); + entropyTimer_.async_wait(std::move(*optionalCountedHandler)); } } - void doSweep () + void + doSweep() { - if (! config_->standalone()) + if (!config_->standalone()) { boost::filesystem::space_info space = - boost::filesystem::space (config_->legacy ("database_path")); + boost::filesystem::space(config_->legacy("database_path")); if (space.available < megabytes(512)) { JLOG(m_journal.fatal()) << "Remaining free disk space is less than 512MB"; - signalStop (); + signalStop(); } DatabaseCon::Setup dbSetup = setup_DatabaseCon(*config_); @@ -1196,14 +1298,14 @@ public: } auto db = mTxnDB->checkoutDb(); - static auto const pageSize = [&]{ + static auto const pageSize = [&] { std::uint32_t ps; *db << "PRAGMA page_size;", soci::into(ps); return ps; }(); - static auto const maxPages = [&]{ + static auto const maxPages = [&] { std::uint32_t mp; - *db << "PRAGMA max_page_count;" , soci::into(mp); + *db << "PRAGMA max_page_count;", soci::into(mp); return mp; }(); std::uint32_t pageCount; @@ -1212,13 +1314,13 @@ public: std::uint64_t freeSpace = safe_cast(freePages) * pageSize; JLOG(m_journal.info()) - << "Transaction DB pathname: " << dbPath.string() - << "; file size: " << dbSize.value_or(-1) << " bytes" - << "; SQLite page size: " << pageSize << " bytes" - << "; Free pages: " << freePages - << "; Free space: " << freeSpace << " bytes; " - << "Note that this does not take into account available disk " - "space."; + << "Transaction DB pathname: " << dbPath.string() + << "; file size: " << dbSize.value_or(-1) << " bytes" + << "; SQLite page size: " << pageSize << " bytes" + << "; Free pages: " << freePages + << "; Free space: " << freeSpace << " bytes; " + << "Note that this does not take into account available disk " + "space."; if (freeSpace < megabytes(512)) { @@ -1257,34 +1359,35 @@ public: setSweepTimer(); } - LedgerIndex getMaxDisallowedLedger() override + LedgerIndex + getMaxDisallowedLedger() override { return maxDisallowedLedger_; } - private: // For a newly-started validator, this is the greatest persisted ledger // and new validations must be greater than this. - std::atomic maxDisallowedLedger_ {0}; + std::atomic maxDisallowedLedger_{0}; - bool nodeToShards (); - bool validateShards (); - void startGenesisLedger (); + bool + nodeToShards(); + bool + validateShards(); + void + startGenesisLedger(); std::shared_ptr getLastFullLedger(); std::shared_ptr - loadLedgerFromFile ( - std::string const& ledgerID); + loadLedgerFromFile(std::string const& ledgerID); - bool loadOldLedger ( - std::string const& ledgerID, - bool replay, - bool isFilename); + bool + loadOldLedger(std::string const& ledgerID, bool replay, bool isFilename); - void setMaxDisallowedLedger(); + void + setMaxDisallowedLedger(); }; //------------------------------------------------------------------------------ @@ -1293,19 +1396,23 @@ private: // Or better yet refactor these initializations into RAII classes // which are members of the Application object. // -bool ApplicationImp::setup() +bool +ApplicationImp::setup() { // We want to intercept and wait for CTRL-C to terminate the process - m_signals.add (SIGINT); + m_signals.add(SIGINT); - m_signals.async_wait(std::bind(&ApplicationImp::signalled, this, - std::placeholders::_1, std::placeholders::_2)); + m_signals.async_wait(std::bind( + &ApplicationImp::signalled, + this, + std::placeholders::_1, + std::placeholders::_2)); - assert (mTxnDB == nullptr); + assert(mTxnDB == nullptr); - auto debug_log = config_->getDebugLogFile (); + auto debug_log = config_->getDebugLogFile(); - if (!debug_log.empty ()) + if (!debug_log.empty()) { // Let debug messages go to the file but only WARNING or higher to // regular output (unless verbose) @@ -1315,21 +1422,21 @@ bool ApplicationImp::setup() using namespace beast::severities; if (logs_->threshold() > kDebug) - logs_->threshold (kDebug); + logs_->threshold(kDebug); } JLOG(m_journal.info()) << "process starting: " - << BuildInfo::getFullVersionString(); + << BuildInfo::getFullVersionString(); if (numberOfThreads(*config_) < 2) { - JLOG (m_journal.warn()) << - "Limited to a single I/O service thread by system configuration."; + JLOG(m_journal.warn()) << "Limited to a single I/O service thread by " + "system configuration."; } // Optionally turn off logging to console. - logs_->silent (config_->silent()); + logs_->silent(config_->silent()); - m_jobQueue->setThreadCount (config_->WORKERS, config_->standalone()); + m_jobQueue->setThreadCount(config_->WORKERS, config_->standalone()); grpcServer_->run(); if (!config_->standalone()) @@ -1341,9 +1448,7 @@ bool ApplicationImp::setup() if (shardStore_) { shardFamily_ = std::make_unique( - *this, - *shardStore_, - *m_collectorManager); + *this, *shardStore_, *m_collectorManager); using namespace std::chrono; shardFamily_->treecache().setTargetSize( @@ -1376,17 +1481,17 @@ bool ApplicationImp::setup() if (f) saHashes.push_back(to_string(*f) + " " + name); } - Section supportedAmendments ("Supported Amendments"); - supportedAmendments.append (saHashes); + Section supportedAmendments("Supported Amendments"); + supportedAmendments.append(saHashes); - Section enabledAmendments = config_->section (SECTION_AMENDMENTS); + Section enabledAmendments = config_->section(SECTION_AMENDMENTS); - m_amendmentTable = make_AmendmentTable ( + m_amendmentTable = make_AmendmentTable( weeks{2}, MAJORITY_FRACTION, supportedAmendments, enabledAmendments, - config_->section (SECTION_VETO_AMENDMENTS), + config_->section(SECTION_VETO_AMENDMENTS), logs_->journal("Amendments")); } @@ -1397,79 +1502,81 @@ bool ApplicationImp::setup() { JLOG(m_journal.info()) << "Starting new Ledger"; - startGenesisLedger (); + startGenesisLedger(); } - else if (startUp == Config::LOAD || - startUp == Config::LOAD_FILE || - startUp == Config::REPLAY) + else if ( + startUp == Config::LOAD || startUp == Config::LOAD_FILE || + startUp == Config::REPLAY) { - JLOG(m_journal.info()) << - "Loading specified Ledger"; + JLOG(m_journal.info()) << "Loading specified Ledger"; - if (!loadOldLedger (config_->START_LEDGER, - startUp == Config::REPLAY, - startUp == Config::LOAD_FILE)) + if (!loadOldLedger( + config_->START_LEDGER, + startUp == Config::REPLAY, + startUp == Config::LOAD_FILE)) { - JLOG(m_journal.error()) << - "The specified ledger could not be loaded."; + JLOG(m_journal.error()) + << "The specified ledger could not be loaded."; return false; } } else if (startUp == Config::NETWORK) { - // This should probably become the default once we have a stable network. + // This should probably become the default once we have a stable + // network. if (!config_->standalone()) m_networkOPs->setNeedNetworkLedger(); - startGenesisLedger (); + startGenesisLedger(); } else { - startGenesisLedger (); + startGenesisLedger(); } - m_orderBookDB.setup (getLedgerMaster ().getCurrentLedger ()); + m_orderBookDB.setup(getLedgerMaster().getCurrentLedger()); - nodeIdentity_ = loadNodeIdentity (*this); + nodeIdentity_ = loadNodeIdentity(*this); - if (!cluster_->load (config().section(SECTION_CLUSTER_NODES))) + if (!cluster_->load(config().section(SECTION_CLUSTER_NODES))) { JLOG(m_journal.fatal()) << "Invalid entry in cluster configuration."; return false; } { - if(validatorKeys_.configInvalid()) + if (validatorKeys_.configInvalid()) return false; - if (!validatorManifests_->load ( - getWalletDB (), "ValidatorManifests", validatorKeys_.manifest, - config().section (SECTION_VALIDATOR_KEY_REVOCATION).values ())) + if (!validatorManifests_->load( + getWalletDB(), + "ValidatorManifests", + validatorKeys_.manifest, + config().section(SECTION_VALIDATOR_KEY_REVOCATION).values())) { JLOG(m_journal.fatal()) << "Invalid configured validator manifest."; return false; } - publisherManifests_->load ( - getWalletDB (), "PublisherManifests"); + publisherManifests_->load(getWalletDB(), "PublisherManifests"); // Setup trusted validators - if (!validators_->load ( + if (!validators_->load( validatorKeys_.publicKey, - config().section (SECTION_VALIDATORS).values (), - config().section (SECTION_VALIDATOR_LIST_KEYS).values ())) + config().section(SECTION_VALIDATORS).values(), + config().section(SECTION_VALIDATOR_LIST_KEYS).values())) { - JLOG(m_journal.fatal()) << - "Invalid entry in validator configuration."; + JLOG(m_journal.fatal()) + << "Invalid entry in validator configuration."; return false; } } - if (!validatorSites_->load ( - config().section (SECTION_VALIDATOR_LIST_SITES).values ())) + if (!validatorSites_->load( + config().section(SECTION_VALIDATOR_LIST_SITES).values())) { - JLOG(m_journal.fatal()) << - "Invalid entry in [" << SECTION_VALIDATOR_LIST_SITES << "]"; + JLOG(m_journal.fatal()) + << "Invalid entry in [" << SECTION_VALIDATOR_LIST_SITES << "]"; return false; } @@ -1484,10 +1591,17 @@ bool ApplicationImp::setup() // move the instantiation inside a conditional: // // if (!config_.standalone()) - overlay_ = make_Overlay (*this, setup_Overlay(*config_), *m_jobQueue, - *serverHandler_, *m_resourceManager, *m_resolver, get_io_service(), - *config_, m_collectorManager->collector ()); - add (*overlay_); // add to PropertyStream + overlay_ = make_Overlay( + *this, + setup_Overlay(*config_), + *m_jobQueue, + *serverHandler_, + *m_resourceManager, + *m_resolver, + get_io_service(), + *config_, + m_collectorManager->collector()); + add(*overlay_); // add to PropertyStream if (!config_->standalone()) { @@ -1499,10 +1613,11 @@ bool ApplicationImp::setup() return false; } - validatorSites_->start (); + validatorSites_->start(); // start first consensus round - if (! m_networkOPs->beginConsensus(m_ledgerMaster->getClosedLedger()->info().hash)) + if (!m_networkOPs->beginConsensus( + m_ledgerMaster->getClosedLedger()->info().hash)) { JLOG(m_journal.fatal()) << "Unable to start consensus"; return false; @@ -1521,7 +1636,7 @@ bool ApplicationImp::setup() if (auto stream = m_journal.fatal()) { stream << "Unable to setup server handler"; - if(std::strlen(e.what()) > 0) + if (std::strlen(e.what()) > 0) stream << ": " << e.what(); } return false; @@ -1533,7 +1648,7 @@ bool ApplicationImp::setup() { // Should this message be here, conceptually? In theory this sort // of message, if displayed, should be displayed from PeerFinder. - if (config_->PEER_PRIVATE && config_->IPS_FIXED.empty ()) + if (config_->PEER_PRIVATE && config_->IPS_FIXED.empty()) { JLOG(m_journal.warn()) << "No outbound peer connections will be made"; @@ -1541,33 +1656,32 @@ bool ApplicationImp::setup() // VFALCO NOTE the state timer resets the deadlock detector. // - m_networkOPs->setStateTimer (); + m_networkOPs->setStateTimer(); } else { JLOG(m_journal.warn()) << "Running in standalone mode"; - m_networkOPs->setStandAlone (); + m_networkOPs->setStandAlone(); } if (config_->canSign()) { - JLOG(m_journal.warn()) << - "*** The server is configured to allow the 'sign' and 'sign_for'"; - JLOG(m_journal.warn()) << - "*** commands. These commands have security implications and have"; - JLOG(m_journal.warn()) << - "*** been deprecated. They will be removed in a future release of"; - JLOG(m_journal.warn()) << - "*** rippled."; - JLOG(m_journal.warn()) << - "*** If you do not use them to sign transactions please edit your"; - JLOG(m_journal.warn()) << - "*** configuration file and remove the [enable_signing] stanza."; - JLOG(m_journal.warn()) << - "*** If you do use them to sign transactions please migrate to a"; - JLOG(m_journal.warn()) << - "*** standalone signing solution as soon as possible."; + JLOG(m_journal.warn()) << "*** The server is configured to allow the " + "'sign' and 'sign_for'"; + JLOG(m_journal.warn()) << "*** commands. These commands have security " + "implications and have"; + JLOG(m_journal.warn()) << "*** been deprecated. They will be removed " + "in a future release of"; + JLOG(m_journal.warn()) << "*** rippled."; + JLOG(m_journal.warn()) << "*** If you do not use them to sign " + "transactions please edit your"; + JLOG(m_journal.warn()) + << "*** configuration file and remove the [enable_signing] stanza."; + JLOG(m_journal.warn()) << "*** If you do use them to sign transactions " + "please migrate to a"; + JLOG(m_journal.warn()) + << "*** standalone signing solution as soon as possible."; } // @@ -1578,32 +1692,33 @@ bool ApplicationImp::setup() Json::Reader jrReader; Json::Value jvCommand; - if (! jrReader.parse (cmd, jvCommand)) + if (!jrReader.parse(cmd, jvCommand)) { - JLOG(m_journal.fatal()) << - "Couldn't parse entry in [" << SECTION_RPC_STARTUP << - "]: '" << cmd; + JLOG(m_journal.fatal()) << "Couldn't parse entry in [" + << SECTION_RPC_STARTUP << "]: '" << cmd; } if (!config_->quiet()) { - JLOG(m_journal.fatal()) << "Startup RPC: " << jvCommand << std::endl; + JLOG(m_journal.fatal()) + << "Startup RPC: " << jvCommand << std::endl; } Resource::Charge loadType = Resource::feeReferenceRPC; Resource::Consumer c; - RPC::JsonContext context{{journal("RPCHandler"), - *this, - loadType, - getOPs(), - getLedgerMaster(), - c, - Role::ADMIN}, - jvCommand, - RPC::ApiMaximumSupportedVersion}; + RPC::JsonContext context{ + {journal("RPCHandler"), + *this, + loadType, + getOPs(), + getLedgerMaster(), + c, + Role::ADMIN}, + jvCommand, + RPC::ApiMaximumSupportedVersion}; Json::Value jvResult; - RPC::doCommand (context, jvResult); + RPC::doCommand(context, jvResult); if (!config_->quiet()) { @@ -1616,18 +1731,16 @@ bool ApplicationImp::setup() using namespace boost::filesystem; auto stateDb( - RPC::ShardArchiveHandler::getDownloadDirectory(*config_) - / stateDBName); + RPC::ShardArchiveHandler::getDownloadDirectory(*config_) / + stateDBName); try { - if (exists(stateDb) && - is_regular_file(stateDb) && + if (exists(stateDb) && is_regular_file(stateDb) && !RPC::ShardArchiveHandler::hasInstance()) { auto handler = RPC::ShardArchiveHandler::recoverInstance( - *this, - *m_jobQueue); + *this, *m_jobQueue); assert(handler); @@ -1648,11 +1761,12 @@ bool ApplicationImp::setup() } } } - catch(std::exception const& e) + catch (std::exception const& e) { JLOG(m_journal.fatal()) << "Exception when starting ShardArchiveHandler from " - "state database: " << e.what(); + "state database: " + << e.what(); return false; } @@ -1665,8 +1779,8 @@ void ApplicationImp::doStart(bool withTimers) { startTimers_ = withTimers; - prepare (); - start (); + prepare(); + start(); } void @@ -1675,20 +1789,21 @@ ApplicationImp::run() if (!config_->standalone()) { // VFALCO NOTE This seems unnecessary. If we properly refactor the load - // manager then the deadlock detector can just always be "armed" + // manager then the deadlock detector can just always be + // "armed" // - getLoadManager ().activateDeadlockDetector (); + getLoadManager().activateDeadlockDetector(); } { std::unique_lock lk{mut_}; - cv_.wait(lk, [this]{return isTimeToStop;}); + cv_.wait(lk, [this] { return isTimeToStop; }); } // Stop the server. When this returns, all // Stoppable objects should be stopped. JLOG(m_journal.info()) << "Received shutdown request"; - stop (m_journal); + stop(m_journal); JLOG(m_journal.info()) << "Done."; StopSustain(); } @@ -1710,17 +1825,20 @@ ApplicationImp::isShutdown() return isStopped(); } -bool ApplicationImp::checkSigs() const +bool +ApplicationImp::checkSigs() const { return checkSigs_; } -void ApplicationImp::checkSigs(bool check) +void +ApplicationImp::checkSigs(bool check) { checkSigs_ = check; } -int ApplicationImp::fdRequired() const +int +ApplicationImp::fdRequired() const { // Standard handles, config file, misc I/O etc: int needed = 128; @@ -1737,8 +1855,8 @@ int ApplicationImp::fdRequired() const // One fd per incoming connection a port can accept, or // if no limit is set, assume it'll handle 256 clients. - for(auto const& p : serverHandler_->setup().ports) - needed += std::max (256, p.limit); + for (auto const& p : serverHandler_->setup().ports) + needed += std::max(256, p.limit); // The minimum number of file descriptors we need is 1024: return std::max(1024, needed); @@ -1750,32 +1868,26 @@ void ApplicationImp::startGenesisLedger() { std::vector initialAmendments = - (config_->START_UP == Config::FRESH) ? - m_amendmentTable->getDesired() : - std::vector{}; + (config_->START_UP == Config::FRESH) ? m_amendmentTable->getDesired() + : std::vector{}; - std::shared_ptr const genesis = - std::make_shared( - create_genesis, - *config_, - initialAmendments, - family()); - m_ledgerMaster->storeLedger (genesis); + std::shared_ptr const genesis = std::make_shared( + create_genesis, *config_, initialAmendments, family()); + m_ledgerMaster->storeLedger(genesis); - auto const next = std::make_shared( - *genesis, timeKeeper().closeTime()); - next->updateSkipList (); - next->setImmutable (*config_); - openLedger_.emplace(next, cachedSLEs_, - logs_->journal("OpenLedger")); + auto const next = + std::make_shared(*genesis, timeKeeper().closeTime()); + next->updateSkipList(); + next->setImmutable(*config_); + openLedger_.emplace(next, cachedSLEs_, logs_->journal("OpenLedger")); m_ledgerMaster->storeLedger(next); - m_ledgerMaster->switchLCL (next); + m_ledgerMaster->switchLCL(next); } std::shared_ptr ApplicationImp::getLastFullLedger() { - auto j = journal ("Ledger"); + auto j = journal("Ledger"); try { @@ -1787,20 +1899,20 @@ ApplicationImp::getLastFullLedger() ledger->setImmutable(*config_); - if (getLedgerMaster ().haveLedger (seq)) - ledger->setValidated (); + if (getLedgerMaster().haveLedger(seq)) + ledger->setValidated(); if (ledger->info().hash == hash) { - JLOG (j.trace()) << "Loaded ledger: " << hash; + JLOG(j.trace()) << "Loaded ledger: " << hash; return ledger; } if (auto stream = j.error()) { - stream << "Failed on ledger"; + stream << "Failed on ledger"; Json::Value p; - addJson (p, {*ledger, LedgerFill::full}); + addJson(p, {*ledger, LedgerFill::full}); stream << p; } @@ -1808,44 +1920,40 @@ ApplicationImp::getLastFullLedger() } catch (SHAMapMissingNode const& mn) { - JLOG (j.warn()) << - "Ledger in database: " << mn.what(); + JLOG(j.warn()) << "Ledger in database: " << mn.what(); return {}; } } std::shared_ptr -ApplicationImp::loadLedgerFromFile ( - std::string const& name) +ApplicationImp::loadLedgerFromFile(std::string const& name) { try { - std::ifstream ledgerFile (name, std::ios::in); + std::ifstream ledgerFile(name, std::ios::in); if (!ledgerFile) { - JLOG(m_journal.fatal()) << - "Unable to open file '" << name << "'"; + JLOG(m_journal.fatal()) << "Unable to open file '" << name << "'"; return nullptr; } Json::Reader reader; Json::Value jLedger; - if (!reader.parse (ledgerFile, jLedger)) + if (!reader.parse(ledgerFile, jLedger)) { - JLOG(m_journal.fatal()) << - "Unable to parse ledger JSON"; + JLOG(m_journal.fatal()) << "Unable to parse ledger JSON"; return nullptr; } - std::reference_wrapper ledger (jLedger); + std::reference_wrapper ledger(jLedger); - // accept a wrapped ledger - if (ledger.get().isMember ("result")) + // accept a wrapped ledger + if (ledger.get().isMember("result")) ledger = ledger.get()["result"]; - if (ledger.get().isMember ("ledger")) + if (ledger.get().isMember("ledger")) ledger = ledger.get()["ledger"]; std::uint32_t seq = 1; @@ -1855,49 +1963,47 @@ ApplicationImp::loadLedgerFromFile ( bool closeTimeEstimated = false; std::uint64_t totalDrops = 0; - if (ledger.get().isMember ("accountState")) + if (ledger.get().isMember("accountState")) { - if (ledger.get().isMember (jss::ledger_index)) + if (ledger.get().isMember(jss::ledger_index)) { seq = ledger.get()[jss::ledger_index].asUInt(); } - if (ledger.get().isMember ("close_time")) + if (ledger.get().isMember("close_time")) { using tp = NetClock::time_point; using d = tp::duration; closeTime = tp{d{ledger.get()["close_time"].asUInt()}}; } - if (ledger.get().isMember ("close_time_resolution")) + if (ledger.get().isMember("close_time_resolution")) { using namespace std::chrono; - closeTimeResolution = seconds{ - ledger.get()["close_time_resolution"].asUInt()}; + closeTimeResolution = + seconds{ledger.get()["close_time_resolution"].asUInt()}; } - if (ledger.get().isMember ("close_time_estimated")) + if (ledger.get().isMember("close_time_estimated")) { closeTimeEstimated = ledger.get()["close_time_estimated"].asBool(); } - if (ledger.get().isMember ("total_coins")) + if (ledger.get().isMember("total_coins")) { - totalDrops = - beast::lexicalCastThrow - (ledger.get()["total_coins"].asString()); + totalDrops = beast::lexicalCastThrow( + ledger.get()["total_coins"].asString()); } ledger = ledger.get()["accountState"]; } - if (!ledger.get().isArrayOrNull ()) + if (!ledger.get().isArrayOrNull()) { - JLOG(m_journal.fatal()) - << "State nodes must be an array"; + JLOG(m_journal.fatal()) << "State nodes must be an array"; return nullptr; } - auto loadLedger = std::make_shared ( - seq, closeTime, *config_, family()); + auto loadLedger = + std::make_shared(seq, closeTime, *config_, family()); loadLedger->setTotalDrops(totalDrops); for (Json::UInt index = 0; index < ledger.get().size(); ++index) @@ -1906,63 +2012,60 @@ ApplicationImp::loadLedgerFromFile ( if (!entry.isObjectOrNull()) { - JLOG(m_journal.fatal()) - << "Invalid entry in ledger"; + JLOG(m_journal.fatal()) << "Invalid entry in ledger"; return nullptr; } uint256 uIndex; - if (!uIndex.SetHex (entry[jss::index].asString())) + if (!uIndex.SetHex(entry[jss::index].asString())) { - JLOG(m_journal.fatal()) - << "Invalid entry in ledger"; + JLOG(m_journal.fatal()) << "Invalid entry in ledger"; return nullptr; } - entry.removeMember (jss::index); + entry.removeMember(jss::index); - STParsedJSONObject stp ("sle", ledger.get()[index]); + STParsedJSONObject stp("sle", ledger.get()[index]); - if (!stp.object || uIndex.isZero ()) + if (!stp.object || uIndex.isZero()) { - JLOG(m_journal.fatal()) - << "Invalid entry in ledger"; + JLOG(m_journal.fatal()) << "Invalid entry in ledger"; return nullptr; } // VFALCO TODO This is the only place that // constructor is used, try to remove it - STLedgerEntry sle (*stp.object, uIndex); + STLedgerEntry sle(*stp.object, uIndex); - if (! loadLedger->addSLE (sle)) + if (!loadLedger->addSLE(sle)) { JLOG(m_journal.fatal()) - << "Couldn't add serialized ledger: " - << uIndex; + << "Couldn't add serialized ledger: " << uIndex; return nullptr; } } - loadLedger->stateMap().flushDirty ( + loadLedger->stateMap().flushDirty( hotACCOUNT_NODE, loadLedger->info().seq); - loadLedger->setAccepted (closeTime, - closeTimeResolution, ! closeTimeEstimated, - *config_); + loadLedger->setAccepted( + closeTime, closeTimeResolution, !closeTimeEstimated, *config_); return loadLedger; } catch (std::exception const& x) { - JLOG (m_journal.fatal()) << - "Ledger contains invalid data: " << x.what(); + JLOG(m_journal.fatal()) << "Ledger contains invalid data: " << x.what(); return nullptr; } } -bool ApplicationImp::loadOldLedger ( - std::string const& ledgerID, bool replay, bool isFileName) +bool +ApplicationImp::loadOldLedger( + std::string const& ledgerID, + bool replay, + bool isFileName) { try { @@ -1971,38 +2074,41 @@ bool ApplicationImp::loadOldLedger ( if (isFileName) { if (!ledgerID.empty()) - loadLedger = loadLedgerFromFile (ledgerID); + loadLedger = loadLedgerFromFile(ledgerID); } - else if (ledgerID.length () == 64) + else if (ledgerID.length() == 64) { uint256 hash; - if (hash.SetHex (ledgerID)) + if (hash.SetHex(ledgerID)) { - loadLedger = loadByHash (hash, *this); + loadLedger = loadByHash(hash, *this); if (!loadLedger) { // Try to build the ledger from the back end - auto il = std::make_shared ( - *this, hash, 0, InboundLedger::Reason::GENERIC, + auto il = std::make_shared( + *this, + hash, + 0, + InboundLedger::Reason::GENERIC, stopwatch()); - if (il->checkLocal ()) - loadLedger = il->getLedger (); + if (il->checkLocal()) + loadLedger = il->getLedger(); } } } - else if (ledgerID.empty () || boost::iequals(ledgerID, "latest")) + else if (ledgerID.empty() || boost::iequals(ledgerID, "latest")) { - loadLedger = getLastFullLedger (); + loadLedger = getLastFullLedger(); } else { // assume by sequence std::uint32_t index; - if (beast::lexicalCastChecked (index, ledgerID)) - loadLedger = loadByIndex (index, *this); + if (beast::lexicalCastChecked(index, ledgerID)) + loadLedger = loadByIndex(index, *this); } if (!loadLedger) @@ -2017,23 +2123,27 @@ bool ApplicationImp::loadOldLedger ( JLOG(m_journal.info()) << "Loading parent ledger"; - loadLedger = loadByHash (replayLedger->info().parentHash, *this); + loadLedger = loadByHash(replayLedger->info().parentHash, *this); if (!loadLedger) { - JLOG(m_journal.info()) << "Loading parent ledger from node store"; + JLOG(m_journal.info()) + << "Loading parent ledger from node store"; // Try to build the ledger from the back end - auto il = std::make_shared ( - *this, replayLedger->info().parentHash, - 0, InboundLedger::Reason::GENERIC, stopwatch()); + auto il = std::make_shared( + *this, + replayLedger->info().parentHash, + 0, + InboundLedger::Reason::GENERIC, + stopwatch()); - if (il->checkLocal ()) - loadLedger = il->getLedger (); + if (il->checkLocal()) + loadLedger = il->getLedger(); if (!loadLedger) { JLOG(m_journal.fatal()) << "Replay ledger missing/damaged"; - assert (false); + assert(false); return false; } } @@ -2041,52 +2151,53 @@ bool ApplicationImp::loadOldLedger ( using namespace std::chrono_literals; using namespace date; static constexpr NetClock::time_point ledgerWarnTimePoint{ - sys_days{January/1/2018} - sys_days{January/1/2000}}; + sys_days{January / 1 / 2018} - sys_days{January / 1 / 2000}}; if (loadLedger->info().closeTime < ledgerWarnTimePoint) { - JLOG(m_journal.fatal()) << - "\n\n*** WARNING ***\n" - "You are replaying a ledger from before " << - to_string(ledgerWarnTimePoint) << " UTC.\n" - "This replay will not handle your ledger as it was originally " - "handled.\nConsider running an earlier version of rippled to " - "get the older rules.\n*** CONTINUING ***\n"; + JLOG(m_journal.fatal()) + << "\n\n*** WARNING ***\n" + "You are replaying a ledger from before " + << to_string(ledgerWarnTimePoint) + << " UTC.\n" + "This replay will not handle your ledger as it was " + "originally " + "handled.\nConsider running an earlier version of rippled " + "to " + "get the older rules.\n*** CONTINUING ***\n"; } - JLOG(m_journal.info()) << - "Loading ledger " << loadLedger->info().hash << - " seq:" << loadLedger->info().seq; + JLOG(m_journal.info()) << "Loading ledger " << loadLedger->info().hash + << " seq:" << loadLedger->info().seq; - if (loadLedger->info().accountHash.isZero ()) + if (loadLedger->info().accountHash.isZero()) { JLOG(m_journal.fatal()) << "Ledger is empty."; - assert (false); + assert(false); return false; } - if (!loadLedger->walkLedger (journal ("Ledger"))) + if (!loadLedger->walkLedger(journal("Ledger"))) { JLOG(m_journal.fatal()) << "Ledger is missing nodes."; assert(false); return false; } - if (!loadLedger->assertSane (journal ("Ledger"))) + if (!loadLedger->assertSane(journal("Ledger"))) { JLOG(m_journal.fatal()) << "Ledger is not sane."; assert(false); return false; } - m_ledgerMaster->setLedgerRangePresent ( - loadLedger->info().seq, - loadLedger->info().seq); + m_ledgerMaster->setLedgerRangePresent( + loadLedger->info().seq, loadLedger->info().seq); - m_ledgerMaster->switchLCL (loadLedger); + m_ledgerMaster->switchLCL(loadLedger); loadLedger->setValidated(); m_ledgerMaster->setFullLedger(loadLedger, true, false); - openLedger_.emplace(loadLedger, cachedSLEs_, - logs_->journal("OpenLedger")); + openLedger_.emplace( + loadLedger, cachedSLEs_, logs_->journal("OpenLedger")); if (replay) { @@ -2100,27 +2211,25 @@ bool ApplicationImp::loadOldLedger ( (void)_; auto txID = tx->getTransactionID(); - auto s = std::make_shared (); + auto s = std::make_shared(); tx->add(*s); - forceValidity(getHashRouter(), - txID, Validity::SigGoodOnly); + forceValidity(getHashRouter(), txID, Validity::SigGoodOnly); openLedger_->modify( - [&txID, &s](OpenView& view, beast::Journal j) - { - view.rawTxInsert (txID, std::move (s), nullptr); + [&txID, &s](OpenView& view, beast::Journal j) { + view.rawTxInsert(txID, std::move(s), nullptr); return true; }); } - m_ledgerMaster->takeReplay (std::move (replayData)); + m_ledgerMaster->takeReplay(std::move(replayData)); } } catch (SHAMapMissingNode const& mn) { - JLOG(m_journal.fatal()) << - "While loading specified ledger: " << mn.what(); + JLOG(m_journal.fatal()) + << "While loading specified ledger: " << mn.what(); return false; } catch (boost::bad_lexical_cast&) @@ -2133,24 +2242,25 @@ bool ApplicationImp::loadOldLedger ( return true; } -bool ApplicationImp::serverOkay (std::string& reason) +bool +ApplicationImp::serverOkay(std::string& reason) { - if (! config().ELB_SUPPORT) + if (!config().ELB_SUPPORT) return true; - if (isShutdown ()) + if (isShutdown()) { reason = "Server is shutting down"; return false; } - if (getOPs ().isNeedNetworkLedger ()) + if (getOPs().isNeedNetworkLedger()) { reason = "Not synchronized with network yet"; return false; } - if (getOPs ().getOperatingMode () < OperatingMode::SYNCING) + if (getOPs().getOperatingMode() < OperatingMode::SYNCING) { reason = "Not synchronized with network"; return false; @@ -2159,13 +2269,13 @@ bool ApplicationImp::serverOkay (std::string& reason) if (!getLedgerMaster().isCaughtUp(reason)) return false; - if (getFeeTrack ().isLoadedLocal ()) + if (getFeeTrack().isLoadedLocal()) { reason = "Too much load"; return false; } - if (getOPs ().isAmendmentBlocked ()) + if (getOPs().isAmendmentBlocked()) { reason = "Server version too old"; return false; @@ -2175,56 +2285,57 @@ bool ApplicationImp::serverOkay (std::string& reason) } beast::Journal -ApplicationImp::journal (std::string const& name) +ApplicationImp::journal(std::string const& name) { - return logs_->journal (name); + return logs_->journal(name); } -bool ApplicationImp::nodeToShards() +bool +ApplicationImp::nodeToShards() { assert(overlay_); assert(!config_->standalone()); if (config_->section(ConfigSection::shardDatabase()).empty()) { - JLOG (m_journal.fatal()) << - "The [shard_db] configuration setting must be set"; + JLOG(m_journal.fatal()) + << "The [shard_db] configuration setting must be set"; return false; } if (!shardStore_) { - JLOG(m_journal.fatal()) << - "Invalid [shard_db] configuration"; + JLOG(m_journal.fatal()) << "Invalid [shard_db] configuration"; return false; } shardStore_->import(getNodeStore()); return true; } -bool ApplicationImp::validateShards() +bool +ApplicationImp::validateShards() { assert(overlay_); assert(!config_->standalone()); if (config_->section(ConfigSection::shardDatabase()).empty()) { - JLOG (m_journal.fatal()) << - "The [shard_db] configuration setting must be set"; + JLOG(m_journal.fatal()) + << "The [shard_db] configuration setting must be set"; return false; } if (!shardStore_) { - JLOG(m_journal.fatal()) << - "Invalid [shard_db] configuration"; + JLOG(m_journal.fatal()) << "Invalid [shard_db] configuration"; return false; } shardStore_->validate(); return true; } -void ApplicationImp::setMaxDisallowedLedger() +void +ApplicationImp::setMaxDisallowedLedger() { - boost::optional seq; + boost::optional seq; { auto db = getLedgerDB().checkoutDb(); *db << "SELECT MAX(LedgerSeq) FROM Ledgers;", soci::into(seq); @@ -2232,29 +2343,26 @@ void ApplicationImp::setMaxDisallowedLedger() if (seq) maxDisallowedLedger_ = *seq; - JLOG (m_journal.trace()) << "Max persisted ledger is " - << maxDisallowedLedger_; + JLOG(m_journal.trace()) + << "Max persisted ledger is " << maxDisallowedLedger_; } - //------------------------------------------------------------------------------ -Application::Application () - : beast::PropertyStream::Source ("app") +Application::Application() : beast::PropertyStream::Source("app") { } //------------------------------------------------------------------------------ std::unique_ptr -make_Application ( +make_Application( std::unique_ptr config, std::unique_ptr logs, std::unique_ptr timeKeeper) { - return std::make_unique ( - std::move(config), std::move(logs), - std::move(timeKeeper)); + return std::make_unique( + std::move(config), std::move(logs), std::move(timeKeeper)); } -} +} // namespace ripple diff --git a/src/ripple/app/main/Application.h b/src/ripple/app/main/Application.h index 5ffa87552d..6b7fc7d74a 100644 --- a/src/ripple/app/main/Application.h +++ b/src/ripple/app/main/Application.h @@ -20,23 +20,32 @@ #ifndef RIPPLE_APP_MAIN_APPLICATION_H_INCLUDED #define RIPPLE_APP_MAIN_APPLICATION_H_INCLUDED +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include #include #include #include namespace ripple { -namespace unl { class Manager; } -namespace Resource { class Manager; } -namespace NodeStore { class Database; class DatabaseShard; } -namespace perf { class PerfLog; } +namespace unl { +class Manager; +} +namespace Resource { +class Manager; +} +namespace NodeStore { +class Database; +class DatabaseShard; +} // namespace NodeStore +namespace perf { +class PerfLog; +} // VFALCO TODO Fix forward declares required for header dependency loops class AmendmentTable; @@ -75,7 +84,7 @@ class Cluster; class DatabaseCon; class SHAMapStore; -using NodeCache = TaggedCache ; +using NodeCache = TaggedCache; template class Validations; @@ -97,110 +106,158 @@ public: other things */ using MutexType = std::recursive_mutex; - virtual MutexType& getMasterMutex () = 0; + virtual MutexType& + getMasterMutex() = 0; public: - Application (); + Application(); - virtual ~Application () = default; + virtual ~Application() = default; - virtual bool setup() = 0; - virtual void doStart(bool withTimers) = 0; - virtual void run() = 0; - virtual bool isShutdown () = 0; - virtual void signalStop () = 0; - virtual bool checkSigs() const = 0; - virtual void checkSigs(bool) = 0; + virtual bool + setup() = 0; + virtual void + doStart(bool withTimers) = 0; + virtual void + run() = 0; + virtual bool + isShutdown() = 0; + virtual void + signalStop() = 0; + virtual bool + checkSigs() const = 0; + virtual void + checkSigs(bool) = 0; // // --- // - virtual Logs& logs() = 0; - virtual Config& config() = 0; + virtual Logs& + logs() = 0; + virtual Config& + config() = 0; - virtual - boost::asio::io_service& - getIOService () = 0; + virtual boost::asio::io_service& + getIOService() = 0; - virtual CollectorManager& getCollectorManager () = 0; - virtual Family& family() = 0; - virtual Family* shardFamily() = 0; - virtual TimeKeeper& timeKeeper() = 0; - virtual JobQueue& getJobQueue () = 0; - virtual NodeCache& getTempNodeCache () = 0; - virtual CachedSLEs& cachedSLEs() = 0; - virtual AmendmentTable& getAmendmentTable() = 0; - virtual HashRouter& getHashRouter () = 0; - virtual LoadFeeTrack& getFeeTrack () = 0; - virtual LoadManager& getLoadManager () = 0; - virtual Overlay& overlay () = 0; - virtual TxQ& getTxQ() = 0; - virtual ValidatorList& validators () = 0; - virtual ValidatorSite& validatorSites () = 0; - virtual ManifestCache& validatorManifests () = 0; - virtual ManifestCache& publisherManifests () = 0; - virtual Cluster& cluster () = 0; - virtual PeerReservationTable& peerReservations () = 0; - virtual RCLValidations& getValidations () = 0; - virtual NodeStore::Database& getNodeStore () = 0; - virtual NodeStore::DatabaseShard* getShardStore() = 0; - virtual InboundLedgers& getInboundLedgers () = 0; - virtual InboundTransactions& getInboundTransactions () = 0; + virtual CollectorManager& + getCollectorManager() = 0; + virtual Family& + family() = 0; + virtual Family* + shardFamily() = 0; + virtual TimeKeeper& + timeKeeper() = 0; + virtual JobQueue& + getJobQueue() = 0; + virtual NodeCache& + getTempNodeCache() = 0; + virtual CachedSLEs& + cachedSLEs() = 0; + virtual AmendmentTable& + getAmendmentTable() = 0; + virtual HashRouter& + getHashRouter() = 0; + virtual LoadFeeTrack& + getFeeTrack() = 0; + virtual LoadManager& + getLoadManager() = 0; + virtual Overlay& + overlay() = 0; + virtual TxQ& + getTxQ() = 0; + virtual ValidatorList& + validators() = 0; + virtual ValidatorSite& + validatorSites() = 0; + virtual ManifestCache& + validatorManifests() = 0; + virtual ManifestCache& + publisherManifests() = 0; + virtual Cluster& + cluster() = 0; + virtual PeerReservationTable& + peerReservations() = 0; + virtual RCLValidations& + getValidations() = 0; + virtual NodeStore::Database& + getNodeStore() = 0; + virtual NodeStore::DatabaseShard* + getShardStore() = 0; + virtual InboundLedgers& + getInboundLedgers() = 0; + virtual InboundTransactions& + getInboundTransactions() = 0; - virtual - TaggedCache & - getAcceptedLedgerCache () = 0; + virtual TaggedCache& + getAcceptedLedgerCache() = 0; - virtual LedgerMaster& getLedgerMaster () = 0; - virtual NetworkOPs& getOPs () = 0; - virtual OrderBookDB& getOrderBookDB () = 0; - virtual TransactionMaster& getMasterTransaction () = 0; - virtual perf::PerfLog& getPerfLog () = 0; + virtual LedgerMaster& + getLedgerMaster() = 0; + virtual NetworkOPs& + getOPs() = 0; + virtual OrderBookDB& + getOrderBookDB() = 0; + virtual TransactionMaster& + getMasterTransaction() = 0; + virtual perf::PerfLog& + getPerfLog() = 0; - virtual - std::pair const& - nodeIdentity () = 0; + virtual std::pair const& + nodeIdentity() = 0; - virtual - PublicKey const & - getValidationPublicKey() const = 0; + virtual PublicKey const& + getValidationPublicKey() const = 0; - virtual Resource::Manager& getResourceManager () = 0; - virtual PathRequests& getPathRequests () = 0; - virtual SHAMapStore& getSHAMapStore () = 0; - virtual PendingSaves& pendingSaves() = 0; - virtual AccountIDCache const& accountIDCache() const = 0; - virtual OpenLedger& openLedger() = 0; - virtual OpenLedger const& openLedger() const = 0; - virtual DatabaseCon& getTxnDB () = 0; - virtual DatabaseCon& getLedgerDB () = 0; + virtual Resource::Manager& + getResourceManager() = 0; + virtual PathRequests& + getPathRequests() = 0; + virtual SHAMapStore& + getSHAMapStore() = 0; + virtual PendingSaves& + pendingSaves() = 0; + virtual AccountIDCache const& + accountIDCache() const = 0; + virtual OpenLedger& + openLedger() = 0; + virtual OpenLedger const& + openLedger() const = 0; + virtual DatabaseCon& + getTxnDB() = 0; + virtual DatabaseCon& + getLedgerDB() = 0; - virtual - std::chrono::milliseconds - getIOLatency () = 0; + virtual std::chrono::milliseconds + getIOLatency() = 0; - virtual bool serverOkay (std::string& reason) = 0; + virtual bool + serverOkay(std::string& reason) = 0; - virtual beast::Journal journal (std::string const& name) = 0; + virtual beast::Journal + journal(std::string const& name) = 0; /* Returns the number of file descriptors the application needs */ - virtual int fdRequired() const = 0; + virtual int + fdRequired() const = 0; /** Retrieve the "wallet database" */ - virtual DatabaseCon& getWalletDB () = 0; + virtual DatabaseCon& + getWalletDB() = 0; /** Ensure that a newly-started validator does not sign proposals older * than the last ledger it persisted. */ - virtual LedgerIndex getMaxDisallowedLedger() = 0; + virtual LedgerIndex + getMaxDisallowedLedger() = 0; }; -std::unique_ptr +std::unique_ptr make_Application( std::unique_ptr config, std::unique_ptr logs, std::unique_ptr timeKeeper); -} +} // namespace ripple #endif diff --git a/src/ripple/app/main/BasicApp.cpp b/src/ripple/app/main/BasicApp.cpp index d86447509f..77a792065f 100644 --- a/src/ripple/app/main/BasicApp.cpp +++ b/src/ripple/app/main/BasicApp.cpp @@ -22,17 +22,16 @@ BasicApp::BasicApp(std::size_t numberOfThreads) { - work_.emplace (io_service_); + work_.emplace(io_service_); threads_.reserve(numberOfThreads); - while(numberOfThreads--) + while (numberOfThreads--) { - threads_.emplace_back([this, numberOfThreads]() - { - beast::setCurrentThreadName("io svc #" + - std::to_string(numberOfThreads)); - this->io_service_.run(); - }); + threads_.emplace_back([this, numberOfThreads]() { + beast::setCurrentThreadName( + "io svc #" + std::to_string(numberOfThreads)); + this->io_service_.run(); + }); } } diff --git a/src/ripple/app/main/CollectorManager.cpp b/src/ripple/app/main/CollectorManager.cpp index fdb481915b..d23aeb8a3f 100644 --- a/src/ripple/app/main/CollectorManager.cpp +++ b/src/ripple/app/main/CollectorManager.cpp @@ -22,46 +22,47 @@ namespace ripple { -class CollectorManagerImp - : public CollectorManager +class CollectorManagerImp : public CollectorManager { public: beast::Journal m_journal; beast::insight::Collector::ptr m_collector; - std::unique_ptr m_groups; + std::unique_ptr m_groups; - CollectorManagerImp (Section const& params, - beast::Journal journal) - : m_journal (journal) + CollectorManagerImp(Section const& params, beast::Journal journal) + : m_journal(journal) { - std::string const& server = get (params, "server"); + std::string const& server = get(params, "server"); if (server == "statsd") { - beast::IP::Endpoint const address (beast::IP::Endpoint::from_string ( - get (params, "address"))); - std::string const& prefix (get (params, "prefix")); + beast::IP::Endpoint const address(beast::IP::Endpoint::from_string( + get(params, "address"))); + std::string const& prefix(get(params, "prefix")); - m_collector = beast::insight::StatsDCollector::New (address, prefix, journal); + m_collector = + beast::insight::StatsDCollector::New(address, prefix, journal); } else { - m_collector = beast::insight::NullCollector::New (); + m_collector = beast::insight::NullCollector::New(); } - m_groups = beast::insight::make_Groups (m_collector); + m_groups = beast::insight::make_Groups(m_collector); } ~CollectorManagerImp() = default; - beast::insight::Collector::ptr const& collector () override + beast::insight::Collector::ptr const& + collector() override { return m_collector; } - beast::insight::Group::ptr const& group (std::string const& name) override + beast::insight::Group::ptr const& + group(std::string const& name) override { - return m_groups->get (name); + return m_groups->get(name); } }; @@ -69,10 +70,10 @@ public: CollectorManager::~CollectorManager() = default; -std::unique_ptr CollectorManager::New(Section const& params, - beast::Journal journal) +std::unique_ptr +CollectorManager::New(Section const& params, beast::Journal journal) { return std::make_unique(params, journal); } -} +} // namespace ripple diff --git a/src/ripple/app/main/CollectorManager.h b/src/ripple/app/main/CollectorManager.h index eb2c4abd9b..0b352a57a3 100644 --- a/src/ripple/app/main/CollectorManager.h +++ b/src/ripple/app/main/CollectorManager.h @@ -29,15 +29,16 @@ namespace ripple { class CollectorManager { public: - static std::unique_ptr New ( - Section const& params, beast::Journal journal); + static std::unique_ptr + New(Section const& params, beast::Journal journal); - virtual ~CollectorManager () = 0; - virtual beast::insight::Collector::ptr const& collector () = 0; - virtual beast::insight::Group::ptr const& group ( - std::string const& name) = 0; + virtual ~CollectorManager() = 0; + virtual beast::insight::Collector::ptr const& + collector() = 0; + virtual beast::insight::Group::ptr const& + group(std::string const& name) = 0; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/main/DBInit.h b/src/ripple/app/main/DBInit.h index 69d9ccf454..2aa183d4e7 100644 --- a/src/ripple/app/main/DBInit.h +++ b/src/ripple/app/main/DBInit.h @@ -27,20 +27,17 @@ namespace ripple { //////////////////////////////////////////////////////////////////////////////// // Ledger database holds ledgers and ledger confirmations -inline constexpr auto LgrDBName {"ledger.db"}; +inline constexpr auto LgrDBName{"ledger.db"}; -inline constexpr -std::array LgrDBPragma {{ - "PRAGMA synchronous=NORMAL;", - "PRAGMA journal_mode=WAL;", - "PRAGMA journal_size_limit=1582080;" -}}; +inline constexpr std::array LgrDBPragma{ + {"PRAGMA synchronous=NORMAL;", + "PRAGMA journal_mode=WAL;", + "PRAGMA journal_size_limit=1582080;"}}; -inline constexpr -std::array LgrDBInit {{ - "BEGIN TRANSACTION;", +inline constexpr std::array LgrDBInit{ + {"BEGIN TRANSACTION;", - "CREATE TABLE IF NOT EXISTS Ledgers ( \ + "CREATE TABLE IF NOT EXISTS Ledgers ( \ LedgerHash CHARACTER(64) PRIMARY KEY, \ LedgerSeq BIGINT UNSIGNED, \ PrevHash CHARACTER(64), \ @@ -52,40 +49,40 @@ std::array LgrDBInit {{ AccountSetHash CHARACTER(64), \ TransSetHash CHARACTER(64) \ );", - "CREATE INDEX IF NOT EXISTS SeqLedger ON Ledgers(LedgerSeq);", + "CREATE INDEX IF NOT EXISTS SeqLedger ON Ledgers(LedgerSeq);", - // Old table and indexes no longer needed - "DROP TABLE IF EXISTS Validations;", + // Old table and indexes no longer needed + "DROP TABLE IF EXISTS Validations;", - "END TRANSACTION;" -}}; + "END TRANSACTION;"}}; //////////////////////////////////////////////////////////////////////////////// // Transaction database holds transactions and public keys -inline constexpr auto TxDBName {"transaction.db"}; +inline constexpr auto TxDBName{"transaction.db"}; inline constexpr -#if (ULONG_MAX > UINT_MAX) && !defined (NO_SQLITE_MMAP) - std::array TxDBPragma {{ +#if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP) + std::array + TxDBPragma +{ + { #else std::array TxDBPragma {{ #endif - "PRAGMA page_size=4096;", - "PRAGMA synchronous=NORMAL;", - "PRAGMA journal_mode=WAL;", - "PRAGMA journal_size_limit=1582080;", - "PRAGMA max_page_count=2147483646;", -#if (ULONG_MAX > UINT_MAX) && !defined (NO_SQLITE_MMAP) - "PRAGMA mmap_size=17179869184;" + "PRAGMA page_size=4096;", "PRAGMA synchronous=NORMAL;", + "PRAGMA journal_mode=WAL;", "PRAGMA journal_size_limit=1582080;", + "PRAGMA max_page_count=2147483646;", +#if (ULONG_MAX > UINT_MAX) && !defined(NO_SQLITE_MMAP) + "PRAGMA mmap_size=17179869184;" #endif -}}; + } +}; -inline constexpr -std::array TxDBInit {{ - "BEGIN TRANSACTION;", +inline constexpr std::array TxDBInit{ + {"BEGIN TRANSACTION;", - "CREATE TABLE IF NOT EXISTS Transactions ( \ + "CREATE TABLE IF NOT EXISTS Transactions ( \ TransID CHARACTER(64) PRIMARY KEY, \ TransType CHARACTER(24), \ FromAcct CHARACTER(35), \ @@ -95,131 +92,111 @@ std::array TxDBInit {{ RawTxn BLOB, \ TxnMeta BLOB \ );", - "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \ + "CREATE INDEX IF NOT EXISTS TxLgrIndex ON \ Transactions(LedgerSeq);", - "CREATE TABLE IF NOT EXISTS AccountTransactions ( \ + "CREATE TABLE IF NOT EXISTS AccountTransactions ( \ TransID CHARACTER(64), \ Account CHARACTER(64), \ LedgerSeq BIGINT UNSIGNED, \ TxnSeq INTEGER \ );", - "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \ + "CREATE INDEX IF NOT EXISTS AcctTxIDIndex ON \ AccountTransactions(TransID);", - "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \ + "CREATE INDEX IF NOT EXISTS AcctTxIndex ON \ AccountTransactions(Account, LedgerSeq, TxnSeq, TransID);", - "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \ + "CREATE INDEX IF NOT EXISTS AcctLgrIndex ON \ AccountTransactions(LedgerSeq, Account, TransID);", - "END TRANSACTION;" -}}; + "END TRANSACTION;"}}; //////////////////////////////////////////////////////////////////////////////// // Temporary database used with an incomplete shard that is being acquired -inline constexpr auto AcquireShardDBName {"acquire.db"}; +inline constexpr auto AcquireShardDBName{"acquire.db"}; -inline constexpr -std::array AcquireShardDBPragma {{ - "PRAGMA synchronous=NORMAL;", - "PRAGMA journal_mode=WAL;", - "PRAGMA journal_size_limit=1582080;" -}}; +inline constexpr std::array AcquireShardDBPragma{ + {"PRAGMA synchronous=NORMAL;", + "PRAGMA journal_mode=WAL;", + "PRAGMA journal_size_limit=1582080;"}}; -inline constexpr -std::array AcquireShardDBInit {{ - "CREATE TABLE IF NOT EXISTS Shard ( \ +inline constexpr std::array AcquireShardDBInit{ + {"CREATE TABLE IF NOT EXISTS Shard ( \ ShardIndex INTEGER PRIMARY KEY, \ LastLedgerHash CHARACTER(64), \ StoredLedgerSeqs BLOB \ - );" -}}; + );"}}; //////////////////////////////////////////////////////////////////////////////// // Pragma for Ledger and Transaction databases with complete shards -inline constexpr -std::array CompleteShardDBPragma{{ - "PRAGMA synchronous=OFF;", - "PRAGMA journal_mode=OFF;" -}}; +inline constexpr std::array CompleteShardDBPragma{ + {"PRAGMA synchronous=OFF;", "PRAGMA journal_mode=OFF;"}}; //////////////////////////////////////////////////////////////////////////////// -inline constexpr auto WalletDBName {"wallet.db"}; +inline constexpr auto WalletDBName{"wallet.db"}; -inline constexpr -std::array WalletDBInit {{ - "BEGIN TRANSACTION;", +inline constexpr std::array WalletDBInit{ + {"BEGIN TRANSACTION;", - // A node's identity must be persisted, including - // for clustering purposes. This table holds one - // entry: the server's unique identity, but the - // value can be overriden by specifying a node - // identity in the config file using a [node_seed] - // entry. - "CREATE TABLE IF NOT EXISTS NodeIdentity ( \ + // A node's identity must be persisted, including + // for clustering purposes. This table holds one + // entry: the server's unique identity, but the + // value can be overriden by specifying a node + // identity in the config file using a [node_seed] + // entry. + "CREATE TABLE IF NOT EXISTS NodeIdentity ( \ PublicKey CHARACTER(53), \ PrivateKey CHARACTER(52) \ );", - // Peer reservations - "CREATE TABLE IF NOT EXISTS PeerReservations ( \ + // Peer reservations + "CREATE TABLE IF NOT EXISTS PeerReservations ( \ PublicKey CHARACTER(53) UNIQUE NOT NULL, \ Description CHARACTER(64) NOT NULL \ );", - // Validator Manifests - "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \ + // Validator Manifests + "CREATE TABLE IF NOT EXISTS ValidatorManifests ( \ RawData BLOB NOT NULL \ );", - "CREATE TABLE IF NOT EXISTS PublisherManifests ( \ + "CREATE TABLE IF NOT EXISTS PublisherManifests ( \ RawData BLOB NOT NULL \ );", - "END TRANSACTION;" -}}; + "END TRANSACTION;"}}; //////////////////////////////////////////////////////////////////////////////// -static constexpr auto stateDBName {"state.db"}; +static constexpr auto stateDBName{"state.db"}; -static constexpr -std::array DownloaderDBPragma -{{ - "PRAGMA synchronous=FULL;", - "PRAGMA journal_mode=DELETE;" -}}; +static constexpr std::array DownloaderDBPragma{ + {"PRAGMA synchronous=FULL;", "PRAGMA journal_mode=DELETE;"}}; -static constexpr -std::array ShardArchiveHandlerDBInit -{{ - "BEGIN TRANSACTION;", +static constexpr std::array ShardArchiveHandlerDBInit{ + {"BEGIN TRANSACTION;", - "CREATE TABLE IF NOT EXISTS State ( \ + "CREATE TABLE IF NOT EXISTS State ( \ ShardIndex INTEGER PRIMARY KEY, \ URL TEXT \ );", - "END TRANSACTION;" -}}; + "END TRANSACTION;"}}; -static constexpr -std::array DatabaseBodyDBInit -{{ - "BEGIN TRANSACTION;", +static constexpr std::array DatabaseBodyDBInit{ + {"BEGIN TRANSACTION;", - "CREATE TABLE IF NOT EXISTS download ( \ + "CREATE TABLE IF NOT EXISTS download ( \ Path TEXT, \ Data BLOB, \ Size BIGINT UNSIGNED, \ Part BIGINT UNSIGNED PRIMARY KEY \ );", - "END TRANSACTION;" -}}; + "END TRANSACTION;"}}; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/main/GRPCServer.cpp b/src/ripple/app/main/GRPCServer.cpp index 4baae0890a..acf33784f3 100644 --- a/src/ripple/app/main/GRPCServer.cpp +++ b/src/ripple/app/main/GRPCServer.cpp @@ -100,15 +100,14 @@ GRPCServerImpl::CallData::process() JobType::jtRPC, "gRPC-Client", [thisShared](std::shared_ptr coro) { - thisShared->process(coro); }); // If coro is null, then the JobQueue has already been shutdown if (!coro) { - grpc::Status status{grpc::StatusCode::INTERNAL, - "Job Queue is already stopped"}; + grpc::Status status{ + grpc::StatusCode::INTERNAL, "Job Queue is already stopped"}; responder_.FinishWithError(status, this); } } @@ -123,8 +122,9 @@ GRPCServerImpl::CallData::process( auto usage = getUsage(); if (usage.disconnect()) { - grpc::Status status{grpc::StatusCode::RESOURCE_EXHAUSTED, - "usage balance exceeds threshhold"}; + grpc::Status status{ + grpc::StatusCode::RESOURCE_EXHAUSTED, + "usage balance exceeds threshhold"}; responder_.FinishWithError(status, this); } else @@ -133,16 +133,17 @@ GRPCServerImpl::CallData::process( usage.charge(loadType); auto role = getRole(); - RPC::GRPCContext context{{app_.journal("gRPCServer"), - app_, - loadType, - app_.getOPs(), - app_.getLedgerMaster(), - usage, - role, - coro, - InfoSub::pointer()}, - request_}; + RPC::GRPCContext context{ + {app_.journal("gRPCServer"), + app_, + loadType, + app_.getOPs(), + app_.getLedgerMaster(), + usage, + role, + coro, + InfoSub::pointer()}, + request_}; // Make sure we can currently handle the rpc error_code_i conditionMetRes = @@ -151,8 +152,9 @@ GRPCServerImpl::CallData::process( if (conditionMetRes != rpcSUCCESS) { RPC::ErrorInfo errorInfo = RPC::get_error_info(conditionMetRes); - grpc::Status status{grpc::StatusCode::FAILED_PRECONDITION, - errorInfo.message.c_str()}; + grpc::Status status{ + grpc::StatusCode::FAILED_PRECONDITION, + errorInfo.message.c_str()}; responder_.FinishWithError(status, this); } else @@ -234,13 +236,13 @@ GRPCServerImpl::shutdown() { JLOG(journal_.debug()) << "Shutting down"; - //The below call cancels all "listeners" (CallData objects that are waiting - //for a request, as opposed to processing a request), and blocks until all - //requests being processed are completed. CallData objects in the midst of - //processing requests need to actually send data back to the client, via - //responder_.Finish(...) or responder_.FinishWithError(...), for this call - //to unblock. Each cancelled listener is returned via cq_.Next(...) with ok - //set to false + // The below call cancels all "listeners" (CallData objects that are waiting + // for a request, as opposed to processing a request), and blocks until all + // requests being processed are completed. CallData objects in the midst of + // processing requests need to actually send data back to the client, via + // responder_.Finish(...) or responder_.FinishWithError(...), for this call + // to unblock. Each cancelled listener is returned via cq_.Next(...) with ok + // set to false server_->Shutdown(); JLOG(journal_.debug()) << "Server has been shutdown"; @@ -249,7 +251,6 @@ GRPCServerImpl::shutdown() // queue have been processed. See handleRpcs() for more details. cq_->Shutdown(); JLOG(journal_.debug()) << "Completion Queue has been shutdown"; - } void @@ -292,13 +293,12 @@ GRPCServerImpl::handleRpcs() { auto ptr = static_cast(tag); JLOG(journal_.trace()) << "Processing CallData object." - << " ptr = " << ptr - << " ok = " << ok; + << " ptr = " << ptr << " ok = " << ok; if (!ok) { JLOG(journal_.debug()) << "Request listener cancelled. " - << "Destroying object"; + << "Destroying object"; erase(ptr); } else @@ -315,7 +315,6 @@ GRPCServerImpl::handleRpcs() } else { - JLOG(journal_.debug()) << "Sent response. Destroying object"; erase(ptr); } @@ -335,13 +334,16 @@ GRPCServerImpl::setupListeners() }; { - using cd = CallData; + using cd = CallData< + org::xrpl::rpc::v1::GetFeeRequest, + org::xrpl::rpc::v1::GetFeeResponse>; addToRequests(std::make_shared( service_, *cq_, app_, - &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::RequestGetFee, + &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService:: + RequestGetFee, doFeeGrpc, RPC::NEEDS_CURRENT_LEDGER, Resource::feeReferenceRPC)); @@ -355,7 +357,8 @@ GRPCServerImpl::setupListeners() service_, *cq_, app_, - &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::RequestGetAccountInfo, + &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService:: + RequestGetAccountInfo, doAccountInfoGrpc, RPC::NO_CONDITION, Resource::feeReferenceRPC)); @@ -369,7 +372,8 @@ GRPCServerImpl::setupListeners() service_, *cq_, app_, - &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService::RequestGetTransaction, + &org::xrpl::rpc::v1::XRPLedgerAPIService::AsyncService:: + RequestGetTransaction, doTxGrpc, RPC::NEEDS_CURRENT_LEDGER, Resource::feeReferenceRPC)); diff --git a/src/ripple/app/main/LoadManager.cpp b/src/ripple/app/main/LoadManager.cpp index d2f19349dc..5c481c5c3e 100644 --- a/src/ripple/app/main/LoadManager.cpp +++ b/src/ripple/app/main/LoadManager.cpp @@ -17,93 +17,101 @@ */ //============================================================================== -#include #include +#include #include #include #include -#include #include +#include #include #include #include namespace ripple { -LoadManager::LoadManager ( - Application& app, Stoppable& parent, beast::Journal journal) - : Stoppable ("LoadManager", parent) - , app_ (app) - , journal_ (journal) - , deadLock_ () - , armed_ (false) - , stop_ (false) +LoadManager::LoadManager( + Application& app, + Stoppable& parent, + beast::Journal journal) + : Stoppable("LoadManager", parent) + , app_(app) + , journal_(journal) + , deadLock_() + , armed_(false) + , stop_(false) { } -LoadManager::~LoadManager () +LoadManager::~LoadManager() { try { - onStop (); + onStop(); } catch (std::exception const& ex) { // Swallow the exception in a destructor. - JLOG(journal_.warn()) << "std::exception in ~LoadManager. " - << ex.what(); + JLOG(journal_.warn()) + << "std::exception in ~LoadManager. " << ex.what(); } } //------------------------------------------------------------------------------ -void LoadManager::activateDeadlockDetector () +void +LoadManager::activateDeadlockDetector() { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); armed_ = true; deadLock_ = std::chrono::steady_clock::now(); } -void LoadManager::resetDeadlockDetector () +void +LoadManager::resetDeadlockDetector() { auto const detector_start = std::chrono::steady_clock::now(); - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); deadLock_ = detector_start; } //------------------------------------------------------------------------------ -void LoadManager::onPrepare () +void +LoadManager::onPrepare() { } -void LoadManager::onStart () +void +LoadManager::onStart() { JLOG(journal_.debug()) << "Starting"; - assert (! thread_.joinable()); + assert(!thread_.joinable()); - thread_ = std::thread {&LoadManager::run, this}; + thread_ = std::thread{&LoadManager::run, this}; } -void LoadManager::onStop () +void +LoadManager::onStop() { if (thread_.joinable()) { JLOG(journal_.debug()) << "Stopping"; { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); stop_ = true; } thread_.join(); } - stopped (); + stopped(); } //------------------------------------------------------------------------------ -void LoadManager::run () +void +LoadManager::run() { - beast::setCurrentThreadName ("LoadManager"); + beast::setCurrentThreadName("LoadManager"); using namespace std::chrono_literals; using clock_type = std::chrono::system_clock; @@ -111,11 +119,11 @@ void LoadManager::run () auto t = clock_type::now(); bool stop = false; - while (! (stop || isStopping ())) + while (!(stop || isStopping())) { { // Copy out shared data under a lock. Use copies outside lock. - std::unique_lock sl (mutex_); + std::unique_lock sl(mutex_); auto const deadLock = deadLock_; auto const armed = armed_; stop = stop_; @@ -131,8 +139,8 @@ void LoadManager::run () constexpr auto deadlockLogicErrorTimeLimit = 600s; if (armed && (timeSpentDeadlocked >= reportingIntervalSeconds)) { - - // Report the deadlocked condition every reportingIntervalSeconds + // Report the deadlocked condition every + // reportingIntervalSeconds if ((timeSpentDeadlocked % reportingIntervalSeconds) == 0s) { if (timeSpentDeadlocked < deadlockFatalLogMessageTimeLimit) @@ -154,9 +162,9 @@ void LoadManager::run () } } - // If we go over the deadlockTimeLimit spent deadlocked, it means that - // the deadlock resolution code has failed, which qualifies - // as undefined behavior. + // If we go over the deadlockTimeLimit spent deadlocked, it + // means that the deadlock resolution code has failed, which + // qualifies as undefined behavior. // if (timeSpentDeadlocked >= deadlockLogicErrorTimeLimit) { @@ -173,21 +181,21 @@ void LoadManager::run () } bool change = false; - if (app_.getJobQueue ().isOverloaded ()) + if (app_.getJobQueue().isOverloaded()) { - JLOG(journal_.info()) << app_.getJobQueue ().getJson (0); - change = app_.getFeeTrack ().raiseLocalFee (); + JLOG(journal_.info()) << app_.getJobQueue().getJson(0); + change = app_.getFeeTrack().raiseLocalFee(); } else { - change = app_.getFeeTrack ().lowerLocalFee (); + change = app_.getFeeTrack().lowerLocalFee(); } if (change) { // VFALCO TODO replace this with a Listener / observer and // subscribe in NetworkOPs or Application. - app_.getOPs ().reportFeeChange (); + app_.getOPs().reportFeeChange(); } t += 1s; @@ -204,16 +212,15 @@ void LoadManager::run () } } - stopped (); + stopped(); } //------------------------------------------------------------------------------ std::unique_ptr -make_LoadManager (Application& app, - Stoppable& parent, beast::Journal journal) +make_LoadManager(Application& app, Stoppable& parent, beast::Journal journal) { return std::unique_ptr{new LoadManager{app, parent, journal}}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/main/LoadManager.h b/src/ripple/app/main/LoadManager.h index de504df259..4ff3d8b8a8 100644 --- a/src/ripple/app/main/LoadManager.h +++ b/src/ripple/app/main/LoadManager.h @@ -42,18 +42,19 @@ class Application; */ class LoadManager : public Stoppable { - LoadManager (Application& app, Stoppable& parent, beast::Journal journal); + LoadManager(Application& app, Stoppable& parent, beast::Journal journal); public: - LoadManager () = delete; - LoadManager (LoadManager const&) = delete; - LoadManager& operator=(LoadManager const&) = delete; + LoadManager() = delete; + LoadManager(LoadManager const&) = delete; + LoadManager& + operator=(LoadManager const&) = delete; /** Destroy the manager. The destructor returns only after the thread has stopped. */ - ~LoadManager (); + ~LoadManager(); /** Turn on deadlock detection. @@ -67,47 +68,55 @@ public: // to prevent it from going off during program startup if // there's a lengthy initialization operation taking place? // - void activateDeadlockDetector (); + void + activateDeadlockDetector(); /** Reset the deadlock detection timer. A dedicated thread monitors the deadlock timer, and if too much time passes it will produce log warnings. */ - void resetDeadlockDetector (); + void + resetDeadlockDetector(); //-------------------------------------------------------------------------- // Stoppable members - void onPrepare () override; + void + onPrepare() override; - void onStart () override; + void + onStart() override; - void onStop () override; + void + onStop() override; private: - void run (); + void + run(); private: Application& app_; beast::Journal const journal_; std::thread thread_; - std::mutex mutex_; // Guards deadLock_, armed_, and stop_. + std::mutex mutex_; // Guards deadLock_, armed_, and stop_. - std::chrono::steady_clock::time_point deadLock_; // Detect server deadlocks. + std::chrono::steady_clock::time_point + deadLock_; // Detect server deadlocks. bool armed_; bool stop_; - friend - std::unique_ptr - make_LoadManager(Application& app, Stoppable& parent, beast::Journal journal); + friend std::unique_ptr + make_LoadManager( + Application& app, + Stoppable& parent, + beast::Journal journal); }; std::unique_ptr -make_LoadManager ( - Application& app, Stoppable& parent, beast::Journal journal); +make_LoadManager(Application& app, Stoppable& parent, beast::Journal journal); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/main/Main.cpp b/src/ripple/app/main/Main.cpp index 44436fb060..a6962c4ce2 100644 --- a/src/ripple/app/main/Main.cpp +++ b/src/ripple/app/main/Main.cpp @@ -17,25 +17,24 @@ */ //============================================================================== - -#include -#include #include #include -#include +#include #include #include +#include +#include +#include #include #include #include #include #include #include +#include +#include #include #include -#include -#include -#include #include #include @@ -43,18 +42,18 @@ #include #include +#include #include #include -#include #include #include -#include #include +#include #if BOOST_OS_WINDOWS -#include #include +#include #endif // Do we know the plaform we're compiling on? If you're adding new platforms @@ -64,9 +63,9 @@ #endif // Ensure that precisely one platform is detected. -#if (BOOST_OS_LINUX && (BOOST_OS_WINDOWS || BOOST_OS_MACOS)) || \ - (BOOST_OS_MACOS && (BOOST_OS_WINDOWS || BOOST_OS_LINUX)) || \ - (BOOST_OS_WINDOWS && (BOOST_OS_LINUX || BOOST_OS_MACOS)) +#if (BOOST_OS_LINUX && (BOOST_OS_WINDOWS || BOOST_OS_MACOS)) || \ + (BOOST_OS_MACOS && (BOOST_OS_WINDOWS || BOOST_OS_LINUX)) || \ + (BOOST_OS_WINDOWS && (BOOST_OS_LINUX || BOOST_OS_MACOS)) #error Multiple supported platforms appear active at once #endif @@ -105,15 +104,12 @@ adjustDescriptorLimit(int needed, beast::Journal j) if (needed > available) { - j.fatal() << - "Insufficient number of file descriptors: " << - needed << " are needed, but only " << - available << " are available."; + j.fatal() << "Insufficient number of file descriptors: " << needed + << " are needed, but only " << available << " are available."; - std::cerr << - "Insufficient number of file descriptors: " << - needed << " are needed, but only " << - available << " are available.\n"; + std::cerr << "Insufficient number of file descriptors: " << needed + << " are needed, but only " << available + << " are available.\n"; return false; } @@ -122,30 +118,37 @@ adjustDescriptorLimit(int needed, beast::Journal j) return true; } -void printHelp (const po::options_description& desc) +void +printHelp(const po::options_description& desc) { std::cerr - << systemName () << "d [options] \n" + << systemName() << "d [options] \n" << desc << std::endl << "Commands: \n" " account_currencies [] [strict]\n" - " account_info ||| [] [strict]\n" + " account_info ||| [] " + "[strict]\n" " account_lines |\"\" []\n" " account_channels |\"\" []\n" " account_objects [] [strict]\n" - " account_offers | [] [strict]\n" - " account_tx accountID [ledger_min [ledger_max [limit [offset]]]] [binary] [count] [descending]\n" - " book_offers [ [ [ []]]]]\n" + " account_offers | [] " + "[strict]\n" + " account_tx accountID [ledger_min [ledger_max [limit " + "[offset]]]] [binary] [count] [descending]\n" + " book_offers [ " + "[ [ []]]]]\n" " can_delete [||now|always|never]\n" " channel_authorize \n" " channel_verify \n" " connect []\n" " consensus_info\n" - " deposit_authorized []\n" + " deposit_authorized " + "[]\n" " download_shard [[ ]]\n" " feature [ [accept|reject]]\n" " fetch_info [clear]\n" - " gateway_balances [] [ [ ]]\n" + " gateway_balances [] [ [ " + " ]]\n" " get_counts\n" " json \n" " ledger [|current|closed|validated] [full]\n" @@ -167,7 +170,8 @@ void printHelp (const po::options_description& desc) " server_info [counters]\n" " server_state [counters]\n" " sign [offline]\n" - " sign_for [offline]\n" + " sign_for " + "[offline]\n" " stop\n" " submit |[ ]\n" " submit_multisigned \n" @@ -188,21 +192,19 @@ class multi_selector { private: std::vector selectors_; + public: - explicit - multi_selector(std::string const& patterns = "") + explicit multi_selector(std::string const& patterns = "") { std::vector v; - boost::split (v, patterns, boost::algorithm::is_any_of (",")); + boost::split(v, patterns, boost::algorithm::is_any_of(",")); selectors_.reserve(v.size()); - std::for_each(v.begin(), v.end(), - [this](std::string s) - { - boost::trim (s); - if (selectors_.empty() || !s.empty()) - selectors_.emplace_back( - beast::unit_test::selector::automatch, s); - }); + std::for_each(v.begin(), v.end(), [this](std::string s) { + boost::trim(s); + if (selectors_.empty() || !s.empty()) + selectors_.emplace_back( + beast::unit_test::selector::automatch, s); + }); } bool @@ -215,9 +217,12 @@ public: } }; -namespace test{ extern std::atomic envUseIPv4; } +namespace test { +extern std::atomic envUseIPv4; +} -static int runUnitTests( +static int +runUnitTests( std::string const& pattern, std::string const& argument, bool quiet, @@ -231,7 +236,7 @@ static int runUnitTests( using namespace beast::unit_test; using namespace ripple::test; - ripple::test::envUseIPv4 = (! ipv6); + ripple::test::envUseIPv4 = (!ipv6); if (!child && num_jobs == 1) { @@ -264,7 +269,7 @@ static int runUnitTests( boost::process::exe = exe_name, boost::process::args = args); int bad_child_exits = 0; - for(auto& c : children) + for (auto& c : children) { try { @@ -298,21 +303,22 @@ static int runUnitTests( //------------------------------------------------------------------------------ -int run (int argc, char** argv) +int +run(int argc, char** argv) { using namespace std; - beast::setCurrentThreadName ("rippled: main"); + beast::setCurrentThreadName("rippled: main"); po::variables_map vm; std::string importText; { importText += "Import an existing node database (specified in the ["; - importText += ConfigSection::importNodeDatabase (); + importText += ConfigSection::importNodeDatabase(); importText += "] configuration file section) into the current "; importText += "node database (specified in the ["; - importText += ConfigSection::nodeDatabase (); + importText += ConfigSection::nodeDatabase(); importText += "] configuration file section)."; } std::string shardsText; @@ -324,90 +330,97 @@ int run (int argc, char** argv) // Set up option parsing. // - po::options_description gen ("General Options"); - gen.add_options () - ("conf", po::value (), "Specify the configuration file.") - ("debug", "Enable normally suppressed debug logging") - ("fg", "Run in the foreground.") - ("help,h", "Display this message.") - ("quorum", po::value (), - "Override the minimum validation quorum.") - ("silent", "No output to the console after startup.") - ("standalone,a", "Run with no peers.") - ("verbose,v", "Verbose logging.") - ("version", "Display the build version.") - ; + po::options_description gen("General Options"); + gen.add_options()( + "conf", po::value(), "Specify the configuration file.")( + "debug", "Enable normally suppressed debug logging")( + "fg", "Run in the foreground.")("help,h", "Display this message.")( + "quorum", + po::value(), + "Override the minimum validation quorum.")( + "silent", "No output to the console after startup.")( + "standalone,a", "Run with no peers.")("verbose,v", "Verbose logging.")( + "version", "Display the build version."); - po::options_description data ("Ledger/Data Options"); - data.add_options () - ("import", importText.c_str ()) - ("ledger", po::value (), - "Load the specified ledger and start from the value given.") - ("ledgerfile", po::value (), "Load the specified ledger file.") - ("load", "Load the current ledger from the local DB.") - ("net", "Get the initial ledger from the network.") - ("nodetoshard", "Import node store into shards") - ("replay","Replay a ledger close.") - ("start", "Start from a fresh Ledger.") - ("vacuum", po::value(), + po::options_description data("Ledger/Data Options"); + data.add_options()("import", importText.c_str())( + "ledger", + po::value(), + "Load the specified ledger and start from the value given.")( + "ledgerfile", + po::value(), + "Load the specified ledger file.")( + "load", "Load the current ledger from the local DB.")( + "net", "Get the initial ledger from the network.")( + "nodetoshard", "Import node store into shards")( + "replay", "Replay a ledger close.")( + "start", "Start from a fresh Ledger.")( + "vacuum", + po::value(), "VACUUM the transaction db. Mandatory string argument specifies " - "temporary directory path.") - ("valid", "Consider the initial ledger a valid network ledger.") - ("validateShards", shardsText.c_str ()) - ; + "temporary directory path.")( + "valid", "Consider the initial ledger a valid network ledger.")( + "validateShards", shardsText.c_str()); - po::options_description rpc ("RPC Client Options"); - rpc.add_options() - ("rpc", + po::options_description rpc("RPC Client Options"); + rpc.add_options()( + "rpc", "Perform rpc command - see below for available commands. " - "This is assumed if any positional parameters are provided.") - ("rpc_ip", po::value (), + "This is assumed if any positional parameters are provided.")( + "rpc_ip", + po::value(), "Specify the IP address for RPC command. " - "Format: [':']") - ("rpc_port", po::value (), + "Format: [':']")( + "rpc_port", + po::value(), "DEPRECATED: include with rpc_ip instead. " - "Specify the port number for RPC command.") - ; + "Specify the port number for RPC command."); - po::options_description test ("Unit Test Options"); - test.add_options() - ("quiet,q", + po::options_description test("Unit Test Options"); + test.add_options()( + "quiet,q", "Suppress test suite messages, " - "including suite/case name (at start) and test log messages.") - ("unittest,u", po::value ()->implicit_value (""), + "including suite/case name (at start) and test log messages.")( + "unittest,u", + po::value()->implicit_value(""), "Perform unit tests. The optional argument specifies one or " "more comma-separated selectors. Each selector specifies a suite name, " "full-name (lib.module.suite), module, or library " - "(checked in that ""order).") - ("unittest-arg", po::value ()->implicit_value (""), + "(checked in that " + "order).")( + "unittest-arg", + po::value()->implicit_value(""), "Supplies an argument string to unit tests. If provided, this argument " "is made available to each suite that runs. Interpretation of the " "argument is handled individually by any suite that accesses it -- " "as such, it typically only make sense to provide this when running " - "a single suite.") - ("unittest-ipv6", "Use IPv6 localhost when running unittests (default is IPv4).") - ("unittest-log", + "a single suite.")( + "unittest-ipv6", + "Use IPv6 localhost when running unittests (default is IPv4).")( + "unittest-log", "Force unit test log message output. Only useful in combination with " "--quiet, in which case log messages will print but suite/case names " - "will not.") - ("unittest-jobs", po::value (), - "Number of unittest jobs to run in parallel (child processes).") - ; + "will not.")( + "unittest-jobs", + po::value(), + "Number of unittest jobs to run in parallel (child processes)."); - // These are hidden options, not intended to be shown in the usage/help message - po::options_description hidden ("Hidden Options"); - hidden.add_options() - ("parameters", po::value< vector > (), + // These are hidden options, not intended to be shown in the usage/help + // message + po::options_description hidden("Hidden Options"); + hidden.add_options()( + "parameters", + po::value>(), "Specify rpc command and parameters. This option must be repeated " - "for each command/param. Positional parameters also serve this purpose, " - "so this option is not needed for users") - ("unittest-child", - "For internal use only when spawning child unit test processes.") - ; + "for each command/param. Positional parameters also serve this " + "purpose, " + "so this option is not needed for users")( + "unittest-child", + "For internal use only when spawning child unit test processes."); // Interpret positional arguments as --parameters. po::positional_options_description p; - p.add ("parameters", -1); + p.add("parameters", -1); po::options_description all; all.add(gen).add(rpc).add(data).add(test).add(hidden); @@ -418,12 +431,13 @@ int run (int argc, char** argv) // Parse options, if no error. try { - po::store (po::command_line_parser (argc, argv) - .options (all) // Parse options. - .positional (p) // Remainder as --parameters. - .run (), + po::store( + po::command_line_parser(argc, argv) + .options(all) // Parse options. + .positional(p) // Remainder as --parameters. + .run(), vm); - po::notify (vm); // Invoke option notify functions. + po::notify(vm); // Invoke option notify functions. } catch (std::exception const&) { @@ -432,23 +446,23 @@ int run (int argc, char** argv) return 1; } - if (vm.count ("help")) + if (vm.count("help")) { - printHelp (desc); + printHelp(desc); return 0; } - if (vm.count ("version")) + if (vm.count("version")) { - std::cout << "rippled version " << - BuildInfo::getVersionString () << std::endl; + std::cout << "rippled version " << BuildInfo::getVersionString() + << std::endl; return 0; } // Run the unit tests if requested. // The unit tests will exit the application with an appropriate return code. // - if (vm.count ("unittest")) + if (vm.count("unittest")) { std::string argument; @@ -459,14 +473,15 @@ int run (int argc, char** argv) bool unittestChild = false; if (vm.count("unittest-jobs")) numJobs = std::max(numJobs, vm["unittest-jobs"].as()); - unittestChild = bool (vm.count("unittest-child")); + unittestChild = bool(vm.count("unittest-child")); return runUnitTests( - vm["unittest"].as(), argument, - bool (vm.count ("quiet")), - bool (vm.count ("unittest-log")), + vm["unittest"].as(), + argument, + bool(vm.count("quiet")), + bool(vm.count("unittest-log")), unittestChild, - bool (vm.count ("unittest-ipv6")), + bool(vm.count("unittest-ipv6")), numJobs, argc, argv); @@ -476,20 +491,25 @@ int run (int argc, char** argv) if (vm.count("unittest-jobs")) { // unittest jobs only makes sense with `unittest` - std::cerr << "rippled: '--unittest-jobs' specified without '--unittest'.\n"; - std::cerr << "To run the unit tests the '--unittest' option must be present.\n"; + std::cerr << "rippled: '--unittest-jobs' specified without " + "'--unittest'.\n"; + std::cerr << "To run the unit tests the '--unittest' option must " + "be present.\n"; return 1; } } auto config = std::make_unique(); - auto configFile = vm.count ("conf") ? - vm["conf"].as () : std::string(); + auto configFile = + vm.count("conf") ? vm["conf"].as() : std::string(); // config file, quiet flag. - config->setup (configFile, bool (vm.count ("quiet")), - bool(vm.count("silent")), bool(vm.count("standalone"))); + config->setup( + configFile, + bool(vm.count("quiet")), + bool(vm.count("silent")), + bool(vm.count("standalone"))); if (vm.count("vacuum")) { @@ -516,8 +536,7 @@ int run (int argc, char** argv) "as much free space as the size of " << dbPath.string() << ", which is " << dbSize << " bytes. The filesystem for " << tmpPath.string() - << " only has " - << space(tmpPath).available + << " only has " << space(tmpPath).available << " bytes.\n"; return -1; } @@ -529,71 +548,71 @@ int run (int argc, char** argv) session << "PRAGMA page_size;", soci::into(pageSize); - std::cout << "VACUUM beginning. page_size: " << - pageSize << std::endl; + std::cout << "VACUUM beginning. page_size: " << pageSize + << std::endl; session << "PRAGMA journal_mode=OFF;"; - session << "PRAGMA temp_store_directory=\"" << - tmpPath.string() << "\";"; + session << "PRAGMA temp_store_directory=\"" << tmpPath.string() + << "\";"; session << "VACUUM;"; session << "PRAGMA journal_mode=WAL;"; session << "PRAGMA page_size;", soci::into(pageSize); - std::cout << "VACUUM finished. page_size: " << - pageSize << std::endl; + std::cout << "VACUUM finished. page_size: " << pageSize + << std::endl; } catch (std::exception const& e) { - std::cerr << "exception " << e.what() << - " in function " << __func__ << std::endl; + std::cerr << "exception " << e.what() << " in function " << __func__ + << std::endl; return -1; } return 0; } - if (vm.count ("start")) + if (vm.count("start")) config->START_UP = Config::FRESH; - if (vm.count ("import")) + if (vm.count("import")) config->doImport = true; if (vm.count("nodetoshard")) config->nodeToShard = true; - if (vm.count ("validateShards")) + if (vm.count("validateShards")) config->validateShards = true; - if (vm.count ("ledger")) + if (vm.count("ledger")) { - config->START_LEDGER = vm["ledger"].as (); + config->START_LEDGER = vm["ledger"].as(); if (vm.count("replay")) config->START_UP = Config::REPLAY; else config->START_UP = Config::LOAD; } - else if (vm.count ("ledgerfile")) + else if (vm.count("ledgerfile")) { - config->START_LEDGER = vm["ledgerfile"].as (); + config->START_LEDGER = vm["ledgerfile"].as(); config->START_UP = Config::LOAD_FILE; } - else if (vm.count ("load")) + else if (vm.count("load")) { config->START_UP = Config::LOAD; } - if (vm.count ("valid")) + if (vm.count("valid")) { config->START_VALID = true; } - if (vm.count ("net")) + if (vm.count("net")) { if ((config->START_UP == Config::LOAD) || (config->START_UP == Config::REPLAY)) { - std::cerr << - "Net and load/reply options are incompatible" << std::endl; + std::cerr << "Net and load/reply options are incompatible" + << std::endl; return -1; } @@ -602,31 +621,31 @@ int run (int argc, char** argv) // Override the RPC destination IP address. This must // happen after the config file is loaded. - if (vm.count ("rpc_ip")) + if (vm.count("rpc_ip")) { auto endpoint = beast::IP::Endpoint::from_string_checked( vm["rpc_ip"].as()); - if (! endpoint) + if (!endpoint) { - std::cerr << "Invalid rpc_ip = " << - vm["rpc_ip"].as() << "\n"; + std::cerr << "Invalid rpc_ip = " << vm["rpc_ip"].as() + << "\n"; return -1; } if (endpoint->port() == 0) { std::cerr << "No port specified in rpc_ip.\n"; - if (vm.count ("rpc_port")) + if (vm.count("rpc_port")) { std::cerr << "WARNING: using deprecated rpc_port param.\n"; try { - endpoint = endpoint->at_port( - vm["rpc_port"].as()); + endpoint = + endpoint->at_port(vm["rpc_port"].as()); if (endpoint->port() == 0) throw std::domain_error("0"); } - catch(std::exception const& e) + catch (std::exception const& e) { std::cerr << "Invalid rpc_port = " << e.what() << "\n"; return -1; @@ -639,20 +658,20 @@ int run (int argc, char** argv) config->rpc_ip = std::move(*endpoint); } - if (vm.count ("quorum")) + if (vm.count("quorum")) { try { - config->VALIDATION_QUORUM = vm["quorum"].as (); + config->VALIDATION_QUORUM = vm["quorum"].as(); if (config->VALIDATION_QUORUM == std::size_t{}) { throw std::domain_error("0"); } } - catch(std::exception const& e) + catch (std::exception const& e) { - std::cerr << "Invalid value specified for --quorum (" - << e.what() << ")\n"; + std::cerr << "Invalid value specified for --quorum (" << e.what() + << ")\n"; return -1; } } @@ -661,25 +680,27 @@ int run (int argc, char** argv) using namespace beast::severities; Severity thresh = kInfo; - if (vm.count ("quiet")) + if (vm.count("quiet")) thresh = kFatal; - else if (vm.count ("verbose")) + else if (vm.count("verbose")) thresh = kTrace; auto logs = std::make_unique(thresh); // No arguments. Run server. - if (!vm.count ("parameters")) + if (!vm.count("parameters")) { // TODO: this comment can be removed in a future release - // say 1.7 or higher if (config->had_trailing_comments()) { - JLOG(logs->journal("Application").warn()) << - "Trailing comments were seen in your config file. " << - "The treatment of inline/trailing comments has changed recently. " << - "Any `#` characters NOT intended to delimit comments should be " << - "preceded by a \\"; + JLOG(logs->journal("Application").warn()) + << "Trailing comments were seen in your config file. " + << "The treatment of inline/trailing comments has changed " + "recently. " + << "Any `#` characters NOT intended to delimit comments should " + "be " + << "preceded by a \\"; } // We want at least 1024 file descriptors. We'll @@ -687,29 +708,25 @@ int run (int argc, char** argv) if (!adjustDescriptorLimit(1024, logs->journal("Application"))) return -1; - if (HaveSustain() && !vm.count ("fg") && !config->standalone()) + if (HaveSustain() && !vm.count("fg") && !config->standalone()) { - auto const ret = DoSustain (); + auto const ret = DoSustain(); - if (!ret.empty ()) + if (!ret.empty()) std::cerr << "Watchdog: " << ret << std::endl; } - if (vm.count ("debug")) + if (vm.count("debug")) { - setDebugLogSink (logs->makeSink ( - "Debug", beast::severities::kTrace)); + setDebugLogSink(logs->makeSink("Debug", beast::severities::kTrace)); } - auto timeKeeper = make_TimeKeeper( - logs->journal("TimeKeeper")); + auto timeKeeper = make_TimeKeeper(logs->journal("TimeKeeper")); auto app = make_Application( - std::move(config), - std::move(logs), - std::move(timeKeeper)); + std::move(config), std::move(logs), std::move(timeKeeper)); - if (!app->setup ()) + if (!app->setup()) { StopSustain(); return -1; @@ -718,8 +735,7 @@ int run (int argc, char** argv) // With our configuration parsed, ensure we have // enough file descriptors available: if (!adjustDescriptorLimit( - app->fdRequired(), - app->logs().journal("Application"))) + app->fdRequired(), app->logs().journal("Application"))) { StopSustain(); return -1; @@ -735,18 +751,17 @@ int run (int argc, char** argv) } // We have an RPC command to process: - beast::setCurrentThreadName ("rippled: rpc"); - return RPCCall::fromCommandLine ( - *config, - vm["parameters"].as>(), - *logs); + beast::setCurrentThreadName("rippled: rpc"); + return RPCCall::fromCommandLine( + *config, vm["parameters"].as>(), *logs); } -} // ripple +} // namespace ripple // Must be outside the namespace for obvious reasons // -int main (int argc, char** argv) +int +main(int argc, char** argv) { #if BOOST_OS_WINDOWS { @@ -758,18 +773,18 @@ int main (int argc, char** argv) // coroutines. At the time of this writing the _ftime call is // used to initialize the timezone information. struct _timeb t; - #ifdef _INC_TIME_INL - _ftime_s (&t); - #else - _ftime (&t); - #endif +#ifdef _INC_TIME_INL + _ftime_s(&t); +#else + _ftime(&t); +#endif } ripple::sha512_deprecatedMSVCWorkaround(); #endif atexit(&google::protobuf::ShutdownProtobufLibrary); - auto const result (ripple::run (argc, argv)); + auto const result(ripple::run(argc, argv)); beast::basic_seconds_clock_main_hook(); diff --git a/src/ripple/app/main/NodeIdentity.cpp b/src/ripple/app/main/NodeIdentity.cpp index 7a44f5ce4b..4ed6fb6f78 100644 --- a/src/ripple/app/main/NodeIdentity.cpp +++ b/src/ripple/app/main/NodeIdentity.cpp @@ -17,19 +17,19 @@ */ //============================================================================== -#include #include #include #include #include #include +#include #include #include namespace ripple { std::pair -loadNodeIdentity (Application& app) +loadNodeIdentity(Application& app) { // If a seed is specified in the configuration file use that directly. if (app.config().exists(SECTION_NODE_SEED)) @@ -38,31 +38,29 @@ loadNodeIdentity (Application& app) app.config().section(SECTION_NODE_SEED).lines().front()); if (!seed) - Throw( - "NodeIdentity: Bad [" SECTION_NODE_SEED "] specified"); + Throw("NodeIdentity: Bad [" SECTION_NODE_SEED + "] specified"); - auto secretKey = - generateSecretKey (KeyType::secp256k1, *seed); - auto publicKey = - derivePublicKey (KeyType::secp256k1, secretKey); + auto secretKey = generateSecretKey(KeyType::secp256k1, *seed); + auto publicKey = derivePublicKey(KeyType::secp256k1, secretKey); - return { publicKey, secretKey }; + return {publicKey, secretKey}; } // Try to load a node identity from the database: boost::optional publicKey; boost::optional secretKey; - auto db = app.getWalletDB ().checkoutDb (); + auto db = app.getWalletDB().checkoutDb(); { boost::optional pubKO, priKO; - soci::statement st = (db->prepare << - "SELECT PublicKey, PrivateKey FROM NodeIdentity;", - soci::into(pubKO), - soci::into(priKO)); - st.execute (); - while (st.fetch ()) + soci::statement st = + (db->prepare << "SELECT PublicKey, PrivateKey FROM NodeIdentity;", + soci::into(pubKO), + soci::into(priKO)); + st.execute(); + while (st.fetch()) { auto const sk = parseBase58( TokenType::NodePrivate, priKO.value_or("")); @@ -70,7 +68,7 @@ loadNodeIdentity (Application& app) TokenType::NodePublic, pubKO.value_or("")); // Only use if the public and secret keys are a pair - if (sk && pk && (*pk == derivePublicKey (KeyType::secp256k1, *sk))) + if (sk && pk && (*pk == derivePublicKey(KeyType::secp256k1, *sk))) { secretKey = sk; publicKey = pk; @@ -83,13 +81,14 @@ loadNodeIdentity (Application& app) { std::tie(publicKey, secretKey) = randomKeyPair(KeyType::secp256k1); - *db << str (boost::format ( - "INSERT INTO NodeIdentity (PublicKey,PrivateKey) VALUES ('%s','%s');") - % toBase58 (TokenType::NodePublic, *publicKey) - % toBase58 (TokenType::NodePrivate, *secretKey)); + *db << str( + boost::format("INSERT INTO NodeIdentity (PublicKey,PrivateKey) " + "VALUES ('%s','%s');") % + toBase58(TokenType::NodePublic, *publicKey) % + toBase58(TokenType::NodePrivate, *secretKey)); } - return { *publicKey, *secretKey }; + return {*publicKey, *secretKey}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/main/NodeIdentity.h b/src/ripple/app/main/NodeIdentity.h index 07a9bba4b8..4d4a520155 100644 --- a/src/ripple/app/main/NodeIdentity.h +++ b/src/ripple/app/main/NodeIdentity.h @@ -29,8 +29,8 @@ namespace ripple { /** The cryptographic credentials identifying this server instance. */ std::pair -loadNodeIdentity (Application& app); +loadNodeIdentity(Application& app); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/main/NodeStoreScheduler.cpp b/src/ripple/app/main/NodeStoreScheduler.cpp index 540da6e2f8..dd58b08e73 100644 --- a/src/ripple/app/main/NodeStoreScheduler.cpp +++ b/src/ripple/app/main/NodeStoreScheduler.cpp @@ -22,43 +22,47 @@ namespace ripple { -NodeStoreScheduler::NodeStoreScheduler (Stoppable& parent) - : Stoppable ("NodeStoreScheduler", parent) +NodeStoreScheduler::NodeStoreScheduler(Stoppable& parent) + : Stoppable("NodeStoreScheduler", parent) { } -void NodeStoreScheduler::setJobQueue (JobQueue& jobQueue) +void +NodeStoreScheduler::setJobQueue(JobQueue& jobQueue) { m_jobQueue = &jobQueue; } -void NodeStoreScheduler::onStop () +void +NodeStoreScheduler::onStop() { } -void NodeStoreScheduler::onChildrenStopped () +void +NodeStoreScheduler::onChildrenStopped() { - assert (m_taskCount == 0); - stopped (); + assert(m_taskCount == 0); + stopped(); } -void NodeStoreScheduler::scheduleTask (NodeStore::Task& task) +void +NodeStoreScheduler::scheduleTask(NodeStore::Task& task) { ++m_taskCount; - if (!m_jobQueue->addJob ( - jtWRITE, - "NodeObject::store", - [this, &task] (Job&) { doTask(task); })) + if (!m_jobQueue->addJob(jtWRITE, "NodeObject::store", [this, &task](Job&) { + doTask(task); + })) { // Job not added, presumably because we're shutting down. // Recover by executing the task synchronously. - doTask (task); + doTask(task); } } -void NodeStoreScheduler::doTask (NodeStore::Task& task) +void +NodeStoreScheduler::doTask(NodeStore::Task& task) { - task.performScheduledTask (); + task.performScheduledTask(); // NOTE: It feels a bit off that there are two different methods that // call stopped(): onChildrenStopped() and doTask(). There's a @@ -71,18 +75,20 @@ void NodeStoreScheduler::doTask (NodeStore::Task& task) stopped(); } -void NodeStoreScheduler::onFetch (NodeStore::FetchReport const& report) +void +NodeStoreScheduler::onFetch(NodeStore::FetchReport const& report) { if (report.wentToDisk) - m_jobQueue->addLoadEvents ( + m_jobQueue->addLoadEvents( report.isAsync ? jtNS_ASYNC_READ : jtNS_SYNC_READ, - 1, report.elapsed); + 1, + report.elapsed); } -void NodeStoreScheduler::onBatchWrite (NodeStore::BatchWriteReport const& report) +void +NodeStoreScheduler::onBatchWrite(NodeStore::BatchWriteReport const& report) { - m_jobQueue->addLoadEvents (jtNS_WRITE, - report.writeCount, report.elapsed); + m_jobQueue->addLoadEvents(jtNS_WRITE, report.writeCount, report.elapsed); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/main/NodeStoreScheduler.h b/src/ripple/app/main/NodeStoreScheduler.h index 657fe6499e..b37f3c3e78 100644 --- a/src/ripple/app/main/NodeStoreScheduler.h +++ b/src/ripple/app/main/NodeStoreScheduler.h @@ -20,39 +20,45 @@ #ifndef RIPPLE_APP_MAIN_NODESTORESCHEDULER_H_INCLUDED #define RIPPLE_APP_MAIN_NODESTORESCHEDULER_H_INCLUDED -#include #include #include +#include #include namespace ripple { -/** A NodeStore::Scheduler which uses the JobQueue and implements the Stoppable API. */ -class NodeStoreScheduler - : public NodeStore::Scheduler - , public Stoppable +/** A NodeStore::Scheduler which uses the JobQueue and implements the Stoppable + * API. */ +class NodeStoreScheduler : public NodeStore::Scheduler, public Stoppable { public: - NodeStoreScheduler (Stoppable& parent); + NodeStoreScheduler(Stoppable& parent); // VFALCO NOTE This is a temporary hack to solve the problem // of circular dependency. // - void setJobQueue (JobQueue& jobQueue); + void + setJobQueue(JobQueue& jobQueue); - void onStop () override; - void onChildrenStopped () override; - void scheduleTask (NodeStore::Task& task) override; - void onFetch (NodeStore::FetchReport const& report) override; - void onBatchWrite (NodeStore::BatchWriteReport const& report) override; + void + onStop() override; + void + onChildrenStopped() override; + void + scheduleTask(NodeStore::Task& task) override; + void + onFetch(NodeStore::FetchReport const& report) override; + void + onBatchWrite(NodeStore::BatchWriteReport const& report) override; private: - void doTask (NodeStore::Task& task); + void + doTask(NodeStore::Task& task); - JobQueue* m_jobQueue {nullptr}; - std::atomic m_taskCount {0}; + JobQueue* m_jobQueue{nullptr}; + std::atomic m_taskCount{0}; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/main/Tuning.h b/src/ripple/app/main/Tuning.h index bf40720e00..bc8ef357aa 100644 --- a/src/ripple/app/main/Tuning.h +++ b/src/ripple/app/main/Tuning.h @@ -25,6 +25,6 @@ namespace ripple { constexpr std::size_t fullBelowTargetSize = 524288; constexpr std::chrono::seconds fullBelowExpiration = std::chrono::minutes{10}; -} +} // namespace ripple #endif diff --git a/src/ripple/app/misc/AmendmentTable.h b/src/ripple/app/misc/AmendmentTable.h index 7371f25814..c56a1d58bf 100644 --- a/src/ripple/app/misc/AmendmentTable.h +++ b/src/ripple/app/misc/AmendmentTable.h @@ -21,9 +21,9 @@ #define RIPPLE_APP_MISC_AMENDMENTTABLE_H_INCLUDED #include -#include #include #include +#include namespace ripple { @@ -36,16 +36,23 @@ class AmendmentTable public: virtual ~AmendmentTable() = default; - virtual uint256 find (std::string const& name) = 0; + virtual uint256 + find(std::string const& name) = 0; - virtual bool veto (uint256 const& amendment) = 0; - virtual bool unVeto (uint256 const& amendment) = 0; + virtual bool + veto(uint256 const& amendment) = 0; + virtual bool + unVeto(uint256 const& amendment) = 0; - virtual bool enable (uint256 const& amendment) = 0; - virtual bool disable (uint256 const& amendment) = 0; + virtual bool + enable(uint256 const& amendment) = 0; + virtual bool + disable(uint256 const& amendment) = 0; - virtual bool isEnabled (uint256 const& amendment) = 0; - virtual bool isSupported (uint256 const& amendment) = 0; + virtual bool + isEnabled(uint256 const& amendment) = 0; + virtual bool + isSupported(uint256 const& amendment) = 0; /** * @brief returns true if one or more amendments on the network @@ -53,14 +60,17 @@ public: * * @return true if an unsupported feature is enabled on the network */ - virtual bool hasUnsupportedEnabled () = 0; + virtual bool + hasUnsupportedEnabled() = 0; virtual boost::optional firstUnsupportedExpected() = 0; - virtual Json::Value getJson (int) = 0; + virtual Json::Value + getJson(int) = 0; /** Returns a Json::objectValue. */ - virtual Json::Value getJson (uint256 const& ) = 0; + virtual Json::Value + getJson(uint256 const&) = 0; /** Called when a new fully-validated ledger is accepted. */ void @@ -78,34 +88,34 @@ public: a new validated ledger. (If it could have changed things.) */ virtual bool - needValidatedLedger (LedgerIndex seq) = 0; + needValidatedLedger(LedgerIndex seq) = 0; virtual void - doValidatedLedger ( + doValidatedLedger( LedgerIndex ledgerSeq, - std::set const& enabled, + std::set const& enabled, majorityAmendments_t const& majority) = 0; // Called by the consensus code when we need to // inject pseudo-transactions - virtual std::map - doVoting ( + virtual std::map + doVoting( NetClock::time_point closeTime, - std::set const& enabledAmendments, + std::set const& enabledAmendments, majorityAmendments_t const& majorityAmendments, std::vector const& valSet) = 0; // Called by the consensus code when we need to // add feature entries to a validation - virtual std::vector - doValidation (std::set const& enabled) = 0; + virtual std::vector + doValidation(std::set const& enabled) = 0; // The set of amendments to enable in the genesis ledger // This will return all known, non-vetoed amendments. // If we ever have two amendments that should not both be // enabled at the same time, we should ensure one is vetoed. - virtual std::vector - getDesired () = 0; + virtual std::vector + getDesired() = 0; // The function below adapts the API callers expect to the // internal amendment table API. This allows the amendment @@ -114,13 +124,13 @@ public: // supports a full ledger API void - doVoting ( - std::shared_ptr const& lastClosedLedger, + doVoting( + std::shared_ptr const& lastClosedLedger, std::vector const& parentValidations, std::shared_ptr const& initialPosition) { // Ask implementation what to do - auto actions = doVoting ( + auto actions = doVoting( lastClosedLedger->parentCloseTime(), getEnabledAmendments(*lastClosedLedger), getMajorityAmendments(*lastClosedLedger), @@ -129,32 +139,31 @@ public: // Inject appropriate pseudo-transactions for (auto const& it : actions) { - STTx amendTx (ttAMENDMENT, - [&it, seq = lastClosedLedger->seq() + 1](auto& obj) - { - obj.setAccountID (sfAccount, AccountID()); - obj.setFieldH256 (sfAmendment, it.first); - obj.setFieldU32 (sfLedgerSequence, seq); + STTx amendTx( + ttAMENDMENT, + [&it, seq = lastClosedLedger->seq() + 1](auto& obj) { + obj.setAccountID(sfAccount, AccountID()); + obj.setFieldH256(sfAmendment, it.first); + obj.setFieldU32(sfLedgerSequence, seq); if (it.second != 0) - obj.setFieldU32 (sfFlags, it.second); + obj.setFieldU32(sfFlags, it.second); }); Serializer s; - amendTx.add (s); + amendTx.add(s); - initialPosition->addGiveItem ( - std::make_shared ( - amendTx.getTransactionID(), - s.peekData()), + initialPosition->addGiveItem( + std::make_shared( + amendTx.getTransactionID(), s.peekData()), true, false); } } - }; -std::unique_ptr make_AmendmentTable ( +std::unique_ptr +make_AmendmentTable( std::chrono::seconds majorityTime, int majorityFraction, Section const& supported, @@ -162,6 +171,6 @@ std::unique_ptr make_AmendmentTable ( Section const& vetoed, beast::Journal journal); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/CanonicalTXSet.cpp b/src/ripple/app/misc/CanonicalTXSet.cpp index 94b2e7a3fd..6d7b0d7965 100644 --- a/src/ripple/app/misc/CanonicalTXSet.cpp +++ b/src/ripple/app/misc/CanonicalTXSet.cpp @@ -22,94 +22,109 @@ namespace ripple { -bool CanonicalTXSet::Key::operator< (Key const& rhs) const +bool +CanonicalTXSet::Key::operator<(Key const& rhs) const { - if (mAccount < rhs.mAccount) return true; + if (mAccount < rhs.mAccount) + return true; - if (mAccount > rhs.mAccount) return false; + if (mAccount > rhs.mAccount) + return false; - if (mSeq < rhs.mSeq) return true; + if (mSeq < rhs.mSeq) + return true; - if (mSeq > rhs.mSeq) return false; + if (mSeq > rhs.mSeq) + return false; return mTXid < rhs.mTXid; } -bool CanonicalTXSet::Key::operator> (Key const& rhs) const +bool +CanonicalTXSet::Key::operator>(Key const& rhs) const { - if (mAccount > rhs.mAccount) return true; + if (mAccount > rhs.mAccount) + return true; - if (mAccount < rhs.mAccount) return false; + if (mAccount < rhs.mAccount) + return false; - if (mSeq > rhs.mSeq) return true; + if (mSeq > rhs.mSeq) + return true; - if (mSeq < rhs.mSeq) return false; + if (mSeq < rhs.mSeq) + return false; return mTXid > rhs.mTXid; } -bool CanonicalTXSet::Key::operator<= (Key const& rhs) const +bool +CanonicalTXSet::Key::operator<=(Key const& rhs) const { - if (mAccount < rhs.mAccount) return true; + if (mAccount < rhs.mAccount) + return true; - if (mAccount > rhs.mAccount) return false; + if (mAccount > rhs.mAccount) + return false; - if (mSeq < rhs.mSeq) return true; + if (mSeq < rhs.mSeq) + return true; - if (mSeq > rhs.mSeq) return false; + if (mSeq > rhs.mSeq) + return false; return mTXid <= rhs.mTXid; } -bool CanonicalTXSet::Key::operator>= (Key const& rhs)const +bool +CanonicalTXSet::Key::operator>=(Key const& rhs) const { - if (mAccount > rhs.mAccount) return true; + if (mAccount > rhs.mAccount) + return true; - if (mAccount < rhs.mAccount) return false; + if (mAccount < rhs.mAccount) + return false; - if (mSeq > rhs.mSeq) return true; + if (mSeq > rhs.mSeq) + return true; - if (mSeq < rhs.mSeq) return false; + if (mSeq < rhs.mSeq) + return false; return mTXid >= rhs.mTXid; } -uint256 CanonicalTXSet::accountKey (AccountID const& account) +uint256 +CanonicalTXSet::accountKey(AccountID const& account) { uint256 ret = beast::zero; - memcpy ( - ret.begin (), - account.begin (), - account.size ()); + memcpy(ret.begin(), account.begin(), account.size()); ret ^= salt_; return ret; } -void CanonicalTXSet::insert (std::shared_ptr const& txn) +void +CanonicalTXSet::insert(std::shared_ptr const& txn) { - map_.insert ( - std::make_pair ( - Key ( - accountKey (txn->getAccountID(sfAccount)), - txn->getSequence (), - txn->getTransactionID ()), - txn)); + map_.insert(std::make_pair( + Key(accountKey(txn->getAccountID(sfAccount)), + txn->getSequence(), + txn->getTransactionID()), + txn)); } std::vector> -CanonicalTXSet::prune(AccountID const& account, - std::uint32_t const seq) +CanonicalTXSet::prune(AccountID const& account, std::uint32_t const seq) { - auto effectiveAccount = accountKey (account); + auto effectiveAccount = accountKey(account); Key keyLow(effectiveAccount, seq, beast::zero); - Key keyHigh(effectiveAccount, seq+1, beast::zero); + Key keyHigh(effectiveAccount, seq + 1, beast::zero); auto range = boost::make_iterator_range( - map_.lower_bound(keyLow), - map_.lower_bound(keyHigh)); - auto txRange = boost::adaptors::transform(range, - [](auto const& p) { return p.second; }); + map_.lower_bound(keyLow), map_.lower_bound(keyHigh)); + auto txRange = boost::adaptors::transform( + range, [](auto const& p) { return p.second; }); std::vector> result( txRange.begin(), txRange.end()); @@ -118,4 +133,4 @@ CanonicalTXSet::prune(AccountID const& account, return result; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/CanonicalTXSet.h b/src/ripple/app/misc/CanonicalTXSet.h index 1df590ef8d..f85bd3fd09 100644 --- a/src/ripple/app/misc/CanonicalTXSet.h +++ b/src/ripple/app/misc/CanonicalTXSet.h @@ -39,28 +39,33 @@ private: class Key { public: - Key (uint256 const& account, std::uint32_t seq, uint256 const& id) - : mAccount (account) - , mTXid (id) - , mSeq (seq) + Key(uint256 const& account, std::uint32_t seq, uint256 const& id) + : mAccount(account), mTXid(id), mSeq(seq) { } - bool operator< (Key const& rhs) const; - bool operator> (Key const& rhs) const; - bool operator<= (Key const& rhs) const; - bool operator>= (Key const& rhs) const; + bool + operator<(Key const& rhs) const; + bool + operator>(Key const& rhs) const; + bool + operator<=(Key const& rhs) const; + bool + operator>=(Key const& rhs) const; - bool operator== (Key const& rhs) const + bool + operator==(Key const& rhs) const { return mTXid == rhs.mTXid; } - bool operator!= (Key const& rhs) const + bool + operator!=(Key const& rhs) const { return mTXid != rhs.mTXid; } - uint256 const& getTXID () const + uint256 const& + getTXID() const { return mTXid; } @@ -72,65 +77,74 @@ private: }; // Calculate the salted key for the given account - uint256 accountKey (AccountID const& account); + uint256 + accountKey(AccountID const& account); public: - using const_iterator = std::map >::const_iterator; + using const_iterator = + std::map>::const_iterator; public: - explicit CanonicalTXSet (LedgerHash const& saltHash) - : salt_ (saltHash) + explicit CanonicalTXSet(LedgerHash const& saltHash) : salt_(saltHash) { } - void insert (std::shared_ptr const& txn); + void + insert(std::shared_ptr const& txn); std::vector> prune(AccountID const& account, std::uint32_t const seq); // VFALCO TODO remove this function - void reset (LedgerHash const& salt) + void + reset(LedgerHash const& salt) { salt_ = salt; - map_.clear (); + map_.clear(); } - const_iterator erase (const_iterator const& it) + const_iterator + erase(const_iterator const& it) { return map_.erase(it); } - const_iterator begin () const + const_iterator + begin() const { return map_.begin(); } - const_iterator end() const + const_iterator + end() const { return map_.end(); } - size_t size () const + size_t + size() const { - return map_.size (); + return map_.size(); } - bool empty () const + bool + empty() const { - return map_.empty (); + return map_.empty(); } - uint256 const& key() const + uint256 const& + key() const { return salt_; } private: - std::map > map_; + std::map> map_; // Used to salt the accounts so people can't mine for low account numbers uint256 salt_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/FeeVote.h b/src/ripple/app/misc/FeeVote.h index 77d57e9532..abcb02cce8 100644 --- a/src/ripple/app/misc/FeeVote.h +++ b/src/ripple/app/misc/FeeVote.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_APP_MISC_FEEVOTE_H_INCLUDED #define RIPPLE_APP_MISC_FEEVOTE_H_INCLUDED -#include -#include -#include #include +#include +#include #include +#include namespace ripple { @@ -40,53 +40,53 @@ public: struct Setup { /** The cost of a reference transaction in drops. */ - XRPAmount reference_fee{ 10 }; + XRPAmount reference_fee{10}; /** The cost of a reference transaction in fee units. */ - static constexpr FeeUnit32 reference_fee_units{ 10 }; + static constexpr FeeUnit32 reference_fee_units{10}; /** The account reserve requirement in drops. */ - XRPAmount account_reserve{ 20 * DROPS_PER_XRP }; + XRPAmount account_reserve{20 * DROPS_PER_XRP}; /** The per-owned item reserve requirement in drops. */ - XRPAmount owner_reserve{ 5 * DROPS_PER_XRP }; + XRPAmount owner_reserve{5 * DROPS_PER_XRP}; }; - virtual ~FeeVote () = default; + virtual ~FeeVote() = default; /** Add local fee preference to validation. @param lastClosedLedger @param baseValidation */ - virtual - void - doValidation (std::shared_ptr const& lastClosedLedger, - STValidation::FeeSettings & fees) = 0; + virtual void + doValidation( + std::shared_ptr const& lastClosedLedger, + STValidation::FeeSettings& fees) = 0; /** Cast our local vote on the fee. @param lastClosedLedger @param initialPosition */ - virtual - void - doVoting (std::shared_ptr const& lastClosedLedger, + virtual void + doVoting( + std::shared_ptr const& lastClosedLedger, std::vector const& parentValidations, - std::shared_ptr const& initialPosition) = 0; + std::shared_ptr const& initialPosition) = 0; }; /** Build FeeVote::Setup from a config section. */ FeeVote::Setup -setup_FeeVote (Section const& section); +setup_FeeVote(Section const& section); /** Create an instance of the FeeVote logic. @param setup The fee schedule to vote for. @param journal Where to log. */ -std::unique_ptr -make_FeeVote (FeeVote::Setup const& setup, beast::Journal journal); +std::unique_ptr +make_FeeVote(FeeVote::Setup const& setup, beast::Journal journal); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/FeeVoteImpl.cpp b/src/ripple/app/misc/FeeVoteImpl.cpp index dec8ba1beb..cb48dc29b7 100644 --- a/src/ripple/app/misc/FeeVoteImpl.cpp +++ b/src/ripple/app/misc/FeeVoteImpl.cpp @@ -17,12 +17,12 @@ */ //============================================================================== -#include -#include #include -#include +#include #include #include +#include +#include namespace ripple { @@ -32,14 +32,13 @@ class VotableValue { private: using value_type = XRPAmount; - value_type const mCurrent; // The current setting - value_type const mTarget; // The setting we want - std::map mVoteMap; + value_type const mCurrent; // The current setting + value_type const mTarget; // The setting we want + std::map mVoteMap; public: - VotableValue (value_type current, value_type target) - : mCurrent (current) - , mTarget (target) + VotableValue(value_type current, value_type target) + : mCurrent(current), mTarget(target) { // Add our vote ++mVoteMap[mTarget]; @@ -54,7 +53,7 @@ public: void noVote() { - addVote (mCurrent); + addVote(mCurrent); } value_type @@ -62,17 +61,15 @@ public: }; auto -VotableValue::getVotes() const - -> value_type +VotableValue::getVotes() const -> value_type { value_type ourVote = mCurrent; int weight = 0; for (auto const& [key, val] : mVoteMap) { // Take most voted value between current and target, inclusive - if ((key <= std::max (mTarget, mCurrent)) && - (key >= std::min (mTarget, mCurrent)) && - (val > weight)) + if ((key <= std::max(mTarget, mCurrent)) && + (key >= std::min(mTarget, mCurrent)) && (val > weight)) { ourVote = key; weight = val; @@ -82,7 +79,7 @@ VotableValue::getVotes() const return ourVote; } -} +} // namespace detail //------------------------------------------------------------------------------ @@ -93,51 +90,52 @@ private: beast::Journal const journal_; public: - FeeVoteImpl (Setup const& setup, beast::Journal journal); + FeeVoteImpl(Setup const& setup, beast::Journal journal); void - doValidation (std::shared_ptr const& lastClosedLedger, + doValidation( + std::shared_ptr const& lastClosedLedger, STValidation::FeeSettings& fees) override; void - doVoting (std::shared_ptr const& lastClosedLedger, + doVoting( + std::shared_ptr const& lastClosedLedger, std::vector const& parentValidations, std::shared_ptr const& initialPosition) override; }; //-------------------------------------------------------------------------- -FeeVoteImpl::FeeVoteImpl (Setup const& setup, beast::Journal journal) - : target_ (setup) - , journal_ (journal) +FeeVoteImpl::FeeVoteImpl(Setup const& setup, beast::Journal journal) + : target_(setup), journal_(journal) { } void FeeVoteImpl::doValidation( std::shared_ptr const& lastClosedLedger, - STValidation::FeeSettings& fees) + STValidation::FeeSettings& fees) { if (lastClosedLedger->fees().base != target_.reference_fee) { - JLOG(journal_.info()) << - "Voting for base fee of " << target_.reference_fee; + JLOG(journal_.info()) + << "Voting for base fee of " << target_.reference_fee; fees.baseFee = target_.reference_fee; } if (lastClosedLedger->fees().accountReserve(0) != target_.account_reserve) { - JLOG(journal_.info()) << - "Voting for base reserve of " << target_.account_reserve; + JLOG(journal_.info()) + << "Voting for base reserve of " << target_.account_reserve; fees.reserveBase = target_.account_reserve; } if (lastClosedLedger->fees().increment != target_.owner_reserve) { - JLOG(journal_.info()) << - "Voting for reserve increment of " << target_.owner_reserve; + JLOG(journal_.info()) + << "Voting for reserve increment of " << target_.owner_reserve; fees.reserveIncrement = target_.owner_reserve; } @@ -150,32 +148,29 @@ FeeVoteImpl::doVoting( std::shared_ptr const& initialPosition) { // LCL must be flag ledger - assert ((lastClosedLedger->info().seq % 256) == 0); + assert((lastClosedLedger->info().seq % 256) == 0); - detail::VotableValue baseFeeVote ( - lastClosedLedger->fees().base, - target_.reference_fee); + detail::VotableValue baseFeeVote( + lastClosedLedger->fees().base, target_.reference_fee); detail::VotableValue baseReserveVote( - lastClosedLedger->fees().accountReserve(0), - target_.account_reserve); + lastClosedLedger->fees().accountReserve(0), target_.account_reserve); - detail::VotableValue incReserveVote ( - lastClosedLedger->fees().increment, - target_.owner_reserve); + detail::VotableValue incReserveVote( + lastClosedLedger->fees().increment, target_.owner_reserve); for (auto const& val : set) { - if (val->isTrusted ()) + if (val->isTrusted()) { - if (val->isFieldPresent (sfBaseFee)) + if (val->isFieldPresent(sfBaseFee)) { using xrptype = XRPAmount::value_type; - auto const vote = val->getFieldU64 (sfBaseFee); + auto const vote = val->getFieldU64(sfBaseFee); if (vote <= std::numeric_limits::max() && isLegalAmount(XRPAmount{unsafe_cast(vote)})) - baseFeeVote.addVote(XRPAmount{ - unsafe_cast(vote)}); + baseFeeVote.addVote( + XRPAmount{unsafe_cast(vote)}); else // Invalid amounts will be treated as if they're // not provided. Don't throw because this value is @@ -184,38 +179,38 @@ FeeVoteImpl::doVoting( } else { - baseFeeVote.noVote (); + baseFeeVote.noVote(); } - if (val->isFieldPresent (sfReserveBase)) + if (val->isFieldPresent(sfReserveBase)) { - baseReserveVote.addVote(XRPAmount{ - val->getFieldU32(sfReserveBase)}); + baseReserveVote.addVote( + XRPAmount{val->getFieldU32(sfReserveBase)}); } else { - baseReserveVote.noVote (); + baseReserveVote.noVote(); } - if (val->isFieldPresent (sfReserveIncrement)) + if (val->isFieldPresent(sfReserveIncrement)) { - incReserveVote.addVote (XRPAmount{ - val->getFieldU32 (sfReserveIncrement)}); + incReserveVote.addVote( + XRPAmount{val->getFieldU32(sfReserveIncrement)}); } else { - incReserveVote.noVote (); + incReserveVote.noVote(); } } } // choose our positions // If any of the values are invalid, send the current values. - auto const baseFee = baseFeeVote.getVotes ().dropsAs( + auto const baseFee = baseFeeVote.getVotes().dropsAs( lastClosedLedger->fees().base); - auto const baseReserve = baseReserveVote.getVotes ().dropsAs( + auto const baseReserve = baseReserveVote.getVotes().dropsAs( lastClosedLedger->fees().accountReserve(0)); - auto const incReserve = incReserveVote.getVotes ().dropsAs( + auto const incReserve = incReserveVote.getVotes().dropsAs( lastClosedLedger->fees().increment); constexpr FeeUnit32 feeUnits = Setup::reference_fee_units; auto const seq = lastClosedLedger->info().seq + 1; @@ -223,16 +218,14 @@ FeeVoteImpl::doVoting( // add transactions to our position if ((baseFee != lastClosedLedger->fees().base) || (baseReserve != lastClosedLedger->fees().accountReserve(0)) || - (incReserve != lastClosedLedger->fees().increment)) + (incReserve != lastClosedLedger->fees().increment)) { - JLOG(journal_.warn()) << - "We are voting for a fee change: " << baseFee << - "/" << baseReserve << - "/" << incReserve; + JLOG(journal_.warn()) << "We are voting for a fee change: " << baseFee + << "/" << baseReserve << "/" << incReserve; - STTx feeTx (ttFEE, - [seq,baseFee,baseReserve,incReserve,feeUnits](auto& obj) - { + STTx feeTx( + ttFEE, + [seq, baseFee, baseReserve, incReserve, feeUnits](auto& obj) { obj[sfAccount] = AccountID(); obj[sfLedgerSequence] = seq; obj[sfBaseFee] = baseFee; @@ -241,20 +234,18 @@ FeeVoteImpl::doVoting( obj[sfReferenceFeeUnits] = feeUnits.fee(); }); - uint256 txID = feeTx.getTransactionID (); + uint256 txID = feeTx.getTransactionID(); - JLOG(journal_.warn()) << - "Vote: " << txID; + JLOG(journal_.warn()) << "Vote: " << txID; Serializer s; - feeTx.add (s); + feeTx.add(s); - auto tItem = std::make_shared (txID, s.peekData ()); + auto tItem = std::make_shared(txID, s.peekData()); - if (!initialPosition->addGiveItem (tItem, true, false)) + if (!initialPosition->addGiveItem(tItem, true, false)) { - JLOG(journal_.warn()) << - "Ledger already had fee change"; + JLOG(journal_.warn()) << "Ledger already had fee change"; } } } @@ -262,7 +253,7 @@ FeeVoteImpl::doVoting( //------------------------------------------------------------------------------ FeeVote::Setup -setup_FeeVote (Section const& section) +setup_FeeVote(Section const& section) { FeeVote::Setup setup; { @@ -282,9 +273,9 @@ setup_FeeVote (Section const& section) } std::unique_ptr -make_FeeVote (FeeVote::Setup const& setup, beast::Journal journal) +make_FeeVote(FeeVote::Setup const& setup, beast::Journal journal) { - return std::make_unique (setup, journal); + return std::make_unique(setup, journal); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/HashRouter.cpp b/src/ripple/app/misc/HashRouter.cpp index 54d07d6501..ea47450826 100644 --- a/src/ripple/app/misc/HashRouter.cpp +++ b/src/ripple/app/misc/HashRouter.cpp @@ -22,92 +22,97 @@ namespace ripple { auto -HashRouter::emplace (uint256 const& key) - -> std::pair +HashRouter::emplace(uint256 const& key) -> std::pair { - auto iter = suppressionMap_.find (key); + auto iter = suppressionMap_.find(key); - if (iter != suppressionMap_.end ()) + if (iter != suppressionMap_.end()) { suppressionMap_.touch(iter); - return std::make_pair( - std::ref(iter->second), false); + return std::make_pair(std::ref(iter->second), false); } // See if any supressions need to be expired expire(suppressionMap_, holdTime_); - return std::make_pair(std::ref( - suppressionMap_.emplace ( - key, Entry ()).first->second), - true); + return std::make_pair( + std::ref(suppressionMap_.emplace(key, Entry()).first->second), true); } -void HashRouter::addSuppression (uint256 const& key) +void +HashRouter::addSuppression(uint256 const& key) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - emplace (key); + emplace(key); } -bool HashRouter::addSuppressionPeer (uint256 const& key, PeerShortID peer) +bool +HashRouter::addSuppressionPeer(uint256 const& key, PeerShortID peer) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); auto result = emplace(key); result.first.addPeer(peer); return result.second; } -bool HashRouter::addSuppressionPeer (uint256 const& key, PeerShortID peer, int& flags) +bool +HashRouter::addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); auto [s, created] = emplace(key); - s.addPeer (peer); - flags = s.getFlags (); + s.addPeer(peer); + flags = s.getFlags(); return created; } -bool HashRouter::shouldProcess (uint256 const& key, PeerShortID peer, - int& flags, std::chrono::seconds tx_interval) +bool +HashRouter::shouldProcess( + uint256 const& key, + PeerShortID peer, + int& flags, + std::chrono::seconds tx_interval) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); auto result = emplace(key); auto& s = result.first; - s.addPeer (peer); - flags = s.getFlags (); - return s.shouldProcess (suppressionMap_.clock().now(), tx_interval); + s.addPeer(peer); + flags = s.getFlags(); + return s.shouldProcess(suppressionMap_.clock().now(), tx_interval); } -int HashRouter::getFlags (uint256 const& key) +int +HashRouter::getFlags(uint256 const& key) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - return emplace(key).first.getFlags (); + return emplace(key).first.getFlags(); } -bool HashRouter::setFlags (uint256 const& key, int flags) +bool +HashRouter::setFlags(uint256 const& key, int flags) { - assert (flags != 0); + assert(flags != 0); - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); auto& s = emplace(key).first; - if ((s.getFlags () & flags) == flags) + if ((s.getFlags() & flags) == flags) return false; - s.setFlags (flags); + s.setFlags(flags); return true; } auto -HashRouter::shouldRelay (uint256 const& key) +HashRouter::shouldRelay(uint256 const& key) -> boost::optional> { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); auto& s = emplace(key).first; @@ -127,4 +132,4 @@ HashRouter::shouldRecover(uint256 const& key) return s.shouldRecover(recoverLimit_); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/HashRouter.h b/src/ripple/app/misc/HashRouter.h index aa44e1f5c8..b5d29e0952 100644 --- a/src/ripple/app/misc/HashRouter.h +++ b/src/ripple/app/misc/HashRouter.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_APP_MISC_HASHROUTER_H_INCLUDED #define RIPPLE_APP_MISC_HASHROUTER_H_INCLUDED -#include -#include #include #include +#include +#include #include #include @@ -32,17 +32,17 @@ namespace ripple { // VFALCO NOTE Are these the flags?? Why aren't we using a packed struct? // VFALCO TODO convert these macros to int constants // VFALCO NOTE How can both bad and good be set on a hash? -#define SF_BAD 0x02 // Temporarily bad -#define SF_SAVED 0x04 -#define SF_TRUSTED 0x10 // comes from trusted source +#define SF_BAD 0x02 // Temporarily bad +#define SF_SAVED 0x04 +#define SF_TRUSTED 0x10 // comes from trusted source // Private flags, used internally in apply.cpp. // Do not attempt to read, set, or reuse. -#define SF_PRIVATE1 0x0100 -#define SF_PRIVATE2 0x0200 -#define SF_PRIVATE3 0x0400 -#define SF_PRIVATE4 0x0800 -#define SF_PRIVATE5 0x1000 -#define SF_PRIVATE6 0x2000 +#define SF_PRIVATE1 0x0100 +#define SF_PRIVATE2 0x0200 +#define SF_PRIVATE3 0x0400 +#define SF_PRIVATE4 0x0800 +#define SF_PRIVATE5 0x1000 +#define SF_PRIVATE6 0x2000 /** Routing table for objects identified by hash. @@ -58,34 +58,42 @@ public: private: /** An entry in the routing table. - */ - class Entry : public CountedObject + */ + class Entry : public CountedObject { public: - static char const* getCountedObjectName () { return "HashRouterEntry"; } + static char const* + getCountedObjectName() + { + return "HashRouterEntry"; + } - Entry () + Entry() { } - void addPeer (PeerShortID peer) + void + addPeer(PeerShortID peer) { if (peer != 0) - peers_.insert (peer); + peers_.insert(peer); } - int getFlags (void) const + int + getFlags(void) const { return flags_; } - void setFlags (int flagsToSet) + void + setFlags(int flagsToSet) { flags_ |= flagsToSet; } /** Return set of peers we've relayed to and reset tracking */ - std::set releasePeerSet() + std::set + releasePeerSet() { return std::move(peers_); } @@ -96,7 +104,9 @@ private: If it has, return false. If it has not, update the last relay timestamp and return true. */ - bool shouldRelay (Stopwatch::time_point const& now, + bool + shouldRelay( + Stopwatch::time_point const& now, std::chrono::seconds holdTime) { if (relayed_ && *relayed_ + holdTime > now) @@ -113,22 +123,24 @@ private: @note The limit must be > 0 */ - bool shouldRecover(std::uint32_t limit) + bool + shouldRecover(std::uint32_t limit) { return ++recoveries_ % limit != 0; } - bool shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval) + bool + shouldProcess(Stopwatch::time_point now, std::chrono::seconds interval) { - if (processed_ && ((*processed_ + interval) > now)) - return false; - processed_.emplace (now); - return true; + if (processed_ && ((*processed_ + interval) > now)) + return false; + processed_.emplace(now); + return true; } private: int flags_ = 0; - std::set peers_; + std::set peers_; // This could be generalized to a map, if more // than one flag needs to expire independently. boost::optional relayed_; @@ -137,50 +149,63 @@ private: }; public: - static inline std::chrono::seconds getDefaultHoldTime () + static inline std::chrono::seconds + getDefaultHoldTime() { using namespace std::chrono; return 300s; } - static inline std::uint32_t getDefaultRecoverLimit() + static inline std::uint32_t + getDefaultRecoverLimit() { return 1; } - HashRouter (Stopwatch& clock, std::chrono::seconds entryHoldTimeInSeconds, + HashRouter( + Stopwatch& clock, + std::chrono::seconds entryHoldTimeInSeconds, std::uint32_t recoverLimit) : suppressionMap_(clock) - , holdTime_ (entryHoldTimeInSeconds) - , recoverLimit_ (recoverLimit + 1u) + , holdTime_(entryHoldTimeInSeconds) + , recoverLimit_(recoverLimit + 1u) { } - HashRouter& operator= (HashRouter const&) = delete; + HashRouter& + operator=(HashRouter const&) = delete; virtual ~HashRouter() = default; // VFALCO TODO Replace "Supression" terminology with something more // semantically meaningful. - void addSuppression(uint256 const& key); + void + addSuppression(uint256 const& key); - bool addSuppressionPeer (uint256 const& key, PeerShortID peer); + bool + addSuppressionPeer(uint256 const& key, PeerShortID peer); - bool addSuppressionPeer (uint256 const& key, PeerShortID peer, - int& flags); + bool + addSuppressionPeer(uint256 const& key, PeerShortID peer, int& flags); // Add a peer suppression and return whether the entry should be processed - bool shouldProcess (uint256 const& key, PeerShortID peer, int& flags, + bool + shouldProcess( + uint256 const& key, + PeerShortID peer, + int& flags, std::chrono::seconds tx_interval); /** Set the flags on a hash. @return `true` if the flags were changed. `false` if unchanged. */ - bool setFlags (uint256 const& key, int flags); + bool + setFlags(uint256 const& key, int flags); - int getFlags (uint256 const& key); + int + getFlags(uint256 const& key); /** Determines whether the hashed item should be relayed. @@ -194,7 +219,8 @@ public: relayed to. If the result is uninitialized, the item should _not_ be relayed. */ - boost::optional> shouldRelay(uint256 const& key); + boost::optional> + shouldRelay(uint256 const& key); /** Determines whether the hashed item should be recovered from the open ledger into the next open ledger or the transaction @@ -202,23 +228,29 @@ public: @return `bool` indicates whether the item should be recovered */ - bool shouldRecover(uint256 const& key); + bool + shouldRecover(uint256 const& key); private: // pair.second indicates whether the entry was created - std::pair emplace (uint256 const&); + std::pair + emplace(uint256 const&); std::mutex mutable mutex_; // Stores all suppressed hashes and their expiration time - beast::aged_unordered_map> suppressionMap_; + beast::aged_unordered_map< + uint256, + Entry, + Stopwatch::clock_type, + hardened_hash> + suppressionMap_; std::chrono::seconds const holdTime_; std::uint32_t const recoverLimit_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/LoadFeeTrack.h b/src/ripple/app/misc/LoadFeeTrack.h index 43715f6d32..30c8766a9b 100644 --- a/src/ripple/app/misc/LoadFeeTrack.h +++ b/src/ripple/app/misc/LoadFeeTrack.h @@ -21,8 +21,8 @@ #define RIPPLE_CORE_LOADFEETRACK_H_INCLUDED #include -#include #include +#include #include #include #include @@ -43,51 +43,58 @@ struct Fees; class LoadFeeTrack final { public: - explicit LoadFeeTrack (beast::Journal journal = - beast::Journal(beast::Journal::getNullSink())) - : j_ (journal) - , localTxnLoadFee_ (lftNormalFee) - , remoteTxnLoadFee_ (lftNormalFee) - , clusterTxnLoadFee_ (lftNormalFee) - , raiseCount_ (0) + explicit LoadFeeTrack( + beast::Journal journal = beast::Journal(beast::Journal::getNullSink())) + : j_(journal) + , localTxnLoadFee_(lftNormalFee) + , remoteTxnLoadFee_(lftNormalFee) + , clusterTxnLoadFee_(lftNormalFee) + , raiseCount_(0) { } ~LoadFeeTrack() = default; - void setRemoteFee (std::uint32_t f) + void + setRemoteFee(std::uint32_t f) { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); remoteTxnLoadFee_ = f; } - std::uint32_t getRemoteFee () const + std::uint32_t + getRemoteFee() const { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); return remoteTxnLoadFee_; } - std::uint32_t getLocalFee () const + std::uint32_t + getLocalFee() const { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); return localTxnLoadFee_; } - std::uint32_t getClusterFee () const + std::uint32_t + getClusterFee() const { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); return clusterTxnLoadFee_; } - std::uint32_t getLoadBase () const + std::uint32_t + getLoadBase() const { return lftNormalFee; } - std::uint32_t getLoadFactor () const + std::uint32_t + getLoadFactor() const { - std::lock_guard sl (lock_); - return std::max({ clusterTxnLoadFee_, localTxnLoadFee_, remoteTxnLoadFee_ }); + std::lock_guard sl(lock_); + return std::max( + {clusterTxnLoadFee_, localTxnLoadFee_, remoteTxnLoadFee_}); } std::pair @@ -100,41 +107,49 @@ public: std::max(remoteTxnLoadFee_, clusterTxnLoadFee_)); } - - void setClusterFee (std::uint32_t fee) + void + setClusterFee(std::uint32_t fee) { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); clusterTxnLoadFee_ = fee; } - bool raiseLocalFee (); - bool lowerLocalFee (); + bool + raiseLocalFee(); + bool + lowerLocalFee(); - bool isLoadedLocal () const + bool + isLoadedLocal() const { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); return (raiseCount_ != 0) || (localTxnLoadFee_ != lftNormalFee); } - bool isLoadedCluster () const + bool + isLoadedCluster() const { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); return (raiseCount_ != 0) || (localTxnLoadFee_ != lftNormalFee) || (clusterTxnLoadFee_ != lftNormalFee); } private: - static std::uint32_t constexpr lftNormalFee = 256; // 256 is the minimum/normal load factor - static std::uint32_t constexpr lftFeeIncFraction = 4; // increase fee by 1/4 - static std::uint32_t constexpr lftFeeDecFraction = 4; // decrease fee by 1/4 + static std::uint32_t constexpr lftNormalFee = + 256; // 256 is the minimum/normal load factor + static std::uint32_t constexpr lftFeeIncFraction = + 4; // increase fee by 1/4 + static std::uint32_t constexpr lftFeeDecFraction = + 4; // decrease fee by 1/4 static std::uint32_t constexpr lftFeeMax = lftNormalFee * 1000000; beast::Journal const j_; std::mutex mutable lock_; - std::uint32_t localTxnLoadFee_; // Scale factor, lftNormalFee = normal fee - std::uint32_t remoteTxnLoadFee_; // Scale factor, lftNormalFee = normal fee - std::uint32_t clusterTxnLoadFee_; // Scale factor, lftNormalFee = normal fee + std::uint32_t localTxnLoadFee_; // Scale factor, lftNormalFee = normal fee + std::uint32_t remoteTxnLoadFee_; // Scale factor, lftNormalFee = normal fee + std::uint32_t + clusterTxnLoadFee_; // Scale factor, lftNormalFee = normal fee std::uint32_t raiseCount_; }; @@ -142,9 +157,12 @@ private: // Scale using load as well as base rate XRPAmount -scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, - Fees const& fees, bool bUnlimited); +scaleFeeLoad( + FeeUnit64 fee, + LoadFeeTrack const& feeTrack, + Fees const& fees, + bool bUnlimited); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/Manifest.h b/src/ripple/app/misc/Manifest.h index c1ddb92327..ec8fe0f4b5 100644 --- a/src/ripple/app/misc/Manifest.h +++ b/src/ripple/app/misc/Manifest.h @@ -21,9 +21,9 @@ #define RIPPLE_APP_MISC_MANIFEST_H_INCLUDED #include +#include #include #include -#include #include #include @@ -33,7 +33,7 @@ namespace ripple { Validator key manifests ----------------------- - Suppose the secret keys installed on a Ripple validator are compromised. Not + Suppose the secret keys installed on a Ripple validator are compromised. Not only do you have to generate and install new key pairs on each validator, EVERY rippled needs to have its config updated with the new public keys, and is vulnerable to forged validation signatures until this is done. The @@ -94,24 +94,31 @@ struct Manifest Manifest() = default; Manifest(Manifest const& other) = delete; - Manifest& operator=(Manifest const& other) = delete; + Manifest& + operator=(Manifest const& other) = delete; Manifest(Manifest&& other) = default; - Manifest& operator=(Manifest&& other) = default; + Manifest& + operator=(Manifest&& other) = default; /// Returns `true` if manifest signature is valid - bool verify () const; + bool + verify() const; /// Returns hash of serialized manifest data - uint256 hash () const; + uint256 + hash() const; /// Returns `true` if manifest revokes master key - bool revoked () const; + bool + revoked() const; /// Returns manifest signature - boost::optional getSignature () const; + boost::optional + getSignature() const; /// Returns manifest master key signature - Blob getMasterSignature () const; + Blob + getMasterSignature() const; }; /** Constructs Manifest from serialized string @@ -127,15 +134,16 @@ struct Manifest boost::optional deserializeManifest(Slice s); -inline -boost::optional +inline boost::optional deserializeManifest(std::string const& s) { return deserializeManifest(makeSlice(s)); } -template ::value || std::is_same::value>> +template < + class T, + class = std::enable_if_t< + std::is_same::value || std::is_same::value>> boost::optional deserializeManifest(std::vector const& v) { @@ -143,21 +151,17 @@ deserializeManifest(std::vector const& v) } /** @} */ -inline -bool +inline bool operator==(Manifest const& lhs, Manifest const& rhs) { // In theory, comparing the two serialized strings should be // sufficient. - return lhs.sequence == rhs.sequence && - lhs.masterKey == rhs.masterKey && - lhs.signingKey == rhs.signingKey && - lhs.domain == rhs.domain && + return lhs.sequence == rhs.sequence && lhs.masterKey == rhs.masterKey && + lhs.signingKey == rhs.signingKey && lhs.domain == rhs.domain && lhs.serialized == rhs.serialized; } -inline -bool +inline bool operator!=(Manifest const& lhs, Manifest const& rhs) { return !(lhs == rhs); @@ -179,8 +183,7 @@ public: make_ValidatorToken(std::vector const& tokenBlob); }; -enum class ManifestDisposition -{ +enum class ManifestDisposition { /// Manifest is valid accepted = 0, @@ -218,16 +221,15 @@ private: std::mutex mutable read_mutex_; /** Active manifests stored by master public key. */ - hash_map map_; + hash_map map_; /** Master public keys stored by current ephemeral public key. */ - hash_map signingToMasterKeys_; + hash_map signingToMasterKeys_; public: - explicit - ManifestCache (beast::Journal j = - beast::Journal(beast::Journal::getNullSink())) - : j_ (j) + explicit ManifestCache( + beast::Journal j = beast::Journal(beast::Journal::getNullSink())) + : j_(j) { } @@ -242,7 +244,7 @@ public: May be called concurrently */ PublicKey - getSigningKey (PublicKey const& pk) const; + getSigningKey(PublicKey const& pk) const; /** Returns ephemeral signing key's master public key. @@ -255,7 +257,7 @@ public: May be called concurrently */ PublicKey - getMasterKey (PublicKey const& pk) const; + getMasterKey(PublicKey const& pk) const; /** Returns master key's current manifest sequence. @@ -263,7 +265,7 @@ public: if configured or boost::none otherwise */ boost::optional - getSequence (PublicKey const& pk) const; + getSequence(PublicKey const& pk) const; /** Returns domain claimed by a given public key @@ -271,7 +273,7 @@ public: if present, otherwise boost::none */ boost::optional - getDomain (PublicKey const& pk) const; + getDomain(PublicKey const& pk) const; /** Returns mainfest corresponding to a given public key @@ -279,7 +281,7 @@ public: if present, otherwise boost::none */ boost::optional - getManifest (PublicKey const& pk) const; + getManifest(PublicKey const& pk) const; /** Returns `true` if master key has been revoked in a manifest. @@ -290,7 +292,7 @@ public: May be called concurrently */ bool - revoked (PublicKey const& pk) const; + revoked(PublicKey const& pk) const; /** Add manifest to cache. @@ -304,8 +306,7 @@ public: May be called concurrently */ ManifestDisposition - applyManifest ( - Manifest m); + applyManifest(Manifest m); /** Populate manifest cache with manifests in database and config. @@ -323,8 +324,10 @@ public: May be called concurrently */ - bool load ( - DatabaseCon& dbCon, std::string const& dbTable, + bool + load( + DatabaseCon& dbCon, + std::string const& dbTable, std::string const& configManifest, std::vector const& configRevocation); @@ -338,8 +341,8 @@ public: May be called concurrently */ - void load ( - DatabaseCon& dbCon, std::string const& dbTable); + void + load(DatabaseCon& dbCon, std::string const& dbTable); /** Save cached manifests to database. @@ -351,9 +354,11 @@ public: May be called concurrently */ - void save ( - DatabaseCon& dbCon, std::string const& dbTable, - std::function isTrusted); + void + save( + DatabaseCon& dbCon, + std::string const& dbTable, + std::function isTrusted); /** Invokes the callback once for every populated manifest. @@ -397,7 +402,7 @@ public: for_each_manifest(PreFun&& pf, EachFun&& f) const { std::lock_guard lock{read_mutex_}; - pf(map_.size ()); + pf(map_.size()); for (auto const& [_, manifest] : map_) { (void)_; @@ -406,6 +411,6 @@ public: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/NetworkOPs.cpp b/src/ripple/app/misc/NetworkOPs.cpp index b0ca013090..077e0ac1ae 100644 --- a/src/ripple/app/misc/NetworkOPs.cpp +++ b/src/ripple/app/misc/NetworkOPs.cpp @@ -17,14 +17,11 @@ */ //============================================================================== -#include -#include #include #include #include #include #include -#include #include #include #include @@ -34,20 +31,26 @@ #include #include #include +#include #include #include #include #include #include #include +#include +#include #include #include -#include #include -#include +#include +#include +#include +#include +#include #include -#include #include +#include #include #include #include @@ -55,11 +58,8 @@ #include #include #include -#include -#include -#include -#include #include +#include #include #include @@ -68,8 +68,7 @@ namespace ripple { -class NetworkOPsImp final - : public NetworkOPs +class NetworkOPsImp final : public NetworkOPs { /** * Transaction with input flags and results to be applied in batches. @@ -85,15 +84,12 @@ class NetworkOPsImp final bool applied; TER result; - TransactionStatus ( - std::shared_ptr t, - bool a, - bool l, - FailHard f) - : transaction (t) - , admin (a) - , local (l) - , failType (f) + TransactionStatus( + std::shared_ptr t, + bool a, + bool l, + FailHard f) + : transaction(t), admin(a), local(l), failType(f) { assert(local || failType == FailHard::no); } @@ -102,8 +98,7 @@ class NetworkOPsImp final /** * Synchronization states for transaction batches. */ - enum class DispatchState : unsigned char - { + enum class DispatchState : unsigned char { none, scheduled, running, @@ -132,7 +127,7 @@ class NetworkOPsImp final explicit Counters() = default; std::uint32_t transitions = 0; - std::chrono::microseconds dur = std::chrono::microseconds (0); + std::chrono::microseconds dur = std::chrono::microseconds(0); }; OperatingMode mode_ = OperatingMode::DISCONNECTED; @@ -145,10 +140,10 @@ class NetworkOPsImp final static Json::StaticString const dur_; public: - explicit StateAccounting () + explicit StateAccounting() { - counters_[static_cast( - OperatingMode::DISCONNECTED)].transitions = 1; + counters_[static_cast(OperatingMode::DISCONNECTED)] + .transitions = 1; } /** @@ -157,31 +152,36 @@ class NetworkOPsImp final * * @param om New state. */ - void mode (OperatingMode om); + void + mode(OperatingMode om); /** * Json-formatted state accounting data. * 1st member: state accounting object. * 2nd member: duration in current state. */ - using StateCountersJson = std::pair ; + using StateCountersJson = std::pair; /** * Output state counters in JSON format. * * @return JSON object. */ - StateCountersJson json() const; + StateCountersJson + json() const; - struct CounterData { + struct CounterData + { decltype(counters_) counters; decltype(mode_) mode; decltype(start_) start; }; - CounterData getCounterData() const{ + CounterData + getCounterData() const + { std::lock_guard lock(mutex_); - return { counters_, mode_, start_ }; + return {counters_, mode_, start_}; } }; @@ -190,54 +190,63 @@ class NetworkOPsImp final { ServerFeeSummary() = default; - ServerFeeSummary(XRPAmount fee, - TxQ::Metrics&& escalationMetrics, - LoadFeeTrack const & loadFeeTrack); + ServerFeeSummary( + XRPAmount fee, + TxQ::Metrics&& escalationMetrics, + LoadFeeTrack const& loadFeeTrack); bool - operator !=(ServerFeeSummary const & b) const; + operator!=(ServerFeeSummary const& b) const; bool - operator ==(ServerFeeSummary const & b) const + operator==(ServerFeeSummary const& b) const { return !(*this != b); } std::uint32_t loadFactorServer = 256; std::uint32_t loadBaseServer = 256; - XRPAmount baseFee{ 10 }; + XRPAmount baseFee{10}; boost::optional em = boost::none; }; - public: - NetworkOPsImp (Application& app, NetworkOPs::clock_type& clock, - bool standalone, std::size_t minPeerCount, bool start_valid, - JobQueue& job_queue, LedgerMaster& ledgerMaster, Stoppable& parent, - ValidatorKeys const & validatorKeys, boost::asio::io_service& io_svc, - beast::Journal journal, beast::insight::Collector::ptr const& collector) - : NetworkOPs (parent) - , app_ (app) - , m_clock (clock) - , m_journal (journal) - , m_localTX (make_LocalTxs ()) - , mMode (start_valid ? OperatingMode::FULL : - OperatingMode::DISCONNECTED) - , heartbeatTimer_ (io_svc) - , clusterTimer_ (io_svc) - , mConsensus (app, - make_FeeVote(setup_FeeVote (app_.config().section ("voting")), - app_.logs().journal("FeeVote")), - ledgerMaster, - *m_localTX, - app.getInboundTransactions(), - beast::get_abstract_clock(), - validatorKeys, - app_.logs().journal("LedgerConsensus")) - , m_ledgerMaster (ledgerMaster) - , m_job_queue (job_queue) - , m_standalone (standalone) - , minPeerCount_ (start_valid ? 0 : minPeerCount) - , m_stats(std::bind (&NetworkOPsImp::collect_metrics, this),collector) + NetworkOPsImp( + Application& app, + NetworkOPs::clock_type& clock, + bool standalone, + std::size_t minPeerCount, + bool start_valid, + JobQueue& job_queue, + LedgerMaster& ledgerMaster, + Stoppable& parent, + ValidatorKeys const& validatorKeys, + boost::asio::io_service& io_svc, + beast::Journal journal, + beast::insight::Collector::ptr const& collector) + : NetworkOPs(parent) + , app_(app) + , m_clock(clock) + , m_journal(journal) + , m_localTX(make_LocalTxs()) + , mMode(start_valid ? OperatingMode::FULL : OperatingMode::DISCONNECTED) + , heartbeatTimer_(io_svc) + , clusterTimer_(io_svc) + , mConsensus( + app, + make_FeeVote( + setup_FeeVote(app_.config().section("voting")), + app_.logs().journal("FeeVote")), + ledgerMaster, + *m_localTX, + app.getInboundTransactions(), + beast::get_abstract_clock(), + validatorKeys, + app_.logs().journal("LedgerConsensus")) + , m_ledgerMaster(ledgerMaster) + , m_job_queue(job_queue) + , m_standalone(standalone) + , minPeerCount_(start_valid ? 0 : minPeerCount) + , m_stats(std::bind(&NetworkOPsImp::collect_metrics, this), collector) { } @@ -250,15 +259,17 @@ public: } public: - OperatingMode getOperatingMode () const override + OperatingMode + getOperatingMode() const override { return mMode; } - std::string strOperatingMode ( - OperatingMode const mode, bool const admin) const override; + std::string + strOperatingMode(OperatingMode const mode, bool const admin) const override; - std::string strOperatingMode (bool const admin = false) const override + std::string + strOperatingMode(bool const admin = false) const override { return strOperatingMode(mMode, admin); } @@ -268,11 +279,15 @@ public: // // Must complete immediately. - void submitTransaction (std::shared_ptr const&) override; + void + submitTransaction(std::shared_ptr const&) override; - void processTransaction ( + void + processTransaction( std::shared_ptr& transaction, - bool bUnlimited, bool bLocal, FailHard failType) override; + bool bUnlimited, + bool bLocal, + FailHard failType) override; /** * For transactions submitted directly by a client, apply batch of @@ -282,8 +297,11 @@ public: * @param bUnliimited Whether a privileged client connection submitted it. * @param failType fail_hard setting from transaction submission. */ - void doTransactionSync (std::shared_ptr transaction, - bool bUnlimited, FailHard failType); + void + doTransactionSync( + std::shared_ptr transaction, + bool bUnlimited, + FailHard failType); /** * For transactions not submitted by a locally connected client, fire and @@ -294,26 +312,32 @@ public: * @param bUnlimited Whether a privileged client connection submitted it. * @param failType fail_hard setting from transaction submission. */ - void doTransactionAsync (std::shared_ptr transaction, - bool bUnlimited, FailHard failtype); + void + doTransactionAsync( + std::shared_ptr transaction, + bool bUnlimited, + FailHard failtype); /** * Apply transactions in batches. Continue until none are queued. */ - void transactionBatch(); + void + transactionBatch(); /** * Attempt to apply transactions and post-process based on the results. * * @param Lock that protects the transaction batching */ - void apply (std::unique_lock& batchLock); + void + apply(std::unique_lock& batchLock); // // Owner functions. // - Json::Value getOwnerInfo ( + Json::Value + getOwnerInfo( std::shared_ptr lpLedger, AccountID const& account) override; @@ -321,75 +345,93 @@ public: // Book functions. // - void getBookPage (std::shared_ptr& lpLedger, - Book const&, AccountID const& uTakerID, const bool bProof, - unsigned int iLimit, - Json::Value const& jvMarker, Json::Value& jvResult) - override; + void + getBookPage( + std::shared_ptr& lpLedger, + Book const&, + AccountID const& uTakerID, + const bool bProof, + unsigned int iLimit, + Json::Value const& jvMarker, + Json::Value& jvResult) override; // Ledger proposal/close functions. - void processTrustedProposal ( + void + processTrustedProposal( RCLCxPeerPos proposal, std::shared_ptr set) override; - bool recvValidation ( - STValidation::ref val, std::string const& source) override; + bool + recvValidation(STValidation::ref val, std::string const& source) override; - std::shared_ptr getTXMap (uint256 const& hash); - bool hasTXSet ( - const std::shared_ptr& peer, uint256 const& set, + std::shared_ptr + getTXMap(uint256 const& hash); + bool + hasTXSet( + const std::shared_ptr& peer, + uint256 const& set, protocol::TxSetStatus status); - void mapComplete ( - std::shared_ptr const& map, - bool fromAcquire) override; + void + mapComplete(std::shared_ptr const& map, bool fromAcquire) override; // Network state machine. // Used for the "jump" case. private: - void switchLastClosedLedger ( - std::shared_ptr const& newLCL); - bool checkLastClosedLedger ( - const Overlay::PeerSequence&, uint256& networkClosed); + void + switchLastClosedLedger(std::shared_ptr const& newLCL); + bool + checkLastClosedLedger(const Overlay::PeerSequence&, uint256& networkClosed); public: - bool beginConsensus (uint256 const& networkClosed) override; - void endConsensus () override; - void setStandAlone () override + bool + beginConsensus(uint256 const& networkClosed) override; + void + endConsensus() override; + void + setStandAlone() override { - setMode (OperatingMode::FULL); + setMode(OperatingMode::FULL); } /** Called to initially start our timers. Not called for stand-alone mode. */ - void setStateTimer () override; + void + setStateTimer() override; - void setNeedNetworkLedger () override + void + setNeedNetworkLedger() override { needNetworkLedger_ = true; } - void clearNeedNetworkLedger () override + void + clearNeedNetworkLedger() override { needNetworkLedger_ = false; } - bool isNeedNetworkLedger () override + bool + isNeedNetworkLedger() override { return needNetworkLedger_; } - bool isFull () override + bool + isFull() override { return !needNetworkLedger_ && (mMode == OperatingMode::FULL); } - void setMode (OperatingMode om) override; + void + setMode(OperatingMode om) override; - bool isAmendmentBlocked () override + bool + isAmendmentBlocked() override { return amendmentBlocked_; } - void setAmendmentBlocked () override; + void + setAmendmentBlocked() override; bool isAmendmentWarned() override { @@ -405,41 +447,64 @@ public: { amendmentWarned_ = false; } - void consensusViewChange () override; + void + consensusViewChange() override; - Json::Value getConsensusInfo () override; - Json::Value getServerInfo (bool human, bool admin, bool counters) override; - void clearLedgerFetch () override; - Json::Value getLedgerFetchInfo () override; - std::uint32_t acceptLedger ( + Json::Value + getConsensusInfo() override; + Json::Value + getServerInfo(bool human, bool admin, bool counters) override; + void + clearLedgerFetch() override; + Json::Value + getLedgerFetchInfo() override; + std::uint32_t + acceptLedger( boost::optional consensusDelay) override; - uint256 getConsensusLCL () override; - void reportFeeChange () override; - void reportConsensusStateChange(ConsensusPhase phase); + uint256 + getConsensusLCL() override; + void + reportFeeChange() override; + void + reportConsensusStateChange(ConsensusPhase phase); - void updateLocalTx (ReadView const& view) override + void + updateLocalTx(ReadView const& view) override { - m_localTX->sweep (view); + m_localTX->sweep(view); } - std::size_t getLocalTxCount () override + std::size_t + getLocalTxCount() override { - return m_localTX->size (); + return m_localTX->size(); } - //Helper function to generate SQL query to get transactions. - std::string transactionsSQL ( - std::string selection, AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, - bool descending, std::uint32_t offset, int limit, - bool binary, bool count, bool bUnlimited); + // Helper function to generate SQL query to get transactions. + std::string + transactionsSQL( + std::string selection, + AccountID const& account, + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool binary, + bool count, + bool bUnlimited); // Client information retrieval functions. using NetworkOPs::AccountTxMarker; using NetworkOPs::AccountTxs; - AccountTxs getAccountTxs ( + AccountTxs + getAccountTxs( AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, bool bUnlimited) override; + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) override; AccountTxs getTxsAccount( @@ -451,15 +516,18 @@ public: int limit, bool bUnlimited) override; - using NetworkOPs::txnMetaLedgerType; using NetworkOPs::MetaTxsList; + using NetworkOPs::txnMetaLedgerType; MetaTxsList - getAccountTxsB ( - AccountID const& account, std::int32_t minLedger, - std::int32_t maxLedger, bool descending, std::uint32_t offset, - int limit, bool bUnlimited) override; - + getAccountTxsB( + AccountID const& account, + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) override; MetaTxsList getTxsAccountB( @@ -474,90 +542,119 @@ public: // // Monitoring: publisher side. // - void pubLedger ( - std::shared_ptr const& lpAccepted) override; - void pubProposedTransaction ( + void + pubLedger(std::shared_ptr const& lpAccepted) override; + void + pubProposedTransaction( std::shared_ptr const& lpCurrent, - std::shared_ptr const& stTxn, TER terResult) override; - void pubValidation ( - STValidation::ref val) override; + std::shared_ptr const& stTxn, + TER terResult) override; + void + pubValidation(STValidation::ref val) override; //-------------------------------------------------------------------------- // // InfoSub::Source. // - void subAccount ( + void + subAccount( InfoSub::ref ispListener, - hash_set const& vnaAccountIDs, bool rt) override; - void unsubAccount ( + hash_set const& vnaAccountIDs, + bool rt) override; + void + unsubAccount( InfoSub::ref ispListener, hash_set const& vnaAccountIDs, bool rt) override; // Just remove the subscription from the tracking // not from the InfoSub. Needed for InfoSub destruction - void unsubAccountInternal ( + void + unsubAccountInternal( std::uint64_t seq, hash_set const& vnaAccountIDs, bool rt) override; - bool subLedger (InfoSub::ref ispListener, Json::Value& jvResult) override; - bool unsubLedger (std::uint64_t uListener) override; + bool + subLedger(InfoSub::ref ispListener, Json::Value& jvResult) override; + bool + unsubLedger(std::uint64_t uListener) override; - bool subServer ( - InfoSub::ref ispListener, Json::Value& jvResult, bool admin) override; - bool unsubServer (std::uint64_t uListener) override; + bool + subServer(InfoSub::ref ispListener, Json::Value& jvResult, bool admin) + override; + bool + unsubServer(std::uint64_t uListener) override; - bool subBook (InfoSub::ref ispListener, Book const&) override; - bool unsubBook (std::uint64_t uListener, Book const&) override; + bool + subBook(InfoSub::ref ispListener, Book const&) override; + bool + unsubBook(std::uint64_t uListener, Book const&) override; - bool subManifests (InfoSub::ref ispListener) override; - bool unsubManifests (std::uint64_t uListener) override; - void pubManifest (Manifest const&) override; + bool + subManifests(InfoSub::ref ispListener) override; + bool + unsubManifests(std::uint64_t uListener) override; + void + pubManifest(Manifest const&) override; - bool subTransactions (InfoSub::ref ispListener) override; - bool unsubTransactions (std::uint64_t uListener) override; + bool + subTransactions(InfoSub::ref ispListener) override; + bool + unsubTransactions(std::uint64_t uListener) override; - bool subRTTransactions (InfoSub::ref ispListener) override; - bool unsubRTTransactions (std::uint64_t uListener) override; + bool + subRTTransactions(InfoSub::ref ispListener) override; + bool + unsubRTTransactions(std::uint64_t uListener) override; - bool subValidations (InfoSub::ref ispListener) override; - bool unsubValidations (std::uint64_t uListener) override; + bool + subValidations(InfoSub::ref ispListener) override; + bool + unsubValidations(std::uint64_t uListener) override; - bool subPeerStatus (InfoSub::ref ispListener) override; - bool unsubPeerStatus (std::uint64_t uListener) override; - void pubPeerStatus (std::function const&) override; + bool + subPeerStatus(InfoSub::ref ispListener) override; + bool + unsubPeerStatus(std::uint64_t uListener) override; + void + pubPeerStatus(std::function const&) override; - bool subConsensus (InfoSub::ref ispListener) override; - bool unsubConsensus (std::uint64_t uListener) override; + bool + subConsensus(InfoSub::ref ispListener) override; + bool + unsubConsensus(std::uint64_t uListener) override; - InfoSub::pointer findRpcSub (std::string const& strUrl) override; - InfoSub::pointer addRpcSub ( - std::string const& strUrl, InfoSub::ref) override; - bool tryRemoveRpcSub (std::string const& strUrl) override; + InfoSub::pointer + findRpcSub(std::string const& strUrl) override; + InfoSub::pointer + addRpcSub(std::string const& strUrl, InfoSub::ref) override; + bool + tryRemoveRpcSub(std::string const& strUrl) override; //-------------------------------------------------------------------------- // // Stoppable. - void onStop () override + void + onStop() override { mAcquiringLedger.reset(); { boost::system::error_code ec; - heartbeatTimer_.cancel (ec); + heartbeatTimer_.cancel(ec); if (ec) { - JLOG (m_journal.error()) + JLOG(m_journal.error()) << "NetworkOPs: heartbeatTimer cancel error: " << ec.message(); } ec.clear(); - clusterTimer_.cancel (ec); + clusterTimer_.cancel(ec); if (ec) { - JLOG (m_journal.error()) + JLOG(m_journal.error()) << "NetworkOPs: clusterTimer cancel error: " << ec.message(); } @@ -566,50 +663,62 @@ public: // before we declare ourselves stopped. using namespace std::chrono_literals; waitHandlerCounter_.join("NetworkOPs", 1s, m_journal); - stopped (); + stopped(); } private: - void setHeartbeatTimer (); - void setClusterTimer (); - void processHeartbeatTimer (); - void processClusterTimer (); + void + setHeartbeatTimer(); + void + setClusterTimer(); + void + processHeartbeatTimer(); + void + processClusterTimer(); - Json::Value transJson ( - const STTx& stTxn, TER terResult, bool bValidated, + Json::Value + transJson( + const STTx& stTxn, + TER terResult, + bool bValidated, std::shared_ptr const& lpCurrent); - void pubValidatedTransaction ( + void + pubValidatedTransaction( std::shared_ptr const& alAccepted, const AcceptedLedgerTx& alTransaction); - void pubAccountTransaction ( + void + pubAccountTransaction( std::shared_ptr const& lpCurrent, const AcceptedLedgerTx& alTransaction, bool isAccepted); - void pubServer (); - void pubConsensus (ConsensusPhase phase); + void + pubServer(); + void + pubConsensus(ConsensusPhase phase); - std::string getHostId (bool forAdmin); + std::string + getHostId(bool forAdmin); private: - using SubMapType = hash_map ; - using SubInfoMapType = hash_map ; + using SubMapType = hash_map; + using SubInfoMapType = hash_map; using subRpcMapType = hash_map; Application& app_; clock_type& m_clock; beast::Journal m_journal; - std::unique_ptr m_localTX; + std::unique_ptr m_localTX; std::recursive_mutex mSubLock; std::atomic mMode; - std::atomic needNetworkLedger_ {false}; - std::atomic amendmentBlocked_ {false}; - std::atomic amendmentWarned_ {false}; + std::atomic needNetworkLedger_{false}; + std::atomic amendmentBlocked_{false}; + std::atomic amendmentWarned_{false}; ClosureCounter waitHandlerCounter_; boost::asio::steady_timer heartbeatTimer_; @@ -627,25 +736,23 @@ private: subRpcMapType mRpcSubMap; - enum SubTypes - { - sLedger, // Accepted ledgers. - sManifests, // Received validator manifests. - sServer, // When server changes connectivity state. - sTransactions, // All accepted transactions. - sRTTransactions, // All proposed and accepted transactions. - sValidations, // Received validations. - sPeerStatus, // Peer status changes. - sConsensusPhase, // Consensus phase + enum SubTypes { + sLedger, // Accepted ledgers. + sManifests, // Received validator manifests. + sServer, // When server changes connectivity state. + sTransactions, // All accepted transactions. + sRTTransactions, // All proposed and accepted transactions. + sValidations, // Received validations. + sPeerStatus, // Peer status changes. + sConsensusPhase, // Consensus phase - sLastEntry = sConsensusPhase // as this name implies, any new entry must - // be ADDED ABOVE this one + sLastEntry = sConsensusPhase // as this name implies, any new entry + // must be ADDED ABOVE this one }; - std::array mStreamMaps; + std::array mStreamMaps; ServerFeeSummary mLastFeeSummary; - JobQueue& m_job_queue; // Whether we are in standalone mode. @@ -658,27 +765,47 @@ private: std::condition_variable mCond; std::mutex mMutex; DispatchState mDispatchState = DispatchState::none; - std::vector mTransactions; + std::vector mTransactions; - StateAccounting accounting_ {}; + StateAccounting accounting_{}; private: struct Stats { template - Stats (Handler const& handler, beast::insight::Collector::ptr const& collector) - : hook (collector->make_hook (handler)) - , disconnected_duration (collector->make_gauge("State_Accounting","Disconnected_duration")) - , connected_duration (collector->make_gauge("State_Accounting","Connected_duration")) - , syncing_duration(collector->make_gauge("State_Accounting", "Syncing_duration")) - , tracking_duration (collector->make_gauge("State_Accounting", "Tracking_duration")) - , full_duration (collector-> make_gauge("State_Accounting", "Full_duration")) - , disconnected_transitions (collector->make_gauge("State_Accounting","Disconnected_transitions")) - , connected_transitions (collector->make_gauge("State_Accounting","Connected_transitions")) - , syncing_transitions(collector->make_gauge("State_Accounting", "Syncing_transitions")) - , tracking_transitions (collector->make_gauge("State_Accounting", "Tracking_transitions")) - , full_transitions (collector-> make_gauge("State_Accounting", "Full_transitions")) - { } + Stats( + Handler const& handler, + beast::insight::Collector::ptr const& collector) + : hook(collector->make_hook(handler)) + , disconnected_duration(collector->make_gauge( + "State_Accounting", + "Disconnected_duration")) + , connected_duration(collector->make_gauge( + "State_Accounting", + "Connected_duration")) + , syncing_duration( + collector->make_gauge("State_Accounting", "Syncing_duration")) + , tracking_duration(collector->make_gauge( + "State_Accounting", + "Tracking_duration")) + , full_duration( + collector->make_gauge("State_Accounting", "Full_duration")) + , disconnected_transitions(collector->make_gauge( + "State_Accounting", + "Disconnected_transitions")) + , connected_transitions(collector->make_gauge( + "State_Accounting", + "Connected_transitions")) + , syncing_transitions(collector->make_gauge( + "State_Accounting", + "Syncing_transitions")) + , tracking_transitions(collector->make_gauge( + "State_Accounting", + "Tracking_transitions")) + , full_transitions( + collector->make_gauge("State_Accounting", "Full_transitions")) + { + } beast::insight::Hook hook; beast::insight::Gauge disconnected_duration; @@ -693,54 +820,73 @@ private: beast::insight::Gauge tracking_transitions; beast::insight::Gauge full_transitions; }; - - std::mutex m_statsMutex;//Mutex to lock m_stats + + std::mutex m_statsMutex; // Mutex to lock m_stats Stats m_stats; private: - void collect_metrics() - { + void + collect_metrics() + { auto [counters, mode, start] = accounting_.getCounterData(); - auto const current = std::chrono::duration_cast(std::chrono::system_clock::now() - start); + auto const current = + std::chrono::duration_cast( + std::chrono::system_clock::now() - start); counters[static_cast(mode)].dur += current; - std::lock_guard lock (m_statsMutex); - m_stats.disconnected_duration.set(counters[static_cast(OperatingMode::DISCONNECTED)].dur.count()); - m_stats.connected_duration.set(counters[static_cast(OperatingMode::CONNECTED)].dur.count()); - m_stats.syncing_duration.set(counters[static_cast(OperatingMode::SYNCING)].dur.count()); - m_stats.tracking_duration.set(counters[static_cast(OperatingMode::TRACKING)].dur.count()); - m_stats.full_duration.set(counters[static_cast(OperatingMode::FULL)].dur.count()); + std::lock_guard lock(m_statsMutex); + m_stats.disconnected_duration.set( + counters[static_cast(OperatingMode::DISCONNECTED)] + .dur.count()); + m_stats.connected_duration.set( + counters[static_cast(OperatingMode::CONNECTED)] + .dur.count()); + m_stats.syncing_duration.set( + counters[static_cast(OperatingMode::SYNCING)] + .dur.count()); + m_stats.tracking_duration.set( + counters[static_cast(OperatingMode::TRACKING)] + .dur.count()); + m_stats.full_duration.set( + counters[static_cast(OperatingMode::FULL)] + .dur.count()); - m_stats.disconnected_transitions.set(counters[static_cast(OperatingMode::DISCONNECTED)].transitions); - m_stats.connected_transitions.set(counters[static_cast(OperatingMode::CONNECTED)].transitions); - m_stats.syncing_transitions.set(counters[static_cast(OperatingMode::SYNCING)].transitions); - m_stats.tracking_transitions.set(counters[static_cast(OperatingMode::TRACKING)].transitions); - m_stats.full_transitions.set(counters[static_cast(OperatingMode::FULL)].transitions); + m_stats.disconnected_transitions.set( + counters[static_cast(OperatingMode::DISCONNECTED)] + .transitions); + m_stats.connected_transitions.set( + counters[static_cast(OperatingMode::CONNECTED)] + .transitions); + m_stats.syncing_transitions.set( + counters[static_cast(OperatingMode::SYNCING)] + .transitions); + m_stats.tracking_transitions.set( + counters[static_cast(OperatingMode::TRACKING)] + .transitions); + m_stats.full_transitions.set( + counters[static_cast(OperatingMode::FULL)] + .transitions); } }; //------------------------------------------------------------------------------ -static std::array const stateNames {{ - "disconnected", - "connected", - "syncing", - "tracking", - "full"}}; +static std::array const stateNames{ + {"disconnected", "connected", "syncing", "tracking", "full"}}; std::array const NetworkOPsImp::states_ = stateNames; std::array const -NetworkOPsImp::StateAccounting::states_ = {{ - Json::StaticString(stateNames[0]), - Json::StaticString(stateNames[1]), - Json::StaticString(stateNames[2]), - Json::StaticString(stateNames[3]), - Json::StaticString(stateNames[4])}}; + NetworkOPsImp::StateAccounting::states_ = { + {Json::StaticString(stateNames[0]), + Json::StaticString(stateNames[1]), + Json::StaticString(stateNames[2]), + Json::StaticString(stateNames[3]), + Json::StaticString(stateNames[4])}}; //------------------------------------------------------------------------------ std::string -NetworkOPsImp::getHostId (bool forAdmin) +NetworkOPsImp::getHostId(bool forAdmin) { static std::string const hostname = boost::asio::ip::host_name(); @@ -749,102 +895,103 @@ NetworkOPsImp::getHostId (bool forAdmin) // For non-admin uses hash the node public key into a // single RFC1751 word: - static std::string const shroudedHostId = - [this]() - { - auto const& id = app_.nodeIdentity(); + static std::string const shroudedHostId = [this]() { + auto const& id = app_.nodeIdentity(); - return RFC1751::getWordFromBlob ( - id.first.data (), - id.first.size ()); - }(); + return RFC1751::getWordFromBlob(id.first.data(), id.first.size()); + }(); return shroudedHostId; } -void NetworkOPsImp::setStateTimer () +void +NetworkOPsImp::setStateTimer() { - setHeartbeatTimer (); - setClusterTimer (); + setHeartbeatTimer(); + setClusterTimer(); } -void NetworkOPsImp::setHeartbeatTimer () +void +NetworkOPsImp::setHeartbeatTimer() { // Only start the timer if waitHandlerCounter_ is not yet joined. - if (auto optionalCountedHandler = waitHandlerCounter_.wrap ( - [this] (boost::system::error_code const& e) - { - if ((e.value() == boost::system::errc::success) && - (! m_job_queue.isStopped())) - { - m_job_queue.addJob (jtNETOP_TIMER, "NetOPs.heartbeat", - [this] (Job&) { processHeartbeatTimer(); }); - } - // Recover as best we can if an unexpected error occurs. - if (e.value() != boost::system::errc::success && - e.value() != boost::asio::error::operation_aborted) - { - // Try again later and hope for the best. - JLOG (m_journal.error()) - << "Heartbeat timer got error '" << e.message() - << "'. Restarting timer."; - setHeartbeatTimer(); - } - })) + if (auto optionalCountedHandler = waitHandlerCounter_.wrap( + [this](boost::system::error_code const& e) { + if ((e.value() == boost::system::errc::success) && + (!m_job_queue.isStopped())) + { + m_job_queue.addJob( + jtNETOP_TIMER, "NetOPs.heartbeat", [this](Job&) { + processHeartbeatTimer(); + }); + } + // Recover as best we can if an unexpected error occurs. + if (e.value() != boost::system::errc::success && + e.value() != boost::asio::error::operation_aborted) + { + // Try again later and hope for the best. + JLOG(m_journal.error()) + << "Heartbeat timer got error '" << e.message() + << "'. Restarting timer."; + setHeartbeatTimer(); + } + })) { - heartbeatTimer_.expires_from_now ( - mConsensus.parms().ledgerGRANULARITY); - heartbeatTimer_.async_wait (std::move (*optionalCountedHandler)); + heartbeatTimer_.expires_from_now(mConsensus.parms().ledgerGRANULARITY); + heartbeatTimer_.async_wait(std::move(*optionalCountedHandler)); } } -void NetworkOPsImp::setClusterTimer () +void +NetworkOPsImp::setClusterTimer() { // Only start the timer if waitHandlerCounter_ is not yet joined. - if (auto optionalCountedHandler = waitHandlerCounter_.wrap ( - [this] (boost::system::error_code const& e) - { - if ((e.value() == boost::system::errc::success) && - (! m_job_queue.isStopped())) - { - m_job_queue.addJob (jtNETOP_CLUSTER, "NetOPs.cluster", - [this] (Job&) { processClusterTimer(); }); - } - // Recover as best we can if an unexpected error occurs. - if (e.value() != boost::system::errc::success && - e.value() != boost::asio::error::operation_aborted) - { - // Try again later and hope for the best. - JLOG (m_journal.error()) - << "Cluster timer got error '" << e.message() - << "'. Restarting timer."; - setClusterTimer(); - } - })) + if (auto optionalCountedHandler = waitHandlerCounter_.wrap( + [this](boost::system::error_code const& e) { + if ((e.value() == boost::system::errc::success) && + (!m_job_queue.isStopped())) + { + m_job_queue.addJob( + jtNETOP_CLUSTER, "NetOPs.cluster", [this](Job&) { + processClusterTimer(); + }); + } + // Recover as best we can if an unexpected error occurs. + if (e.value() != boost::system::errc::success && + e.value() != boost::asio::error::operation_aborted) + { + // Try again later and hope for the best. + JLOG(m_journal.error()) + << "Cluster timer got error '" << e.message() + << "'. Restarting timer."; + setClusterTimer(); + } + })) { using namespace std::chrono_literals; - clusterTimer_.expires_from_now (10s); - clusterTimer_.async_wait (std::move (*optionalCountedHandler)); + clusterTimer_.expires_from_now(10s); + clusterTimer_.async_wait(std::move(*optionalCountedHandler)); } } -void NetworkOPsImp::processHeartbeatTimer () +void +NetworkOPsImp::processHeartbeatTimer() { { std::unique_lock lock{app_.getMasterMutex()}; // VFALCO NOTE This is for diagnosing a crash on exit - LoadManager& mgr (app_.getLoadManager ()); - mgr.resetDeadlockDetector (); + LoadManager& mgr(app_.getLoadManager()); + mgr.resetDeadlockDetector(); - std::size_t const numPeers = app_.overlay ().size (); + std::size_t const numPeers = app_.overlay().size(); // do we have sufficient peers? If not, we are disconnected. if (numPeers < minPeerCount_) { if (mMode != OperatingMode::DISCONNECTED) { - setMode (OperatingMode::DISCONNECTED); + setMode(OperatingMode::DISCONNECTED); JLOG(m_journal.warn()) << "Node count (" << numPeers << ") has fallen " << "below required minimum (" << minPeerCount_ << ")."; @@ -854,13 +1001,13 @@ void NetworkOPsImp::processHeartbeatTimer () lock.unlock(); // We do not call mConsensus.timerEntry until there are enough // peers providing meaningful inputs to consensus - setHeartbeatTimer (); + setHeartbeatTimer(); return; } if (mMode == OperatingMode::DISCONNECTED) { - setMode (OperatingMode::CONNECTED); + setMode(OperatingMode::CONNECTED); JLOG(m_journal.info()) << "Node count (" << numPeers << ") is sufficient."; } @@ -868,12 +1015,12 @@ void NetworkOPsImp::processHeartbeatTimer () // Check if the last validated ledger forces a change between these // states. if (mMode == OperatingMode::SYNCING) - setMode (OperatingMode::SYNCING); + setMode(OperatingMode::SYNCING); else if (mMode == OperatingMode::CONNECTED) - setMode (OperatingMode::CONNECTED); + setMode(OperatingMode::CONNECTED); } - mConsensus.timerEntry (app_.timeKeeper().closeTime()); + mConsensus.timerEntry(app_.timeKeeper().closeTime()); const ConsensusPhase currPhase = mConsensus.phase(); if (mLastConsensusPhase != currPhase) @@ -882,10 +1029,11 @@ void NetworkOPsImp::processHeartbeatTimer () mLastConsensusPhase = currPhase; } - setHeartbeatTimer (); + setHeartbeatTimer(); } -void NetworkOPsImp::processClusterTimer () +void +NetworkOPsImp::processClusterTimer() { using namespace std::chrono_literals; bool const update = app_.cluster().update( @@ -899,42 +1047,38 @@ void NetworkOPsImp::processClusterTimer () if (!update) { JLOG(m_journal.debug()) << "Too soon to send cluster update"; - setClusterTimer (); + setClusterTimer(); return; } protocol::TMCluster cluster; - app_.cluster().for_each( - [&cluster](ClusterNode const& node) - { - protocol::TMClusterNode& n = *cluster.add_clusternodes(); - n.set_publickey(toBase58 ( - TokenType::NodePublic, - node.identity())); - n.set_reporttime( - node.getReportTime().time_since_epoch().count()); - n.set_nodeload(node.getLoadFee()); - if (!node.name().empty()) - n.set_nodename(node.name()); - }); + app_.cluster().for_each([&cluster](ClusterNode const& node) { + protocol::TMClusterNode& n = *cluster.add_clusternodes(); + n.set_publickey(toBase58(TokenType::NodePublic, node.identity())); + n.set_reporttime(node.getReportTime().time_since_epoch().count()); + n.set_nodeload(node.getLoadFee()); + if (!node.name().empty()) + n.set_nodename(node.name()); + }); Resource::Gossip gossip = app_.getResourceManager().exportConsumers(); - for (auto& item: gossip.items) + for (auto& item : gossip.items) { protocol::TMLoadSource& node = *cluster.add_loadsources(); - node.set_name (to_string (item.address)); - node.set_cost (item.balance); + node.set_name(to_string(item.address)); + node.set_cost(item.balance); } - app_.overlay ().foreach (send_if ( + app_.overlay().foreach (send_if( std::make_shared(cluster, protocol::mtCLUSTER), - peer_in_cluster ())); - setClusterTimer (); + peer_in_cluster())); + setClusterTimer(); } //------------------------------------------------------------------------------ -std::string NetworkOPsImp::strOperatingMode (OperatingMode const mode, - bool const admin) const +std::string +NetworkOPsImp::strOperatingMode(OperatingMode const mode, bool const admin) + const { if (mode == OperatingMode::FULL && admin) { @@ -952,9 +1096,10 @@ std::string NetworkOPsImp::strOperatingMode (OperatingMode const mode, return states_[static_cast(mode)]; } -void NetworkOPsImp::submitTransaction (std::shared_ptr const& iTrans) +void +NetworkOPsImp::submitTransaction(std::shared_ptr const& iTrans) { - if (isNeedNetworkLedger ()) + if (isNeedNetworkLedger()) { // Nothing we can do if we've never been in sync return; @@ -963,7 +1108,7 @@ void NetworkOPsImp::submitTransaction (std::shared_ptr const& iTrans // this is an asynchronous interface auto const trans = sterilize(*iTrans); - auto const txid = trans->getTransactionID (); + auto const txid = trans->getTransactionID(); auto const flags = app_.getHashRouter().getFlags(txid); if ((flags & SF_BAD) != 0) @@ -975,15 +1120,15 @@ void NetworkOPsImp::submitTransaction (std::shared_ptr const& iTrans try { auto const [validity, reason] = checkValidity( - app_.getHashRouter(), *trans, - m_ledgerMaster.getValidatedRules(), - app_.config()); + app_.getHashRouter(), + *trans, + m_ledgerMaster.getValidatedRules(), + app_.config()); if (validity != Validity::Valid) { - JLOG(m_journal.warn()) << - "Submitted transaction invalid: " << - reason; + JLOG(m_journal.warn()) + << "Submitted transaction invalid: " << reason; return; } } @@ -996,28 +1141,29 @@ void NetworkOPsImp::submitTransaction (std::shared_ptr const& iTrans std::string reason; - auto tx = std::make_shared ( - trans, reason, app_); + auto tx = std::make_shared(trans, reason, app_); - m_job_queue.addJob ( - jtTRANSACTION, "submitTxn", - [this, tx] (Job&) { - auto t = tx; - processTransaction(t, false, false, FailHard::no); - }); + m_job_queue.addJob(jtTRANSACTION, "submitTxn", [this, tx](Job&) { + auto t = tx; + processTransaction(t, false, false, FailHard::no); + }); } -void NetworkOPsImp::processTransaction (std::shared_ptr& transaction, - bool bUnlimited, bool bLocal, FailHard failType) +void +NetworkOPsImp::processTransaction( + std::shared_ptr& transaction, + bool bUnlimited, + bool bLocal, + FailHard failType) { - auto ev = m_job_queue.makeLoadEvent (jtTXN_PROC, "ProcessTXN"); - auto const newFlags = app_.getHashRouter ().getFlags (transaction->getID ()); + auto ev = m_job_queue.makeLoadEvent(jtTXN_PROC, "ProcessTXN"); + auto const newFlags = app_.getHashRouter().getFlags(transaction->getID()); if ((newFlags & SF_BAD) != 0) { // cached bad - transaction->setStatus (INVALID); - transaction->setResult (temBAD_SIGNATURE); + transaction->setStatus(INVALID); + transaction->setResult(temBAD_SIGNATURE); return; } @@ -1027,63 +1173,68 @@ void NetworkOPsImp::processTransaction (std::shared_ptr& transactio auto const view = m_ledgerMaster.getCurrentLedger(); auto const [validity, reason] = checkValidity( app_.getHashRouter(), - *transaction->getSTransaction(), - view->rules(), app_.config()); + *transaction->getSTransaction(), + view->rules(), + app_.config()); assert(validity == Validity::Valid); // Not concerned with local checks at this point. if (validity == Validity::SigBad) { - JLOG(m_journal.info()) << "Transaction has bad signature: " << - reason; + JLOG(m_journal.info()) << "Transaction has bad signature: " << reason; transaction->setStatus(INVALID); transaction->setResult(temBAD_SIGNATURE); - app_.getHashRouter().setFlags(transaction->getID(), - SF_BAD); + app_.getHashRouter().setFlags(transaction->getID(), SF_BAD); return; } // canonicalize can change our pointer - app_.getMasterTransaction ().canonicalize (&transaction); + app_.getMasterTransaction().canonicalize(&transaction); if (bLocal) - doTransactionSync (transaction, bUnlimited, failType); + doTransactionSync(transaction, bUnlimited, failType); else - doTransactionAsync (transaction, bUnlimited, failType); + doTransactionAsync(transaction, bUnlimited, failType); } -void NetworkOPsImp::doTransactionAsync (std::shared_ptr transaction, - bool bUnlimited, FailHard failType) +void +NetworkOPsImp::doTransactionAsync( + std::shared_ptr transaction, + bool bUnlimited, + FailHard failType) { - std::lock_guard lock (mMutex); + std::lock_guard lock(mMutex); if (transaction->getApplying()) return; - mTransactions.push_back (TransactionStatus (transaction, bUnlimited, false, - failType)); + mTransactions.push_back( + TransactionStatus(transaction, bUnlimited, false, failType)); transaction->setApplying(); if (mDispatchState == DispatchState::none) { - if (m_job_queue.addJob ( - jtBATCH, "transactionBatch", - [this] (Job&) { transactionBatch(); })) + if (m_job_queue.addJob(jtBATCH, "transactionBatch", [this](Job&) { + transactionBatch(); + })) { mDispatchState = DispatchState::scheduled; } } } -void NetworkOPsImp::doTransactionSync (std::shared_ptr transaction, - bool bUnlimited, FailHard failType) +void +NetworkOPsImp::doTransactionSync( + std::shared_ptr transaction, + bool bUnlimited, + FailHard failType) { - std::unique_lock lock (mMutex); + std::unique_lock lock(mMutex); - if (! transaction->getApplying()) + if (!transaction->getApplying()) { - mTransactions.push_back (TransactionStatus (transaction, bUnlimited, - true, failType)); + mTransactions.push_back( + TransactionStatus(transaction, bUnlimited, true, failType)); transaction->setApplying(); } @@ -1092,48 +1243,50 @@ void NetworkOPsImp::doTransactionSync (std::shared_ptr transaction, if (mDispatchState == DispatchState::running) { // A batch processing job is already running, so wait. - mCond.wait (lock); + mCond.wait(lock); } else { - apply (lock); + apply(lock); if (mTransactions.size()) { // More transactions need to be applied, but by another job. - if (m_job_queue.addJob ( - jtBATCH, "transactionBatch", - [this] (Job&) { transactionBatch(); })) + if (m_job_queue.addJob( + jtBATCH, "transactionBatch", [this](Job&) { + transactionBatch(); + })) { mDispatchState = DispatchState::scheduled; } } } - } - while (transaction->getApplying()); + } while (transaction->getApplying()); } -void NetworkOPsImp::transactionBatch() +void +NetworkOPsImp::transactionBatch() { - std::unique_lock lock (mMutex); + std::unique_lock lock(mMutex); if (mDispatchState == DispatchState::running) return; while (mTransactions.size()) { - apply (lock); + apply(lock); } } -void NetworkOPsImp::apply (std::unique_lock& batchLock) +void +NetworkOPsImp::apply(std::unique_lock& batchLock) { std::vector submit_held; std::vector transactions; - mTransactions.swap (transactions); - assert (! transactions.empty()); + mTransactions.swap(transactions); + assert(!transactions.empty()); - assert (mDispatchState != DispatchState::running); + assert(mDispatchState != DispatchState::running); mDispatchState = DispatchState::running; batchLock.unlock(); @@ -1142,12 +1295,11 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) std::unique_lock masterLock{app_.getMasterMutex(), std::defer_lock}; bool changed = false; { - std::unique_lock ledgerLock{m_ledgerMaster.peekMutex(), std::defer_lock}; + std::unique_lock ledgerLock{ + m_ledgerMaster.peekMutex(), std::defer_lock}; std::lock(masterLock, ledgerLock); - app_.openLedger().modify( - [&](OpenView& view, beast::Journal j) - { + app_.openLedger().modify([&](OpenView& view, beast::Journal j) { for (TransactionStatus& e : transactions) { // we check before adding to the batch @@ -1159,8 +1311,7 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) flags |= tapFAIL_HARD; auto const result = app_.getTxQ().apply( - app_, view, e.transaction->getSTransaction(), - flags, j); + app_, view, e.transaction->getSTransaction(), flags, j); e.result = result.first; e.applied = result.second; changed = changed || result.second; @@ -1182,28 +1333,28 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) if (e.applied) { - pubProposedTransaction (newOL, - e.transaction->getSTransaction(), e.result); + pubProposedTransaction( + newOL, e.transaction->getSTransaction(), e.result); e.transaction->setApplied(); } - e.transaction->setResult (e.result); + e.transaction->setResult(e.result); - if (isTemMalformed (e.result)) - app_.getHashRouter().setFlags (e.transaction->getID(), SF_BAD); + if (isTemMalformed(e.result)) + app_.getHashRouter().setFlags(e.transaction->getID(), SF_BAD); - #ifdef DEBUG +#ifdef DEBUG if (e.result != tesSUCCESS) { std::string token, human; - if (transResultInfo (e.result, token, human)) + if (transResultInfo(e.result, token, human)) { - JLOG(m_journal.info()) << "TransactionResult: " - << token << ": " << human; + JLOG(m_journal.info()) + << "TransactionResult: " << token << ": " << human; } } - #endif +#endif bool addLocal = e.local; @@ -1211,18 +1362,17 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) { JLOG(m_journal.debug()) << "Transaction is now included in open ledger"; - e.transaction->setStatus (INCLUDED); + e.transaction->setStatus(INCLUDED); auto txCur = e.transaction->getSTransaction(); for (auto const& tx : m_ledgerMaster.pruneHeldTransactions( - txCur->getAccountID(sfAccount), txCur->getSequence() + 1)) + txCur->getAccountID(sfAccount), + txCur->getSequence() + 1)) { std::string reason; auto const trans = sterilize(*tx); - auto t = std::make_shared( - trans, reason, app_); - submit_held.emplace_back( - t, false, false, FailHard::no); + auto t = std::make_shared(trans, reason, app_); + submit_held.emplace_back(t, false, false, FailHard::no); t->setApplying(); } } @@ -1230,11 +1380,12 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) { // duplicate or conflict JLOG(m_journal.info()) << "Transaction is obsolete"; - e.transaction->setStatus (OBSOLETE); + e.transaction->setStatus(OBSOLETE); } else if (e.result == terQUEUED) { - JLOG(m_journal.debug()) << "Transaction is likely to claim a" + JLOG(m_journal.debug()) + << "Transaction is likely to claim a" << " fee, but is queued until fee drops"; e.transaction->setStatus(HELD); @@ -1245,15 +1396,15 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) e.transaction->setQueued(); e.transaction->setKept(); } - else if (isTerRetry (e.result)) + else if (isTerRetry(e.result)) { if (e.failType != FailHard::yes) { // transaction should be held JLOG(m_journal.debug()) << "Transaction should be held: " << e.result; - e.transaction->setStatus (HELD); - m_ledgerMaster.addHeldTransaction (e.transaction); + e.transaction->setStatus(HELD); + m_ledgerMaster.addHeldTransaction(e.transaction); e.transaction->setKept(); } } @@ -1261,40 +1412,43 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) { JLOG(m_journal.debug()) << "Status other than success " << e.result; - e.transaction->setStatus (INVALID); + e.transaction->setStatus(INVALID); } - auto const enforceFailHard = e.failType == FailHard::yes && - !isTesSuccess(e.result); + auto const enforceFailHard = + e.failType == FailHard::yes && !isTesSuccess(e.result); if (addLocal && !enforceFailHard) { - m_localTX->push_back ( + m_localTX->push_back( m_ledgerMaster.getCurrentLedgerIndex(), e.transaction->getSTransaction()); e.transaction->setKept(); } - if ((e.applied || ((mMode != OperatingMode::FULL) && - (e.failType != FailHard::yes) && e.local) || - (e.result == terQUEUED)) && !enforceFailHard) + if ((e.applied || + ((mMode != OperatingMode::FULL) && + (e.failType != FailHard::yes) && e.local) || + (e.result == terQUEUED)) && + !enforceFailHard) { - auto const toSkip = app_.getHashRouter().shouldRelay( - e.transaction->getID()); + auto const toSkip = + app_.getHashRouter().shouldRelay(e.transaction->getID()); if (toSkip) { protocol::TMTransaction tx; Serializer s; - e.transaction->getSTransaction()->add (s); - tx.set_rawtransaction (s.data(), s.size()); - tx.set_status (protocol::tsCURRENT); - tx.set_receivetimestamp (app_.timeKeeper().now().time_since_epoch().count()); + e.transaction->getSTransaction()->add(s); + tx.set_rawtransaction(s.data(), s.size()); + tx.set_status(protocol::tsCURRENT); + tx.set_receivetimestamp( + app_.timeKeeper().now().time_since_epoch().count()); tx.set_deferred(e.result == terQUEUED); // FIXME: This should be when we received it - app_.overlay().foreach (send_if_not ( - std::make_shared (tx, protocol::mtTRANSACTION), + app_.overlay().foreach (send_if_not( + std::make_shared(tx, protocol::mtTRANSACTION), peer_in_set(*toSkip))); e.transaction->setBroadcast(); } @@ -1316,7 +1470,7 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) for (TransactionStatus& e : transactions) e.transaction->clearApplying(); - if (! submit_held.empty()) + if (!submit_held.empty()) { if (mTransactions.empty()) mTransactions.swap(submit_held); @@ -1334,61 +1488,63 @@ void NetworkOPsImp::apply (std::unique_lock& batchLock) // Owner functions // -Json::Value NetworkOPsImp::getOwnerInfo ( - std::shared_ptr lpLedger, AccountID const& account) +Json::Value +NetworkOPsImp::getOwnerInfo( + std::shared_ptr lpLedger, + AccountID const& account) { - Json::Value jvObjects (Json::objectValue); - auto uRootIndex = getOwnerDirIndex (account); - auto sleNode = lpLedger->read (keylet::page (uRootIndex)); + Json::Value jvObjects(Json::objectValue); + auto uRootIndex = getOwnerDirIndex(account); + auto sleNode = lpLedger->read(keylet::page(uRootIndex)); if (sleNode) { - std::uint64_t uNodeDir; + std::uint64_t uNodeDir; do { - for (auto const& uDirEntry : sleNode->getFieldV256 (sfIndexes)) + for (auto const& uDirEntry : sleNode->getFieldV256(sfIndexes)) { - auto sleCur = lpLedger->read (keylet::child (uDirEntry)); - assert (sleCur); + auto sleCur = lpLedger->read(keylet::child(uDirEntry)); + assert(sleCur); - switch (sleCur->getType ()) + switch (sleCur->getType()) { - case ltOFFER: - if (!jvObjects.isMember (jss::offers)) - jvObjects[jss::offers] = Json::Value (Json::arrayValue); + case ltOFFER: + if (!jvObjects.isMember(jss::offers)) + jvObjects[jss::offers] = + Json::Value(Json::arrayValue); - jvObjects[jss::offers].append ( - sleCur->getJson (JsonOptions::none)); - break; + jvObjects[jss::offers].append( + sleCur->getJson(JsonOptions::none)); + break; - case ltRIPPLE_STATE: - if (!jvObjects.isMember (jss::ripple_lines)) - { - jvObjects[jss::ripple_lines] = - Json::Value (Json::arrayValue); - } + case ltRIPPLE_STATE: + if (!jvObjects.isMember(jss::ripple_lines)) + { + jvObjects[jss::ripple_lines] = + Json::Value(Json::arrayValue); + } - jvObjects[jss::ripple_lines].append ( - sleCur->getJson (JsonOptions::none)); - break; + jvObjects[jss::ripple_lines].append( + sleCur->getJson(JsonOptions::none)); + break; - case ltACCOUNT_ROOT: - case ltDIR_NODE: - default: - assert (false); - break; + case ltACCOUNT_ROOT: + case ltDIR_NODE: + default: + assert(false); + break; } } - uNodeDir = sleNode->getFieldU64 (sfIndexNext); + uNodeDir = sleNode->getFieldU64(sfIndexNext); if (uNodeDir) { - sleNode = lpLedger->read (keylet::page (uRootIndex, uNodeDir)); - assert (sleNode); + sleNode = lpLedger->read(keylet::page(uRootIndex, uNodeDir)); + assert(sleNode); } - } - while (uNodeDir); + } while (uNodeDir); } return jvObjects; @@ -1398,14 +1554,17 @@ Json::Value NetworkOPsImp::getOwnerInfo ( // Other // -void NetworkOPsImp::setAmendmentBlocked () +void +NetworkOPsImp::setAmendmentBlocked() { amendmentBlocked_ = true; - setMode (OperatingMode::TRACKING); + setMode(OperatingMode::TRACKING); } -bool NetworkOPsImp::checkLastClosedLedger ( - const Overlay::PeerSequence& peerList, uint256& networkClosed) +bool +NetworkOPsImp::checkLastClosedLedger( + const Overlay::PeerSequence& peerList, + uint256& networkClosed) { // Returns true if there's an *abnormal* ledger issue, normal changing in // TRACKING mode should return false. Do we have sufficient validations for @@ -1414,7 +1573,7 @@ bool NetworkOPsImp::checkLastClosedLedger ( JLOG(m_journal.trace()) << "NetworkOPsImp::checkLastClosedLedger"; - auto const ourClosed = m_ledgerMaster.getClosedLedger (); + auto const ourClosed = m_ledgerMaster.getClosedLedger(); if (!ourClosed) return false; @@ -1427,7 +1586,7 @@ bool NetworkOPsImp::checkLastClosedLedger ( //------------------------------------------------------------------------- // Determine preferred last closed ledger - auto & validations = app_.getValidations(); + auto& validations = app_.getValidations(); JLOG(m_journal.debug()) << "ValidationTrie " << Json::Compact(validations.getJsonTrie()); @@ -1445,7 +1604,7 @@ bool NetworkOPsImp::checkLastClosedLedger ( ++peerCounts[peerLedger]; } - for(auto const & it: peerCounts) + for (auto const& it : peerCounts) JLOG(m_journal.debug()) << "L: " << it.first << " n=" << it.second; uint256 preferredLCL = validations.getPreferredLCL( @@ -1454,7 +1613,7 @@ bool NetworkOPsImp::checkLastClosedLedger ( peerCounts); bool switchLedgers = preferredLCL != closedLedger; - if(switchLedgers) + if (switchLedgers) closedLedger = preferredLCL; //------------------------------------------------------------------------- if (switchLedgers && (closedLedger == prevClosedLedger)) @@ -1477,9 +1636,9 @@ bool NetworkOPsImp::checkLastClosedLedger ( closedLedger, 0, InboundLedger::Reason::CONSENSUS); if (consensus && - (!m_ledgerMaster.canBeCurrent (consensus) || - !m_ledgerMaster.isCompatible( - *consensus, m_journal.debug(), "Not switching"))) + (!m_ledgerMaster.canBeCurrent(consensus) || + !m_ledgerMaster.isCompatible( + *consensus, m_journal.debug(), "Not switching"))) { // Don't switch to a ledger not on the validated chain // or with an invalid close time or sequence @@ -1491,8 +1650,7 @@ bool NetworkOPsImp::checkLastClosedLedger ( JLOG(m_journal.info()) << "Our LCL: " << getJson(*ourClosed); JLOG(m_journal.info()) << "Net LCL " << closedLedger; - if ((mMode == OperatingMode::TRACKING) - || (mMode == OperatingMode::FULL)) + if ((mMode == OperatingMode::TRACKING) || (mMode == OperatingMode::FULL)) { setMode(OperatingMode::CONNECTED); } @@ -1508,14 +1666,15 @@ bool NetworkOPsImp::checkLastClosedLedger ( return true; } -void NetworkOPsImp::switchLastClosedLedger ( +void +NetworkOPsImp::switchLastClosedLedger( std::shared_ptr const& newLCL) { // set the newLCL as our last closed ledger -- this is abnormal code - JLOG(m_journal.error()) << - "JUMP last closed ledger to " << newLCL->info().hash; + JLOG(m_journal.error()) + << "JUMP last closed ledger to " << newLCL->info().hash; - clearNeedNetworkLedger (); + clearNeedNetworkLedger(); // Update fee computations. app_.getTxQ().processClosedLedger(app_, *newLCL, true); @@ -1526,68 +1685,69 @@ void NetworkOPsImp::switchLastClosedLedger ( // open ledger. Then apply local tx. auto retries = m_localTX->getTxSet(); - auto const lastVal = - app_.getLedgerMaster().getValidatedLedger(); + auto const lastVal = app_.getLedgerMaster().getValidatedLedger(); boost::optional rules; if (lastVal) rules.emplace(*lastVal, app_.config().features); else rules.emplace(app_.config().features); - app_.openLedger().accept(app_, *rules, - newLCL, OrderedTxs({}), false, retries, - tapNONE, "jump", - [&](OpenView& view, beast::Journal j) - { - // Stuff the ledger with transactions from the queue. - return app_.getTxQ().accept(app_, view); - }); + app_.openLedger().accept( + app_, + *rules, + newLCL, + OrderedTxs({}), + false, + retries, + tapNONE, + "jump", + [&](OpenView& view, beast::Journal j) { + // Stuff the ledger with transactions from the queue. + return app_.getTxQ().accept(app_, view); + }); } - m_ledgerMaster.switchLCL (newLCL); + m_ledgerMaster.switchLCL(newLCL); protocol::TMStatusChange s; - s.set_newevent (protocol::neSWITCHED_LEDGER); - s.set_ledgerseq (newLCL->info().seq); - s.set_networktime (app_.timeKeeper().now().time_since_epoch().count()); - s.set_ledgerhashprevious ( - newLCL->info().parentHash.begin (), - newLCL->info().parentHash.size ()); - s.set_ledgerhash ( - newLCL->info().hash.begin (), - newLCL->info().hash.size ()); + s.set_newevent(protocol::neSWITCHED_LEDGER); + s.set_ledgerseq(newLCL->info().seq); + s.set_networktime(app_.timeKeeper().now().time_since_epoch().count()); + s.set_ledgerhashprevious( + newLCL->info().parentHash.begin(), newLCL->info().parentHash.size()); + s.set_ledgerhash(newLCL->info().hash.begin(), newLCL->info().hash.size()); - app_.overlay ().foreach (send_always ( - std::make_shared (s, protocol::mtSTATUS_CHANGE))); + app_.overlay().foreach ( + send_always(std::make_shared(s, protocol::mtSTATUS_CHANGE))); } -bool NetworkOPsImp::beginConsensus (uint256 const& networkClosed) +bool +NetworkOPsImp::beginConsensus(uint256 const& networkClosed) { - assert (networkClosed.isNonZero ()); + assert(networkClosed.isNonZero()); auto closingInfo = m_ledgerMaster.getCurrentLedger()->info(); - JLOG(m_journal.info()) << - "Consensus time for #" << closingInfo.seq << - " with LCL " << closingInfo.parentHash; + JLOG(m_journal.info()) << "Consensus time for #" << closingInfo.seq + << " with LCL " << closingInfo.parentHash; - auto prevLedger = m_ledgerMaster.getLedgerByHash( - closingInfo.parentHash); + auto prevLedger = m_ledgerMaster.getLedgerByHash(closingInfo.parentHash); - if(! prevLedger) + if (!prevLedger) { // this shouldn't happen unless we jump ledgers if (mMode == OperatingMode::FULL) { JLOG(m_journal.warn()) << "Don't have LCL, going to tracking"; - setMode (OperatingMode::TRACKING); + setMode(OperatingMode::TRACKING); } return false; } - assert (prevLedger->info().hash == closingInfo.parentHash); - assert (closingInfo.parentHash == - m_ledgerMaster.getClosedLedger()->info().hash); + assert(prevLedger->info().hash == closingInfo.parentHash); + assert( + closingInfo.parentHash == + m_ledgerMaster.getClosedLedger()->info().hash); TrustChanges const changes = app_.validators().updateTrusted( app_.getValidations().getCurrentNodeIDs()); @@ -1612,17 +1772,18 @@ bool NetworkOPsImp::beginConsensus (uint256 const& networkClosed) return true; } -uint256 NetworkOPsImp::getConsensusLCL () +uint256 +NetworkOPsImp::getConsensusLCL() { - return mConsensus.prevLedgerID (); + return mConsensus.prevLedgerID(); } -void NetworkOPsImp::processTrustedProposal ( +void +NetworkOPsImp::processTrustedProposal( RCLCxPeerPos peerPos, std::shared_ptr set) { - if (mConsensus.peerProposal( - app_.timeKeeper().closeTime(), peerPos)) + if (mConsensus.peerProposal(app_.timeKeeper().closeTime(), peerPos)) { app_.overlay().relay(*set, peerPos.suppressionID()); } @@ -1631,8 +1792,7 @@ void NetworkOPsImp::processTrustedProposal ( } void -NetworkOPsImp::mapComplete ( - std::shared_ptr const& map, bool fromAcquire) +NetworkOPsImp::mapComplete(std::shared_ptr const& map, bool fromAcquire) { // We now have an additional transaction set // either created locally during the consensus process @@ -1640,37 +1800,35 @@ NetworkOPsImp::mapComplete ( // Inform peers we have this set protocol::TMHaveTransactionSet msg; - msg.set_hash (map->getHash().as_uint256().begin(), 256 / 8); - msg.set_status (protocol::tsHAVE); - app_.overlay().foreach (send_always ( - std::make_shared ( - msg, protocol::mtHAVE_SET))); + msg.set_hash(map->getHash().as_uint256().begin(), 256 / 8); + msg.set_status(protocol::tsHAVE); + app_.overlay().foreach ( + send_always(std::make_shared(msg, protocol::mtHAVE_SET))); // We acquired it because consensus asked us to if (fromAcquire) - mConsensus.gotTxSet ( - app_.timeKeeper().closeTime(), - RCLTxSet{map}); + mConsensus.gotTxSet(app_.timeKeeper().closeTime(), RCLTxSet{map}); } -void NetworkOPsImp::endConsensus () +void +NetworkOPsImp::endConsensus() { - uint256 deadLedger = m_ledgerMaster.getClosedLedger ()->info().parentHash; + uint256 deadLedger = m_ledgerMaster.getClosedLedger()->info().parentHash; - for (auto const& it : app_.overlay ().getActivePeers ()) + for (auto const& it : app_.overlay().getActivePeers()) { - if (it && (it->getClosedLedgerHash () == deadLedger)) + if (it && (it->getClosedLedgerHash() == deadLedger)) { JLOG(m_journal.trace()) << "Killing obsolete peer status"; - it->cycleStatus (); + it->cycleStatus(); } } uint256 networkClosed; - bool ledgerChange = checkLastClosedLedger ( - app_.overlay ().getActivePeers (), networkClosed); + bool ledgerChange = + checkLastClosedLedger(app_.overlay().getActivePeers(), networkClosed); - if (networkClosed.isZero ()) + if (networkClosed.isZero()) return; // WRITEME: Unless we are in FULL and in the process of doing a consensus, @@ -1679,318 +1837,324 @@ void NetworkOPsImp::endConsensus () // timing to make sure there shouldn't be a newer LCL. We need this // information to do the next three tests. - if (((mMode == OperatingMode::CONNECTED) - || (mMode == OperatingMode::SYNCING)) && !ledgerChange) + if (((mMode == OperatingMode::CONNECTED) || + (mMode == OperatingMode::SYNCING)) && + !ledgerChange) { // Count number of peers that agree with us and UNL nodes whose // validations we have for LCL. If the ledger is good enough, go to // TRACKING - TODO if (!needNetworkLedger_) - setMode (OperatingMode::TRACKING); + setMode(OperatingMode::TRACKING); } - if (((mMode == OperatingMode::CONNECTED) - || (mMode == OperatingMode::TRACKING)) && !ledgerChange) + if (((mMode == OperatingMode::CONNECTED) || + (mMode == OperatingMode::TRACKING)) && + !ledgerChange) { // check if the ledger is good enough to go to FULL // Note: Do not go to FULL if we don't have the previous ledger // check if the ledger is bad enough to go to CONNECTE D -- TODO auto current = m_ledgerMaster.getCurrentLedger(); - if (app_.timeKeeper().now() < - (current->info().parentCloseTime + 2* current->info().closeTimeResolution)) + if (app_.timeKeeper().now() < (current->info().parentCloseTime + + 2 * current->info().closeTimeResolution)) { - setMode (OperatingMode::FULL); + setMode(OperatingMode::FULL); } } - beginConsensus (networkClosed); + beginConsensus(networkClosed); } -void NetworkOPsImp::consensusViewChange () +void +NetworkOPsImp::consensusViewChange() { - if ((mMode == OperatingMode::FULL) - || (mMode == OperatingMode::TRACKING)) + if ((mMode == OperatingMode::FULL) || (mMode == OperatingMode::TRACKING)) { - setMode (OperatingMode::CONNECTED); + setMode(OperatingMode::CONNECTED); } } -void NetworkOPsImp::pubManifest (Manifest const& mo) +void +NetworkOPsImp::pubManifest(Manifest const& mo) { // VFALCO consider std::shared_mutex - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!mStreamMaps[sManifests].empty ()) + if (!mStreamMaps[sManifests].empty()) { - Json::Value jvObj (Json::objectValue); + Json::Value jvObj(Json::objectValue); - jvObj [jss::type] = "manifestReceived"; - jvObj [jss::master_key] = toBase58( - TokenType::NodePublic, mo.masterKey); + jvObj[jss::type] = "manifestReceived"; + jvObj[jss::master_key] = toBase58(TokenType::NodePublic, mo.masterKey); if (!mo.signingKey.empty()) jvObj[jss::signing_key] = toBase58(TokenType::NodePublic, mo.signingKey); - jvObj [jss::seq] = Json::UInt (mo.sequence); + jvObj[jss::seq] = Json::UInt(mo.sequence); if (auto sig = mo.getSignature()) - jvObj [jss::signature] = strHex (*sig); - jvObj [jss::master_signature] = strHex (mo.getMasterSignature ()); + jvObj[jss::signature] = strHex(*sig); + jvObj[jss::master_signature] = strHex(mo.getMasterSignature()); - for (auto i = mStreamMaps[sManifests].begin (); - i != mStreamMaps[sManifests].end (); ) + for (auto i = mStreamMaps[sManifests].begin(); + i != mStreamMaps[sManifests].end();) { if (auto p = i->second.lock()) { - p->send (jvObj, true); + p->send(jvObj, true); ++i; } else { - i = mStreamMaps[sManifests].erase (i); + i = mStreamMaps[sManifests].erase(i); } } } } NetworkOPsImp::ServerFeeSummary::ServerFeeSummary( - XRPAmount fee, - TxQ::Metrics&& escalationMetrics, - LoadFeeTrack const & loadFeeTrack) + XRPAmount fee, + TxQ::Metrics&& escalationMetrics, + LoadFeeTrack const& loadFeeTrack) : loadFactorServer{loadFeeTrack.getLoadFactor()} , loadBaseServer{loadFeeTrack.getLoadBase()} , baseFee{fee} , em{std::move(escalationMetrics)} { - } - bool -NetworkOPsImp::ServerFeeSummary::operator !=(NetworkOPsImp::ServerFeeSummary const & b) const +NetworkOPsImp::ServerFeeSummary::operator!=( + NetworkOPsImp::ServerFeeSummary const& b) const { - if(loadFactorServer != b.loadFactorServer || - loadBaseServer != b.loadBaseServer || - baseFee != b.baseFee || - em.is_initialized() != b.em.is_initialized()) - return true; + if (loadFactorServer != b.loadFactorServer || + loadBaseServer != b.loadBaseServer || baseFee != b.baseFee || + em.is_initialized() != b.em.is_initialized()) + return true; - if(em && b.em) + if (em && b.em) { - return (em->minProcessingFeeLevel != b.em->minProcessingFeeLevel || - em->openLedgerFeeLevel != b.em->openLedgerFeeLevel || - em->referenceFeeLevel != b.em->referenceFeeLevel); + return ( + em->minProcessingFeeLevel != b.em->minProcessingFeeLevel || + em->openLedgerFeeLevel != b.em->openLedgerFeeLevel || + em->referenceFeeLevel != b.em->referenceFeeLevel); } return false; } // Need to cap to uint64 to uint32 due to JSON limitations -static std::uint32_t trunc32(std::uint64_t v) +static std::uint32_t +trunc32(std::uint64_t v) { - constexpr std::uint64_t max32 = - std::numeric_limits::max(); + constexpr std::uint64_t max32 = std::numeric_limits::max(); return std::min(max32, v); }; -void NetworkOPsImp::pubServer () +void +NetworkOPsImp::pubServer() { // VFALCO TODO Don't hold the lock across calls to send...make a copy of the - // list into a local array while holding the lock then release the - // lock and call send on everyone. + // list into a local array while holding the lock then release + // the lock and call send on everyone. // - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!mStreamMaps[sServer].empty ()) + if (!mStreamMaps[sServer].empty()) { - Json::Value jvObj (Json::objectValue); + Json::Value jvObj(Json::objectValue); - ServerFeeSummary f{app_.openLedger().current()->fees().base, + ServerFeeSummary f{ + app_.openLedger().current()->fees().base, app_.getTxQ().getMetrics(*app_.openLedger().current()), app_.getFeeTrack()}; - jvObj [jss::type] = "serverStatus"; - jvObj [jss::server_status] = strOperatingMode (); - jvObj [jss::load_base] = f.loadBaseServer; - jvObj [jss::load_factor_server] = f.loadFactorServer; - jvObj [jss::base_fee] = f.baseFee.jsonClipped(); + jvObj[jss::type] = "serverStatus"; + jvObj[jss::server_status] = strOperatingMode(); + jvObj[jss::load_base] = f.loadBaseServer; + jvObj[jss::load_factor_server] = f.loadFactorServer; + jvObj[jss::base_fee] = f.baseFee.jsonClipped(); - if(f.em) + if (f.em) { - auto const loadFactor = - std::max(safe_cast(f.loadFactorServer), - mulDiv(f.em->openLedgerFeeLevel, f.loadBaseServer, - f.em->referenceFeeLevel).second); + auto const loadFactor = std::max( + safe_cast(f.loadFactorServer), + mulDiv( + f.em->openLedgerFeeLevel, + f.loadBaseServer, + f.em->referenceFeeLevel) + .second); - jvObj [jss::load_factor] = trunc32(loadFactor); - jvObj [jss::load_factor_fee_escalation] = + jvObj[jss::load_factor] = trunc32(loadFactor); + jvObj[jss::load_factor_fee_escalation] = f.em->openLedgerFeeLevel.jsonClipped(); - jvObj [jss::load_factor_fee_queue] = + jvObj[jss::load_factor_fee_queue] = f.em->minProcessingFeeLevel.jsonClipped(); - jvObj [jss::load_factor_fee_reference] = + jvObj[jss::load_factor_fee_reference] = f.em->referenceFeeLevel.jsonClipped(); - } else - jvObj [jss::load_factor] = f.loadFactorServer; + jvObj[jss::load_factor] = f.loadFactorServer; mLastFeeSummary = f; - for (auto i = mStreamMaps[sServer].begin (); - i != mStreamMaps[sServer].end (); ) + for (auto i = mStreamMaps[sServer].begin(); + i != mStreamMaps[sServer].end();) { - InfoSub::pointer p = i->second.lock (); + InfoSub::pointer p = i->second.lock(); // VFALCO TODO research the possibility of using thread queues and // linearizing the deletion of subscribers with the // sending of JSON data. if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++i; } else { - i = mStreamMaps[sServer].erase (i); + i = mStreamMaps[sServer].erase(i); } } } } -void NetworkOPsImp::pubConsensus (ConsensusPhase phase) +void +NetworkOPsImp::pubConsensus(ConsensusPhase phase) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); auto& streamMap = mStreamMaps[sConsensusPhase]; - if (!streamMap.empty ()) + if (!streamMap.empty()) { - Json::Value jvObj (Json::objectValue); - jvObj [jss::type] = "consensusPhase"; - jvObj [jss::consensus] = to_string(phase); + Json::Value jvObj(Json::objectValue); + jvObj[jss::type] = "consensusPhase"; + jvObj[jss::consensus] = to_string(phase); - for (auto i = streamMap.begin (); - i != streamMap.end (); ) + for (auto i = streamMap.begin(); i != streamMap.end();) { if (auto p = i->second.lock()) { - p->send (jvObj, true); + p->send(jvObj, true); ++i; } else { - i = streamMap.erase (i); + i = streamMap.erase(i); } } } } - -void NetworkOPsImp::pubValidation (STValidation::ref val) +void +NetworkOPsImp::pubValidation(STValidation::ref val) { // VFALCO consider std::shared_mutex - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!mStreamMaps[sValidations].empty ()) + if (!mStreamMaps[sValidations].empty()) { - Json::Value jvObj (Json::objectValue); + Json::Value jvObj(Json::objectValue); auto const signerPublic = val->getSignerPublic(); - jvObj [jss::type] = "validationReceived"; - jvObj [jss::validation_public_key] = toBase58( - TokenType::NodePublic, - signerPublic); - jvObj [jss::ledger_hash] = to_string (val->getLedgerHash ()); - jvObj [jss::signature] = strHex (val->getSignature ()); - jvObj [jss::full] = val->isFull(); - jvObj [jss::flags] = val->getFlags(); - jvObj [jss::signing_time] = *(*val)[~sfSigningTime]; + jvObj[jss::type] = "validationReceived"; + jvObj[jss::validation_public_key] = + toBase58(TokenType::NodePublic, signerPublic); + jvObj[jss::ledger_hash] = to_string(val->getLedgerHash()); + jvObj[jss::signature] = strHex(val->getSignature()); + jvObj[jss::full] = val->isFull(); + jvObj[jss::flags] = val->getFlags(); + jvObj[jss::signing_time] = *(*val)[~sfSigningTime]; - auto const masterKey = app_.validatorManifests().getMasterKey(signerPublic); + auto const masterKey = + app_.validatorManifests().getMasterKey(signerPublic); - if(masterKey != signerPublic) - jvObj [jss::master_key] = toBase58(TokenType::NodePublic, masterKey); + if (masterKey != signerPublic) + jvObj[jss::master_key] = toBase58(TokenType::NodePublic, masterKey); if (auto const seq = (*val)[~sfLedgerSequence]) - jvObj [jss::ledger_index] = to_string (*seq); + jvObj[jss::ledger_index] = to_string(*seq); - if (val->isFieldPresent (sfAmendments)) + if (val->isFieldPresent(sfAmendments)) { - jvObj[jss::amendments] = Json::Value (Json::arrayValue); + jvObj[jss::amendments] = Json::Value(Json::arrayValue); for (auto const& amendment : val->getFieldV256(sfAmendments)) - jvObj [jss::amendments].append (to_string (amendment)); + jvObj[jss::amendments].append(to_string(amendment)); } if (auto const closeTime = (*val)[~sfCloseTime]) - jvObj [jss::close_time] = *closeTime; + jvObj[jss::close_time] = *closeTime; if (auto const loadFee = (*val)[~sfLoadFee]) - jvObj [jss::load_fee] = *loadFee; + jvObj[jss::load_fee] = *loadFee; if (auto const baseFee = (*val)[~sfBaseFee]) - jvObj [jss::base_fee] = static_cast (*baseFee); + jvObj[jss::base_fee] = static_cast(*baseFee); if (auto const reserveBase = (*val)[~sfReserveBase]) - jvObj [jss::reserve_base] = *reserveBase; + jvObj[jss::reserve_base] = *reserveBase; if (auto const reserveInc = (*val)[~sfReserveIncrement]) - jvObj [jss::reserve_inc] = *reserveInc; + jvObj[jss::reserve_inc] = *reserveInc; - for (auto i = mStreamMaps[sValidations].begin (); - i != mStreamMaps[sValidations].end (); ) + for (auto i = mStreamMaps[sValidations].begin(); + i != mStreamMaps[sValidations].end();) { if (auto p = i->second.lock()) { - p->send (jvObj, true); + p->send(jvObj, true); ++i; } else { - i = mStreamMaps[sValidations].erase (i); + i = mStreamMaps[sValidations].erase(i); } } } } -void NetworkOPsImp::pubPeerStatus ( - std::function const& func) +void +NetworkOPsImp::pubPeerStatus(std::function const& func) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!mStreamMaps[sPeerStatus].empty ()) + if (!mStreamMaps[sPeerStatus].empty()) { - Json::Value jvObj (func()); + Json::Value jvObj(func()); - jvObj [jss::type] = "peerStatusChange"; + jvObj[jss::type] = "peerStatusChange"; - for (auto i = mStreamMaps[sPeerStatus].begin (); - i != mStreamMaps[sPeerStatus].end (); ) + for (auto i = mStreamMaps[sPeerStatus].begin(); + i != mStreamMaps[sPeerStatus].end();) { - InfoSub::pointer p = i->second.lock (); + InfoSub::pointer p = i->second.lock(); if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++i; } else { - i = mStreamMaps[sPeerStatus].erase (i); + i = mStreamMaps[sPeerStatus].erase(i); } } } } -void NetworkOPsImp::setMode (OperatingMode om) +void +NetworkOPsImp::setMode(OperatingMode om) { using namespace std::chrono_literals; if (om == OperatingMode::CONNECTED) { - if (app_.getLedgerMaster ().getValidatedLedgerAge () < 1min) + if (app_.getLedgerMaster().getValidatedLedgerAge() < 1min) om = OperatingMode::SYNCING; } else if (om == OperatingMode::SYNCING) { - if (app_.getLedgerMaster ().getValidatedLedgerAge () >= 1min) + if (app_.getLedgerMaster().getValidatedLedgerAge() >= 1min) om = OperatingMode::CONNECTED; } @@ -2002,19 +2166,24 @@ void NetworkOPsImp::setMode (OperatingMode om) mMode = om; - accounting_.mode (om); + accounting_.mode(om); - JLOG(m_journal.info()) << "STATE->" << strOperatingMode (); - pubServer (); + JLOG(m_journal.info()) << "STATE->" << strOperatingMode(); + pubServer(); } - std::string -NetworkOPsImp::transactionsSQL ( - std::string selection, AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, - bool binary, bool count, bool bUnlimited) +NetworkOPsImp::transactionsSQL( + std::string selection, + AccountID const& account, + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool binary, + bool count, + bool bUnlimited) { std::uint32_t NONBINARY_PAGE_LENGTH = 200; std::uint32_t BINARY_PAGE_LENGTH = 500; @@ -2031,9 +2200,9 @@ NetworkOPsImp::transactionsSQL ( } else if (!bUnlimited) { - numberOfResults = std::min ( + numberOfResults = std::min( binary ? BINARY_PAGE_LENGTH : NONBINARY_PAGE_LENGTH, - static_cast (limit)); + static_cast(limit)); } else { @@ -2045,107 +2214,110 @@ NetworkOPsImp::transactionsSQL ( if (maxLedger != -1) { - maxClause = boost::str (boost::format ( - "AND AccountTransactions.LedgerSeq <= '%u'") % maxLedger); + maxClause = boost::str( + boost::format("AND AccountTransactions.LedgerSeq <= '%u'") % + maxLedger); } if (minLedger != -1) { - minClause = boost::str (boost::format ( - "AND AccountTransactions.LedgerSeq >= '%u'") % minLedger); + minClause = boost::str( + boost::format("AND AccountTransactions.LedgerSeq >= '%u'") % + minLedger); } std::string sql; if (count) - sql = - boost::str (boost::format ( - "SELECT %s FROM AccountTransactions " - "WHERE Account = '%s' %s %s LIMIT %u, %u;") - % selection - % app_.accountIDCache().toBase58(account) - % maxClause - % minClause - % beast::lexicalCastThrow (offset) - % beast::lexicalCastThrow (numberOfResults) - ); + sql = boost::str( + boost::format("SELECT %s FROM AccountTransactions " + "WHERE Account = '%s' %s %s LIMIT %u, %u;") % + selection % app_.accountIDCache().toBase58(account) % maxClause % + minClause % beast::lexicalCastThrow(offset) % + beast::lexicalCastThrow(numberOfResults)); else - sql = - boost::str (boost::format ( + sql = boost::str( + boost::format( "SELECT %s FROM " "AccountTransactions INNER JOIN Transactions " "ON Transactions.TransID = AccountTransactions.TransID " "WHERE Account = '%s' %s %s " "ORDER BY AccountTransactions.LedgerSeq %s, " "AccountTransactions.TxnSeq %s, AccountTransactions.TransID %s " - "LIMIT %u, %u;") - % selection - % app_.accountIDCache().toBase58(account) - % maxClause - % minClause - % (descending ? "DESC" : "ASC") - % (descending ? "DESC" : "ASC") - % (descending ? "DESC" : "ASC") - % beast::lexicalCastThrow (offset) - % beast::lexicalCastThrow (numberOfResults) - ); + "LIMIT %u, %u;") % + selection % app_.accountIDCache().toBase58(account) % maxClause % + minClause % (descending ? "DESC" : "ASC") % + (descending ? "DESC" : "ASC") % (descending ? "DESC" : "ASC") % + beast::lexicalCastThrow(offset) % + beast::lexicalCastThrow(numberOfResults)); JLOG(m_journal.trace()) << "txSQL query: " << sql; return sql; } -NetworkOPs::AccountTxs NetworkOPsImp::getAccountTxs ( +NetworkOPs::AccountTxs +NetworkOPsImp::getAccountTxs( AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, bool bUnlimited) + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) { // can be called with no locks AccountTxs ret; - std::string sql = transactionsSQL ( - "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta", account, - minLedger, maxLedger, descending, offset, limit, false, false, + std::string sql = transactionsSQL( + "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta", + account, + minLedger, + maxLedger, + descending, + offset, + limit, + false, + false, bUnlimited); { - auto db = app_.getTxnDB ().checkoutDb (); + auto db = app_.getTxnDB().checkoutDb(); boost::optional ledgerSeq; boost::optional status; - soci::blob sociTxnBlob (*db), sociTxnMetaBlob (*db); + soci::blob sociTxnBlob(*db), sociTxnMetaBlob(*db); soci::indicator rti, tmi; Blob rawTxn, txnMeta; soci::statement st = - (db->prepare << sql, - soci::into(ledgerSeq), - soci::into(status), - soci::into(sociTxnBlob, rti), - soci::into(sociTxnMetaBlob, tmi)); + (db->prepare << sql, + soci::into(ledgerSeq), + soci::into(status), + soci::into(sociTxnBlob, rti), + soci::into(sociTxnMetaBlob, tmi)); - st.execute (); - while (st.fetch ()) + st.execute(); + while (st.fetch()) { if (soci::i_ok == rti) convert(sociTxnBlob, rawTxn); else - rawTxn.clear (); + rawTxn.clear(); if (soci::i_ok == tmi) - convert (sociTxnMetaBlob, txnMeta); + convert(sociTxnMetaBlob, txnMeta); else - txnMeta.clear (); + txnMeta.clear(); - auto txn = Transaction::transactionFromSQL ( + auto txn = Transaction::transactionFromSQL( ledgerSeq, status, rawTxn, app_); - if (txnMeta.empty ()) - { // Work around a bug that could leave the metadata missing - auto const seq = rangeCheckedCast( - ledgerSeq.value_or (0)); + if (txnMeta.empty()) + { // Work around a bug that could leave the metadata missing + auto const seq = + rangeCheckedCast(ledgerSeq.value_or(0)); - JLOG(m_journal.warn()) << - "Recovering ledger " << seq << - ", txn " << txn->getID(); + JLOG(m_journal.warn()) + << "Recovering ledger " << seq << ", txn " << txn->getID(); if (auto l = m_ledgerMaster.getLedgerBySeq(seq)) pendSaveValidated(app_, l, false, false); @@ -2162,49 +2334,60 @@ NetworkOPs::AccountTxs NetworkOPsImp::getAccountTxs ( return ret; } -std::vector NetworkOPsImp::getAccountTxsB ( +std::vector +NetworkOPsImp::getAccountTxsB( AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, bool bUnlimited) + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) { // can be called with no locks std::vector ret; - std::string sql = transactionsSQL ( - "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta", account, - minLedger, maxLedger, descending, offset, limit, true/*binary*/, false, + std::string sql = transactionsSQL( + "AccountTransactions.LedgerSeq,Status,RawTxn,TxnMeta", + account, + minLedger, + maxLedger, + descending, + offset, + limit, + true /*binary*/, + false, bUnlimited); { - auto db = app_.getTxnDB ().checkoutDb (); + auto db = app_.getTxnDB().checkoutDb(); boost::optional ledgerSeq; boost::optional status; - soci::blob sociTxnBlob (*db), sociTxnMetaBlob (*db); + soci::blob sociTxnBlob(*db), sociTxnMetaBlob(*db); soci::indicator rti, tmi; soci::statement st = - (db->prepare << sql, - soci::into(ledgerSeq), - soci::into(status), - soci::into(sociTxnBlob, rti), - soci::into(sociTxnMetaBlob, tmi)); + (db->prepare << sql, + soci::into(ledgerSeq), + soci::into(status), + soci::into(sociTxnBlob, rti), + soci::into(sociTxnMetaBlob, tmi)); - st.execute (); - while (st.fetch ()) + st.execute(); + while (st.fetch()) { Blob rawTxn; if (soci::i_ok == rti) - convert (sociTxnBlob, rawTxn); + convert(sociTxnBlob, rawTxn); Blob txnMeta; if (soci::i_ok == tmi) - convert (sociTxnMetaBlob, txnMeta); + convert(sociTxnMetaBlob, txnMeta); auto const seq = - rangeCheckedCast(ledgerSeq.value_or (0)); + rangeCheckedCast(ledgerSeq.value_or(0)); - ret.emplace_back ( - std::move(rawTxn), std::move(txnMeta), seq); + ret.emplace_back(std::move(rawTxn), std::move(txnMeta), seq); } } @@ -2289,21 +2472,23 @@ NetworkOPsImp::getTxsAccountB( return ret; } -bool NetworkOPsImp::recvValidation ( - STValidation::ref val, std::string const& source) +bool +NetworkOPsImp::recvValidation(STValidation::ref val, std::string const& source) { - JLOG(m_journal.debug()) << "recvValidation " << val->getLedgerHash () - << " from " << source; - pubValidation (val); + JLOG(m_journal.debug()) + << "recvValidation " << val->getLedgerHash() << " from " << source; + pubValidation(val); return handleNewValidation(app_, val, source); } -Json::Value NetworkOPsImp::getConsensusInfo () +Json::Value +NetworkOPsImp::getConsensusInfo() { - return mConsensus.getJson (true); + return mConsensus.getJson(true); } -Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) +Json::Value +NetworkOPsImp::getServerInfo(bool human, bool admin, bool counters) { Json::Value info = Json::objectValue; @@ -2341,23 +2526,23 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) // hostid: unique string describing the machine if (human) - info [jss::hostid] = getHostId (admin); + info[jss::hostid] = getHostId(admin); if (auto const netid = app_.overlay().networkID()) - info [jss::network_id] = static_cast(*netid); + info[jss::network_id] = static_cast(*netid); - info [jss::build_version] = BuildInfo::getVersionString (); + info[jss::build_version] = BuildInfo::getVersionString(); - info [jss::server_state] = strOperatingMode (admin); + info[jss::server_state] = strOperatingMode(admin); - info [jss::time] = to_string(date::floor( + info[jss::time] = to_string(date::floor( std::chrono::system_clock::now())); if (needNetworkLedger_) info[jss::network_ledger] = "waiting"; - info[jss::validation_quorum] = static_cast( - app_.validators ().quorum ()); + info[jss::validation_quorum] = + static_cast(app_.validators().quorum()); if (admin) { @@ -2401,16 +2586,15 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) } } } - info[jss::io_latency_ms] = static_cast ( - app_.getIOLatency().count()); + info[jss::io_latency_ms] = + static_cast(app_.getIOLatency().count()); if (admin) { if (!app_.getValidationPublicKey().empty()) { - info[jss::pubkey_validator] = toBase58 ( - TokenType::NodePublic, - app_.validators().localPublicKey()); + info[jss::pubkey_validator] = toBase58( + TokenType::NodePublic, app_.validators().localPublicKey()); } else { @@ -2424,22 +2608,20 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) info[jss::current_activities] = app_.getPerfLog().currentJson(); } - info[jss::pubkey_node] = toBase58 ( - TokenType::NodePublic, - app_.nodeIdentity().first); + info[jss::pubkey_node] = + toBase58(TokenType::NodePublic, app_.nodeIdentity().first); - info[jss::complete_ledgers] = - app_.getLedgerMaster ().getCompleteLedgers (); + info[jss::complete_ledgers] = app_.getLedgerMaster().getCompleteLedgers(); if (amendmentBlocked_) info[jss::amendment_blocked] = true; - auto const fp = m_ledgerMaster.getFetchPackCacheSize (); + auto const fp = m_ledgerMaster.getFetchPackCacheSize(); if (fp != 0) - info[jss::fetch_pack] = Json::UInt (fp); + info[jss::fetch_pack] = Json::UInt(fp); - info[jss::peers] = Json::UInt (app_.overlay ().size ()); + info[jss::peers] = Json::UInt(app_.overlay().size()); Json::Value lastClose = Json::objectValue; lastClose[jss::proposers] = Json::UInt(mConsensus.prevProposers()); @@ -2447,13 +2629,12 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) if (human) { lastClose[jss::converge_time_s] = - std::chrono::duration{ - mConsensus.prevRoundTime()}.count(); + std::chrono::duration{mConsensus.prevRoundTime()}.count(); } else { lastClose[jss::converge_time] = - Json::Int (mConsensus.prevRoundTime().count()); + Json::Int(mConsensus.prevRoundTime().count()); } info[jss::last_close] = lastClose; @@ -2461,22 +2642,25 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) // info[jss::consensus] = mConsensus.getJson(); if (admin) - info[jss::load] = m_job_queue.getJson (); + info[jss::load] = m_job_queue.getJson(); - auto const escalationMetrics = app_.getTxQ().getMetrics( - *app_.openLedger().current()); + auto const escalationMetrics = + app_.getTxQ().getMetrics(*app_.openLedger().current()); auto const loadFactorServer = app_.getFeeTrack().getLoadFactor(); auto const loadBaseServer = app_.getFeeTrack().getLoadBase(); /* Scale the escalated fee level to unitless "load factor". In practice, this just strips the units, but it will continue to work correctly if either base value ever changes. */ - auto const loadFactorFeeEscalation = mulDiv( - escalationMetrics.openLedgerFeeLevel, loadBaseServer, - escalationMetrics.referenceFeeLevel).second; + auto const loadFactorFeeEscalation = + mulDiv( + escalationMetrics.openLedgerFeeLevel, + loadBaseServer, + escalationMetrics.referenceFeeLevel) + .second; - auto const loadFactor = std::max(safe_cast(loadFactorServer), - loadFactorFeeEscalation); + auto const loadFactor = std::max( + safe_cast(loadFactorServer), loadFactorFeeEscalation); if (!human) { @@ -2498,80 +2682,78 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) } else { - info[jss::load_factor] = static_cast (loadFactor) / loadBaseServer; + info[jss::load_factor] = + static_cast(loadFactor) / loadBaseServer; if (loadFactorServer != loadFactor) info[jss::load_factor_server] = - static_cast (loadFactorServer) / loadBaseServer; + static_cast(loadFactorServer) / loadBaseServer; if (admin) { std::uint32_t fee = app_.getFeeTrack().getLocalFee(); if (fee != loadBaseServer) info[jss::load_factor_local] = - static_cast (fee) / loadBaseServer; - fee = app_.getFeeTrack ().getRemoteFee(); + static_cast(fee) / loadBaseServer; + fee = app_.getFeeTrack().getRemoteFee(); if (fee != loadBaseServer) info[jss::load_factor_net] = - static_cast (fee) / loadBaseServer; + static_cast(fee) / loadBaseServer; fee = app_.getFeeTrack().getClusterFee(); if (fee != loadBaseServer) info[jss::load_factor_cluster] = - static_cast (fee) / loadBaseServer; + static_cast(fee) / loadBaseServer; } if (escalationMetrics.openLedgerFeeLevel != escalationMetrics.referenceFeeLevel && - (admin || - loadFactorFeeEscalation != loadFactor)) + (admin || loadFactorFeeEscalation != loadFactor)) info[jss::load_factor_fee_escalation] = escalationMetrics.openLedgerFeeLevel.decimalFromReference( - escalationMetrics.referenceFeeLevel); + escalationMetrics.referenceFeeLevel); if (escalationMetrics.minProcessingFeeLevel != - escalationMetrics.referenceFeeLevel) + escalationMetrics.referenceFeeLevel) info[jss::load_factor_fee_queue] = escalationMetrics.minProcessingFeeLevel.decimalFromReference( escalationMetrics.referenceFeeLevel); } bool valid = false; - auto lpClosed = m_ledgerMaster.getValidatedLedger (); + auto lpClosed = m_ledgerMaster.getValidatedLedger(); if (lpClosed) valid = true; else - lpClosed = m_ledgerMaster.getClosedLedger (); + lpClosed = m_ledgerMaster.getClosedLedger(); if (lpClosed) { XRPAmount const baseFee = lpClosed->fees().base; - Json::Value l (Json::objectValue); - l[jss::seq] = Json::UInt (lpClosed->info().seq); - l[jss::hash] = to_string (lpClosed->info().hash); + Json::Value l(Json::objectValue); + l[jss::seq] = Json::UInt(lpClosed->info().seq); + l[jss::hash] = to_string(lpClosed->info().hash); if (!human) { l[jss::base_fee] = baseFee.jsonClipped(); - l[jss::reserve_base] = lpClosed->fees().accountReserve(0).jsonClipped(); - l[jss::reserve_inc] = - lpClosed->fees().increment.jsonClipped(); - l[jss::close_time] = Json::Value::UInt ( + l[jss::reserve_base] = + lpClosed->fees().accountReserve(0).jsonClipped(); + l[jss::reserve_inc] = lpClosed->fees().increment.jsonClipped(); + l[jss::close_time] = Json::Value::UInt( lpClosed->info().closeTime.time_since_epoch().count()); } else { - l[jss::base_fee_xrp] = - baseFee.decimalXRP(); + l[jss::base_fee_xrp] = baseFee.decimalXRP(); l[jss::reserve_base_xrp] = lpClosed->fees().accountReserve(0).decimalXRP(); - l[jss::reserve_inc_xrp] = - lpClosed->fees().increment.decimalXRP(); + l[jss::reserve_inc_xrp] = lpClosed->fees().increment.decimalXRP(); auto const nowOffset = app_.timeKeeper().nowOffset(); - if (std::abs (nowOffset.count()) >= 60) + if (std::abs(nowOffset.count()) >= 60) l[jss::system_time_offset] = nowOffset.count(); auto const closeOffset = app_.timeKeeper().closeOffset(); - if (std::abs (closeOffset.count()) >= 60) + if (std::abs(closeOffset.count()) >= 60) l[jss::close_time_offset] = closeOffset.count(); auto lCloseTime = lpClosed->info().closeTime; @@ -2581,7 +2763,7 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) using namespace std::chrono_literals; auto age = closeTime - lCloseTime; if (age < 1000000s) - l[jss::age] = Json::UInt (age.count()); + l[jss::age] = Json::UInt(age.count()); else l[jss::age] = 0; } @@ -2592,217 +2774,233 @@ Json::Value NetworkOPsImp::getServerInfo (bool human, bool admin, bool counters) else info[jss::closed_ledger] = l; - auto lpPublished = m_ledgerMaster.getPublishedLedger (); - if (! lpPublished) + auto lpPublished = m_ledgerMaster.getPublishedLedger(); + if (!lpPublished) info[jss::published_ledger] = "none"; else if (lpPublished->info().seq != lpClosed->info().seq) info[jss::published_ledger] = lpPublished->info().seq; } - std::tie(info[jss::state_accounting], - info[jss::server_state_duration_us]) = accounting_.json(); + std::tie(info[jss::state_accounting], info[jss::server_state_duration_us]) = + accounting_.json(); info[jss::uptime] = UptimeClock::now().time_since_epoch().count(); - info[jss::jq_trans_overflow] = std::to_string( - app_.overlay().getJqTransOverflow()); - info[jss::peer_disconnects] = std::to_string( - app_.overlay().getPeerDisconnect()); - info[jss::peer_disconnects_resources] = std::to_string( - app_.overlay().getPeerDisconnectCharges()); + info[jss::jq_trans_overflow] = + std::to_string(app_.overlay().getJqTransOverflow()); + info[jss::peer_disconnects] = + std::to_string(app_.overlay().getPeerDisconnect()); + info[jss::peer_disconnects_resources] = + std::to_string(app_.overlay().getPeerDisconnectCharges()); return info; } -void NetworkOPsImp::clearLedgerFetch () +void +NetworkOPsImp::clearLedgerFetch() { app_.getInboundLedgers().clearFailures(); } -Json::Value NetworkOPsImp::getLedgerFetchInfo () +Json::Value +NetworkOPsImp::getLedgerFetchInfo() { return app_.getInboundLedgers().getInfo(); } -void NetworkOPsImp::pubProposedTransaction ( +void +NetworkOPsImp::pubProposedTransaction( std::shared_ptr const& lpCurrent, - std::shared_ptr const& stTxn, TER terResult) + std::shared_ptr const& stTxn, + TER terResult) { - Json::Value jvObj = transJson (*stTxn, terResult, false, lpCurrent); + Json::Value jvObj = transJson(*stTxn, terResult, false, lpCurrent); { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - auto it = mStreamMaps[sRTTransactions].begin (); - while (it != mStreamMaps[sRTTransactions].end ()) + auto it = mStreamMaps[sRTTransactions].begin(); + while (it != mStreamMaps[sRTTransactions].end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++it; } else { - it = mStreamMaps[sRTTransactions].erase (it); + it = mStreamMaps[sRTTransactions].erase(it); } } } - AcceptedLedgerTx alt (lpCurrent, stTxn, terResult, - app_.accountIDCache(), app_.logs()); - JLOG(m_journal.trace()) << "pubProposed: " << alt.getJson (); - pubAccountTransaction (lpCurrent, alt, false); + AcceptedLedgerTx alt( + lpCurrent, stTxn, terResult, app_.accountIDCache(), app_.logs()); + JLOG(m_journal.trace()) << "pubProposed: " << alt.getJson(); + pubAccountTransaction(lpCurrent, alt, false); } -void NetworkOPsImp::pubLedger ( - std::shared_ptr const& lpAccepted) +void +NetworkOPsImp::pubLedger(std::shared_ptr const& lpAccepted) { // Ledgers are published only when they acquire sufficient validations // Holes are filled across connection loss or other catastrophe std::shared_ptr alpAccepted = - app_.getAcceptedLedgerCache().fetch (lpAccepted->info().hash); - if (! alpAccepted) + app_.getAcceptedLedgerCache().fetch(lpAccepted->info().hash); + if (!alpAccepted) { - alpAccepted = std::make_shared ( + alpAccepted = std::make_shared( lpAccepted, app_.accountIDCache(), app_.logs()); app_.getAcceptedLedgerCache().canonicalize_replace_client( lpAccepted->info().hash, alpAccepted); } { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!mStreamMaps[sLedger].empty ()) + if (!mStreamMaps[sLedger].empty()) { - Json::Value jvObj (Json::objectValue); + Json::Value jvObj(Json::objectValue); jvObj[jss::type] = "ledgerClosed"; jvObj[jss::ledger_index] = lpAccepted->info().seq; - jvObj[jss::ledger_hash] = to_string (lpAccepted->info().hash); - jvObj[jss::ledger_time] - = Json::Value::UInt (lpAccepted->info().closeTime.time_since_epoch().count()); + jvObj[jss::ledger_hash] = to_string(lpAccepted->info().hash); + jvObj[jss::ledger_time] = Json::Value::UInt( + lpAccepted->info().closeTime.time_since_epoch().count()); jvObj[jss::fee_ref] = lpAccepted->fees().units.jsonClipped(); jvObj[jss::fee_base] = lpAccepted->fees().base.jsonClipped(); jvObj[jss::reserve_base] = lpAccepted->fees().accountReserve(0).jsonClipped(); - jvObj[jss::reserve_inc] = lpAccepted->fees().increment.jsonClipped(); + jvObj[jss::reserve_inc] = + lpAccepted->fees().increment.jsonClipped(); - jvObj[jss::txn_count] = Json::UInt (alpAccepted->getTxnCount ()); + jvObj[jss::txn_count] = Json::UInt(alpAccepted->getTxnCount()); if (mMode >= OperatingMode::SYNCING) { - jvObj[jss::validated_ledgers] - = app_.getLedgerMaster ().getCompleteLedgers (); + jvObj[jss::validated_ledgers] = + app_.getLedgerMaster().getCompleteLedgers(); } - auto it = mStreamMaps[sLedger].begin (); - while (it != mStreamMaps[sLedger].end ()) + auto it = mStreamMaps[sLedger].begin(); + while (it != mStreamMaps[sLedger].end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++it; } else - it = mStreamMaps[sLedger].erase (it); + it = mStreamMaps[sLedger].erase(it); } } } // Don't lock since pubAcceptedTransaction is locking. - for (auto const& [_, accTx] : alpAccepted->getMap ()) + for (auto const& [_, accTx] : alpAccepted->getMap()) { (void)_; - JLOG(m_journal.trace()) << "pubAccepted: " << accTx->getJson (); - pubValidatedTransaction (lpAccepted, *accTx); + JLOG(m_journal.trace()) << "pubAccepted: " << accTx->getJson(); + pubValidatedTransaction(lpAccepted, *accTx); } } -void NetworkOPsImp::reportFeeChange () +void +NetworkOPsImp::reportFeeChange() { - ServerFeeSummary f{app_.openLedger().current()->fees().base, + ServerFeeSummary f{ + app_.openLedger().current()->fees().base, app_.getTxQ().getMetrics(*app_.openLedger().current()), app_.getFeeTrack()}; // only schedule the job if something has changed if (f != mLastFeeSummary) { - m_job_queue.addJob ( - jtCLIENT, "reportFeeChange->pubServer", - [this] (Job&) { pubServer(); }); + m_job_queue.addJob( + jtCLIENT, "reportFeeChange->pubServer", [this](Job&) { + pubServer(); + }); } } -void NetworkOPsImp::reportConsensusStateChange (ConsensusPhase phase) +void +NetworkOPsImp::reportConsensusStateChange(ConsensusPhase phase) { - m_job_queue.addJob ( - jtCLIENT, "reportConsensusStateChange->pubConsensus", - [this, phase] (Job&) { pubConsensus(phase); }); + m_job_queue.addJob( + jtCLIENT, + "reportConsensusStateChange->pubConsensus", + [this, phase](Job&) { pubConsensus(phase); }); } // This routine should only be used to publish accepted or validated // transactions. -Json::Value NetworkOPsImp::transJson( - const STTx& stTxn, TER terResult, bool bValidated, +Json::Value +NetworkOPsImp::transJson( + const STTx& stTxn, + TER terResult, + bool bValidated, std::shared_ptr const& lpCurrent) { - Json::Value jvObj (Json::objectValue); + Json::Value jvObj(Json::objectValue); std::string sToken; std::string sHuman; - transResultInfo (terResult, sToken, sHuman); + transResultInfo(terResult, sToken, sHuman); - jvObj[jss::type] = "transaction"; - jvObj[jss::transaction] = stTxn.getJson (JsonOptions::none); + jvObj[jss::type] = "transaction"; + jvObj[jss::transaction] = stTxn.getJson(JsonOptions::none); if (bValidated) { - jvObj[jss::ledger_index] = lpCurrent->info().seq; - jvObj[jss::ledger_hash] = to_string (lpCurrent->info().hash); + jvObj[jss::ledger_index] = lpCurrent->info().seq; + jvObj[jss::ledger_hash] = to_string(lpCurrent->info().hash); jvObj[jss::transaction][jss::date] = lpCurrent->info().closeTime.time_since_epoch().count(); - jvObj[jss::validated] = true; + jvObj[jss::validated] = true; // WRITEME: Put the account next seq here - } else { - jvObj[jss::validated] = false; - jvObj[jss::ledger_current_index] = lpCurrent->info().seq; + jvObj[jss::validated] = false; + jvObj[jss::ledger_current_index] = lpCurrent->info().seq; } - jvObj[jss::status] = bValidated ? "closed" : "proposed"; - jvObj[jss::engine_result] = sToken; - jvObj[jss::engine_result_code] = terResult; - jvObj[jss::engine_result_message] = sHuman; + jvObj[jss::status] = bValidated ? "closed" : "proposed"; + jvObj[jss::engine_result] = sToken; + jvObj[jss::engine_result_code] = terResult; + jvObj[jss::engine_result_message] = sHuman; if (stTxn.getTxnType() == ttOFFER_CREATE) { auto const account = stTxn.getAccountID(sfAccount); - auto const amount = stTxn.getFieldAmount (sfTakerGets); + auto const amount = stTxn.getFieldAmount(sfTakerGets); // If the offer create is not self funded then add the owner balance - if (account != amount.issue ().account) + if (account != amount.issue().account) { - auto const ownerFunds = accountFunds(*lpCurrent, - account, amount, fhIGNORE_FREEZE, app_.journal ("View")); - jvObj[jss::transaction][jss::owner_funds] = ownerFunds.getText (); + auto const ownerFunds = accountFunds( + *lpCurrent, + account, + amount, + fhIGNORE_FREEZE, + app_.journal("View")); + jvObj[jss::transaction][jss::owner_funds] = ownerFunds.getText(); } } return jvObj; } -void NetworkOPsImp::pubValidatedTransaction ( +void +NetworkOPsImp::pubValidatedTransaction( std::shared_ptr const& alAccepted, const AcceptedLedgerTx& alTx) { std::shared_ptr stTxn = alTx.getTxn(); - Json::Value jvObj = transJson ( - *stTxn, alTx.getResult (), true, alAccepted); + Json::Value jvObj = transJson(*stTxn, alTx.getResult(), true, alAccepted); if (auto const txMeta = alTx.getMeta()) { @@ -2812,116 +3010,117 @@ void NetworkOPsImp::pubValidatedTransaction ( } { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - auto it = mStreamMaps[sTransactions].begin (); - while (it != mStreamMaps[sTransactions].end ()) + auto it = mStreamMaps[sTransactions].begin(); + while (it != mStreamMaps[sTransactions].end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++it; } else - it = mStreamMaps[sTransactions].erase (it); + it = mStreamMaps[sTransactions].erase(it); } - it = mStreamMaps[sRTTransactions].begin (); + it = mStreamMaps[sRTTransactions].begin(); - while (it != mStreamMaps[sRTTransactions].end ()) + while (it != mStreamMaps[sRTTransactions].end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - p->send (jvObj, true); + p->send(jvObj, true); ++it; } else - it = mStreamMaps[sRTTransactions].erase (it); + it = mStreamMaps[sRTTransactions].erase(it); } } - app_.getOrderBookDB ().processTxn (alAccepted, alTx, jvObj); - pubAccountTransaction (alAccepted, alTx, true); + app_.getOrderBookDB().processTxn(alAccepted, alTx, jvObj); + pubAccountTransaction(alAccepted, alTx, true); } -void NetworkOPsImp::pubAccountTransaction ( +void +NetworkOPsImp::pubAccountTransaction( std::shared_ptr const& lpCurrent, const AcceptedLedgerTx& alTx, bool bAccepted) { - hash_set notify; - int iProposed = 0; - int iAccepted = 0; + hash_set notify; + int iProposed = 0; + int iAccepted = 0; { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - if (!bAccepted && mSubRTAccount.empty ()) return; + if (!bAccepted && mSubRTAccount.empty()) + return; - if (!mSubAccount.empty () || (!mSubRTAccount.empty ()) ) + if (!mSubAccount.empty() || (!mSubRTAccount.empty())) { - for (auto const& affectedAccount: alTx.getAffected ()) + for (auto const& affectedAccount : alTx.getAffected()) { - auto simiIt - = mSubRTAccount.find (affectedAccount); - if (simiIt != mSubRTAccount.end ()) + auto simiIt = mSubRTAccount.find(affectedAccount); + if (simiIt != mSubRTAccount.end()) { - auto it = simiIt->second.begin (); + auto it = simiIt->second.begin(); - while (it != simiIt->second.end ()) + while (it != simiIt->second.end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - notify.insert (p); + notify.insert(p); ++it; ++iProposed; } else - it = simiIt->second.erase (it); + it = simiIt->second.erase(it); } } if (bAccepted) { - simiIt = mSubAccount.find (affectedAccount); + simiIt = mSubAccount.find(affectedAccount); - if (simiIt != mSubAccount.end ()) + if (simiIt != mSubAccount.end()) { - auto it = simiIt->second.begin (); - while (it != simiIt->second.end ()) + auto it = simiIt->second.begin(); + while (it != simiIt->second.end()) { - InfoSub::pointer p = it->second.lock (); + InfoSub::pointer p = it->second.lock(); if (p) { - notify.insert (p); + notify.insert(p); ++it; ++iAccepted; } else - it = simiIt->second.erase (it); + it = simiIt->second.erase(it); } } } } } } - JLOG(m_journal.trace()) << "pubAccountTransaction:" << - " iProposed=" << iProposed << - " iAccepted=" << iAccepted; + JLOG(m_journal.trace()) + << "pubAccountTransaction:" + << " iProposed=" << iProposed << " iAccepted=" << iAccepted; - if (!notify.empty ()) + if (!notify.empty()) { std::shared_ptr stTxn = alTx.getTxn(); - Json::Value jvObj = transJson ( - *stTxn, alTx.getResult (), bAccepted, lpCurrent); + Json::Value jvObj = + transJson(*stTxn, alTx.getResult(), bAccepted, lpCurrent); - if (alTx.isApplied ()) + if (alTx.isApplied()) { if (auto const txMeta = alTx.getMeta()) { @@ -2932,7 +3131,7 @@ void NetworkOPsImp::pubAccountTransaction ( } for (InfoSub::ref isrListener : notify) - isrListener->send (jvObj, true); + isrListener->send(jvObj, true); } } @@ -2940,43 +3139,45 @@ void NetworkOPsImp::pubAccountTransaction ( // Monitoring // -void NetworkOPsImp::subAccount ( +void +NetworkOPsImp::subAccount( InfoSub::ref isrListener, - hash_set const& vnaAccountIDs, bool rt) + hash_set const& vnaAccountIDs, + bool rt) { SubInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount; for (auto const& naAccountID : vnaAccountIDs) { - JLOG(m_journal.trace()) << - "subAccount: account: " << toBase58(naAccountID); + JLOG(m_journal.trace()) + << "subAccount: account: " << toBase58(naAccountID); - isrListener->insertSubAccountInfo (naAccountID, rt); + isrListener->insertSubAccountInfo(naAccountID, rt); } - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); for (auto const& naAccountID : vnaAccountIDs) { - auto simIterator = subMap.find (naAccountID); - if (simIterator == subMap.end ()) + auto simIterator = subMap.find(naAccountID); + if (simIterator == subMap.end()) { // Not found, note that account has a new single listner. - SubMapType usisElement; - usisElement[isrListener->getSeq ()] = isrListener; + SubMapType usisElement; + usisElement[isrListener->getSeq()] = isrListener; // VFALCO NOTE This is making a needless copy of naAccountID - subMap.insert (simIterator, - make_pair(naAccountID, usisElement)); + subMap.insert(simIterator, make_pair(naAccountID, usisElement)); } else { // Found, note that the account has another listener. - simIterator->second[isrListener->getSeq ()] = isrListener; + simIterator->second[isrListener->getSeq()] = isrListener; } } } -void NetworkOPsImp::unsubAccount ( +void +NetworkOPsImp::unsubAccount( InfoSub::ref isrListener, hash_set const& vnaAccountIDs, bool rt) @@ -2988,121 +3189,135 @@ void NetworkOPsImp::unsubAccount ( } // Remove from the server - unsubAccountInternal (isrListener->getSeq(), vnaAccountIDs, rt); + unsubAccountInternal(isrListener->getSeq(), vnaAccountIDs, rt); } -void NetworkOPsImp::unsubAccountInternal ( +void +NetworkOPsImp::unsubAccountInternal( std::uint64_t uSeq, hash_set const& vnaAccountIDs, bool rt) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); SubInfoMapType& subMap = rt ? mSubRTAccount : mSubAccount; for (auto const& naAccountID : vnaAccountIDs) { - auto simIterator = subMap.find (naAccountID); + auto simIterator = subMap.find(naAccountID); - if (simIterator != subMap.end ()) + if (simIterator != subMap.end()) { // Found - simIterator->second.erase (uSeq); + simIterator->second.erase(uSeq); - if (simIterator->second.empty ()) + if (simIterator->second.empty()) { // Don't need hash entry. - subMap.erase (simIterator); + subMap.erase(simIterator); } } } } -bool NetworkOPsImp::subBook (InfoSub::ref isrListener, Book const& book) +bool +NetworkOPsImp::subBook(InfoSub::ref isrListener, Book const& book) { - if (auto listeners = app_.getOrderBookDB ().makeBookListeners (book)) - listeners->addSubscriber (isrListener); + if (auto listeners = app_.getOrderBookDB().makeBookListeners(book)) + listeners->addSubscriber(isrListener); else - assert (false); + assert(false); return true; } -bool NetworkOPsImp::unsubBook (std::uint64_t uSeq, Book const& book) +bool +NetworkOPsImp::unsubBook(std::uint64_t uSeq, Book const& book) { - if (auto listeners = app_.getOrderBookDB ().getBookListeners (book)) - listeners->removeSubscriber (uSeq); + if (auto listeners = app_.getOrderBookDB().getBookListeners(book)) + listeners->removeSubscriber(uSeq); return true; } -std::uint32_t NetworkOPsImp::acceptLedger ( +std::uint32_t +NetworkOPsImp::acceptLedger( boost::optional consensusDelay) { // This code-path is exclusively used when the server is in standalone // mode via `ledger_accept` - assert (m_standalone); + assert(m_standalone); if (!m_standalone) - Throw ("Operation only possible in STANDALONE mode."); + Throw( + "Operation only possible in STANDALONE mode."); // FIXME Could we improve on this and remove the need for a specialized // API in Consensus? - beginConsensus (m_ledgerMaster.getClosedLedger()->info().hash); - mConsensus.simulate (app_.timeKeeper().closeTime(), consensusDelay); - return m_ledgerMaster.getCurrentLedger ()->info().seq; + beginConsensus(m_ledgerMaster.getClosedLedger()->info().hash); + mConsensus.simulate(app_.timeKeeper().closeTime(), consensusDelay); + return m_ledgerMaster.getCurrentLedger()->info().seq; } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subLedger (InfoSub::ref isrListener, Json::Value& jvResult) +bool +NetworkOPsImp::subLedger(InfoSub::ref isrListener, Json::Value& jvResult) { - if (auto lpClosed = m_ledgerMaster.getValidatedLedger ()) + if (auto lpClosed = m_ledgerMaster.getValidatedLedger()) { - jvResult[jss::ledger_index] = lpClosed->info().seq; - jvResult[jss::ledger_hash] = to_string (lpClosed->info().hash); + jvResult[jss::ledger_index] = lpClosed->info().seq; + jvResult[jss::ledger_hash] = to_string(lpClosed->info().hash); jvResult[jss::ledger_time] = Json::Value::UInt( lpClosed->info().closeTime.time_since_epoch().count()); jvResult[jss::fee_ref] = lpClosed->fees().units.jsonClipped(); - jvResult[jss::fee_base] = lpClosed->fees().base.jsonClipped(); - jvResult[jss::reserve_base] = + jvResult[jss::fee_base] = lpClosed->fees().base.jsonClipped(); + jvResult[jss::reserve_base] = lpClosed->fees().accountReserve(0).jsonClipped(); - jvResult[jss::reserve_inc] = lpClosed->fees().increment.jsonClipped(); + jvResult[jss::reserve_inc] = lpClosed->fees().increment.jsonClipped(); } - if ((mMode >= OperatingMode::SYNCING) && !isNeedNetworkLedger ()) + if ((mMode >= OperatingMode::SYNCING) && !isNeedNetworkLedger()) { - jvResult[jss::validated_ledgers] - = app_.getLedgerMaster ().getCompleteLedgers (); + jvResult[jss::validated_ledgers] = + app_.getLedgerMaster().getCompleteLedgers(); } - std::lock_guard sl (mSubLock); - return mStreamMaps[sLedger].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sLedger] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubLedger (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubLedger(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sLedger].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sLedger].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subManifests (InfoSub::ref isrListener) +bool +NetworkOPsImp::subManifests(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sManifests].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sManifests] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubManifests (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubManifests(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sManifests].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sManifests].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subServer (InfoSub::ref isrListener, Json::Value& jvResult, +bool +NetworkOPsImp::subServer( + InfoSub::ref isrListener, + Json::Value& jvResult, bool admin) { uint256 uRandom; @@ -3111,133 +3326,148 @@ bool NetworkOPsImp::subServer (InfoSub::ref isrListener, Json::Value& jvResult, jvResult[jss::stand_alone] = m_standalone; // CHECKME: is it necessary to provide a random number here? - beast::rngfill ( - uRandom.begin(), - uRandom.size(), - crypto_prng()); + beast::rngfill(uRandom.begin(), uRandom.size(), crypto_prng()); auto const& feeTrack = app_.getFeeTrack(); - jvResult[jss::random] = to_string (uRandom); - jvResult[jss::server_status] = strOperatingMode (admin); - jvResult[jss::load_base] = feeTrack.getLoadBase (); - jvResult[jss::load_factor] = feeTrack.getLoadFactor (); - jvResult [jss::hostid] = getHostId (admin); - jvResult[jss::pubkey_node] = toBase58 ( - TokenType::NodePublic, - app_.nodeIdentity().first); + jvResult[jss::random] = to_string(uRandom); + jvResult[jss::server_status] = strOperatingMode(admin); + jvResult[jss::load_base] = feeTrack.getLoadBase(); + jvResult[jss::load_factor] = feeTrack.getLoadFactor(); + jvResult[jss::hostid] = getHostId(admin); + jvResult[jss::pubkey_node] = + toBase58(TokenType::NodePublic, app_.nodeIdentity().first); - std::lock_guard sl (mSubLock); - return mStreamMaps[sServer].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sServer] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubServer (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubServer(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sServer].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sServer].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subTransactions (InfoSub::ref isrListener) +bool +NetworkOPsImp::subTransactions(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sTransactions].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sTransactions] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubTransactions (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubTransactions(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sTransactions].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sTransactions].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subRTTransactions (InfoSub::ref isrListener) +bool +NetworkOPsImp::subRTTransactions(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sRTTransactions].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sRTTransactions] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubRTTransactions (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubRTTransactions(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sRTTransactions].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sRTTransactions].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subValidations (InfoSub::ref isrListener) +bool +NetworkOPsImp::subValidations(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sValidations].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sValidations] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubValidations (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubValidations(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sValidations].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sValidations].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subPeerStatus (InfoSub::ref isrListener) +bool +NetworkOPsImp::subPeerStatus(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sPeerStatus].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sPeerStatus] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubPeerStatus (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubPeerStatus(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sPeerStatus].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sPeerStatus].erase(uSeq); } // <-- bool: true=added, false=already there -bool NetworkOPsImp::subConsensus (InfoSub::ref isrListener) +bool +NetworkOPsImp::subConsensus(InfoSub::ref isrListener) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sConsensusPhase].emplace ( - isrListener->getSeq (), isrListener).second; + std::lock_guard sl(mSubLock); + return mStreamMaps[sConsensusPhase] + .emplace(isrListener->getSeq(), isrListener) + .second; } // <-- bool: true=erased, false=was not there -bool NetworkOPsImp::unsubConsensus (std::uint64_t uSeq) +bool +NetworkOPsImp::unsubConsensus(std::uint64_t uSeq) { - std::lock_guard sl (mSubLock); - return mStreamMaps[sConsensusPhase].erase (uSeq); + std::lock_guard sl(mSubLock); + return mStreamMaps[sConsensusPhase].erase(uSeq); } -InfoSub::pointer NetworkOPsImp::findRpcSub (std::string const& strUrl) +InfoSub::pointer +NetworkOPsImp::findRpcSub(std::string const& strUrl) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - subRpcMapType::iterator it = mRpcSubMap.find (strUrl); + subRpcMapType::iterator it = mRpcSubMap.find(strUrl); - if (it != mRpcSubMap.end ()) + if (it != mRpcSubMap.end()) return it->second; - return InfoSub::pointer (); + return InfoSub::pointer(); } -InfoSub::pointer NetworkOPsImp::addRpcSub ( - std::string const& strUrl, InfoSub::ref rspEntry) +InfoSub::pointer +NetworkOPsImp::addRpcSub(std::string const& strUrl, InfoSub::ref rspEntry) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); - mRpcSubMap.emplace (strUrl, rspEntry); + mRpcSubMap.emplace(strUrl, rspEntry); return rspEntry; } -bool NetworkOPsImp::tryRemoveRpcSub (std::string const& strUrl) +bool +NetworkOPsImp::tryRemoveRpcSub(std::string const& strUrl) { - std::lock_guard sl (mSubLock); + std::lock_guard sl(mSubLock); auto pInfo = findRpcSub(strUrl); if (!pInfo) @@ -3259,7 +3489,8 @@ bool NetworkOPsImp::tryRemoveRpcSub (std::string const& strUrl) // NIKB FIXME this should be looked at. There's no reason why this shouldn't // work, but it demonstrated poor performance. // -void NetworkOPsImp::getBookPage ( +void +NetworkOPsImp::getBookPage( std::shared_ptr& lpLedger, Book const& book, AccountID const& uTakerID, @@ -3267,14 +3498,14 @@ void NetworkOPsImp::getBookPage ( unsigned int iLimit, Json::Value const& jvMarker, Json::Value& jvResult) -{ // CAUTION: This is the old get book page logic +{ // CAUTION: This is the old get book page logic Json::Value& jvOffers = - (jvResult[jss::offers] = Json::Value (Json::arrayValue)); + (jvResult[jss::offers] = Json::Value(Json::arrayValue)); std::map umBalance; - const uint256 uBookBase = getBookBase (book); - const uint256 uBookEnd = getQualityNext (uBookBase); - uint256 uTipIndex = uBookBase; + const uint256 uBookBase = getBookBase(book); + const uint256 uBookEnd = getQualityNext(uBookBase); + uint256 uTipIndex = uBookBase; if (auto stream = m_journal.trace()) { @@ -3286,26 +3517,25 @@ void NetworkOPsImp::getBookPage ( ReadView const& view = *lpLedger; - bool const bGlobalFreeze = - isGlobalFrozen(view, book.out.account) || - isGlobalFrozen(view, book.in.account); + bool const bGlobalFreeze = isGlobalFrozen(view, book.out.account) || + isGlobalFrozen(view, book.in.account); - bool bDone = false; - bool bDirectAdvance = true; + bool bDone = false; + bool bDirectAdvance = true; std::shared_ptr sleOfferDir; - uint256 offerIndex; - unsigned int uBookEntry; - STAmount saDirRate; + uint256 offerIndex; + unsigned int uBookEntry; + STAmount saDirRate; auto const rate = transferRate(view, book.out.account); - auto viewJ = app_.journal ("View"); + auto viewJ = app_.journal("View"); - while (! bDone && iLimit-- > 0) + while (!bDone && iLimit-- > 0) { if (bDirectAdvance) { - bDirectAdvance = false; + bDirectAdvance = false; JLOG(m_journal.trace()) << "getBookPage: bDirectAdvance"; @@ -3318,15 +3548,20 @@ void NetworkOPsImp::getBookPage ( if (!sleOfferDir) { JLOG(m_journal.trace()) << "getBookPage: bDone"; - bDone = true; + bDone = true; } else { uTipIndex = sleOfferDir->key(); - saDirRate = amountFromQuality (getQuality (uTipIndex)); + saDirRate = amountFromQuality(getQuality(uTipIndex)); - cdirFirst (view, - uTipIndex, sleOfferDir, uBookEntry, offerIndex, viewJ); + cdirFirst( + view, + uTipIndex, + sleOfferDir, + uBookEntry, + offerIndex, + viewJ); JLOG(m_journal.trace()) << "getBookPage: uTipIndex=" << uTipIndex; @@ -3341,55 +3576,56 @@ void NetworkOPsImp::getBookPage ( if (sleOffer) { - auto const uOfferOwnerID = - sleOffer->getAccountID (sfAccount); - auto const& saTakerGets = - sleOffer->getFieldAmount (sfTakerGets); - auto const& saTakerPays = - sleOffer->getFieldAmount (sfTakerPays); + auto const uOfferOwnerID = sleOffer->getAccountID(sfAccount); + auto const& saTakerGets = sleOffer->getFieldAmount(sfTakerGets); + auto const& saTakerPays = sleOffer->getFieldAmount(sfTakerPays); STAmount saOwnerFunds; - bool firstOwnerOffer (true); + bool firstOwnerOffer(true); if (book.out.account == uOfferOwnerID) { // If an offer is selling issuer's own IOUs, it is fully // funded. - saOwnerFunds = saTakerGets; + saOwnerFunds = saTakerGets; } else if (bGlobalFreeze) { // If either asset is globally frozen, consider all offers // that aren't ours to be totally unfunded - saOwnerFunds.clear (book.out); + saOwnerFunds.clear(book.out); } else { - auto umBalanceEntry = umBalance.find (uOfferOwnerID); - if (umBalanceEntry != umBalance.end ()) + auto umBalanceEntry = umBalance.find(uOfferOwnerID); + if (umBalanceEntry != umBalance.end()) { // Found in running balance table. - saOwnerFunds = umBalanceEntry->second; + saOwnerFunds = umBalanceEntry->second; firstOwnerOffer = false; } else { // Did not find balance in table. - saOwnerFunds = accountHolds (view, - uOfferOwnerID, book.out.currency, - book.out.account, fhZERO_IF_FROZEN, viewJ); + saOwnerFunds = accountHolds( + view, + uOfferOwnerID, + book.out.currency, + book.out.account, + fhZERO_IF_FROZEN, + viewJ); if (saOwnerFunds < beast::zero) { // Treat negative funds as zero. - saOwnerFunds.clear (); + saOwnerFunds.clear(); } } } - Json::Value jvOffer = sleOffer->getJson (JsonOptions::none); + Json::Value jvOffer = sleOffer->getJson(JsonOptions::none); STAmount saTakerGetsFunded; STAmount saOwnerFundsLimit = saOwnerFunds; @@ -3400,18 +3636,17 @@ void NetworkOPsImp::getBookPage ( && uTakerID != book.out.account // Not taking offers of own IOUs. && book.out.account != uOfferOwnerID) - // Offer owner not issuing ownfunds + // Offer owner not issuing ownfunds { // Need to charge a transfer fee to offer owner. offerRate = rate; - saOwnerFundsLimit = divide ( - saOwnerFunds, offerRate); + saOwnerFundsLimit = divide(saOwnerFunds, offerRate); } if (saOwnerFundsLimit >= saTakerGets) { // Sufficient funds no shenanigans. - saTakerGetsFunded = saTakerGets; + saTakerGetsFunded = saTakerGets; } else { @@ -3419,37 +3654,42 @@ void NetworkOPsImp::getBookPage ( saTakerGetsFunded = saOwnerFundsLimit; - saTakerGetsFunded.setJson (jvOffer[jss::taker_gets_funded]); - std::min ( - saTakerPays, multiply ( - saTakerGetsFunded, saDirRate, saTakerPays.issue ())).setJson - (jvOffer[jss::taker_pays_funded]); + saTakerGetsFunded.setJson(jvOffer[jss::taker_gets_funded]); + std::min( + saTakerPays, + multiply( + saTakerGetsFunded, saDirRate, saTakerPays.issue())) + .setJson(jvOffer[jss::taker_pays_funded]); } STAmount saOwnerPays = (parityRate == offerRate) ? saTakerGetsFunded - : std::min ( - saOwnerFunds, - multiply (saTakerGetsFunded, offerRate)); + : std::min( + saOwnerFunds, multiply(saTakerGetsFunded, offerRate)); - umBalance[uOfferOwnerID] = saOwnerFunds - saOwnerPays; + umBalance[uOfferOwnerID] = saOwnerFunds - saOwnerPays; // Include all offers funded and unfunded - Json::Value& jvOf = jvOffers.append (jvOffer); - jvOf[jss::quality] = saDirRate.getText (); + Json::Value& jvOf = jvOffers.append(jvOffer); + jvOf[jss::quality] = saDirRate.getText(); if (firstOwnerOffer) - jvOf[jss::owner_funds] = saOwnerFunds.getText (); + jvOf[jss::owner_funds] = saOwnerFunds.getText(); } else { JLOG(m_journal.warn()) << "Missing offer"; } - if (! cdirNext(view, - uTipIndex, sleOfferDir, uBookEntry, offerIndex, viewJ)) + if (!cdirNext( + view, + uTipIndex, + sleOfferDir, + uBookEntry, + offerIndex, + viewJ)) { - bDirectAdvance = true; + bDirectAdvance = true; } else { @@ -3463,13 +3703,13 @@ void NetworkOPsImp::getBookPage ( // jvResult[jss::nodes] = Json::Value(Json::arrayValue); } - #else // This is the new code that uses the book iterators // It has temporarily been disabled -void NetworkOPsImp::getBookPage ( +void +NetworkOPsImp::getBookPage( std::shared_ptr lpLedger, Book const& book, AccountID const& uTakerID, @@ -3478,68 +3718,70 @@ void NetworkOPsImp::getBookPage ( Json::Value const& jvMarker, Json::Value& jvResult) { - auto& jvOffers = (jvResult[jss::offers] = Json::Value (Json::arrayValue)); + auto& jvOffers = (jvResult[jss::offers] = Json::Value(Json::arrayValue)); std::map umBalance; - MetaView lesActive (lpLedger, tapNONE, true); - OrderBookIterator obIterator (lesActive, book); + MetaView lesActive(lpLedger, tapNONE, true); + OrderBookIterator obIterator(lesActive, book); auto const rate = transferRate(lesActive, book.out.account); - const bool bGlobalFreeze = lesActive.isGlobalFrozen (book.out.account) || - lesActive.isGlobalFrozen (book.in.account); + const bool bGlobalFreeze = lesActive.isGlobalFrozen(book.out.account) || + lesActive.isGlobalFrozen(book.in.account); - while (iLimit-- > 0 && obIterator.nextOffer ()) + while (iLimit-- > 0 && obIterator.nextOffer()) { - - SLE::pointer sleOffer = obIterator.getCurrentOffer(); + SLE::pointer sleOffer = obIterator.getCurrentOffer(); if (sleOffer) { - auto const uOfferOwnerID = sleOffer->getAccountID (sfAccount); - auto const& saTakerGets = sleOffer->getFieldAmount (sfTakerGets); - auto const& saTakerPays = sleOffer->getFieldAmount (sfTakerPays); - STAmount saDirRate = obIterator.getCurrentRate (); + auto const uOfferOwnerID = sleOffer->getAccountID(sfAccount); + auto const& saTakerGets = sleOffer->getFieldAmount(sfTakerGets); + auto const& saTakerPays = sleOffer->getFieldAmount(sfTakerPays); + STAmount saDirRate = obIterator.getCurrentRate(); STAmount saOwnerFunds; if (book.out.account == uOfferOwnerID) { // If offer is selling issuer's own IOUs, it is fully funded. - saOwnerFunds = saTakerGets; + saOwnerFunds = saTakerGets; } else if (bGlobalFreeze) { // If either asset is globally frozen, consider all offers // that aren't ours to be totally unfunded - saOwnerFunds.clear (book.out); + saOwnerFunds.clear(book.out); } else { - auto umBalanceEntry = umBalance.find (uOfferOwnerID); + auto umBalanceEntry = umBalance.find(uOfferOwnerID); - if (umBalanceEntry != umBalance.end ()) + if (umBalanceEntry != umBalance.end()) { // Found in running balance table. - saOwnerFunds = umBalanceEntry->second; + saOwnerFunds = umBalanceEntry->second; } else { // Did not find balance in table. - saOwnerFunds = lesActive.accountHolds ( - uOfferOwnerID, book.out.currency, book.out.account, fhZERO_IF_FROZEN); + saOwnerFunds = lesActive.accountHolds( + uOfferOwnerID, + book.out.currency, + book.out.account, + fhZERO_IF_FROZEN); - if (saOwnerFunds.isNegative ()) + if (saOwnerFunds.isNegative()) { // Treat negative funds as zero. - saOwnerFunds.zero (); + saOwnerFunds.zero(); } } } - Json::Value jvOffer = sleOffer->getJson (JsonOptions::none); + Json::Value jvOffer = sleOffer->getJson(JsonOptions::none); STAmount saTakerGetsFunded; STAmount saOwnerFundsLimit = saOwnerFunds; @@ -3550,47 +3792,46 @@ void NetworkOPsImp::getBookPage ( && uTakerID != book.out.account // Not taking offers of own IOUs. && book.out.account != uOfferOwnerID) - // Offer owner not issuing ownfunds + // Offer owner not issuing ownfunds { // Need to charge a transfer fee to offer owner. offerRate = rate; - saOwnerFundsLimit = divide (saOwnerFunds, offerRate); + saOwnerFundsLimit = divide(saOwnerFunds, offerRate); } if (saOwnerFundsLimit >= saTakerGets) { // Sufficient funds no shenanigans. - saTakerGetsFunded = saTakerGets; + saTakerGetsFunded = saTakerGets; } else { // Only provide, if not fully funded. - saTakerGetsFunded = saOwnerFundsLimit; + saTakerGetsFunded = saOwnerFundsLimit; - saTakerGetsFunded.setJson (jvOffer[jss::taker_gets_funded]); + saTakerGetsFunded.setJson(jvOffer[jss::taker_gets_funded]); // TOOD(tom): The result of this expression is not used - what's // going on here? - std::min (saTakerPays, multiply ( - saTakerGetsFunded, saDirRate, saTakerPays.issue ())).setJson ( - jvOffer[jss::taker_pays_funded]); + std::min( + saTakerPays, + multiply(saTakerGetsFunded, saDirRate, saTakerPays.issue())) + .setJson(jvOffer[jss::taker_pays_funded]); } STAmount saOwnerPays = (parityRate == offerRate) ? saTakerGetsFunded - : std::min ( - saOwnerFunds, - multiply (saTakerGetsFunded, offerRate)); + : std::min( + saOwnerFunds, multiply(saTakerGetsFunded, offerRate)); - umBalance[uOfferOwnerID] = saOwnerFunds - saOwnerPays; + umBalance[uOfferOwnerID] = saOwnerFunds - saOwnerPays; - if (!saOwnerFunds.isZero () || uOfferOwnerID == uTakerID) + if (!saOwnerFunds.isZero() || uOfferOwnerID == uTakerID) { // Only provide funded offers and offers of the taker. - Json::Value& jvOf = jvOffers.append (jvOffer); - jvOf[jss::quality] = saDirRate.getText (); + Json::Value& jvOf = jvOffers.append(jvOffer); + jvOf[jss::quality] = saDirRate.getText(); } - } } @@ -3602,19 +3843,19 @@ void NetworkOPsImp::getBookPage ( //------------------------------------------------------------------------------ -NetworkOPs::NetworkOPs (Stoppable& parent) - : InfoSub::Source ("NetworkOPs", parent) +NetworkOPs::NetworkOPs(Stoppable& parent) + : InfoSub::Source("NetworkOPs", parent) { } //------------------------------------------------------------------------------ - -void NetworkOPsImp::StateAccounting::mode (OperatingMode om) +void +NetworkOPsImp::StateAccounting::mode(OperatingMode om) { auto now = std::chrono::system_clock::now(); - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); ++counters_[static_cast(om)].transitions; counters_[static_cast(mode_)].dur += std::chrono::duration_cast(now - start_); @@ -3627,15 +3868,15 @@ NetworkOPsImp::StateAccounting::StateCountersJson NetworkOPsImp::StateAccounting::json() const { auto [counters, mode, start] = getCounterData(); - auto const current = std::chrono::duration_cast< - std::chrono::microseconds>(std::chrono::system_clock::now() - start); + auto const current = std::chrono::duration_cast( + std::chrono::system_clock::now() - start); counters[static_cast(mode)].dur += current; Json::Value ret = Json::objectValue; - for (std::size_t i = static_cast( - OperatingMode::DISCONNECTED); - i <= static_cast(OperatingMode::FULL); ++i) + for (std::size_t i = static_cast(OperatingMode::DISCONNECTED); + i <= static_cast(OperatingMode::FULL); + ++i) { ret[states_[i]] = Json::objectValue; auto& state = ret[states_[i]]; @@ -3649,15 +3890,33 @@ NetworkOPsImp::StateAccounting::json() const //------------------------------------------------------------------------------ std::unique_ptr -make_NetworkOPs (Application& app, NetworkOPs::clock_type& clock, - bool standalone, std::size_t minPeerCount, bool startvalid, - JobQueue& job_queue, LedgerMaster& ledgerMaster, Stoppable& parent, - ValidatorKeys const & validatorKeys, boost::asio::io_service& io_svc, - beast::Journal journal, beast::insight::Collector::ptr const& collector) +make_NetworkOPs( + Application& app, + NetworkOPs::clock_type& clock, + bool standalone, + std::size_t minPeerCount, + bool startvalid, + JobQueue& job_queue, + LedgerMaster& ledgerMaster, + Stoppable& parent, + ValidatorKeys const& validatorKeys, + boost::asio::io_service& io_svc, + beast::Journal journal, + beast::insight::Collector::ptr const& collector) { - return std::make_unique (app, clock, standalone, - minPeerCount, startvalid, job_queue, ledgerMaster, parent, - validatorKeys, io_svc, journal, collector); + return std::make_unique( + app, + clock, + standalone, + minPeerCount, + startvalid, + job_queue, + ledgerMaster, + parent, + validatorKeys, + io_svc, + journal, + collector); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/NetworkOPs.h b/src/ripple/app/misc/NetworkOPs.h index a1562ddc44..e27f25cc4f 100644 --- a/src/ripple/app/misc/NetworkOPs.h +++ b/src/ripple/app/misc/NetworkOPs.h @@ -20,16 +20,16 @@ #ifndef RIPPLE_APP_MISC_NETWORKOPS_H_INCLUDED #define RIPPLE_APP_MISC_NETWORKOPS_H_INCLUDED -#include #include +#include #include #include #include #include #include #include -#include #include +#include #include #include @@ -65,13 +65,12 @@ class ValidatorKeys; not change them without verifying each use and ensuring that it is not a breaking change. */ -enum class OperatingMode -{ - DISCONNECTED = 0, //!< not ready to process requests - CONNECTED = 1, //!< convinced we are talking to the network - SYNCING = 2, //!< fallen slightly behind - TRACKING = 3, //!< convinced we agree with the network - FULL = 4 //!< we have the ledger and can even validate +enum class OperatingMode { + DISCONNECTED = 0, //!< not ready to process requests + CONNECTED = 1, //!< convinced we are talking to the network + SYNCING = 2, //!< fallen slightly behind + TRACKING = 3, //!< convinced we agree with the network + FULL = 4 //!< we have the ledger and can even validate }; /** Provides server functionality for clients. @@ -86,37 +85,36 @@ enum class OperatingMode instances of rippled will need to be hardened to protect against hostile or unreliable servers. */ -class NetworkOPs - : public InfoSub::Source +class NetworkOPs : public InfoSub::Source { protected: - explicit NetworkOPs (Stoppable& parent); + explicit NetworkOPs(Stoppable& parent); public: - using clock_type = beast::abstract_clock ; + using clock_type = beast::abstract_clock; - enum class FailHard : unsigned char - { - no, - yes - }; - static inline FailHard doFailHard (bool noMeansDont) + enum class FailHard : unsigned char { no, yes }; + static inline FailHard + doFailHard(bool noMeansDont) { return noMeansDont ? FailHard::yes : FailHard::no; } public: - ~NetworkOPs () override = default; + ~NetworkOPs() override = default; //-------------------------------------------------------------------------- // // Network information // - virtual OperatingMode getOperatingMode () const = 0; - virtual std::string strOperatingMode ( - OperatingMode const mode, bool const admin = false) const = 0; - virtual std::string strOperatingMode (bool const admin = false) const = 0; + virtual OperatingMode + getOperatingMode() const = 0; + virtual std::string + strOperatingMode(OperatingMode const mode, bool const admin = false) + const = 0; + virtual std::string + strOperatingMode(bool const admin = false) const = 0; //-------------------------------------------------------------------------- // @@ -124,7 +122,8 @@ public: // // must complete immediately - virtual void submitTransaction (std::shared_ptr const&) = 0; + virtual void + submitTransaction(std::shared_ptr const&) = 0; /** * Process transactions as they arrive from the network or which are @@ -135,15 +134,21 @@ public: * @param bLocal Client submission. * @param failType fail_hard setting from transaction submission. */ - virtual void processTransaction (std::shared_ptr& transaction, - bool bUnlimited, bool bLocal, FailHard failType) = 0; + virtual void + processTransaction( + std::shared_ptr& transaction, + bool bUnlimited, + bool bLocal, + FailHard failType) = 0; //-------------------------------------------------------------------------- // // Owner functions // - virtual Json::Value getOwnerInfo (std::shared_ptr lpLedger, + virtual Json::Value + getOwnerInfo( + std::shared_ptr lpLedger, AccountID const& account) = 0; //-------------------------------------------------------------------------- @@ -151,7 +156,8 @@ public: // Book functions // - virtual void getBookPage ( + virtual void + getBookPage( std::shared_ptr& lpLedger, Book const& book, AccountID const& uTakerID, @@ -163,38 +169,58 @@ public: //-------------------------------------------------------------------------- // ledger proposal/close functions - virtual void processTrustedProposal (RCLCxPeerPos peerPos, + virtual void + processTrustedProposal( + RCLCxPeerPos peerPos, std::shared_ptr set) = 0; - virtual bool recvValidation (STValidation::ref val, - std::string const& source) = 0; + virtual bool + recvValidation(STValidation::ref val, std::string const& source) = 0; - virtual void mapComplete (std::shared_ptr const& map, - bool fromAcquire) = 0; + virtual void + mapComplete(std::shared_ptr const& map, bool fromAcquire) = 0; // network state machine - virtual bool beginConsensus (uint256 const& netLCL) = 0; - virtual void endConsensus () = 0; - virtual void setStandAlone () = 0; - virtual void setStateTimer () = 0; + virtual bool + beginConsensus(uint256 const& netLCL) = 0; + virtual void + endConsensus() = 0; + virtual void + setStandAlone() = 0; + virtual void + setStateTimer() = 0; - virtual void setNeedNetworkLedger () = 0; - virtual void clearNeedNetworkLedger () = 0; - virtual bool isNeedNetworkLedger () = 0; - virtual bool isFull () = 0; - virtual void setMode(OperatingMode om) = 0; - virtual bool isAmendmentBlocked () = 0; - virtual void setAmendmentBlocked () = 0; - virtual bool isAmendmentWarned() = 0; - virtual void setAmendmentWarned() = 0; - virtual void clearAmendmentWarned() = 0; - virtual void consensusViewChange () = 0; + virtual void + setNeedNetworkLedger() = 0; + virtual void + clearNeedNetworkLedger() = 0; + virtual bool + isNeedNetworkLedger() = 0; + virtual bool + isFull() = 0; + virtual void + setMode(OperatingMode om) = 0; + virtual bool + isAmendmentBlocked() = 0; + virtual void + setAmendmentBlocked() = 0; + virtual bool + isAmendmentWarned() = 0; + virtual void + setAmendmentWarned() = 0; + virtual void + clearAmendmentWarned() = 0; + virtual void + consensusViewChange() = 0; - virtual Json::Value getConsensusInfo () = 0; - virtual Json::Value getServerInfo ( - bool human, bool admin, bool counters) = 0; - virtual void clearLedgerFetch () = 0; - virtual Json::Value getLedgerFetchInfo () = 0; + virtual Json::Value + getConsensusInfo() = 0; + virtual Json::Value + getServerInfo(bool human, bool admin, bool counters) = 0; + virtual void + clearLedgerFetch() = 0; + virtual Json::Value + getLedgerFetchInfo() = 0; /** Accepts the current transaction tree, return the new ledger's sequence @@ -202,15 +228,21 @@ public: performs a virtual consensus round, with all the transactions we are proposing being accepted. */ - virtual std::uint32_t acceptLedger ( - boost::optional consensusDelay = boost::none) = 0; + virtual std::uint32_t + acceptLedger( + boost::optional consensusDelay = + boost::none) = 0; - virtual uint256 getConsensusLCL () = 0; + virtual uint256 + getConsensusLCL() = 0; - virtual void reportFeeChange () = 0; + virtual void + reportFeeChange() = 0; - virtual void updateLocalTx (ReadView const& newValidLedger) = 0; - virtual std::size_t getLocalTxCount () = 0; + virtual void + updateLocalTx(ReadView const& newValidLedger) = 0; + virtual std::size_t + getLocalTxCount() = 0; struct AccountTxMarker { @@ -219,13 +251,18 @@ public: }; // client information retrieval functions - using AccountTx = std::pair, TxMeta::pointer>; + using AccountTx = std::pair, TxMeta::pointer>; using AccountTxs = std::vector; - virtual AccountTxs getAccountTxs ( + virtual AccountTxs + getAccountTxs( AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, bool bUnlimited) = 0; + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) = 0; virtual AccountTxs getTxsAccount( @@ -238,11 +275,17 @@ public: bool bUnlimited) = 0; using txnMetaLedgerType = std::tuple; - using MetaTxsList = std::vector; + using MetaTxsList = std::vector; - virtual MetaTxsList getAccountTxsB (AccountID const& account, - std::int32_t minLedger, std::int32_t maxLedger, bool descending, - std::uint32_t offset, int limit, bool bUnlimited) = 0; + virtual MetaTxsList + getAccountTxsB( + AccountID const& account, + std::int32_t minLedger, + std::int32_t maxLedger, + bool descending, + std::uint32_t offset, + int limit, + bool bUnlimited) = 0; virtual MetaTxsList getTxsAccountB( @@ -258,26 +301,34 @@ public: // // Monitoring: publisher side // - virtual void pubLedger ( - std::shared_ptr const& lpAccepted) = 0; - virtual void pubProposedTransaction ( + virtual void + pubLedger(std::shared_ptr const& lpAccepted) = 0; + virtual void + pubProposedTransaction( std::shared_ptr const& lpCurrent, - std::shared_ptr const& stTxn, TER terResult) = 0; - virtual void pubValidation (STValidation::ref val) = 0; - - - + std::shared_ptr const& stTxn, + TER terResult) = 0; + virtual void + pubValidation(STValidation::ref val) = 0; }; //------------------------------------------------------------------------------ std::unique_ptr -make_NetworkOPs (Application& app, NetworkOPs::clock_type& clock, - bool standalone, std::size_t minPeerCount, bool start_valid, - JobQueue& job_queue, LedgerMaster& ledgerMaster, Stoppable& parent, - ValidatorKeys const & validatorKeys, boost::asio::io_service& io_svc, - beast::Journal journal, beast::insight::Collector::ptr const& collector); +make_NetworkOPs( + Application& app, + NetworkOPs::clock_type& clock, + bool standalone, + std::size_t minPeerCount, + bool start_valid, + JobQueue& job_queue, + LedgerMaster& ledgerMaster, + Stoppable& parent, + ValidatorKeys const& validatorKeys, + boost::asio::io_service& io_svc, + beast::Journal journal, + beast::insight::Collector::ptr const& collector); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/OrderBook.h b/src/ripple/app/misc/OrderBook.h index bbedf8d16f..fb96bd5c00 100644 --- a/src/ripple/app/misc/OrderBook.h +++ b/src/ripple/app/misc/OrderBook.h @@ -26,8 +26,8 @@ namespace ripple { class OrderBook { public: - using pointer = std::shared_ptr ; - using ref = std::shared_ptr const&; + using pointer = std::shared_ptr; + using ref = std::shared_ptr const&; using List = std::vector; /** Construct from a currency specification. @@ -36,37 +36,43 @@ public: @param book in and out currency/issuer pairs. */ // VFALCO NOTE what is the meaning of the index parameter? - OrderBook (uint256 const& base, Book const& book) - : mBookBase(base), mBook(book) + OrderBook(uint256 const& base, Book const& book) + : mBookBase(base), mBook(book) { } - uint256 const& getBookBase () const + uint256 const& + getBookBase() const { return mBookBase; } - Book const& book() const + Book const& + book() const { return mBook; } - Currency const& getCurrencyIn () const + Currency const& + getCurrencyIn() const { return mBook.in.currency; } - Currency const& getCurrencyOut () const + Currency const& + getCurrencyOut() const { return mBook.out.currency; } - AccountID const& getIssuerIn () const + AccountID const& + getIssuerIn() const { return mBook.in.account; } - AccountID const& getIssuerOut () const + AccountID const& + getIssuerOut() const { return mBook.out.account; } @@ -76,6 +82,6 @@ private: Book const mBook; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/SHAMapStore.h b/src/ripple/app/misc/SHAMapStore.h index 0814b98df5..d5f63ab421 100644 --- a/src/ripple/app/misc/SHAMapStore.h +++ b/src/ripple/app/misc/SHAMapStore.h @@ -21,9 +21,9 @@ #define RIPPLE_APP_MISC_SHAMAPSTORE_H_INCLUDED #include +#include #include #include -#include namespace ripple { @@ -33,39 +33,47 @@ class TransactionMaster; * class to create database, launch online delete thread, and * related SQLite database */ -class SHAMapStore - : public Stoppable +class SHAMapStore : public Stoppable { public: - SHAMapStore (Stoppable& parent) : Stoppable ("SHAMapStore", parent) {} + SHAMapStore(Stoppable& parent) : Stoppable("SHAMapStore", parent) + { + } /** Called by LedgerMaster every time a ledger validates. */ - virtual void onLedgerClosed(std::shared_ptr const& ledger) = 0; + virtual void + onLedgerClosed(std::shared_ptr const& ledger) = 0; - virtual void rendezvous() const = 0; + virtual void + rendezvous() const = 0; - virtual std::uint32_t clampFetchDepth (std::uint32_t fetch_depth) const = 0; + virtual std::uint32_t + clampFetchDepth(std::uint32_t fetch_depth) const = 0; - virtual - std::unique_ptr + virtual std::unique_ptr makeNodeStore(std::string const& name, std::int32_t readThreads) = 0; /** Highest ledger that may be deleted. */ - virtual LedgerIndex setCanDelete (LedgerIndex canDelete) = 0; + virtual LedgerIndex + setCanDelete(LedgerIndex canDelete) = 0; /** Whether advisory delete is enabled. */ - virtual bool advisoryDelete() const = 0; + virtual bool + advisoryDelete() const = 0; /** Maximum ledger that has been deleted, or will be deleted if * currently in the act of online deletion. */ - virtual LedgerIndex getLastRotated() = 0; + virtual LedgerIndex + getLastRotated() = 0; /** Highest ledger that may be deleted. */ - virtual LedgerIndex getCanDelete() = 0; + virtual LedgerIndex + getCanDelete() = 0; /** Returns the number of file descriptors that are needed. */ - virtual int fdRequired() const = 0; + virtual int + fdRequired() const = 0; }; //------------------------------------------------------------------------------ @@ -76,6 +84,6 @@ make_SHAMapStore( Stoppable& parent, NodeStore::Scheduler& scheduler, beast::Journal journal); -} +} // namespace ripple #endif diff --git a/src/ripple/app/misc/SHAMapStoreImp.cpp b/src/ripple/app/misc/SHAMapStoreImp.cpp index 5c2644e635..46fdeeac01 100644 --- a/src/ripple/app/misc/SHAMapStoreImp.cpp +++ b/src/ripple/app/misc/SHAMapStoreImp.cpp @@ -17,7 +17,6 @@ */ //============================================================================== - #include #include #include @@ -28,63 +27,58 @@ #include namespace ripple { -void SHAMapStoreImp::SavedStateDB::init (BasicConfig const& config, - std::string const& dbName) +void +SHAMapStoreImp::SavedStateDB::init( + BasicConfig const& config, + std::string const& dbName) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); open(session_, config, dbName); session_ << "PRAGMA synchronous=FULL;"; - session_ << - "CREATE TABLE IF NOT EXISTS DbState (" - " Key INTEGER PRIMARY KEY," - " WritableDb TEXT," - " ArchiveDb TEXT," - " LastRotatedLedger INTEGER" - ");" - ; + session_ << "CREATE TABLE IF NOT EXISTS DbState (" + " Key INTEGER PRIMARY KEY," + " WritableDb TEXT," + " ArchiveDb TEXT," + " LastRotatedLedger INTEGER" + ");"; - session_ << - "CREATE TABLE IF NOT EXISTS CanDelete (" - " Key INTEGER PRIMARY KEY," - " CanDeleteSeq INTEGER" - ");" - ; + session_ << "CREATE TABLE IF NOT EXISTS CanDelete (" + " Key INTEGER PRIMARY KEY," + " CanDeleteSeq INTEGER" + ");"; std::int64_t count = 0; { boost::optional countO; - session_ << - "SELECT COUNT(Key) FROM DbState WHERE Key = 1;" - , soci::into (countO); + session_ << "SELECT COUNT(Key) FROM DbState WHERE Key = 1;", + soci::into(countO); if (!countO) - Throw ("Failed to fetch Key Count from DbState."); + Throw( + "Failed to fetch Key Count from DbState."); count = *countO; } if (!count) { - session_ << - "INSERT INTO DbState VALUES (1, '', '', 0);"; + session_ << "INSERT INTO DbState VALUES (1, '', '', 0);"; } - { boost::optional countO; - session_ << - "SELECT COUNT(Key) FROM CanDelete WHERE Key = 1;" - , soci::into (countO); + session_ << "SELECT COUNT(Key) FROM CanDelete WHERE Key = 1;", + soci::into(countO); if (!countO) - Throw ("Failed to fetch Key Count from CanDelete."); + Throw( + "Failed to fetch Key Count from CanDelete."); count = *countO; } if (!count) { - session_ << - "INSERT INTO CanDelete VALUES (1, 0);"; + session_ << "INSERT INTO CanDelete VALUES (1, 0);"; } } @@ -92,25 +86,22 @@ LedgerIndex SHAMapStoreImp::SavedStateDB::getCanDelete() { LedgerIndex seq; - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - session_ << - "SELECT CanDeleteSeq FROM CanDelete WHERE Key = 1;" - , soci::into (seq); + session_ << "SELECT CanDeleteSeq FROM CanDelete WHERE Key = 1;", + soci::into(seq); ; return seq; } LedgerIndex -SHAMapStoreImp::SavedStateDB::setCanDelete (LedgerIndex canDelete) +SHAMapStoreImp::SavedStateDB::setCanDelete(LedgerIndex canDelete) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - session_ << - "UPDATE CanDelete SET CanDeleteSeq = :canDelete WHERE Key = 1;" - , soci::use (canDelete) - ; + session_ << "UPDATE CanDelete SET CanDeleteSeq = :canDelete WHERE Key = 1;", + soci::use(canDelete); return canDelete; } @@ -120,43 +111,36 @@ SHAMapStoreImp::SavedStateDB::getState() { SavedState state; - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - session_ << - "SELECT WritableDb, ArchiveDb, LastRotatedLedger" - " FROM DbState WHERE Key = 1;" - , soci::into (state.writableDb), soci::into (state.archiveDb) - , soci::into (state.lastRotated) - ; + session_ << "SELECT WritableDb, ArchiveDb, LastRotatedLedger" + " FROM DbState WHERE Key = 1;", + soci::into(state.writableDb), soci::into(state.archiveDb), + soci::into(state.lastRotated); return state; } void -SHAMapStoreImp::SavedStateDB::setState (SavedState const& state) +SHAMapStoreImp::SavedStateDB::setState(SavedState const& state) { - std::lock_guard lock (mutex_); - session_ << - "UPDATE DbState" - " SET WritableDb = :writableDb," - " ArchiveDb = :archiveDb," - " LastRotatedLedger = :lastRotated" - " WHERE Key = 1;" - , soci::use (state.writableDb) - , soci::use (state.archiveDb) - , soci::use (state.lastRotated) - ; + std::lock_guard lock(mutex_); + session_ << "UPDATE DbState" + " SET WritableDb = :writableDb," + " ArchiveDb = :archiveDb," + " LastRotatedLedger = :lastRotated" + " WHERE Key = 1;", + soci::use(state.writableDb), soci::use(state.archiveDb), + soci::use(state.lastRotated); } void -SHAMapStoreImp::SavedStateDB::setLastRotated (LedgerIndex seq) +SHAMapStoreImp::SavedStateDB::setLastRotated(LedgerIndex seq) { - std::lock_guard lock (mutex_); - session_ << - "UPDATE DbState SET LastRotatedLedger = :seq" - " WHERE Key = 1;" - , soci::use (seq) - ; + std::lock_guard lock(mutex_); + session_ << "UPDATE DbState SET LastRotatedLedger = :seq" + " WHERE Key = 1;", + soci::use(seq); } //------------------------------------------------------------------------------ @@ -166,31 +150,30 @@ SHAMapStoreImp::SHAMapStoreImp( Stoppable& parent, NodeStore::Scheduler& scheduler, beast::Journal journal) - : SHAMapStore (parent) - , app_ (app) - , scheduler_ (scheduler) - , journal_ (journal) + : SHAMapStore(parent) + , app_(app) + , scheduler_(scheduler) + , journal_(journal) , working_(true) - , canDelete_ (std::numeric_limits ::max()) + , canDelete_(std::numeric_limits::max()) { - Config& config {app.config()}; - Section& section {config.section(ConfigSection::nodeDatabase())}; + Config& config{app.config()}; + Section& section{config.section(ConfigSection::nodeDatabase())}; if (section.empty()) { Throw( "Missing [" + ConfigSection::nodeDatabase() + "] entry in configuration file"); - } // RocksDB only. Use sensible defaults if no values specified. - if (boost::iequals( - get(section, "type"), "RocksDB")) + if (boost::iequals(get(section, "type"), "RocksDB")) { if (!section.exists("cache_mb")) { - section.set("cache_mb", std::to_string( - config.getValueFor(SizedItem::hashNodeDBCache))); + section.set( + "cache_mb", + std::to_string(config.getValueFor(SizedItem::hashNodeDBCache))); } if (!section.exists("filter_bits") && (config.NODE_SIZE >= 2)) @@ -206,19 +189,22 @@ SHAMapStoreImp::SHAMapStoreImp( { get_if_exists(section, "advisory_delete", advisoryDelete_); - auto const minInterval = config.standalone() ? - minimumDeletionIntervalSA_ : minimumDeletionInterval_; + auto const minInterval = config.standalone() + ? minimumDeletionIntervalSA_ + : minimumDeletionInterval_; if (deleteInterval_ < minInterval) { - Throw("online_delete must be at least " + - std::to_string (minInterval)); + Throw( + "online_delete must be at least " + + std::to_string(minInterval)); } if (config.LEDGER_HISTORY > deleteInterval_) { Throw( - "online_delete must not be less than ledger_history (currently " + - std::to_string (config.LEDGER_HISTORY) + ")"); + "online_delete must not be less than ledger_history " + "(currently " + + std::to_string(config.LEDGER_HISTORY) + ")"); } state_db_.init(config, dbName_); @@ -226,22 +212,22 @@ SHAMapStoreImp::SHAMapStoreImp( } } -std::unique_ptr +std::unique_ptr SHAMapStoreImp::makeNodeStore(std::string const& name, std::int32_t readThreads) { // Anything which calls addJob must be a descendant of the JobQueue. // Therefore Database objects use the JobQueue as Stoppable parent. - std::unique_ptr db; + std::unique_ptr db; if (deleteInterval_) { SavedState state = state_db_.getState(); auto writableBackend = makeBackendRotating(state.writableDb); auto archiveBackend = makeBackendRotating(state.archiveDb); - if (! state.writableDb.size()) + if (!state.writableDb.size()) { state.writableDb = writableBackend->getName(); state.archiveDb = archiveBackend->getName(); - state_db_.setState (state); + state_db_.setState(state); } // Create NodeStore with two backends to allow online deletion of data @@ -273,11 +259,10 @@ SHAMapStoreImp::makeNodeStore(std::string const& name, std::int32_t readThreads) } void -SHAMapStoreImp::onLedgerClosed( - std::shared_ptr const& ledger) +SHAMapStoreImp::onLedgerClosed(std::shared_ptr const& ledger) { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); newLedger_ = ledger; working_ = true; } @@ -290,11 +275,8 @@ SHAMapStoreImp::rendezvous() const if (!working_) return; - std::unique_lock lock(mutex_); - rendezvous_.wait(lock, [&] - { - return !working_; - }); + std::unique_lock lock(mutex_); + rendezvous_.wait(lock, [&] { return !working_; }); } int @@ -304,12 +286,13 @@ SHAMapStoreImp::fdRequired() const } bool -SHAMapStoreImp::copyNode (std::uint64_t& nodeCount, - SHAMapAbstractNode const& node) +SHAMapStoreImp::copyNode( + std::uint64_t& nodeCount, + SHAMapAbstractNode const& node) { // Copy a single record from node to dbRotating_ dbRotating_->fetch(node.getNodeHash().as_uint256(), node.getSeq()); - if (! (++nodeCount % checkHealthInterval_)) + if (!(++nodeCount % checkHealthInterval_)) { if (health()) return false; @@ -321,7 +304,7 @@ SHAMapStoreImp::copyNode (std::uint64_t& nodeCount, void SHAMapStoreImp::run() { - beast::setCurrentThreadName ("SHAMapStore"); + beast::setCurrentThreadName("SHAMapStore"); lastRotated_ = state_db_.getState().lastRotated; netOPs_ = &app_.getOPs(); ledgerMaster_ = &app_.getLedgerMaster(); @@ -331,7 +314,7 @@ SHAMapStoreImp::run() ledgerDb_ = &app_.getLedgerDB(); if (advisoryDelete_) - canDelete_ = state_db_.getCanDelete (); + canDelete_ = state_db_.getCanDelete(); while (true) { @@ -339,7 +322,7 @@ SHAMapStoreImp::run() std::shared_ptr validatedLedger; { - std::unique_lock lock (mutex_); + std::unique_lock lock(mutex_); working_ = false; rendezvous_.notify_all(); if (stop_) @@ -347,7 +330,7 @@ SHAMapStoreImp::run() stopped(); return; } - cond_.wait (lock); + cond_.wait(lock); if (newLedger_) { validatedLedger = std::move(newLedger_); @@ -360,16 +343,17 @@ SHAMapStoreImp::run() if (!lastRotated_) { lastRotated_ = validatedSeq; - state_db_.setLastRotated (lastRotated_); + state_db_.setLastRotated(lastRotated_); } // will delete up to (not including) lastRotated_ - if (validatedSeq >= lastRotated_ + deleteInterval_ - && canDelete_ >= lastRotated_ - 1) + if (validatedSeq >= lastRotated_ + deleteInterval_ && + canDelete_ >= lastRotated_ - 1) { - JLOG(journal_.warn()) << "rotating validatedSeq " << validatedSeq - << " lastRotated_ " << lastRotated_ << " deleteInterval " - << deleteInterval_ << " canDelete_ " << canDelete_; + JLOG(journal_.warn()) + << "rotating validatedSeq " << validatedSeq << " lastRotated_ " + << lastRotated_ << " deleteInterval " << deleteInterval_ + << " canDelete_ " << canDelete_; switch (health()) { @@ -379,11 +363,10 @@ SHAMapStoreImp::run() case Health::unhealthy: continue; case Health::ok: - default: - ; + default:; } - clearPrior (lastRotated_); + clearPrior(lastRotated_); switch (health()) { case Health::stopping: @@ -392,17 +375,17 @@ SHAMapStoreImp::run() case Health::unhealthy: continue; case Health::ok: - default: - ; + default:; } std::uint64_t nodeCount = 0; - validatedLedger->stateMap().snapShot ( - false)->visitNodes ( - std::bind (&SHAMapStoreImp::copyNode, this, - std::ref(nodeCount), std::placeholders::_1)); + validatedLedger->stateMap().snapShot(false)->visitNodes(std::bind( + &SHAMapStoreImp::copyNode, + this, + std::ref(nodeCount), + std::placeholders::_1)); JLOG(journal_.debug()) << "copied ledger " << validatedSeq - << " nodecount " << nodeCount; + << " nodecount " << nodeCount; switch (health()) { case Health::stopping: @@ -411,8 +394,7 @@ SHAMapStoreImp::run() case Health::unhealthy: continue; case Health::ok: - default: - ; + default:; } freshenCaches(); @@ -425,15 +407,14 @@ SHAMapStoreImp::run() case Health::unhealthy: continue; case Health::ok: - default: - ; + default:; } auto newBackend = makeBackendRotating(); - JLOG(journal_.debug()) << validatedSeq << " new backend " - << newBackend->getName(); + JLOG(journal_.debug()) + << validatedSeq << " new backend " << newBackend->getName(); - clearCaches (validatedSeq); + clearCaches(validatedSeq); switch (health()) { case Health::stopping: @@ -442,8 +423,7 @@ SHAMapStoreImp::run() case Health::unhealthy: continue; case Health::ok: - default: - ; + default:; } std::string nextArchiveDir = @@ -451,14 +431,13 @@ SHAMapStoreImp::run() lastRotated_ = validatedSeq; std::shared_ptr oldBackend; { - std::lock_guard lock (dbRotating_->peekMutex()); + std::lock_guard lock(dbRotating_->peekMutex()); - state_db_.setState (SavedState {newBackend->getName(), - nextArchiveDir, lastRotated_}); - clearCaches (validatedSeq); - oldBackend = dbRotating_->rotateBackends( - std::move(newBackend), - lock); + state_db_.setState(SavedState{ + newBackend->getName(), nextArchiveDir, lastRotated_}); + clearCaches(validatedSeq); + oldBackend = + dbRotating_->rotateBackends(std::move(newBackend), lock); } JLOG(journal_.warn()) << "finished rotation " << validatedSeq; @@ -470,35 +449,33 @@ SHAMapStoreImp::run() void SHAMapStoreImp::dbPaths() { - Section section {app_.config().section(ConfigSection::nodeDatabase())}; + Section section{app_.config().section(ConfigSection::nodeDatabase())}; boost::filesystem::path dbPath = get(section, "path"); - if (boost::filesystem::exists (dbPath)) + if (boost::filesystem::exists(dbPath)) { - if (! boost::filesystem::is_directory (dbPath)) + if (!boost::filesystem::is_directory(dbPath)) { - journal_.error() << "node db path must be a directory. " - << dbPath.string(); - Throw ( - "node db path must be a directory."); + journal_.error() + << "node db path must be a directory. " << dbPath.string(); + Throw("node db path must be a directory."); } } else { - boost::filesystem::create_directories (dbPath); + boost::filesystem::create_directories(dbPath); } SavedState state = state_db_.getState(); { - auto update = [&dbPath](std::string& sPath) - { + auto update = [&dbPath](std::string& sPath) { if (sPath.empty()) return false; // Check if configured "path" matches stored directory path using namespace boost::filesystem; - auto const stored {path(sPath)}; + auto const stored{path(sPath)}; if (stored.parent_path() == dbPath) return false; @@ -516,50 +493,51 @@ SHAMapStoreImp::dbPaths() bool writableDbExists = false; bool archiveDbExists = false; - for (boost::filesystem::directory_iterator it (dbPath); - it != boost::filesystem::directory_iterator(); ++it) + for (boost::filesystem::directory_iterator it(dbPath); + it != boost::filesystem::directory_iterator(); + ++it) { - if (! state.writableDb.compare (it->path().string())) + if (!state.writableDb.compare(it->path().string())) writableDbExists = true; - else if (! state.archiveDb.compare (it->path().string())) + else if (!state.archiveDb.compare(it->path().string())) archiveDbExists = true; - else if (! dbPrefix_.compare (it->path().stem().string())) - boost::filesystem::remove_all (it->path()); + else if (!dbPrefix_.compare(it->path().stem().string())) + boost::filesystem::remove_all(it->path()); } if ((!writableDbExists && state.writableDb.size()) || - (!archiveDbExists && state.archiveDb.size()) || - (writableDbExists != archiveDbExists) || - state.writableDb.empty() != state.archiveDb.empty()) + (!archiveDbExists && state.archiveDb.size()) || + (writableDbExists != archiveDbExists) || + state.writableDb.empty() != state.archiveDb.empty()) { boost::filesystem::path stateDbPathName = app_.config().legacy("database_path"); stateDbPathName /= dbName_; stateDbPathName += "*"; - journal_.error() << "state db error:\n" - << " writableDbExists " << writableDbExists - << " archiveDbExists " << archiveDbExists << '\n' - << " writableDb '" << state.writableDb - << "' archiveDb '" << state.archiveDb << "\n\n" - << "The existing data is in a corrupted state.\n" - << "To resume operation, remove the files matching " - << stateDbPathName.string() - << " and contents of the directory " - << get(section, "path") << '\n' - << "Optionally, you can move those files to another\n" - << "location if you wish to analyze or back up the data.\n" - << "However, there is no guarantee that the data in its\n" - << "existing form is usable."; + journal_.error() + << "state db error:\n" + << " writableDbExists " << writableDbExists << " archiveDbExists " + << archiveDbExists << '\n' + << " writableDb '" << state.writableDb << "' archiveDb '" + << state.archiveDb << "\n\n" + << "The existing data is in a corrupted state.\n" + << "To resume operation, remove the files matching " + << stateDbPathName.string() << " and contents of the directory " + << get(section, "path") << '\n' + << "Optionally, you can move those files to another\n" + << "location if you wish to analyze or back up the data.\n" + << "However, there is no guarantee that the data in its\n" + << "existing form is usable."; - Throw ("state db error"); + Throw("state db error"); } } -std::unique_ptr -SHAMapStoreImp::makeBackendRotating (std::string path) +std::unique_ptr +SHAMapStoreImp::makeBackendRotating(std::string path) { - Section section {app_.config().section(ConfigSection::nodeDatabase())}; + Section section{app_.config().section(ConfigSection::nodeDatabase())}; boost::filesystem::path newPath; if (path.size()) @@ -571,26 +549,27 @@ SHAMapStoreImp::makeBackendRotating (std::string path) boost::filesystem::path p = get(section, "path"); p /= dbPrefix_; p += ".%%%%"; - newPath = boost::filesystem::unique_path (p); + newPath = boost::filesystem::unique_path(p); } section.set("path", newPath.string()); - auto backend {NodeStore::Manager::instance().make_Backend( + auto backend{NodeStore::Manager::instance().make_Backend( section, scheduler_, app_.logs().journal(nodeStoreName_))}; backend->open(); return backend; } bool -SHAMapStoreImp::clearSql (DatabaseCon& database, - LedgerIndex lastRotated, - std::string const& minQuery, - std::string const& deleteQuery) +SHAMapStoreImp::clearSql( + DatabaseCon& database, + LedgerIndex lastRotated, + std::string const& minQuery, + std::string const& deleteQuery) { - LedgerIndex min = std::numeric_limits ::max(); + LedgerIndex min = std::numeric_limits::max(); { - auto db = database.checkoutDb (); + auto db = database.checkoutDb(); boost::optional m; *db << minQuery, soci::into(m); if (!m) @@ -598,71 +577,76 @@ SHAMapStoreImp::clearSql (DatabaseCon& database, min = *m; } - if(min > lastRotated || health() != Health::ok) + if (min > lastRotated || health() != Health::ok) return false; - boost::format formattedDeleteQuery (deleteQuery); + boost::format formattedDeleteQuery(deleteQuery); - JLOG(journal_.debug()) << - "start: " << deleteQuery << " from " << min << " to " << lastRotated; + JLOG(journal_.debug()) << "start: " << deleteQuery << " from " << min + << " to " << lastRotated; while (min < lastRotated) { min = std::min(lastRotated, min + deleteBatch_); { - auto db = database.checkoutDb (); - *db << boost::str (formattedDeleteQuery % min); + auto db = database.checkoutDb(); + *db << boost::str(formattedDeleteQuery % min); } if (health()) return true; if (min < lastRotated) - std::this_thread::sleep_for ( - std::chrono::milliseconds (backOff_)); + std::this_thread::sleep_for(std::chrono::milliseconds(backOff_)); } JLOG(journal_.debug()) << "finished: " << deleteQuery; return true; } void -SHAMapStoreImp::clearCaches (LedgerIndex validatedSeq) +SHAMapStoreImp::clearCaches(LedgerIndex validatedSeq) { - ledgerMaster_->clearLedgerCachePrior (validatedSeq); + ledgerMaster_->clearLedgerCachePrior(validatedSeq); fullBelowCache_->clear(); } void SHAMapStoreImp::freshenCaches() { - if (freshenCache (dbRotating_->getPositiveCache())) + if (freshenCache(dbRotating_->getPositiveCache())) return; - if (freshenCache (*treeNodeCache_)) + if (freshenCache(*treeNodeCache_)) return; - if (freshenCache (app_.getMasterTransaction().getCache())) + if (freshenCache(app_.getMasterTransaction().getCache())) return; } void -SHAMapStoreImp::clearPrior (LedgerIndex lastRotated) +SHAMapStoreImp::clearPrior(LedgerIndex lastRotated) { if (health()) return; - ledgerMaster_->clearPriorLedgers (lastRotated); + ledgerMaster_->clearPriorLedgers(lastRotated); if (health()) return; - clearSql (*ledgerDb_, lastRotated, + clearSql( + *ledgerDb_, + lastRotated, "SELECT MIN(LedgerSeq) FROM Ledgers;", "DELETE FROM Ledgers WHERE LedgerSeq < %u;"); if (health()) return; - clearSql (*transactionDb_, lastRotated, + clearSql( + *transactionDb_, + lastRotated, "SELECT MIN(LedgerSeq) FROM Transactions;", "DELETE FROM Transactions WHERE LedgerSeq < %u;"); if (health()) return; - clearSql (*transactionDb_, lastRotated, + clearSql( + *transactionDb_, + lastRotated, "SELECT MIN(LedgerSeq) FROM AccountTransactions;", "DELETE FROM AccountTransactions WHERE LedgerSeq < %u;"); if (health()) @@ -673,11 +657,11 @@ SHAMapStoreImp::Health SHAMapStoreImp::health() { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); if (stop_) return Health::stopping; } - if (! netOPs_) + if (!netOPs_) return Health::ok; constexpr static std::chrono::seconds age_threshold(60); @@ -703,7 +687,7 @@ SHAMapStoreImp::onStop() if (deleteInterval_) { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); stop_ = true; } cond_.notify_one(); @@ -720,7 +704,7 @@ SHAMapStoreImp::onChildrenStopped() if (deleteInterval_) { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); stop_ = true; } cond_.notify_one(); @@ -743,4 +727,4 @@ make_SHAMapStore( return std::make_unique(app, parent, scheduler, journal); } -} +} // namespace ripple diff --git a/src/ripple/app/misc/SHAMapStoreImp.h b/src/ripple/app/misc/SHAMapStoreImp.h index 5c8454b356..619e202d33 100644 --- a/src/ripple/app/misc/SHAMapStoreImp.h +++ b/src/ripple/app/misc/SHAMapStoreImp.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED #define RIPPLE_APP_MISC_SHAMAPSTOREIMP_H_INCLUDED -#include #include +#include #include #include @@ -43,12 +43,7 @@ private: LedgerIndex lastRotated; }; - enum Health : std::uint8_t - { - ok = 0, - stopping, - unhealthy - }; + enum Health : std::uint8_t { ok = 0, stopping, unhealthy }; class SavedStateDB { @@ -59,18 +54,24 @@ private: // Just instantiate without any logic in case online delete is not // configured - explicit SavedStateDB() - : journal_ {beast::Journal::getNullSink()} - { } + explicit SavedStateDB() : journal_{beast::Journal::getNullSink()} + { + } // opens database and, if necessary, creates & initializes its tables. - void init (BasicConfig const& config, std::string const& dbName); + void + init(BasicConfig const& config, std::string const& dbName); // get/set the ledger index that we can delete up to and including - LedgerIndex getCanDelete(); - LedgerIndex setCanDelete (LedgerIndex canDelete); - SavedState getState(); - void setState (SavedState const& state); - void setLastRotated (LedgerIndex seq); + LedgerIndex + getCanDelete(); + LedgerIndex + setCanDelete(LedgerIndex canDelete); + SavedState + getState(); + void + setState(SavedState const& state); + void + setLastRotated(LedgerIndex seq); }; Application& app_; @@ -91,7 +92,7 @@ private: // not including) this value. All ledgers past this value are accumulated // until the next online deletion. This value is persisted to SQLite // nearly immediately after modification. - std::atomic lastRotated_ {}; + std::atomic lastRotated_{}; NodeStore::Scheduler& scheduler_; beast::Journal const journal_; @@ -105,7 +106,7 @@ private: mutable std::mutex mutex_; std::shared_ptr newLedger_; std::atomic working_; - std::atomic canDelete_; + std::atomic canDelete_; int fdRequired_ = 0; std::uint32_t deleteInterval_ = 0; @@ -139,21 +140,21 @@ public: } std::uint32_t - clampFetchDepth (std::uint32_t fetch_depth) const override + clampFetchDepth(std::uint32_t fetch_depth) const override { - return deleteInterval_ ? std::min (fetch_depth, - deleteInterval_) : fetch_depth; + return deleteInterval_ ? std::min(fetch_depth, deleteInterval_) + : fetch_depth; } - std::unique_ptr + std::unique_ptr makeNodeStore(std::string const& name, std::int32_t readThreads) override; LedgerIndex - setCanDelete (LedgerIndex seq) override + setCanDelete(LedgerIndex seq) override { if (advisoryDelete_) canDelete_ = seq; - return state_db_.setCanDelete (seq); + return state_db_.setCanDelete(seq); } bool @@ -178,30 +179,36 @@ public: return canDelete_; } - void onLedgerClosed (std::shared_ptr const& ledger) override; + void + onLedgerClosed(std::shared_ptr const& ledger) override; - void rendezvous() const override; - int fdRequired() const override; + void + rendezvous() const override; + int + fdRequired() const override; private: // callback for visitNodes - bool copyNode (std::uint64_t& nodeCount, SHAMapAbstractNode const &node); - void run(); - void dbPaths(); + bool + copyNode(std::uint64_t& nodeCount, SHAMapAbstractNode const& node); + void + run(); + void + dbPaths(); std::unique_ptr - makeBackendRotating (std::string path = std::string()); + makeBackendRotating(std::string path = std::string()); template bool - freshenCache (CacheInstance& cache) + freshenCache(CacheInstance& cache) { std::uint64_t check = 0; - for (auto const& key: cache.getKeys()) + for (auto const& key : cache.getKeys()) { dbRotating_->fetch(key, 0); - if (! (++check % checkHealthInterval_) && health()) + if (!(++check % checkHealthInterval_) && health()) return true; } @@ -214,18 +221,26 @@ private: * @return true if any deletable rows were found (though not * necessarily deleted. */ - bool clearSql (DatabaseCon& database, LedgerIndex lastRotated, - std::string const& minQuery, std::string const& deleteQuery); - void clearCaches (LedgerIndex validatedSeq); - void freshenCaches(); - void clearPrior (LedgerIndex lastRotated); + bool + clearSql( + DatabaseCon& database, + LedgerIndex lastRotated, + std::string const& minQuery, + std::string const& deleteQuery); + void + clearCaches(LedgerIndex validatedSeq); + void + freshenCaches(); + void + clearPrior(LedgerIndex lastRotated); // If rippled is not healthy, defer rotate-delete. // If already unhealthy, do not change state on further check. // Assume that, once unhealthy, a necessary step has been // aborted, so the online-delete process needs to restart // at next ledger. - Health health(); + Health + health(); // // Stoppable // @@ -238,16 +253,17 @@ private: onStart() override { if (deleteInterval_) - thread_ = std::thread (&SHAMapStoreImp::run, this); + thread_ = std::thread(&SHAMapStoreImp::run, this); } // Called when the application begins shutdown - void onStop() override; + void + onStop() override; // Called when all child Stoppable objects have stoped - void onChildrenStopped() override; - + void + onChildrenStopped() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/misc/Transaction.h b/src/ripple/app/misc/Transaction.h index c829f69862..455bc4fae8 100644 --- a/src/ripple/app/misc/Transaction.h +++ b/src/ripple/app/misc/Transaction.h @@ -20,12 +20,12 @@ #ifndef RIPPLE_APP_MISC_TRANSACTION_H_INCLUDED #define RIPPLE_APP_MISC_TRANSACTION_H_INCLUDED +#include +#include #include #include #include #include -#include -#include #include #include @@ -40,84 +40,95 @@ class Application; class Database; class Rules; -enum TransStatus -{ - NEW = 0, // just received / generated - INVALID = 1, // no valid signature, insufficient funds - INCLUDED = 2, // added to the current ledger - CONFLICTED = 3, // losing to a conflicting transaction - COMMITTED = 4, // known to be in a ledger - HELD = 5, // not valid now, maybe later - REMOVED = 6, // taken out of a ledger - OBSOLETE = 7, // a compatible transaction has taken precedence - INCOMPLETE = 8 // needs more signatures +enum TransStatus { + NEW = 0, // just received / generated + INVALID = 1, // no valid signature, insufficient funds + INCLUDED = 2, // added to the current ledger + CONFLICTED = 3, // losing to a conflicting transaction + COMMITTED = 4, // known to be in a ledger + HELD = 5, // not valid now, maybe later + REMOVED = 6, // taken out of a ledger + OBSOLETE = 7, // a compatible transaction has taken precedence + INCOMPLETE = 8 // needs more signatures }; // This class is for constructing and examining transactions. // Transactions are static so manipulation functions are unnecessary. -class Transaction - : public std::enable_shared_from_this - , public CountedObject +class Transaction : public std::enable_shared_from_this, + public CountedObject { public: - static char const* getCountedObjectName () { return "Transaction"; } + static char const* + getCountedObjectName() + { + return "Transaction"; + } using pointer = std::shared_ptr; using ref = const pointer&; - Transaction ( - std::shared_ptr const&, std::string&, Application&) noexcept; + Transaction( + std::shared_ptr const&, + std::string&, + Application&) noexcept; - static - Transaction::pointer - transactionFromSQL ( + static Transaction::pointer + transactionFromSQL( boost::optional const& ledgerSeq, boost::optional const& status, Blob const& rawTxn, Application& app); - static - TransStatus + static TransStatus sqlTransactionStatus(boost::optional const& status); - std::shared_ptr const& getSTransaction () + std::shared_ptr const& + getSTransaction() { return mTransaction; } - uint256 const& getID () const + uint256 const& + getID() const { return mTransactionID; } - LedgerIndex getLedger () const + LedgerIndex + getLedger() const { return mInLedger; } - TransStatus getStatus () const + TransStatus + getStatus() const { return mStatus; } - TER getResult () + TER + getResult() { return mResult; } - void setResult (TER terResult) + void + setResult(TER terResult) { mResult = terResult; } - void setStatus (TransStatus status, std::uint32_t ledgerSeq); + void + setStatus(TransStatus status, std::uint32_t ledgerSeq); - void setStatus (TransStatus status) + void + setStatus(TransStatus status) { mStatus = status; } - void setLedger (LedgerIndex ledger) + void + setLedger(LedgerIndex ledger) { mInLedger = ledger; } @@ -125,7 +136,8 @@ public: /** * Set this flag once added to a batch. */ - void setApplying() + void + setApplying() { mApplying = true; } @@ -135,7 +147,8 @@ public: * * @return Whether transaction is being applied within a batch. */ - bool getApplying() + bool + getApplying() { return mApplying; } @@ -143,7 +156,8 @@ public: /** * Indicate that transaction application has been attempted. */ - void clearApplying() + void + clearApplying() { mApplying = false; } @@ -153,7 +167,8 @@ public: /** * @brief clear Clear all states */ - void clear() + void + clear() { applied = false; broadcast = false; @@ -165,7 +180,8 @@ public: * @brief any Get true of any state is true * @return True if any state if true */ - bool any() const + bool + any() const { return applied || broadcast || queued || kept; } @@ -180,7 +196,8 @@ public: * @brief getSubmitResult Return submit result * @return SubmitResult struct */ - SubmitResult getSubmitResult() const + SubmitResult + getSubmitResult() const { return submitResult_; } @@ -188,7 +205,8 @@ public: /** * @brief clearSubmitResult Clear all flags in SubmitResult */ - void clearSubmitResult() + void + clearSubmitResult() { submitResult_.clear(); } @@ -196,7 +214,8 @@ public: /** * @brief setApplied Set this flag once was applied to open ledger */ - void setApplied() + void + setApplied() { submitResult_.applied = true; } @@ -204,7 +223,8 @@ public: /** * @brief setQueued Set this flag once was put into heldtxns queue */ - void setQueued() + void + setQueued() { submitResult_.queued = true; } @@ -212,7 +232,8 @@ public: /** * @brief setBroadcast Set this flag once was broadcasted via network */ - void setBroadcast() + void + setBroadcast() { submitResult_.broadcast = true; } @@ -220,7 +241,8 @@ public: /** * @brief setKept Set this flag once was put to localtxns queue */ - void setKept() + void + setKept() { submitResult_.kept = true; } @@ -275,37 +297,44 @@ public: validatedLedger, fee, accountSeq, availableSeq); } - Json::Value getJson (JsonOptions options, bool binary = false) const; + Json::Value + getJson(JsonOptions options, bool binary = false) const; static pointer - load (uint256 const& id, Application& app, error_code_i& ec); + load(uint256 const& id, Application& app, error_code_i& ec); static boost::variant - load (uint256 const& id, Application& app, ClosedInterval const& range, error_code_i& ec); - -private: - - static boost::variant - load (uint256 const& id, Application& app, boost::optional> const& range, + load( + uint256 const& id, + Application& app, + ClosedInterval const& range, error_code_i& ec); - uint256 mTransactionID; +private: + static boost::variant + load( + uint256 const& id, + Application& app, + boost::optional> const& range, + error_code_i& ec); - LedgerIndex mInLedger = 0; - TransStatus mStatus = INVALID; - TER mResult = temUNCERTAIN; - bool mApplying = false; + uint256 mTransactionID; + + LedgerIndex mInLedger = 0; + TransStatus mStatus = INVALID; + TER mResult = temUNCERTAIN; + bool mApplying = false; /** different ways for transaction to be accepted */ - SubmitResult submitResult_; + SubmitResult submitResult_; boost::optional currentLedgerState_; - std::shared_ptr mTransaction; - Application& mApp; - beast::Journal j_; + std::shared_ptr mTransaction; + Application& mApp; + beast::Journal j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/TxQ.h b/src/ripple/app/misc/TxQ.h index a3e56bb0e2..4bdf3e23ee 100644 --- a/src/ripple/app/misc/TxQ.h +++ b/src/ripple/app/misc/TxQ.h @@ -21,12 +21,12 @@ #define RIPPLE_TXQ_H_INCLUDED #include -#include #include -#include +#include #include -#include +#include #include +#include namespace ripple { @@ -55,7 +55,7 @@ class TxQ { public: /// Fee level for single-signed reference transaction. - static constexpr FeeLevel64 baseLevel{ 256 }; + static constexpr FeeLevel64 baseLevel{256}; /** Structure used to customize @ref TxQ behavior. @@ -168,7 +168,7 @@ public: we can make this more complicated. But avoid bikeshedding for now. */ - FeeLevel64 zeroBaseFeeTransactionFeeLevel{ 256000 }; + FeeLevel64 zeroBaseFeeTransactionFeeLevel{256000}; /// Use standalone mode behavior. bool standAlone = false; }; @@ -264,8 +264,7 @@ public: }; /// Constructor - TxQ(Setup const& setup, - beast::Journal j); + TxQ(Setup const& setup, beast::Journal j); /// Destructor virtual ~TxQ(); @@ -280,9 +279,12 @@ public: will return `{ terQUEUED, false }`. */ std::pair - apply(Application& app, OpenView& view, + apply( + Application& app, + OpenView& view, std::shared_ptr const& tx, - ApplyFlags flags, beast::Journal j); + ApplyFlags flags, + beast::Journal j); /** Fill the new open ledger with transactions from the queue. @@ -311,11 +313,10 @@ public: that have no candidates under them are removed. */ void - processClosedLedger(Application& app, - ReadView const& view, bool timeLeap); + processClosedLedger(Application& app, ReadView const& view, bool timeLeap); /** Returns fee metrics in reference fee level units. - */ + */ Metrics getMetrics(OpenView const& view) const; @@ -336,7 +337,8 @@ public: * and first available sequence */ FeeAndSeq - getTxRequiredFeeAndSeq(OpenView const& view, + getTxRequiredFeeAndSeq( + OpenView const& view, std::shared_ptr const& tx) const; /** Returns information about the transactions currently @@ -396,15 +398,19 @@ private: public: /// Constructor FeeMetrics(Setup const& setup, beast::Journal j) - : minimumTxnCount_(setup.standAlone ? - setup.minimumTxnInLedgerSA : - setup.minimumTxnInLedger) - , targetTxnCount_(setup.targetTxnInLedger < minimumTxnCount_ ? - minimumTxnCount_ : setup.targetTxnInLedger) - , maximumTxnCount_(setup.maximumTxnInLedger ? - *setup.maximumTxnInLedger < targetTxnCount_ ? - targetTxnCount_ : *setup.maximumTxnInLedger : - boost::optional(boost::none)) + : minimumTxnCount_( + setup.standAlone ? setup.minimumTxnInLedgerSA + : setup.minimumTxnInLedger) + , targetTxnCount_( + setup.targetTxnInLedger < minimumTxnCount_ + ? minimumTxnCount_ + : setup.targetTxnInLedger) + , maximumTxnCount_( + setup.maximumTxnInLedger + ? *setup.maximumTxnInLedger < targetTxnCount_ + ? targetTxnCount_ + : *setup.maximumTxnInLedger + : boost::optional(boost::none)) , txnsExpected_(minimumTxnCount_) , recentTxnCounts_(setup.ledgersInQueue) , escalationMultiplier_(setup.minimumEscalationMultiplier) @@ -423,8 +429,10 @@ private: @param setup Customization params. */ std::size_t - update(Application& app, - ReadView const& view, bool timeLeap, + update( + Application& app, + ReadView const& view, + bool timeLeap, TxQ::Setup const& setup); /// Snapshot of the externally relevant FeeMetrics @@ -444,10 +452,7 @@ private: Snapshot getSnapshot() const { - return { - txnsExpected_, - escalationMultiplier_ - }; + return {txnsExpected_, escalationMultiplier_}; } /** Use the number of transactions in the current open ledger @@ -458,8 +463,7 @@ private: @return A fee level value. */ - static - FeeLevel64 + static FeeLevel64 scaleFeeLevel(Snapshot const& snapshot, OpenView const& view); /** @@ -492,10 +496,12 @@ private: @return A `std::pair` as returned from @ref `mulDiv` indicating whether the calculation result overflows. */ - static - std::pair - escalatedSeriesFeeLevel(Snapshot const& snapshot, OpenView const& view, - std::size_t extraCount, std::size_t seriesSize); + static std::pair + escalatedSeriesFeeLevel( + Snapshot const& snapshot, + OpenView const& view, + std::size_t extraCount, + std::size_t seriesSize); }; /** @@ -578,10 +584,12 @@ private: public: /// Constructor - MaybeTx(std::shared_ptr const&, - TxID const& txID, FeeLevel64 feeLevel, - ApplyFlags const flags, - PreflightResult const& pfresult); + MaybeTx( + std::shared_ptr const&, + TxID const& txID, + FeeLevel64 feeLevel, + ApplyFlags const flags, + PreflightResult const& pfresult); /// Attempt to apply the queued transaction to the open ledger. std::pair @@ -596,7 +604,8 @@ private: explicit GreaterFee() = default; /// Is the fee level of `lhs` greater than the fee level of `rhs`? - bool operator()(const MaybeTx& lhs, const MaybeTx& rhs) const + bool + operator()(const MaybeTx& lhs, const MaybeTx& rhs) const { return lhs.feeLevel > rhs.feeLevel; } @@ -608,7 +617,7 @@ private: class TxQAccount { public: - using TxMap = std::map ; + using TxMap = std::map; /// The account AccountID const account; @@ -662,15 +671,15 @@ private: remove(TxSeq const& sequence); }; - using FeeHook = boost::intrusive::member_hook - , + using FeeHook = boost::intrusive::member_hook< + MaybeTx, + boost::intrusive::set_member_hook<>, &MaybeTx::byFeeListHook>; - using FeeMultiSet = boost::intrusive::multiset - < MaybeTx, FeeHook, - boost::intrusive::compare >; + using FeeMultiSet = boost::intrusive:: + multiset>; - using AccountMap = std::map ; + using AccountMap = std::map; /// Setup parameters used to control the behavior of the queue Setup const setup_; @@ -710,16 +719,20 @@ private: private: /// Is the queue at least `fillPercentage` full? - template + template bool isFull() const; /** Checks if the indicated transaction fits the conditions for being stored in the queue. */ - bool canBeHeld(STTx const&, ApplyFlags const, OpenView const&, + bool + canBeHeld( + STTx const&, + ApplyFlags const, + OpenView const&, AccountMap::iterator, - boost::optional); + boost::optional); /// Erase and return the next entry in byFee_ (lower fee level) FeeMultiSet::iterator_type erase(FeeMultiSet::const_iterator_type); @@ -727,25 +740,32 @@ private: is higher), or next entry in byFee_ (lower fee level). Used to get the next "applyable" MaybeTx for accept(). */ - FeeMultiSet::iterator_type eraseAndAdvance(FeeMultiSet::const_iterator_type); + FeeMultiSet::iterator_type eraseAndAdvance( + FeeMultiSet::const_iterator_type); /// Erase a range of items, based on TxQAccount::TxMap iterators TxQAccount::TxMap::iterator - erase(TxQAccount& txQAccount, TxQAccount::TxMap::const_iterator begin, + erase( + TxQAccount& txQAccount, + TxQAccount::TxMap::const_iterator begin, TxQAccount::TxMap::const_iterator end); /** - All-or-nothing attempt to try to apply all the queued txs for `accountIter` - up to and including `tx`. + All-or-nothing attempt to try to apply all the queued txs for + `accountIter` up to and including `tx`. */ std::pair - tryClearAccountQueue(Application& app, OpenView& view, - STTx const& tx, AccountMap::iterator const& accountIter, - TxQAccount::TxMap::iterator, FeeLevel64 feeLevelPaid, - PreflightResult const& pfresult, - std::size_t const txExtraCount, ApplyFlags flags, - FeeMetrics::Snapshot const& metricsSnapshot, - beast::Journal j); - + tryClearAccountQueue( + Application& app, + OpenView& view, + STTx const& tx, + AccountMap::iterator const& accountIter, + TxQAccount::TxMap::iterator, + FeeLevel64 feeLevelPaid, + PreflightResult const& pfresult, + std::size_t const txExtraCount, + ApplyFlags flags, + FeeMetrics::Snapshot const& metricsSnapshot, + beast::Journal j); }; /** @@ -760,20 +780,19 @@ setup_TxQ(Config const&); std::unique_ptr make_TxQ(TxQ::Setup const&, beast::Journal); -template +template std::pair toDrops(FeeLevel const& level, XRPAmount const& baseFee) { return mulDiv(level, baseFee, TxQ::baseLevel); } -inline -std::pair +inline std::pair toFeeLevel(XRPAmount const& drops, XRPAmount const& baseFee) { return mulDiv(drops, TxQ::baseLevel, baseFee); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/ValidatorKeys.h b/src/ripple/app/misc/ValidatorKeys.h index 96af105624..b6f4b992ec 100644 --- a/src/ripple/app/misc/ValidatorKeys.h +++ b/src/ripple/app/misc/ValidatorKeys.h @@ -43,13 +43,14 @@ public: ValidatorKeys(Config const& config, beast::Journal j); - bool configInvalid() const + bool + configInvalid() const { return configInvalid_; } private: - bool configInvalid_ = false; //< Set to true if config was invalid + bool configInvalid_ = false; //< Set to true if config was invalid }; } // namespace ripple diff --git a/src/ripple/app/misc/ValidatorList.h b/src/ripple/app/misc/ValidatorList.h index d8c4fd7b95..c47119eb75 100644 --- a/src/ripple/app/misc/ValidatorList.h +++ b/src/ripple/app/misc/ValidatorList.h @@ -30,8 +30,8 @@ #include #include #include -#include #include +#include namespace ripple { @@ -39,8 +39,7 @@ namespace ripple { class Overlay; class HashRouter; -enum class ListDisposition -{ +enum class ListDisposition { /// List is valid accepted = 0, @@ -64,7 +63,7 @@ std::string to_string(ListDisposition disposition); /** Changes in trusted nodes after updating validator list -*/ + */ struct TrustChanges { explicit TrustChanges() = default; @@ -165,14 +164,14 @@ class ValidatorList static const std::string filePrefix_; public: - ValidatorList ( + ValidatorList( ManifestCache& validatorManifests, ManifestCache& publisherManifests, TimeKeeper& timeKeeper, std::string const& databasePath, beast::Journal j, boost::optional minimumQuorum = boost::none); - ~ValidatorList () = default; + ~ValidatorList() = default; /** Describes the result of processing a Validator List (UNL), including some of the information from the list which can @@ -181,17 +180,16 @@ public: */ struct PublisherListStats { - explicit PublisherListStats(ListDisposition d) - : disposition(d) + explicit PublisherListStats(ListDisposition d) : disposition(d) { } - PublisherListStats(ListDisposition d, PublicKey key, - bool avail, std::size_t seq) - : disposition(d) - , publisherKey(key) - , available(avail) - , sequence(seq) + PublisherListStats( + ListDisposition d, + PublicKey key, + bool avail, + std::size_t seq) + : disposition(d), publisherKey(key), available(avail), sequence(seq) { } @@ -219,7 +217,7 @@ public: @return `false` if an entry is invalid or unparsable */ bool - load ( + load( PublicKey const& localSigningKey, std::vector const& configKeys, std::vector const& publisherKeys); @@ -252,7 +250,7 @@ public: May be called concurrently */ PublisherListStats - applyListAndBroadcast ( + applyListAndBroadcast( std::string const& manifest, std::string const& blob, std::string const& signature, @@ -285,7 +283,7 @@ public: May be called concurrently */ PublisherListStats - applyList ( + applyList( std::string const& manifest, std::string const& blob, std::string const& signature, @@ -321,7 +319,7 @@ public: May be called concurrently */ TrustChanges - updateTrusted (hash_set const& seenValidators); + updateTrusted(hash_set const& seenValidators); /** Get quorum value for current trusted key set @@ -337,7 +335,7 @@ public: @return quorum value */ std::size_t - quorum () const + quorum() const { return quorum_; } @@ -351,8 +349,7 @@ public: May be called concurrently */ bool - trusted ( - PublicKey const& identity) const; + trusted(PublicKey const& identity) const; /** Returns `true` if public key is included on any lists @@ -363,8 +360,7 @@ public: May be called concurrently */ bool - listed ( - PublicKey const& identity) const; + listed(PublicKey const& identity) const; /** Returns master public key if public key is trusted @@ -377,8 +373,7 @@ public: May be called concurrently */ boost::optional - getTrustedKey ( - PublicKey const& identity) const; + getTrustedKey(PublicKey const& identity) const; /** Returns listed master public if public key is included on any lists @@ -391,8 +386,7 @@ public: May be called concurrently */ boost::optional - getListedKey ( - PublicKey const& identity) const; + getListedKey(PublicKey const& identity) const; /** Returns `true` if public key is a trusted publisher @@ -403,8 +397,7 @@ public: May be called concurrently */ bool - trustedPublisher ( - PublicKey const& identity) const; + trustedPublisher(PublicKey const& identity) const; /** Returns local validator public key @@ -413,7 +406,7 @@ public: May be called concurrently */ PublicKey - localPublicKey () const; + localPublicKey() const; /** Invokes the callback once for every listed validation public key. @@ -431,8 +424,7 @@ public: May be called concurrently */ void - for_each_listed ( - std::function func) const; + for_each_listed(std::function func) const; /** Invokes the callback once for every available publisher list's raw data members @@ -462,12 +454,14 @@ public: May be called concurrently */ void - for_each_available ( - std::function func) const; + for_each_available(std::function func) const; /** Returns the current valid list for the given publisher key, if available, as a Json object. @@ -511,18 +505,16 @@ public: return {quorum_, trustedSigningKeys_}; } - private: /** Get the filename used for caching UNLs - */ + */ boost::filesystem::path GetCacheFileName(PublicKey const& pubKey); /** Write a JSON UNL to a cache file - */ + */ void - CacheValidatorFile(PublicKey const& pubKey, - PublisherList const& publisher); + CacheValidatorFile(PublicKey const& pubKey, PublisherList const& publisher); /** Check response for trusted valid published list @@ -533,7 +525,7 @@ private: Calling public member function is expected to lock mutex */ ListDisposition - verify ( + verify( Json::Value& list, PublicKey& pubKey, std::string const& manifest, @@ -551,7 +543,7 @@ private: Calling public member function is expected to lock mutex */ bool - removePublisherList (PublicKey const& publisherKey); + removePublisherList(PublicKey const& publisherKey); /** Return quorum for trusted validator set @@ -560,9 +552,8 @@ private: @param seen Number of trusted validators that have signed recently received validations */ std::size_t - calculateQuorum ( - std::size_t trusted, std::size_t seen); + calculateQuorum(std::size_t trusted, std::size_t seen); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/ValidatorSite.h b/src/ripple/app/misc/ValidatorSite.h index 490c26ffb0..9adb88f05f 100644 --- a/src/ripple/app/misc/ValidatorSite.h +++ b/src/ripple/app/misc/ValidatorSite.h @@ -20,17 +20,17 @@ #ifndef RIPPLE_APP_MISC_VALIDATORSITE_H_INCLUDED #define RIPPLE_APP_MISC_VALIDATORSITE_H_INCLUDED +#include #include #include -#include #include #include #include #include #include #include -#include #include +#include namespace ripple { @@ -133,11 +133,11 @@ private: const std::chrono::seconds requestTimeout_; public: - ValidatorSite ( + ValidatorSite( Application& app, boost::optional j = boost::none, std::chrono::seconds timeout = std::chrono::seconds{20}); - ~ValidatorSite (); + ~ValidatorSite(); /** Load configured site URIs. @@ -150,8 +150,7 @@ public: @return `false` if an entry is invalid or unparsable */ bool - load ( - std::vector const& siteURIs); + load(std::vector const& siteURIs); /** Start fetching lists from sites @@ -162,7 +161,7 @@ public: May be called concurrently */ void - start (); + start(); /** Wait for current fetches from sites to complete @@ -171,7 +170,7 @@ public: May be called concurrently */ void - join (); + join(); /** Stop fetching lists from sites @@ -182,7 +181,7 @@ public: May be called concurrently */ void - stop (); + stop(); /** Return JSON representation of configured validator sites */ @@ -193,23 +192,19 @@ private: /// Queue next site to be fetched /// lock over state_mutex_ required void - setTimer (std::lock_guard&); + setTimer(std::lock_guard&); /// request took too long void - onRequestTimeout ( - std::size_t siteIdx, - error_code const& ec); + onRequestTimeout(std::size_t siteIdx, error_code const& ec); /// Fetch site whose time has come void - onTimer ( - std::size_t siteIdx, - error_code const& ec); + onTimer(std::size_t siteIdx, error_code const& ec); /// Store latest list fetched from site void - onSiteFetch ( + onSiteFetch( boost::system::error_code const& ec, detail::response_type&& res, std::size_t siteIdx); @@ -224,7 +219,7 @@ private: /// Initiate request to given resource. /// lock over sites_mutex_ required void - makeRequest ( + makeRequest( std::shared_ptr resource, std::size_t siteIdx, std::lock_guard& lock); @@ -232,7 +227,7 @@ private: /// Parse json response from validator list site. /// lock over sites_mutex_ required void - parseJsonResponse ( + parseJsonResponse( std::string const& res, std::size_t siteIdx, std::lock_guard& lock); @@ -240,7 +235,7 @@ private: /// Interpret a redirect response. /// lock over sites_mutex_ required std::shared_ptr - processRedirect ( + processRedirect( detail::response_type& res, std::size_t siteIdx, std::lock_guard& lock); @@ -251,6 +246,6 @@ private: missingSite(); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/detail/Work.h b/src/ripple/app/misc/detail/Work.h index f4f4b556b9..15be569566 100644 --- a/src/ripple/app/misc/detail/Work.h +++ b/src/ripple/app/misc/detail/Work.h @@ -35,13 +35,15 @@ class Work public: virtual ~Work() = default; - virtual void run() = 0; + virtual void + run() = 0; - virtual void cancel() = 0; + virtual void + cancel() = 0; }; -} // detail +} // namespace detail -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/detail/WorkBase.h b/src/ripple/app/misc/detail/WorkBase.h index 4ab2f49bfa..35ee573d43 100644 --- a/src/ripple/app/misc/detail/WorkBase.h +++ b/src/ripple/app/misc/detail/WorkBase.h @@ -22,19 +22,18 @@ #include #include +#include #include #include #include #include -#include namespace ripple { namespace detail { template -class WorkBase - : public Work +class WorkBase : public Work { protected: using error_code = boost::system::error_code; @@ -42,6 +41,7 @@ protected: public: using callback_type = std::function; + protected: using socket_type = boost::asio::ip::tcp::socket; using endpoint_type = boost::asio::ip::tcp::endpoint; @@ -64,9 +64,11 @@ protected: public: WorkBase( - std::string const& host, std::string const& path, + std::string const& host, + std::string const& path, std::string const& port, - boost::asio::io_service& ios, callback_type cb); + boost::asio::io_service& ios, + callback_type cb); ~WorkBase(); Impl& @@ -75,9 +77,11 @@ public: return *static_cast(this); } - void run() override; + void + run() override; - void cancel() override; + void + cancel() override; void fail(error_code const& ec); @@ -97,10 +101,13 @@ public: //------------------------------------------------------------------------------ -template -WorkBase::WorkBase(std::string const& host, - std::string const& path, std::string const& port, - boost::asio::io_service& ios, callback_type cb) +template +WorkBase::WorkBase( + std::string const& host, + std::string const& path, + std::string const& port, + boost::asio::io_service& ios, + callback_type cb) : host_(host) , path_(path) , port_(port) @@ -112,45 +119,47 @@ WorkBase::WorkBase(std::string const& host, { } -template +template WorkBase::~WorkBase() { if (cb_) - cb_ (make_error_code(boost::system::errc::not_a_socket), + cb_(make_error_code(boost::system::errc::not_a_socket), std::move(res_)); } -template +template void WorkBase::run() { - if (! strand_.running_in_this_thread()) - return ios_.post(strand_.wrap (std::bind( - &WorkBase::run, impl().shared_from_this()))); + if (!strand_.running_in_this_thread()) + return ios_.post( + strand_.wrap(std::bind(&WorkBase::run, impl().shared_from_this()))); resolver_.async_resolve( query_type{host_, port_}, - strand_.wrap (std::bind(&WorkBase::onResolve, impl().shared_from_this(), + strand_.wrap(std::bind( + &WorkBase::onResolve, + impl().shared_from_this(), std::placeholders::_1, - std::placeholders::_2))); + std::placeholders::_2))); } -template +template void WorkBase::cancel() { - if (! strand_.running_in_this_thread()) + if (!strand_.running_in_this_thread()) { - return ios_.post(strand_.wrap (std::bind( - &WorkBase::cancel, impl().shared_from_this()))); + return ios_.post(strand_.wrap( + std::bind(&WorkBase::cancel, impl().shared_from_this()))); } error_code ec; resolver_.cancel(); - socket_.cancel (ec); + socket_.cancel(ec); } -template +template void WorkBase::fail(error_code const& ec) { @@ -161,47 +170,58 @@ WorkBase::fail(error_code const& ec) } } -template +template void WorkBase::onResolve(error_code const& ec, resolver_type::iterator it) { if (ec) return fail(ec); - socket_.async_connect(*it, - strand_.wrap (std::bind(&Impl::onConnect, impl().shared_from_this(), + socket_.async_connect( + *it, + strand_.wrap(std::bind( + &Impl::onConnect, + impl().shared_from_this(), std::placeholders::_1))); } -template +template void WorkBase::onStart() { req_.method(boost::beast::http::verb::get); req_.target(path_.empty() ? "/" : path_); req_.version(11); - req_.set ( - "Host", host_ + ":" + port_); - req_.set ("User-Agent", BuildInfo::getFullVersionString()); + req_.set("Host", host_ + ":" + port_); + req_.set("User-Agent", BuildInfo::getFullVersionString()); req_.prepare_payload(); - boost::beast::http::async_write(impl().stream(), req_, - strand_.wrap (std::bind (&WorkBase::onRequest, - impl().shared_from_this(), std::placeholders::_1))); + boost::beast::http::async_write( + impl().stream(), + req_, + strand_.wrap(std::bind( + &WorkBase::onRequest, + impl().shared_from_this(), + std::placeholders::_1))); } -template +template void WorkBase::onRequest(error_code const& ec) { if (ec) return fail(ec); - boost::beast::http::async_read (impl().stream(), read_buf_, res_, - strand_.wrap (std::bind (&WorkBase::onResponse, - impl().shared_from_this(), std::placeholders::_1))); + boost::beast::http::async_read( + impl().stream(), + read_buf_, + res_, + strand_.wrap(std::bind( + &WorkBase::onResponse, + impl().shared_from_this(), + std::placeholders::_1))); } -template +template void WorkBase::onResponse(error_code const& ec) { @@ -213,8 +233,8 @@ WorkBase::onResponse(error_code const& ec) cb_ = nullptr; } -} // detail +} // namespace detail -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/detail/WorkFile.h b/src/ripple/app/misc/detail/WorkFile.h index 274e31fb85..2dc451feac 100644 --- a/src/ripple/app/misc/detail/WorkFile.h +++ b/src/ripple/app/misc/detail/WorkFile.h @@ -31,8 +31,7 @@ namespace ripple { namespace detail { // Work with files -class WorkFile: public Work - , public std::enable_shared_from_this +class WorkFile : public Work, public std::enable_shared_from_this { protected: using error_code = boost::system::error_code; @@ -42,48 +41,49 @@ protected: public: using callback_type = std::function; + public: WorkFile( std::string const& path, - boost::asio::io_service& ios, callback_type cb); + boost::asio::io_service& ios, + callback_type cb); ~WorkFile(); - void run() override; + void + run() override; - void cancel() override; + void + cancel() override; private: std::string path_; callback_type cb_; boost::asio::io_service& ios_; boost::asio::io_service::strand strand_; - }; //------------------------------------------------------------------------------ WorkFile::WorkFile( std::string const& path, - boost::asio::io_service& ios, callback_type cb) - : path_(path) - , cb_(std::move(cb)) - , ios_(ios) - , strand_(ios) + boost::asio::io_service& ios, + callback_type cb) + : path_(path), cb_(std::move(cb)), ios_(ios), strand_(ios) { } WorkFile::~WorkFile() { if (cb_) - cb_ (make_error_code(boost::system::errc::interrupted), {}); + cb_(make_error_code(boost::system::errc::interrupted), {}); } void WorkFile::run() { - if (! strand_.running_in_this_thread()) - return ios_.post(strand_.wrap (std::bind( - &WorkFile::run, shared_from_this()))); + if (!strand_.running_in_this_thread()) + return ios_.post( + strand_.wrap(std::bind(&WorkFile::run, shared_from_this()))); error_code ec; auto const fileContents = getFileContents(ec, path_, megabytes(1)); @@ -99,9 +99,8 @@ WorkFile::cancel() // Nothing to do. Either it finished in run, or it didn't start. } +} // namespace detail -} // detail - -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/detail/WorkPlain.h b/src/ripple/app/misc/detail/WorkPlain.h index 6942e823e7..ab421f32bf 100644 --- a/src/ripple/app/misc/detail/WorkPlain.h +++ b/src/ripple/app/misc/detail/WorkPlain.h @@ -27,16 +27,18 @@ namespace ripple { namespace detail { // Work over TCP/IP -class WorkPlain : public WorkBase - , public std::enable_shared_from_this +class WorkPlain : public WorkBase, + public std::enable_shared_from_this { friend class WorkBase; public: WorkPlain( std::string const& host, - std::string const& path, std::string const& port, - boost::asio::io_service& ios, callback_type cb); + std::string const& path, + std::string const& port, + boost::asio::io_service& ios, + callback_type cb); ~WorkPlain() = default; private: @@ -54,9 +56,11 @@ private: WorkPlain::WorkPlain( std::string const& host, - std::string const& path, std::string const& port, - boost::asio::io_service& ios, callback_type cb) - : WorkBase (host, path, port, ios, cb) + std::string const& path, + std::string const& port, + boost::asio::io_service& ios, + callback_type cb) + : WorkBase(host, path, port, ios, cb) { } @@ -66,11 +70,11 @@ WorkPlain::onConnect(error_code const& ec) if (ec) return fail(ec); - onStart (); + onStart(); } -} // detail +} // namespace detail -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/detail/WorkSSL.h b/src/ripple/app/misc/detail/WorkSSL.h index 5887acc0c3..b59f6e1065 100644 --- a/src/ripple/app/misc/detail/WorkSSL.h +++ b/src/ripple/app/misc/detail/WorkSSL.h @@ -33,8 +33,8 @@ namespace ripple { namespace detail { // Work over SSL -class WorkSSL : public WorkBase - , public std::enable_shared_from_this +class WorkSSL : public WorkBase, + public std::enable_shared_from_this { friend class WorkBase; @@ -85,8 +85,8 @@ WorkSSL::WorkSSL( { auto ec = context_.preConnectVerify(stream_, host_); if (ec) - Throw ( - boost::str (boost::format ("preConnectVerify: %s") % ec.message ())); + Throw( + boost::str(boost::format("preConnectVerify: %s") % ec.message())); } void @@ -98,7 +98,9 @@ WorkSSL::onConnect(error_code const& ec) stream_.async_handshake( boost::asio::ssl::stream_base::client, - strand_.wrap (boost::bind(&WorkSSL::onHandshake, shared_from_this(), + strand_.wrap(boost::bind( + &WorkSSL::onHandshake, + shared_from_this(), boost::asio::placeholders::error))); } @@ -108,11 +110,11 @@ WorkSSL::onHandshake(error_code const& ec) if (ec) return fail(ec); - onStart (); + onStart(); } -} // detail +} // namespace detail -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/misc/impl/AccountTxPaging.cpp b/src/ripple/app/misc/impl/AccountTxPaging.cpp index 85b70641ba..317178c4d7 100644 --- a/src/ripple/app/misc/impl/AccountTxPaging.cpp +++ b/src/ripple/app/misc/impl/AccountTxPaging.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -30,7 +30,7 @@ namespace ripple { void -convertBlobsToTxResult ( +convertBlobsToTxResult( NetworkOPs::AccountTxs& to, std::uint32_t ledger_index, std::string const& status, @@ -38,23 +38,23 @@ convertBlobsToTxResult ( Blob const& rawMeta, Application& app) { - SerialIter it (makeSlice(rawTxn)); - auto txn = std::make_shared (it); + SerialIter it(makeSlice(rawTxn)); + auto txn = std::make_shared(it); std::string reason; - auto tr = std::make_shared (txn, reason, app); + auto tr = std::make_shared(txn, reason, app); - tr->setStatus (Transaction::sqlTransactionStatus(status)); - tr->setLedger (ledger_index); + tr->setStatus(Transaction::sqlTransactionStatus(status)); + tr->setLedger(ledger_index); - auto metaset = std::make_shared ( - tr->getID (), tr->getLedger (), rawMeta); + auto metaset = + std::make_shared(tr->getID(), tr->getLedger(), rawMeta); to.emplace_back(std::move(tr), metaset); }; void -saveLedgerAsync (Application& app, std::uint32_t seq) +saveLedgerAsync(Application& app, std::uint32_t seq) { if (auto l = app.getLedgerMaster().getLedgerBySeq(seq)) pendSaveValidated(app, l, false, false); @@ -259,4 +259,4 @@ accountTxPage( return; } -} +} // namespace ripple diff --git a/src/ripple/app/misc/impl/AccountTxPaging.h b/src/ripple/app/misc/impl/AccountTxPaging.h index 4319adf244..36dfeabf3a 100644 --- a/src/ripple/app/misc/impl/AccountTxPaging.h +++ b/src/ripple/app/misc/impl/AccountTxPaging.h @@ -20,19 +20,18 @@ #ifndef RIPPLE_APP_MISC_IMPL_ACCOUNTTXPAGING_H_INCLUDED #define RIPPLE_APP_MISC_IMPL_ACCOUNTTXPAGING_H_INCLUDED -#include #include +#include #include #include #include - //------------------------------------------------------------------------------ namespace ripple { void -convertBlobsToTxResult ( +convertBlobsToTxResult( NetworkOPs::AccountTxs& to, std::uint32_t ledger_index, std::string const& status, @@ -41,7 +40,7 @@ convertBlobsToTxResult ( Application& app); void -saveLedgerAsync (Application& app, std::uint32_t seq); +saveLedgerAsync(Application& app, std::uint32_t seq); void accountTxPage( @@ -61,6 +60,6 @@ accountTxPage( int limit, bool bAdmin, std::uint32_t page_length); -} +} // namespace ripple #endif diff --git a/src/ripple/app/misc/impl/AmendmentTable.cpp b/src/ripple/app/misc/impl/AmendmentTable.cpp index a676400993..bf6ee723ee 100644 --- a/src/ripple/app/misc/impl/AmendmentTable.cpp +++ b/src/ripple/app/misc/impl/AmendmentTable.cpp @@ -19,11 +19,11 @@ #include #include -#include -#include #include -#include +#include +#include #include +#include #include #include #include @@ -31,38 +31,36 @@ namespace ripple { -static -std::vector> -parseSection (Section const& section) +static std::vector> +parseSection(Section const& section) { - static boost::regex const re1 ( - "^" // start of line - "(?:\\s*)" // whitespace (optional) - "([abcdefABCDEF0-9]{64})" // - "(?:\\s+)" // whitespace - "(\\S+)" // - , boost::regex_constants::optimize - ); + static boost::regex const re1( + "^" // start of line + "(?:\\s*)" // whitespace (optional) + "([abcdefABCDEF0-9]{64})" // + "(?:\\s+)" // whitespace + "(\\S+)" // + , + boost::regex_constants::optimize); std::vector> names; - for (auto const& line : section.lines ()) + for (auto const& line : section.lines()) { boost::smatch match; - if (!boost::regex_match (line, match, re1)) - Throw ( - "Invalid entry '" + line + - "' in [" + section.name () + "]"); + if (!boost::regex_match(line, match, re1)) + Throw( + "Invalid entry '" + line + "' in [" + section.name() + "]"); uint256 id; - if (!id.SetHexExact (match[1])) - Throw ( - "Invalid amendment ID '" + match[1] + - "' in [" + section.name () + "]"); + if (!id.SetHexExact(match[1])) + Throw( + "Invalid amendment ID '" + match[1] + "' in [" + + section.name() + "]"); - names.push_back (std::make_pair (id, match[2])); + names.push_back(std::make_pair(id, match[2])); } return names; @@ -90,7 +88,7 @@ struct AmendmentState /** The name of this amendment, possibly empty. */ std::string name; - explicit AmendmentState () = default; + explicit AmendmentState() = default; }; /** The status of all amendments requested in a given window. */ @@ -107,9 +105,10 @@ public: // number of votes needed int mThreshold = 0; - AmendmentSet () = default; + AmendmentSet() = default; - void tally (std::set const& amendments) + void + tally(std::set const& amendments) { ++mTrustedValidations; @@ -117,9 +116,10 @@ public: ++votes_[amendment]; } - int votes (uint256 const& amendment) const + int + votes(uint256 const& amendment) const { - auto const& it = votes_.find (amendment); + auto const& it = votes_.find(amendment); if (it == votes_.end()) return 0; @@ -136,8 +136,7 @@ public: Amendments are proposed and then adopted or rejected by the network. An Amendment is uniquely identified by its AmendmentID, a 256-bit key. */ -class AmendmentTableImpl final - : public AmendmentTable +class AmendmentTableImpl final : public AmendmentTable { protected: std::mutex mutex_; @@ -154,7 +153,7 @@ protected: // The results of the last voting round - may be empty if // we haven't participated in one yet. - std::unique_ptr lastVote_; + std::unique_ptr lastVote_; // True if an unsupported amendment is enabled bool unsupportedEnabled_; @@ -166,15 +165,18 @@ protected: beast::Journal const j_; // Finds or creates state - AmendmentState* add (uint256 const& amendment); + AmendmentState* + add(uint256 const& amendment); // Finds existing state - AmendmentState* get (uint256 const& amendment); + AmendmentState* + get(uint256 const& amendment); - void setJson (Json::Value& v, uint256 const& amendment, const AmendmentState&); + void + setJson(Json::Value& v, uint256 const& amendment, const AmendmentState&); public: - AmendmentTableImpl ( + AmendmentTableImpl( std::chrono::seconds majorityTime, int majorityFraction, Section const& supported, @@ -182,25 +184,36 @@ public: Section const& vetoed, beast::Journal journal); - uint256 find (std::string const& name) override; + uint256 + find(std::string const& name) override; - bool veto (uint256 const& amendment) override; - bool unVeto (uint256 const& amendment) override; + bool + veto(uint256 const& amendment) override; + bool + unVeto(uint256 const& amendment) override; - bool enable (uint256 const& amendment) override; - bool disable (uint256 const& amendment) override; + bool + enable(uint256 const& amendment) override; + bool + disable(uint256 const& amendment) override; - bool isEnabled (uint256 const& amendment) override; - bool isSupported (uint256 const& amendment) override; + bool + isEnabled(uint256 const& amendment) override; + bool + isSupported(uint256 const& amendment) override; - bool hasUnsupportedEnabled () override; + bool + hasUnsupportedEnabled() override; boost::optional firstUnsupportedExpected() override; - Json::Value getJson (int) override; - Json::Value getJson (uint256 const&) override; + Json::Value + getJson(int) override; + Json::Value + getJson(uint256 const&) override; - bool needValidatedLedger (LedgerIndex seq) override; + bool + needValidatedLedger(LedgerIndex seq) override; void doValidatedLedger( @@ -208,14 +221,14 @@ public: std::set const& enabled, majorityAmendments_t const& majority) override; - std::vector - doValidation (std::set const& enabledAmendments) override; + std::vector + doValidation(std::set const& enabledAmendments) override; - std::vector - getDesired () override; + std::vector + getDesired() override; - std::map - doVoting ( + std::map + doVoting( NetClock::time_point closeTime, std::set const& enabledAmendments, majorityAmendments_t const& majorityAmendments, @@ -224,45 +237,43 @@ public: //------------------------------------------------------------------------------ -AmendmentTableImpl::AmendmentTableImpl ( - std::chrono::seconds majorityTime, - int majorityFraction, - Section const& supported, - Section const& enabled, - Section const& vetoed, - beast::Journal journal) - : lastUpdateSeq_ (0) - , majorityTime_ (majorityTime) - , majorityFraction_ (majorityFraction) - , unsupportedEnabled_ (false) - , j_ (journal) +AmendmentTableImpl::AmendmentTableImpl( + std::chrono::seconds majorityTime, + int majorityFraction, + Section const& supported, + Section const& enabled, + Section const& vetoed, + beast::Journal journal) + : lastUpdateSeq_(0) + , majorityTime_(majorityTime) + , majorityFraction_(majorityFraction) + , unsupportedEnabled_(false) + , j_(journal) { - assert (majorityFraction_ != 0); + assert(majorityFraction_ != 0); - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); for (auto const& a : parseSection(supported)) { - if (auto s = add (a.first)) + if (auto s = add(a.first)) { - JLOG (j_.debug()) << - "Amendment " << a.first << " is supported."; + JLOG(j_.debug()) << "Amendment " << a.first << " is supported."; - if (!a.second.empty ()) + if (!a.second.empty()) s->name = a.second; s->supported = true; } } - for (auto const& a : parseSection (enabled)) + for (auto const& a : parseSection(enabled)) { - if (auto s = add (a.first)) + if (auto s = add(a.first)) { - JLOG (j_.debug()) << - "Amendment " << a.first << " is enabled."; + JLOG(j_.debug()) << "Amendment " << a.first << " is enabled."; - if (!a.second.empty ()) + if (!a.second.empty()) s->name = a.second; s->supported = true; @@ -270,15 +281,14 @@ AmendmentTableImpl::AmendmentTableImpl ( } } - for (auto const& a : parseSection (vetoed)) + for (auto const& a : parseSection(vetoed)) { // Unknown amendments are effectively vetoed already - if (auto s = get (a.first)) + if (auto s = get(a.first)) { - JLOG (j_.info()) << - "Amendment " << a.first << " is vetoed."; + JLOG(j_.info()) << "Amendment " << a.first << " is vetoed."; - if (!a.second.empty ()) + if (!a.second.empty()) s->name = a.second; s->vetoed = true; @@ -287,17 +297,17 @@ AmendmentTableImpl::AmendmentTableImpl ( } AmendmentState* -AmendmentTableImpl::add (uint256 const& amendmentHash) +AmendmentTableImpl::add(uint256 const& amendmentHash) { // call with the mutex held return &amendmentMap_[amendmentHash]; } AmendmentState* -AmendmentTableImpl::get (uint256 const& amendmentHash) +AmendmentTableImpl::get(uint256 const& amendmentHash) { // call with the mutex held - auto ret = amendmentMap_.find (amendmentHash); + auto ret = amendmentMap_.find(amendmentHash); if (ret == amendmentMap_.end()) return nullptr; @@ -306,9 +316,9 @@ AmendmentTableImpl::get (uint256 const& amendmentHash) } uint256 -AmendmentTableImpl::find (std::string const& name) +AmendmentTableImpl::find(std::string const& name) { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); for (auto const& e : amendmentMap_) { @@ -320,10 +330,10 @@ AmendmentTableImpl::find (std::string const& name) } bool -AmendmentTableImpl::veto (uint256 const& amendment) +AmendmentTableImpl::veto(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = add (amendment); + std::lock_guard sl(mutex_); + auto s = add(amendment); if (s->vetoed) return false; @@ -332,10 +342,10 @@ AmendmentTableImpl::veto (uint256 const& amendment) } bool -AmendmentTableImpl::unVeto (uint256 const& amendment) +AmendmentTableImpl::unVeto(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = get (amendment); + std::lock_guard sl(mutex_); + auto s = get(amendment); if (!s || !s->vetoed) return false; @@ -344,20 +354,20 @@ AmendmentTableImpl::unVeto (uint256 const& amendment) } bool -AmendmentTableImpl::enable (uint256 const& amendment) +AmendmentTableImpl::enable(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = add (amendment); + std::lock_guard sl(mutex_); + auto s = add(amendment); if (s->enabled) return false; s->enabled = true; - if (! s->supported) + if (!s->supported) { - JLOG (j_.error()) << - "Unsupported amendment " << amendment << " activated."; + JLOG(j_.error()) << "Unsupported amendment " << amendment + << " activated."; unsupportedEnabled_ = true; } @@ -365,10 +375,10 @@ AmendmentTableImpl::enable (uint256 const& amendment) } bool -AmendmentTableImpl::disable (uint256 const& amendment) +AmendmentTableImpl::disable(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = get (amendment); + std::lock_guard sl(mutex_); + auto s = get(amendment); if (!s || !s->enabled) return false; @@ -378,25 +388,25 @@ AmendmentTableImpl::disable (uint256 const& amendment) } bool -AmendmentTableImpl::isEnabled (uint256 const& amendment) +AmendmentTableImpl::isEnabled(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = get (amendment); + std::lock_guard sl(mutex_); + auto s = get(amendment); return s && s->enabled; } bool -AmendmentTableImpl::isSupported (uint256 const& amendment) +AmendmentTableImpl::isSupported(uint256 const& amendment) { - std::lock_guard sl (mutex_); - auto s = get (amendment); + std::lock_guard sl(mutex_); + auto s = get(amendment); return s && s->supported; } bool -AmendmentTableImpl::hasUnsupportedEnabled () +AmendmentTableImpl::hasUnsupportedEnabled() { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); return unsupportedEnabled_; } @@ -407,86 +417,82 @@ AmendmentTableImpl::firstUnsupportedExpected() return firstUnsupportedExpected_; } -std::vector -AmendmentTableImpl::doValidation ( - std::set const& enabled) +std::vector +AmendmentTableImpl::doValidation(std::set const& enabled) { // Get the list of amendments we support and do not // veto, but that are not already enabled - std::vector amendments; - amendments.reserve (amendmentMap_.size()); + std::vector amendments; + amendments.reserve(amendmentMap_.size()); { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); for (auto const& e : amendmentMap_) { - if (e.second.supported && ! e.second.vetoed && - (enabled.count (e.first) == 0)) + if (e.second.supported && !e.second.vetoed && + (enabled.count(e.first) == 0)) { - amendments.push_back (e.first); + amendments.push_back(e.first); } } } - if (! amendments.empty()) - std::sort (amendments.begin (), amendments.end ()); + if (!amendments.empty()) + std::sort(amendments.begin(), amendments.end()); return amendments; } -std::vector -AmendmentTableImpl::getDesired () +std::vector +AmendmentTableImpl::getDesired() { // Get the list of amendments we support and do not veto return doValidation({}); } -std::map -AmendmentTableImpl::doVoting ( +std::map +AmendmentTableImpl::doVoting( NetClock::time_point closeTime, std::set const& enabledAmendments, majorityAmendments_t const& majorityAmendments, std::vector const& valSet) { - JLOG (j_.trace()) << - "voting at " << closeTime.time_since_epoch().count() << - ": " << enabledAmendments.size() << - ", " << majorityAmendments.size() << - ", " << valSet.size(); + JLOG(j_.trace()) << "voting at " << closeTime.time_since_epoch().count() + << ": " << enabledAmendments.size() << ", " + << majorityAmendments.size() << ", " << valSet.size(); - auto vote = std::make_unique (); + auto vote = std::make_unique(); // process validations for ledger before flag ledger for (auto const& val : valSet) { - if (val->isTrusted ()) + if (val->isTrusted()) { std::set ballot; - if (val->isFieldPresent (sfAmendments)) + if (val->isFieldPresent(sfAmendments)) { - auto const choices = - val->getFieldV256 (sfAmendments); - ballot.insert (choices.begin (), choices.end ()); + auto const choices = val->getFieldV256(sfAmendments); + ballot.insert(choices.begin(), choices.end()); } - vote->tally (ballot); + vote->tally(ballot); } } - vote->mThreshold = std::max(1, - (vote->mTrustedValidations * majorityFraction_) / 256); + vote->mThreshold = + std::max(1, (vote->mTrustedValidations * majorityFraction_) / 256); - JLOG (j_.debug()) << - "Received " << vote->mTrustedValidations << - " trusted validations, threshold is: " << vote->mThreshold; + JLOG(j_.debug()) << "Received " << vote->mTrustedValidations + << " trusted validations, threshold is: " + << vote->mThreshold; // Map of amendments to the action to be taken for each one. The action is // the value of the flags in the pseudo-transaction - std::map actions; + std::map actions; { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); // process all amendments we know of for (auto const& entry : amendmentMap_) @@ -494,43 +500,41 @@ AmendmentTableImpl::doVoting ( NetClock::time_point majorityTime = {}; bool const hasValMajority = - (vote->votes (entry.first) >= vote->mThreshold); + (vote->votes(entry.first) >= vote->mThreshold); { - auto const it = majorityAmendments.find (entry.first); - if (it != majorityAmendments.end ()) + auto const it = majorityAmendments.find(entry.first); + if (it != majorityAmendments.end()) majorityTime = it->second; } - if (enabledAmendments.count (entry.first) != 0) + if (enabledAmendments.count(entry.first) != 0) { - JLOG (j_.debug()) << - entry.first << ": amendment already enabled"; + JLOG(j_.debug()) + << entry.first << ": amendment already enabled"; } - else if (hasValMajority && - (majorityTime == NetClock::time_point{}) && - ! entry.second.vetoed) + else if ( + hasValMajority && (majorityTime == NetClock::time_point{}) && + !entry.second.vetoed) { // Ledger says no majority, validators say yes - JLOG (j_.debug()) << - entry.first << ": amendment got majority"; + JLOG(j_.debug()) << entry.first << ": amendment got majority"; actions[entry.first] = tfGotMajority; } - else if (! hasValMajority && - (majorityTime != NetClock::time_point{})) + else if ( + !hasValMajority && (majorityTime != NetClock::time_point{})) { // Ledger says majority, validators say no - JLOG (j_.debug()) << - entry.first << ": amendment lost majority"; + JLOG(j_.debug()) << entry.first << ": amendment lost majority"; actions[entry.first] = tfLostMajority; } - else if ((majorityTime != NetClock::time_point{}) && + else if ( + (majorityTime != NetClock::time_point{}) && ((majorityTime + majorityTime_) <= closeTime) && - ! entry.second.vetoed) + !entry.second.vetoed) { // Ledger says majority held - JLOG (j_.debug()) << - entry.first << ": amendment majority held"; + JLOG(j_.debug()) << entry.first << ": amendment majority held"; actions[entry.first] = 0; } } @@ -543,9 +547,9 @@ AmendmentTableImpl::doVoting ( } bool -AmendmentTableImpl::needValidatedLedger (LedgerIndex ledgerSeq) +AmendmentTableImpl::needValidatedLedger(LedgerIndex ledgerSeq) { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); // Is there a ledger in which an amendment could have been enabled // between these two ledger sequences? @@ -567,7 +571,7 @@ AmendmentTableImpl::doValidatedLedger( // it's currently set. If it's not set when the loop is done, then any // prior unknown amendments have lost majority. firstUnsupportedExpected_.reset(); - for (auto const& [ hash, time ] : majority) + for (auto const& [hash, time] : majority) { auto s = add(hash); @@ -577,7 +581,7 @@ AmendmentTableImpl::doValidatedLedger( if (!s->supported) { JLOG(j_.info()) << "Unsupported amendment " << hash - << " reached majority at " << to_string(time); + << " reached majority at " << to_string(time); if (!firstUnsupportedExpected_ || firstUnsupportedExpected_ > time) firstUnsupportedExpected_ = time; } @@ -587,7 +591,10 @@ AmendmentTableImpl::doValidatedLedger( } void -AmendmentTableImpl::setJson (Json::Value& v, const uint256& id, const AmendmentState& fs) +AmendmentTableImpl::setJson( + Json::Value& v, + const uint256& id, + const AmendmentState& fs) { if (!fs.name.empty()) v[jss::name] = fs.name; @@ -600,7 +607,7 @@ AmendmentTableImpl::setJson (Json::Value& v, const uint256& id, const AmendmentS { auto const votesTotal = lastVote_->mTrustedValidations; auto const votesNeeded = lastVote_->mThreshold; - auto const votesFor = lastVote_->votes (id); + auto const votesFor = lastVote_->votes(id); v[jss::count] = votesFor; v[jss::validations] = votesTotal; @@ -614,36 +621,37 @@ AmendmentTableImpl::setJson (Json::Value& v, const uint256& id, const AmendmentS } Json::Value -AmendmentTableImpl::getJson (int) +AmendmentTableImpl::getJson(int) { Json::Value ret(Json::objectValue); { std::lock_guard sl(mutex_); for (auto const& e : amendmentMap_) { - setJson (ret[to_string (e.first)] = Json::objectValue, - e.first, e.second); + setJson( + ret[to_string(e.first)] = Json::objectValue, e.first, e.second); } } return ret; } Json::Value -AmendmentTableImpl::getJson (uint256 const& amendmentID) +AmendmentTableImpl::getJson(uint256 const& amendmentID) { Json::Value ret = Json::objectValue; - Json::Value& jAmendment = (ret[to_string (amendmentID)] = Json::objectValue); + Json::Value& jAmendment = (ret[to_string(amendmentID)] = Json::objectValue); { std::lock_guard sl(mutex_); - auto a = add (amendmentID); - setJson (jAmendment, amendmentID, *a); + auto a = add(amendmentID); + setJson(jAmendment, amendmentID, *a); } return ret; } -std::unique_ptr make_AmendmentTable ( +std::unique_ptr +make_AmendmentTable( std::chrono::seconds majorityTime, int majorityFraction, Section const& supported, @@ -651,13 +659,8 @@ std::unique_ptr make_AmendmentTable ( Section const& vetoed, beast::Journal journal) { - return std::make_unique ( - majorityTime, - majorityFraction, - supported, - enabled, - vetoed, - journal); + return std::make_unique( + majorityTime, majorityFraction, supported, enabled, vetoed, journal); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/impl/LoadFeeTrack.cpp b/src/ripple/app/misc/impl/LoadFeeTrack.cpp index 6ea4b378ad..efe3b83b32 100644 --- a/src/ripple/app/misc/impl/LoadFeeTrack.cpp +++ b/src/ripple/app/misc/impl/LoadFeeTrack.cpp @@ -18,14 +18,14 @@ //============================================================================== #include -#include #include #include +#include #include #include #include -#include #include +#include #include #include @@ -34,9 +34,9 @@ namespace ripple { bool -LoadFeeTrack::raiseLocalFee () +LoadFeeTrack::raiseLocalFee() { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); if (++raiseCount_ < 2) return false; @@ -56,20 +56,20 @@ LoadFeeTrack::raiseLocalFee () if (origFee == localTxnLoadFee_) return false; - JLOG(j_.debug()) << "Local load fee raised from " << - origFee << " to " << localTxnLoadFee_; + JLOG(j_.debug()) << "Local load fee raised from " << origFee << " to " + << localTxnLoadFee_; return true; } bool -LoadFeeTrack::lowerLocalFee () +LoadFeeTrack::lowerLocalFee() { - std::lock_guard sl (lock_); + std::lock_guard sl(lock_); std::uint32_t origFee = localTxnLoadFee_; raiseCount_ = 0; // Reduce slowly - localTxnLoadFee_ -= (localTxnLoadFee_ / lftFeeDecFraction ); + localTxnLoadFee_ -= (localTxnLoadFee_ / lftFeeDecFraction); if (localTxnLoadFee_ < lftNormalFee) localTxnLoadFee_ = lftNormalFee; @@ -77,8 +77,8 @@ LoadFeeTrack::lowerLocalFee () if (origFee == localTxnLoadFee_) return false; - JLOG(j_.debug()) << "Local load fee lowered from " << - origFee << " to " << localTxnLoadFee_; + JLOG(j_.debug()) << "Local load fee lowered from " << origFee << " to " + << localTxnLoadFee_; return true; } @@ -86,19 +86,20 @@ LoadFeeTrack::lowerLocalFee () // Scale using load as well as base rate XRPAmount -scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, - Fees const& fees, bool bUnlimited) +scaleFeeLoad( + FeeUnit64 fee, + LoadFeeTrack const& feeTrack, + Fees const& fees, + bool bUnlimited) { if (fee == 0) return XRPAmount{0}; // Normally, types with different units wouldn't be mathematically // compatible. This function is an exception. - auto lowestTerms = [](auto& a, auto& b) - { - auto value = [](auto val) - { - if constexpr(std::is_arithmetic_v) + auto lowestTerms = [](auto& a, auto& b) { + auto value = [](auto val) { + if constexpr (std::is_arithmetic_v) return val; else return val.value(); @@ -127,8 +128,8 @@ scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, // The denominator of the fraction we're trying to compute. // fees.units and lftNormalFee are both 32 bit, // so the multiplication can't overflow. - auto den = FeeUnit64{ fees.units } - * safe_cast(feeTrack.getLoadBase()); + auto den = FeeUnit64{fees.units} * + safe_cast(feeTrack.getLoadBase()); // Reduce fee * baseFee * feeFactor / (fees.units * lftNormalFee) // to lowest terms. lowestTerms(fee, den); @@ -159,8 +160,8 @@ scaleFeeLoad(FeeUnit64 fee, LoadFeeTrack const& feeTrack, auto const result = mulDiv(fee, baseFee, den); if (!result.first) - Throw ("scaleFeeLoad"); + Throw("scaleFeeLoad"); return result.second; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/impl/Manifest.cpp b/src/ripple/app/misc/impl/Manifest.cpp index 2b8cdbd9b0..5898080873 100644 --- a/src/ripple/app/misc/impl/Manifest.cpp +++ b/src/ripple/app/misc/impl/Manifest.cpp @@ -18,10 +18,10 @@ //============================================================================== #include -#include -#include #include #include +#include +#include #include #include #include @@ -34,40 +34,41 @@ namespace ripple { -boost::optional deserializeManifest(Slice s) +boost::optional +deserializeManifest(Slice s) { if (s.empty()) return boost::none; - static SOTemplate const manifestFormat { - // A manifest must include: - // - the master public key - {sfPublicKey, soeREQUIRED}, + static SOTemplate const manifestFormat{ + // A manifest must include: + // - the master public key + {sfPublicKey, soeREQUIRED}, - // - a signature with that public key - {sfMasterSignature, soeREQUIRED}, + // - a signature with that public key + {sfMasterSignature, soeREQUIRED}, - // - a sequence number - {sfSequence, soeREQUIRED}, + // - a sequence number + {sfSequence, soeREQUIRED}, - // It may, optionally, contain: - // - a version number which defaults to 0 - {sfVersion, soeDEFAULT}, + // It may, optionally, contain: + // - a version number which defaults to 0 + {sfVersion, soeDEFAULT}, - // - a domain name - {sfDomain, soeOPTIONAL}, + // - a domain name + {sfDomain, soeOPTIONAL}, - // - an ephemeral signing key that can be changed as necessary - {sfSigningPubKey, soeOPTIONAL}, + // - an ephemeral signing key that can be changed as necessary + {sfSigningPubKey, soeOPTIONAL}, - // - a signature using the ephemeral signing key, if it is present - {sfSignature, soeOPTIONAL}, - }; + // - a signature using the ephemeral signing key, if it is present + {sfSignature, soeOPTIONAL}, + }; try { - SerialIter sit{ s }; - STObject st{ sit, sfGeneric }; + SerialIter sit{s}; + STObject st{sit, sfGeneric}; st.applyTemplate(manifestFormat); @@ -75,15 +76,15 @@ boost::optional deserializeManifest(Slice s) if (st.isFieldPresent(sfVersion) && st.getFieldU16(sfVersion) != 0) return boost::none; - auto const pk = st.getFieldVL (sfPublicKey); + auto const pk = st.getFieldVL(sfPublicKey); - if (! publicKeyType (makeSlice(pk))) + if (!publicKeyType(makeSlice(pk))) return boost::none; Manifest m; m.serialized.assign(reinterpret_cast(s.data()), s.size()); m.masterKey = PublicKey(makeSlice(pk)); - m.sequence = st.getFieldU32 (sfSequence); + m.sequence = st.getFieldU32(sfSequence); if (st.isFieldPresent(sfDomain)) { @@ -93,23 +94,24 @@ boost::optional deserializeManifest(Slice s) if (boost::algorithm::clamp(d.size(), 4, 128) != d.size()) return boost::none; - m.domain.assign (reinterpret_cast(d.data()), d.size()); + m.domain.assign(reinterpret_cast(d.data()), d.size()); // This regular expression should do a decent job of weeding out // obviously wrong domain names but it isn't perfect. It does not // really support IDNs. If this turns out to be an issue, a more // thorough regex can be used or this check can just be removed. static boost::regex const re( - "^" // Beginning of line - "(" // Beginning of a segment - "(?!-)" // - must not begin with '-' - "[a-zA-Z0-9-]{1,63}" // - only alphanumeric and '-' - "(? deserializeManifest(Slice s) auto const spk = st.getFieldVL(sfSigningPubKey); - if (!publicKeyType (makeSlice(spk))) + if (!publicKeyType(makeSlice(spk))) return boost::none; m.signingKey = PublicKey(makeSlice(spk)); @@ -154,88 +156,92 @@ boost::optional deserializeManifest(Slice s) } } -template +template Stream& -logMftAct ( +logMftAct( Stream& s, std::string const& action, PublicKey const& pk, std::uint32_t seq) { - s << "Manifest: " << action << - ";Pk: " << toBase58 (TokenType::NodePublic, pk) << - ";Seq: " << seq << ";"; + s << "Manifest: " << action + << ";Pk: " << toBase58(TokenType::NodePublic, pk) << ";Seq: " << seq + << ";"; return s; } -template -Stream& logMftAct ( +template +Stream& +logMftAct( Stream& s, std::string const& action, PublicKey const& pk, std::uint32_t seq, std::uint32_t oldSeq) { - s << "Manifest: " << action << - ";Pk: " << toBase58 (TokenType::NodePublic, pk) << - ";Seq: " << seq << - ";OldSeq: " << oldSeq << ";"; + s << "Manifest: " << action + << ";Pk: " << toBase58(TokenType::NodePublic, pk) << ";Seq: " << seq + << ";OldSeq: " << oldSeq << ";"; return s; } -bool Manifest::verify () const +bool +Manifest::verify() const { - STObject st (sfGeneric); - SerialIter sit (serialized.data (), serialized.size ()); - st.set (sit); + STObject st(sfGeneric); + SerialIter sit(serialized.data(), serialized.size()); + st.set(sit); // Signing key and signature are not required for // master key revocations - if (! revoked () && ! ripple::verify (st, HashPrefix::manifest, signingKey)) + if (!revoked() && !ripple::verify(st, HashPrefix::manifest, signingKey)) return false; - return ripple::verify ( + return ripple::verify( st, HashPrefix::manifest, masterKey, sfMasterSignature); } -uint256 Manifest::hash () const +uint256 +Manifest::hash() const { - STObject st (sfGeneric); - SerialIter sit (serialized.data (), serialized.size ()); - st.set (sit); - return st.getHash (HashPrefix::manifest); + STObject st(sfGeneric); + SerialIter sit(serialized.data(), serialized.size()); + st.set(sit); + return st.getHash(HashPrefix::manifest); } -bool Manifest::revoked () const +bool +Manifest::revoked() const { /* The maximum possible sequence number means that the master key has been revoked. */ - return sequence == std::numeric_limits::max (); + return sequence == std::numeric_limits::max(); } -boost::optional Manifest::getSignature () const +boost::optional +Manifest::getSignature() const { - STObject st (sfGeneric); - SerialIter sit (serialized.data (), serialized.size ()); - st.set (sit); + STObject st(sfGeneric); + SerialIter sit(serialized.data(), serialized.size()); + st.set(sit); if (!get(st, sfSignature)) return boost::none; - return st.getFieldVL (sfSignature); + return st.getFieldVL(sfSignature); } -Blob Manifest::getMasterSignature () const +Blob +Manifest::getMasterSignature() const { - STObject st (sfGeneric); - SerialIter sit (serialized.data (), serialized.size ()); - st.set (sit); - return st.getFieldVL (sfMasterSignature); + STObject st(sfGeneric); + SerialIter sit(serialized.data(), serialized.size()); + st.set(sit); + return st.getFieldVL(sfMasterSignature); } ValidatorToken::ValidatorToken(std::string const& m, SecretKey const& valSecret) - : manifest(m) - , validationSecret(valSecret) + : manifest(m), validationSecret(valSecret) { } @@ -245,12 +251,13 @@ ValidatorToken::make_ValidatorToken(std::vector const& tokenBlob) try { std::string tokenStr; - tokenStr.reserve ( - std::accumulate (tokenBlob.cbegin(), tokenBlob.cend(), std::size_t(0), - [] (std::size_t init, std::string const& s) - { - return init + s.size(); - })); + tokenStr.reserve(std::accumulate( + tokenBlob.cbegin(), + tokenBlob.cend(), + std::size_t(0), + [](std::size_t init, std::string const& s) { + return init + s.size(); + })); for (auto const& line : tokenBlob) tokenStr += beast::rfc2616::trim(line); @@ -259,15 +266,16 @@ ValidatorToken::make_ValidatorToken(std::vector const& tokenBlob) Json::Reader r; Json::Value token; - if (! r.parse (tokenStr, token)) + if (!r.parse(tokenStr, token)) return boost::none; if (token.isMember("manifest") && token["manifest"].isString() && token.isMember("validation_secret_key") && token["validation_secret_key"].isString()) { - auto const ret = strUnHex (token["validation_secret_key"].asString()); - if (! ret || ret->empty()) + auto const ret = + strUnHex(token["validation_secret_key"].asString()); + if (!ret || ret->empty()) return boost::none; return ValidatorToken( @@ -286,79 +294,79 @@ ValidatorToken::make_ValidatorToken(std::vector const& tokenBlob) } PublicKey -ManifestCache::getSigningKey (PublicKey const& pk) const +ManifestCache::getSigningKey(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = map_.find (pk); + auto const iter = map_.find(pk); - if (iter != map_.end () && !iter->second.revoked ()) + if (iter != map_.end() && !iter->second.revoked()) return iter->second.signingKey; return pk; } PublicKey -ManifestCache::getMasterKey (PublicKey const& pk) const +ManifestCache::getMasterKey(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = signingToMasterKeys_.find (pk); + auto const iter = signingToMasterKeys_.find(pk); - if (iter != signingToMasterKeys_.end ()) + if (iter != signingToMasterKeys_.end()) return iter->second; return pk; } boost::optional -ManifestCache::getSequence (PublicKey const& pk) const +ManifestCache::getSequence(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = map_.find (pk); + auto const iter = map_.find(pk); - if (iter != map_.end () && !iter->second.revoked ()) + if (iter != map_.end() && !iter->second.revoked()) return iter->second.sequence; return boost::none; } boost::optional -ManifestCache::getDomain (PublicKey const& pk) const +ManifestCache::getDomain(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = map_.find (pk); + auto const iter = map_.find(pk); - if (iter != map_.end () && !iter->second.revoked ()) + if (iter != map_.end() && !iter->second.revoked()) return iter->second.domain; return boost::none; } boost::optional -ManifestCache::getManifest (PublicKey const& pk) const +ManifestCache::getManifest(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = map_.find (pk); + auto const iter = map_.find(pk); - if (iter != map_.end () && !iter->second.revoked ()) + if (iter != map_.end() && !iter->second.revoked()) return iter->second.serialized; return boost::none; } bool -ManifestCache::revoked (PublicKey const& pk) const +ManifestCache::revoked(PublicKey const& pk) const { std::lock_guard lock{read_mutex_}; - auto const iter = map_.find (pk); + auto const iter = map_.find(pk); - if (iter != map_.end ()) - return iter->second.revoked (); + if (iter != map_.end()) + return iter->second.revoked(); return false; } ManifestDisposition -ManifestCache::applyManifest (Manifest m) +ManifestCache::applyManifest(Manifest m) { std::lock_guard applyLock{apply_mutex_}; @@ -368,8 +376,7 @@ ManifestCache::applyManifest (Manifest m) */ auto const iter = map_.find(m.masterKey); - if (iter != map_.end() && - m.sequence <= iter->second.sequence) + if (iter != map_.end() && m.sequence <= iter->second.sequence) { /* A manifest was received for a validator we're tracking, but @@ -378,11 +385,16 @@ ManifestCache::applyManifest (Manifest m) connects. */ if (auto stream = j_.debug()) - logMftAct(stream, "Stale", m.masterKey, m.sequence, iter->second.sequence); + logMftAct( + stream, + "Stale", + m.masterKey, + m.sequence, + iter->second.sequence); return ManifestDisposition::stale; // not a newer manifest, ignore } - if (! m.verify()) + if (!m.verify()) { /* A manifest's signature is invalid. @@ -410,7 +422,7 @@ ManifestCache::applyManifest (Manifest m) logMftAct(stream, "Revoked", m.masterKey, m.sequence); } - if (iter == map_.end ()) + if (iter == map_.end()) { /* This is the first received manifest for a trusted master key @@ -420,7 +432,7 @@ ManifestCache::applyManifest (Manifest m) if (auto stream = j_.info()) logMftAct(stream, "AcceptedNew", m.masterKey, m.sequence); - if (! revoked) + if (!revoked) signingToMasterKeys_[m.signingKey] = m.masterKey; auto masterKey = m.masterKey; @@ -433,106 +445,104 @@ ManifestCache::applyManifest (Manifest m) This is expected, but should happen infrequently. */ if (auto stream = j_.info()) - logMftAct(stream, "AcceptedUpdate", - m.masterKey, m.sequence, iter->second.sequence); + logMftAct( + stream, + "AcceptedUpdate", + m.masterKey, + m.sequence, + iter->second.sequence); - signingToMasterKeys_.erase (iter->second.signingKey); + signingToMasterKeys_.erase(iter->second.signingKey); - if (! revoked) + if (!revoked) signingToMasterKeys_[m.signingKey] = m.masterKey; - iter->second = std::move (m); + iter->second = std::move(m); } return ManifestDisposition::accepted; } void -ManifestCache::load ( - DatabaseCon& dbCon, std::string const& dbTable) +ManifestCache::load(DatabaseCon& dbCon, std::string const& dbTable) { // Load manifests stored in database - std::string const sql = - "SELECT RawData FROM " + dbTable + ";"; - auto db = dbCon.checkoutDb (); - soci::blob sociRawData (*db); - soci::statement st = - (db->prepare << sql, - soci::into (sociRawData)); - st.execute (); - while (st.fetch ()) + std::string const sql = "SELECT RawData FROM " + dbTable + ";"; + auto db = dbCon.checkoutDb(); + soci::blob sociRawData(*db); + soci::statement st = (db->prepare << sql, soci::into(sociRawData)); + st.execute(); + while (st.fetch()) { std::string serialized; - convert (sociRawData, serialized); + convert(sociRawData, serialized); if (auto mo = deserializeManifest(serialized)) { if (!mo->verify()) { - JLOG(j_.warn()) - << "Unverifiable manifest in db"; + JLOG(j_.warn()) << "Unverifiable manifest in db"; continue; } - applyManifest (std::move(*mo)); + applyManifest(std::move(*mo)); } else { - JLOG(j_.warn()) - << "Malformed manifest in database"; + JLOG(j_.warn()) << "Malformed manifest in database"; } } } bool -ManifestCache::load ( - DatabaseCon& dbCon, std::string const& dbTable, +ManifestCache::load( + DatabaseCon& dbCon, + std::string const& dbTable, std::string const& configManifest, std::vector const& configRevocation) { - load (dbCon, dbTable); + load(dbCon, dbTable); - if (! configManifest.empty()) + if (!configManifest.empty()) { auto mo = deserializeManifest(base64_decode(configManifest)); - if (! mo) + if (!mo) { - JLOG (j_.error()) << "Malformed validator_token in config"; + JLOG(j_.error()) << "Malformed validator_token in config"; return false; } if (mo->revoked()) { - JLOG (j_.warn()) << - "Configured manifest revokes public key"; + JLOG(j_.warn()) << "Configured manifest revokes public key"; } - if (applyManifest (std::move(*mo)) == - ManifestDisposition::invalid) + if (applyManifest(std::move(*mo)) == ManifestDisposition::invalid) { - JLOG (j_.error()) << "Manifest in config was rejected"; + JLOG(j_.error()) << "Manifest in config was rejected"; return false; } } - if (! configRevocation.empty()) + if (!configRevocation.empty()) { std::string revocationStr; - revocationStr.reserve ( - std::accumulate (configRevocation.cbegin(), configRevocation.cend(), std::size_t(0), - [] (std::size_t init, std::string const& s) - { - return init + s.size(); - })); + revocationStr.reserve(std::accumulate( + configRevocation.cbegin(), + configRevocation.cend(), + std::size_t(0), + [](std::size_t init, std::string const& s) { + return init + s.size(); + })); for (auto const& line : configRevocation) revocationStr += beast::rfc2616::trim(line); auto mo = deserializeManifest(base64_decode(revocationStr)); - if (! mo || ! mo->revoked() || - applyManifest (std::move(*mo)) == ManifestDisposition::invalid) + if (!mo || !mo->revoked() || + applyManifest(std::move(*mo)) == ManifestDisposition::invalid) { - JLOG (j_.error()) << "Invalid validator key revocation in config"; + JLOG(j_.error()) << "Invalid validator key revocation in config"; return false; } } @@ -540,13 +550,15 @@ ManifestCache::load ( return true; } -void ManifestCache::save ( - DatabaseCon& dbCon, std::string const& dbTable, - std::function isTrusted) +void +ManifestCache::save( + DatabaseCon& dbCon, + std::string const& dbTable, + std::function isTrusted) { std::lock_guard lock{apply_mutex_}; - auto db = dbCon.checkoutDb (); + auto db = dbCon.checkoutDb(); soci::transaction tr(*db); *db << "DELETE FROM " << dbTable; @@ -556,11 +568,9 @@ void ManifestCache::save ( { // Save all revocation manifests, // but only save trusted non-revocation manifests. - if (! v.second.revoked() && ! isTrusted (v.second.masterKey)) + if (!v.second.revoked() && !isTrusted(v.second.masterKey)) { - - JLOG(j_.info()) - << "Untrusted manifest in cache not saved to db"; + JLOG(j_.info()) << "Untrusted manifest in cache not saved to db"; continue; } @@ -568,10 +578,9 @@ void ManifestCache::save ( // Do not reuse blob because manifest ecdsa signatures vary in length // but blob write length is expected to be >= the last write soci::blob rawData(*db); - convert (v.second.serialized, rawData); - *db << sql, - soci::use (rawData); + convert(v.second.serialized, rawData); + *db << sql, soci::use(rawData); } - tr.commit (); -} + tr.commit(); } +} // namespace ripple diff --git a/src/ripple/app/misc/impl/Transaction.cpp b/src/ripple/app/misc/impl/Transaction.cpp index 4ba0b3b2b1..645eb79ad2 100644 --- a/src/ripple/app/misc/impl/Transaction.cpp +++ b/src/ripple/app/misc/impl/Transaction.cpp @@ -17,14 +17,14 @@ */ //============================================================================== -#include -#include -#include -#include #include #include #include +#include +#include +#include #include +#include #include #include #include @@ -32,16 +32,15 @@ namespace ripple { -Transaction::Transaction (std::shared_ptr const& stx, - std::string& reason, Application& app) - noexcept - : mTransaction (stx) - , mApp (app) - , j_ (app.journal ("Ledger")) +Transaction::Transaction( + std::shared_ptr const& stx, + std::string& reason, + Application& app) noexcept + : mTransaction(stx), mApp(app), j_(app.journal("Ledger")) { try { - mTransactionID = mTransaction->getTransactionID (); + mTransactionID = mTransaction->getTransactionID(); } catch (std::exception& e) { @@ -56,86 +55,100 @@ Transaction::Transaction (std::shared_ptr const& stx, // Misc. // -void Transaction::setStatus (TransStatus ts, std::uint32_t lseq) +void +Transaction::setStatus(TransStatus ts, std::uint32_t lseq) { - mStatus = ts; - mInLedger = lseq; + mStatus = ts; + mInLedger = lseq; } -TransStatus Transaction::sqlTransactionStatus( - boost::optional const& status) +TransStatus +Transaction::sqlTransactionStatus(boost::optional const& status) { char const c = (status) ? (*status)[0] : safe_cast(txnSqlUnknown); switch (c) { - case txnSqlNew: return NEW; - case txnSqlConflict: return CONFLICTED; - case txnSqlHeld: return HELD; - case txnSqlValidated: return COMMITTED; - case txnSqlIncluded: return INCLUDED; + case txnSqlNew: + return NEW; + case txnSqlConflict: + return CONFLICTED; + case txnSqlHeld: + return HELD; + case txnSqlValidated: + return COMMITTED; + case txnSqlIncluded: + return INCLUDED; } - assert (c == txnSqlUnknown); + assert(c == txnSqlUnknown); return INVALID; } -Transaction::pointer Transaction::transactionFromSQL ( +Transaction::pointer +Transaction::transactionFromSQL( boost::optional const& ledgerSeq, boost::optional const& status, Blob const& rawTxn, Application& app) { std::uint32_t const inLedger = - rangeCheckedCast(ledgerSeq.value_or (0)); + rangeCheckedCast(ledgerSeq.value_or(0)); - SerialIter it (makeSlice(rawTxn)); - auto txn = std::make_shared (it); + SerialIter it(makeSlice(rawTxn)); + auto txn = std::make_shared(it); std::string reason; - auto tr = std::make_shared ( - txn, reason, app); + auto tr = std::make_shared(txn, reason, app); - tr->setStatus (sqlTransactionStatus (status)); - tr->setLedger (inLedger); + tr->setStatus(sqlTransactionStatus(status)); + tr->setLedger(inLedger); return tr; } -Transaction::pointer Transaction::load (uint256 const& id, Application& app, error_code_i& ec) +Transaction::pointer +Transaction::load(uint256 const& id, Application& app, error_code_i& ec) { - return boost::get (load (id, app, boost::none, ec)); + return boost::get(load(id, app, boost::none, ec)); } boost::variant -Transaction::load (uint256 const& id, Application& app, ClosedInterval const& range, +Transaction::load( + uint256 const& id, + Application& app, + ClosedInterval const& range, error_code_i& ec) { using op = boost::optional>; - return load (id, app, op {range}, ec); + return load(id, app, op{range}, ec); } boost::variant -Transaction::load (uint256 const& id, Application& app, boost::optional> const& range, +Transaction::load( + uint256 const& id, + Application& app, + boost::optional> const& range, error_code_i& ec) { - std::string sql = "SELECT LedgerSeq,Status,RawTxn " - "FROM Transactions WHERE TransID='"; + std::string sql = + "SELECT LedgerSeq,Status,RawTxn " + "FROM Transactions WHERE TransID='"; - sql.append (to_string (id)); - sql.append ("';"); + sql.append(to_string(id)); + sql.append("';"); boost::optional ledgerSeq; boost::optional status; Blob rawTxn; { - auto db = app.getTxnDB ().checkoutDb (); - soci::blob sociRawTxnBlob (*db); + auto db = app.getTxnDB().checkoutDb(); + soci::blob sociRawTxnBlob(*db); soci::indicator rti; - *db << sql, soci::into (ledgerSeq), soci::into (status), - soci::into (sociRawTxnBlob, rti); + *db << sql, soci::into(ledgerSeq), soci::into(status), + soci::into(sociRawTxnBlob, rti); - auto const got_data = db->got_data (); + auto const got_data = db->got_data(); if ((!got_data || rti != soci::i_ok) && !range) return nullptr; @@ -144,27 +157,23 @@ Transaction::load (uint256 const& id, Application& app, boost::optionalfirst () - << " AND " - << range->last () - << ";", - soci::into (count, rti); + *db << "SELECT COUNT(DISTINCT LedgerSeq) FROM Transactions WHERE " + "LedgerSeq BETWEEN " + << range->first() << " AND " << range->last() << ";", + soci::into(count, rti); - if (!db->got_data () || rti != soci::i_ok) + if (!db->got_data() || rti != soci::i_ok) return false; - return count == (range->last () - range->first () + 1); + return count == (range->last() - range->first() + 1); } - convert (sociRawTxnBlob, rawTxn); + convert(sociRawTxnBlob, rawTxn); } try { - return Transaction::transactionFromSQL( - ledgerSeq, status, - rawTxn, app); + return Transaction::transactionFromSQL(ledgerSeq, status, rawTxn, app); } catch (std::exception& e) { @@ -179,19 +188,19 @@ Transaction::load (uint256 const& id, Application& app, boost::optionalgetJson (JsonOptions::none, binary)); + Json::Value ret(mTransaction->getJson(JsonOptions::none, binary)); if (mInLedger) { - ret[jss::inLedger] = mInLedger; // Deprecated. + ret[jss::inLedger] = mInLedger; // Deprecated. ret[jss::ledger_index] = mInLedger; if (options == JsonOptions::include_date) { - auto ct = mApp.getLedgerMaster(). - getCloseTimeBySeq (mInLedger); + auto ct = mApp.getLedgerMaster().getCloseTimeBySeq(mInLedger); if (ct) ret[jss::date] = ct->time_since_epoch().count(); } @@ -200,4 +209,4 @@ Json::Value Transaction::getJson (JsonOptions options, bool binary) const return ret; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/impl/TxQ.cpp b/src/ripple/app/misc/impl/TxQ.cpp index c9421dec0c..ed967ddd2d 100644 --- a/src/ripple/app/misc/impl/TxQ.cpp +++ b/src/ripple/app/misc/impl/TxQ.cpp @@ -17,26 +17,25 @@ */ //============================================================================== -#include #include #include #include +#include #include +#include #include #include #include -#include #include +#include #include #include -#include namespace ripple { ////////////////////////////////////////////////////////////////////////// -static -FeeLevel64 +static FeeLevel64 getFeeLevelPaid( STTx const& tx, FeeLevel64 baseRefLevel, @@ -51,13 +50,10 @@ getFeeLevelPaid( // If the math overflows, return the clipped // result blindly. This is very unlikely to ever // happen. - return mulDiv(tx[sfFee].xrp(), - baseRefLevel, - refTxnCostDrops).second; + return mulDiv(tx[sfFee].xrp(), baseRefLevel, refTxnCostDrops).second; } -static -boost::optional +static boost::optional getLastLedgerSequence(STTx const& tx) { if (!tx.isFieldPresent(sfLastLedgerSequence)) @@ -65,22 +61,21 @@ getLastLedgerSequence(STTx const& tx) return tx.getFieldU32(sfLastLedgerSequence); } -static -FeeLevel64 -increase(FeeLevel64 level, - std::uint32_t increasePercent) +static FeeLevel64 +increase(FeeLevel64 level, std::uint32_t increasePercent) { return mulDiv(level, 100 + increasePercent, 100).second; } - ////////////////////////////////////////////////////////////////////////// constexpr FeeLevel64 TxQ::baseLevel; std::size_t -TxQ::FeeMetrics::update(Application& app, - ReadView const& view, bool timeLeap, +TxQ::FeeMetrics::update( + Application& app, + ReadView const& view, + bool timeLeap, TxQ::Setup const& setup) { std::vector feeLevels; @@ -88,25 +83,21 @@ TxQ::FeeMetrics::update(Application& app, auto const txEnd = view.txs.end(); auto const size = std::distance(txBegin, txEnd); feeLevels.reserve(size); - std::for_each(txBegin, txEnd, - [&](auto const& tx) - { - auto const baseFee = view.fees().toDrops( - calculateBaseFee(view, *tx.first)).second; - feeLevels.push_back(getFeeLevelPaid(*tx.first, - baseLevel, baseFee, setup)); - } - ); + std::for_each(txBegin, txEnd, [&](auto const& tx) { + auto const baseFee = + view.fees().toDrops(calculateBaseFee(view, *tx.first)).second; + feeLevels.push_back( + getFeeLevelPaid(*tx.first, baseLevel, baseFee, setup)); + }); std::sort(feeLevels.begin(), feeLevels.end()); assert(size == feeLevels.size()); - JLOG(j_.debug()) << "Ledger " << view.info().seq << - " has " << size << " transactions. " << - "Ledgers are processing " << - (timeLeap ? "slowly" : "as expected") << - ". Expected transactions is currently " << - txnsExpected_ << " and multiplier is " << - escalationMultiplier_; + JLOG(j_.debug()) << "Ledger " << view.info().seq << " has " << size + << " transactions. " + << "Ledgers are processing " + << (timeLeap ? "slowly" : "as expected") + << ". Expected transactions is currently " << txnsExpected_ + << " and multiplier is " << escalationMultiplier_; if (timeLeap) { @@ -121,17 +112,15 @@ TxQ::FeeMetrics::update(Application& app, mulDiv(size, cutPct, 100).second, minimumTxnCount_, upperLimit); recentTxnCounts_.clear(); } - else if (size > txnsExpected_ || - size > targetTxnCount_) + else if (size > txnsExpected_ || size > targetTxnCount_) { recentTxnCounts_.push_back( - mulDiv(size, 100 + setup.normalConsensusIncreasePercent, - 100).second); - auto const iter = std::max_element(recentTxnCounts_.begin(), - recentTxnCounts_.end()); + mulDiv(size, 100 + setup.normalConsensusIncreasePercent, 100) + .second); + auto const iter = + std::max_element(recentTxnCounts_.begin(), recentTxnCounts_.end()); BOOST_ASSERT(iter != recentTxnCounts_.end()); - auto const next = [&] - { + auto const next = [&] { // Grow quickly: If the max_element is >= the // current size limit, use it. if (*iter >= txnsExpected_) @@ -145,8 +134,7 @@ TxQ::FeeMetrics::update(Application& app, // Ledgers are processing in a timely manner, // so keep the limit high, but don't let it // grow without bound. - txnsExpected_ = std::min(next, - maximumTxnCount_.value_or(next)); + txnsExpected_ = std::min(next, maximumTxnCount_.value_or(next)); } if (!size) @@ -159,21 +147,20 @@ TxQ::FeeMetrics::update(Application& app, // evaluates to the middle element; for an even // number of elements, it will add the two elements // on either side of the "middle" and average them. - escalationMultiplier_ = (feeLevels[size / 2] + - feeLevels[(size - 1) / 2] + FeeLevel64{ 1 }) / 2; - escalationMultiplier_ = std::max(escalationMultiplier_, - setup.minimumEscalationMultiplier); + escalationMultiplier_ = + (feeLevels[size / 2] + feeLevels[(size - 1) / 2] + FeeLevel64{1}) / + 2; + escalationMultiplier_ = + std::max(escalationMultiplier_, setup.minimumEscalationMultiplier); } - JLOG(j_.debug()) << "Expected transactions updated to " << - txnsExpected_ << " and multiplier updated to " << - escalationMultiplier_; + JLOG(j_.debug()) << "Expected transactions updated to " << txnsExpected_ + << " and multiplier updated to " << escalationMultiplier_; return size; } FeeLevel64 -TxQ::FeeMetrics::scaleFeeLevel(Snapshot const& snapshot, - OpenView const& view) +TxQ::FeeMetrics::scaleFeeLevel(Snapshot const& snapshot, OpenView const& view) { // Transactions in the open ledger so far auto const current = view.txCount(); @@ -187,8 +174,7 @@ TxQ::FeeMetrics::scaleFeeLevel(Snapshot const& snapshot, { // Compute escalated fee level // Don't care about the overflow flag - return mulDiv(multiplier, current * current, - target * target).second; + return mulDiv(multiplier, current * current, target * target).second; } return baseLevel; @@ -196,8 +182,7 @@ TxQ::FeeMetrics::scaleFeeLevel(Snapshot const& snapshot, namespace detail { -static -std::pair +static std::pair sumOfFirstSquares(std::size_t x) { // sum(n = 1->x) : n * n = x(x + 1)(2x + 1) / 6 @@ -208,16 +193,17 @@ sumOfFirstSquares(std::size_t x) // it does. If we have anywhere near 2^^21 transactions // in a ledger, this is the least of our problems. if (x >= (1 << 21)) - return std::make_pair(false, - std::numeric_limits::max()); + return std::make_pair(false, std::numeric_limits::max()); return std::make_pair(true, (x * (x + 1) * (2 * x + 1)) / 6); } -} +} // namespace detail std::pair -TxQ::FeeMetrics::escalatedSeriesFeeLevel(Snapshot const& snapshot, - OpenView const& view, std::size_t extraCount, +TxQ::FeeMetrics::escalatedSeriesFeeLevel( + Snapshot const& snapshot, + OpenView const& view, + std::size_t extraCount, std::size_t seriesSize) { /* Transactions in the open ledger so far. @@ -247,19 +233,19 @@ TxQ::FeeMetrics::escalatedSeriesFeeLevel(Snapshot const& snapshot, // `sumNlast` definitely overflowed. Also the odds of this // are nearly nil. if (!sumNlast.first) - return { sumNlast.first, FeeLevel64{ sumNlast.second } }; - auto const totalFeeLevel = mulDiv(multiplier, - sumNlast.second - sumNcurrent.second, target * target); + return {sumNlast.first, FeeLevel64{sumNlast.second}}; + auto const totalFeeLevel = mulDiv( + multiplier, sumNlast.second - sumNcurrent.second, target * target); return totalFeeLevel; } - TxQ::MaybeTx::MaybeTx( std::shared_ptr const& txn_, - TxID const& txID_, FeeLevel64 feeLevel_, - ApplyFlags const flags_, - PreflightResult const& pfresult_) + TxID const& txID_, + FeeLevel64 feeLevel_, + ApplyFlags const flags_, + PreflightResult const& pfresult_) : txn(txn_) , feeLevel(feeLevel_) , txID(txID_) @@ -280,39 +266,32 @@ TxQ::MaybeTx::apply(Application& app, OpenView& view, beast::Journal j) { // If the rules or flags change, preflight again assert(pfresult); - if (pfresult->rules != view.rules() || - pfresult->flags != flags) + if (pfresult->rules != view.rules() || pfresult->flags != flags) { - JLOG(j.debug()) << "Queued transaction " << - txID << " rules or flags have changed. Flags from " << - pfresult->flags << " to " << flags ; + JLOG(j.debug()) << "Queued transaction " << txID + << " rules or flags have changed. Flags from " + << pfresult->flags << " to " << flags; pfresult.emplace( - preflight(app, view.rules(), - pfresult->tx, - flags, - pfresult->j)); + preflight(app, view.rules(), pfresult->tx, flags, pfresult->j)); } - auto pcresult = preclaim( - *pfresult, app, view); + auto pcresult = preclaim(*pfresult, app, view); return doApply(pcresult, app, view); } TxQ::TxQAccount::TxQAccount(std::shared_ptr const& txn) - :TxQAccount(txn->getAccountID(sfAccount)) + : TxQAccount(txn->getAccountID(sfAccount)) { } -TxQ::TxQAccount::TxQAccount(const AccountID& account_) - : account(account_) +TxQ::TxQAccount::TxQAccount(const AccountID& account_) : account(account_) { } auto -TxQ::TxQAccount::add(MaybeTx&& txn) - -> MaybeTx& +TxQ::TxQAccount::add(MaybeTx&& txn) -> MaybeTx& { auto sequence = txn.sequence; @@ -331,12 +310,8 @@ TxQ::TxQAccount::remove(TxSeq const& sequence) ////////////////////////////////////////////////////////////////////////// -TxQ::TxQ(Setup const& setup, - beast::Journal j) - : setup_(setup) - , j_(j) - , feeMetrics_(setup, j) - , maxSize_(boost::none) +TxQ::TxQ(Setup const& setup, beast::Journal j) + : setup_(setup), j_(j), feeMetrics_(setup, j), maxSize_(boost::none) { } @@ -345,30 +320,29 @@ TxQ::~TxQ() byFee_.clear(); } -template +template bool TxQ::isFull() const { - static_assert(fillPercentage > 0 && - fillPercentage <= 100, - "Invalid fill percentage"); - return maxSize_ && byFee_.size() >= - (*maxSize_ * fillPercentage / 100); + static_assert( + fillPercentage > 0 && fillPercentage <= 100, "Invalid fill percentage"); + return maxSize_ && byFee_.size() >= (*maxSize_ * fillPercentage / 100); } bool -TxQ::canBeHeld(STTx const& tx, ApplyFlags const flags, OpenView const& view, +TxQ::canBeHeld( + STTx const& tx, + ApplyFlags const flags, + OpenView const& view, AccountMap::iterator accountIter, - boost::optional replacementIter) + boost::optional replacementIter) { // PreviousTxnID is deprecated and should never be used // AccountTxnID is not supported by the transaction // queue yet, but should be added in the future // tapFAIL_HARD transactions are never held - bool canBeHeld = - ! tx.isFieldPresent(sfPreviousTxnID) && - ! tx.isFieldPresent(sfAccountTxnID) && - ! (flags & tapFAIL_HARD); + bool canBeHeld = !tx.isFieldPresent(sfPreviousTxnID) && + !tx.isFieldPresent(sfAccountTxnID) && !(flags & tapFAIL_HARD); if (canBeHeld) { /* To be queued and relayed, the transaction needs to @@ -376,8 +350,8 @@ TxQ::canBeHeld(STTx const& tx, ApplyFlags const flags, OpenView const& view, a realistic chance of getting into a ledger. */ auto const lastValid = getLastLedgerSequence(tx); - canBeHeld = !lastValid || *lastValid >= - view.info().seq + setup_.minimumLastLedgerBuffer; + canBeHeld = !lastValid || + *lastValid >= view.info().seq + setup_.minimumLastLedgerBuffer; } if (canBeHeld) { @@ -389,7 +363,7 @@ TxQ::canBeHeld(STTx const& tx, ApplyFlags const flags, OpenView const& view, // Allow if the account is not in the queue at all canBeHeld = accountIter == byAccount_.end(); - if(!canBeHeld) + if (!canBeHeld) { // Allow this tx to replace another one canBeHeld = replacementIter.is_initialized(); @@ -398,8 +372,8 @@ TxQ::canBeHeld(STTx const& tx, ApplyFlags const flags, OpenView const& view, if (!canBeHeld) { // Allow if there are fewer than the limit - canBeHeld = accountIter->second.getTxnCount() < - setup_.maximumTxnPerAccount; + canBeHeld = + accountIter->second.getTxnCount() < setup_.maximumTxnPerAccount; } if (!canBeHeld) @@ -436,8 +410,8 @@ TxQ::eraseAndAdvance(TxQ::FeeMultiSet::const_iterator_type candidateIter) -> FeeMultiSet::iterator_type { auto& txQAccount = byAccount_.at(candidateIter->account); - auto const accountIter = txQAccount.transactions.find( - candidateIter->sequence); + auto const accountIter = + txQAccount.transactions.find(candidateIter->sequence); assert(accountIter != txQAccount.transactions.end()); assert(accountIter == txQAccount.transactions.begin()); assert(byFee_.iterator_to(accountIter->second) == candidateIter); @@ -456,23 +430,22 @@ TxQ::eraseAndAdvance(TxQ::FeeMultiSet::const_iterator_type candidateIter) to head off potential ordering manipulation problems. */ auto const feeNextIter = std::next(candidateIter); - bool const useAccountNext = accountNextIter != txQAccount.transactions.end() && + bool const useAccountNext = + accountNextIter != txQAccount.transactions.end() && accountNextIter->first == candidateIter->sequence + 1 && - (feeNextIter == byFee_.end() || - accountNextIter->second.feeLevel > feeNextIter->feeLevel); + (feeNextIter == byFee_.end() || + accountNextIter->second.feeLevel > feeNextIter->feeLevel); auto const candidateNextIter = byFee_.erase(candidateIter); txQAccount.transactions.erase(accountIter); - return useAccountNext ? - byFee_.iterator_to(accountNextIter->second) : - candidateNextIter; - + return useAccountNext ? byFee_.iterator_to(accountNextIter->second) + : candidateNextIter; } auto -TxQ::erase(TxQ::TxQAccount& txQAccount, +TxQ::erase( + TxQ::TxQAccount& txQAccount, TxQ::TxQAccount::TxMap::const_iterator begin, - TxQ::TxQAccount::TxMap::const_iterator end) - -> TxQAccount::TxMap::iterator + TxQ::TxQAccount::TxMap::const_iterator end) -> TxQAccount::TxMap::iterator { for (auto it = begin; it != end; ++it) { @@ -482,12 +455,18 @@ TxQ::erase(TxQ::TxQAccount& txQAccount, } std::pair -TxQ::tryClearAccountQueue(Application& app, OpenView& view, - STTx const& tx, TxQ::AccountMap::iterator const& accountIter, - TxQAccount::TxMap::iterator beginTxIter, FeeLevel64 feeLevelPaid, - PreflightResult const& pfresult, std::size_t const txExtraCount, - ApplyFlags flags, FeeMetrics::Snapshot const& metricsSnapshot, - beast::Journal j) +TxQ::tryClearAccountQueue( + Application& app, + OpenView& view, + STTx const& tx, + TxQ::AccountMap::iterator const& accountIter, + TxQAccount::TxMap::iterator beginTxIter, + FeeLevel64 feeLevelPaid, + PreflightResult const& pfresult, + std::size_t const txExtraCount, + ApplyFlags flags, + FeeMetrics::Snapshot const& metricsSnapshot, + beast::Journal j) { auto const tSeq = tx.getSequence(); assert(beginTxIter != accountIter->second.transactions.end()); @@ -506,10 +485,11 @@ TxQ::tryClearAccountQueue(Application& app, OpenView& view, // from [aSeq, tSeq) auto endTxIter = accountIter->second.transactions.lower_bound(tSeq); - auto const totalFeeLevelPaid = std::accumulate(beginTxIter, endTxIter, + auto const totalFeeLevelPaid = std::accumulate( + beginTxIter, + endTxIter, feeLevelPaid, - [](auto const& total, auto const& txn) - { + [](auto const& total, auto const& txn) { return total + txn.second.feeLevel; }); @@ -536,11 +516,12 @@ TxQ::tryClearAccountQueue(Application& app, OpenView& view, } // Apply the current tx. Because the state of the view has been changed // by the queued txs, we also need to preclaim again. - auto const txResult = doApply (preclaim (pfresult, app, view), app, view); + auto const txResult = doApply(preclaim(pfresult, app, view), app, view); if (txResult.second) { - // All of the queued transactions applied, so remove them from the queue. + // All of the queued transactions applied, so remove them from the + // queue. endTxIter = erase(accountIter->second, beginTxIter, endTxIter); // If `tx` is replacing a queued tx, delete that one, too. if (endTxIter != accountIter->second.transactions.end() && @@ -551,7 +532,6 @@ TxQ::tryClearAccountQueue(Application& app, OpenView& view, return txResult; } - /* How the decision to apply, queue, or reject is made: 1. Does `preflight` indicate that the tx is valid? @@ -614,9 +594,12 @@ TxQ::tryClearAccountQueue(Application& app, OpenView& view, 8. Put `txn` in the queue. */ std::pair -TxQ::apply(Application& app, OpenView& view, +TxQ::apply( + Application& app, + OpenView& view, std::shared_ptr const& tx, - ApplyFlags flags, beast::Journal j) + ApplyFlags flags, + beast::Journal j) { auto const account = (*tx)[sfAccount]; auto const transactionID = tx->getTransactionID(); @@ -624,10 +607,9 @@ TxQ::apply(Application& app, OpenView& view, // See if the transaction is valid, properly formed, // etc. before doing potentially expensive queue // replace and multi-transaction operations. - auto const pfresult = preflight(app, view.rules(), - *tx, flags, j); + auto const pfresult = preflight(app, view.rules(), *tx, flags, j); if (pfresult.ter != tesSUCCESS) - return{ pfresult.ter, false }; + return {pfresult.ter, false}; struct MultiTxn { @@ -655,12 +637,10 @@ TxQ::apply(Application& app, OpenView& view, // or transaction replacement, so just pull it up now. // TODO: Do we want to avoid doing it again during // preclaim? - auto const baseFee = view.fees().toDrops( - calculateBaseFee(view, *tx)).second; - auto const feeLevelPaid = getFeeLevelPaid(*tx, - baseLevel, baseFee, setup_); - auto const requiredFeeLevel = [&]() - { + auto const baseFee = + view.fees().toDrops(calculateBaseFee(view, *tx)).second; + auto const feeLevelPaid = getFeeLevelPaid(*tx, baseLevel, baseFee, setup_); + auto const requiredFeeLevel = [&]() { auto feeLevel = FeeMetrics::scaleFeeLevel(metricsSnapshot, view); if ((flags & tapPREFER_QUEUE) && byFee_.size()) { @@ -683,19 +663,17 @@ TxQ::apply(Application& app, OpenView& view, // Is the current transaction's fee higher than // the queued transaction's fee + a percentage auto requiredRetryLevel = increase( - existingIter->second.feeLevel, - setup_.retrySequencePercent); - JLOG(j_.trace()) << "Found transaction in queue for account " << - account << " with sequence number " << tSeq << - " new txn fee level is " << feeLevelPaid << - ", old txn fee level is " << - existingIter->second.feeLevel << - ", new txn needs fee level of " << - requiredRetryLevel; - if (feeLevelPaid > requiredRetryLevel - || (existingIter->second.feeLevel < requiredFeeLevel && - feeLevelPaid >= requiredFeeLevel && - existingIter == txQAcct.transactions.begin())) + existingIter->second.feeLevel, setup_.retrySequencePercent); + JLOG(j_.trace()) + << "Found transaction in queue for account " << account + << " with sequence number " << tSeq << " new txn fee level is " + << feeLevelPaid << ", old txn fee level is " + << existingIter->second.feeLevel + << ", new txn needs fee level of " << requiredRetryLevel; + if (feeLevelPaid > requiredRetryLevel || + (existingIter->second.feeLevel < requiredFeeLevel && + feeLevelPaid >= requiredFeeLevel && + existingIter == txQAcct.transactions.begin())) { /* Either the fee is high enough to retry or the prior txn is the first for this account, and @@ -720,29 +698,24 @@ TxQ::apply(Application& app, OpenView& view, TxConsequences::normal) { assert(!consequences); - consequences.emplace(calculateConsequences( - pfresult)); - if (consequences->category == - TxConsequences::blocker) + consequences.emplace(calculateConsequences(pfresult)); + if (consequences->category == TxConsequences::blocker) { // Can't replace a normal transaction in the // middle of the queue with a blocker. - JLOG(j_.trace()) << - "Ignoring blocker transaction " << - transactionID << - " in favor of normal queued " << - existingIter->second.txID; - return {telCAN_NOT_QUEUE_BLOCKS, false }; + JLOG(j_.trace()) << "Ignoring blocker transaction " + << transactionID + << " in favor of normal queued " + << existingIter->second.txID; + return {telCAN_NOT_QUEUE_BLOCKS, false}; } } } - // Remove the queued transaction and continue - JLOG(j_.trace()) << - "Removing transaction from queue " << - existingIter->second.txID << - " in favor of " << transactionID; + JLOG(j_.trace()) << "Removing transaction from queue " + << existingIter->second.txID << " in favor of " + << transactionID; // Then save the queued tx to remove from the queue if // the new tx succeeds or gets queued. DO NOT REMOVE // if the new tx fails, because there may be other txs @@ -757,12 +730,10 @@ TxQ::apply(Application& app, OpenView& view, else { // Drop the current transaction - JLOG(j_.trace()) << - "Ignoring transaction " << - transactionID << - " in favor of queued " << - existingIter->second.txID; - return{ telCAN_NOT_QUEUE_FEE, false }; + JLOG(j_.trace()) + << "Ignoring transaction " << transactionID + << " in favor of queued " << existingIter->second.txID; + return {telCAN_NOT_QUEUE_FEE, false}; } } } @@ -783,7 +754,8 @@ TxQ::apply(Application& app, OpenView& view, // object to hold the info we need to adjust for // prior txns. Otherwise, let preclaim fail as if // we didn't have the queue at all. - if (canBeHeld(*tx, flags, view, accountIter, replacedItemDeleteIter)) + if (canBeHeld( + *tx, flags, view, accountIter, replacedItemDeleteIter)) multiTxn.emplace(); } @@ -798,10 +770,9 @@ TxQ::apply(Application& app, OpenView& view, auto workingIter = multiTxn->nextTxIter; auto workingSeq = aSeq; for (; workingIter != txQAcct.transactions.end(); - ++workingIter, ++workingSeq) + ++workingIter, ++workingSeq) { - if (workingSeq < tSeq && - workingIter->first != workingSeq) + if (workingSeq < tSeq && workingIter->first != workingSeq) { // If any transactions are missing before `tx`, abort. multiTxn.reset(); @@ -818,15 +789,13 @@ TxQ::apply(Application& app, OpenView& view, if (feeLevelPaid <= requiredMultiLevel) { // Drop the current transaction - JLOG(j_.trace()) << - "Ignoring transaction " << - transactionID << - ". Needs fee level of " << + JLOG(j_.trace()) + << "Ignoring transaction " << transactionID + << ". Needs fee level of " << - requiredMultiLevel << - ". Only paid " << - feeLevelPaid; - return{ telINSUF_FEE_P, false }; + requiredMultiLevel << ". Only paid " + << feeLevelPaid; + return {telINSUF_FEE_P, false}; } } if (workingIter->first == tSeq) @@ -834,9 +803,8 @@ TxQ::apply(Application& app, OpenView& view, // If we're replacing this transaction, don't // count it. assert(replacedItemDeleteIter); - multiTxn->includeCurrentFee = - std::next(workingIter) != - txQAcct.transactions.end(); + multiTxn->includeCurrentFee = std::next(workingIter) != + txQAcct.transactions.end(); continue; } if (!workingIter->second.consequences) @@ -851,15 +819,13 @@ TxQ::apply(Application& app, OpenView& view, { // Drop the current transaction, because it's // blocked by workingIter. - JLOG(j_.trace()) << - "Ignoring transaction " << - transactionID << - ". A blocker-type transaction " << - "is in the queue."; - return{ telCAN_NOT_QUEUE_BLOCKED, false }; + JLOG(j_.trace()) + << "Ignoring transaction " << transactionID + << ". A blocker-type transaction " + << "is in the queue."; + return {telCAN_NOT_QUEUE_BLOCKED, false}; } - multiTxn->fee += - workingIter->second.consequences->fee; + multiTxn->fee += workingIter->second.consequences->fee; multiTxn->potentialSpend += workingIter->second.consequences->potentialSpend; } @@ -920,32 +886,29 @@ TxQ::apply(Application& app, OpenView& view, auto totalFee = multiTxn->fee; if (multiTxn->includeCurrentFee) totalFee += (*tx)[sfFee].xrp(); - if (totalFee >= balance || - totalFee >= reserve) + if (totalFee >= balance || totalFee >= reserve) { // Drop the current transaction - JLOG(j_.trace()) << - "Ignoring transaction " << - transactionID << - ". Total fees in flight too high."; - return{ telCAN_NOT_QUEUE_BALANCE, false }; + JLOG(j_.trace()) << "Ignoring transaction " << transactionID + << ". Total fees in flight too high."; + return {telCAN_NOT_QUEUE_BALANCE, false}; } // Create the test view from the current view multiTxn->applyView.emplace(&view, flags); multiTxn->openView.emplace(&*multiTxn->applyView); - auto const sleBump = multiTxn->applyView->peek( - keylet::account(account)); + auto const sleBump = + multiTxn->applyView->peek(keylet::account(account)); if (!sleBump) return {tefINTERNAL, false}; auto const potentialTotalSpend = multiTxn->fee + std::min(balance - std::min(balance, reserve), - multiTxn->potentialSpend); + multiTxn->potentialSpend); assert(potentialTotalSpend > XRPAmount{0}); - sleBump->setFieldAmount(sfBalance, - balance - potentialTotalSpend); + sleBump->setFieldAmount( + sfBalance, balance - potentialTotalSpend); sleBump->setFieldU32(sfSequence, tSeq); } } @@ -953,21 +916,19 @@ TxQ::apply(Application& app, OpenView& view, // See if the transaction is likely to claim a fee. assert(!multiTxn || multiTxn->openView); - auto const pcresult = preclaim(pfresult, app, - multiTxn ? *multiTxn->openView : view); + auto const pcresult = + preclaim(pfresult, app, multiTxn ? *multiTxn->openView : view); if (!pcresult.likelyToClaimFee) - return{ pcresult.ter, false }; + return {pcresult.ter, false}; // Too low of a fee should get caught by preclaim assert(feeLevelPaid >= baseLevel); - JLOG(j_.trace()) << "Transaction " << - transactionID << - " from account " << account << - " has fee level of " << feeLevelPaid << - " needs at least " << requiredFeeLevel << - " to get in the open ledger, which has " << - view.txCount() << " entries."; + JLOG(j_.trace()) << "Transaction " << transactionID << " from account " + << account << " has fee level of " << feeLevelPaid + << " needs at least " << requiredFeeLevel + << " to get in the open ledger, which has " + << view.txCount() << " entries."; /* Quick heuristic check to see if it's worth checking that this tx has a high enough fee to clear all the txs in the queue. @@ -990,15 +951,23 @@ TxQ::apply(Application& app, OpenView& view, multiTxn.is_initialized() && multiTxn->nextTxIter->second.retriesRemaining == MaybeTx::retriesAllowed && - feeLevelPaid > requiredFeeLevel && - requiredFeeLevel > baseLevel && - baseFee != 0) + feeLevelPaid > requiredFeeLevel && requiredFeeLevel > baseLevel && + baseFee != 0) { OpenView sandbox(open_ledger, &view, view.rules()); - auto result = tryClearAccountQueue(app, sandbox, *tx, accountIter, - multiTxn->nextTxIter, feeLevelPaid, pfresult, view.txCount(), - flags, metricsSnapshot, j); + auto result = tryClearAccountQueue( + app, + sandbox, + *tx, + accountIter, + multiTxn->nextTxIter, + feeLevelPaid, + pfresult, + view.txCount(), + flags, + metricsSnapshot, + j); if (result.second) { sandbox.apply(view); @@ -1014,32 +983,29 @@ TxQ::apply(Application& app, OpenView& view, { // Transaction fee is sufficient to go in open ledger immediately - JLOG(j_.trace()) << "Applying transaction " << - transactionID << - " to open ledger."; + JLOG(j_.trace()) << "Applying transaction " << transactionID + << " to open ledger."; auto const [txnResult, didApply] = doApply(pcresult, app, view); - JLOG(j_.trace()) << "New transaction " << - transactionID << - (didApply ? " applied successfully with " : - " failed with ") << - transToken(txnResult); + JLOG(j_.trace()) << "New transaction " << transactionID + << (didApply ? " applied successfully with " + : " failed with ") + << transToken(txnResult); if (didApply && replacedItemDeleteIter) erase(*replacedItemDeleteIter); - return { txnResult, didApply }; + return {txnResult, didApply}; } // If `multiTxn` has a value, then `canBeHeld` has already been verified - if (! multiTxn && - ! canBeHeld(*tx, flags, view, accountIter, replacedItemDeleteIter)) + if (!multiTxn && + !canBeHeld(*tx, flags, view, accountIter, replacedItemDeleteIter)) { // Bail, transaction cannot be held - JLOG(j_.trace()) << "Transaction " << - transactionID << - " can not be held"; - return { telCAN_NOT_QUEUE, false }; + JLOG(j_.trace()) << "Transaction " << transactionID + << " can not be held"; + return {telCAN_NOT_QUEUE, false}; } // If the queue is full, decide whether to drop the current @@ -1050,41 +1016,39 @@ TxQ::apply(Application& app, OpenView& view, auto lastRIter = byFee_.rbegin(); if (lastRIter->account == account) { - JLOG(j_.warn()) << "Queue is full, and transaction " << - transactionID << - " would kick a transaction from the same account (" << - account << ") out of the queue."; - return { telCAN_NOT_QUEUE_FULL, false }; + JLOG(j_.warn()) + << "Queue is full, and transaction " << transactionID + << " would kick a transaction from the same account (" + << account << ") out of the queue."; + return {telCAN_NOT_QUEUE_FULL, false}; } auto const& endAccount = byAccount_.at(lastRIter->account); - auto endEffectiveFeeLevel = [&]() - { + auto endEffectiveFeeLevel = [&]() { // Compute the average of all the txs for the endAccount, // but only if the last tx in the queue has a lower fee // level than this candidate tx. - if (lastRIter->feeLevel > feeLevelPaid - || endAccount.transactions.size() == 1) + if (lastRIter->feeLevel > feeLevelPaid || + endAccount.transactions.size() == 1) return lastRIter->feeLevel; - constexpr FeeLevel64 max{ - std::numeric_limits::max() }; - auto endTotal = std::accumulate(endAccount.transactions.begin(), + constexpr FeeLevel64 max{std::numeric_limits::max()}; + auto endTotal = std::accumulate( + endAccount.transactions.begin(), endAccount.transactions.end(), - std::pair(0, 0), - [&](auto const& total, auto const& txn) - { - // Check for overflow. - auto next = txn.second.feeLevel / - endAccount.transactions.size(); - auto mod = txn.second.feeLevel % - endAccount.transactions.size(); - if (total.first >= max - next || - total.second >= max - mod) - return std::make_pair(max, FeeLevel64 { 0 }); - return std::make_pair(total.first + next, total.second + mod); - }); - return endTotal.first + endTotal.second / - endAccount.transactions.size(); + std::pair(0, 0), + [&](auto const& total, auto const& txn) { + // Check for overflow. + auto next = + txn.second.feeLevel / endAccount.transactions.size(); + auto mod = + txn.second.feeLevel % endAccount.transactions.size(); + if (total.first >= max - next || total.second >= max - mod) + return std::make_pair(max, FeeLevel64{0}); + return std::make_pair( + total.first + next, total.second + mod); + }); + return endTotal.first + + endTotal.second / endAccount.transactions.size(); }(); if (feeLevelPaid > endEffectiveFeeLevel) { @@ -1092,21 +1056,19 @@ TxQ::apply(Application& app, OpenView& view, // valuable, so kick out the cheapest transaction. auto dropRIter = endAccount.transactions.rbegin(); assert(dropRIter->second.account == lastRIter->account); - JLOG(j_.warn()) << - "Removing last item of account " << - lastRIter->account << - " from queue with average fee of " << - endEffectiveFeeLevel << " in favor of " << - transactionID << " with fee of " << - feeLevelPaid; + JLOG(j_.warn()) + << "Removing last item of account " << lastRIter->account + << " from queue with average fee of " << endEffectiveFeeLevel + << " in favor of " << transactionID << " with fee of " + << feeLevelPaid; erase(byFee_.iterator_to(dropRIter->second)); } else { - JLOG(j_.warn()) << "Queue is full, and transaction " << - transactionID << - " fee is lower than end item's account average fee"; - return { telCAN_NOT_QUEUE_FULL, false }; + JLOG(j_.warn()) + << "Queue is full, and transaction " << transactionID + << " fee is lower than end item's account average fee"; + return {telCAN_NOT_QUEUE_FULL, false}; } } @@ -1117,8 +1079,8 @@ TxQ::apply(Application& app, OpenView& view, { // Create a new TxQAccount object and add the byAccount lookup. bool created; - std::tie(accountIter, created) = byAccount_.emplace( - account, TxQAccount(tx)); + std::tie(accountIter, created) = + byAccount_.emplace(account, TxQAccount(tx)); (void)created; assert(created); } @@ -1134,7 +1096,7 @@ TxQ::apply(Application& app, OpenView& view, flags &= ~tapPREFER_QUEUE; auto& candidate = accountIter->second.add( - { tx, transactionID, feeLevelPaid, flags, pfresult }); + {tx, transactionID, feeLevelPaid, flags, pfresult}); /* Normally we defer figuring out the consequences until something later requires us to, but if we know the consequences now, save them for later. @@ -1143,13 +1105,13 @@ TxQ::apply(Application& app, OpenView& view, candidate.consequences.emplace(*consequences); // Then index it into the byFee lookup. byFee_.insert(candidate); - JLOG(j_.debug()) << "Added transaction " << candidate.txID << - " with result " << transToken(pfresult.ter) << - " from " << (accountExists ? "existing" : "new") << - " account " << candidate.account << " to queue." << - " Flags: " << flags; + JLOG(j_.debug()) << "Added transaction " << candidate.txID + << " with result " << transToken(pfresult.ter) << " from " + << (accountExists ? "existing" : "new") << " account " + << candidate.account << " to queue." + << " Flags: " << flags; - return { terQUEUED, false }; + return {terQUEUED, false}; } /* @@ -1165,8 +1127,7 @@ TxQ::apply(Application& app, OpenView& view, */ void -TxQ::processClosedLedger(Application& app, - ReadView const& view, bool timeLeap) +TxQ::processClosedLedger(Application& app, ReadView const& view, bool timeLeap) { std::lock_guard lock(mutex_); @@ -1176,14 +1137,13 @@ TxQ::processClosedLedger(Application& app, auto ledgerSeq = view.info().seq; if (!timeLeap) - maxSize_ = std::max (snapshot.txnsExpected * setup_.ledgersInQueue, - setup_.queueSizeMin); + maxSize_ = std::max( + snapshot.txnsExpected * setup_.ledgersInQueue, setup_.queueSizeMin); // Remove any queued candidates whose LastLedgerSequence has gone by. - for(auto candidateIter = byFee_.begin(); candidateIter != byFee_.end(); ) + for (auto candidateIter = byFee_.begin(); candidateIter != byFee_.end();) { - if (candidateIter->lastValid - && *candidateIter->lastValid <= ledgerSeq) + if (candidateIter->lastValid && *candidateIter->lastValid <= ledgerSeq) { byAccount_.at(candidateIter->account).dropPenalty = true; candidateIter = erase(candidateIter); @@ -1197,7 +1157,7 @@ TxQ::processClosedLedger(Application& app, // Remove any TxQAccounts that don't have candidates // under them for (auto txQAccountIter = byAccount_.begin(); - txQAccountIter != byAccount_.end();) + txQAccountIter != byAccount_.end();) { if (txQAccountIter->second.empty()) txQAccountIter = byAccount_.erase(txQAccountIter); @@ -1236,8 +1196,7 @@ TxQ::processClosedLedger(Application& app, * the next tx in the queue, simply ordered by fee. */ bool -TxQ::accept(Application& app, - OpenView& view) +TxQ::accept(Application& app, OpenView& view) { /* Move transactions from the queue from largest fee level to smallest. As we add more transactions, the required fee level will increase. @@ -1254,96 +1213,93 @@ TxQ::accept(Application& app, for (auto candidateIter = byFee_.begin(); candidateIter != byFee_.end();) { auto& account = byAccount_.at(candidateIter->account); - if (candidateIter->sequence > - account.transactions.begin()->first) + if (candidateIter->sequence > account.transactions.begin()->first) { // This is not the first transaction for this account, so skip it. // It can not succeed yet. - JLOG(j_.trace()) << "Skipping queued transaction " << - candidateIter->txID << " from account " << - candidateIter->account << " as it is not the first."; + JLOG(j_.trace()) + << "Skipping queued transaction " << candidateIter->txID + << " from account " << candidateIter->account + << " as it is not the first."; candidateIter++; continue; } - auto const requiredFeeLevel = FeeMetrics::scaleFeeLevel( - metricSnapshot, view); + auto const requiredFeeLevel = + FeeMetrics::scaleFeeLevel(metricSnapshot, view); auto const feeLevelPaid = candidateIter->feeLevel; - JLOG(j_.trace()) << "Queued transaction " << - candidateIter->txID << " from account " << - candidateIter->account << " has fee level of " << - feeLevelPaid << " needs at least " << - requiredFeeLevel; + JLOG(j_.trace()) << "Queued transaction " << candidateIter->txID + << " from account " << candidateIter->account + << " has fee level of " << feeLevelPaid + << " needs at least " << requiredFeeLevel; if (feeLevelPaid >= requiredFeeLevel) { auto firstTxn = candidateIter->txn; - JLOG(j_.trace()) << "Applying queued transaction " << - candidateIter->txID << " to open ledger."; + JLOG(j_.trace()) << "Applying queued transaction " + << candidateIter->txID << " to open ledger."; - auto const [txnResult, didApply] = candidateIter->apply(app, view, j_); + auto const [txnResult, didApply] = + candidateIter->apply(app, view, j_); if (didApply) { // Remove the candidate from the queue - JLOG(j_.debug()) << "Queued transaction " << - candidateIter->txID << - " applied successfully with " << - transToken(txnResult) << ". Remove from queue."; + JLOG(j_.debug()) + << "Queued transaction " << candidateIter->txID + << " applied successfully with " << transToken(txnResult) + << ". Remove from queue."; candidateIter = eraseAndAdvance(candidateIter); ledgerChanged = true; } - else if (isTefFailure(txnResult) || isTemMalformed(txnResult) || + else if ( + isTefFailure(txnResult) || isTemMalformed(txnResult) || candidateIter->retriesRemaining <= 0) { if (candidateIter->retriesRemaining <= 0) account.retryPenalty = true; else account.dropPenalty = true; - JLOG(j_.debug()) << "Queued transaction " << - candidateIter->txID << " failed with " << - transToken(txnResult) << ". Remove from queue."; + JLOG(j_.debug()) << "Queued transaction " << candidateIter->txID + << " failed with " << transToken(txnResult) + << ". Remove from queue."; candidateIter = eraseAndAdvance(candidateIter); } else { - JLOG(j_.debug()) << "Queued transaction " << - candidateIter->txID << " failed with " << - transToken(txnResult) << ". Leave in queue." << - " Applied: " << didApply << - ". Flags: " << - candidateIter->flags; - if (account.retryPenalty && - candidateIter->retriesRemaining > 2) + JLOG(j_.debug()) << "Queued transaction " << candidateIter->txID + << " failed with " << transToken(txnResult) + << ". Leave in queue." + << " Applied: " << didApply + << ". Flags: " << candidateIter->flags; + if (account.retryPenalty && candidateIter->retriesRemaining > 2) candidateIter->retriesRemaining = 1; else --candidateIter->retriesRemaining; candidateIter->lastResult = txnResult; - if (account.dropPenalty && - account.transactions.size() > 1 && isFull<95>()) + if (account.dropPenalty && account.transactions.size() > 1 && + isFull<95>()) { /* The queue is close to full, this account has multiple txs queued, and this account has had a transaction fail. Even though we're giving this transaction another chance, chances are it won't recover. So we don't make - things worse, drop the _last_ transaction for this account. + things worse, drop the _last_ transaction for this + account. */ auto dropRIter = account.transactions.rbegin(); assert(dropRIter->second.account == candidateIter->account); - JLOG(j_.warn()) << - "Queue is nearly full, and transaction " << - candidateIter->txID << " failed with " << - transToken(txnResult) << - ". Removing last item of account " << - account.account; + JLOG(j_.warn()) << "Queue is nearly full, and transaction " + << candidateIter->txID << " failed with " + << transToken(txnResult) + << ". Removing last item of account " + << account.account; auto endIter = byFee_.iterator_to(dropRIter->second); assert(endIter != candidateIter); erase(endIter); - } ++candidateIter; } - } else { @@ -1368,9 +1324,8 @@ TxQ::getMetrics(OpenView const& view) const result.txInLedger = view.txCount(); result.txPerLedger = snapshot.txnsExpected; result.referenceFeeLevel = baseLevel; - result.minProcessingFeeLevel = isFull() ? - byFee_.rbegin()->feeLevel + FeeLevel64{ 1 } : - baseLevel; + result.minProcessingFeeLevel = + isFull() ? byFee_.rbegin()->feeLevel + FeeLevel64{1} : baseLevel; result.medFeeLevel = snapshot.escalationMultiplier; result.openLedgerFeeLevel = FeeMetrics::scaleFeeLevel(snapshot, view); @@ -1378,7 +1333,8 @@ TxQ::getMetrics(OpenView const& view) const } TxQ::FeeAndSeq -TxQ::getTxRequiredFeeAndSeq(OpenView const& view, +TxQ::getTxRequiredFeeAndSeq( + OpenView const& view, std::shared_ptr const& tx) const { auto const account = (*tx)[sfAccount]; @@ -1386,8 +1342,8 @@ TxQ::getTxRequiredFeeAndSeq(OpenView const& view, std::lock_guard lock(mutex_); auto const snapshot = feeMetrics_.getSnapshot(); - auto const baseFee = view.fees().toDrops( - calculateBaseFee(view, *tx)).second; + auto const baseFee = + view.fees().toDrops(calculateBaseFee(view, *tx)).second; auto const fee = FeeMetrics::scaleFeeLevel(snapshot, view); auto const accountSeq = [&view, &account]() -> std::uint32_t { @@ -1399,7 +1355,7 @@ TxQ::getTxRequiredFeeAndSeq(OpenView const& view, auto availableSeq = accountSeq; - if (auto iter {byAccount_.find(account)}; iter != byAccount_.end()) + if (auto iter{byAccount_.find(account)}; iter != byAccount_.end()) { auto& txQAcct = iter->second; for (auto const& [seq, _] : txQAcct.transactions) @@ -1428,8 +1384,7 @@ TxQ::getAccountTxs(AccountID const& account, ReadView const& view) const for (auto const& tx : accountIter->second.transactions) { - result.emplace(tx.first, [&] - { + result.emplace(tx.first, [&] { AccountTxDetails resultTx; resultTx.feeLevel = tx.second.feeLevel; if (tx.second.lastValid) @@ -1443,8 +1398,7 @@ TxQ::getAccountTxs(AccountID const& account, ReadView const& view) const } auto -TxQ::getTxs(ReadView const& view) const --> std::vector +TxQ::getTxs(ReadView const& view) const -> std::vector { std::lock_guard lock(mutex_); @@ -1456,8 +1410,7 @@ TxQ::getTxs(ReadView const& view) const for (auto const& tx : byFee_) { - result.emplace_back([&] - { + result.emplace_back([&] { TxDetails resultTx; resultTx.feeLevel = tx.feeLevel; if (tx.lastValid) @@ -1477,7 +1430,6 @@ TxQ::getTxs(ReadView const& view) const return result; } - Json::Value TxQ::doRPC(Application& app) const { @@ -1510,14 +1462,14 @@ TxQ::doRPC(Application& app) const auto& drops = ret[jss::drops] = Json::Value(); // Don't care about the overflow flags - drops[jss::base_fee] = to_string(toDrops( - metrics.referenceFeeLevel, baseFee).second); - drops[jss::minimum_fee] = to_string(toDrops( - metrics.minProcessingFeeLevel, baseFee).second); - drops[jss::median_fee] = to_string(toDrops( - metrics.medFeeLevel, baseFee).second); - drops[jss::open_ledger_fee] = to_string(toDrops( - metrics.openLedgerFeeLevel - FeeLevel64{ 1 }, baseFee).second + + drops[jss::base_fee] = + to_string(toDrops(metrics.referenceFeeLevel, baseFee).second); + drops[jss::minimum_fee] = + to_string(toDrops(metrics.minProcessingFeeLevel, baseFee).second); + drops[jss::median_fee] = + to_string(toDrops(metrics.medFeeLevel, baseFee).second); + drops[jss::open_ledger_fee] = to_string( + toDrops(metrics.openLedgerFeeLevel - FeeLevel64{1}, baseFee).second + 1); return ret; @@ -1535,10 +1487,12 @@ setup_TxQ(Config const& config) set(setup.retrySequencePercent, "retry_sequence_percent", section); set(setup.multiTxnPercent, "multi_txn_percent", section); set(setup.minimumEscalationMultiplier, - "minimum_escalation_multiplier", section); + "minimum_escalation_multiplier", + section); set(setup.minimumTxnInLedger, "minimum_txn_in_ledger", section); set(setup.minimumTxnInLedgerSA, - "minimum_txn_in_ledger_standalone", section); + "minimum_txn_in_ledger_standalone", + section); set(setup.targetTxnInLedger, "target_txn_in_ledger", section); std::uint32_t max; if (set(max, "maximum_txn_in_ledger", section)) @@ -1549,8 +1503,7 @@ setup_TxQ(Config const& config) "The minimum number of low-fee transactions allowed " "per ledger (minimum_txn_in_ledger) exceeds " "the maximum number of low-fee transactions allowed per " - "ledger (maximum_txn_in_ledger)." - ); + "ledger (maximum_txn_in_ledger)."); } if (max < setup.minimumTxnInLedgerSA) { @@ -1558,8 +1511,7 @@ setup_TxQ(Config const& config) "The minimum number of low-fee transactions allowed " "per ledger (minimum_txn_in_ledger_standalone) exceeds " "the maximum number of low-fee transactions allowed per " - "ledger (maximum_txn_in_ledger)." - ); + "ledger (maximum_txn_in_ledger)."); } setup.maximumTxnInLedger.emplace(max); @@ -1572,34 +1524,35 @@ setup_TxQ(Config const& config) minimum_txn_in_ledger.) */ set(setup.normalConsensusIncreasePercent, - "normal_consensus_increase_percent", section); - setup.normalConsensusIncreasePercent = boost::algorithm::clamp( - setup.normalConsensusIncreasePercent, 0, 1000); + "normal_consensus_increase_percent", + section); + setup.normalConsensusIncreasePercent = + boost::algorithm::clamp(setup.normalConsensusIncreasePercent, 0, 1000); /* If this percentage is outside of the 0-100 range, the results are nonsensical (uint overflows happen, so the limit grows instead of shrinking). 0 is not recommended. */ - set(setup.slowConsensusDecreasePercent, "slow_consensus_decrease_percent", + set(setup.slowConsensusDecreasePercent, + "slow_consensus_decrease_percent", section); - setup.slowConsensusDecreasePercent = boost::algorithm::clamp( - setup.slowConsensusDecreasePercent, 0, 100); + setup.slowConsensusDecreasePercent = + boost::algorithm::clamp(setup.slowConsensusDecreasePercent, 0, 100); set(setup.maximumTxnPerAccount, "maximum_txn_per_account", section); - set(setup.minimumLastLedgerBuffer, - "minimum_last_ledger_buffer", section); + set(setup.minimumLastLedgerBuffer, "minimum_last_ledger_buffer", section); set(setup.zeroBaseFeeTransactionFeeLevel, - "zero_basefee_transaction_feelevel", section); + "zero_basefee_transaction_feelevel", + section); setup.standAlone = config.standalone(); return setup; } - std::unique_ptr make_TxQ(TxQ::Setup const& setup, beast::Journal j) { return std::make_unique(setup, std::move(j)); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/impl/ValidatorKeys.cpp b/src/ripple/app/misc/impl/ValidatorKeys.cpp index b630300100..6d6c166ed1 100644 --- a/src/ripple/app/misc/impl/ValidatorKeys.cpp +++ b/src/ripple/app/misc/impl/ValidatorKeys.cpp @@ -20,8 +20,8 @@ #include #include -#include #include +#include #include #include @@ -42,15 +42,16 @@ ValidatorKeys::ValidatorKeys(Config const& config, beast::Journal j) if (auto const token = ValidatorToken::make_ValidatorToken( config.section(SECTION_VALIDATOR_TOKEN).lines())) { - auto const pk = derivePublicKey( - KeyType::secp256k1, token->validationSecret); + auto const pk = + derivePublicKey(KeyType::secp256k1, token->validationSecret); auto const m = deserializeManifest(base64_decode(token->manifest)); - if (! m || pk != m->signingKey) + if (!m || pk != m->signingKey) { configInvalid_ = true; JLOG(j.fatal()) - << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN "]"; + << "Invalid token specified in [" SECTION_VALIDATOR_TOKEN + "]"; } else { @@ -74,8 +75,8 @@ ValidatorKeys::ValidatorKeys(Config const& config, beast::Journal j) if (!seed) { configInvalid_ = true; - JLOG(j.fatal()) << - "Invalid seed specified in [" SECTION_VALIDATION_SEED "]"; + JLOG(j.fatal()) + << "Invalid seed specified in [" SECTION_VALIDATION_SEED "]"; } else { diff --git a/src/ripple/app/misc/impl/ValidatorList.cpp b/src/ripple/app/misc/impl/ValidatorList.cpp index e9bee97c9b..4712483325 100644 --- a/src/ripple/app/misc/impl/ValidatorList.cpp +++ b/src/ripple/app/misc/impl/ValidatorList.cpp @@ -17,12 +17,12 @@ */ //============================================================================== -#include #include -#include +#include #include #include #include +#include #include #include #include @@ -60,74 +60,73 @@ to_string(ListDisposition disposition) const std::string ValidatorList::filePrefix_ = "cache."; -ValidatorList::ValidatorList ( +ValidatorList::ValidatorList( ManifestCache& validatorManifests, ManifestCache& publisherManifests, TimeKeeper& timeKeeper, std::string const& databasePath, beast::Journal j, boost::optional minimumQuorum) - : validatorManifests_ (validatorManifests) - , publisherManifests_ (publisherManifests) - , timeKeeper_ (timeKeeper) + : validatorManifests_(validatorManifests) + , publisherManifests_(publisherManifests) + , timeKeeper_(timeKeeper) , dataPath_(databasePath) - , j_ (j) - , quorum_ (minimumQuorum.value_or(1)) // Genesis ledger quorum - , minimumQuorum_ (minimumQuorum) + , j_(j) + , quorum_(minimumQuorum.value_or(1)) // Genesis ledger quorum + , minimumQuorum_(minimumQuorum) { } bool -ValidatorList::load ( +ValidatorList::load( PublicKey const& localSigningKey, std::vector const& configKeys, std::vector const& publisherKeys) { - static boost::regex const re ( - "[[:space:]]*" // skip leading whitespace - "([[:alnum:]]+)" // node identity - "(?:" // begin optional comment block - "[[:space:]]+" // (skip all leading whitespace) - "(?:" // begin optional comment - "(.*[^[:space:]]+)" // the comment - "[[:space:]]*" // (skip all trailing whitespace) - ")?" // end optional comment - ")?" // end optional comment block + static boost::regex const re( + "[[:space:]]*" // skip leading whitespace + "([[:alnum:]]+)" // node identity + "(?:" // begin optional comment block + "[[:space:]]+" // (skip all leading whitespace) + "(?:" // begin optional comment + "(.*[^[:space:]]+)" // the comment + "[[:space:]]*" // (skip all trailing whitespace) + ")?" // end optional comment + ")?" // end optional comment block ); std::unique_lock read_lock{mutex_}; - JLOG (j_.debug()) << - "Loading configured trusted validator list publisher keys"; + JLOG(j_.debug()) + << "Loading configured trusted validator list publisher keys"; std::size_t count = 0; for (auto key : publisherKeys) { - JLOG (j_.trace()) << - "Processing '" << key << "'"; + JLOG(j_.trace()) << "Processing '" << key << "'"; - auto const ret = strUnHex (key); + auto const ret = strUnHex(key); - if (! ret || ! publicKeyType(makeSlice(*ret))) + if (!ret || !publicKeyType(makeSlice(*ret))) { - JLOG (j_.error()) << - "Invalid validator list publisher key: " << key; + JLOG(j_.error()) << "Invalid validator list publisher key: " << key; return false; } auto id = PublicKey(makeSlice(*ret)); - if (publisherManifests_.revoked (id)) + if (publisherManifests_.revoked(id)) { - JLOG (j_.warn()) << - "Configured validator list publisher key is revoked: " << key; + JLOG(j_.warn()) + << "Configured validator list publisher key is revoked: " + << key; continue; } if (publisherLists_.count(id)) { - JLOG (j_.warn()) << - "Duplicate validator list publisher key: " << key; + JLOG(j_.warn()) + << "Duplicate validator list publisher key: " << key; continue; } @@ -135,40 +134,35 @@ ValidatorList::load ( ++count; } - JLOG (j_.debug()) << - "Loaded " << count << " keys"; + JLOG(j_.debug()) << "Loaded " << count << " keys"; - localPubKey_ = validatorManifests_.getMasterKey (localSigningKey); + localPubKey_ = validatorManifests_.getMasterKey(localSigningKey); // Treat local validator key as though it was listed in the config if (localPubKey_.size()) - keyListings_.insert ({ localPubKey_, 1 }); + keyListings_.insert({localPubKey_, 1}); - JLOG (j_.debug()) << - "Loading configured validator keys"; + JLOG(j_.debug()) << "Loading configured validator keys"; count = 0; PublicKey local; for (auto const& n : configKeys) { - JLOG (j_.trace()) << - "Processing '" << n << "'"; + JLOG(j_.trace()) << "Processing '" << n << "'"; boost::smatch match; - if (!boost::regex_match (n, match, re)) + if (!boost::regex_match(n, match, re)) { - JLOG (j_.error()) << - "Malformed entry: '" << n << "'"; + JLOG(j_.error()) << "Malformed entry: '" << n << "'"; return false; } - auto const id = parseBase58( - TokenType::NodePublic, match[1]); + auto const id = parseBase58(TokenType::NodePublic, match[1]); if (!id) { - JLOG (j_.error()) << "Invalid node identity: " << match[1]; + JLOG(j_.error()) << "Invalid node identity: " << match[1]; return false; } @@ -176,10 +170,10 @@ ValidatorList::load ( if (*id == localPubKey_ || *id == localSigningKey) continue; - auto ret = keyListings_.insert ({*id, 1}); - if (! ret.second) + auto ret = keyListings_.insert({*id, 1}); + if (!ret.second) { - JLOG (j_.warn()) << "Duplicate node identity: " << match[1]; + JLOG(j_.warn()) << "Duplicate node identity: " << match[1]; continue; } auto it = publisherLists_.emplace( @@ -194,8 +188,7 @@ ValidatorList::load ( ++count; } - JLOG (j_.debug()) << - "Loaded " << count << " entries"; + JLOG(j_.debug()) << "Loaded " << count << " entries"; return true; } @@ -207,14 +200,14 @@ ValidatorList::GetCacheFileName(PublicKey const& pubKey) } void -ValidatorList::CacheValidatorFile(PublicKey const& pubKey, +ValidatorList::CacheValidatorFile( + PublicKey const& pubKey, PublisherList const& publisher) { if (dataPath_.empty()) return; - boost::filesystem::path const filename = - GetCacheFileName(pubKey); + boost::filesystem::path const filename = GetCacheFileName(pubKey); boost::system::error_code ec; @@ -230,13 +223,8 @@ ValidatorList::CacheValidatorFile(PublicKey const& pubKey, if (ec) { // Log and ignore any file I/O exceptions - JLOG(j_.error()) << - "Problem writing " << - filename << - " " << - ec.value() << - ": " << - ec.message(); + JLOG(j_.error()) << "Problem writing " << filename << " " << ec.value() + << ": " << ec.message(); } } @@ -251,8 +239,8 @@ ValidatorList::applyListAndBroadcast( Overlay& overlay, HashRouter& hashRouter) { - auto const result = applyList(manifest, blob, signature, - version, std::move(siteUri), hash); + auto const result = + applyList(manifest, blob, signature, version, std::move(siteUri), hash); auto const disposition = result.disposition; bool broadcast = disposition == ListDisposition::accepted || @@ -304,7 +292,7 @@ ValidatorList::applyListAndBroadcast( } ValidatorList::PublisherListStats -ValidatorList::applyList ( +ValidatorList::applyList( std::string const& manifest, std::string const& blob, std::string const& signature, @@ -315,13 +303,13 @@ ValidatorList::applyList ( using namespace std::string_literals; if (version != requiredListVersion) - return PublisherListStats{ ListDisposition::unsupported_version }; + return PublisherListStats{ListDisposition::unsupported_version}; std::unique_lock lock{mutex_}; Json::Value list; PublicKey pubKey; - auto const result = verify (list, pubKey, manifest, blob, signature); + auto const result = verify(list, pubKey, manifest, blob, signature); if (result != ListDisposition::accepted) { if (result == ListDisposition::same_sequence && @@ -330,17 +318,17 @@ ValidatorList::applyList ( // We've seen this valid list already, so return // what we know about it. auto const& publisher = publisherLists_[pubKey]; - return PublisherListStats{ result, pubKey, - publisher.available, publisher.sequence }; + return PublisherListStats{ + result, pubKey, publisher.available, publisher.sequence}; } - return PublisherListStats{ result }; + return PublisherListStats{result}; } // Update publisher's list Json::Value const& newList = list["validators"]; auto& publisher = publisherLists_[pubKey]; publisher.available = true; - publisher.sequence = list["sequence"].asUInt (); + publisher.sequence = list["sequence"].asUInt(); publisher.expiration = TimeKeeper::time_point{ TimeKeeper::duration{list["expiration"].asUInt()}}; publisher.siteUri = std::move(siteUri); @@ -348,66 +336,62 @@ ValidatorList::applyList ( publisher.rawBlob = blob; publisher.rawSignature = signature; publisher.rawVersion = version; - if(hash) + if (hash) publisher.hash = *hash; std::vector& publisherList = publisher.list; - PublisherListStats const applyResult{ result, pubKey, - publisher.available, publisher.sequence }; + PublisherListStats const applyResult{ + result, pubKey, publisher.available, publisher.sequence}; std::vector oldList = publisherList; - publisherList.clear (); - publisherList.reserve (newList.size ()); + publisherList.clear(); + publisherList.reserve(newList.size()); std::vector manifests; for (auto const& val : newList) { - if (val.isObject() && - val.isMember ("validation_public_key") && - val["validation_public_key"].isString ()) + if (val.isObject() && val.isMember("validation_public_key") && + val["validation_public_key"].isString()) { - boost::optional const ret = strUnHex(val["validation_public_key"].asString()); + boost::optional const ret = + strUnHex(val["validation_public_key"].asString()); - if (! ret || ! publicKeyType(makeSlice(*ret))) + if (!ret || !publicKeyType(makeSlice(*ret))) { - JLOG (j_.error()) << - "Invalid node identity: " << - val["validation_public_key"].asString (); + JLOG(j_.error()) << "Invalid node identity: " + << val["validation_public_key"].asString(); } else { - publisherList.push_back ( - PublicKey(Slice{ ret->data (), ret->size() })); + publisherList.push_back( + PublicKey(Slice{ret->data(), ret->size()})); } - if (val.isMember ("manifest") && val["manifest"].isString ()) - manifests.push_back(val["manifest"].asString ()); + if (val.isMember("manifest") && val["manifest"].isString()) + manifests.push_back(val["manifest"].asString()); } } // Update keyListings_ for added and removed keys - std::sort ( - publisherList.begin (), - publisherList.end ()); + std::sort(publisherList.begin(), publisherList.end()); - auto iNew = publisherList.begin (); - auto iOld = oldList.begin (); - while (iNew != publisherList.end () || - iOld != oldList.end ()) + auto iNew = publisherList.begin(); + auto iOld = oldList.begin(); + while (iNew != publisherList.end() || iOld != oldList.end()) { - if (iOld == oldList.end () || - (iNew != publisherList.end () && - *iNew < *iOld)) + if (iOld == oldList.end() || + (iNew != publisherList.end() && *iNew < *iOld)) { // Increment list count for added keys ++keyListings_[*iNew]; ++iNew; } - else if (iNew == publisherList.end () || - (iOld != oldList.end () && *iOld < *iNew)) + else if ( + iNew == publisherList.end() || + (iOld != oldList.end() && *iOld < *iNew)) { // Decrement list count for removed keys if (keyListings_[*iOld] <= 1) - keyListings_.erase (*iOld); + keyListings_.erase(*iOld); else --keyListings_[*iOld]; ++iOld; @@ -421,28 +405,25 @@ ValidatorList::applyList ( if (publisherList.empty()) { - JLOG (j_.warn()) << - "No validator keys included in valid list"; + JLOG(j_.warn()) << "No validator keys included in valid list"; } for (auto const& valManifest : manifests) { auto m = deserializeManifest(base64_decode(valManifest)); - if (! m || ! keyListings_.count (m->masterKey)) + if (!m || !keyListings_.count(m->masterKey)) { - JLOG (j_.warn()) << - "List for " << strHex(pubKey) << - " contained untrusted validator manifest"; + JLOG(j_.warn()) << "List for " << strHex(pubKey) + << " contained untrusted validator manifest"; continue; } - if (auto const r = validatorManifests_.applyManifest (std::move(*m)); + if (auto const r = validatorManifests_.applyManifest(std::move(*m)); r == ManifestDisposition::invalid) { - JLOG (j_.warn()) << - "List for " << strHex(pubKey) << - " contained invalid validator manifest"; + JLOG(j_.warn()) << "List for " << strHex(pubKey) + << " contained invalid validator manifest"; } } @@ -470,10 +451,9 @@ ValidatorList::loadLists() if (publisher.available) continue; - boost::filesystem::path const filename = - GetCacheFileName(pubKey); + boost::filesystem::path const filename = GetCacheFileName(pubKey); - auto const fullPath{ canonical(filename, ec) }; + auto const fullPath{canonical(filename, ec)}; if (ec) continue; @@ -488,10 +468,9 @@ ValidatorList::loadLists() continue; std::string const prefix = [&fullPath]() { -#if _MSC_VER // MSVC: Windows paths need a leading / added +#if _MSC_VER // MSVC: Windows paths need a leading / added { - return fullPath.root_path() == "/"s ? - "file://" : "file:///"; + return fullPath.root_path() == "/"s ? "file://" : "file:///"; } #else { @@ -508,7 +487,7 @@ ValidatorList::loadLists() } ListDisposition -ValidatorList::verify ( +ValidatorList::verify( Json::Value& list, PublicKey& pubKey, std::string const& manifest, @@ -517,35 +496,34 @@ ValidatorList::verify ( { auto m = deserializeManifest(base64_decode(manifest)); - if (! m || ! publisherLists_.count (m->masterKey)) + if (!m || !publisherLists_.count(m->masterKey)) return ListDisposition::untrusted; pubKey = m->masterKey; auto const revoked = m->revoked(); - auto const result = publisherManifests_.applyManifest ( - std::move(*m)); + auto const result = publisherManifests_.applyManifest(std::move(*m)); if (revoked && result == ManifestDisposition::accepted) { - removePublisherList (pubKey); - publisherLists_.erase (pubKey); + removePublisherList(pubKey); + publisherLists_.erase(pubKey); } if (revoked || result == ManifestDisposition::invalid) return ListDisposition::untrusted; auto const sig = strUnHex(signature); - auto const data = base64_decode (blob); - if (! sig || - ! ripple::verify ( + auto const data = base64_decode(blob); + if (!sig || + !ripple::verify( publisherManifests_.getSigningKey(pubKey), makeSlice(data), makeSlice(*sig))) return ListDisposition::invalid; Json::Reader r; - if (! r.parse (data, list)) + if (!r.parse(data, list)) return ListDisposition::invalid; if (list.isMember("sequence") && list["sequence"].isInt() && @@ -570,79 +548,77 @@ ValidatorList::verify ( } bool -ValidatorList::listed ( - PublicKey const& identity) const +ValidatorList::listed(PublicKey const& identity) const { std::shared_lock read_lock{mutex_}; - auto const pubKey = validatorManifests_.getMasterKey (identity); - return keyListings_.find (pubKey) != keyListings_.end (); + auto const pubKey = validatorManifests_.getMasterKey(identity); + return keyListings_.find(pubKey) != keyListings_.end(); } bool -ValidatorList::trusted (PublicKey const& identity) const +ValidatorList::trusted(PublicKey const& identity) const { std::shared_lock read_lock{mutex_}; - auto const pubKey = validatorManifests_.getMasterKey (identity); - return trustedMasterKeys_.find (pubKey) != trustedMasterKeys_.end(); + auto const pubKey = validatorManifests_.getMasterKey(identity); + return trustedMasterKeys_.find(pubKey) != trustedMasterKeys_.end(); } boost::optional -ValidatorList::getListedKey ( - PublicKey const& identity) const +ValidatorList::getListedKey(PublicKey const& identity) const { std::shared_lock read_lock{mutex_}; - auto const pubKey = validatorManifests_.getMasterKey (identity); - if (keyListings_.find (pubKey) != keyListings_.end ()) + auto const pubKey = validatorManifests_.getMasterKey(identity); + if (keyListings_.find(pubKey) != keyListings_.end()) return pubKey; return boost::none; } boost::optional -ValidatorList::getTrustedKey (PublicKey const& identity) const +ValidatorList::getTrustedKey(PublicKey const& identity) const { std::shared_lock read_lock{mutex_}; - auto const pubKey = validatorManifests_.getMasterKey (identity); - if (trustedMasterKeys_.find (pubKey) != trustedMasterKeys_.end()) + auto const pubKey = validatorManifests_.getMasterKey(identity); + if (trustedMasterKeys_.find(pubKey) != trustedMasterKeys_.end()) return pubKey; return boost::none; } bool -ValidatorList::trustedPublisher (PublicKey const& identity) const +ValidatorList::trustedPublisher(PublicKey const& identity) const { std::shared_lock read_lock{mutex_}; - return identity.size() && publisherLists_.count (identity); + return identity.size() && publisherLists_.count(identity); } PublicKey -ValidatorList::localPublicKey () const +ValidatorList::localPublicKey() const { std::shared_lock read_lock{mutex_}; return localPubKey_; } bool -ValidatorList::removePublisherList (PublicKey const& publisherKey) +ValidatorList::removePublisherList(PublicKey const& publisherKey) { - auto const iList = publisherLists_.find (publisherKey); - if (iList == publisherLists_.end ()) + auto const iList = publisherLists_.find(publisherKey); + if (iList == publisherLists_.end()) return false; - JLOG (j_.debug()) << - "Removing validator list for publisher " << strHex(publisherKey); + JLOG(j_.debug()) << "Removing validator list for publisher " + << strHex(publisherKey); for (auto const& val : iList->second.list) { - auto const& iVal = keyListings_.find (val); + auto const& iVal = keyListings_.find(val); if (iVal == keyListings_.end()) continue; if (iVal->second <= 1) - keyListings_.erase (iVal); + keyListings_.erase(iVal); else --iVal->second; } @@ -720,12 +696,10 @@ ValidatorList::getJson() const PublicKey local; Json::Value& jLocalStaticKeys = (res[jss::local_static_keys] = Json::arrayValue); - if (auto it = publisherLists_.find(local); - it != publisherLists_.end()) + if (auto it = publisherLists_.find(local); it != publisherLists_.end()) { for (auto const& key : it->second.list) - jLocalStaticKeys.append( - toBase58(TokenType::NodePublic, key)); + jLocalStaticKeys.append(toBase58(TokenType::NodePublic, key)); } // Publisher lists @@ -733,13 +707,13 @@ ValidatorList::getJson() const (res[jss::publisher_lists] = Json::arrayValue); for (auto const& p : publisherLists_) { - if(local == p.first) + if (local == p.first) continue; Json::Value& curr = jPublisherLists.append(Json::objectValue); curr[jss::pubkey_publisher] = strHex(p.first); curr[jss::available] = p.second.available; curr[jss::uri] = p.second.siteUri; - if(p.second.expiration != TimeKeeper::time_point{}) + if (p.second.expiration != TimeKeeper::time_point{}) { curr[jss::seq] = static_cast(p.second.sequence); curr[jss::expiration] = to_string(p.second.expiration); @@ -762,38 +736,38 @@ ValidatorList::getJson() const // signing keys Json::Value& jSigningKeys = (res[jss::signing_keys] = Json::objectValue); - validatorManifests_.for_each_manifest( - [&jSigningKeys, this](Manifest const& manifest) { - - auto it = keyListings_.find(manifest.masterKey); - if (it != keyListings_.end()) - { - jSigningKeys[toBase58( - TokenType::NodePublic, manifest.masterKey)] = - toBase58(TokenType::NodePublic, manifest.signingKey); - } - }); + validatorManifests_.for_each_manifest([&jSigningKeys, + this](Manifest const& manifest) { + auto it = keyListings_.find(manifest.masterKey); + if (it != keyListings_.end()) + { + jSigningKeys[toBase58(TokenType::NodePublic, manifest.masterKey)] = + toBase58(TokenType::NodePublic, manifest.signingKey); + } + }); return res; } void -ValidatorList::for_each_listed ( +ValidatorList::for_each_listed( std::function func) const { std::shared_lock read_lock{mutex_}; for (auto const& v : keyListings_) - func (v.first, trusted(v.first)); + func(v.first, trusted(v.first)); } void -ValidatorList::for_each_available ( - std::function func) const +ValidatorList::for_each_available(std::function func) const { std::shared_lock read_lock{mutex_}; @@ -801,8 +775,14 @@ ValidatorList::for_each_available ( { if (!pl.available) continue; - func(pl.rawManifest, pl.rawBlob, pl.rawSignature, pl.rawVersion, - key, pl.sequence, pl.hash); + func( + pl.rawManifest, + pl.rawBlob, + pl.rawSignature, + pl.rawVersion, + key, + pl.sequence, + pl.hash); } } @@ -811,12 +791,12 @@ ValidatorList::getAvailable(boost::beast::string_view const& pubKey) { std::shared_lock read_lock{mutex_}; - auto const keyBlob = strViewUnHex (pubKey); + auto const keyBlob = strViewUnHex(pubKey); - if (! keyBlob || ! publicKeyType(makeSlice(*keyBlob))) + if (!keyBlob || !publicKeyType(makeSlice(*keyBlob))) { - JLOG (j_.info()) << - "Invalid requested validator list publisher key: " << pubKey; + JLOG(j_.info()) << "Invalid requested validator list publisher key: " + << pubKey; return {}; } @@ -824,8 +804,7 @@ ValidatorList::getAvailable(boost::beast::string_view const& pubKey) auto iter = publisherLists_.find(id); - if (iter == publisherLists_.end() - || !iter->second.available) + if (iter == publisherLists_.end() || !iter->second.available) return {}; Json::Value value(Json::objectValue); @@ -839,14 +818,13 @@ ValidatorList::getAvailable(boost::beast::string_view const& pubKey) } std::size_t -ValidatorList::calculateQuorum ( - std::size_t trusted, std::size_t seen) +ValidatorList::calculateQuorum(std::size_t trusted, std::size_t seen) { // Do not use achievable quorum until lists from all configured // publishers are available for (auto const& list : publisherLists_) { - if (! list.second.available) + if (!list.second.available) return std::numeric_limits::max(); } @@ -888,10 +866,8 @@ ValidatorList::calculateQuorum ( { quorum = *minimumQuorum_; - JLOG (j_.warn()) - << "Using unsafe quorum of " - << quorum - << " as specified in the command line"; + JLOG(j_.warn()) << "Using unsafe quorum of " << quorum + << " as specified in the command line"; } return quorum; @@ -915,8 +891,7 @@ ValidatorList::updateTrusted(hash_set const& seenValidators) auto it = trustedMasterKeys_.cbegin(); while (it != trustedMasterKeys_.cend()) { - if (!keyListings_.count(*it) || - validatorManifests_.revoked(*it)) + if (!keyListings_.count(*it) || validatorManifests_.revoked(*it)) { trustChanges.removed.insert(calcNodeID(*it)); it = trustedMasterKeys_.erase(it); @@ -943,27 +918,25 @@ ValidatorList::updateTrusted(hash_set const& seenValidators) trustedSigningKeys_.insert(validatorManifests_.getSigningKey(k)); } - JLOG (j_.debug()) + JLOG(j_.debug()) << trustedMasterKeys_.size() << " of " << keyListings_.size() << " listed validators eligible for inclusion in the trusted set"; - quorum_ = calculateQuorum (trustedMasterKeys_.size(), seenValidators.size()); + quorum_ = calculateQuorum(trustedMasterKeys_.size(), seenValidators.size()); - JLOG(j_.debug()) - << "Using quorum of " << quorum_ << " for new set of " - << trustedMasterKeys_.size() << " trusted validators (" - << trustChanges.added.size() << " added, " - << trustChanges.removed.size() << " removed)"; + JLOG(j_.debug()) << "Using quorum of " << quorum_ << " for new set of " + << trustedMasterKeys_.size() << " trusted validators (" + << trustChanges.added.size() << " added, " + << trustChanges.removed.size() << " removed)"; if (trustedMasterKeys_.size() < quorum_) { - JLOG (j_.warn()) - << "New quorum of " << quorum_ - << " exceeds the number of trusted validators (" - << trustedMasterKeys_.size() << ")"; + JLOG(j_.warn()) << "New quorum of " << quorum_ + << " exceeds the number of trusted validators (" + << trustedMasterKeys_.size() << ")"; } return trustChanges; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/misc/impl/ValidatorSite.cpp b/src/ripple/app/misc/impl/ValidatorSite.cpp index 286184d1eb..aa20252588 100644 --- a/src/ripple/app/misc/impl/ValidatorSite.cpp +++ b/src/ripple/app/misc/impl/ValidatorSite.cpp @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -33,17 +33,16 @@ namespace ripple { -auto constexpr default_refresh_interval = std::chrono::minutes{5}; -auto constexpr error_retry_interval = std::chrono::seconds{30}; -unsigned short constexpr max_redirects = 3; +auto constexpr default_refresh_interval = std::chrono::minutes{5}; +auto constexpr error_retry_interval = std::chrono::seconds{30}; +unsigned short constexpr max_redirects = 3; -ValidatorSite::Site::Resource::Resource (std::string uri_) - : uri {std::move(uri_)} +ValidatorSite::Site::Resource::Resource(std::string uri_) : uri{std::move(uri_)} { - if (! parseUrl (pUrl, uri)) + if (!parseUrl(pUrl, uri)) throw std::runtime_error("URI '" + uri + "' cannot be parsed"); - if (pUrl.scheme == "file") + if (pUrl.scheme == "file") { if (!pUrl.domain.empty()) throw std::runtime_error("file URI cannot contain a hostname"); @@ -74,29 +73,29 @@ ValidatorSite::Site::Resource::Resource (std::string uri_) pUrl.port = 443; } else - throw std::runtime_error ("Unsupported scheme: '" + pUrl.scheme + "'"); + throw std::runtime_error("Unsupported scheme: '" + pUrl.scheme + "'"); } -ValidatorSite::Site::Site (std::string uri) - : loadedResource {std::make_shared(std::move(uri))} - , startingResource {loadedResource} - , redirCount {0} - , refreshInterval {default_refresh_interval} - , nextRefresh {clock_type::now()} +ValidatorSite::Site::Site(std::string uri) + : loadedResource{std::make_shared(std::move(uri))} + , startingResource{loadedResource} + , redirCount{0} + , refreshInterval{default_refresh_interval} + , nextRefresh{clock_type::now()} { } -ValidatorSite::ValidatorSite ( +ValidatorSite::ValidatorSite( Application& app, boost::optional j, std::chrono::seconds timeout) - : app_ {app} - , j_ {j ? *j : app_.logs().journal("ValidatorSite") } - , timer_ {app_.getIOService()} - , fetching_ {false} - , pending_ {false} - , stopping_ {false} - , requestTimeout_ {timeout} + : app_{app} + , j_{j ? *j : app_.logs().journal("ValidatorSite")} + , timer_{app_.getIOService()} + , fetching_{false} + , pending_{false} + , stopping_{false} + , requestTimeout_{timeout} { } @@ -105,14 +104,14 @@ ValidatorSite::~ValidatorSite() std::unique_lock lock{state_mutex_}; if (timer_.expires_at() > clock_type::time_point{}) { - if (! stopping_) + if (!stopping_) { lock.unlock(); stop(); } else { - cv_.wait(lock, [&]{ return ! fetching_; }); + cv_.wait(lock, [&] { return !fetching_; }); } } } @@ -125,8 +124,7 @@ ValidatorSite::missingSite() } bool -ValidatorSite::load ( - std::vector const& siteURIs) +ValidatorSite::load(std::vector const& siteURIs) { // If no sites are provided, act as if a site failed to load. if (siteURIs.empty()) @@ -134,8 +132,7 @@ ValidatorSite::load ( return missingSite(); } - JLOG (j_.debug()) << - "Loading configured validator list sites"; + JLOG(j_.debug()) << "Loading configured validator list sites"; std::lock_guard lock{sites_mutex_}; @@ -143,36 +140,34 @@ ValidatorSite::load ( { try { - sites_.emplace_back (uri); + sites_.emplace_back(uri); } catch (std::exception const& e) { - JLOG (j_.error()) << - "Invalid validator site uri: " << uri << - ": " << e.what(); + JLOG(j_.error()) + << "Invalid validator site uri: " << uri << ": " << e.what(); return false; } } - JLOG (j_.debug()) << - "Loaded " << siteURIs.size() << " sites"; + JLOG(j_.debug()) << "Loaded " << siteURIs.size() << " sites"; return true; } void -ValidatorSite::start () +ValidatorSite::start() { std::lock_guard lock{state_mutex_}; if (timer_.expires_at() == clock_type::time_point{}) - setTimer (lock); + setTimer(lock); } void -ValidatorSite::join () +ValidatorSite::join() { std::unique_lock lock{state_mutex_}; - cv_.wait(lock, [&]{ return ! pending_; }); + cv_.wait(lock, [&] { return !pending_; }); } void @@ -183,9 +178,9 @@ ValidatorSite::stop() // work::cancel() must be called before the // cv wait in order to kick any asio async operations // that might be pending. - if(auto sp = work_.lock()) + if (auto sp = work_.lock()) sp->cancel(); - cv_.wait(lock, [&]{ return ! fetching_; }); + cv_.wait(lock, [&] { return !fetching_; }); // docs indicate cancel() can throw, but this should be // reconsidered if it changes to noexcept @@ -202,31 +197,29 @@ ValidatorSite::stop() } void -ValidatorSite::setTimer (std::lock_guard& state_lock) +ValidatorSite::setTimer(std::lock_guard& state_lock) { std::lock_guard lock{sites_mutex_}; - auto next = std::min_element(sites_.begin(), sites_.end(), - [](Site const& a, Site const& b) - { + auto next = std::min_element( + sites_.begin(), sites_.end(), [](Site const& a, Site const& b) { return a.nextRefresh < b.nextRefresh; }); - if (next != sites_.end ()) + if (next != sites_.end()) { pending_ = next->nextRefresh <= clock_type::now(); cv_.notify_all(); - timer_.expires_at (next->nextRefresh); - auto idx = std::distance (sites_.begin (), next); - timer_.async_wait ([this, idx] (boost::system::error_code const& ec) - { - this->onTimer (idx, ec); + timer_.expires_at(next->nextRefresh); + auto idx = std::distance(sites_.begin(), next); + timer_.async_wait([this, idx](boost::system::error_code const& ec) { + this->onTimer(idx, ec); }); } } void -ValidatorSite::makeRequest ( +ValidatorSite::makeRequest( std::shared_ptr resource, std::size_t siteIdx, std::lock_guard& sites_lock) @@ -234,37 +227,31 @@ ValidatorSite::makeRequest ( fetching_ = true; sites_[siteIdx].activeResource = resource; std::shared_ptr sp; - auto timeoutCancel = - [this] () + auto timeoutCancel = [this]() { + std::lock_guard lock_state{state_mutex_}; + // docs indicate cancel_one() can throw, but this + // should be reconsidered if it changes to noexcept + try { - std::lock_guard lock_state{state_mutex_}; - // docs indicate cancel_one() can throw, but this - // should be reconsidered if it changes to noexcept - try - { - timer_.cancel_one(); - } - catch (boost::system::system_error const&) - { - } - }; - auto onFetch = - [this, siteIdx, timeoutCancel] ( - error_code const& err, detail::response_type&& resp) + timer_.cancel_one(); + } + catch (boost::system::system_error const&) { - timeoutCancel (); - onSiteFetch (err, std::move(resp), siteIdx); - }; + } + }; + auto onFetch = [this, siteIdx, timeoutCancel]( + error_code const& err, detail::response_type&& resp) { + timeoutCancel(); + onSiteFetch(err, std::move(resp), siteIdx); + }; - auto onFetchFile = - [this, siteIdx, timeoutCancel] ( - error_code const& err, std::string const& resp) - { - timeoutCancel (); - onTextFetch (err, resp, siteIdx); - }; + auto onFetchFile = [this, siteIdx, timeoutCancel]( + error_code const& err, std::string const& resp) { + timeoutCancel(); + onTextFetch(err, resp, siteIdx); + }; - JLOG (j_.debug()) << "Starting request for " << resource->uri; + JLOG(j_.debug()) << "Starting request for " << resource->uri; if (resource->pUrl.scheme == "https") { @@ -278,7 +265,7 @@ ValidatorSite::makeRequest ( app_.config(), onFetch); } - else if(resource->pUrl.scheme == "http") + else if (resource->pUrl.scheme == "http") { sp = std::make_shared( resource->pUrl.domain, @@ -291,54 +278,46 @@ ValidatorSite::makeRequest ( { BOOST_ASSERT(resource->pUrl.scheme == "file"); sp = std::make_shared( - resource->pUrl.path, - app_.getIOService(), - onFetchFile); + resource->pUrl.path, app_.getIOService(), onFetchFile); } work_ = sp; - sp->run (); + sp->run(); // start a timer for the request, which shouldn't take more // than requestTimeout_ to complete std::lock_guard lock_state{state_mutex_}; - timer_.expires_after (requestTimeout_); - timer_.async_wait ([this, siteIdx] (boost::system::error_code const& ec) - { - this->onRequestTimeout (siteIdx, ec); - }); + timer_.expires_after(requestTimeout_); + timer_.async_wait([this, siteIdx](boost::system::error_code const& ec) { + this->onRequestTimeout(siteIdx, ec); + }); } void -ValidatorSite::onRequestTimeout ( - std::size_t siteIdx, - error_code const& ec) +ValidatorSite::onRequestTimeout(std::size_t siteIdx, error_code const& ec) { if (ec) return; { std::lock_guard lock_site{sites_mutex_}; - JLOG (j_.warn()) << - "Request for " << sites_[siteIdx].activeResource->uri << - " took too long"; + JLOG(j_.warn()) << "Request for " << sites_[siteIdx].activeResource->uri + << " took too long"; } std::lock_guard lock_state{state_mutex_}; - if(auto sp = work_.lock()) + if (auto sp = work_.lock()) sp->cancel(); } void -ValidatorSite::onTimer ( - std::size_t siteIdx, - error_code const& ec) +ValidatorSite::onTimer(std::size_t siteIdx, error_code const& ec) { if (ec) { // Restart the timer if any errors are encountered, unless the error // is from the wait operating being aborted due to a shutdown request. if (ec != boost::asio::error::operation_aborted) - onSiteFetch(ec, detail::response_type {}, siteIdx); + onSiteFetch(ec, detail::response_type{}, siteIdx); return; } @@ -351,50 +330,48 @@ ValidatorSite::onTimer ( // the WorkSSL client ctor can throw if SSL init fails makeRequest(sites_[siteIdx].startingResource, siteIdx, lock); } - catch (std::exception &) + catch (std::exception&) { onSiteFetch( - boost::system::error_code {-1, boost::system::generic_category()}, - detail::response_type {}, + boost::system::error_code{-1, boost::system::generic_category()}, + detail::response_type{}, siteIdx); } } void -ValidatorSite::parseJsonResponse ( +ValidatorSite::parseJsonResponse( std::string const& res, std::size_t siteIdx, std::lock_guard& sites_lock) { Json::Reader r; Json::Value body; - if (! r.parse(res.data(), body)) + if (!r.parse(res.data(), body)) { - JLOG (j_.warn()) << - "Unable to parse JSON response from " << - sites_[siteIdx].activeResource->uri; + JLOG(j_.warn()) << "Unable to parse JSON response from " + << sites_[siteIdx].activeResource->uri; throw std::runtime_error{"bad json"}; } - if( ! body.isObject () || - ! body.isMember("blob") || ! body["blob"].isString () || - ! body.isMember("manifest") || ! body["manifest"].isString () || - ! body.isMember("signature") || ! body["signature"].isString() || - ! body.isMember("version") || ! body["version"].isInt()) + if (!body.isObject() || !body.isMember("blob") || + !body["blob"].isString() || !body.isMember("manifest") || + !body["manifest"].isString() || !body.isMember("signature") || + !body["signature"].isString() || !body.isMember("version") || + !body["version"].isInt()) { - JLOG (j_.warn()) << - "Missing fields in JSON response from " << - sites_[siteIdx].activeResource->uri; + JLOG(j_.warn()) << "Missing fields in JSON response from " + << sites_[siteIdx].activeResource->uri; throw std::runtime_error{"missing fields"}; } - auto const manifest = body["manifest"].asString (); - auto const blob = body["blob"].asString (); + auto const manifest = body["manifest"].asString(); + auto const blob = body["blob"].asString(); auto const signature = body["signature"].asString(); auto const version = body["version"].asUInt(); auto const& uri = sites_[siteIdx].activeResource->uri; auto const hash = sha512Half(manifest, blob, signature, version); - auto const applyResult = app_.validators().applyListAndBroadcast ( + auto const applyResult = app_.validators().applyListAndBroadcast( manifest, blob, signature, @@ -410,49 +387,36 @@ ValidatorSite::parseJsonResponse ( switch (disp) { - case ListDisposition::accepted: - JLOG (j_.debug()) << - "Applied new validator list from " << - uri; - break; - case ListDisposition::same_sequence: - JLOG (j_.debug()) << - "Validator list with current sequence from " << - uri; - break; - case ListDisposition::stale: - JLOG (j_.warn()) << - "Stale validator list from " << - uri; - break; - case ListDisposition::untrusted: - JLOG (j_.warn()) << - "Untrusted validator list from " << - uri; - break; - case ListDisposition::invalid: - JLOG (j_.warn()) << - "Invalid validator list from " << - uri; - break; - case ListDisposition::unsupported_version: - JLOG (j_.warn()) << - "Unsupported version validator list from " << - uri; - break; - default: - BOOST_ASSERT(false); + case ListDisposition::accepted: + JLOG(j_.debug()) << "Applied new validator list from " << uri; + break; + case ListDisposition::same_sequence: + JLOG(j_.debug()) + << "Validator list with current sequence from " << uri; + break; + case ListDisposition::stale: + JLOG(j_.warn()) << "Stale validator list from " << uri; + break; + case ListDisposition::untrusted: + JLOG(j_.warn()) << "Untrusted validator list from " << uri; + break; + case ListDisposition::invalid: + JLOG(j_.warn()) << "Invalid validator list from " << uri; + break; + case ListDisposition::unsupported_version: + JLOG(j_.warn()) + << "Unsupported version validator list from " << uri; + break; + default: + BOOST_ASSERT(false); } - if (body.isMember ("refresh_interval") && - body["refresh_interval"].isNumeric ()) + if (body.isMember("refresh_interval") && + body["refresh_interval"].isNumeric()) { using namespace std::chrono_literals; - std::chrono::minutes const refresh = - boost::algorithm::clamp( - std::chrono::minutes {body["refresh_interval"].asUInt ()}, - 1min, - 24h); + std::chrono::minutes const refresh = boost::algorithm::clamp( + std::chrono::minutes{body["refresh_interval"].asUInt()}, 1min, 24h); sites_[siteIdx].refreshInterval = refresh; sites_[siteIdx].nextRefresh = clock_type::now() + sites_[siteIdx].refreshInterval; @@ -460,50 +424,46 @@ ValidatorSite::parseJsonResponse ( } std::shared_ptr -ValidatorSite::processRedirect ( +ValidatorSite::processRedirect( detail::response_type& res, std::size_t siteIdx, std::lock_guard& sites_lock) { using namespace boost::beast::http; std::shared_ptr newLocation; - if (res.find(field::location) == res.end() || - res[field::location].empty()) + if (res.find(field::location) == res.end() || res[field::location].empty()) { - JLOG (j_.warn()) << - "Request for validator list at " << - sites_[siteIdx].activeResource->uri << - " returned a redirect with no Location."; + JLOG(j_.warn()) << "Request for validator list at " + << sites_[siteIdx].activeResource->uri + << " returned a redirect with no Location."; throw std::runtime_error{"missing location"}; } if (sites_[siteIdx].redirCount == max_redirects) { - JLOG (j_.warn()) << - "Exceeded max redirects for validator list at " << - sites_[siteIdx].loadedResource->uri ; + JLOG(j_.warn()) << "Exceeded max redirects for validator list at " + << sites_[siteIdx].loadedResource->uri; throw std::runtime_error{"max redirects"}; } - JLOG (j_.debug()) << - "Got redirect for validator list from " << - sites_[siteIdx].activeResource->uri << - " to new location " << res[field::location]; + JLOG(j_.debug()) << "Got redirect for validator list from " + << sites_[siteIdx].activeResource->uri + << " to new location " << res[field::location]; try { - newLocation = std::make_shared( - std::string(res[field::location])); + newLocation = + std::make_shared(std::string(res[field::location])); ++sites_[siteIdx].redirCount; if (newLocation->pUrl.scheme != "http" && newLocation->pUrl.scheme != "https") - throw std::runtime_error("invalid scheme in redirect " + - newLocation->pUrl.scheme); + throw std::runtime_error( + "invalid scheme in redirect " + newLocation->pUrl.scheme); } - catch (std::exception &) + catch (std::exception&) { - JLOG (j_.error()) << - "Invalid redirect location: " << res[field::location]; + JLOG(j_.error()) << "Invalid redirect location: " + << res[field::location]; throw; } return newLocation; @@ -517,17 +477,14 @@ ValidatorSite::onSiteFetch( { { std::lock_guard lock_sites{sites_mutex_}; - JLOG (j_.debug()) << "Got completion for " - << sites_[siteIdx].activeResource->uri; - auto onError = [&](std::string const& errMsg, bool retry) - { - sites_[siteIdx].lastRefreshStatus.emplace( - Site::Status{clock_type::now(), - ListDisposition::invalid, - errMsg}); + JLOG(j_.debug()) << "Got completion for " + << sites_[siteIdx].activeResource->uri; + auto onError = [&](std::string const& errMsg, bool retry) { + sites_[siteIdx].lastRefreshStatus.emplace(Site::Status{ + clock_type::now(), ListDisposition::invalid, errMsg}); if (retry) sites_[siteIdx].nextRefresh = - clock_type::now() + error_retry_interval; + clock_type::now() + error_retry_interval; // See if there's a copy saved locally from last time we // saw the list. @@ -535,13 +492,9 @@ ValidatorSite::onSiteFetch( }; if (ec) { - JLOG (j_.warn()) << - "Problem retrieving from " << - sites_[siteIdx].activeResource->uri << - " " << - ec.value() << - ":" << - ec.message(); + JLOG(j_.warn()) << "Problem retrieving from " + << sites_[siteIdx].activeResource->uri << " " + << ec.value() << ":" << ec.message(); onError("fetch error", true); } else @@ -551,36 +504,33 @@ ValidatorSite::onSiteFetch( using namespace boost::beast::http; switch (res.result()) { - case status::ok: - parseJsonResponse(res.body(), siteIdx, lock_sites); - break; - case status::moved_permanently : - case status::permanent_redirect : - case status::found : - case status::temporary_redirect : - { - auto newLocation = - processRedirect (res, siteIdx, lock_sites); - assert(newLocation); - // for perm redirects, also update our starting URI - if (res.result() == status::moved_permanently || - res.result() == status::permanent_redirect) - { - sites_[siteIdx].startingResource = newLocation; + case status::ok: + parseJsonResponse(res.body(), siteIdx, lock_sites); + break; + case status::moved_permanently: + case status::permanent_redirect: + case status::found: + case status::temporary_redirect: { + auto newLocation = + processRedirect(res, siteIdx, lock_sites); + assert(newLocation); + // for perm redirects, also update our starting URI + if (res.result() == status::moved_permanently || + res.result() == status::permanent_redirect) + { + sites_[siteIdx].startingResource = newLocation; + } + makeRequest(newLocation, siteIdx, lock_sites); + return; // we are still fetching, so skip + // state update/notify below + } + default: { + JLOG(j_.warn()) + << "Request for validator list at " + << sites_[siteIdx].activeResource->uri + << " returned bad status: " << res.result_int(); + onError("bad result code", true); } - makeRequest(newLocation, siteIdx, lock_sites); - return; // we are still fetching, so skip - // state update/notify below - } - default: - { - JLOG (j_.warn()) << - "Request for validator list at " << - sites_[siteIdx].activeResource->uri << - " returned bad status: " << - res.result_int(); - onError("bad result code", true); - } } } catch (std::exception& ex) @@ -593,8 +543,8 @@ ValidatorSite::onSiteFetch( std::lock_guard lock_state{state_mutex_}; fetching_ = false; - if (! stopping_) - setTimer (lock_state); + if (!stopping_) + setTimer(lock_state); cv_.notify_all(); } @@ -610,13 +560,9 @@ ValidatorSite::onTextFetch( { if (ec) { - JLOG (j_.warn()) << - "Problem retrieving from " << - sites_[siteIdx].activeResource->uri << - " " << - ec.value() << - ": " << - ec.message(); + JLOG(j_.warn()) << "Problem retrieving from " + << sites_[siteIdx].activeResource->uri << " " + << ec.value() << ": " << ec.message(); throw std::runtime_error{"fetch error"}; } @@ -624,18 +570,16 @@ ValidatorSite::onTextFetch( } catch (std::exception& ex) { - sites_[siteIdx].lastRefreshStatus.emplace( - Site::Status{clock_type::now(), - ListDisposition::invalid, - ex.what()}); + sites_[siteIdx].lastRefreshStatus.emplace(Site::Status{ + clock_type::now(), ListDisposition::invalid, ex.what()}); } sites_[siteIdx].activeResource.reset(); } std::lock_guard lock_state{state_mutex_}; fetching_ = false; - if (! stopping_) - setTimer (lock_state); + if (!stopping_) + setTimer(lock_state); cv_.notify_all(); } @@ -664,7 +608,7 @@ ValidatorSite::getJson() const to_string(site.lastRefreshStatus->refreshed); v[jss::last_refresh_status] = to_string(site.lastRefreshStatus->disposition); - if (! site.lastRefreshStatus->message.empty()) + if (!site.lastRefreshStatus->message.empty()) v[jss::last_refresh_message] = site.lastRefreshStatus->message; } @@ -674,4 +618,4 @@ ValidatorSite::getJson() const } return jrr; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/AccountCurrencies.cpp b/src/ripple/app/paths/AccountCurrencies.cpp index 7104889b9d..93eb59551e 100644 --- a/src/ripple/app/paths/AccountCurrencies.cpp +++ b/src/ripple/app/paths/AccountCurrencies.cpp @@ -21,7 +21,8 @@ namespace ripple { -hash_set accountSourceCurrencies ( +hash_set +accountSourceCurrencies( AccountID const& account, std::shared_ptr const& lrCache, bool includeXRP) @@ -30,36 +31,37 @@ hash_set accountSourceCurrencies ( // YYY Only bother if they are above reserve if (includeXRP) - currencies.insert (xrpCurrency()); + currencies.insert(xrpCurrency()); // List of ripple lines. - auto& rippleLines = lrCache->getRippleLines (account); + auto& rippleLines = lrCache->getRippleLines(account); for (auto const& item : rippleLines) { - auto rspEntry = (RippleState*) item.get (); - assert (rspEntry); + auto rspEntry = (RippleState*)item.get(); + assert(rspEntry); if (!rspEntry) continue; - auto& saBalance = rspEntry->getBalance (); + auto& saBalance = rspEntry->getBalance(); // Filter out non if (saBalance > beast::zero // Have IOUs to send. - || (rspEntry->getLimitPeer () + || (rspEntry->getLimitPeer() // Peer extends credit. - && ((-saBalance) < rspEntry->getLimitPeer ()))) // Credit left. + && ((-saBalance) < rspEntry->getLimitPeer()))) // Credit left. { - currencies.insert (saBalance.getCurrency ()); + currencies.insert(saBalance.getCurrency()); } } - currencies.erase (badCurrency()); + currencies.erase(badCurrency()); return currencies; } -hash_set accountDestCurrencies ( +hash_set +accountDestCurrencies( AccountID const& account, std::shared_ptr const& lrCache, bool includeXRP) @@ -67,27 +69,27 @@ hash_set accountDestCurrencies ( hash_set currencies; if (includeXRP) - currencies.insert (xrpCurrency()); + currencies.insert(xrpCurrency()); // Even if account doesn't exist // List of ripple lines. - auto& rippleLines = lrCache->getRippleLines (account); + auto& rippleLines = lrCache->getRippleLines(account); for (auto const& item : rippleLines) { - auto rspEntry = (RippleState*) item.get (); - assert (rspEntry); + auto rspEntry = (RippleState*)item.get(); + assert(rspEntry); if (!rspEntry) continue; - auto& saBalance = rspEntry->getBalance (); + auto& saBalance = rspEntry->getBalance(); - if (saBalance < rspEntry->getLimit ()) // Can take more - currencies.insert (saBalance.getCurrency ()); + if (saBalance < rspEntry->getLimit()) // Can take more + currencies.insert(saBalance.getCurrency()); } - currencies.erase (badCurrency()); + currencies.erase(badCurrency()); return currencies; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/AccountCurrencies.h b/src/ripple/app/paths/AccountCurrencies.h index 47bfdfa812..fa70ec2a08 100644 --- a/src/ripple/app/paths/AccountCurrencies.h +++ b/src/ripple/app/paths/AccountCurrencies.h @@ -28,15 +28,15 @@ namespace ripple { hash_set accountDestCurrencies( AccountID const& account, - std::shared_ptr const& cache, - bool includeXRP); + std::shared_ptr const& cache, + bool includeXRP); hash_set accountSourceCurrencies( AccountID const& account, - std::shared_ptr const& lrLedger, - bool includeXRP); + std::shared_ptr const& lrLedger, + bool includeXRP); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/Credit.cpp b/src/ripple/app/paths/Credit.cpp index 7ac5c81ebd..b9b19d29f9 100644 --- a/src/ripple/app/paths/Credit.cpp +++ b/src/ripple/app/paths/Credit.cpp @@ -19,67 +19,66 @@ #include #include -#include #include +#include namespace ripple { STAmount -creditLimit ( +creditLimit( ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency) { - STAmount result ({currency, account}); + STAmount result({currency, account}); - auto sleRippleState = view.read( - keylet::line(account, issuer, currency)); + auto sleRippleState = view.read(keylet::line(account, issuer, currency)); if (sleRippleState) { - result = sleRippleState->getFieldAmount ( + result = sleRippleState->getFieldAmount( account < issuer ? sfLowLimit : sfHighLimit); - result.setIssuer (account); + result.setIssuer(account); } - assert (result.getIssuer () == account); - assert (result.getCurrency () == currency); + assert(result.getIssuer() == account); + assert(result.getCurrency() == currency); return result; } IOUAmount -creditLimit2 ( +creditLimit2( ReadView const& v, AccountID const& acc, AccountID const& iss, Currency const& cur) { - return toAmount (creditLimit (v, acc, iss, cur)); + return toAmount(creditLimit(v, acc, iss, cur)); } -STAmount creditBalance ( +STAmount +creditBalance( ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency) { - STAmount result ({currency, account}); + STAmount result({currency, account}); - auto sleRippleState = view.read( - keylet::line(account, issuer, currency)); + auto sleRippleState = view.read(keylet::line(account, issuer, currency)); if (sleRippleState) { - result = sleRippleState->getFieldAmount (sfBalance); + result = sleRippleState->getFieldAmount(sfBalance); if (account < issuer) - result.negate (); - result.setIssuer (account); + result.negate(); + result.setIssuer(account); } - assert (result.getIssuer () == account); - assert (result.getCurrency () == currency); + assert(result.getIssuer() == account); + assert(result.getCurrency() == currency); return result; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/Credit.h b/src/ripple/app/paths/Credit.h index 8e55bbe672..dea9cbcde3 100644 --- a/src/ripple/app/paths/Credit.h +++ b/src/ripple/app/paths/Credit.h @@ -34,14 +34,15 @@ namespace ripple { @return The maximum amount that can be held. */ /** @{ */ -STAmount creditLimit ( +STAmount +creditLimit( ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency); IOUAmount -creditLimit2 ( +creditLimit2( ReadView const& v, AccountID const& acc, AccountID const& iss, @@ -55,13 +56,14 @@ creditLimit2 ( @param currency the IOU to check. */ /** @{ */ -STAmount creditBalance ( +STAmount +creditBalance( ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency); /** @} */ -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/Flow.cpp b/src/ripple/app/paths/Flow.cpp index 1834b43464..c939af9f09 100644 --- a/src/ripple/app/paths/Flow.cpp +++ b/src/ripple/app/paths/Flow.cpp @@ -20,10 +20,10 @@ #include #include #include -#include #include -#include +#include #include +#include #include #include @@ -33,27 +33,29 @@ namespace ripple { -template -static -auto finishFlow (PaymentSandbox& sb, - Issue const& srcIssue, Issue const& dstIssue, +template +static auto +finishFlow( + PaymentSandbox& sb, + Issue const& srcIssue, + Issue const& dstIssue, FlowResult&& f) { path::RippleCalc::Output result; if (f.ter == tesSUCCESS) - f.sandbox->apply (sb); + f.sandbox->apply(sb); else - result.removableOffers = std::move (f.removableOffers); + result.removableOffers = std::move(f.removableOffers); - result.setResult (f.ter); - result.actualAmountIn = toSTAmount (f.in, srcIssue); - result.actualAmountOut = toSTAmount (f.out, dstIssue); + result.setResult(f.ter); + result.actualAmountIn = toSTAmount(f.in, srcIssue); + result.actualAmountOut = toSTAmount(f.out, dstIssue); return result; }; path::RippleCalc::Output -flow ( +flow( PaymentSandbox& sb, STAmount const& deliver, AccountID const& src, @@ -70,39 +72,49 @@ flow ( { Issue const srcIssue = [&] { if (sendMax) - return sendMax->issue (); - if (!isXRP (deliver.issue ().currency)) - return Issue (deliver.issue ().currency, src); - return xrpIssue (); + return sendMax->issue(); + if (!isXRP(deliver.issue().currency)) + return Issue(deliver.issue().currency, src); + return xrpIssue(); }(); - Issue const dstIssue = deliver.issue (); + Issue const dstIssue = deliver.issue(); boost::optional sendMaxIssue; if (sendMax) - sendMaxIssue = sendMax->issue (); + sendMaxIssue = sendMax->issue(); - // convert the paths to a collection of strands. Each strand is the collection - // of account->account steps and book steps that may be used in this payment. - auto [toStrandsTer, strands] = - toStrands(sb, src, dst, dstIssue, limitQuality, sendMaxIssue, paths, - defaultPaths, ownerPaysTransferFee, offerCrossing, j); + // convert the paths to a collection of strands. Each strand is the + // collection of account->account steps and book steps that may be used in + // this payment. + auto [toStrandsTer, strands] = toStrands( + sb, + src, + dst, + dstIssue, + limitQuality, + sendMaxIssue, + paths, + defaultPaths, + ownerPaysTransferFee, + offerCrossing, + j); if (toStrandsTer != tesSUCCESS) { path::RippleCalc::Output result; - result.setResult (toStrandsTer); + result.setResult(toStrandsTer); return result; } if (j.trace()) { j.trace() << "\nsrc: " << src << "\ndst: " << dst - << "\nsrcIssue: " << srcIssue << "\ndstIssue: " << dstIssue; - j.trace() << "\nNumStrands: " << strands.size (); + << "\nsrcIssue: " << srcIssue << "\ndstIssue: " << dstIssue; + j.trace() << "\nNumStrands: " << strands.size(); for (auto const& curStrand : strands) { - j.trace() << "NumSteps: " << curStrand.size (); + j.trace() << "NumSteps: " << curStrand.size(); for (auto const& step : curStrand) { j.trace() << '\n' << *step << '\n'; @@ -110,44 +122,83 @@ flow ( } } - const bool srcIsXRP = isXRP (srcIssue.currency); - const bool dstIsXRP = isXRP (dstIssue.currency); + const bool srcIsXRP = isXRP(srcIssue.currency); + const bool dstIsXRP = isXRP(dstIssue.currency); - auto const asDeliver = toAmountSpec (deliver); + auto const asDeliver = toAmountSpec(deliver); // The src account may send either xrp or iou. The dst account may receive // either xrp or iou. Since XRP and IOU amounts are represented by different // types, use templates to tell `flow` about the amount types. if (srcIsXRP && dstIsXRP) { - return finishFlow (sb, srcIssue, dstIssue, - flow ( - sb, strands, asDeliver.xrp, partialPayment, offerCrossing, - limitQuality, sendMax, j, flowDebugInfo)); + return finishFlow( + sb, + srcIssue, + dstIssue, + flow( + sb, + strands, + asDeliver.xrp, + partialPayment, + offerCrossing, + limitQuality, + sendMax, + j, + flowDebugInfo)); } if (srcIsXRP && !dstIsXRP) { - return finishFlow (sb, srcIssue, dstIssue, - flow ( - sb, strands, asDeliver.iou, partialPayment, offerCrossing, - limitQuality, sendMax, j, flowDebugInfo)); + return finishFlow( + sb, + srcIssue, + dstIssue, + flow( + sb, + strands, + asDeliver.iou, + partialPayment, + offerCrossing, + limitQuality, + sendMax, + j, + flowDebugInfo)); } if (!srcIsXRP && dstIsXRP) { - return finishFlow (sb, srcIssue, dstIssue, - flow ( - sb, strands, asDeliver.xrp, partialPayment, offerCrossing, - limitQuality, sendMax, j, flowDebugInfo)); + return finishFlow( + sb, + srcIssue, + dstIssue, + flow( + sb, + strands, + asDeliver.xrp, + partialPayment, + offerCrossing, + limitQuality, + sendMax, + j, + flowDebugInfo)); } - assert (!srcIsXRP && !dstIsXRP); - return finishFlow (sb, srcIssue, dstIssue, - flow ( - sb, strands, asDeliver.iou, partialPayment, offerCrossing, - limitQuality, sendMax, j, flowDebugInfo)); - + assert(!srcIsXRP && !dstIsXRP); + return finishFlow( + sb, + srcIssue, + dstIssue, + flow( + sb, + strands, + asDeliver.iou, + partialPayment, + offerCrossing, + limitQuality, + sendMax, + j, + flowDebugInfo)); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/Flow.h b/src/ripple/app/paths/Flow.h index 99c6e7b365..70128b0955 100644 --- a/src/ripple/app/paths/Flow.h +++ b/src/ripple/app/paths/Flow.h @@ -20,18 +20,17 @@ #ifndef RIPPLE_APP_PATHS_FLOW_H_INCLUDED #define RIPPLE_APP_PATHS_FLOW_H_INCLUDED -#include #include +#include #include -namespace ripple -{ +namespace ripple { namespace path { -namespace detail{ +namespace detail { struct FlowDebugInfo; } -} +} // namespace path /** Make a payment from the src account to the dst account @@ -45,7 +44,8 @@ struct FlowDebugInfo; @param partialPayment If the payment cannot deliver the entire requested amount, deliver as much as possible, given the constraints @param ownerPaysTransferFee If true then owner, not sender, pays fee - @param offerCrossing If true then flow is executing offer crossing, not payments + @param offerCrossing If true then flow is executing offer crossing, not + payments @param limitQuality Do not use liquidity below this quality threshold @param sendMax Do not spend more than this amount @param j Journal to write journal messages to @@ -53,7 +53,8 @@ struct FlowDebugInfo; @return Actual amount in and out, and the result code */ path::RippleCalc::Output -flow (PaymentSandbox& view, +flow( + PaymentSandbox& view, STAmount const& deliver, AccountID const& src, AccountID const& dst, @@ -65,8 +66,8 @@ flow (PaymentSandbox& view, boost::optional const& limitQuality, boost::optional const& sendMax, beast::Journal j, - path::detail::FlowDebugInfo* flowDebugInfo=nullptr); + path::detail::FlowDebugInfo* flowDebugInfo = nullptr); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/NodeDirectory.h b/src/ripple/app/paths/NodeDirectory.h index cba69741b3..b9d4a3f076 100644 --- a/src/ripple/app/paths/NodeDirectory.h +++ b/src/ripple/app/paths/NodeDirectory.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_APP_PATHS_NODEDIRECTORY_H_INCLUDED #define RIPPLE_APP_PATHS_NODEDIRECTORY_H_INCLUDED +#include #include #include -#include namespace ripple { @@ -48,15 +48,17 @@ public: SLE::pointer ledgerEntry; - void restart (bool multiQuality) + void + restart(bool multiQuality) { if (multiQuality) - current = beast::zero; // Restart book searching. + current = beast::zero; // Restart book searching. else - restartNeeded = true; // Restart at same quality. + restartNeeded = true; // Restart at same quality. } - bool initialize (Book const& book, ApplyView& view) + bool + initialize(Book const& book, ApplyView& view) { if (current != beast::zero) return false; @@ -67,28 +69,23 @@ public: // TODO(tom): it seems impossible that any actual offers with // quality == 0 could occur - we should disallow them, and clear // directory.ledgerEntry without the database call in the next line. - ledgerEntry = view.peek (keylet::page(current)); + ledgerEntry = view.peek(keylet::page(current)); // Advance, if didn't find it. Normal not to be unable to lookup // firstdirectory. Maybe even skip this lookup. - advanceNeeded = ! ledgerEntry; - restartNeeded = false; + advanceNeeded = !ledgerEntry; + restartNeeded = false; // Associated vars are dirty, if found it. return bool(ledgerEntry); } - enum Advance - { - NO_ADVANCE, - NEW_QUALITY, - END_ADVANCE - }; + enum Advance { NO_ADVANCE, NEW_QUALITY, END_ADVANCE }; /** Advance to the next quality directory in the order book. */ // VFALCO Consider renaming this to `nextQuality` or something Advance - advance (ApplyView& view) + advance(ApplyView& view) { if (!(advanceNeeded || restartNeeded)) return NO_ADVANCE; @@ -98,21 +95,20 @@ public: // quality in O(1). if (advanceNeeded) { - auto const opt = - view.succ (current, next); + auto const opt = view.succ(current, next); current = opt ? *opt : uint256{}; } - advanceNeeded = false; - restartNeeded = false; + advanceNeeded = false; + restartNeeded = false; if (current == beast::zero) return END_ADVANCE; - ledgerEntry = view.peek (keylet::page(current)); + ledgerEntry = view.peek(keylet::page(current)); return NEW_QUALITY; } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/PathRequest.cpp b/src/ripple/app/paths/PathRequest.cpp index 972ad7a321..704adab0e8 100644 --- a/src/ripple/app/paths/PathRequest.cpp +++ b/src/ripple/app/paths/PathRequest.cpp @@ -17,78 +17,76 @@ */ //============================================================================== -#include -#include -#include -#include #include #include #include +#include +#include +#include +#include #include +#include #include #include #include #include #include -#include #include #include #include namespace ripple { -PathRequest::PathRequest ( +PathRequest::PathRequest( Application& app, const std::shared_ptr& subscriber, int id, PathRequests& owner, beast::Journal journal) - : app_ (app) - , m_journal (journal) - , mOwner (owner) - , wpSubscriber (subscriber) - , consumer_(subscriber->getConsumer()) - , jvStatus (Json::objectValue) - , mLastIndex (0) - , mInProgress (false) - , iLevel (0) - , bLastSuccess (false) - , iIdentifier (id) - , created_ (std::chrono::steady_clock::now()) + : app_(app) + , m_journal(journal) + , mOwner(owner) + , wpSubscriber(subscriber) + , consumer_(subscriber->getConsumer()) + , jvStatus(Json::objectValue) + , mLastIndex(0) + , mInProgress(false) + , iLevel(0) + , bLastSuccess(false) + , iIdentifier(id) + , created_(std::chrono::steady_clock::now()) { - JLOG(m_journal.debug()) - << iIdentifier << " created"; + JLOG(m_journal.debug()) << iIdentifier << " created"; } -PathRequest::PathRequest ( +PathRequest::PathRequest( Application& app, - std::function const& completion, + std::function const& completion, Resource::Consumer& consumer, int id, PathRequests& owner, beast::Journal journal) - : app_ (app) - , m_journal (journal) - , mOwner (owner) - , fCompletion (completion) - , consumer_ (consumer) - , jvStatus (Json::objectValue) - , mLastIndex (0) - , mInProgress (false) - , iLevel (0) - , bLastSuccess (false) - , iIdentifier (id) - , created_ (std::chrono::steady_clock::now()) + : app_(app) + , m_journal(journal) + , mOwner(owner) + , fCompletion(completion) + , consumer_(consumer) + , jvStatus(Json::objectValue) + , mLastIndex(0) + , mInProgress(false) + , iLevel(0) + , bLastSuccess(false) + , iIdentifier(id) + , created_(std::chrono::steady_clock::now()) { - JLOG(m_journal.debug()) - << iIdentifier << " created"; + JLOG(m_journal.debug()) << iIdentifier << " created"; } PathRequest::~PathRequest() { using namespace std::chrono; auto stream = m_journal.info(); - if (! stream) + if (!stream) return; std::string fast, full; @@ -106,22 +104,25 @@ PathRequest::~PathRequest() duration_cast(full_reply_ - created_).count()); full += "ms"; } - stream << iIdentifier << " complete:" << fast << full << - " total:" << duration_cast(steady_clock::now() - - created_).count() << "ms"; + stream + << iIdentifier << " complete:" << fast << full << " total:" + << duration_cast(steady_clock::now() - created_).count() + << "ms"; } -bool PathRequest::isNew () +bool +PathRequest::isNew() { - std::lock_guard sl (mIndexLock); + std::lock_guard sl(mIndexLock); // does this path request still need its first full path return mLastIndex == 0; } -bool PathRequest::needsUpdate (bool newOnly, LedgerIndex index) +bool +PathRequest::needsUpdate(bool newOnly, LedgerIndex index) { - std::lock_guard sl (mIndexLock); + std::lock_guard sl(mIndexLock); if (mInProgress) { @@ -144,31 +145,34 @@ bool PathRequest::needsUpdate (bool newOnly, LedgerIndex index) return true; } -bool PathRequest::hasCompletion () +bool +PathRequest::hasCompletion() { - return bool (fCompletion); + return bool(fCompletion); } -void PathRequest::updateComplete () +void +PathRequest::updateComplete() { - std::lock_guard sl (mIndexLock); + std::lock_guard sl(mIndexLock); - assert (mInProgress); + assert(mInProgress); mInProgress = false; if (fCompletion) { fCompletion(); - fCompletion = std::function(); + fCompletion = std::function(); } } -bool PathRequest::isValid (std::shared_ptr const& crCache) +bool +PathRequest::isValid(std::shared_ptr const& crCache) { - if (! raSrcAccount || ! raDstAccount) + if (!raSrcAccount || !raDstAccount) return false; - if (! convert_all_ && (saSendMax || saDstAmount <= beast::zero)) + if (!convert_all_ && (saSendMax || saDstAmount <= beast::zero)) { // If send max specified, dst amt must be -1. jvStatus = rpcError(rpcDST_AMT_MALFORMED); @@ -177,52 +181,50 @@ bool PathRequest::isValid (std::shared_ptr const& crCache) auto const& lrLedger = crCache->getLedger(); - if (! lrLedger->exists(keylet::account(*raSrcAccount))) + if (!lrLedger->exists(keylet::account(*raSrcAccount))) { // Source account does not exist. - jvStatus = rpcError (rpcSRC_ACT_NOT_FOUND); + jvStatus = rpcError(rpcSRC_ACT_NOT_FOUND); return false; } - auto const sleDest = lrLedger->read( - keylet::account(*raDstAccount)); + auto const sleDest = lrLedger->read(keylet::account(*raDstAccount)); Json::Value& jvDestCur = (jvStatus[jss::destination_currencies] = Json::arrayValue); - if (! sleDest) + if (!sleDest) { - jvDestCur.append (Json::Value (systemCurrencyCode())); - if (! saDstAmount.native ()) + jvDestCur.append(Json::Value(systemCurrencyCode())); + if (!saDstAmount.native()) { // Only XRP can be send to a non-existent account. - jvStatus = rpcError (rpcACT_NOT_FOUND); + jvStatus = rpcError(rpcACT_NOT_FOUND); return false; } - if (! convert_all_ && saDstAmount < - STAmount (lrLedger->fees().accountReserve (0))) + if (!convert_all_ && + saDstAmount < STAmount(lrLedger->fees().accountReserve(0))) { // Payment must meet reserve. - jvStatus = rpcError (rpcDST_AMT_MALFORMED); + jvStatus = rpcError(rpcDST_AMT_MALFORMED); return false; } } else { - bool const disallowXRP ( - sleDest->getFlags() & lsfDisallowXRP); + bool const disallowXRP(sleDest->getFlags() & lsfDisallowXRP); - auto usDestCurrID = accountDestCurrencies ( - *raDstAccount, crCache, ! disallowXRP); + auto usDestCurrID = + accountDestCurrencies(*raDstAccount, crCache, !disallowXRP); for (auto const& currency : usDestCurrID) - jvDestCur.append (to_string (currency)); + jvDestCur.append(to_string(currency)); jvStatus[jss::destination_tag] = (sleDest->getFlags() & lsfRequireDestTag); } - jvStatus[jss::ledger_hash] = to_string (lrLedger->info().hash); + jvStatus[jss::ledger_hash] = to_string(lrLedger->info().hash); jvStatus[jss::ledger_index] = lrLedger->seq(); return true; } @@ -237,16 +239,16 @@ bool PathRequest::isValid (std::shared_ptr const& crCache) in all cases. */ std::pair -PathRequest::doCreate ( +PathRequest::doCreate( std::shared_ptr const& cache, Json::Value const& value) { bool valid = false; - if (parseJson (value) != PFR_PJ_INVALID) + if (parseJson(value) != PFR_PJ_INVALID) { - valid = isValid (cache); - if (! hasCompletion() && valid) + valid = isValid(cache); + if (!hasCompletion() && valid) doUpdate(cache, true); } @@ -254,10 +256,8 @@ PathRequest::doCreate ( { if (valid) { - stream << iIdentifier << - " valid: " << toBase58(*raSrcAccount); - stream << iIdentifier << - " deliver: " << saDstAmount.getFullText (); + stream << iIdentifier << " valid: " << toBase58(*raSrcAccount); + stream << iIdentifier << " deliver: " << saDstAmount.getFullText(); } else { @@ -265,118 +265,116 @@ PathRequest::doCreate ( } } - return { valid, jvStatus }; + return {valid, jvStatus}; } -int PathRequest::parseJson (Json::Value const& jvParams) +int +PathRequest::parseJson(Json::Value const& jvParams) { - if (! jvParams.isMember(jss::source_account)) + if (!jvParams.isMember(jss::source_account)) { jvStatus = rpcError(rpcSRC_ACT_MISSING); return PFR_PJ_INVALID; } - if (! jvParams.isMember(jss::destination_account)) + if (!jvParams.isMember(jss::destination_account)) { jvStatus = rpcError(rpcDST_ACT_MISSING); return PFR_PJ_INVALID; } - if (! jvParams.isMember(jss::destination_amount)) + if (!jvParams.isMember(jss::destination_amount)) { jvStatus = rpcError(rpcDST_AMT_MISSING); return PFR_PJ_INVALID; } - raSrcAccount = parseBase58( - jvParams[jss::source_account].asString()); - if (! raSrcAccount) + raSrcAccount = + parseBase58(jvParams[jss::source_account].asString()); + if (!raSrcAccount) { - jvStatus = rpcError (rpcSRC_ACT_MALFORMED); + jvStatus = rpcError(rpcSRC_ACT_MALFORMED); return PFR_PJ_INVALID; } - raDstAccount = parseBase58( - jvParams[jss::destination_account].asString()); - if (! raDstAccount) + raDstAccount = + parseBase58(jvParams[jss::destination_account].asString()); + if (!raDstAccount) { - jvStatus = rpcError (rpcDST_ACT_MALFORMED); + jvStatus = rpcError(rpcDST_ACT_MALFORMED); return PFR_PJ_INVALID; } - if (! amountFromJsonNoThrow ( - saDstAmount, jvParams[jss::destination_amount])) + if (!amountFromJsonNoThrow(saDstAmount, jvParams[jss::destination_amount])) { - jvStatus = rpcError (rpcDST_AMT_MALFORMED); + jvStatus = rpcError(rpcDST_AMT_MALFORMED); return PFR_PJ_INVALID; } convert_all_ = saDstAmount == STAmount(saDstAmount.issue(), 1u, 0, true); - if ((saDstAmount.getCurrency ().isZero () && - saDstAmount.getIssuer ().isNonZero ()) || - (saDstAmount.getCurrency () == badCurrency ()) || - (! convert_all_ && saDstAmount <= beast::zero)) + if ((saDstAmount.getCurrency().isZero() && + saDstAmount.getIssuer().isNonZero()) || + (saDstAmount.getCurrency() == badCurrency()) || + (!convert_all_ && saDstAmount <= beast::zero)) { - jvStatus = rpcError (rpcDST_AMT_MALFORMED); + jvStatus = rpcError(rpcDST_AMT_MALFORMED); return PFR_PJ_INVALID; } if (jvParams.isMember(jss::send_max)) { // Send_max requires destination amount to be -1. - if (! convert_all_) + if (!convert_all_) { jvStatus = rpcError(rpcDST_AMT_MALFORMED); return PFR_PJ_INVALID; } saSendMax.emplace(); - if (! amountFromJsonNoThrow( - *saSendMax, jvParams[jss::send_max]) || + if (!amountFromJsonNoThrow(*saSendMax, jvParams[jss::send_max]) || (saSendMax->getCurrency().isZero() && - saSendMax->getIssuer().isNonZero()) || + saSendMax->getIssuer().isNonZero()) || (saSendMax->getCurrency() == badCurrency()) || (*saSendMax <= beast::zero && - *saSendMax != STAmount(saSendMax->issue(), 1u, 0, true))) + *saSendMax != STAmount(saSendMax->issue(), 1u, 0, true))) { jvStatus = rpcError(rpcSENDMAX_MALFORMED); return PFR_PJ_INVALID; } } - if (jvParams.isMember (jss::source_currencies)) + if (jvParams.isMember(jss::source_currencies)) { Json::Value const& jvSrcCurrencies = jvParams[jss::source_currencies]; - if (! jvSrcCurrencies.isArray() || jvSrcCurrencies.size() == 0 || + if (!jvSrcCurrencies.isArray() || jvSrcCurrencies.size() == 0 || jvSrcCurrencies.size() > RPC::Tuning::max_src_cur) { - jvStatus = rpcError (rpcSRC_CUR_MALFORMED); + jvStatus = rpcError(rpcSRC_CUR_MALFORMED); return PFR_PJ_INVALID; } - sciSourceCurrencies.clear (); + sciSourceCurrencies.clear(); for (auto const& c : jvSrcCurrencies) { // Mandatory currency Currency srcCurrencyID; - if (! c.isObject() || - ! c.isMember(jss::currency) || - ! c[jss::currency].isString() || - ! to_currency(srcCurrencyID, c[jss::currency].asString())) + if (!c.isObject() || !c.isMember(jss::currency) || + !c[jss::currency].isString() || + !to_currency(srcCurrencyID, c[jss::currency].asString())) { - jvStatus = rpcError (rpcSRC_CUR_MALFORMED); + jvStatus = rpcError(rpcSRC_CUR_MALFORMED); return PFR_PJ_INVALID; } // Optional issuer AccountID srcIssuerID; - if (c.isMember (jss::issuer) && - (! c[jss::issuer].isString() || - ! to_issuer(srcIssuerID, c[jss::issuer].asString()))) + if (c.isMember(jss::issuer) && + (!c[jss::issuer].isString() || + !to_issuer(srcIssuerID, c[jss::issuer].asString()))) { - jvStatus = rpcError (rpcSRC_ISR_MALFORMED); + jvStatus = rpcError(rpcSRC_ISR_MALFORMED); return PFR_PJ_INVALID; } @@ -402,9 +400,9 @@ int PathRequest::parseJson (Json::Value const& jvParams) // source issuer is illegal. if (srcIssuerID != *raSrcAccount && saSendMax->getIssuer() != *raSrcAccount && - srcIssuerID != saSendMax->getIssuer()) + srcIssuerID != saSendMax->getIssuer()) { - jvStatus = rpcError (rpcSRC_ISR_MALFORMED); + jvStatus = rpcError(rpcSRC_ISR_MALFORMED); return PFR_PJ_INVALID; } @@ -434,39 +432,49 @@ int PathRequest::parseJson (Json::Value const& jvParams) } } - if (jvParams.isMember (jss::id)) + if (jvParams.isMember(jss::id)) jvId = jvParams[jss::id]; return PFR_PJ_NOCHANGE; } -Json::Value PathRequest::doClose (Json::Value const&) +Json::Value +PathRequest::doClose(Json::Value const&) { JLOG(m_journal.debug()) << iIdentifier << " closed"; - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); jvStatus[jss::closed] = true; return jvStatus; } -Json::Value PathRequest::doStatus (Json::Value const&) +Json::Value +PathRequest::doStatus(Json::Value const&) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); jvStatus[jss::status] = jss::success; return jvStatus; } std::unique_ptr const& -PathRequest::getPathFinder(std::shared_ptr const& cache, +PathRequest::getPathFinder( + std::shared_ptr const& cache, hash_map>& currency_map, - Currency const& currency, STAmount const& dst_amount, - int const level) + Currency const& currency, + STAmount const& dst_amount, + int const level) { auto i = currency_map.find(currency); if (i != currency_map.end()) return i->second; auto pathfinder = std::make_unique( - cache, *raSrcAccount, *raDstAccount, currency, - boost::none, dst_amount, saSendMax, app_); + cache, + *raSrcAccount, + *raDstAccount, + currency, + boost::none, + dst_amount, + saSendMax, + app_); if (pathfinder->findPaths(level)) pathfinder->computePathRanks(max_paths_); else @@ -475,17 +483,19 @@ PathRequest::getPathFinder(std::shared_ptr const& cache, } bool -PathRequest::findPaths (std::shared_ptr const& cache, - int const level, Json::Value& jvArray) +PathRequest::findPaths( + std::shared_ptr const& cache, + int const level, + Json::Value& jvArray) { auto sourceCurrencies = sciSourceCurrencies; - if (sourceCurrencies.empty ()) + if (sourceCurrencies.empty()) { auto currencies = accountSourceCurrencies(*raSrcAccount, cache, true); bool const sameAccount = *raSrcAccount == *raDstAccount; for (auto const& c : currencies) { - if (! sameAccount || c != saDstAmount.getCurrency()) + if (!sameAccount || c != saDstAmount.getCurrency()) { if (sourceCurrencies.size() >= RPC::Tuning::max_auto_src_cur) return false; @@ -495,20 +505,20 @@ PathRequest::findPaths (std::shared_ptr const& cache, } } - auto const dst_amount = convert_all_ ? - STAmount(saDstAmount.issue(), STAmount::cMaxValue, STAmount::cMaxOffset) - : saDstAmount; + auto const dst_amount = convert_all_ + ? STAmount( + saDstAmount.issue(), STAmount::cMaxValue, STAmount::cMaxOffset) + : saDstAmount; hash_map> currency_map; for (auto const& issue : sourceCurrencies) { JLOG(m_journal.debug()) << iIdentifier - << " Trying to find paths: " - << STAmount(issue, 1).getFullText(); + << " Trying to find paths: " << STAmount(issue, 1).getFullText(); - auto& pathfinder = getPathFinder(cache, currency_map, - issue.currency, dst_amount, level); - if (! pathfinder) + auto& pathfinder = getPathFinder( + cache, currency_map, issue.currency, dst_amount, level); + if (!pathfinder) { assert(false); JLOG(m_journal.debug()) << iIdentifier << " No paths found"; @@ -516,26 +526,24 @@ PathRequest::findPaths (std::shared_ptr const& cache, } STPath fullLiquidityPath; - auto ps = pathfinder->getBestPaths(max_paths_, - fullLiquidityPath, mContext[issue], issue.account); + auto ps = pathfinder->getBestPaths( + max_paths_, fullLiquidityPath, mContext[issue], issue.account); mContext[issue] = ps; - auto& sourceAccount = ! isXRP(issue.account) + auto& sourceAccount = !isXRP(issue.account) ? issue.account - : isXRP(issue.currency) - ? xrpAccount() - : *raSrcAccount; + : isXRP(issue.currency) ? xrpAccount() : *raSrcAccount; STAmount saMaxAmount = saSendMax.value_or( STAmount({issue.currency, sourceAccount}, 1u, 0, true)); - JLOG(m_journal.debug()) << iIdentifier - << " Paths found, calling rippleCalc"; + JLOG(m_journal.debug()) + << iIdentifier << " Paths found, calling rippleCalc"; path::RippleCalc::Input rcInput; if (convert_all_) rcInput.partialPaymentAllowed = true; - auto sandbox = std::make_unique - (&*cache->getLedger(), tapNONE); + auto sandbox = + std::make_unique(&*cache->getLedger(), tapNONE); auto rc = path::RippleCalc::rippleCalculate( *sandbox, saMaxAmount, // --> Amount to send is unlimited @@ -547,16 +555,15 @@ PathRequest::findPaths (std::shared_ptr const& cache, app_.logs(), &rcInput); - if (! convert_all_ && - ! fullLiquidityPath.empty() && + if (!convert_all_ && !fullLiquidityPath.empty() && (rc.result() == terNO_LINE || rc.result() == tecPATH_PARTIAL)) { - JLOG(m_journal.debug()) << iIdentifier - << " Trying with an extra path element"; + JLOG(m_journal.debug()) + << iIdentifier << " Trying with an extra path element"; ps.push_back(fullLiquidityPath); - sandbox = std::make_unique - (&*cache->getLedger(), tapNONE); + sandbox = + std::make_unique(&*cache->getLedger(), tapNONE); rc = path::RippleCalc::rippleCalculate( *sandbox, saMaxAmount, // --> Amount to send is unlimited @@ -569,42 +576,42 @@ PathRequest::findPaths (std::shared_ptr const& cache, if (rc.result() != tesSUCCESS) { - JLOG(m_journal.warn()) << iIdentifier - << " Failed with covering path " + JLOG(m_journal.warn()) + << iIdentifier << " Failed with covering path " << transHuman(rc.result()); } else { - JLOG(m_journal.debug()) << iIdentifier - << " Extra path element gives " + JLOG(m_journal.debug()) + << iIdentifier << " Extra path element gives " << transHuman(rc.result()); } } - if (rc.result () == tesSUCCESS) + if (rc.result() == tesSUCCESS) { - Json::Value jvEntry (Json::objectValue); - rc.actualAmountIn.setIssuer (sourceAccount); + Json::Value jvEntry(Json::objectValue); + rc.actualAmountIn.setIssuer(sourceAccount); jvEntry[jss::source_amount] = - rc.actualAmountIn.getJson (JsonOptions::none); + rc.actualAmountIn.getJson(JsonOptions::none); jvEntry[jss::paths_computed] = ps.getJson(JsonOptions::none); if (convert_all_) jvEntry[jss::destination_amount] = rc.actualAmountOut.getJson(JsonOptions::none); - if (hasCompletion ()) + if (hasCompletion()) { // Old ripple_path_find API requires this jvEntry[jss::paths_canonical] = Json::arrayValue; } - jvArray.append (jvEntry); + jvArray.append(jvEntry); } else { JLOG(m_journal.debug()) << iIdentifier << " rippleCalc returns " - << transHuman(rc.result()); + << transHuman(rc.result()); } } @@ -613,41 +620,43 @@ PathRequest::findPaths (std::shared_ptr const& cache, after four source currencies, 50 - (4 * 4) = 34. */ int const size = sourceCurrencies.size(); - consumer_.charge({boost::algorithm::clamp(size * size + 34, 50, 400), - "path update"}); + consumer_.charge( + {boost::algorithm::clamp(size * size + 34, 50, 400), "path update"}); return true; } -Json::Value PathRequest::doUpdate( - std::shared_ptr const& cache, bool fast) +Json::Value +PathRequest::doUpdate(std::shared_ptr const& cache, bool fast) { using namespace std::chrono; - JLOG(m_journal.debug()) << iIdentifier - << " update " << (fast ? "fast" : "normal"); + JLOG(m_journal.debug()) + << iIdentifier << " update " << (fast ? "fast" : "normal"); { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - if (!isValid (cache)) + if (!isValid(cache)) return jvStatus; } Json::Value newStatus = Json::objectValue; - if (hasCompletion ()) + if (hasCompletion()) { // Old ripple_path_find API gives destination_currencies - auto& destCurrencies = (newStatus[jss::destination_currencies] = Json::arrayValue); - auto usCurrencies = accountDestCurrencies (*raDstAccount, cache, true); + auto& destCurrencies = + (newStatus[jss::destination_currencies] = Json::arrayValue); + auto usCurrencies = accountDestCurrencies(*raDstAccount, cache, true); for (auto const& c : usCurrencies) - destCurrencies.append (to_string (c)); + destCurrencies.append(to_string(c)); } - newStatus[jss::source_account] = app_.accountIDCache().toBase58(*raSrcAccount); - newStatus[jss::destination_account] = app_.accountIDCache().toBase58(*raDstAccount); - newStatus[jss::destination_amount] = - saDstAmount.getJson (JsonOptions::none); - newStatus[jss::full_reply] = ! fast; + newStatus[jss::source_account] = + app_.accountIDCache().toBase58(*raSrcAccount); + newStatus[jss::destination_account] = + app_.accountIDCache().toBase58(*raDstAccount); + newStatus[jss::destination_amount] = saDstAmount.getJson(JsonOptions::none); + newStatus[jss::full_reply] = !fast; if (jvId) newStatus[jss::id] = jvId; @@ -685,14 +694,13 @@ Json::Value PathRequest::doUpdate( --iLevel; } - JLOG(m_journal.debug()) << iIdentifier - << " processing at level " << iLevel; + JLOG(m_journal.debug()) << iIdentifier << " processing at level " << iLevel; Json::Value jvArray = Json::arrayValue; if (findPaths(cache, iLevel, jvArray)) { bLastSuccess = jvArray.size() != 0; - newStatus[jss::alternatives] = std::move (jvArray); + newStatus[jss::alternatives] = std::move(jvArray); } else { @@ -705,7 +713,7 @@ Json::Value PathRequest::doUpdate( quick_reply_ = steady_clock::now(); mOwner.reportFast(duration_cast(quick_reply_ - created_)); } - else if (! fast && full_reply_ == steady_clock::time_point{}) + else if (!fast && full_reply_ == steady_clock::time_point{}) { full_reply_ = steady_clock::now(); mOwner.reportFull(duration_cast(full_reply_ - created_)); @@ -719,9 +727,10 @@ Json::Value PathRequest::doUpdate( return newStatus; } -InfoSub::pointer PathRequest::getSubscriber () +InfoSub::pointer +PathRequest::getSubscriber() { - return wpSubscriber.lock (); + return wpSubscriber.lock(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/PathRequest.h b/src/ripple/app/paths/PathRequest.h index 93f702bfc7..e0b9f37cdb 100644 --- a/src/ripple/app/paths/PathRequest.h +++ b/src/ripple/app/paths/PathRequest.h @@ -41,82 +41,96 @@ class RippleLineCache; class PathRequests; // Return values from parseJson <0 = invalid, >0 = valid -#define PFR_PJ_INVALID -1 -#define PFR_PJ_NOCHANGE 0 -#define PFR_PJ_CHANGE 1 +#define PFR_PJ_INVALID -1 +#define PFR_PJ_NOCHANGE 0 +#define PFR_PJ_CHANGE 1 -class PathRequest : - public std::enable_shared_from_this , - public CountedObject +class PathRequest : public std::enable_shared_from_this, + public CountedObject { public: - static char const* getCountedObjectName () { return "PathRequest"; } + static char const* + getCountedObjectName() + { + return "PathRequest"; + } - using wptr = std::weak_ptr; + using wptr = std::weak_ptr; using pointer = std::shared_ptr; - using ref = const pointer&; - using wref = const wptr&; + using ref = const pointer&; + using wref = const wptr&; public: // VFALCO TODO Break the cyclic dependency on InfoSub // path_find semantics // Subscriber is updated - PathRequest ( + PathRequest( Application& app, - std::shared_ptr const& subscriber, + std::shared_ptr const& subscriber, int id, PathRequests&, beast::Journal journal); // ripple_path_find semantics // Completion function is called after path update is complete - PathRequest ( + PathRequest( Application& app, - std::function const& completion, + std::function const& completion, Resource::Consumer& consumer, int id, PathRequests&, beast::Journal journal); - ~PathRequest (); + ~PathRequest(); - bool isNew (); - bool needsUpdate (bool newOnly, LedgerIndex index); + bool + isNew(); + bool + needsUpdate(bool newOnly, LedgerIndex index); // Called when the PathRequest update is complete. - void updateComplete (); + void + updateComplete(); - std::pair doCreate ( - std::shared_ptr const&, - Json::Value const&); + std::pair + doCreate(std::shared_ptr const&, Json::Value const&); - Json::Value doClose (Json::Value const&); - Json::Value doStatus (Json::Value const&); + Json::Value + doClose(Json::Value const&); + Json::Value + doStatus(Json::Value const&); // update jvStatus - Json::Value doUpdate ( - std::shared_ptr const&, bool fast); - InfoSub::pointer getSubscriber (); - bool hasCompletion (); + Json::Value + doUpdate(std::shared_ptr const&, bool fast); + InfoSub::pointer + getSubscriber(); + bool + hasCompletion(); private: - - bool isValid (std::shared_ptr const& crCache); - void setValid (); + bool + isValid(std::shared_ptr const& crCache); + void + setValid(); std::unique_ptr const& - getPathFinder(std::shared_ptr const&, - hash_map>&, Currency const&, - STAmount const&, int const); + getPathFinder( + std::shared_ptr const&, + hash_map>&, + Currency const&, + STAmount const&, + int const); /** Finds and sets a PathSet in the JSON argument. Returns false if the source currencies are inavlid. */ bool - findPaths (std::shared_ptr const&, int const, Json::Value&); + findPaths(std::shared_ptr const&, int const, Json::Value&); - int parseJson (Json::Value const&); + int + parseJson(Json::Value const&); Application& app_; beast::Journal m_journal; @@ -125,12 +139,12 @@ private: PathRequests& mOwner; - std::weak_ptr wpSubscriber; // Who this request came from - std::function fCompletion; - Resource::Consumer& consumer_; // Charge according to source currencies + std::weak_ptr wpSubscriber; // Who this request came from + std::function fCompletion; + Resource::Consumer& consumer_; // Charge according to source currencies Json::Value jvId; - Json::Value jvStatus; // Last result + Json::Value jvStatus; // Last result // Client request parameters boost::optional raSrcAccount; @@ -159,6 +173,6 @@ private: static unsigned int const max_paths_ = 4; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/PathRequests.cpp b/src/ripple/app/paths/PathRequests.cpp index 6814bc6d82..fe78f360fe 100644 --- a/src/ripple/app/paths/PathRequests.cpp +++ b/src/ripple/app/paths/PathRequests.cpp @@ -17,15 +17,15 @@ */ //============================================================================== -#include #include #include +#include #include #include #include #include -#include #include +#include #include namespace ripple { @@ -34,48 +34,49 @@ namespace ripple { Get the correct ledger to use. */ std::shared_ptr -PathRequests::getLineCache ( - std::shared_ptr const& ledger, +PathRequests::getLineCache( + std::shared_ptr const& ledger, bool authoritative) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); std::uint32_t lineSeq = mLineCache ? mLineCache->getLedger()->seq() : 0; std::uint32_t lgrSeq = ledger->seq(); - if ( (lineSeq == 0) || // no ledger - (authoritative && (lgrSeq > lineSeq)) || // newer authoritative ledger - (authoritative && ((lgrSeq + 8) < lineSeq)) || // we jumped way back for some reason - (lgrSeq > (lineSeq + 8))) // we jumped way forward for some reason + if ((lineSeq == 0) || // no ledger + (authoritative && (lgrSeq > lineSeq)) || // newer authoritative ledger + (authoritative && + ((lgrSeq + 8) < lineSeq)) || // we jumped way back for some reason + (lgrSeq > (lineSeq + 8))) // we jumped way forward for some reason { - mLineCache = std::make_shared (ledger); + mLineCache = std::make_shared(ledger); } return mLineCache; } -void PathRequests::updateAll (std::shared_ptr const& inLedger, - Job::CancelCallback shouldCancel) +void +PathRequests::updateAll( + std::shared_ptr const& inLedger, + Job::CancelCallback shouldCancel) { auto event = - app_.getJobQueue().makeLoadEvent( - jtPATH_FIND, "PathRequest::updateAll"); + app_.getJobQueue().makeLoadEvent(jtPATH_FIND, "PathRequest::updateAll"); std::vector requests; std::shared_ptr cache; // Get the ledger and cache we should be using { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); requests = requests_; - cache = getLineCache (inLedger, true); + cache = getLineCache(inLedger, true); } bool newRequests = app_.getLedgerMaster().isNewPathRequest(); bool mustBreak = false; - JLOG (mJournal.trace()) << - "updateAll seq=" << cache->getLedger()->seq() << - ", " << requests.size() << " requests"; + JLOG(mJournal.trace()) << "updateAll seq=" << cache->getLedger()->seq() + << ", " << requests.size() << " requests"; int processed = 0, removed = 0; @@ -86,31 +87,33 @@ void PathRequests::updateAll (std::shared_ptr const& inLedger, if (shouldCancel()) break; - auto request = wr.lock (); + auto request = wr.lock(); bool remove = true; if (request) { - if (!request->needsUpdate (newRequests, cache->getLedger()->seq())) + if (!request->needsUpdate( + newRequests, cache->getLedger()->seq())) remove = false; else { - if (auto ipSub = request->getSubscriber ()) + if (auto ipSub = request->getSubscriber()) { - if (!ipSub->getConsumer ().warn ()) + if (!ipSub->getConsumer().warn()) { - Json::Value update = request->doUpdate (cache, false); - request->updateComplete (); + Json::Value update = + request->doUpdate(cache, false); + request->updateComplete(); update[jss::type] = "path_find"; - ipSub->send (update, false); + ipSub->send(update, false); remove = false; ++processed; } } - else if (request->hasCompletion ()) + else if (request->hasCompletion()) { // One-shot request with completion function - request->doUpdate (cache, false); + request->doUpdate(cache, false); request->updateComplete(); ++processed; } @@ -119,14 +122,14 @@ void PathRequests::updateAll (std::shared_ptr const& inLedger, if (remove) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); // Remove any dangling weak pointers or weak // pointers that refer to this path request. - auto ret = std::remove_if ( - requests_.begin(), requests_.end(), - [&removed,&request](auto const& wl) - { + auto ret = std::remove_if( + requests_.begin(), + requests_.end(), + [&removed, &request](auto const& wl) { auto r = wl.lock(); if (r && r != request) @@ -135,29 +138,28 @@ void PathRequests::updateAll (std::shared_ptr const& inLedger, return true; }); - requests_.erase (ret, requests_.end()); + requests_.erase(ret, requests_.end()); } - mustBreak = !newRequests && - app_.getLedgerMaster().isNewPathRequest(); + mustBreak = + !newRequests && app_.getLedgerMaster().isNewPathRequest(); // We weren't handling new requests and then // there was a new request if (mustBreak) break; - } if (mustBreak) - { // a new request came in while we were working + { // a new request came in while we were working newRequests = true; } else if (newRequests) - { // we only did new requests, so we always need a last pass + { // we only did new requests, so we always need a last pass newRequests = app_.getLedgerMaster().isNewPathRequest(); } else - { // if there are no new requests, we are done + { // if there are no new requests, we are done newRequests = app_.getLedgerMaster().isNewPathRequest(); if (!newRequests) break; @@ -165,77 +167,72 @@ void PathRequests::updateAll (std::shared_ptr const& inLedger, { // Get the latest requests, cache, and ledger for next pass - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); if (requests_.empty()) break; requests = requests_; - cache = getLineCache (cache->getLedger(), false); + cache = getLineCache(cache->getLedger(), false); } - } - while (!shouldCancel ()); + } while (!shouldCancel()); - JLOG (mJournal.debug()) << - "updateAll complete: " << processed << " processed and " << - removed << " removed"; + JLOG(mJournal.debug()) << "updateAll complete: " << processed + << " processed and " << removed << " removed"; } -void PathRequests::insertPathRequest ( - PathRequest::pointer const& req) +void +PathRequests::insertPathRequest(PathRequest::pointer const& req) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); // Insert after any older unserviced requests but before // any serviced requests - auto ret = std::find_if ( - requests_.begin(), requests_.end(), - [](auto const& wl) - { + auto ret = + std::find_if(requests_.begin(), requests_.end(), [](auto const& wl) { auto r = wl.lock(); // We come before handled requests return r && !r->isNew(); }); - requests_.emplace (ret, req); + requests_.emplace(ret, req); } // Make a new-style path_find request Json::Value PathRequests::makePathRequest( - std::shared_ptr const& subscriber, + std::shared_ptr const& subscriber, std::shared_ptr const& inLedger, Json::Value const& requestJson) { - auto req = std::make_shared ( + auto req = std::make_shared( app_, subscriber, ++mLastIdentifier, *this, mJournal); - auto [valid, jvRes] = req->doCreate ( - getLineCache (inLedger, false), requestJson); + auto [valid, jvRes] = + req->doCreate(getLineCache(inLedger, false), requestJson); if (valid) { - subscriber->setPathRequest (req); - insertPathRequest (req); + subscriber->setPathRequest(req); + insertPathRequest(req); app_.getLedgerMaster().newPathRequest(); } - return std::move (jvRes); + return std::move(jvRes); } // Make an old-style ripple_path_find request Json::Value PathRequests::makeLegacyPathRequest( PathRequest::pointer& req, - std::function completion, + std::function completion, Resource::Consumer& consumer, std::shared_ptr const& inLedger, Json::Value const& request) { // This assignment must take place before the // completion function is called - req = std::make_shared ( - app_, completion, consumer, ++mLastIdentifier, - *this, mJournal); + req = std::make_shared( + app_, completion, consumer, ++mLastIdentifier, *this, mJournal); auto [valid, jvRes] = req->doCreate(getLineCache(inLedger, false), request); @@ -245,33 +242,33 @@ PathRequests::makeLegacyPathRequest( } else { - insertPathRequest (req); - if (! app_.getLedgerMaster().newPathRequest()) + insertPathRequest(req); + if (!app_.getLedgerMaster().newPathRequest()) { // The newPathRequest failed. Tell the caller. - jvRes = rpcError (rpcTOO_BUSY); + jvRes = rpcError(rpcTOO_BUSY); req.reset(); } } - return std::move (jvRes); + return std::move(jvRes); } Json::Value -PathRequests::doLegacyPathRequest ( - Resource::Consumer& consumer, - std::shared_ptr const& inLedger, - Json::Value const& request) +PathRequests::doLegacyPathRequest( + Resource::Consumer& consumer, + std::shared_ptr const& inLedger, + Json::Value const& request) { - auto cache = std::make_shared (inLedger); + auto cache = std::make_shared(inLedger); - auto req = std::make_shared (app_, []{}, - consumer, ++mLastIdentifier, *this, mJournal); + auto req = std::make_shared( + app_, [] {}, consumer, ++mLastIdentifier, *this, mJournal); - auto [valid, jvRes] = req->doCreate (cache, request); + auto [valid, jvRes] = req->doCreate(cache, request); if (valid) - jvRes = req->doUpdate (cache, false); - return std::move (jvRes); + jvRes = req->doUpdate(cache, false); + return std::move(jvRes); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/PathRequests.h b/src/ripple/app/paths/PathRequests.h index 67ed74407c..d95ab9c5d4 100644 --- a/src/ripple/app/paths/PathRequests.h +++ b/src/ripple/app/paths/PathRequests.h @@ -34,14 +34,14 @@ class PathRequests { public: /** A collection of all PathRequest instances. */ - PathRequests (Application& app, - beast::Journal journal, beast::insight::Collector::ptr const& collector) - : app_ (app) - , mJournal (journal) - , mLastIdentifier (0) + PathRequests( + Application& app, + beast::Journal journal, + beast::insight::Collector::ptr const& collector) + : app_(app), mJournal(journal), mLastIdentifier(0) { - mFast = collector->make_event ("pathfind_fast"); - mFull = collector->make_event ("pathfind_full"); + mFast = collector->make_event("pathfind_fast"); + mFull = collector->make_event("pathfind_full"); } /** Update all of the contained PathRequest instances. @@ -49,67 +49,76 @@ public: @param ledger Ledger we are pathfinding in. @param shouldCancel Invocable that returns whether to cancel. */ - void updateAll (std::shared_ptr const& ledger, - Job::CancelCallback shouldCancel); + void + updateAll( + std::shared_ptr const& ledger, + Job::CancelCallback shouldCancel); - std::shared_ptr getLineCache ( - std::shared_ptr const& ledger, bool authoritative); + std::shared_ptr + getLineCache( + std::shared_ptr const& ledger, + bool authoritative); // Create a new-style path request that pushes // updates to a subscriber - Json::Value makePathRequest ( - std::shared_ptr const& subscriber, + Json::Value + makePathRequest( + std::shared_ptr const& subscriber, std::shared_ptr const& ledger, Json::Value const& request); // Create an old-style path request that is // managed by a coroutine and updated by // the path engine - Json::Value makeLegacyPathRequest ( + Json::Value + makeLegacyPathRequest( PathRequest::pointer& req, - std::function completion, + std::function completion, Resource::Consumer& consumer, std::shared_ptr const& inLedger, Json::Value const& request); // Execute an old-style path request immediately // with the ledger specified by the caller - Json::Value doLegacyPathRequest ( + Json::Value + doLegacyPathRequest( Resource::Consumer& consumer, std::shared_ptr const& inLedger, Json::Value const& request); - void reportFast (std::chrono::milliseconds ms) + void + reportFast(std::chrono::milliseconds ms) { - mFast.notify (ms); + mFast.notify(ms); } - void reportFull (std::chrono::milliseconds ms) + void + reportFull(std::chrono::milliseconds ms) { - mFull.notify (ms); + mFull.notify(ms); } private: - void insertPathRequest (PathRequest::pointer const&); + void + insertPathRequest(PathRequest::pointer const&); Application& app_; - beast::Journal mJournal; + beast::Journal mJournal; - beast::insight::Event mFast; - beast::insight::Event mFull; + beast::insight::Event mFast; + beast::insight::Event mFull; // Track all requests std::vector requests_; // Use a RippleLineCache - std::shared_ptr mLineCache; + std::shared_ptr mLineCache; - std::atomic mLastIdentifier; + std::atomic mLastIdentifier; std::recursive_mutex mLock; - }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/Pathfinder.cpp b/src/ripple/app/paths/Pathfinder.cpp index 766590e096..752a155f35 100644 --- a/src/ripple/app/paths/Pathfinder.cpp +++ b/src/ripple/app/paths/Pathfinder.cpp @@ -17,17 +17,17 @@ */ //============================================================================== +#include #include -#include #include #include #include -#include -#include +#include #include -#include -#include #include +#include +#include +#include #include /* @@ -74,9 +74,11 @@ struct AccountCandidate static const int highPriority = 10000; }; -bool compareAccountCandidate ( +bool +compareAccountCandidate( std::uint32_t seq, - AccountCandidate const& first, AccountCandidate const& second) + AccountCandidate const& first, + AccountCandidate const& second) { if (first.priority < second.priority) return false; @@ -99,7 +101,8 @@ using CostedPathList = std::vector; using PathTable = std::map; -struct PathCost { +struct PathCost +{ int cost; char const* path; }; @@ -107,7 +110,8 @@ using PathCostList = std::vector; static PathTable mPathTable; -std::string pathTypeToString (Pathfinder::PathType const& type) +std::string +pathTypeToString(Pathfinder::PathType const& type) { std::string ret; @@ -141,7 +145,7 @@ std::string pathTypeToString (Pathfinder::PathType const& type) } // namespace -Pathfinder::Pathfinder ( +Pathfinder::Pathfinder( std::shared_ptr const& cache, AccountID const& uSrcAccount, AccountID const& uDstAccount, @@ -150,46 +154,55 @@ Pathfinder::Pathfinder ( STAmount const& saDstAmount, boost::optional const& srcAmount, Application& app) - : mSrcAccount (uSrcAccount), - mDstAccount (uDstAccount), - mEffectiveDst (isXRP(saDstAmount.getIssuer ()) ? - uDstAccount : saDstAmount.getIssuer ()), - mDstAmount (saDstAmount), - mSrcCurrency (uSrcCurrency), - mSrcIssuer (uSrcIssuer), - mSrcAmount (srcAmount.value_or(STAmount({uSrcCurrency, - uSrcIssuer.value_or(isXRP(uSrcCurrency) ? - xrpAccount() : uSrcAccount)}, 1u, 0, true))), - convert_all_ (mDstAmount == - STAmount(mDstAmount.issue(), STAmount::cMaxValue, STAmount::cMaxOffset)), - mLedger (cache->getLedger ()), - mRLCache (cache), - app_ (app), - j_ (app.journal ("Pathfinder")) + : mSrcAccount(uSrcAccount) + , mDstAccount(uDstAccount) + , mEffectiveDst( + isXRP(saDstAmount.getIssuer()) ? uDstAccount + : saDstAmount.getIssuer()) + , mDstAmount(saDstAmount) + , mSrcCurrency(uSrcCurrency) + , mSrcIssuer(uSrcIssuer) + , mSrcAmount(srcAmount.value_or(STAmount( + {uSrcCurrency, + uSrcIssuer.value_or( + isXRP(uSrcCurrency) ? xrpAccount() : uSrcAccount)}, + 1u, + 0, + true))) + , convert_all_( + mDstAmount == + STAmount( + mDstAmount.issue(), + STAmount::cMaxValue, + STAmount::cMaxOffset)) + , mLedger(cache->getLedger()) + , mRLCache(cache) + , app_(app) + , j_(app.journal("Pathfinder")) { - assert (! uSrcIssuer || isXRP(uSrcCurrency) == isXRP(uSrcIssuer.get())); + assert(!uSrcIssuer || isXRP(uSrcCurrency) == isXRP(uSrcIssuer.get())); } -bool Pathfinder::findPaths (int searchLevel) +bool +Pathfinder::findPaths(int searchLevel) { if (mDstAmount == beast::zero) { // No need to send zero money. - JLOG (j_.debug()) << "Destination amount was zero."; - mLedger.reset (); + JLOG(j_.debug()) << "Destination amount was zero."; + mLedger.reset(); return false; // TODO(tom): why do we reset the ledger just in this case and the one // below - why don't we do it each time we return false? } - if (mSrcAccount == mDstAccount && - mDstAccount == mEffectiveDst && - mSrcCurrency == mDstAmount.getCurrency ()) + if (mSrcAccount == mDstAccount && mDstAccount == mEffectiveDst && + mSrcCurrency == mDstAmount.getCurrency()) { // No need to send to same account with same currency. - JLOG (j_.debug()) << "Tried to send to same issuer"; - mLedger.reset (); + JLOG(j_.debug()) << "Tried to send to same issuer"; + mLedger.reset(); return false; } @@ -200,66 +213,61 @@ bool Pathfinder::findPaths (int searchLevel) return true; } - m_loadEvent = app_.getJobQueue ().makeLoadEvent ( - jtPATH_FIND, "FindPath"); - auto currencyIsXRP = isXRP (mSrcCurrency); + m_loadEvent = app_.getJobQueue().makeLoadEvent(jtPATH_FIND, "FindPath"); + auto currencyIsXRP = isXRP(mSrcCurrency); - bool useIssuerAccount - = mSrcIssuer && !currencyIsXRP && !isXRP (*mSrcIssuer); + bool useIssuerAccount = mSrcIssuer && !currencyIsXRP && !isXRP(*mSrcIssuer); auto& account = useIssuerAccount ? *mSrcIssuer : mSrcAccount; auto issuer = currencyIsXRP ? AccountID() : account; - mSource = STPathElement (account, mSrcCurrency, issuer); - auto issuerString = mSrcIssuer - ? to_string (*mSrcIssuer) : std::string ("none"); - JLOG (j_.trace()) - << "findPaths>" - << " mSrcAccount=" << mSrcAccount - << " mDstAccount=" << mDstAccount - << " mDstAmount=" << mDstAmount.getFullText () - << " mSrcCurrency=" << mSrcCurrency - << " mSrcIssuer=" << issuerString; + mSource = STPathElement(account, mSrcCurrency, issuer); + auto issuerString = + mSrcIssuer ? to_string(*mSrcIssuer) : std::string("none"); + JLOG(j_.trace()) << "findPaths>" + << " mSrcAccount=" << mSrcAccount + << " mDstAccount=" << mDstAccount + << " mDstAmount=" << mDstAmount.getFullText() + << " mSrcCurrency=" << mSrcCurrency + << " mSrcIssuer=" << issuerString; if (!mLedger) { - JLOG (j_.debug()) << "findPaths< no ledger"; + JLOG(j_.debug()) << "findPaths< no ledger"; return false; } - bool bSrcXrp = isXRP (mSrcCurrency); - bool bDstXrp = isXRP (mDstAmount.getCurrency()); + bool bSrcXrp = isXRP(mSrcCurrency); + bool bDstXrp = isXRP(mDstAmount.getCurrency()); - if (! mLedger->exists (keylet::account(mSrcAccount))) + if (!mLedger->exists(keylet::account(mSrcAccount))) { // We can't even start without a source account. - JLOG (j_.debug()) << "invalid source account"; + JLOG(j_.debug()) << "invalid source account"; return false; } if ((mEffectiveDst != mDstAccount) && - ! mLedger->exists (keylet::account(mEffectiveDst))) + !mLedger->exists(keylet::account(mEffectiveDst))) { - JLOG (j_.debug()) - << "Non-existent gateway"; + JLOG(j_.debug()) << "Non-existent gateway"; return false; } - if (! mLedger->exists (keylet::account (mDstAccount))) + if (!mLedger->exists(keylet::account(mDstAccount))) { // Can't find the destination account - we must be funding a new // account. if (!bDstXrp) { - JLOG (j_.debug()) - << "New account not being funded in XRP "; + JLOG(j_.debug()) << "New account not being funded in XRP "; return false; } - auto const reserve = STAmount (mLedger->fees().accountReserve (0)); + auto const reserve = STAmount(mLedger->fees().accountReserve(0)); if (mDstAmount < reserve) { - JLOG (j_.debug()) - << "New account not getting enough funding: " - << mDstAmount << " < " << reserve; + JLOG(j_.debug()) + << "New account not getting enough funding: " << mDstAmount + << " < " << reserve; return false; } } @@ -270,31 +278,31 @@ bool Pathfinder::findPaths (int searchLevel) if (bSrcXrp && bDstXrp) { // XRP -> XRP - JLOG (j_.debug()) << "XRP to XRP payment"; + JLOG(j_.debug()) << "XRP to XRP payment"; paymentType = pt_XRP_to_XRP; } else if (bSrcXrp) { // XRP -> non-XRP - JLOG (j_.debug()) << "XRP to non-XRP payment"; + JLOG(j_.debug()) << "XRP to non-XRP payment"; paymentType = pt_XRP_to_nonXRP; } else if (bDstXrp) { // non-XRP -> XRP - JLOG (j_.debug()) << "non-XRP to XRP payment"; + JLOG(j_.debug()) << "non-XRP to XRP payment"; paymentType = pt_nonXRP_to_XRP; } - else if (mSrcCurrency == mDstAmount.getCurrency ()) + else if (mSrcCurrency == mDstAmount.getCurrency()) { // non-XRP -> non-XRP - Same currency - JLOG (j_.debug()) << "non-XRP to non-XRP - same currency"; + JLOG(j_.debug()) << "non-XRP to non-XRP - same currency"; paymentType = pt_nonXRP_to_same; } else { // non-XRP to non-XRP - Different currency - JLOG (j_.debug()) << "non-XRP to non-XRP - cross currency"; + JLOG(j_.debug()) << "non-XRP to non-XRP - cross currency"; paymentType = pt_nonXRP_to_nonXRP; } @@ -304,24 +312,24 @@ bool Pathfinder::findPaths (int searchLevel) // Only use paths with at most the current search level. if (costedPath.searchLevel <= searchLevel) { - addPathsForType (costedPath.type); + addPathsForType(costedPath.type); // TODO(tom): we might be missing other good paths with this // arbitrary cut off. - if (mCompletePaths.size () > PATHFINDER_MAX_COMPLETE_PATHS) + if (mCompletePaths.size() > PATHFINDER_MAX_COMPLETE_PATHS) break; } } - JLOG (j_.debug()) - << mCompletePaths.size () << " complete paths found"; + JLOG(j_.debug()) << mCompletePaths.size() << " complete paths found"; // Even if we find no paths, default paths may work, and we don't check them // currently. return true; } -TER Pathfinder::getPathLiquidity ( +TER +Pathfinder::getPathLiquidity( STPath const& path, // IN: The path to check. STAmount const& minDstAmount, // IN: The minimum output this path must // deliver to be worth keeping. @@ -329,12 +337,12 @@ TER Pathfinder::getPathLiquidity ( uint64_t& qualityOut) const // OUT: The returned initial quality { STPathSet pathSet; - pathSet.push_back (path); + pathSet.push_back(path); path::RippleCalc::Input rcInput; rcInput.defaultPathsAllowed = false; - PaymentSandbox sandbox (&*mLedger, tapNONE); + PaymentSandbox sandbox(&*mLedger, tapNONE); try { @@ -342,7 +350,7 @@ TER Pathfinder::getPathLiquidity ( if (convert_all_) rcInput.partialPaymentAllowed = true; - auto rc = path::RippleCalc::rippleCalculate ( + auto rc = path::RippleCalc::rippleCalculate( sandbox, mSrcAmount, minDstAmount, @@ -352,28 +360,28 @@ TER Pathfinder::getPathLiquidity ( app_.logs(), &rcInput); // If we can't get even the minimum liquidity requested, we're done. - if (rc.result () != tesSUCCESS) - return rc.result (); + if (rc.result() != tesSUCCESS) + return rc.result(); - qualityOut = getRate (rc.actualAmountOut, rc.actualAmountIn); + qualityOut = getRate(rc.actualAmountOut, rc.actualAmountIn); amountOut = rc.actualAmountOut; - if (! convert_all_) + if (!convert_all_) { // Now try to compute the remaining liquidity. rcInput.partialPaymentAllowed = true; - rc = path::RippleCalc::rippleCalculate ( + rc = path::RippleCalc::rippleCalculate( sandbox, mSrcAmount, mDstAmount - amountOut, mDstAccount, mSrcAccount, pathSet, - app_.logs (), + app_.logs(), &rcInput); // If we found further liquidity, add it into the result. - if (rc.result () == tesSUCCESS) + if (rc.result() == tesSUCCESS) amountOut += rc.actualAmountOut; } @@ -381,9 +389,8 @@ TER Pathfinder::getPathLiquidity ( } catch (std::exception const& e) { - JLOG (j_.info()) << - "checkpath: exception (" << e.what() << ") " << - path.getJson (JsonOptions::none); + JLOG(j_.info()) << "checkpath: exception (" << e.what() << ") " + << path.getJson(JsonOptions::none); return tefEXCEPTION; } } @@ -392,58 +399,61 @@ namespace { // Return the smallest amount of useful liquidity for a given amount, and the // total number of paths we have to evaluate. -STAmount smallestUsefulAmount (STAmount const& amount, int maxPaths) +STAmount +smallestUsefulAmount(STAmount const& amount, int maxPaths) { - return divide (amount, STAmount (maxPaths + 2), amount.issue ()); + return divide(amount, STAmount(maxPaths + 2), amount.issue()); } -} // namespace +} // namespace -void Pathfinder::computePathRanks (int maxPaths) +void +Pathfinder::computePathRanks(int maxPaths) { - mRemainingAmount = convert_all_ ? - STAmount(mDstAmount.issue(), STAmount::cMaxValue, - STAmount::cMaxOffset) + mRemainingAmount = convert_all_ + ? STAmount( + mDstAmount.issue(), STAmount::cMaxValue, STAmount::cMaxOffset) : mDstAmount; // Must subtract liquidity in default path from remaining amount. try { - PaymentSandbox sandbox (&*mLedger, tapNONE); + PaymentSandbox sandbox(&*mLedger, tapNONE); path::RippleCalc::Input rcInput; rcInput.partialPaymentAllowed = true; - auto rc = path::RippleCalc::rippleCalculate ( + auto rc = path::RippleCalc::rippleCalculate( sandbox, mSrcAmount, mRemainingAmount, mDstAccount, mSrcAccount, STPathSet(), - app_.logs (), + app_.logs(), &rcInput); - if (rc.result () == tesSUCCESS) + if (rc.result() == tesSUCCESS) { - JLOG (j_.debug()) - << "Default path contributes: " << rc.actualAmountIn; + JLOG(j_.debug()) + << "Default path contributes: " << rc.actualAmountIn; mRemainingAmount -= rc.actualAmountOut; } else { - JLOG (j_.debug()) - << "Default path fails: " << transToken (rc.result ()); + JLOG(j_.debug()) + << "Default path fails: " << transToken(rc.result()); } } catch (std::exception const&) { - JLOG (j_.debug()) << "Default path causes exception"; + JLOG(j_.debug()) << "Default path causes exception"; } - rankPaths (maxPaths, mCompletePaths, mPathRanks); + rankPaths(maxPaths, mCompletePaths, mPathRanks); } -static bool isDefaultPath (STPath const& path) +static bool +isDefaultPath(STPath const& path) { // TODO(tom): default paths can consist of more than just an account: // https://forum.ripple.com/viewtopic.php?f=2&t=8206&start=10#p57713 @@ -462,35 +472,37 @@ static bool isDefaultPath (STPath const& path) return path.size() == 1; } -static STPath removeIssuer (STPath const& path) +static STPath +removeIssuer(STPath const& path) { // This path starts with the issuer, which is already implied // so remove the head node STPath ret; for (auto it = path.begin() + 1; it != path.end(); ++it) - ret.push_back (*it); + ret.push_back(*it); return ret; } // For each useful path in the input path set, // create a ranking entry in the output vector of path ranks -void Pathfinder::rankPaths ( +void +Pathfinder::rankPaths( int maxPaths, STPathSet const& paths, - std::vector & rankedPaths) + std::vector& rankedPaths) { - rankedPaths.clear (); - rankedPaths.reserve (paths.size()); + rankedPaths.clear(); + rankedPaths.reserve(paths.size()); STAmount saMinDstAmount; if (convert_all_) { // On convert_all_ partialPaymentAllowed will be set to true // and requiring a huge amount will find the highest liquidity. - saMinDstAmount = STAmount(mDstAmount.issue(), - STAmount::cMaxValue, STAmount::cMaxOffset); + saMinDstAmount = STAmount( + mDstAmount.issue(), STAmount::cMaxValue, STAmount::cMaxOffset); } else { @@ -498,30 +510,28 @@ void Pathfinder::rankPaths ( saMinDstAmount = smallestUsefulAmount(mDstAmount, maxPaths); } - for (int i = 0; i < paths.size (); ++i) + for (int i = 0; i < paths.size(); ++i) { auto const& currentPath = paths[i]; - if (! currentPath.empty()) + if (!currentPath.empty()) { STAmount liquidity; uint64_t uQuality; - auto const resultCode = getPathLiquidity ( + auto const resultCode = getPathLiquidity( currentPath, saMinDstAmount, liquidity, uQuality); if (resultCode != tesSUCCESS) { - JLOG (j_.debug()) << - "findPaths: dropping : " << - transToken (resultCode) << - ": " << currentPath.getJson (JsonOptions::none); + JLOG(j_.debug()) + << "findPaths: dropping : " << transToken(resultCode) + << ": " << currentPath.getJson(JsonOptions::none); } else { - JLOG (j_.debug()) << - "findPaths: quality: " << uQuality << - ": " << currentPath.getJson (JsonOptions::none); + JLOG(j_.debug()) << "findPaths: quality: " << uQuality << ": " + << currentPath.getJson(JsonOptions::none); - rankedPaths.push_back ({uQuality, - currentPath.size (), liquidity, i}); + rankedPaths.push_back( + {uQuality, currentPath.size(), liquidity, i}); } } } @@ -531,45 +541,46 @@ void Pathfinder::rankPaths ( // width of path // length of path // A better PathRank is lower, best are sorted to the beginning. - std::sort(rankedPaths.begin(), rankedPaths.end(), - [&](Pathfinder::PathRank const& a, Pathfinder::PathRank const& b) - { - // 1) Higher quality (lower cost) is better - if (! convert_all_ && a.quality != b.quality) - return a.quality < b.quality; + std::sort( + rankedPaths.begin(), + rankedPaths.end(), + [&](Pathfinder::PathRank const& a, Pathfinder::PathRank const& b) { + // 1) Higher quality (lower cost) is better + if (!convert_all_ && a.quality != b.quality) + return a.quality < b.quality; - // 2) More liquidity (higher volume) is better - if (a.liquidity != b.liquidity) - return a.liquidity > b.liquidity; + // 2) More liquidity (higher volume) is better + if (a.liquidity != b.liquidity) + return a.liquidity > b.liquidity; - // 3) Shorter paths are better - if (a.length != b.length) - return a.length < b.length; + // 3) Shorter paths are better + if (a.length != b.length) + return a.length < b.length; - // 4) Tie breaker - return a.index > b.index; - }); + // 4) Tie breaker + return a.index > b.index; + }); } STPathSet -Pathfinder::getBestPaths ( +Pathfinder::getBestPaths( int maxPaths, STPath& fullLiquidityPath, STPathSet const& extraPaths, AccountID const& srcIssuer) { - JLOG (j_.debug()) << "findPaths: " << - mCompletePaths.size() << " paths and " << - extraPaths.size () << " extras"; + JLOG(j_.debug()) << "findPaths: " << mCompletePaths.size() << " paths and " + << extraPaths.size() << " extras"; if (mCompletePaths.empty() && extraPaths.empty()) return mCompletePaths; - assert (fullLiquidityPath.empty ()); - const bool issuerIsSender = isXRP (mSrcCurrency) || (srcIssuer == mSrcAccount); + assert(fullLiquidityPath.empty()); + const bool issuerIsSender = + isXRP(mSrcCurrency) || (srcIssuer == mSrcAccount); - std::vector extraPathRanks; - rankPaths (maxPaths, extraPaths, extraPathRanks); + std::vector extraPathRanks; + rankPaths(maxPaths, extraPaths, extraPathRanks); STPathSet bestPaths; @@ -582,7 +593,7 @@ Pathfinder::getBestPaths ( auto extraPathsIterator = extraPathRanks.begin(); while (pathsIterator != mPathRanks.end() || - extraPathsIterator != extraPathRanks.end()) + extraPathsIterator != extraPathRanks.end()) { bool usePath = false; bool useExtraPath = false; @@ -608,8 +619,8 @@ Pathfinder::getBestPaths ( auto& pathRank = usePath ? *pathsIterator : *extraPathsIterator; - auto const& path = usePath ? mCompletePaths[pathRank.index] : - extraPaths[pathRank.index]; + auto const& path = usePath ? mCompletePaths[pathRank.index] + : extraPaths[pathRank.index]; if (useExtraPath) ++extraPathsIterator; @@ -617,23 +628,22 @@ Pathfinder::getBestPaths ( if (usePath) ++pathsIterator; - auto iPathsLeft = maxPaths - bestPaths.size (); - if (!(iPathsLeft > 0 || fullLiquidityPath.empty ())) + auto iPathsLeft = maxPaths - bestPaths.size(); + if (!(iPathsLeft > 0 || fullLiquidityPath.empty())) break; - if (path.empty ()) + if (path.empty()) { - assert (false); + assert(false); continue; } bool startsWithIssuer = false; - if (! issuerIsSender && usePath) + if (!issuerIsSender && usePath) { // Need to make sure path matches issuer constraints - if (isDefaultPath(path) || - path.front().getAccountID() != srcIssuer) + if (isDefaultPath(path) || path.front().getAccountID() != srcIssuer) { continue; } @@ -643,76 +653,74 @@ Pathfinder::getBestPaths ( if (iPathsLeft > 1 || (iPathsLeft > 0 && pathRank.liquidity >= remaining)) - // last path must fill + // last path must fill { --iPathsLeft; remaining -= pathRank.liquidity; - bestPaths.push_back (startsWithIssuer ? removeIssuer (path) : path); + bestPaths.push_back(startsWithIssuer ? removeIssuer(path) : path); } - else if (iPathsLeft == 0 && - pathRank.liquidity >= mDstAmount && - fullLiquidityPath.empty ()) + else if ( + iPathsLeft == 0 && pathRank.liquidity >= mDstAmount && + fullLiquidityPath.empty()) { // We found an extra path that can move the whole amount. - fullLiquidityPath = (startsWithIssuer ? removeIssuer (path) : path); - JLOG (j_.debug()) << - "Found extra full path: " << - fullLiquidityPath.getJson (JsonOptions::none); + fullLiquidityPath = (startsWithIssuer ? removeIssuer(path) : path); + JLOG(j_.debug()) << "Found extra full path: " + << fullLiquidityPath.getJson(JsonOptions::none); } else { - JLOG (j_.debug()) << - "Skipping a non-filling path: " << - path.getJson (JsonOptions::none); + JLOG(j_.debug()) << "Skipping a non-filling path: " + << path.getJson(JsonOptions::none); } } if (remaining > beast::zero) { - assert (fullLiquidityPath.empty ()); - JLOG (j_.info()) << - "Paths could not send " << remaining << " of " << mDstAmount; + assert(fullLiquidityPath.empty()); + JLOG(j_.info()) << "Paths could not send " << remaining << " of " + << mDstAmount; } else { - JLOG (j_.debug()) << - "findPaths: RESULTS: " << - bestPaths.getJson (JsonOptions::none); + JLOG(j_.debug()) << "findPaths: RESULTS: " + << bestPaths.getJson(JsonOptions::none); } return bestPaths; } -bool Pathfinder::issueMatchesOrigin (Issue const& issue) +bool +Pathfinder::issueMatchesOrigin(Issue const& issue) { bool matchingCurrency = (issue.currency == mSrcCurrency); - bool matchingAccount = - isXRP (issue.currency) || - (mSrcIssuer && issue.account == mSrcIssuer) || - issue.account == mSrcAccount; + bool matchingAccount = isXRP(issue.currency) || + (mSrcIssuer && issue.account == mSrcIssuer) || + issue.account == mSrcAccount; return matchingCurrency && matchingAccount; } -int Pathfinder::getPathsOut ( +int +Pathfinder::getPathsOut( Currency const& currency, AccountID const& account, bool isDstCurrency, AccountID const& dstAccount) { - Issue const issue (currency, account); + Issue const issue(currency, account); - auto [it, inserted] = mPathsOutCountMap.emplace (issue, 0); + auto [it, inserted] = mPathsOutCountMap.emplace(issue, 0); // If it was already present, return the stored number of paths if (!inserted) return it->second; - auto sleAccount = mLedger->read(keylet::account (account)); + auto sleAccount = mLedger->read(keylet::account(account)); if (!sleAccount) return 0; - int aFlags = sleAccount->getFieldU32 (sfFlags); + int aFlags = sleAccount->getFieldU32(sfFlags); bool const bAuthRequired = (aFlags & lsfRequireAuth) != 0; bool const bFrozen = ((aFlags & lsfGlobalFreeze) != 0); @@ -720,31 +728,32 @@ int Pathfinder::getPathsOut ( if (!bFrozen) { - count = app_.getOrderBookDB ().getBookSize (issue); + count = app_.getOrderBookDB().getBookSize(issue); - for (auto const& item : mRLCache->getRippleLines (account)) + for (auto const& item : mRLCache->getRippleLines(account)) { - RippleState* rspEntry = (RippleState*) item.get (); + RippleState* rspEntry = (RippleState*)item.get(); - if (currency != rspEntry->getLimit ().getCurrency ()) + if (currency != rspEntry->getLimit().getCurrency()) { } - else if (rspEntry->getBalance () <= beast::zero && - (!rspEntry->getLimitPeer () - || -rspEntry->getBalance () >= rspEntry->getLimitPeer () - || (bAuthRequired && !rspEntry->getAuth ()))) + else if ( + rspEntry->getBalance() <= beast::zero && + (!rspEntry->getLimitPeer() || + -rspEntry->getBalance() >= rspEntry->getLimitPeer() || + (bAuthRequired && !rspEntry->getAuth()))) { } - else if (isDstCurrency && - dstAccount == rspEntry->getAccountIDPeer ()) + else if ( + isDstCurrency && dstAccount == rspEntry->getAccountIDPeer()) { - count += 10000; // count a path to the destination extra + count += 10000; // count a path to the destination extra } - else if (rspEntry->getNoRipplePeer ()) + else if (rspEntry->getNoRipplePeer()) { // This probably isn't a useful path out } - else if (rspEntry->getFreezePeer ()) + else if (rspEntry->getFreezePeer()) { // Not a useful path out } @@ -758,128 +767,130 @@ int Pathfinder::getPathsOut ( return count; } -void Pathfinder::addLinks ( +void +Pathfinder::addLinks( STPathSet const& currentPaths, // The paths to build from STPathSet& incompletePaths, // The set of partial paths we add to int addFlags) { - JLOG (j_.debug()) - << "addLink< on " << currentPaths.size () - << " source(s), flags=" << addFlags; - for (auto const& path: currentPaths) - addLink (path, incompletePaths, addFlags); + JLOG(j_.debug()) << "addLink< on " << currentPaths.size() + << " source(s), flags=" << addFlags; + for (auto const& path : currentPaths) + addLink(path, incompletePaths, addFlags); } -STPathSet& Pathfinder::addPathsForType (PathType const& pathType) +STPathSet& +Pathfinder::addPathsForType(PathType const& pathType) { // See if the set of paths for this type already exists. - auto it = mPaths.find (pathType); - if (it != mPaths.end ()) + auto it = mPaths.find(pathType); + if (it != mPaths.end()) return it->second; // Otherwise, if the type has no nodes, return the empty path. - if (pathType.empty ()) + if (pathType.empty()) return mPaths[pathType]; // Otherwise, get the paths for the parent PathType by calling // addPathsForType recursively. PathType parentPathType = pathType; - parentPathType.pop_back (); + parentPathType.pop_back(); - STPathSet const& parentPaths = addPathsForType (parentPathType); + STPathSet const& parentPaths = addPathsForType(parentPathType); STPathSet& pathsOut = mPaths[pathType]; - JLOG (j_.debug()) - << "getPaths< adding onto '" - << pathTypeToString (parentPathType) << "' to get '" - << pathTypeToString (pathType) << "'"; + JLOG(j_.debug()) << "getPaths< adding onto '" + << pathTypeToString(parentPathType) << "' to get '" + << pathTypeToString(pathType) << "'"; - int initialSize = mCompletePaths.size (); + int initialSize = mCompletePaths.size(); // Add the last NodeType to the lists. - auto nodeType = pathType.back (); + auto nodeType = pathType.back(); switch (nodeType) { - case nt_SOURCE: - // Source must always be at the start, so pathsOut has to be empty. - assert (pathsOut.empty ()); - pathsOut.push_back (STPath ()); - break; + case nt_SOURCE: + // Source must always be at the start, so pathsOut has to be empty. + assert(pathsOut.empty()); + pathsOut.push_back(STPath()); + break; - case nt_ACCOUNTS: - addLinks (parentPaths, pathsOut, afADD_ACCOUNTS); - break; + case nt_ACCOUNTS: + addLinks(parentPaths, pathsOut, afADD_ACCOUNTS); + break; - case nt_BOOKS: - addLinks (parentPaths, pathsOut, afADD_BOOKS); - break; + case nt_BOOKS: + addLinks(parentPaths, pathsOut, afADD_BOOKS); + break; - case nt_XRP_BOOK: - addLinks (parentPaths, pathsOut, afADD_BOOKS | afOB_XRP); - break; + case nt_XRP_BOOK: + addLinks(parentPaths, pathsOut, afADD_BOOKS | afOB_XRP); + break; - case nt_DEST_BOOK: - addLinks (parentPaths, pathsOut, afADD_BOOKS | afOB_LAST); - break; + case nt_DEST_BOOK: + addLinks(parentPaths, pathsOut, afADD_BOOKS | afOB_LAST); + break; - case nt_DESTINATION: - // FIXME: What if a different issuer was specified on the - // destination amount? - // TODO(tom): what does this even mean? Should it be a JIRA? - addLinks (parentPaths, pathsOut, afADD_ACCOUNTS | afAC_LAST); - break; + case nt_DESTINATION: + // FIXME: What if a different issuer was specified on the + // destination amount? + // TODO(tom): what does this even mean? Should it be a JIRA? + addLinks(parentPaths, pathsOut, afADD_ACCOUNTS | afAC_LAST); + break; } - if (mCompletePaths.size () != initialSize) + if (mCompletePaths.size() != initialSize) { - JLOG (j_.debug()) - << (mCompletePaths.size () - initialSize) - << " complete paths added"; + JLOG(j_.debug()) << (mCompletePaths.size() - initialSize) + << " complete paths added"; } - JLOG (j_.debug()) - << "getPaths> " << pathsOut.size () << " partial paths found"; + JLOG(j_.debug()) << "getPaths> " << pathsOut.size() + << " partial paths found"; return pathsOut; } -bool Pathfinder::isNoRipple ( +bool +Pathfinder::isNoRipple( AccountID const& fromAccount, AccountID const& toAccount, Currency const& currency) { - auto sleRipple = mLedger->read(keylet::line( - toAccount, fromAccount, currency)); + auto sleRipple = + mLedger->read(keylet::line(toAccount, fromAccount, currency)); - auto const flag ((toAccount > fromAccount) - ? lsfHighNoRipple : lsfLowNoRipple); + auto const flag( + (toAccount > fromAccount) ? lsfHighNoRipple : lsfLowNoRipple); - return sleRipple && (sleRipple->getFieldU32 (sfFlags) & flag); + return sleRipple && (sleRipple->getFieldU32(sfFlags) & flag); } // Does this path end on an account-to-account link whose last account has // set "no ripple" on the link? -bool Pathfinder::isNoRippleOut (STPath const& currentPath) +bool +Pathfinder::isNoRippleOut(STPath const& currentPath) { // Must have at least one link. - if (currentPath.empty ()) + if (currentPath.empty()) return false; // Last link must be an account. - STPathElement const& endElement = currentPath.back (); - if (!(endElement.getNodeType () & STPathElement::typeAccount)) + STPathElement const& endElement = currentPath.back(); + if (!(endElement.getNodeType() & STPathElement::typeAccount)) return false; // If there's only one item in the path, return true if that item specifies // no ripple on the output. A path with no ripple on its output can't be // followed by a link with no ripple on its input. - auto const& fromAccount = (currentPath.size () == 1) + auto const& fromAccount = (currentPath.size() == 1) ? mSrcAccount - : (currentPath.end () - 2)->getAccountID (); - auto const& toAccount = endElement.getAccountID (); - return isNoRipple (fromAccount, toAccount, endElement.getCurrency ()); + : (currentPath.end() - 2)->getAccountID(); + auto const& toAccount = endElement.getAccountID(); + return isNoRipple(fromAccount, toAccount, endElement.getCurrency()); } -void addUniquePath (STPathSet& pathSet, STPath const& path) +void +addUniquePath(STPathSet& pathSet, STPath const& path) { // TODO(tom): building an STPathSet this way is quadratic in the size // of the STPathSet! @@ -888,40 +899,39 @@ void addUniquePath (STPathSet& pathSet, STPath const& path) if (p == path) return; } - pathSet.push_back (path); + pathSet.push_back(path); } -void Pathfinder::addLink ( - const STPath& currentPath, // The path to build from - STPathSet& incompletePaths, // The set of partial paths we add to +void +Pathfinder::addLink( + const STPath& currentPath, // The path to build from + STPathSet& incompletePaths, // The set of partial paths we add to int addFlags) { - auto const& pathEnd = currentPath.empty() ? mSource : currentPath.back (); - auto const& uEndCurrency = pathEnd.getCurrency (); - auto const& uEndIssuer = pathEnd.getIssuerID (); - auto const& uEndAccount = pathEnd.getAccountID (); - bool const bOnXRP = uEndCurrency.isZero (); + auto const& pathEnd = currentPath.empty() ? mSource : currentPath.back(); + auto const& uEndCurrency = pathEnd.getCurrency(); + auto const& uEndIssuer = pathEnd.getIssuerID(); + auto const& uEndAccount = pathEnd.getAccountID(); + bool const bOnXRP = uEndCurrency.isZero(); // Does pathfinding really need to get this to // a gateway (the issuer of the destination amount) // rather than the ultimate destination? bool const hasEffectiveDestination = mEffectiveDst != mDstAccount; - JLOG (j_.trace()) << "addLink< flags=" - << addFlags << " onXRP=" << bOnXRP; - JLOG (j_.trace()) << currentPath.getJson (JsonOptions::none); + JLOG(j_.trace()) << "addLink< flags=" << addFlags << " onXRP=" << bOnXRP; + JLOG(j_.trace()) << currentPath.getJson(JsonOptions::none); if (addFlags & afADD_ACCOUNTS) { // add accounts if (bOnXRP) { - if (mDstAmount.native () && !currentPath.empty ()) - { // non-default path to XRP destination - JLOG (j_.trace()) - << "complete path found ax: " - << currentPath.getJson(JsonOptions::none); - addUniquePath (mCompletePaths, currentPath); + if (mDstAmount.native() && !currentPath.empty()) + { // non-default path to XRP destination + JLOG(j_.trace()) << "complete path found ax: " + << currentPath.getJson(JsonOptions::none); + addUniquePath(mCompletePaths, currentPath); } } else @@ -931,30 +941,27 @@ void Pathfinder::addLink ( if (sleEnd) { - bool const bRequireAuth ( - sleEnd->getFieldU32 (sfFlags) & lsfRequireAuth); - bool const bIsEndCurrency ( - uEndCurrency == mDstAmount.getCurrency ()); - bool const bIsNoRippleOut ( - isNoRippleOut (currentPath)); - bool const bDestOnly ( - addFlags & afAC_LAST); + bool const bRequireAuth( + sleEnd->getFieldU32(sfFlags) & lsfRequireAuth); + bool const bIsEndCurrency( + uEndCurrency == mDstAmount.getCurrency()); + bool const bIsNoRippleOut(isNoRippleOut(currentPath)); + bool const bDestOnly(addFlags & afAC_LAST); - auto& rippleLines (mRLCache->getRippleLines (uEndAccount)); + auto& rippleLines(mRLCache->getRippleLines(uEndAccount)); AccountCandidates candidates; - candidates.reserve (rippleLines.size ()); + candidates.reserve(rippleLines.size()); for (auto const& item : rippleLines) { - auto* rs = dynamic_cast (item.get ()); + auto* rs = dynamic_cast(item.get()); if (!rs) { - JLOG (j_.error()) - << "Couldn't decipher RippleState"; + JLOG(j_.error()) << "Couldn't decipher RippleState"; continue; } - auto const& acct = rs->getAccountIDPeer (); + auto const& acct = rs->getAccountIDPeer(); if (hasEffectiveDestination && (acct == mDstAccount)) { @@ -969,40 +976,40 @@ void Pathfinder::addLink ( continue; } - if ((uEndCurrency == rs->getLimit ().getCurrency ()) && - !currentPath.hasSeen (acct, uEndCurrency, acct)) + if ((uEndCurrency == rs->getLimit().getCurrency()) && + !currentPath.hasSeen(acct, uEndCurrency, acct)) { // path is for correct currency and has not been seen - if (rs->getBalance () <= beast::zero - && (!rs->getLimitPeer () - || -rs->getBalance () >= rs->getLimitPeer () - || (bRequireAuth && !rs->getAuth ()))) + if (rs->getBalance() <= beast::zero && + (!rs->getLimitPeer() || + -rs->getBalance() >= rs->getLimitPeer() || + (bRequireAuth && !rs->getAuth()))) { // path has no credit } - else if (bIsNoRippleOut && rs->getNoRipple ()) + else if (bIsNoRippleOut && rs->getNoRipple()) { // Can't leave on this path } else if (bToDestination) { // destination is always worth trying - if (uEndCurrency == mDstAmount.getCurrency ()) + if (uEndCurrency == mDstAmount.getCurrency()) { // this is a complete path - if (!currentPath.empty ()) + if (!currentPath.empty()) { - JLOG (j_.trace()) - << "complete path found ae: " - << currentPath.getJson (JsonOptions::none); - addUniquePath - (mCompletePaths, currentPath); + JLOG(j_.trace()) + << "complete path found ae: " + << currentPath.getJson( + JsonOptions::none); + addUniquePath(mCompletePaths, currentPath); } } else if (!bDestOnly) { // this is a high-priority candidate - candidates.push_back ( + candidates.push_back( {AccountCandidate::highPriority, acct}); } } @@ -1013,26 +1020,29 @@ void Pathfinder::addLink ( else { // save this candidate - int out = getPathsOut ( + int out = getPathsOut( uEndCurrency, acct, bIsEndCurrency, mEffectiveDst); if (out) - candidates.push_back ({out, acct}); + candidates.push_back({out, acct}); } } } if (!candidates.empty()) { - std::sort (candidates.begin (), candidates.end (), - std::bind(compareAccountCandidate, - mLedger->seq(), - std::placeholders::_1, - std::placeholders::_2)); + std::sort( + candidates.begin(), + candidates.end(), + std::bind( + compareAccountCandidate, + mLedger->seq(), + std::placeholders::_1, + std::placeholders::_2)); - int count = candidates.size (); + int count = candidates.size(); // allow more paths from source if ((count > 10) && (uEndAccount != mSrcAccount)) count = 10; @@ -1043,21 +1053,19 @@ void Pathfinder::addLink ( while (count-- != 0) { // Add accounts to incompletePaths - STPathElement pathElement ( + STPathElement pathElement( STPathElement::typeAccount, it->account, uEndCurrency, it->account); - incompletePaths.assembleAdd (currentPath, pathElement); + incompletePaths.assembleAdd(currentPath, pathElement); ++it; } } - } else { - JLOG (j_.warn()) - << "Path ends on non-existent issuer"; + JLOG(j_.warn()) << "Path ends on non-existent issuer"; } } } @@ -1067,82 +1075,86 @@ void Pathfinder::addLink ( if (addFlags & afOB_XRP) { // to XRP only - if (!bOnXRP && app_.getOrderBookDB ().isBookToXRP ( - {uEndCurrency, uEndIssuer})) + if (!bOnXRP && + app_.getOrderBookDB().isBookToXRP({uEndCurrency, uEndIssuer})) { STPathElement pathElement( STPathElement::typeCurrency, - xrpAccount (), - xrpCurrency (), - xrpAccount ()); - incompletePaths.assembleAdd (currentPath, pathElement); + xrpAccount(), + xrpCurrency(), + xrpAccount()); + incompletePaths.assembleAdd(currentPath, pathElement); } } else { bool bDestOnly = (addFlags & afOB_LAST) != 0; - auto books = app_.getOrderBookDB ().getBooksByTakerPays( + auto books = app_.getOrderBookDB().getBooksByTakerPays( {uEndCurrency, uEndIssuer}); - JLOG (j_.trace()) - << books.size () << " books found from this currency/issuer"; + JLOG(j_.trace()) + << books.size() << " books found from this currency/issuer"; for (auto const& book : books) { - if (!currentPath.hasSeen ( + if (!currentPath.hasSeen( xrpAccount(), - book->getCurrencyOut (), - book->getIssuerOut ()) && - !issueMatchesOrigin (book->book ().out) && + book->getCurrencyOut(), + book->getIssuerOut()) && + !issueMatchesOrigin(book->book().out) && (!bDestOnly || - (book->getCurrencyOut () == mDstAmount.getCurrency ()))) + (book->getCurrencyOut() == mDstAmount.getCurrency()))) { - STPath newPath (currentPath); + STPath newPath(currentPath); if (book->getCurrencyOut().isZero()) - { // to XRP + { // to XRP // add the order book itself - newPath.emplace_back ( + newPath.emplace_back( STPathElement::typeCurrency, - xrpAccount (), - xrpCurrency (), - xrpAccount ()); + xrpAccount(), + xrpCurrency(), + xrpAccount()); - if (mDstAmount.getCurrency ().isZero ()) + if (mDstAmount.getCurrency().isZero()) { // destination is XRP, add account and path is // complete - JLOG (j_.trace()) + JLOG(j_.trace()) << "complete path found bx: " << currentPath.getJson(JsonOptions::none); - addUniquePath (mCompletePaths, newPath); + addUniquePath(mCompletePaths, newPath); } else - incompletePaths.push_back (newPath); + incompletePaths.push_back(newPath); } else if (!currentPath.hasSeen( - book->getIssuerOut (), - book->getCurrencyOut (), - book->getIssuerOut ())) + book->getIssuerOut(), + book->getCurrencyOut(), + book->getIssuerOut())) { // Don't want the book if we've already seen the issuer // book -> account -> book if ((newPath.size() >= 2) && - (newPath.back().isAccount ()) && - (newPath[newPath.size() - 2].isOffer ())) + (newPath.back().isAccount()) && + (newPath[newPath.size() - 2].isOffer())) { // replace the redundant account with the order book - newPath[newPath.size() - 1] = STPathElement ( - STPathElement::typeCurrency | STPathElement::typeIssuer, - xrpAccount(), book->getCurrencyOut(), + newPath[newPath.size() - 1] = STPathElement( + STPathElement::typeCurrency | + STPathElement::typeIssuer, + xrpAccount(), + book->getCurrencyOut(), book->getIssuerOut()); } else { // add the order book newPath.emplace_back( - STPathElement::typeCurrency | STPathElement::typeIssuer, - xrpAccount(), book->getCurrencyOut(), + STPathElement::typeCurrency | + STPathElement::typeIssuer, + xrpAccount(), + book->getCurrencyOut(), book->getIssuerOut()); } @@ -1152,25 +1164,28 @@ void Pathfinder::addLink ( { // We skipped a required issuer } - else if (book->getIssuerOut() == mEffectiveDst && + else if ( + book->getIssuerOut() == mEffectiveDst && book->getCurrencyOut() == mDstAmount.getCurrency()) - { // with the destination account, this path is complete - JLOG (j_.trace()) + { // with the destination account, this path is + // complete + JLOG(j_.trace()) << "complete path found ba: " << currentPath.getJson(JsonOptions::none); - addUniquePath (mCompletePaths, newPath); + addUniquePath(mCompletePaths, newPath); } else { // add issuer's account, path still incomplete - incompletePaths.assembleAdd(newPath, - STPathElement (STPathElement::typeAccount, - book->getIssuerOut (), - book->getCurrencyOut (), - book->getIssuerOut ())); + incompletePaths.assembleAdd( + newPath, + STPathElement( + STPathElement::typeAccount, + book->getIssuerOut(), + book->getCurrencyOut(), + book->getIssuerOut())); } } - } } } @@ -1179,7 +1194,8 @@ void Pathfinder::addLink ( namespace { -Pathfinder::PathType makePath (char const *string) +Pathfinder::PathType +makePath(char const* string) { Pathfinder::PathType ret; @@ -1187,30 +1203,30 @@ Pathfinder::PathType makePath (char const *string) { switch (*string++) { - case 's': // source - ret.push_back (Pathfinder::nt_SOURCE); + case 's': // source + ret.push_back(Pathfinder::nt_SOURCE); break; - case 'a': // accounts - ret.push_back (Pathfinder::nt_ACCOUNTS); + case 'a': // accounts + ret.push_back(Pathfinder::nt_ACCOUNTS); break; - case 'b': // books - ret.push_back (Pathfinder::nt_BOOKS); + case 'b': // books + ret.push_back(Pathfinder::nt_BOOKS); break; - case 'x': // xrp book - ret.push_back (Pathfinder::nt_XRP_BOOK); + case 'x': // xrp book + ret.push_back(Pathfinder::nt_XRP_BOOK); break; - case 'f': // book to final currency - ret.push_back (Pathfinder::nt_DEST_BOOK); + case 'f': // book to final currency + ret.push_back(Pathfinder::nt_DEST_BOOK); break; case 'd': // Destination (with account, if required and not already // present). - ret.push_back (Pathfinder::nt_DESTINATION); + ret.push_back(Pathfinder::nt_DESTINATION); break; case 0: @@ -1219,16 +1235,16 @@ Pathfinder::PathType makePath (char const *string) } } -void fillPaths (Pathfinder::PaymentType type, PathCostList const& costs) +void +fillPaths(Pathfinder::PaymentType type, PathCostList const& costs) { auto& list = mPathTable[type]; - assert (list.empty()); - for (auto& cost: costs) - list.push_back ({cost.cost, makePath (cost.path)}); + assert(list.empty()); + for (auto& cost : costs) + list.push_back({cost.cost, makePath(cost.path)}); } -} // namespace - +} // namespace // Costs: // 0 = minimum to make some payments possible @@ -1237,55 +1253,56 @@ void fillPaths (Pathfinder::PaymentType type, PathCostList const& costs) // 7 = normal slow search level // 10 = most agressive -void Pathfinder::initPathTable () +void +Pathfinder::initPathTable() { // CAUTION: Do not include rules that build default paths mPathTable.clear(); - fillPaths (pt_XRP_to_XRP, {}); + fillPaths(pt_XRP_to_XRP, {}); fillPaths( - pt_XRP_to_nonXRP, { - {1, "sfd"}, // source -> book -> gateway - {3, "sfad"}, // source -> book -> account -> destination - {5, "sfaad"}, // source -> book -> account -> account -> destination - {6, "sbfd"}, // source -> book -> book -> destination - {8, "sbafd"}, // source -> book -> account -> book -> destination - {9, "sbfad"}, // source -> book -> book -> account -> destination - {10, "sbafad"} - }); + pt_XRP_to_nonXRP, + {{1, "sfd"}, // source -> book -> gateway + {3, "sfad"}, // source -> book -> account -> destination + {5, "sfaad"}, // source -> book -> account -> account -> destination + {6, "sbfd"}, // source -> book -> book -> destination + {8, "sbafd"}, // source -> book -> account -> book -> destination + {9, "sbfad"}, // source -> book -> book -> account -> destination + {10, "sbafad"}}); fillPaths( - pt_nonXRP_to_XRP, { - {1, "sxd"}, // gateway buys XRP - {2, "saxd"}, // source -> gateway -> book(XRP) -> dest - {6, "saaxd"}, - {7, "sbxd"}, - {8, "sabxd"}, - {9, "sabaxd"} - }); + pt_nonXRP_to_XRP, + {{1, "sxd"}, // gateway buys XRP + {2, "saxd"}, // source -> gateway -> book(XRP) -> dest + {6, "saaxd"}, + {7, "sbxd"}, + {8, "sabxd"}, + {9, "sabaxd"}}); // non-XRP to non-XRP (same currency) fillPaths( - pt_nonXRP_to_same, { - {1, "sad"}, // source -> gateway -> destination - {1, "sfd"}, // source -> book -> destination - {4, "safd"}, // source -> gateway -> book -> destination + pt_nonXRP_to_same, + { + {1, "sad"}, // source -> gateway -> destination + {1, "sfd"}, // source -> book -> destination + {4, "safd"}, // source -> gateway -> book -> destination {4, "sfad"}, {5, "saad"}, {5, "sbfd"}, {6, "sxfad"}, {6, "safad"}, - {6, "saxfd"}, // source -> gateway -> book to XRP -> book -> - // destination + {6, "saxfd"}, // source -> gateway -> book to XRP -> book -> + // destination {6, "saxfad"}, - {6, "sabfd"}, // source -> gateway -> book -> book -> destination + {6, "sabfd"}, // source -> gateway -> book -> book -> destination {7, "saaad"}, }); // non-XRP to non-XRP (different currency) fillPaths( - pt_nonXRP_to_nonXRP, { + pt_nonXRP_to_nonXRP, + { {1, "sfad"}, {1, "safd"}, {3, "safad"}, @@ -1301,4 +1318,4 @@ void Pathfinder::initPathTable () }); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/Pathfinder.h b/src/ripple/app/paths/Pathfinder.h index 6fdee019b3..d34a6fa388 100644 --- a/src/ripple/app/paths/Pathfinder.h +++ b/src/ripple/app/paths/Pathfinder.h @@ -38,7 +38,7 @@ class Pathfinder { public: /** Construct a pathfinder without an issuer.*/ - Pathfinder ( + Pathfinder( std::shared_ptr const& cache, AccountID const& srcAccount, AccountID const& dstAccount, @@ -47,16 +47,20 @@ public: STAmount const& dstAmount, boost::optional const& srcAmount, Application& app); - Pathfinder (Pathfinder const&) = delete; - Pathfinder& operator= (Pathfinder const&) = delete; + Pathfinder(Pathfinder const&) = delete; + Pathfinder& + operator=(Pathfinder const&) = delete; ~Pathfinder() = default; - static void initPathTable (); + static void + initPathTable(); - bool findPaths (int searchLevel); + bool + findPaths(int searchLevel); /** Compute the rankings of the paths. */ - void computePathRanks (int maxPaths); + void + computePathRanks(int maxPaths); /* Get the best paths, up to maxPaths in number, from mCompletePaths. @@ -64,29 +68,27 @@ public: additional single path which can consume all the liquidity. */ STPathSet - getBestPaths ( + getBestPaths( int maxPaths, STPath& fullLiquidityPath, STPathSet const& extraPaths, AccountID const& srcIssuer); - enum NodeType - { + enum NodeType { nt_SOURCE, // The source account: with an issuer account, if needed. nt_ACCOUNTS, // Accounts that connect from this source/currency. nt_BOOKS, // Order books that connect to this currency. nt_XRP_BOOK, // The order book from this currency to XRP. nt_DEST_BOOK, // The order book to the destination currency/issuer. - nt_DESTINATION // The destination account only. + nt_DESTINATION // The destination account only. }; // The PathType is a list of the NodeTypes for a path. - using PathType = std::vector ; + using PathType = std::vector; // PaymentType represents the types of the source and destination currencies // in a path request. - enum PaymentType - { + enum PaymentType { pt_XRP_to_XRP, pt_XRP_to_nonXRP, pt_nonXRP_to_XRP, @@ -123,32 +125,37 @@ private: getBestPaths */ - // Add all paths of one type to mCompletePaths. - STPathSet& addPathsForType (PathType const& type); + STPathSet& + addPathsForType(PathType const& type); - bool issueMatchesOrigin (Issue const&); + bool + issueMatchesOrigin(Issue const&); - int getPathsOut ( + int + getPathsOut( Currency const& currency, AccountID const& account, bool isDestCurrency, AccountID const& dest); - void addLink ( + void + addLink( STPath const& currentPath, STPathSet& incompletePaths, int addFlags); // Call addLink() for each path in currentPaths. - void addLinks ( + void + addLinks( STPathSet const& currentPaths, STPathSet& incompletePaths, int addFlags); // Compute the liquidity for a path. Return tesSUCCESS if it has has enough // liquidity to be worth keeping, otherwise an error. - TER getPathLiquidity ( + TER + getPathLiquidity( STPath const& path, // IN: The path to check. STAmount const& minDstAmount, // IN: The minimum output this path must // deliver to be worth keeping. @@ -157,22 +164,25 @@ private: // Does this path end on an account-to-account link whose last account has // set the "no ripple" flag on the link? - bool isNoRippleOut (STPath const& currentPath); + bool + isNoRippleOut(STPath const& currentPath); // Is the "no ripple" flag set from one account to another? - bool isNoRipple ( + bool + isNoRipple( AccountID const& fromAccount, AccountID const& toAccount, Currency const& currency); - void rankPaths ( + void + rankPaths( int maxPaths, STPathSet const& paths, - std::vector & rankedPaths); + std::vector& rankedPaths); AccountID mSrcAccount; AccountID mDstAccount; - AccountID mEffectiveDst; // The account the paths need to end at + AccountID mEffectiveDst; // The account the paths need to end at STAmount mDstAmount; Currency mSrcCurrency; boost::optional mSrcIssuer; @@ -182,7 +192,7 @@ private: STAmount mRemainingAmount; bool convert_all_; - std::shared_ptr mLedger; + std::shared_ptr mLedger; std::unique_ptr m_loadEvent; std::shared_ptr mRLCache; @@ -212,6 +222,6 @@ private: static std::uint32_t const afAC_LAST = 0x080; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/RippleLineCache.cpp b/src/ripple/app/paths/RippleLineCache.cpp index c9f088e5a7..6bb710f4da 100644 --- a/src/ripple/app/paths/RippleLineCache.cpp +++ b/src/ripple/app/paths/RippleLineCache.cpp @@ -22,8 +22,7 @@ namespace ripple { -RippleLineCache::RippleLineCache( - std::shared_ptr const& ledger) +RippleLineCache::RippleLineCache(std::shared_ptr const& ledger) { // We want the caching that OpenView provides // And we need to own a shared_ptr to the input view @@ -32,20 +31,19 @@ RippleLineCache::RippleLineCache( } std::vector const& -RippleLineCache::getRippleLines (AccountID const& accountID) +RippleLineCache::getRippleLines(AccountID const& accountID) { - AccountKey key (accountID, hasher_ (accountID)); + AccountKey key(accountID, hasher_(accountID)); - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - auto [it, inserted] = lines_.emplace (key, - std::vector()); + auto [it, inserted] = + lines_.emplace(key, std::vector()); if (inserted) - it->second = getRippleStateItems ( - accountID, *mLedger); + it->second = getRippleStateItems(accountID, *mLedger); return it->second; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/RippleLineCache.h b/src/ripple/app/paths/RippleLineCache.h index 01fd1aa2bc..b73e7afade 100644 --- a/src/ripple/app/paths/RippleLineCache.h +++ b/src/ripple/app/paths/RippleLineCache.h @@ -34,47 +34,46 @@ namespace ripple { class RippleLineCache { public: - explicit - RippleLineCache ( - std::shared_ptr const& l); + explicit RippleLineCache(std::shared_ptr const& l); - std::shared_ptr const& - getLedger () const + std::shared_ptr const& + getLedger() const { return mLedger; } std::vector const& - getRippleLines (AccountID const& accountID); + getRippleLines(AccountID const& accountID); private: std::mutex mLock; ripple::hardened_hash<> hasher_; - std::shared_ptr mLedger; + std::shared_ptr mLedger; struct AccountKey { AccountID account_; std::size_t hash_value_; - AccountKey (AccountID const& account, std::size_t hash) - : account_ (account) - , hash_value_ (hash) - { } + AccountKey(AccountID const& account, std::size_t hash) + : account_(account), hash_value_(hash) + { + } - AccountKey (AccountKey const& other) = default; + AccountKey(AccountKey const& other) = default; AccountKey& operator=(AccountKey const& other) = default; - bool operator== (AccountKey const& lhs) const + bool + operator==(AccountKey const& lhs) const { return hash_value_ == lhs.hash_value_ && account_ == lhs.account_; } std::size_t - get_hash () const + get_hash() const { return hash_value_; } @@ -84,19 +83,17 @@ private: explicit Hash() = default; std::size_t - operator () (AccountKey const& key) const noexcept + operator()(AccountKey const& key) const noexcept { - return key.get_hash (); + return key.get_hash(); } }; }; - hash_map < - AccountKey, - std::vector , - AccountKey::Hash> lines_; + hash_map, AccountKey::Hash> + lines_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/RippleState.cpp b/src/ripple/app/paths/RippleState.cpp index 416c6ea98c..5813f47ea5 100644 --- a/src/ripple/app/paths/RippleState.cpp +++ b/src/ripple/app/paths/RippleState.cpp @@ -26,61 +26,60 @@ namespace ripple { RippleState::pointer -RippleState::makeItem ( +RippleState::makeItem( AccountID const& accountID, - std::shared_ptr sle) + std::shared_ptr sle) { // VFALCO Does this ever happen in practice? - if (! sle || sle->getType () != ltRIPPLE_STATE) + if (!sle || sle->getType() != ltRIPPLE_STATE) return {}; - return std::make_shared( - std::move(sle), accountID); + return std::make_shared(std::move(sle), accountID); } -RippleState::RippleState ( +RippleState::RippleState( std::shared_ptr&& sle, - AccountID const& viewAccount) - : sle_ (std::move(sle)) - , mFlags (sle_->getFieldU32 (sfFlags)) - , mLowLimit (sle_->getFieldAmount (sfLowLimit)) - , mHighLimit (sle_->getFieldAmount (sfHighLimit)) - , mLowID (mLowLimit.getIssuer ()) - , mHighID (mHighLimit.getIssuer ()) - , lowQualityIn_ (sle_->getFieldU32 (sfLowQualityIn)) - , lowQualityOut_ (sle_->getFieldU32 (sfLowQualityOut)) - , highQualityIn_ (sle_->getFieldU32 (sfHighQualityIn)) - , highQualityOut_ (sle_->getFieldU32 (sfHighQualityOut)) - , mBalance (sle_->getFieldAmount (sfBalance)) + AccountID const& viewAccount) + : sle_(std::move(sle)) + , mFlags(sle_->getFieldU32(sfFlags)) + , mLowLimit(sle_->getFieldAmount(sfLowLimit)) + , mHighLimit(sle_->getFieldAmount(sfHighLimit)) + , mLowID(mLowLimit.getIssuer()) + , mHighID(mHighLimit.getIssuer()) + , lowQualityIn_(sle_->getFieldU32(sfLowQualityIn)) + , lowQualityOut_(sle_->getFieldU32(sfLowQualityOut)) + , highQualityIn_(sle_->getFieldU32(sfHighQualityIn)) + , highQualityOut_(sle_->getFieldU32(sfHighQualityOut)) + , mBalance(sle_->getFieldAmount(sfBalance)) { mViewLowest = (mLowID == viewAccount); if (!mViewLowest) - mBalance.negate (); + mBalance.negate(); } -Json::Value RippleState::getJson (int) +Json::Value +RippleState::getJson(int) { - Json::Value ret (Json::objectValue); - ret["low_id"] = to_string (mLowID); - ret["high_id"] = to_string (mHighID); + Json::Value ret(Json::objectValue); + ret["low_id"] = to_string(mLowID); + ret["high_id"] = to_string(mHighID); return ret; } -std::vector -getRippleStateItems (AccountID const& accountID, - ReadView const& view) +std::vector +getRippleStateItems(AccountID const& accountID, ReadView const& view) { - std::vector items; - forEachItem(view, accountID, - [&items,&accountID]( - std::shared_ptr const&sleCur) - { - auto ret = RippleState::makeItem (accountID, sleCur); - if (ret) - items.push_back (ret); + std::vector items; + forEachItem( + view, + accountID, + [&items, &accountID](std::shared_ptr const& sleCur) { + auto ret = RippleState::makeItem(accountID, sleCur); + if (ret) + items.push_back(ret); }); return items; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/RippleState.h b/src/ripple/app/paths/RippleState.h index 9ade7e48af..96b4fd2546 100644 --- a/src/ripple/app/paths/RippleState.h +++ b/src/ripple/app/paths/RippleState.h @@ -25,7 +25,7 @@ #include #include #include -#include // +#include // namespace ripple { @@ -40,20 +40,18 @@ class RippleState { public: // VFALCO Why is this shared_ptr? - using pointer = std::shared_ptr ; + using pointer = std::shared_ptr; public: - RippleState () = delete; + RippleState() = delete; virtual ~RippleState() = default; - static RippleState::pointer makeItem( - AccountID const& accountID, - std::shared_ptr sle); + static RippleState::pointer + makeItem(AccountID const& accountID, std::shared_ptr sle); // Must be public, for make_shared - RippleState (std::shared_ptr&& sle, - AccountID const& viewAccount); + RippleState(std::shared_ptr&& sle, AccountID const& viewAccount); /** Returns the state map key for the ledger entry. */ uint256 @@ -64,103 +62,114 @@ public: // VFALCO Take off the "get" from each function name - AccountID const& getAccountID () const + AccountID const& + getAccountID() const { - return mViewLowest ? mLowID : mHighID; + return mViewLowest ? mLowID : mHighID; } - AccountID const& getAccountIDPeer () const + AccountID const& + getAccountIDPeer() const { return !mViewLowest ? mLowID : mHighID; } // True, Provided auth to peer. - bool getAuth () const + bool + getAuth() const { return mFlags & (mViewLowest ? lsfLowAuth : lsfHighAuth); } - bool getAuthPeer () const + bool + getAuthPeer() const { return mFlags & (!mViewLowest ? lsfLowAuth : lsfHighAuth); } - bool getNoRipple () const + bool + getNoRipple() const { return mFlags & (mViewLowest ? lsfLowNoRipple : lsfHighNoRipple); } - bool getNoRipplePeer () const + bool + getNoRipplePeer() const { return mFlags & (!mViewLowest ? lsfLowNoRipple : lsfHighNoRipple); } /** Have we set the freeze flag on our peer */ - bool getFreeze () const + bool + getFreeze() const { return mFlags & (mViewLowest ? lsfLowFreeze : lsfHighFreeze); } /** Has the peer set the freeze flag on us */ - bool getFreezePeer () const + bool + getFreezePeer() const { return mFlags & (!mViewLowest ? lsfLowFreeze : lsfHighFreeze); } - STAmount const& getBalance () const + STAmount const& + getBalance() const { return mBalance; } - STAmount const& getLimit () const + STAmount const& + getLimit() const { - return mViewLowest ? mLowLimit : mHighLimit; + return mViewLowest ? mLowLimit : mHighLimit; } - STAmount const& getLimitPeer () const + STAmount const& + getLimitPeer() const { return !mViewLowest ? mLowLimit : mHighLimit; } Rate const& - getQualityIn () const + getQualityIn() const { return mViewLowest ? lowQualityIn_ : highQualityIn_; } Rate const& - getQualityOut () const + getQualityOut() const { return mViewLowest ? lowQualityOut_ : highQualityOut_; } - Json::Value getJson (int); + Json::Value + getJson(int); private: std::shared_ptr sle_; - bool mViewLowest; + bool mViewLowest; - std::uint32_t mFlags; + std::uint32_t mFlags; - STAmount const& mLowLimit; - STAmount const& mHighLimit; + STAmount const& mLowLimit; + STAmount const& mHighLimit; - AccountID const& mLowID; - AccountID const& mHighID; + AccountID const& mLowID; + AccountID const& mHighID; Rate lowQualityIn_; Rate lowQualityOut_; Rate highQualityIn_; Rate highQualityOut_; - STAmount mBalance; + STAmount mBalance; }; -std::vector -getRippleStateItems (AccountID const& accountID, - ReadView const& view); +std::vector +getRippleStateItems(AccountID const& accountID, ReadView const& view); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/Tuning.h b/src/ripple/app/paths/Tuning.h index 26cce973d7..b07d3b16fd 100644 --- a/src/ripple/app/paths/Tuning.h +++ b/src/ripple/app/paths/Tuning.h @@ -31,6 +31,6 @@ int const PATHFINDER_MAX_PATHS = 50; int const PATHFINDER_MAX_COMPLETE_PATHS = 1000; int const PATHFINDER_MAX_PATHS_FROM_SOURCE = 10; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/Types.h b/src/ripple/app/paths/Types.h index f0ad869d86..ce1055e29d 100644 --- a/src/ripple/app/paths/Types.h +++ b/src/ripple/app/paths/Types.h @@ -23,7 +23,7 @@ namespace ripple { // account id, issue. -using AccountIssue = std::pair ; +using AccountIssue = std::pair; // Map of account, issue to node index. namespace path { @@ -32,8 +32,8 @@ using NodeIndex = unsigned int; } -using AccountIssueToNodeIndex = hash_map ; +using AccountIssueToNodeIndex = hash_map; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/AmountSpec.h b/src/ripple/app/paths/impl/AmountSpec.h index 315b4cde78..c1adfc15d4 100644 --- a/src/ripple/app/paths/impl/AmountSpec.h +++ b/src/ripple/app/paths/impl/AmountSpec.h @@ -39,16 +39,13 @@ struct AmountSpec boost::optional issuer; boost::optional currency; - friend - std::ostream& - operator << ( - std::ostream& stream, - AmountSpec const& amt) + friend std::ostream& + operator<<(std::ostream& stream, AmountSpec const& amt) { if (amt.native) - stream << to_string (amt.xrp); + stream << to_string(amt.xrp); else - stream << to_string (amt.iou); + stream << to_string(amt.iou); if (amt.currency) stream << "/(" << *amt.currency << ")"; if (amt.issuer) @@ -69,11 +66,9 @@ struct EitherAmount XRPAmount xrp; }; - EitherAmount () = default; + EitherAmount() = default; - explicit - EitherAmount (IOUAmount const& a) - :iou(a) + explicit EitherAmount(IOUAmount const& a) : iou(a) { } @@ -82,9 +77,7 @@ struct EitherAmount // ignore warning about half of iou amount being uninitialized #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" #endif - explicit - EitherAmount (XRPAmount const& a) - :xrp(a) + explicit EitherAmount(XRPAmount const& a) : xrp(a) { #ifndef NDEBUG native = true; @@ -94,8 +87,7 @@ struct EitherAmount #pragma GCC diagnostic pop #endif - explicit - EitherAmount (AmountSpec const& a) + explicit EitherAmount(AmountSpec const& a) { #ifndef NDEBUG native = a.native; @@ -121,74 +113,69 @@ struct EitherAmount template T& -get (EitherAmount& amt) +get(EitherAmount& amt) { static_assert(sizeof(T) == -1, "Must used specialized function"); return T(0); } template <> -inline -IOUAmount& -get (EitherAmount& amt) +inline IOUAmount& +get(EitherAmount& amt) { - assert (!amt.native); + assert(!amt.native); return amt.iou; } template <> -inline -XRPAmount& -get (EitherAmount& amt) +inline XRPAmount& +get(EitherAmount& amt) { - assert (amt.native); + assert(amt.native); return amt.xrp; } template T const& -get (EitherAmount const& amt) +get(EitherAmount const& amt) { static_assert(sizeof(T) == -1, "Must used specialized function"); return T(0); } template <> -inline -IOUAmount const& -get (EitherAmount const& amt) +inline IOUAmount const& +get(EitherAmount const& amt) { - assert (!amt.native); + assert(!amt.native); return amt.iou; } template <> -inline -XRPAmount const& -get (EitherAmount const& amt) +inline XRPAmount const& +get(EitherAmount const& amt) { - assert (amt.native); + assert(amt.native); return amt.xrp; } -inline -AmountSpec -toAmountSpec (STAmount const& amt) +inline AmountSpec +toAmountSpec(STAmount const& amt) { - assert (amt.mantissa () < std::numeric_limits::max ()); - bool const isNeg = amt.negative (); + assert(amt.mantissa() < std::numeric_limits::max()); + bool const isNeg = amt.negative(); std::int64_t const sMant = - isNeg ? - std::int64_t (amt.mantissa ()) : amt.mantissa (); + isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa(); AmountSpec result; - result.native = isXRP (amt); + result.native = isXRP(amt); if (result.native) { - result.xrp = XRPAmount (sMant); + result.xrp = XRPAmount(sMant); } else { - result.iou = IOUAmount (sMant, amt.exponent ()); + result.iou = IOUAmount(sMant, amt.exponent()); result.issuer = amt.issue().account; result.currency = amt.issue().currency; } @@ -196,25 +183,21 @@ toAmountSpec (STAmount const& amt) return result; } -inline -EitherAmount -toEitherAmount (STAmount const& amt) +inline EitherAmount +toEitherAmount(STAmount const& amt) { - if (isXRP (amt)) + if (isXRP(amt)) return EitherAmount{amt.xrp()}; return EitherAmount{amt.iou()}; } -inline -AmountSpec -toAmountSpec ( - EitherAmount const& ea, - boost::optional const& c) +inline AmountSpec +toAmountSpec(EitherAmount const& ea, boost::optional const& c) { AmountSpec r; - r.native = (!c || isXRP (*c)); + r.native = (!c || isXRP(*c)); r.currency = c; - assert (ea.native == r.native); + assert(ea.native == r.native); if (r.native) { r.xrp = ea.xrp; @@ -226,6 +209,6 @@ toAmountSpec ( return r; } -} +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/BookStep.cpp b/src/ripple/app/paths/impl/BookStep.cpp index d0bbca0e66..ee8e8edcdc 100644 --- a/src/ripple/app/paths/impl/BookStep.cpp +++ b/src/ripple/app/paths/impl/BookStep.cpp @@ -17,15 +17,15 @@ */ //============================================================================== -#include -#include #include #include +#include +#include #include -#include #include #include #include +#include #include #include #include @@ -39,7 +39,7 @@ namespace ripple { -template +template class BookStep : public StepImp> { protected: @@ -59,16 +59,15 @@ protected: TIn in; TOut out; - Cache (TIn const& in_, TOut const& out_) - : in (in_), out (out_) + Cache(TIn const& in_, TOut const& out_) : in(in_), out(out_) { } }; boost::optional cache_; - static - uint32_t getMaxOffersToConsume(StrandContext const& ctx) + static uint32_t + getMaxOffersToConsume(StrandContext const& ctx) { if (ctx.view.rules().enabled(fix1515)) return 1000; @@ -76,38 +75,37 @@ protected: } public: - BookStep (StrandContext const& ctx, - Issue const& in, - Issue const& out) - : maxOffersToConsume_ (getMaxOffersToConsume(ctx)) - , book_ (in, out) - , strandSrc_ (ctx.strandSrc) - , strandDst_ (ctx.strandDst) - , prevStep_ (ctx.prevStep) - , ownerPaysTransferFee_ (ctx.ownerPaysTransferFee) - , j_ (ctx.j) + BookStep(StrandContext const& ctx, Issue const& in, Issue const& out) + : maxOffersToConsume_(getMaxOffersToConsume(ctx)) + , book_(in, out) + , strandSrc_(ctx.strandSrc) + , strandDst_(ctx.strandDst) + , prevStep_(ctx.prevStep) + , ownerPaysTransferFee_(ctx.ownerPaysTransferFee) + , j_(ctx.j) { } - Book const& book() const + Book const& + book() const { return book_; } boost::optional - cachedIn () const override + cachedIn() const override { if (!cache_) return boost::none; - return EitherAmount (cache_->in); + return EitherAmount(cache_->in); } boost::optional - cachedOut () const override + cachedOut() const override { if (!cache_) return boost::none; - return EitherAmount (cache_->out); + return EitherAmount(cache_->out); } DebtDirection @@ -118,66 +116,71 @@ public: } boost::optional - bookStepBook () const override + bookStepBook() const override { return book_; } std::pair, DebtDirection> - qualityUpperBound(ReadView const& v, DebtDirection prevStepDir) const override; + qualityUpperBound(ReadView const& v, DebtDirection prevStepDir) + const override; std::pair - revImp ( + revImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, TOut const& out); std::pair - fwdImp ( + fwdImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, TIn const& in); std::pair - validFwd ( - PaymentSandbox& sb, - ApplyView& afView, - EitherAmount const& in) override; + validFwd(PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) + override; // Check for errors frozen constraints. - TER check(StrandContext const& ctx) const; + TER + check(StrandContext const& ctx) const; - bool inactive() const override { + bool + inactive() const override + { return inactive_; } protected: - std::string logStringImpl (char const* name) const + std::string + logStringImpl(char const* name) const { std::ostringstream ostr; - ostr << - name << ": " << - "\ninIss: " << book_.in.account << - "\noutIss: " << book_.out.account << - "\ninCur: " << book_.in.currency << - "\noutCur: " << book_.out.currency; - return ostr.str (); + ostr << name << ": " + << "\ninIss: " << book_.in.account + << "\noutIss: " << book_.out.account + << "\ninCur: " << book_.in.currency + << "\noutCur: " << book_.out.currency; + return ostr.str(); } private: - friend bool operator==(BookStep const& lhs, BookStep const& rhs) + friend bool + operator==(BookStep const& lhs, BookStep const& rhs) { return lhs.book_ == rhs.book_; } - friend bool operator!=(BookStep const& lhs, BookStep const& rhs) + friend bool + operator!=(BookStep const& lhs, BookStep const& rhs) { - return ! (lhs == rhs); + return !(lhs == rhs); } - bool equal (Step const& rhs) const override; + bool + equal(Step const& rhs) const override; // Iterate through the offers at the best quality in a book. // Unfunded offers and bad offers are skipped (and returned). @@ -186,13 +189,15 @@ private: // Return the unfunded and bad offers and the number of offers consumed. template std::pair, std::uint32_t> - forEachOffer ( + forEachOffer( PaymentSandbox& sb, ApplyView& afView, DebtDirection prevStepDebtDir, Callback& callback) const; - void consumeOffer (PaymentSandbox& sb, + void + consumeOffer( + PaymentSandbox& sb, TOffer& offer, TAmounts const& ofrAmt, TAmounts const& stepAmt, @@ -208,9 +213,8 @@ private: // quite, the same. // Payment BookStep template class (not offer crossing). -template -class BookPaymentStep - : public BookStep> +template +class BookPaymentStep : public BookStep> { public: explicit BookPaymentStep() = default; @@ -219,35 +223,47 @@ public: using BookStep>::qualityUpperBound; // Never limit self cross quality on a payment. - bool limitSelfCrossQuality (AccountID const&, AccountID const&, - TOffer const& offer, boost::optional&, - FlowOfferStream&, bool) const + bool + limitSelfCrossQuality( + AccountID const&, + AccountID const&, + TOffer const& offer, + boost::optional&, + FlowOfferStream&, + bool) const { return false; } // A payment can look at offers of any quality - bool checkQualityThreshold(TOffer const& offer) const + bool + checkQualityThreshold(TOffer const& offer) const { return true; } // For a payment ofrInRate is always the same as trIn. - std::uint32_t getOfrInRate ( - Step const*, TOffer const&, std::uint32_t trIn) const + std::uint32_t + getOfrInRate(Step const*, TOffer const&, std::uint32_t trIn) + const { return trIn; } // For a payment ofrOutRate is always the same as trOut. - std::uint32_t getOfrOutRate (Step const*, TOffer const&, - AccountID const&, std::uint32_t trOut) const + std::uint32_t + getOfrOutRate( + Step const*, + TOffer const&, + AccountID const&, + std::uint32_t trOut) const { return trOut; } Quality - adjustQualityWithFees(ReadView const& v, + adjustQualityWithFees( + ReadView const& v, Quality const& ofrQ, DebtDirection prevStepDir) const { @@ -265,8 +281,7 @@ public: auto const trIn = redeems(prevStepDir) ? rate(this->book_.in.account) : parityRate; // Always charge the transfer fee, even if the owner is the issuer - auto const trOut = - this->ownerPaysTransferFee_ + auto const trOut = this->ownerPaysTransferFee_ ? rate(this->book_.out.account) : parityRate; @@ -274,42 +289,52 @@ public: return composed_quality(q1, ofrQ); } - std::string logString () const override + std::string + logString() const override { - return this->logStringImpl ("BookPaymentStep"); + return this->logStringImpl("BookPaymentStep"); } }; // Offer crossing BookStep template class (not a payment). -template +template class BookOfferCrossingStep : public BookStep> { - using BookStep>::qualityUpperBound; + using BookStep>:: + qualityUpperBound; + private: // Helper function that throws if the optional passed to the constructor // is none. - static Quality getQuality (boost::optional const& limitQuality) + static Quality + getQuality(boost::optional const& limitQuality) { // It's really a programming error if the quality is missing. - assert (limitQuality); + assert(limitQuality); if (!limitQuality) - Throw (tefINTERNAL, "Offer requires quality."); + Throw(tefINTERNAL, "Offer requires quality."); return *limitQuality; } public: - BookOfferCrossingStep ( - StrandContext const& ctx, Issue const& in, Issue const& out) - : BookStep> (ctx, in, out) - , defaultPath_ (ctx.isDefaultPath) - , qualityThreshold_ (getQuality (ctx.limitQuality)) + BookOfferCrossingStep( + StrandContext const& ctx, + Issue const& in, + Issue const& out) + : BookStep>(ctx, in, out) + , defaultPath_(ctx.isDefaultPath) + , qualityThreshold_(getQuality(ctx.limitQuality)) { } - bool limitSelfCrossQuality (AccountID const& strandSrc, - AccountID const& strandDst, TOffer const& offer, - boost::optional& ofrQ, FlowOfferStream& offers, + bool + limitSelfCrossQuality( + AccountID const& strandSrc, + AccountID const& strandDst, + TOffer const& offer, + boost::optional& ofrQ, + FlowOfferStream& offers, bool const offerAttempted) const { // This method supports some correct but slightly surprising @@ -345,7 +370,7 @@ public: strandSrc == offer.owner() && strandDst == offer.owner()) { // Remove this offer even if no crossing occurs. - offers.permRmOffer (offer.key()); + offers.permRmOffer(offer.key()); // If no offers have been attempted yet then it's okay to move to // a different quality. @@ -360,54 +385,64 @@ public: // Offer crossing can prune the offers it needs to look at with a // quality threshold. - bool checkQualityThreshold(TOffer const& offer) const + bool + checkQualityThreshold(TOffer const& offer) const { return !defaultPath_ || offer.quality() >= qualityThreshold_; } // For offer crossing don't pay the transfer fee if alice is paying alice. // A regular (non-offer-crossing) payment does not apply this rule. - std::uint32_t getOfrInRate (Step const* prevStep, - TOffer const& offer, std::uint32_t trIn) const + std::uint32_t + getOfrInRate( + Step const* prevStep, + TOffer const& offer, + std::uint32_t trIn) const { - auto const srcAcct = prevStep ? - prevStep->directStepSrcAcct() : - boost::none; + auto const srcAcct = + prevStep ? prevStep->directStepSrcAcct() : boost::none; - return // If offer crossing - srcAcct && // && prevStep is DirectI - offer.owner() == *srcAcct // && src is offer owner - ? QUALITY_ONE : trIn; // then rate = QUALITY_ONE + return // If offer crossing + srcAcct && // && prevStep is DirectI + offer.owner() == *srcAcct // && src is offer owner + ? QUALITY_ONE + : trIn; // then rate = QUALITY_ONE } // See comment on getOfrInRate(). - std::uint32_t getOfrOutRate ( - Step const* prevStep, TOffer const& offer, - AccountID const& strandDst, std::uint32_t trOut) const + std::uint32_t + getOfrOutRate( + Step const* prevStep, + TOffer const& offer, + AccountID const& strandDst, + std::uint32_t trOut) const { - return // If offer crossing - prevStep && prevStep->bookStepBook() && // && prevStep is BookStep - offer.owner() == strandDst // && dest is offer owner - ? QUALITY_ONE : trOut; // then rate = QUALITY_ONE + return // If offer crossing + prevStep && prevStep->bookStepBook() && // && prevStep is BookStep + offer.owner() == strandDst // && dest is offer owner + ? QUALITY_ONE + : trOut; // then rate = QUALITY_ONE } Quality - adjustQualityWithFees(ReadView const& v, + adjustQualityWithFees( + ReadView const& v, Quality const& ofrQ, DebtDirection prevStepDir) const { // Offer x-ing does not charge a transfer fee when the offer's owner - // is the same as the strand dst. It is important that `qualityUpperBound` - // is an upper bound on the quality (it is used to ignore strands - // whose quality cannot meet a minimum threshold). When calculating - // quality assume no fee is charged, or the estimate will no longer - // be an upper bound. + // is the same as the strand dst. It is important that + // `qualityUpperBound` is an upper bound on the quality (it is used to + // ignore strands whose quality cannot meet a minimum threshold). When + // calculating quality assume no fee is charged, or the estimate will no + // longer be an upper bound. return ofrQ; } - std::string logString () const override + std::string + logString() const override { - return this->logStringImpl ("BookOfferCrossingStep"); + return this->logStringImpl("BookOfferCrossingStep"); } private: @@ -418,7 +453,8 @@ private: //------------------------------------------------------------------------------ template -bool BookStep::equal (Step const& rhs) const +bool +BookStep::equal(Step const& rhs) const { if (auto bs = dynamic_cast const*>(&rhs)) return book_ == bs->book_; @@ -446,8 +482,9 @@ BookStep::qualityUpperBound( // Adjust the offer amount and step amount subject to the given input limit template -static -void limitStepIn (Quality const& ofrQ, +static void +limitStepIn( + Quality const& ofrQ, TAmounts& ofrAmt, TAmounts& stpAmt, TOut& ownerGives, @@ -458,19 +495,20 @@ void limitStepIn (Quality const& ofrQ, if (limit < stpAmt.in) { stpAmt.in = limit; - auto const inLmt = mulRatio ( - stpAmt.in, QUALITY_ONE, transferRateIn, /*roundUp*/ false); - ofrAmt = ofrQ.ceil_in (ofrAmt, inLmt); + auto const inLmt = + mulRatio(stpAmt.in, QUALITY_ONE, transferRateIn, /*roundUp*/ false); + ofrAmt = ofrQ.ceil_in(ofrAmt, inLmt); stpAmt.out = ofrAmt.out; - ownerGives = mulRatio ( + ownerGives = mulRatio( ofrAmt.out, transferRateOut, QUALITY_ONE, /*roundUp*/ false); } } // Adjust the offer amount and step amount subject to the given output limit template -static -void limitStepOut (Quality const& ofrQ, +static void +limitStepOut( + Quality const& ofrQ, TAmounts& ofrAmt, TAmounts& stpAmt, TOut& ownerGives, @@ -481,18 +519,18 @@ void limitStepOut (Quality const& ofrQ, if (limit < stpAmt.out) { stpAmt.out = limit; - ownerGives = mulRatio ( + ownerGives = mulRatio( stpAmt.out, transferRateOut, QUALITY_ONE, /*roundUp*/ false); - ofrAmt = ofrQ.ceil_out (ofrAmt, stpAmt.out); - stpAmt.in = mulRatio ( - ofrAmt.in, transferRateIn, QUALITY_ONE, /*roundUp*/ true); + ofrAmt = ofrQ.ceil_out(ofrAmt, stpAmt.out); + stpAmt.in = + mulRatio(ofrAmt.in, transferRateIn, QUALITY_ONE, /*roundUp*/ true); } } template template std::pair, std::uint32_t> -BookStep::forEachOffer ( +BookStep::forEachOffer( PaymentSandbox& sb, ApplyView& afView, DebtDirection prevStepDir, @@ -501,53 +539,51 @@ BookStep::forEachOffer ( // Charge the offer owner, not the sender // Charge a fee even if the owner is the same as the issuer // (the old code does not charge a fee) - // Calculate amount that goes to the taker and the amount charged the offer owner - auto rate = [this, &sb](AccountID const& id)->std::uint32_t - { - if (isXRP (id) || id == this->strandDst_) + // Calculate amount that goes to the taker and the amount charged the offer + // owner + auto rate = [this, &sb](AccountID const& id) -> std::uint32_t { + if (isXRP(id) || id == this->strandDst_) return QUALITY_ONE; - return transferRate (sb, id).value; + return transferRate(sb, id).value; }; std::uint32_t const trIn = redeems(prevStepDir) ? rate(book_.in.account) : QUALITY_ONE; // Always charge the transfer fee, even if the owner is the issuer - std::uint32_t const trOut = ownerPaysTransferFee_ - ? rate (book_.out.account) - : QUALITY_ONE; + std::uint32_t const trOut = + ownerPaysTransferFee_ ? rate(book_.out.account) : QUALITY_ONE; - typename FlowOfferStream::StepCounter - counter (maxOffersToConsume_, j_); + typename FlowOfferStream::StepCounter counter( + maxOffersToConsume_, j_); - FlowOfferStream offers ( - sb, afView, book_, sb.parentCloseTime (), counter, j_); + FlowOfferStream offers( + sb, afView, book_, sb.parentCloseTime(), counter, j_); bool const flowCross = afView.rules().enabled(featureFlowCross); bool offerAttempted = false; boost::optional ofrQ; - while (offers.step ()) + while (offers.step()) { - auto& offer = offers.tip (); + auto& offer = offers.tip(); // Note that offer.quality() returns a (non-optional) Quality. So // ofrQ is always safe to use below this point in the loop. if (!ofrQ) - ofrQ = offer.quality (); - else if (*ofrQ != offer.quality ()) + ofrQ = offer.quality(); + else if (*ofrQ != offer.quality()) break; - if (static_cast(this)->limitSelfCrossQuality ( - strandSrc_, strandDst_, offer, ofrQ, offers, offerAttempted)) - continue; + if (static_cast(this)->limitSelfCrossQuality( + strandSrc_, strandDst_, offer, ofrQ, offers, offerAttempted)) + continue; // Make sure offer owner has authorization to own IOUs from issuer. // An account can always own XRP or their own IOUs. - if (flowCross && - (!isXRP (offer.issueIn().currency)) && + if (flowCross && (!isXRP(offer.issueIn().currency)) && (offer.owner() != offer.issueIn().account)) { auto const& issuerID = offer.issueIn().account; - auto const issuer = afView.read (keylet::account (issuerID)); + auto const issuer = afView.read(keylet::account(issuerID)); if (issuer && ((*issuer)[sfFlags] & lsfRequireAuth)) { // Issuer requires authorization. See if offer owner has that. @@ -555,14 +591,14 @@ BookStep::forEachOffer ( auto const authFlag = issuerID > ownerID ? lsfHighAuth : lsfLowAuth; - auto const line = afView.read (keylet::line ( - ownerID, issuerID, offer.issueIn().currency)); + auto const line = afView.read( + keylet::line(ownerID, issuerID, offer.issueIn().currency)); if (!line || (((*line)[sfFlags] & authFlag) == 0)) { // Offer owner not authorized to hold IOU from issuer. // Remove this offer even if no crossing occurs. - offers.permRmOffer (offer.key()); + offers.permRmOffer(offer.key()); if (!offerAttempted) // Change quality only if no previous offers were tried. ofrQ = boost::none; @@ -572,101 +608,107 @@ BookStep::forEachOffer ( } } - if (! static_cast(this)->checkQualityThreshold(offer)) + if (!static_cast(this)->checkQualityThreshold(offer)) break; - auto const ofrInRate = - static_cast(this)->getOfrInRate ( - prevStep_, offer, trIn); + auto const ofrInRate = static_cast(this)->getOfrInRate( + prevStep_, offer, trIn); auto const ofrOutRate = - static_cast(this)->getOfrOutRate ( + static_cast(this)->getOfrOutRate( prevStep_, offer, strandDst_, trOut); - auto ofrAmt = offer.amount (); + auto ofrAmt = offer.amount(); TAmounts stpAmt{ - mulRatio (ofrAmt.in, ofrInRate, QUALITY_ONE, /*roundUp*/ true), + mulRatio(ofrAmt.in, ofrInRate, QUALITY_ONE, /*roundUp*/ true), ofrAmt.out}; // owner pays the transfer fee. auto ownerGives = - mulRatio (ofrAmt.out, ofrOutRate, QUALITY_ONE, /*roundUp*/ false); + mulRatio(ofrAmt.out, ofrOutRate, QUALITY_ONE, /*roundUp*/ false); - auto const funds = - (offer.owner () == offer.issueOut ().account) - ? ownerGives // Offer owner is issuer; they have unlimited funds - : offers.ownerFunds (); + auto const funds = (offer.owner() == offer.issueOut().account) + ? ownerGives // Offer owner is issuer; they have unlimited funds + : offers.ownerFunds(); if (funds < ownerGives) { // We already know offer.owner()!=offer.issueOut().account ownerGives = funds; - stpAmt.out = mulRatio ( + stpAmt.out = mulRatio( ownerGives, QUALITY_ONE, ofrOutRate, /*roundUp*/ false); - ofrAmt = ofrQ->ceil_out (ofrAmt, stpAmt.out); - stpAmt.in = mulRatio ( - ofrAmt.in, ofrInRate, QUALITY_ONE, /*roundUp*/ true); + ofrAmt = ofrQ->ceil_out(ofrAmt, stpAmt.out); + stpAmt.in = + mulRatio(ofrAmt.in, ofrInRate, QUALITY_ONE, /*roundUp*/ true); } offerAttempted = true; - if (!callback ( - offer, ofrAmt, stpAmt, ownerGives, ofrInRate, ofrOutRate)) + if (!callback(offer, ofrAmt, stpAmt, ownerGives, ofrInRate, ofrOutRate)) break; } - return {offers.permToRemove (), counter.count()}; + return {offers.permToRemove(), counter.count()}; } template -void BookStep::consumeOffer ( +void +BookStep::consumeOffer( PaymentSandbox& sb, TOffer& offer, TAmounts const& ofrAmt, TAmounts const& stepAmt, TOut const& ownerGives) const { - // The offer owner gets the ofrAmt. The difference between ofrAmt and stepAmt - // is a transfer fee that goes to book_.in.account + // The offer owner gets the ofrAmt. The difference between ofrAmt and + // stepAmt is a transfer fee that goes to book_.in.account { - auto const dr = accountSend (sb, book_.in.account, offer.owner (), - toSTAmount (ofrAmt.in, book_.in), j_); + auto const dr = accountSend( + sb, + book_.in.account, + offer.owner(), + toSTAmount(ofrAmt.in, book_.in), + j_); if (dr != tesSUCCESS) - Throw (dr); + Throw(dr); } // The offer owner pays `ownerGives`. The difference between ownerGives and // stepAmt is a transfer fee that goes to book_.out.account { - auto const cr = accountSend (sb, offer.owner (), book_.out.account, - toSTAmount (ownerGives, book_.out), j_); + auto const cr = accountSend( + sb, + offer.owner(), + book_.out.account, + toSTAmount(ownerGives, book_.out), + j_); if (cr != tesSUCCESS) - Throw (cr); + Throw(cr); } - offer.consume (sb, ofrAmt); + offer.consume(sb, ofrAmt); } -template -static -auto sum (TCollection const& col) +template +static auto +sum(TCollection const& col) { - using TResult = std::decay_t; - if (col.empty ()) + using TResult = std::decay_t; + if (col.empty()) return TResult{beast::zero}; - return std::accumulate (col.begin () + 1, col.end (), *col.begin ()); + return std::accumulate(col.begin() + 1, col.end(), *col.begin()); }; -template +template std::pair -BookStep::revImp ( +BookStep::revImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, TOut const& out) { - cache_.reset (); + cache_.reset(); - TAmounts result (beast::zero, beast::zero); + TAmounts result(beast::zero, beast::zero); auto remainingOut = out; @@ -679,14 +721,12 @@ BookStep::revImp ( amounts - tho always <=) Return true to continue to receive offers, false to stop receiving offers. */ - auto eachOffer = - [&](TOffer& offer, - TAmounts const& ofrAmt, - TAmounts const& stpAmt, - TOut const& ownerGives, - std::uint32_t transferRateIn, - std::uint32_t transferRateOut) mutable -> bool - { + auto eachOffer = [&](TOffer& offer, + TAmounts const& ofrAmt, + TAmounts const& stpAmt, + TOut const& ownerGives, + std::uint32_t transferRateIn, + std::uint32_t transferRateOut) mutable -> bool { if (remainingOut <= beast::zero) return false; @@ -694,9 +734,9 @@ BookStep::revImp ( { savedIns.insert(stpAmt.in); savedOuts.insert(stpAmt.out); - result = TAmounts(sum (savedIns), sum(savedOuts)); + result = TAmounts(sum(savedIns), sum(savedOuts)); remainingOut = out - result.out; - this->consumeOffer (sb, offer, ofrAmt, stpAmt, ownerGives); + this->consumeOffer(sb, offer, ofrAmt, stpAmt, ownerGives); // return true b/c even if the payment is satisfied, // we need to consume the offer return true; @@ -706,14 +746,20 @@ BookStep::revImp ( auto ofrAdjAmt = ofrAmt; auto stpAdjAmt = stpAmt; auto ownerGivesAdj = ownerGives; - limitStepOut (offer.quality (), ofrAdjAmt, stpAdjAmt, ownerGivesAdj, - transferRateIn, transferRateOut, remainingOut); + limitStepOut( + offer.quality(), + ofrAdjAmt, + stpAdjAmt, + ownerGivesAdj, + transferRateIn, + transferRateOut, + remainingOut); remainingOut = beast::zero; - savedIns.insert (stpAdjAmt.in); - savedOuts.insert (remainingOut); + savedIns.insert(stpAdjAmt.in); + savedOuts.insert(remainingOut); result.in = sum(savedIns); result.out = out; - this->consumeOffer (sb, offer, ofrAdjAmt, stpAdjAmt, ownerGivesAdj); + this->consumeOffer(sb, offer, ofrAdjAmt, stpAdjAmt, ownerGivesAdj); // Explicitly check whether the offer is funded. Given that we have // (stpAmt.out > remainingOut), it's natural to assume the offer @@ -725,12 +771,12 @@ BookStep::revImp ( }; { - auto const prevStepDebtDir = [&]{ + auto const prevStepDebtDir = [&] { if (prevStep_) - return prevStep_->debtDirection (sb, StrandDirection::reverse); + return prevStep_->debtDirection(sb, StrandDirection::reverse); return DebtDirection::issues; }(); - auto const r = forEachOffer (sb, afView, prevStepDebtDir, eachOffer); + auto const r = forEachOffer(sb, afView, prevStepDebtDir, eachOffer); boost::container::flat_set toRm = std::move(std::get<0>(r)); std::uint32_t const offersConsumed = std::get<1>(r); SetUnion(ofrsToRm, toRm); @@ -751,32 +797,30 @@ BookStep::revImp ( } } - switch(remainingOut.signum()) + switch (remainingOut.signum()) { - case -1: - { + case -1: { // something went very wrong - JLOG (j_.error ()) - << "BookStep remainingOut < 0 " << to_string (remainingOut); - assert (0); - cache_.emplace (beast::zero, beast::zero); + JLOG(j_.error()) + << "BookStep remainingOut < 0 " << to_string(remainingOut); + assert(0); + cache_.emplace(beast::zero, beast::zero); return {beast::zero, beast::zero}; } - case 0: - { + case 0: { // due to normalization, remainingOut can be zero without // result.out == out. Force result.out == out for this case result.out = out; } } - cache_.emplace (result.in, result.out); + cache_.emplace(result.in, result.out); return {result.in, result.out}; } -template +template std::pair -BookStep::fwdImp ( +BookStep::fwdImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, @@ -784,7 +828,7 @@ BookStep::fwdImp ( { assert(cache_); - TAmounts result (beast::zero, beast::zero); + TAmounts result(beast::zero, beast::zero); auto remainingIn = in; @@ -795,14 +839,12 @@ BookStep::fwdImp ( // amt fed will be adjusted by owner funds (and may differ from the offer's // amounts - tho always <=) - auto eachOffer = - [&](TOffer& offer, - TAmounts const& ofrAmt, - TAmounts const& stpAmt, - TOut const& ownerGives, - std::uint32_t transferRateIn, - std::uint32_t transferRateOut) mutable -> bool - { + auto eachOffer = [&](TOffer& offer, + TAmounts const& ofrAmt, + TAmounts const& stpAmt, + TOut const& ownerGives, + std::uint32_t transferRateIn, + std::uint32_t transferRateOut) mutable -> bool { assert(cache_); if (remainingIn <= beast::zero) @@ -818,17 +860,23 @@ BookStep::fwdImp ( { savedIns.insert(stpAmt.in); lastOut = savedOuts.insert(stpAmt.out); - result = TAmounts(sum (savedIns), sum(savedOuts)); + result = TAmounts(sum(savedIns), sum(savedOuts)); // consume the offer even if stepAmt.in == remainingIn processMore = true; } else { - limitStepIn (offer.quality (), ofrAdjAmt, stpAdjAmt, ownerGivesAdj, - transferRateIn, transferRateOut, remainingIn); - savedIns.insert (remainingIn); - lastOut = savedOuts.insert (stpAdjAmt.out); - result.out = sum (savedOuts); + limitStepIn( + offer.quality(), + ofrAdjAmt, + stpAdjAmt, + ownerGivesAdj, + transferRateIn, + transferRateOut, + remainingIn); + savedIns.insert(remainingIn); + lastOut = savedOuts.insert(stpAdjAmt.out); + result.out = sum(savedOuts); result.in = in; processMore = false; @@ -845,12 +893,17 @@ BookStep::fwdImp ( // requested from the reverse step. auto const lastOutAmt = *lastOut; savedOuts.erase(lastOut); - auto const remainingOut = cache_->out - sum (savedOuts); + auto const remainingOut = cache_->out - sum(savedOuts); auto ofrAdjAmtRev = ofrAmt; auto stpAdjAmtRev = stpAmt; auto ownerGivesAdjRev = ownerGives; - limitStepOut (offer.quality (), ofrAdjAmtRev, stpAdjAmtRev, - ownerGivesAdjRev, transferRateIn, transferRateOut, + limitStepOut( + offer.quality(), + ofrAdjAmtRev, + stpAdjAmtRev, + ownerGivesAdjRev, + transferRateIn, + transferRateOut, remainingOut); if (stpAdjAmtRev.in == remainingIn) @@ -872,17 +925,17 @@ BookStep::fwdImp ( { // This is (likely) a problem case, and wil be caught // with later checks - savedOuts.insert (lastOutAmt); + savedOuts.insert(lastOutAmt); } } remainingIn = in - result.in; - this->consumeOffer (sb, offer, ofrAdjAmt, stpAdjAmt, ownerGivesAdj); + this->consumeOffer(sb, offer, ofrAdjAmt, stpAdjAmt, ownerGivesAdj); // When the mantissas of two iou amounts differ by less than ten, then - // subtracting them leaves a result of zero. This can cause the check for - // (stpAmt.in > remainingIn) to incorrectly think an offer will be funded - // after subtracting remainingIn. + // subtracting them leaves a result of zero. This can cause the check + // for (stpAmt.in > remainingIn) to incorrectly think an offer will be + // funded after subtracting remainingIn. return processMore || offer.fully_consumed(); }; @@ -913,40 +966,38 @@ BookStep::fwdImp ( } } - switch(remainingIn.signum()) + switch (remainingIn.signum()) { - case -1: - { + case -1: { // something went very wrong - JLOG (j_.error ()) - << "BookStep remainingIn < 0 " << to_string (remainingIn); - assert (0); - cache_.emplace (beast::zero, beast::zero); + JLOG(j_.error()) + << "BookStep remainingIn < 0 " << to_string(remainingIn); + assert(0); + cache_.emplace(beast::zero, beast::zero); return {beast::zero, beast::zero}; } - case 0: - { + case 0: { // due to normalization, remainingIn can be zero without // result.in == in. Force result.in == in for this case result.in = in; } } - cache_.emplace (result.in, result.out); + cache_.emplace(result.in, result.out); return {result.in, result.out}; } -template +template std::pair -BookStep::validFwd ( +BookStep::validFwd( PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) { if (!cache_) { - JLOG (j_.trace()) << "Expected valid cache in validFwd"; - return {false, EitherAmount (TOut (beast::zero))}; + JLOG(j_.trace()) << "Expected valid cache in validFwd"; + return {false, EitherAmount(TOut(beast::zero))}; } auto const savCache = *cache_; @@ -954,48 +1005,49 @@ BookStep::validFwd ( try { boost::container::flat_set dummy; - fwdImp (sb, afView, dummy, get (in)); // changes cache + fwdImp(sb, afView, dummy, get(in)); // changes cache } catch (FlowException const&) { - return {false, EitherAmount (TOut (beast::zero))}; + return {false, EitherAmount(TOut(beast::zero))}; } - if (!(checkNear (savCache.in, cache_->in) && - checkNear (savCache.out, cache_->out))) + if (!(checkNear(savCache.in, cache_->in) && + checkNear(savCache.out, cache_->out))) { - JLOG (j_.warn()) << - "Strand re-execute check failed." << - " ExpectedIn: " << to_string (savCache.in) << - " CachedIn: " << to_string (cache_->in) << - " ExpectedOut: " << to_string (savCache.out) << - " CachedOut: " << to_string (cache_->out); - return {false, EitherAmount (cache_->out)}; + JLOG(j_.warn()) << "Strand re-execute check failed." + << " ExpectedIn: " << to_string(savCache.in) + << " CachedIn: " << to_string(cache_->in) + << " ExpectedOut: " << to_string(savCache.out) + << " CachedOut: " << to_string(cache_->out); + return {false, EitherAmount(cache_->out)}; } - return {true, EitherAmount (cache_->out)}; + return {true, EitherAmount(cache_->out)}; } -template +template TER BookStep::check(StrandContext const& ctx) const { if (book_.in == book_.out) { - JLOG (j_.debug()) << "BookStep: Book with same in and out issuer " << *this; + JLOG(j_.debug()) << "BookStep: Book with same in and out issuer " + << *this; return temBAD_PATH; } - if (!isConsistent (book_.in) || !isConsistent (book_.out)) + if (!isConsistent(book_.in) || !isConsistent(book_.out)) { - JLOG (j_.debug()) << "Book: currency is inconsistent with issuer." << *this; + JLOG(j_.debug()) << "Book: currency is inconsistent with issuer." + << *this; return temBAD_PATH; } // Do not allow two books to output the same issue. This may cause offers on // one step to unfund offers in another step. - if (!ctx.seenBookOuts.insert (book_.out).second || - ctx.seenDirectIssues[0].count (book_.out)) + if (!ctx.seenBookOuts.insert(book_.out).second || + ctx.seenDirectIssues[0].count(book_.out)) { - JLOG (j_.debug()) << "BookStep: loop detected: " << *this; + JLOG(j_.debug()) << "BookStep: loop detected: " << *this; return temBAD_PATH_LOOP; } @@ -1022,8 +1074,7 @@ BookStep::check(StrandContext const& ctx) const auto const& view = ctx.view; auto const& cur = book_.in.account; - auto sle = - view.read(keylet::line(*prev, cur, book_.in.currency)); + auto sle = view.read(keylet::line(*prev, cur, book_.in.currency)); if (!sle) return terNO_LINE; if ((*sle)[sfFlags] & @@ -1037,64 +1088,68 @@ BookStep::check(StrandContext const& ctx) const //------------------------------------------------------------------------------ -namespace test -{ +namespace test { // Needed for testing template -static -bool equalHelper (Step const& step, ripple::Book const& book) +static bool +equalHelper(Step const& step, ripple::Book const& book) { - if (auto bs = dynamic_cast const*> (&step)) - return book == bs->book (); + if (auto bs = dynamic_cast const*>(&step)) + return book == bs->book(); return false; } -bool bookStepEqual (Step const& step, ripple::Book const& book) +bool +bookStepEqual(Step const& step, ripple::Book const& book) { - bool const inXRP = isXRP (book.in.currency); - bool const outXRP = isXRP (book.out.currency); + bool const inXRP = isXRP(book.in.currency); + bool const outXRP = isXRP(book.out.currency); if (inXRP && outXRP) - return equalHelper> (step, book); + return equalHelper< + XRPAmount, + XRPAmount, + BookPaymentStep>(step, book); if (inXRP && !outXRP) - return equalHelper> (step, book); + return equalHelper< + XRPAmount, + IOUAmount, + BookPaymentStep>(step, book); if (!inXRP && outXRP) - return equalHelper> (step, book); + return equalHelper< + IOUAmount, + XRPAmount, + BookPaymentStep>(step, book); if (!inXRP && !outXRP) - return equalHelper> (step, book); + return equalHelper< + IOUAmount, + IOUAmount, + BookPaymentStep>(step, book); return false; } -} +} // namespace test //------------------------------------------------------------------------------ template -static -std::pair> -make_BookStepHelper ( - StrandContext const& ctx, - Issue const& in, - Issue const& out) +static std::pair> +make_BookStepHelper(StrandContext const& ctx, Issue const& in, Issue const& out) { TER ter = tefINTERNAL; std::unique_ptr r; if (ctx.offerCrossing) { auto offerCrossingStep = - std::make_unique> (ctx, in, out); - ter = offerCrossingStep->check (ctx); - r = std::move (offerCrossingStep); + std::make_unique>(ctx, in, out); + ter = offerCrossingStep->check(ctx); + r = std::move(offerCrossingStep); } - else // payment + else // payment { auto paymentStep = - std::make_unique> (ctx, in, out); - ter = paymentStep->check (ctx); - r = std::move (paymentStep); + std::make_unique>(ctx, in, out); + ter = paymentStep->check(ctx); + r = std::move(paymentStep); } if (ter != tesSUCCESS) return {ter, nullptr}; @@ -1103,28 +1158,21 @@ make_BookStepHelper ( } std::pair> -make_BookStepII ( - StrandContext const& ctx, - Issue const& in, - Issue const& out) +make_BookStepII(StrandContext const& ctx, Issue const& in, Issue const& out) { - return make_BookStepHelper (ctx, in, out); + return make_BookStepHelper(ctx, in, out); } std::pair> -make_BookStepIX ( - StrandContext const& ctx, - Issue const& in) +make_BookStepIX(StrandContext const& ctx, Issue const& in) { - return make_BookStepHelper (ctx, in, xrpIssue()); + return make_BookStepHelper(ctx, in, xrpIssue()); } std::pair> -make_BookStepXI ( - StrandContext const& ctx, - Issue const& out) +make_BookStepXI(StrandContext const& ctx, Issue const& out) { - return make_BookStepHelper (ctx, xrpIssue(), out); + return make_BookStepHelper(ctx, xrpIssue(), out); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/impl/DirectStep.cpp b/src/ripple/app/paths/impl/DirectStep.cpp index 2d2e174ddd..760029a861 100644 --- a/src/ripple/app/paths/impl/DirectStep.cpp +++ b/src/ripple/app/paths/impl/DirectStep.cpp @@ -54,16 +54,14 @@ protected: IOUAmount out; DebtDirection srcDebtDir; - Cache ( + Cache( IOUAmount const& in_, IOUAmount const& srcToDst_, IOUAmount const& out_, DebtDirection srcDebtDir_) - : in(in_) - , srcToDst(srcToDst_) - , out(out_) - , srcDebtDir(srcDebtDir_) - {} + : in(in_), srcToDst(srcToDst_), out(out_), srcDebtDir(srcDebtDir_) + { + } }; boost::optional cache_; @@ -73,147 +71,150 @@ protected: // return: first element is max amount that can flow, // second is the debt direction of the source w.r.t. the dst std::pair - maxPaymentFlow ( - ReadView const& sb) const; + maxPaymentFlow(ReadView const& sb) const; // Compute srcQOut and dstQIn when the source redeems. std::pair - qualitiesSrcRedeems( - ReadView const& sb) const; + qualitiesSrcRedeems(ReadView const& sb) const; // Compute srcQOut and dstQIn when the source issues. std::pair - qualitiesSrcIssues( - ReadView const& sb, - DebtDirection prevStepDebtDirection) const; + qualitiesSrcIssues(ReadView const& sb, DebtDirection prevStepDebtDirection) + const; // Returns srcQOut, dstQIn - std::pair - qualities ( + std::pair + qualities( ReadView const& sb, DebtDirection srcDebtDir, StrandDirection strandDir) const; public: - DirectStepI ( + DirectStepI( StrandContext const& ctx, AccountID const& src, AccountID const& dst, Currency const& c) - : src_(src) - , dst_(dst) - , currency_ (c) - , prevStep_ (ctx.prevStep) - , isLast_ (ctx.isLast) - , j_ (ctx.j) - {} + : src_(src) + , dst_(dst) + , currency_(c) + , prevStep_(ctx.prevStep) + , isLast_(ctx.isLast) + , j_(ctx.j) + { + } - AccountID const& src () const + AccountID const& + src() const { return src_; } - AccountID const& dst () const + AccountID const& + dst() const { return dst_; } - Currency const& currency () const + Currency const& + currency() const { return currency_; } - boost::optional cachedIn () const override + boost::optional + cachedIn() const override { if (!cache_) return boost::none; - return EitherAmount (cache_->in); + return EitherAmount(cache_->in); } boost::optional - cachedOut () const override + cachedOut() const override { if (!cache_) return boost::none; - return EitherAmount (cache_->out); + return EitherAmount(cache_->out); } boost::optional - directStepSrcAcct () const override + directStepSrcAcct() const override { return src_; } - boost::optional> - directStepAccts () const override + boost::optional> + directStepAccts() const override { return std::make_pair(src_, dst_); } DebtDirection - debtDirection (ReadView const& sb, StrandDirection dir) const override; + debtDirection(ReadView const& sb, StrandDirection dir) const override; std::uint32_t - lineQualityIn (ReadView const& v) const override; + lineQualityIn(ReadView const& v) const override; std::pair, DebtDirection> qualityUpperBound(ReadView const& v, DebtDirection dir) const override; std::pair - revImp ( + revImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, IOUAmount const& out); std::pair - fwdImp ( + fwdImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, IOUAmount const& in); std::pair - validFwd ( - PaymentSandbox& sb, - ApplyView& afView, - EitherAmount const& in) override; + validFwd(PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) + override; // Check for error, existing liquidity, and violations of auth/frozen // constraints. - TER check (StrandContext const& ctx) const; + TER + check(StrandContext const& ctx) const; - void setCacheLimiting ( + void + setCacheLimiting( IOUAmount const& fwdIn, IOUAmount const& fwdSrcToDst, IOUAmount const& fwdOut, DebtDirection srcDebtDir); - friend bool operator==(DirectStepI const& lhs, DirectStepI const& rhs) + friend bool + operator==(DirectStepI const& lhs, DirectStepI const& rhs) { - return lhs.src_ == rhs.src_ && - lhs.dst_ == rhs.dst_ && + return lhs.src_ == rhs.src_ && lhs.dst_ == rhs.dst_ && lhs.currency_ == rhs.currency_; } - friend bool operator!=(DirectStepI const& lhs, DirectStepI const& rhs) + friend bool + operator!=(DirectStepI const& lhs, DirectStepI const& rhs) { - return ! (lhs == rhs); + return !(lhs == rhs); } protected: - std::string logStringImpl (char const* name) const + std::string + logStringImpl(char const* name) const { std::ostringstream ostr; - ostr << - name << ": " << - "\nSrc: " << src_ << - "\nDst: " << dst_; - return ostr.str (); + ostr << name << ": " + << "\nSrc: " << src_ << "\nDst: " << dst_; + return ostr.str(); } private: - bool equal (Step const& rhs) const override + bool + equal(Step const& rhs) const override { - if (auto ds = dynamic_cast (&rhs)) + if (auto ds = dynamic_cast(&rhs)) { return *this == *ds; } @@ -236,38 +237,39 @@ public: using DirectStepI::DirectStepI; using DirectStepI::check; - bool verifyPrevStepDebtDirection (DebtDirection) const + bool verifyPrevStepDebtDirection(DebtDirection) const { // A payment doesn't care whether or not prevStepRedeems. return true; } - bool verifyDstQualityIn (std::uint32_t dstQIn) const + bool + verifyDstQualityIn(std::uint32_t dstQIn) const { // Payments have no particular expectations for what dstQIn will be. return true; } std::uint32_t - quality (ReadView const& sb, - QualityDirection qDir) const; + quality(ReadView const& sb, QualityDirection qDir) const; // Compute the maximum value that can flow from src->dst at // the best available quality. // return: first element is max amount that can flow, // second is the debt direction w.r.t. the source account std::pair - maxFlow (ReadView const& sb, IOUAmount const& desired) const; + maxFlow(ReadView const& sb, IOUAmount const& desired) const; // Verify the consistency of the step. These checks are specific to // payments and assume that general checks were already performed. TER - check (StrandContext const& ctx, - std::shared_ptr const& sleSrc) const; + check(StrandContext const& ctx, std::shared_ptr const& sleSrc) + const; - std::string logString () const override + std::string + logString() const override { - return logStringImpl ("DirectIPaymentStep"); + return logStringImpl("DirectIPaymentStep"); } }; @@ -278,18 +280,21 @@ public: using DirectStepI::DirectStepI; using DirectStepI::check; - bool verifyPrevStepDebtDirection (DebtDirection prevStepDir) const + bool + verifyPrevStepDebtDirection(DebtDirection prevStepDir) const { // During offer crossing we rely on the fact that prevStepRedeems // will *always* issue. That's because: // o If there's a prevStep_, it will always be a BookStep. - // o BookStep::debtDirection() aways returns `issues` when offer crossing. + // o BookStep::debtDirection() aways returns `issues` when offer + // crossing. // An assert based on this return value will tell us if that // behavior changes. return issues(prevStepDir); } - bool verifyDstQualityIn (std::uint32_t dstQIn) const + bool + verifyDstQualityIn(std::uint32_t dstQIn) const { // Due to a couple of factors dstQIn is always QUALITY_ONE for // offer crossing. If that changes we need to know. @@ -297,45 +302,42 @@ public: } std::uint32_t - quality (ReadView const& sb, - QualityDirection qDir) const; + quality(ReadView const& sb, QualityDirection qDir) const; // Compute the maximum value that can flow from src->dst at // the best available quality. // return: first element is max amount that can flow, // second is the debt direction w.r.t the source std::pair - maxFlow (ReadView const& sb, IOUAmount const& desired) const; + maxFlow(ReadView const& sb, IOUAmount const& desired) const; // Verify the consistency of the step. These checks are specific to // offer crossing and assume that general checks were already performed. TER - check (StrandContext const& ctx, - std::shared_ptr const& sleSrc) const; + check(StrandContext const& ctx, std::shared_ptr const& sleSrc) + const; - - std::string logString () const override + std::string + logString() const override { - return logStringImpl ("DirectIOfferCrossingStep"); + return logStringImpl("DirectIOfferCrossingStep"); } }; //------------------------------------------------------------------------------ std::uint32_t -DirectIPaymentStep::quality (ReadView const& sb, - QualityDirection qDir) const +DirectIPaymentStep::quality(ReadView const& sb, QualityDirection qDir) const { if (src_ == dst_) return QUALITY_ONE; - auto const sle = sb.read (keylet::line (dst_, src_, currency_)); + auto const sle = sb.read(keylet::line(dst_, src_, currency_)); if (!sle) return QUALITY_ONE; - auto const& field = [this, qDir]() -> SF_U32 const& - { + auto const& field = [this, qDir]() -> SF_U32 const& { if (qDir == QualityDirection::in) { // compute dst quality in @@ -354,7 +356,7 @@ DirectIPaymentStep::quality (ReadView const& sb, } }(); - if (! sle->isFieldPresent (field)) + if (!sle->isFieldPresent(field)) return QUALITY_ONE; auto const q = (*sle)[field]; @@ -364,8 +366,7 @@ DirectIPaymentStep::quality (ReadView const& sb, } std::uint32_t -DirectIOfferCrossingStep::quality (ReadView const&, - QualityDirection qDir) const +DirectIOfferCrossingStep::quality(ReadView const&, QualityDirection qDir) const { // If offer crossing then ignore trust line Quality fields. This // preserves a long-standing tradition. @@ -373,14 +374,14 @@ DirectIOfferCrossingStep::quality (ReadView const&, } std::pair -DirectIPaymentStep::maxFlow (ReadView const& sb, IOUAmount const&) const +DirectIPaymentStep::maxFlow(ReadView const& sb, IOUAmount const&) const { - return maxPaymentFlow (sb); + return maxPaymentFlow(sb); } std::pair -DirectIOfferCrossingStep::maxFlow ( - ReadView const& sb, IOUAmount const& desired) const +DirectIOfferCrossingStep::maxFlow(ReadView const& sb, IOUAmount const& desired) + const { // When isLast and offer crossing then ignore trust line limits. Offer // crossing has the ability to exceed the limit set by a trust line. @@ -397,20 +398,21 @@ DirectIOfferCrossingStep::maxFlow ( if (isLast_) return {desired, DebtDirection::issues}; - return maxPaymentFlow (sb); + return maxPaymentFlow(sb); } TER -DirectIPaymentStep::check ( - StrandContext const& ctx, std::shared_ptr const& sleSrc) const +DirectIPaymentStep::check( + StrandContext const& ctx, + std::shared_ptr const& sleSrc) const { // Since this is a payment a trust line must be present. Perform all // trust line related checks. { - auto const sleLine = ctx.view.read (keylet::line (src_, dst_, currency_)); + auto const sleLine = ctx.view.read(keylet::line(src_, dst_, currency_)); if (!sleLine) { - JLOG (j_.trace()) << "DirectStepI: No credit line. " << *this; + JLOG(j_.trace()) << "DirectStepI: No credit line. " << *this; return terNO_LINE; } @@ -420,7 +422,7 @@ DirectIPaymentStep::check ( !((*sleLine)[sfFlags] & authField) && (*sleLine)[sfBalance] == beast::zero) { - JLOG (j_.warn()) + JLOG(j_.warn()) << "DirectStepI: can't receive IOUs from issuer without auth." << " src: " << src_; return terNO_AUTH; @@ -440,14 +442,14 @@ DirectIPaymentStep::check ( } { - auto const owed = creditBalance (ctx.view, dst_, src_, currency_); + auto const owed = creditBalance(ctx.view, dst_, src_, currency_); if (owed <= beast::zero) { - auto const limit = creditLimit (ctx.view, dst_, src_, currency_); + auto const limit = creditLimit(ctx.view, dst_, src_, currency_); if (-owed >= limit) { - JLOG (j_.debug()) - << "DirectStepI: dry: owed: " << owed << " limit: " << limit; + JLOG(j_.debug()) << "DirectStepI: dry: owed: " << owed + << " limit: " << limit; return tecPATH_DRY; } } @@ -456,8 +458,9 @@ DirectIPaymentStep::check ( } TER -DirectIOfferCrossingStep::check ( - StrandContext const&, std::shared_ptr const&) const +DirectIOfferCrossingStep::check( + StrandContext const&, + std::shared_ptr const&) const { // The standard checks are all we can do because any remaining checks // require the existence of a trust line. Offer crossing does not @@ -469,16 +472,18 @@ DirectIOfferCrossingStep::check ( template std::pair -DirectStepI::maxPaymentFlow (ReadView const& sb) const +DirectStepI::maxPaymentFlow(ReadView const& sb) const { - auto const srcOwed = toAmount ( - accountHolds (sb, src_, currency_, dst_, fhIGNORE_FREEZE, j_)); + auto const srcOwed = toAmount( + accountHolds(sb, src_, currency_, dst_, fhIGNORE_FREEZE, j_)); - if (srcOwed.signum () > 0) + if (srcOwed.signum() > 0) return {srcOwed, DebtDirection::redeems}; // srcOwed is negative or zero - return {creditLimit2 (sb, dst_, src_, currency_) + srcOwed, DebtDirection::issues}; + return { + creditLimit2(sb, dst_, src_, currency_) + srcOwed, + DebtDirection::issues}; } template @@ -497,77 +502,81 @@ DirectStepI::debtDirection(ReadView const& sb, StrandDirection dir) template std::pair -DirectStepI::revImp ( +DirectStepI::revImp( PaymentSandbox& sb, ApplyView& /*afView*/, boost::container::flat_set& /*ofrsToRm*/, IOUAmount const& out) { - cache_.reset (); + cache_.reset(); auto const [maxSrcToDst, srcDebtDir] = static_cast(this)->maxFlow(sb, out); - auto const [srcQOut, dstQIn] = qualities (sb, srcDebtDir, StrandDirection::reverse); - assert (static_cast(this)->verifyDstQualityIn (dstQIn)); + auto const [srcQOut, dstQIn] = + qualities(sb, srcDebtDir, StrandDirection::reverse); + assert(static_cast(this)->verifyDstQualityIn(dstQIn)); - Issue const srcToDstIss( - currency_, redeems(srcDebtDir) ? dst_ : src_); + Issue const srcToDstIss(currency_, redeems(srcDebtDir) ? dst_ : src_); - JLOG (j_.trace()) << - "DirectStepI::rev" << - " srcRedeems: " << redeems(srcDebtDir) << - " outReq: " << to_string (out) << - " maxSrcToDst: " << to_string (maxSrcToDst) << - " srcQOut: " << srcQOut << - " dstQIn: " << dstQIn; + JLOG(j_.trace()) << "DirectStepI::rev" + << " srcRedeems: " << redeems(srcDebtDir) + << " outReq: " << to_string(out) + << " maxSrcToDst: " << to_string(maxSrcToDst) + << " srcQOut: " << srcQOut << " dstQIn: " << dstQIn; - if (maxSrcToDst.signum () <= 0) + if (maxSrcToDst.signum() <= 0) { - JLOG (j_.trace()) << "DirectStepI::rev: dry"; - cache_.emplace ( - IOUAmount (beast::zero), - IOUAmount (beast::zero), - IOUAmount (beast::zero), + JLOG(j_.trace()) << "DirectStepI::rev: dry"; + cache_.emplace( + IOUAmount(beast::zero), + IOUAmount(beast::zero), + IOUAmount(beast::zero), srcDebtDir); return {beast::zero, beast::zero}; } - IOUAmount const srcToDst = mulRatio ( - out, QUALITY_ONE, dstQIn, /*roundUp*/ true); + IOUAmount const srcToDst = + mulRatio(out, QUALITY_ONE, dstQIn, /*roundUp*/ true); if (srcToDst <= maxSrcToDst) { - IOUAmount const in = mulRatio ( - srcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); - cache_.emplace (in, srcToDst, out, srcDebtDir); - rippleCredit (sb, - src_, dst_, toSTAmount (srcToDst, srcToDstIss), - /*checkIssuer*/ true, j_); - JLOG (j_.trace()) << - "DirectStepI::rev: Non-limiting" << - " srcRedeems: " << redeems(srcDebtDir) << - " in: " << to_string (in) << - " srcToDst: " << to_string (srcToDst) << - " out: " << to_string (out); + IOUAmount const in = + mulRatio(srcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); + cache_.emplace(in, srcToDst, out, srcDebtDir); + rippleCredit( + sb, + src_, + dst_, + toSTAmount(srcToDst, srcToDstIss), + /*checkIssuer*/ true, + j_); + JLOG(j_.trace()) << "DirectStepI::rev: Non-limiting" + << " srcRedeems: " << redeems(srcDebtDir) + << " in: " << to_string(in) + << " srcToDst: " << to_string(srcToDst) + << " out: " << to_string(out); return {in, out}; } // limiting node - IOUAmount const in = mulRatio ( - maxSrcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); - IOUAmount const actualOut = mulRatio ( - maxSrcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); - cache_.emplace (in, maxSrcToDst, actualOut, srcDebtDir); - rippleCredit (sb, - src_, dst_, toSTAmount (maxSrcToDst, srcToDstIss), - /*checkIssuer*/ true, j_); - JLOG (j_.trace()) << - "DirectStepI::rev: Limiting" << - " srcRedeems: " << redeems(srcDebtDir) << - " in: " << to_string (in) << - " srcToDst: " << to_string (maxSrcToDst) << - " out: " << to_string (out); + IOUAmount const in = + mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); + IOUAmount const actualOut = + mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); + cache_.emplace(in, maxSrcToDst, actualOut, srcDebtDir); + rippleCredit( + sb, + src_, + dst_, + toSTAmount(maxSrcToDst, srcToDstIss), + /*checkIssuer*/ true, + j_); + JLOG(j_.trace()) << "DirectStepI::rev: Limiting" + << " srcRedeems: " << redeems(srcDebtDir) + << " in: " << to_string(in) + << " srcToDst: " << to_string(maxSrcToDst) + << " out: " << to_string(out); return {in, actualOut}; } @@ -577,7 +586,7 @@ DirectStepI::revImp ( // to prevent this. template void -DirectStepI::setCacheLimiting ( +DirectStepI::setCacheLimiting( IOUAmount const& fwdIn, IOUAmount const& fwdSrcToDst, IOUAmount const& fwdOut, @@ -589,21 +598,22 @@ DirectStepI::setCacheLimiting ( auto const diff = fwdIn - cache_->in; if (diff > smallDiff) { - if (fwdIn.exponent () != cache_->in.exponent () || - !cache_->in.mantissa () || - (double(fwdIn.mantissa ()) / - double(cache_->in.mantissa ())) > 1.01) + if (fwdIn.exponent() != cache_->in.exponent() || + !cache_->in.mantissa() || + (double(fwdIn.mantissa()) / double(cache_->in.mantissa())) > + 1.01) { - // Detect large diffs on forward pass so they may be investigated - JLOG (j_.warn()) + // Detect large diffs on forward pass so they may be + // investigated + JLOG(j_.warn()) << "DirectStepI::fwd: setCacheLimiting" - << " fwdIn: " << to_string (fwdIn) - << " cacheIn: " << to_string (cache_->in) - << " fwdSrcToDst: " << to_string (fwdSrcToDst) - << " cacheSrcToDst: " << to_string (cache_->srcToDst) - << " fwdOut: " << to_string (fwdOut) - << " cacheOut: " << to_string (cache_->out); - cache_.emplace (fwdIn, fwdSrcToDst, fwdOut, srcDebtDir); + << " fwdIn: " << to_string(fwdIn) + << " cacheIn: " << to_string(cache_->in) + << " fwdSrcToDst: " << to_string(fwdSrcToDst) + << " cacheSrcToDst: " << to_string(cache_->srcToDst) + << " fwdOut: " << to_string(fwdOut) + << " cacheOut: " << to_string(cache_->out); + cache_.emplace(fwdIn, fwdSrcToDst, fwdOut, srcDebtDir); return; } } @@ -618,96 +628,100 @@ DirectStepI::setCacheLimiting ( template std::pair -DirectStepI::fwdImp ( +DirectStepI::fwdImp( PaymentSandbox& sb, ApplyView& /*afView*/, boost::container::flat_set& /*ofrsToRm*/, IOUAmount const& in) { - assert (cache_); + assert(cache_); auto const [maxSrcToDst, srcDebtDir] = - static_cast(this)->maxFlow (sb, cache_->srcToDst); + static_cast(this)->maxFlow(sb, cache_->srcToDst); - auto const [srcQOut, dstQIn] = qualities (sb, srcDebtDir, StrandDirection::forward); + auto const [srcQOut, dstQIn] = + qualities(sb, srcDebtDir, StrandDirection::forward); - Issue const srcToDstIss (currency_, redeems(srcDebtDir) ? dst_ : src_); + Issue const srcToDstIss(currency_, redeems(srcDebtDir) ? dst_ : src_); - JLOG (j_.trace()) << - "DirectStepI::fwd" << - " srcRedeems: " << redeems(srcDebtDir) << - " inReq: " << to_string (in) << - " maxSrcToDst: " << to_string (maxSrcToDst) << - " srcQOut: " << srcQOut << - " dstQIn: " << dstQIn; + JLOG(j_.trace()) << "DirectStepI::fwd" + << " srcRedeems: " << redeems(srcDebtDir) + << " inReq: " << to_string(in) + << " maxSrcToDst: " << to_string(maxSrcToDst) + << " srcQOut: " << srcQOut << " dstQIn: " << dstQIn; - if (maxSrcToDst.signum () <= 0) + if (maxSrcToDst.signum() <= 0) { - JLOG (j_.trace()) << "DirectStepI::fwd: dry"; - cache_.emplace ( - IOUAmount (beast::zero), - IOUAmount (beast::zero), - IOUAmount (beast::zero), + JLOG(j_.trace()) << "DirectStepI::fwd: dry"; + cache_.emplace( + IOUAmount(beast::zero), + IOUAmount(beast::zero), + IOUAmount(beast::zero), srcDebtDir); return {beast::zero, beast::zero}; } - IOUAmount const srcToDst = mulRatio ( - in, QUALITY_ONE, srcQOut, /*roundUp*/ false); + IOUAmount const srcToDst = + mulRatio(in, QUALITY_ONE, srcQOut, /*roundUp*/ false); if (srcToDst <= maxSrcToDst) { - IOUAmount const out = mulRatio ( - srcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); - setCacheLimiting (in, srcToDst, out, srcDebtDir); - rippleCredit (sb, - src_, dst_, toSTAmount (cache_->srcToDst, srcToDstIss), - /*checkIssuer*/ true, j_); - JLOG (j_.trace()) << - "DirectStepI::fwd: Non-limiting" << - " srcRedeems: " << redeems(srcDebtDir) << - " in: " << to_string (in) << - " srcToDst: " << to_string (srcToDst) << - " out: " << to_string (out); + IOUAmount const out = + mulRatio(srcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); + setCacheLimiting(in, srcToDst, out, srcDebtDir); + rippleCredit( + sb, + src_, + dst_, + toSTAmount(cache_->srcToDst, srcToDstIss), + /*checkIssuer*/ true, + j_); + JLOG(j_.trace()) << "DirectStepI::fwd: Non-limiting" + << " srcRedeems: " << redeems(srcDebtDir) + << " in: " << to_string(in) + << " srcToDst: " << to_string(srcToDst) + << " out: " << to_string(out); } else { // limiting node - IOUAmount const actualIn = mulRatio ( - maxSrcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); - IOUAmount const out = mulRatio ( - maxSrcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); - setCacheLimiting (actualIn, maxSrcToDst, out, srcDebtDir); - rippleCredit (sb, - src_, dst_, toSTAmount (cache_->srcToDst, srcToDstIss), - /*checkIssuer*/ true, j_); - JLOG (j_.trace()) << - "DirectStepI::rev: Limiting" << - " srcRedeems: " << redeems(srcDebtDir) << - " in: " << to_string (actualIn) << - " srcToDst: " << to_string (srcToDst) << - " out: " << to_string (out); + IOUAmount const actualIn = + mulRatio(maxSrcToDst, srcQOut, QUALITY_ONE, /*roundUp*/ true); + IOUAmount const out = + mulRatio(maxSrcToDst, dstQIn, QUALITY_ONE, /*roundUp*/ false); + setCacheLimiting(actualIn, maxSrcToDst, out, srcDebtDir); + rippleCredit( + sb, + src_, + dst_, + toSTAmount(cache_->srcToDst, srcToDstIss), + /*checkIssuer*/ true, + j_); + JLOG(j_.trace()) << "DirectStepI::rev: Limiting" + << " srcRedeems: " << redeems(srcDebtDir) + << " in: " << to_string(actualIn) + << " srcToDst: " << to_string(srcToDst) + << " out: " << to_string(out); } return {cache_->in, cache_->out}; } template std::pair -DirectStepI::validFwd ( +DirectStepI::validFwd( PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) { if (!cache_) { - JLOG (j_.trace()) << "Expected valid cache in validFwd"; - return {false, EitherAmount (IOUAmount (beast::zero))}; + JLOG(j_.trace()) << "Expected valid cache in validFwd"; + return {false, EitherAmount(IOUAmount(beast::zero))}; } - auto const savCache = *cache_; - assert (!in.native); + assert(!in.native); auto const [maxSrcToDst, srcDebtDir] = static_cast(this)->maxFlow(sb, cache_->srcToDst); @@ -716,42 +730,39 @@ DirectStepI::validFwd ( try { boost::container::flat_set dummy; - fwdImp (sb, afView, dummy, in.iou); // changes cache + fwdImp(sb, afView, dummy, in.iou); // changes cache } catch (FlowException const&) { - return {false, EitherAmount (IOUAmount (beast::zero))}; + return {false, EitherAmount(IOUAmount(beast::zero))}; } if (maxSrcToDst < cache_->srcToDst) { - JLOG (j_.warn()) << - "DirectStepI: Strand re-execute check failed." << - " Exceeded max src->dst limit" << - " max src->dst: " << to_string (maxSrcToDst) << - " actual src->dst: " << to_string (cache_->srcToDst); + JLOG(j_.warn()) << "DirectStepI: Strand re-execute check failed." + << " Exceeded max src->dst limit" + << " max src->dst: " << to_string(maxSrcToDst) + << " actual src->dst: " << to_string(cache_->srcToDst); return {false, EitherAmount(cache_->out)}; } - if (!(checkNear (savCache.in, cache_->in) && - checkNear (savCache.out, cache_->out))) + if (!(checkNear(savCache.in, cache_->in) && + checkNear(savCache.out, cache_->out))) { - JLOG (j_.warn()) << - "DirectStepI: Strand re-execute check failed." << - " ExpectedIn: " << to_string (savCache.in) << - " CachedIn: " << to_string (cache_->in) << - " ExpectedOut: " << to_string (savCache.out) << - " CachedOut: " << to_string (cache_->out); - return {false, EitherAmount (cache_->out)}; + JLOG(j_.warn()) << "DirectStepI: Strand re-execute check failed." + << " ExpectedIn: " << to_string(savCache.in) + << " CachedIn: " << to_string(cache_->in) + << " ExpectedOut: " << to_string(savCache.out) + << " CachedOut: " << to_string(cache_->out); + return {false, EitherAmount(cache_->out)}; } - return {true, EitherAmount (cache_->out)}; + return {true, EitherAmount(cache_->out)}; } // Returns srcQOut, dstQIn template std::pair -DirectStepI::qualitiesSrcRedeems( - ReadView const& sb) const +DirectStepI::qualitiesSrcRedeems(ReadView const& sb) const { if (!prevStep_) return {QUALITY_ONE, QUALITY_ONE}; @@ -772,20 +783,20 @@ DirectStepI::qualitiesSrcIssues( ReadView const& sb, DebtDirection prevStepDebtDirection) const { - // Charge a transfer rate when issuing and previous step redeems + // Charge a transfer rate when issuing and previous step redeems - assert(static_cast(this)->verifyPrevStepDebtDirection( - prevStepDebtDirection)); + assert(static_cast(this)->verifyPrevStepDebtDirection( + prevStepDebtDirection)); - std::uint32_t const srcQOut = redeems(prevStepDebtDirection) - ? transferRate(sb, src_).value - : QUALITY_ONE; - auto dstQIn = static_cast(this)->quality( - sb, QualityDirection::in); + std::uint32_t const srcQOut = redeems(prevStepDebtDirection) + ? transferRate(sb, src_).value + : QUALITY_ONE; + auto dstQIn = + static_cast(this)->quality(sb, QualityDirection::in); - if (isLast_ && dstQIn > QUALITY_ONE) - dstQIn = QUALITY_ONE; - return {srcQOut, dstQIn}; + if (isLast_ && dstQIn > QUALITY_ONE) + dstQIn = QUALITY_ONE; + return {srcQOut, dstQIn}; } // Returns srcQOut, dstQIn @@ -813,17 +824,17 @@ DirectStepI::qualities( template std::uint32_t -DirectStepI::lineQualityIn (ReadView const& v) const +DirectStepI::lineQualityIn(ReadView const& v) const { // dst quality in - return static_cast(this)->quality ( - v, QualityDirection::in); + return static_cast(this)->quality(v, QualityDirection::in); } template std::pair, DebtDirection> -DirectStepI::qualityUpperBound(ReadView const& v, DebtDirection prevStepDir) - const +DirectStepI::qualityUpperBound( + ReadView const& v, + DebtDirection prevStepDir) const { auto const dir = this->debtDirection(v, StrandDirection::forward); @@ -840,8 +851,9 @@ DirectStepI::qualityUpperBound(ReadView const& v, DebtDirection prevSt if (isLast_ && dstQIn > QUALITY_ONE) dstQIn = QUALITY_ONE; Issue const iss{currency_, src_}; - return {Quality(getRate(STAmount(iss, srcQOut), STAmount(iss, dstQIn))), - dir}; + return { + Quality(getRate(STAmount(iss, srcQOut), STAmount(iss, dstQIn))), + dir}; } auto const [srcQOut, dstQIn] = redeems(dir) @@ -855,30 +867,31 @@ DirectStepI::qualityUpperBound(ReadView const& v, DebtDirection prevSt // (Input*dstQIn/srcQOut = Output; So rate = srcQOut/dstQIn). Although the // first parameter is called `offerOut`, it should take the `dstQIn` // variable. - return {Quality(getRate(STAmount(iss, dstQIn), STAmount(iss, srcQOut))), - dir}; + return { + Quality(getRate(STAmount(iss, dstQIn), STAmount(iss, srcQOut))), dir}; } template -TER DirectStepI::check (StrandContext const& ctx) const +TER +DirectStepI::check(StrandContext const& ctx) const { // The following checks apply for both payments and offer crossing. if (!src_ || !dst_) { - JLOG (j_.debug()) << "DirectStepI: specified bad account."; + JLOG(j_.debug()) << "DirectStepI: specified bad account."; return temBAD_PATH; } if (src_ == dst_) { - JLOG (j_.debug()) << "DirectStepI: same src and dst."; + JLOG(j_.debug()) << "DirectStepI: same src and dst."; return temBAD_PATH; } - auto const sleSrc = ctx.view.read (keylet::account (src_)); + auto const sleSrc = ctx.view.read(keylet::account(src_)); if (!sleSrc) { - JLOG (j_.warn()) + JLOG(j_.warn()) << "DirectStepI: can't receive IOUs from non-existent issuer: " << src_; return terNO_ACCOUNT; @@ -898,8 +911,8 @@ TER DirectStepI::check (StrandContext const& ctx) const { if (auto prevSrc = ctx.prevStep->directStepSrcAcct()) { - auto const ter = checkNoRipple( - ctx.view, *prevSrc, src_, dst_, currency_, j_); + auto const ter = + checkNoRipple(ctx.view, *prevSrc, src_, dst_, currency_, j_); if (ter != tesSUCCESS) return ter; } @@ -908,15 +921,16 @@ TER DirectStepI::check (StrandContext const& ctx) const Issue const srcIssue{currency_, src_}; Issue const dstIssue{currency_, dst_}; - if (ctx.seenBookOuts.count (srcIssue)) + if (ctx.seenBookOuts.count(srcIssue)) { if (!ctx.prevStep) { - assert(0); // prev seen book without a prev step!?! + assert(0); // prev seen book without a prev step!?! return temBAD_PATH_LOOP; } - // This is OK if the previous step is a book step that outputs this issue + // This is OK if the previous step is a book step that outputs this + // issue if (auto book = ctx.prevStep->bookStepBook()) { if (book->out != srcIssue) @@ -924,43 +938,43 @@ TER DirectStepI::check (StrandContext const& ctx) const } } - if (!ctx.seenDirectIssues[0].insert (srcIssue).second || - !ctx.seenDirectIssues[1].insert (dstIssue).second) + if (!ctx.seenDirectIssues[0].insert(srcIssue).second || + !ctx.seenDirectIssues[1].insert(dstIssue).second) { - JLOG (j_.debug ()) + JLOG(j_.debug()) << "DirectStepI: loop detected: Index: " << ctx.strandSize << ' ' << *this; return temBAD_PATH_LOOP; } } - return static_cast(this)->check (ctx, sleSrc); + return static_cast(this)->check(ctx, sleSrc); } //------------------------------------------------------------------------------ -namespace test -{ +namespace test { // Needed for testing -bool directStepEqual (Step const& step, +bool +directStepEqual( + Step const& step, AccountID const& src, AccountID const& dst, Currency const& currency) { - if (auto ds = - dynamic_cast const*> (&step)) + if (auto ds = dynamic_cast const*>(&step)) { - return ds->src () == src && ds->dst () == dst && - ds->currency () == currency; + return ds->src() == src && ds->dst() == dst && + ds->currency() == currency; } return false; } -} // test +} // namespace test //------------------------------------------------------------------------------ std::pair> -make_DirectStepI ( +make_DirectStepI( StrandContext const& ctx, AccountID const& src, AccountID const& dst, @@ -971,21 +985,21 @@ make_DirectStepI ( if (ctx.offerCrossing) { auto offerCrossingStep = - std::make_unique (ctx, src, dst, c); - ter = offerCrossingStep->check (ctx); - r = std::move (offerCrossingStep); + std::make_unique(ctx, src, dst, c); + ter = offerCrossingStep->check(ctx); + r = std::move(offerCrossingStep); } - else // payment + else // payment { auto paymentStep = - std::make_unique (ctx, src, dst, c); - ter = paymentStep->check (ctx); - r = std::move (paymentStep); + std::make_unique(ctx, src, dst, c); + ter = paymentStep->check(ctx); + r = std::move(paymentStep); } if (ter != tesSUCCESS) return {ter, nullptr}; - return {tesSUCCESS, std::move (r)}; + return {tesSUCCESS, std::move(r)}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/impl/FlatSets.h b/src/ripple/app/paths/impl/FlatSets.h index 0cf5ce8b86..4742e9b515 100644 --- a/src/ripple/app/paths/impl/FlatSets.h +++ b/src/ripple/app/paths/impl/FlatSets.h @@ -26,7 +26,8 @@ namespace ripple { /** Given two flat sets dst and src, compute dst = dst union src - @param dst set to store the resulting union, and also a source of elements for the union + @param dst set to store the resulting union, and also a source of elements + for the union @param src second source of elements for the union */ template diff --git a/src/ripple/app/paths/impl/FlowDebugInfo.h b/src/ripple/app/paths/impl/FlowDebugInfo.h index ecd85f53c0..eeb5625d31 100644 --- a/src/ripple/app/paths/impl/FlowDebugInfo.h +++ b/src/ripple/app/paths/impl/FlowDebugInfo.h @@ -31,12 +31,9 @@ #include #include -namespace ripple -{ -namespace path -{ -namespace detail -{ +namespace ripple { +namespace path { +namespace detail { // Track performance information of a single payment struct FlowDebugInfo { @@ -48,9 +45,9 @@ struct FlowDebugInfo struct PassInfo { - PassInfo () = delete; - PassInfo (bool nativeIn_, bool nativeOut_) - : nativeIn (nativeIn_), nativeOut (nativeOut_) + PassInfo() = delete; + PassInfo(bool nativeIn_, bool nativeOut_) + : nativeIn(nativeIn_), nativeOut(nativeOut_) { } bool const nativeIn; @@ -63,33 +60,34 @@ struct FlowDebugInfo std::vector> liquiditySrcOut; void - reserve (size_t s) + reserve(size_t s) { - in.reserve (s); - out.reserve (s); + in.reserve(s); + out.reserve(s); liquiditySrcIn.reserve(s); liquiditySrcOut.reserve(s); - numActive.reserve (s); + numActive.reserve(s); } size_t - size () const + size() const { - return in.size (); + return in.size(); } void - push_back (EitherAmount const& in_amt, + push_back( + EitherAmount const& in_amt, EitherAmount const& out_amt, std::size_t active) { - in.push_back (in_amt); - out.push_back (out_amt); - numActive.push_back (active); + in.push_back(in_amt); + out.push_back(out_amt); + numActive.push_back(active); } void - pushLiquiditySrc (EitherAmount const& eIn, EitherAmount const& eOut) + pushLiquiditySrc(EitherAmount const& eIn, EitherAmount const& eOut) { assert(!liquiditySrcIn.empty()); liquiditySrcIn.back().push_back(eIn); @@ -101,76 +99,75 @@ struct FlowDebugInfo { auto const s = liquiditySrcIn.size(); size_t const r = !numActive.empty() ? numActive.back() : 16; - liquiditySrcIn.resize(s+1); + liquiditySrcIn.resize(s + 1); liquiditySrcIn.back().reserve(r); - liquiditySrcOut.resize(s+1); + liquiditySrcOut.resize(s + 1); liquiditySrcOut.back().reserve(r); } }; PassInfo passInfo; - FlowDebugInfo () = delete; - FlowDebugInfo (bool nativeIn, bool nativeOut) - : passInfo (nativeIn, nativeOut) + FlowDebugInfo() = delete; + FlowDebugInfo(bool nativeIn, bool nativeOut) : passInfo(nativeIn, nativeOut) { - timePoints.reserve (16); - counts.reserve (16); - passInfo.reserve (64); + timePoints.reserve(16); + counts.reserve(16); + passInfo.reserve(64); } auto - duration (std::string const& tag) const + duration(std::string const& tag) const { - auto i = timePoints.find (tag); - if (i == timePoints.end ()) + auto i = timePoints.find(tag); + if (i == timePoints.end()) { - assert (0); + assert(0); return std::chrono::duration(0); } auto const& t = i->second; - return std::chrono::duration_cast> ( + return std::chrono::duration_cast>( t.second - t.first); } std::size_t - count (std::string const& tag) const + count(std::string const& tag) const { - auto i = counts.find (tag); - if (i == counts.end ()) + auto i = counts.find(tag); + if (i == counts.end()) return 0; return i->second; } // Time the duration of the existence of the result auto - timeBlock (std::string name) + timeBlock(std::string name) { struct Stopper { std::string tag; FlowDebugInfo* info; - Stopper (std::string name, FlowDebugInfo& pi) - : tag (std::move (name)), info (&pi) + Stopper(std::string name, FlowDebugInfo& pi) + : tag(std::move(name)), info(&pi) { - auto const start = FlowDebugInfo::clock::now (); - info->timePoints.emplace (tag, std::make_pair (start, start)); + auto const start = FlowDebugInfo::clock::now(); + info->timePoints.emplace(tag, std::make_pair(start, start)); } - ~Stopper () + ~Stopper() { - auto const end = FlowDebugInfo::clock::now (); + auto const end = FlowDebugInfo::clock::now(); info->timePoints[tag].second = end; } Stopper(Stopper&&) = default; }; - return Stopper (std::move (name), *this); + return Stopper(std::move(name), *this); } void - inc (std::string const& tag) + inc(std::string const& tag) { - auto i = counts.find (tag); - if (i == counts.end ()) + auto i = counts.find(tag); + if (i == counts.end()) { counts[tag] = 1; } @@ -178,165 +175,173 @@ struct FlowDebugInfo } void - setCount (std::string const& tag, std::size_t c) + setCount(std::string const& tag, std::size_t c) { counts[tag] = c; } std::size_t - passCount () const + passCount() const { - return passInfo.size (); + return passInfo.size(); } void - pushPass (EitherAmount const& in, + pushPass( + EitherAmount const& in, EitherAmount const& out, std::size_t activeStrands) { - passInfo.push_back (in, out, activeStrands); + passInfo.push_back(in, out, activeStrands); } void - pushLiquiditySrc (EitherAmount const& in, EitherAmount const& out) + pushLiquiditySrc(EitherAmount const& in, EitherAmount const& out) { - passInfo.pushLiquiditySrc (in, out); + passInfo.pushLiquiditySrc(in, out); } void - newLiquidityPass () + newLiquidityPass() { - passInfo.newLiquidityPass (); + passInfo.newLiquidityPass(); } std::string - to_string (bool writePassInfo) const + to_string(bool writePassInfo) const { std::ostringstream ostr; - auto const d = duration ("main"); + auto const d = duration("main"); - ostr << "duration: " << d.count () << ", pass_count: " << passCount (); + ostr << "duration: " << d.count() << ", pass_count: " << passCount(); if (writePassInfo) { - auto write_list = [&ostr](auto const& vals, auto&& fun, char delim=';') { - ostr << '['; - if (!vals.empty ()) - { - ostr << fun (vals[0]); - for (size_t i = 1, e = vals.size (); i < e; ++i) - ostr << delim << fun (vals[i]); - } - ostr << ']'; - }; - auto writeXrpAmtList = [&write_list]( - std::vector const& amts, char delim=';') { - auto get_val = [](EitherAmount const& a) -> std::string { - return ripple::to_string (a.xrp); + auto write_list = + [&ostr](auto const& vals, auto&& fun, char delim = ';') { + ostr << '['; + if (!vals.empty()) + { + ostr << fun(vals[0]); + for (size_t i = 1, e = vals.size(); i < e; ++i) + ostr << delim << fun(vals[i]); + } + ostr << ']'; }; - write_list (amts, get_val, delim); + auto writeXrpAmtList = [&write_list]( + std::vector const& amts, + char delim = ';') { + auto get_val = [](EitherAmount const& a) -> std::string { + return ripple::to_string(a.xrp); + }; + write_list(amts, get_val, delim); }; auto writeIouAmtList = [&write_list]( - std::vector const& amts, char delim=';') { + std::vector const& amts, + char delim = ';') { auto get_val = [](EitherAmount const& a) -> std::string { - return ripple::to_string (a.iou); + return ripple::to_string(a.iou); }; - write_list (amts, get_val, delim); + write_list(amts, get_val, delim); }; auto writeIntList = [&write_list]( - std::vector const& vals, char delim=';') { - auto get_val = []( - size_t const& v) -> size_t const& { return v; }; - write_list (vals, get_val); + std::vector const& vals, + char delim = ';') { + auto get_val = [](size_t const& v) -> size_t const& { + return v; + }; + write_list(vals, get_val); }; - auto writeNestedIouAmtList = [&ostr, &writeIouAmtList]( - std::vector> const& amts) { - ostr << '['; - if (!amts.empty ()) - { - writeIouAmtList(amts[0], '|'); - for (size_t i = 1, e = amts.size (); i < e; ++i) + auto writeNestedIouAmtList = + [&ostr, &writeIouAmtList]( + std::vector> const& amts) { + ostr << '['; + if (!amts.empty()) { - ostr << ';'; - writeIouAmtList(amts[i], '|'); + writeIouAmtList(amts[0], '|'); + for (size_t i = 1, e = amts.size(); i < e; ++i) + { + ostr << ';'; + writeIouAmtList(amts[i], '|'); + } } - } - ostr << ']'; - }; - auto writeNestedXrpAmtList = [&ostr, &writeXrpAmtList]( - std::vector> const& amts) { - ostr << '['; - if (!amts.empty ()) - { - writeXrpAmtList(amts[0], '|'); - for (size_t i = 1, e = amts.size (); i < e; ++i) + ostr << ']'; + }; + auto writeNestedXrpAmtList = + [&ostr, &writeXrpAmtList]( + std::vector> const& amts) { + ostr << '['; + if (!amts.empty()) { - ostr << ';'; - writeXrpAmtList(amts[i], '|'); + writeXrpAmtList(amts[0], '|'); + for (size_t i = 1, e = amts.size(); i < e; ++i) + { + ostr << ';'; + writeXrpAmtList(amts[i], '|'); + } } - } - ostr << ']'; - }; + ostr << ']'; + }; ostr << ", in_pass: "; if (passInfo.nativeIn) - writeXrpAmtList (passInfo.in); + writeXrpAmtList(passInfo.in); else - writeIouAmtList (passInfo.in); + writeIouAmtList(passInfo.in); ostr << ", out_pass: "; if (passInfo.nativeOut) - writeXrpAmtList (passInfo.out); + writeXrpAmtList(passInfo.out); else - writeIouAmtList (passInfo.out); + writeIouAmtList(passInfo.out); ostr << ", num_active: "; - writeIntList (passInfo.numActive); - if (!passInfo.liquiditySrcIn.empty () && - !passInfo.liquiditySrcIn.back ().empty ()) + writeIntList(passInfo.numActive); + if (!passInfo.liquiditySrcIn.empty() && + !passInfo.liquiditySrcIn.back().empty()) { ostr << ", l_src_in: "; if (passInfo.nativeIn) - writeNestedXrpAmtList (passInfo.liquiditySrcIn); + writeNestedXrpAmtList(passInfo.liquiditySrcIn); else - writeNestedIouAmtList (passInfo.liquiditySrcIn); + writeNestedIouAmtList(passInfo.liquiditySrcIn); ostr << ", l_src_out: "; if (passInfo.nativeOut) - writeNestedXrpAmtList (passInfo.liquiditySrcOut); + writeNestedXrpAmtList(passInfo.liquiditySrcOut); else - writeNestedIouAmtList (passInfo.liquiditySrcOut); + writeNestedIouAmtList(passInfo.liquiditySrcOut); } } - return ostr.str (); + return ostr.str(); } }; -inline -void -writeDiffElement (std::ostringstream& ostr, +inline void +writeDiffElement( + std::ostringstream& ostr, std::pair, STAmount> const& elem) { using namespace std; auto const k = elem.first; auto const v = elem.second; - ostr << '[' << get<0> (k) << '|' << get<1> (k) << '|' << get<2> (k) << '|' - << v << ']'; + ostr << '[' << get<0>(k) << '|' << get<1>(k) << '|' << get<2>(k) << '|' << v + << ']'; }; -template +template void -writeDiffs (std::ostringstream& ostr, Iter begin, Iter end) +writeDiffs(std::ostringstream& ostr, Iter begin, Iter end) { ostr << '['; if (begin != end) { - writeDiffElement (ostr, *begin); + writeDiffElement(ostr, *begin); ++begin; } for (; begin != end; ++begin) { ostr << ';'; - writeDiffElement (ostr, *begin); + writeDiffElement(ostr, *begin); } ostr << ']'; }; @@ -345,29 +350,27 @@ using BalanceDiffs = std::pair< std::map, STAmount>, XRPAmount>; -inline -BalanceDiffs +inline BalanceDiffs balanceDiffs(PaymentSandbox const& sb, ReadView const& rv) { - return {sb.balanceChanges (rv), sb.xrpDestroyed ()}; + return {sb.balanceChanges(rv), sb.xrpDestroyed()}; } -inline -std::string -balanceDiffsToString (boost::optional const& bd) +inline std::string +balanceDiffsToString(boost::optional const& bd) { if (!bd) return std::string{}; auto const& diffs = bd->first; auto const& xrpDestroyed = bd->second; std::ostringstream ostr; - ostr << ", xrpDestroyed: " << to_string (xrpDestroyed); + ostr << ", xrpDestroyed: " << to_string(xrpDestroyed); ostr << ", balanceDiffs: "; - writeDiffs (ostr, diffs.begin (), diffs.end ()); - return ostr.str (); + writeDiffs(ostr, diffs.begin(), diffs.end()); + return ostr.str(); }; -} // detail -} // path -} // ripple +} // namespace detail +} // namespace path +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/PaySteps.cpp b/src/ripple/app/paths/impl/PaySteps.cpp index 6d87c33a23..4a49f84b34 100644 --- a/src/ripple/app/paths/impl/PaySteps.cpp +++ b/src/ripple/app/paths/impl/PaySteps.cpp @@ -18,9 +18,9 @@ //============================================================================== #include -#include #include #include +#include #include #include #include @@ -32,46 +32,46 @@ namespace ripple { // Check equal with tolerance -bool checkNear (IOUAmount const& expected, IOUAmount const& actual) +bool +checkNear(IOUAmount const& expected, IOUAmount const& actual) { double const ratTol = 0.001; - if (abs (expected.exponent () - actual.exponent ()) > 1) + if (abs(expected.exponent() - actual.exponent()) > 1) return false; - if (actual.exponent () < -20) + if (actual.exponent() < -20) return true; - auto const a = (expected.exponent () < actual.exponent ()) - ? expected.mantissa () / 10 - : expected.mantissa (); - auto const b = (actual.exponent () < expected.exponent ()) - ? actual.mantissa () / 10 - : actual.mantissa (); + auto const a = (expected.exponent() < actual.exponent()) + ? expected.mantissa() / 10 + : expected.mantissa(); + auto const b = (actual.exponent() < expected.exponent()) + ? actual.mantissa() / 10 + : actual.mantissa(); if (a == b) return true; - double const diff = std::abs (a - b); - auto const r = diff / std::max (std::abs (a), std::abs (b)); + double const diff = std::abs(a - b); + auto const r = diff / std::max(std::abs(a), std::abs(b)); return r <= ratTol; }; -bool checkNear (XRPAmount const& expected, XRPAmount const& actual) +bool +checkNear(XRPAmount const& expected, XRPAmount const& actual) { return expected == actual; }; -static -bool isXRPAccount (STPathElement const& pe) +static bool +isXRPAccount(STPathElement const& pe) { - if (pe.getNodeType () != STPathElement::typeAccount) + if (pe.getNodeType() != STPathElement::typeAccount) return false; - return isXRP (pe.getAccountID ()); + return isXRP(pe.getAccountID()); }; - -static -std::pair> -toStep ( +static std::pair> +toStep( StrandContext const& ctx, STPathElement const* e1, STPathElement const* e2, @@ -79,59 +79,60 @@ toStep ( { auto& j = ctx.j; - if (ctx.isFirst && e1->isAccount () && - (e1->getNodeType () & STPathElement::typeCurrency) && - isXRP (e1->getCurrency ())) + if (ctx.isFirst && e1->isAccount() && + (e1->getNodeType() & STPathElement::typeCurrency) && + isXRP(e1->getCurrency())) { - return make_XRPEndpointStep (ctx, e1->getAccountID ()); + return make_XRPEndpointStep(ctx, e1->getAccountID()); } - if (ctx.isLast && isXRPAccount (*e1) && e2->isAccount()) - return make_XRPEndpointStep (ctx, e2->getAccountID()); + if (ctx.isLast && isXRPAccount(*e1) && e2->isAccount()) + return make_XRPEndpointStep(ctx, e2->getAccountID()); if (e1->isAccount() && e2->isAccount()) { - return make_DirectStepI (ctx, e1->getAccountID (), - e2->getAccountID (), curIssue.currency); + return make_DirectStepI( + ctx, e1->getAccountID(), e2->getAccountID(), curIssue.currency); } if (e1->isOffer() && e2->isAccount()) { // should already be taken care of - JLOG (j.error()) + JLOG(j.error()) << "Found offer/account payment step. Aborting payment strand."; - assert (0); + assert(0); return {temBAD_PATH, std::unique_ptr{}}; } - assert ((e2->getNodeType () & STPathElement::typeCurrency) || - (e2->getNodeType () & STPathElement::typeIssuer)); - auto const outCurrency = e2->getNodeType () & STPathElement::typeCurrency - ? e2->getCurrency () + assert( + (e2->getNodeType() & STPathElement::typeCurrency) || + (e2->getNodeType() & STPathElement::typeIssuer)); + auto const outCurrency = e2->getNodeType() & STPathElement::typeCurrency + ? e2->getCurrency() : curIssue.currency; - auto const outIssuer = e2->getNodeType () & STPathElement::typeIssuer - ? e2->getIssuerID () + auto const outIssuer = e2->getNodeType() & STPathElement::typeIssuer + ? e2->getIssuerID() : curIssue.account; - if (isXRP (curIssue.currency) && isXRP (outCurrency)) + if (isXRP(curIssue.currency) && isXRP(outCurrency)) { - JLOG (j.info()) << "Found xrp/xrp offer payment step"; + JLOG(j.info()) << "Found xrp/xrp offer payment step"; return {temBAD_PATH, std::unique_ptr{}}; } - assert (e2->isOffer ()); + assert(e2->isOffer()); - if (isXRP (outCurrency)) - return make_BookStepIX (ctx, curIssue); + if (isXRP(outCurrency)) + return make_BookStepIX(ctx, curIssue); - if (isXRP (curIssue.currency)) - return make_BookStepXI (ctx, {outCurrency, outIssuer}); + if (isXRP(curIssue.currency)) + return make_BookStepXI(ctx, {outCurrency, outIssuer}); - return make_BookStepII (ctx, curIssue, {outCurrency, outIssuer}); + return make_BookStepII(ctx, curIssue, {outCurrency, outIssuer}); } std::pair -toStrand ( +toStrand( ReadView const& view, AccountID const& src, AccountID const& dst, @@ -143,13 +144,12 @@ toStrand ( bool offerCrossing, beast::Journal j) { - if (isXRP(src) || isXRP(dst) || - !isConsistent(deliver) || (sendMaxIssue && !isConsistent(*sendMaxIssue))) + if (isXRP(src) || isXRP(dst) || !isConsistent(deliver) || + (sendMaxIssue && !isConsistent(*sendMaxIssue))) return {temBAD_PATH, Strand{}}; if ((sendMaxIssue && sendMaxIssue->account == noAccount()) || - (src == noAccount()) || - (dst == noAccount()) || + (src == noAccount()) || (dst == noAccount()) || (deliver.account == noAccount())) return {temBAD_PATH, Strand{}}; @@ -184,18 +184,16 @@ toStrand ( return {temBAD_PATH, Strand{}}; } - Issue curIssue = [&] - { + Issue curIssue = [&] { auto const& currency = sendMaxIssue ? sendMaxIssue->currency : deliver.currency; - if (isXRP (currency)) - return xrpIssue (); + if (isXRP(currency)) + return xrpIssue(); return Issue{currency, src}; }(); - auto hasCurrency = [](STPathElement const pe) - { - return pe.getNodeType () & STPathElement::typeCurrency; + auto hasCurrency = [](STPathElement const pe) { + return pe.getNodeType() & STPathElement::typeCurrency; }; std::vector normPath; @@ -210,7 +208,8 @@ toStrand ( (path.empty() || !path[0].isAccount() || path[0].getAccountID() != sendMaxIssue->account)) { - normPath.emplace_back(sendMaxIssue->account, boost::none, boost::none); + normPath.emplace_back( + sendMaxIssue->account, boost::none, boost::none); } for (auto const& i : path) @@ -220,11 +219,10 @@ toStrand ( // Note that for offer crossing (only) we do use an offer book // even if all that is changing is the Issue.account. STPathElement const& lastCurrency = - *std::find_if (normPath.rbegin(), normPath.rend(), - hasCurrency); + *std::find_if(normPath.rbegin(), normPath.rend(), hasCurrency); if ((lastCurrency.getCurrency() != deliver.currency) || (offerCrossing && - lastCurrency.getIssuerID() != deliver.account)) + lastCurrency.getIssuerID() != deliver.account)) { normPath.emplace_back( boost::none, deliver.currency, deliver.account); @@ -248,92 +246,114 @@ toStrand ( if (normPath.size() < 2) return {temBAD_PATH, Strand{}}; - auto const strandSrc = normPath.front().getAccountID (); - auto const strandDst = normPath.back().getAccountID (); + auto const strandSrc = normPath.front().getAccountID(); + auto const strandDst = normPath.back().getAccountID(); bool const isDefaultPath = path.empty(); Strand result; - result.reserve (2 * normPath.size ()); + result.reserve(2 * normPath.size()); /* A strand may not include the same account node more than once in the same currency. In a direct step, an account will show up - at most twice: once as a src and once as a dst (hence the two element array). - The strandSrc and strandDst will only show up once each. + at most twice: once as a src and once as a dst (hence the two element + array). The strandSrc and strandDst will only show up once each. */ std::array, 2> seenDirectIssues; // A strand may not include the same offer book more than once boost::container::flat_set seenBookOuts; - seenDirectIssues[0].reserve (normPath.size()); - seenDirectIssues[1].reserve (normPath.size()); - seenBookOuts.reserve (normPath.size()); - auto ctx = [&](bool isLast = false) - { - return StrandContext{view, result, strandSrc, strandDst, deliver, - limitQuality, isLast, ownerPaysTransferFee, offerCrossing, - isDefaultPath, seenDirectIssues, seenBookOuts, j}; + seenDirectIssues[0].reserve(normPath.size()); + seenDirectIssues[1].reserve(normPath.size()); + seenBookOuts.reserve(normPath.size()); + auto ctx = [&](bool isLast = false) { + return StrandContext{ + view, + result, + strandSrc, + strandDst, + deliver, + limitQuality, + isLast, + ownerPaysTransferFee, + offerCrossing, + isDefaultPath, + seenDirectIssues, + seenBookOuts, + j}; }; - for (std::size_t i = 0; i < normPath.size () - 1; ++i) + for (std::size_t i = 0; i < normPath.size() - 1; ++i) { /* Iterate through the path elements considering them in pairs. The first element of the pair is `cur` and the second element is - `next`. When an offer is one of the pairs, the step created will be for - `next`. This means when `cur` is an offer and `next` is an - account then no step is created, as a step has already been created for - that offer. + `next`. When an offer is one of the pairs, the step created will be + for `next`. This means when `cur` is an offer and `next` is an + account then no step is created, as a step has already been created + for that offer. */ boost::optional impliedPE; auto cur = &normPath[i]; auto const next = &normPath[i + 1]; if (cur->isAccount()) - curIssue.account = cur->getAccountID (); + curIssue.account = cur->getAccountID(); else if (cur->hasIssuer()) - curIssue.account = cur->getIssuerID (); + curIssue.account = cur->getIssuerID(); if (cur->hasCurrency()) { - curIssue.currency = cur->getCurrency (); + curIssue.currency = cur->getCurrency(); if (isXRP(curIssue.currency)) curIssue.account = xrpAccount(); } if (cur->isAccount() && next->isAccount()) { - if (!isXRP (curIssue.currency) && - curIssue.account != cur->getAccountID () && - curIssue.account != next->getAccountID ()) + if (!isXRP(curIssue.currency) && + curIssue.account != cur->getAccountID() && + curIssue.account != next->getAccountID()) { - JLOG (j.trace()) << "Inserting implied account"; - auto msr = make_DirectStepI (ctx(), cur->getAccountID (), - curIssue.account, curIssue.currency); + JLOG(j.trace()) << "Inserting implied account"; + auto msr = make_DirectStepI( + ctx(), + cur->getAccountID(), + curIssue.account, + curIssue.currency); if (msr.first != tesSUCCESS) return {msr.first, Strand{}}; - result.push_back (std::move (msr.second)); - impliedPE.emplace(STPathElement::typeAccount, - curIssue.account, xrpCurrency(), xrpAccount()); + result.push_back(std::move(msr.second)); + impliedPE.emplace( + STPathElement::typeAccount, + curIssue.account, + xrpCurrency(), + xrpAccount()); cur = &*impliedPE; } } else if (cur->isAccount() && next->isOffer()) { - if (curIssue.account != cur->getAccountID ()) + if (curIssue.account != cur->getAccountID()) { - JLOG (j.trace()) << "Inserting implied account before offer"; - auto msr = make_DirectStepI (ctx(), cur->getAccountID (), - curIssue.account, curIssue.currency); + JLOG(j.trace()) << "Inserting implied account before offer"; + auto msr = make_DirectStepI( + ctx(), + cur->getAccountID(), + curIssue.account, + curIssue.currency); if (msr.first != tesSUCCESS) return {msr.first, Strand{}}; - result.push_back (std::move (msr.second)); - impliedPE.emplace(STPathElement::typeAccount, - curIssue.account, xrpCurrency(), xrpAccount()); + result.push_back(std::move(msr.second)); + impliedPE.emplace( + STPathElement::typeAccount, + curIssue.account, + xrpCurrency(), + xrpAccount()); cur = &*impliedPE; } } else if (cur->isOffer() && next->isAccount()) { - if (curIssue.account != next->getAccountID () && - !isXRP (next->getAccountID ())) + if (curIssue.account != next->getAccountID() && + !isXRP(next->getAccountID())) { if (isXRP(curIssue)) { @@ -342,7 +362,8 @@ toStrand ( else { // Last step. insert xrp endpoint step - auto msr = make_XRPEndpointStep (ctx(), next->getAccountID()); + auto msr = + make_XRPEndpointStep(ctx(), next->getAccountID()); if (msr.first != tesSUCCESS) return {msr.first, Strand{}}; result.push_back(std::move(msr.second)); @@ -351,8 +372,11 @@ toStrand ( else { JLOG(j.trace()) << "Inserting implied account after offer"; - auto msr = make_DirectStepI(ctx(), - curIssue.account, next->getAccountID(), curIssue.currency); + auto msr = make_DirectStepI( + ctx(), + curIssue.account, + next->getAccountID(), + curIssue.currency); if (msr.first != tesSUCCESS) return {msr.first, Strand{}}; result.push_back(std::move(msr.second)); @@ -361,21 +385,21 @@ toStrand ( continue; } - if (!next->isOffer() && - next->hasCurrency() && next->getCurrency () != curIssue.currency) + if (!next->isOffer() && next->hasCurrency() && + next->getCurrency() != curIssue.currency) { // Should never happen assert(0); return {temBAD_PATH, Strand{}}; } - auto s = - toStep (ctx (/*isLast*/ i == normPath.size () - 2), cur, next, curIssue); + auto s = toStep( + ctx(/*isLast*/ i == normPath.size() - 2), cur, next, curIssue); if (s.first == tesSUCCESS) - result.emplace_back (std::move (s.second)); + result.emplace_back(std::move(s.second)); else { - JLOG (j.debug()) << "toStep failed: " << s.first; + JLOG(j.debug()) << "toStep failed: " << s.first; return {s.first, Strand{}}; } } @@ -423,24 +447,23 @@ toStrand ( return false; if (curIss.currency != deliver.currency) return false; - if (curIss.account != deliver.account && - curIss.account != dst) + if (curIss.account != deliver.account && curIss.account != dst) return false; return true; }; if (!checkStrand()) { - JLOG (j.warn()) << "Flow check strand failed"; + JLOG(j.warn()) << "Flow check strand failed"; assert(0); return {temBAD_PATH, Strand{}}; } - return {tesSUCCESS, std::move (result)}; + return {tesSUCCESS, std::move(result)}; } std::pair> -toStrands ( +toStrands( ReadView const& view, AccountID const& src, AccountID const& dst, @@ -454,69 +477,88 @@ toStrands ( beast::Journal j) { std::vector result; - result.reserve (1 + paths.size ()); + result.reserve(1 + paths.size()); // Insert the strand into result if it is not already part of the vector - auto insert = [&](Strand s) - { + auto insert = [&](Strand s) { bool const hasStrand = - std::find (result.begin(), result.end(), s) != result.end (); + std::find(result.begin(), result.end(), s) != result.end(); if (!hasStrand) - result.emplace_back (std::move (s)); + result.emplace_back(std::move(s)); }; if (addDefaultPath) { - auto sp = toStrand (view, src, dst, deliver, limitQuality, - sendMax, STPath(), ownerPaysTransferFee, offerCrossing, j); + auto sp = toStrand( + view, + src, + dst, + deliver, + limitQuality, + sendMax, + STPath(), + ownerPaysTransferFee, + offerCrossing, + j); auto const ter = sp.first; auto& strand = sp.second; if (ter != tesSUCCESS) { - JLOG (j.trace()) << "failed to add default path"; - if (isTemMalformed (ter) || paths.empty ()) { + JLOG(j.trace()) << "failed to add default path"; + if (isTemMalformed(ter) || paths.empty()) + { return {ter, std::vector{}}; } } - else if (strand.empty ()) + else if (strand.empty()) { - JLOG (j.trace()) << "toStrand failed"; - Throw (tefEXCEPTION, "toStrand returned tes & empty strand"); + JLOG(j.trace()) << "toStrand failed"; + Throw( + tefEXCEPTION, "toStrand returned tes & empty strand"); } else { insert(std::move(strand)); } } - else if (paths.empty ()) + else if (paths.empty()) { - JLOG (j.debug()) - << "Flow: Invalid transaction: No paths and direct ripple not allowed."; + JLOG(j.debug()) << "Flow: Invalid transaction: No paths and direct " + "ripple not allowed."; return {temRIPPLE_EMPTY, std::vector{}}; } TER lastFailTer = tesSUCCESS; for (auto const& p : paths) { - auto sp = toStrand (view, src, dst, deliver, - limitQuality, sendMax, p, ownerPaysTransferFee, offerCrossing, j); + auto sp = toStrand( + view, + src, + dst, + deliver, + limitQuality, + sendMax, + p, + ownerPaysTransferFee, + offerCrossing, + j); auto ter = sp.first; auto& strand = sp.second; if (ter != tesSUCCESS) { lastFailTer = ter; - JLOG (j.trace()) - << "failed to add path: ter: " << ter - << "path: " << p.getJson(JsonOptions::none); - if (isTemMalformed (ter)) + JLOG(j.trace()) << "failed to add path: ter: " << ter + << "path: " << p.getJson(JsonOptions::none); + if (isTemMalformed(ter)) return {ter, std::vector{}}; } - else if (strand.empty ()) + else if (strand.empty()) { - JLOG (j.trace()) << "toStrand failed"; - Throw (tefEXCEPTION, "toStrand returned tes & empty strand"); + JLOG(j.trace()) << "toStrand failed"; + Throw( + tefEXCEPTION, "toStrand returned tes & empty strand"); } else { @@ -524,13 +566,13 @@ toStrands ( } } - if (result.empty ()) - return {lastFailTer, std::move (result)}; + if (result.empty()) + return {lastFailTer, std::move(result)}; - return {tesSUCCESS, std::move (result)}; + return {tesSUCCESS, std::move(result)}; } -StrandContext::StrandContext ( +StrandContext::StrandContext( ReadView const& view_, std::vector> const& strand_, // A strand may not include an inner node that @@ -546,47 +588,43 @@ StrandContext::StrandContext ( std::array, 2>& seenDirectIssues_, boost::container::flat_set& seenBookOuts_, beast::Journal j_) - : view (view_) - , strandSrc (strandSrc_) - , strandDst (strandDst_) - , strandDeliver (strandDeliver_) - , limitQuality (limitQuality_) - , isFirst (strand_.empty ()) - , isLast (isLast_) - , ownerPaysTransferFee (ownerPaysTransferFee_) - , offerCrossing (offerCrossing_) - , isDefaultPath (isDefaultPath_) - , strandSize (strand_.size ()) - , prevStep (!strand_.empty () ? strand_.back ().get () - : nullptr) - , seenDirectIssues(seenDirectIssues_) - , seenBookOuts(seenBookOuts_) - , j (j_) + : view(view_) + , strandSrc(strandSrc_) + , strandDst(strandDst_) + , strandDeliver(strandDeliver_) + , limitQuality(limitQuality_) + , isFirst(strand_.empty()) + , isLast(isLast_) + , ownerPaysTransferFee(ownerPaysTransferFee_) + , offerCrossing(offerCrossing_) + , isDefaultPath(isDefaultPath_) + , strandSize(strand_.size()) + , prevStep(!strand_.empty() ? strand_.back().get() : nullptr) + , seenDirectIssues(seenDirectIssues_) + , seenBookOuts(seenBookOuts_) + , j(j_) { } -template +template bool isDirectXrpToXrp(Strand const& strand) { return false; } -template<> +template <> bool -isDirectXrpToXrp (Strand const& strand) +isDirectXrpToXrp(Strand const& strand) { - return (strand.size () == 2); + return (strand.size() == 2); } -template -bool -isDirectXrpToXrp (Strand const& strand); -template -bool -isDirectXrpToXrp (Strand const& strand); -template -bool -isDirectXrpToXrp (Strand const& strand); +template bool +isDirectXrpToXrp(Strand const& strand); +template bool +isDirectXrpToXrp(Strand const& strand); +template bool +isDirectXrpToXrp(Strand const& strand); -} // ripple +} // namespace ripple diff --git a/src/ripple/app/paths/impl/StepChecks.h b/src/ripple/app/paths/impl/StepChecks.h index 6489a416d1..9d8664a8dc 100644 --- a/src/ripple/app/paths/impl/StepChecks.h +++ b/src/ripple/app/paths/impl/StepChecks.h @@ -28,28 +28,27 @@ namespace ripple { -inline -TER -checkFreeze ( +inline TER +checkFreeze( ReadView const& view, AccountID const& src, AccountID const& dst, Currency const& currency) { - assert (src != dst); + assert(src != dst); // check freeze - if (auto sle = view.read (keylet::account (dst))) + if (auto sle = view.read(keylet::account(dst))) { - if (sle->isFlag (lsfGlobalFreeze)) + if (sle->isFlag(lsfGlobalFreeze)) { return terNO_LINE; } } - if (auto sle = view.read (keylet::line (src, dst, currency))) + if (auto sle = view.read(keylet::line(src, dst, currency))) { - if (sle->isFlag ((dst > src) ? lsfHighFreeze : lsfLowFreeze)) + if (sle->isFlag((dst > src) ? lsfHighFreeze : lsfLowFreeze)) { return terNO_LINE; } @@ -58,9 +57,8 @@ checkFreeze ( return tesSUCCESS; } -inline -TER -checkNoRipple ( +inline TER +checkNoRipple( ReadView const& view, AccountID const& prev, AccountID const& cur, @@ -70,25 +68,23 @@ checkNoRipple ( beast::Journal j) { // fetch the ripple lines into and out of this node - auto sleIn = view.read (keylet::line (prev, cur, currency)); - auto sleOut = view.read (keylet::line (cur, next, currency)); + auto sleIn = view.read(keylet::line(prev, cur, currency)); + auto sleOut = view.read(keylet::line(cur, next, currency)); if (!sleIn || !sleOut) return terNO_LINE; - if ((*sleIn)[sfFlags] & - ((cur > prev) ? lsfHighNoRipple : lsfLowNoRipple) && - (*sleOut)[sfFlags] & - ((cur > next) ? lsfHighNoRipple : lsfLowNoRipple)) + if ((*sleIn)[sfFlags] & ((cur > prev) ? lsfHighNoRipple : lsfLowNoRipple) && + (*sleOut)[sfFlags] & ((cur > next) ? lsfHighNoRipple : lsfLowNoRipple)) { - JLOG (j.info()) << "Path violates noRipple constraint between " << prev - << ", " << cur << " and " << next; + JLOG(j.info()) << "Path violates noRipple constraint between " << prev + << ", " << cur << " and " << next; return terNO_RIPPLE; } return tesSUCCESS; } -} +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/Steps.h b/src/ripple/app/paths/impl/Steps.h index 55bbcc5a41..8860ddbc9d 100644 --- a/src/ripple/app/paths/impl/Steps.h +++ b/src/ripple/app/paths/impl/Steps.h @@ -38,15 +38,13 @@ enum class DebtDirection { issues, redeems }; enum class QualityDirection { in, out }; enum class StrandDirection { forward, reverse }; -inline -bool +inline bool redeems(DebtDirection dir) { return dir == DebtDirection::redeems; } -inline -bool +inline bool issues(DebtDirection dir) { return dir == DebtDirection::issues; @@ -90,14 +88,13 @@ public: @param sb view with the strand's state of balances and offers @param afView view the the state of balances before the strand runs this determines if an offer becomes unfunded or is found unfunded - @param ofrsToRm offers found unfunded or in an error state are added to this collection + @param ofrsToRm offers found unfunded or in an error state are added to + this collection @param out requested step output @return actual step input and output */ - virtual - std::pair - rev ( - PaymentSandbox& sb, + virtual std::pair + rev(PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, EitherAmount const& out) = 0; @@ -109,14 +106,13 @@ public: @param sb view with the strand's state of balances and offers @param afView view the the state of balances before the strand runs this determines if an offer becomes unfunded or is found unfunded - @param ofrsToRm offers found unfunded or in an error state are added to this collection + @param ofrsToRm offers found unfunded or in an error state are added to + this collection @param in requested step input @return actual step input and output */ - virtual - std::pair - fwd ( - PaymentSandbox& sb, + virtual std::pair + fwd(PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, EitherAmount const& in) = 0; @@ -125,53 +121,50 @@ public: Amount of currency computed coming into the Step the last time the step ran in reverse. */ - virtual - boost::optional - cachedIn () const = 0; + virtual boost::optional + cachedIn() const = 0; /** Amount of currency computed coming out of the Step the last time the step ran in reverse. */ - virtual - boost::optional - cachedOut () const = 0; + virtual boost::optional + cachedOut() const = 0; /** If this step is DirectStepI (IOU->IOU direct step), return the src account. This is needed for checkNoRipple. */ virtual boost::optional - directStepSrcAcct () const + directStepSrcAcct() const { return boost::none; } // for debugging. Return the src and dst accounts for a direct step // For XRP endpoints, one of src or dst will be the root account - virtual boost::optional> - directStepAccts () const + virtual boost::optional> + directStepAccts() const { return boost::none; } /** - If this step is a DirectStepI and the src redeems to the dst, return true, - otherwise return false. - If this step is a BookStep, return false if the owner pays the transfer fee, - otherwise return true. + If this step is a DirectStepI and the src redeems to the dst, return + true, otherwise return false. If this step is a BookStep, return false if + the owner pays the transfer fee, otherwise return true. @param sb view with the strand's state of balances and offers @param dir reverse -> called from rev(); forward -> called from fwd(). */ virtual DebtDirection - debtDirection (ReadView const& sb, StrandDirection dir) const = 0; + debtDirection(ReadView const& sb, StrandDirection dir) const = 0; /** If this step is a DirectStepI, return the quality in of the dst account. */ virtual std::uint32_t - lineQualityIn (ReadView const&) const + lineQualityIn(ReadView const&) const { return QUALITY_ONE; } @@ -199,7 +192,7 @@ public: If this step is a BookStep, return the book. */ virtual boost::optional - bookStepBook () const + bookStepBook() const { return boost::none; } @@ -207,35 +200,31 @@ public: /** Check if amount is zero */ - virtual - bool - isZero (EitherAmount const& out) const = 0; + virtual bool + isZero(EitherAmount const& out) const = 0; /** Return true if the step should be considered inactive. A strand that has additional liquidity may be marked inactive if a step has consumed too many offers. */ - virtual - bool inactive() const{ + virtual bool + inactive() const + { return false; } /** Return true if Out of lhs == Out of rhs. */ - virtual - bool - equalOut ( - EitherAmount const& lhs, - EitherAmount const& rhs) const = 0; + virtual bool + equalOut(EitherAmount const& lhs, EitherAmount const& rhs) const = 0; /** Return true if In of lhs == In of rhs. */ - virtual bool equalIn ( - EitherAmount const& lhs, - EitherAmount const& rhs) const = 0; + virtual bool + equalIn(EitherAmount const& lhs, EitherAmount const& rhs) const = 0; /** Check that the step can correctly execute in the forward direction @@ -244,14 +233,11 @@ public: @param afView view the the state of balances before the strand runs this determines if an offer becomes unfunded or is found unfunded @param in requested step input - @return first element is true if step is valid, second element is out amount + @return first element is true if step is valid, second element is out + amount */ - virtual - std::pair - validFwd ( - PaymentSandbox& sb, - ApplyView& afView, - EitherAmount const& in) = 0; + virtual std::pair + validFwd(PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) = 0; /** Return true if lhs == rhs. @@ -259,9 +245,10 @@ public: @param rhs Step to compare. @return true if lhs == rhs. */ - friend bool operator==(Step const& lhs, Step const& rhs) + friend bool + operator==(Step const& lhs, Step const& rhs) { - return lhs.equal (rhs); + return lhs.equal(rhs); } /** Return true if lhs != rhs. @@ -270,28 +257,26 @@ public: @param rhs Step to compare. @return true if lhs != rhs. */ - friend bool operator!=(Step const& lhs, Step const& rhs) + friend bool + operator!=(Step const& lhs, Step const& rhs) { - return ! (lhs == rhs); + return !(lhs == rhs); } /** Streaming operator for a Step. */ - friend - std::ostream& - operator << ( - std::ostream& stream, - Step const& step) + friend std::ostream& + operator<<(std::ostream& stream, Step const& step) { - stream << step.logString (); + stream << step.logString(); return stream; } private: - virtual - std::string - logString () const = 0; + virtual std::string + logString() const = 0; - virtual bool equal (Step const& rhs) const = 0; + virtual bool + equal(Step const& rhs) const = 0; }; /// @cond INTERNAL @@ -299,12 +284,12 @@ using Strand = std::vector>; /// @endcond /// @cond INTERNAL -inline -bool operator==(Strand const& lhs, Strand const& rhs) +inline bool +operator==(Strand const& lhs, Strand const& rhs) { - if (lhs.size () != rhs.size ()) + if (lhs.size() != rhs.size()) return false; - for (size_t i = 0, e = lhs.size (); i != e; ++i) + for (size_t i = 0, e = lhs.size(); i != e; ++i) if (*lhs[i] != *rhs[i]) return false; return true; @@ -325,7 +310,8 @@ bool operator==(Strand const& lhs, Strand const& rhs) @return error code and normalized path */ std::pair -normalizePath(AccountID const& src, +normalizePath( + AccountID const& src, AccountID const& dst, Issue const& deliver, boost::optional const& sendMaxIssue, @@ -347,13 +333,14 @@ normalizePath(AccountID const& src, @param path Liquidity sources to use for this strand of the payment. The path contains an ordered collection of the offer books to use and accounts to ripple through. - @param ownerPaysTransferFee false -> charge sender; true -> charge offer owner + @param ownerPaysTransferFee false -> charge sender; true -> charge offer + owner @param offerCrossing false -> payment; true -> offer crossing @param j Journal for logging messages @return Error code and constructed Strand */ std::pair -toStrand ( +toStrand( ReadView const& sb, AccountID const& src, AccountID const& dst, @@ -383,13 +370,15 @@ toStrand ( contains an ordered collection of the offer books to use and accounts to ripple through. @param addDefaultPath Determines if the default path should be included - @param ownerPaysTransferFee false -> charge sender; true -> charge offer owner + @param ownerPaysTransferFee false -> charge sender; true -> charge offer + owner @param offerCrossing false -> payment; true -> offer crossing @param j Journal for logging messages @return error code and collection of strands */ std::pair> -toStrands (ReadView const& sb, +toStrands( + ReadView const& sb, AccountID const& src, AccountID const& dst, Issue const& deliver, @@ -408,47 +397,45 @@ struct StepImp : public Step explicit StepImp() = default; std::pair - rev ( - PaymentSandbox& sb, + rev(PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, EitherAmount const& out) override { - auto const r = - static_cast (this)->revImp (sb, afView, ofrsToRm, get(out)); - return {EitherAmount (r.first), EitherAmount (r.second)}; + auto const r = static_cast(this)->revImp( + sb, afView, ofrsToRm, get(out)); + return {EitherAmount(r.first), EitherAmount(r.second)}; } // Given the requested amount to consume, compute the amount produced. // Return the consumed/produced std::pair - fwd ( - PaymentSandbox& sb, + fwd(PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, EitherAmount const& in) override { - auto const r = - static_cast (this)->fwdImp (sb, afView, ofrsToRm, get(in)); - return {EitherAmount (r.first), EitherAmount (r.second)}; + auto const r = static_cast(this)->fwdImp( + sb, afView, ofrsToRm, get(in)); + return {EitherAmount(r.first), EitherAmount(r.second)}; } bool - isZero (EitherAmount const& out) const override + isZero(EitherAmount const& out) const override { return get(out) == beast::zero; } bool - equalOut (EitherAmount const& lhs, EitherAmount const& rhs) const override + equalOut(EitherAmount const& lhs, EitherAmount const& rhs) const override { - return get (lhs) == get (rhs); + return get(lhs) == get(rhs); } bool - equalIn (EitherAmount const& lhs, EitherAmount const& rhs) const override + equalIn(EitherAmount const& lhs, EitherAmount const& rhs) const override { - return get (lhs) == get (rhs); + return get(lhs) == get(rhs); } }; /// @endcond @@ -460,16 +447,12 @@ class FlowException : public std::runtime_error public: TER ter; - FlowException (TER t, std::string const& msg) - : std::runtime_error (msg) - , ter (t) + FlowException(TER t, std::string const& msg) + : std::runtime_error(msg), ter(t) { } - explicit - FlowException (TER t) - : std::runtime_error (transHuman (t)) - , ter (t) + explicit FlowException(TER t) : std::runtime_error(transHuman(t)), ter(t) { } }; @@ -477,8 +460,10 @@ public: /// @cond INTERNAL // Check equal with tolerance -bool checkNear (IOUAmount const& expected, IOUAmount const& actual); -bool checkNear (XRPAmount const& expected, XRPAmount const& actual); +bool +checkNear(IOUAmount const& expected, IOUAmount const& actual); +bool +checkNear(XRPAmount const& expected, XRPAmount const& actual); /// @endcond /** @@ -486,25 +471,25 @@ bool checkNear (XRPAmount const& expected, XRPAmount const& actual); */ struct StrandContext { - ReadView const& view; ///< Current ReadView - AccountID const strandSrc; ///< Strand source account - AccountID const strandDst; ///< Strand destination account - Issue const strandDeliver; ///< Issue strand delivers - boost::optional const limitQuality; ///< Worst accepted quality - bool const isFirst; ///< true if Step is first in Strand - bool const isLast = false; ///< true if Step is last in Strand - bool const ownerPaysTransferFee; ///< true if owner, not sender, pays fee - bool const offerCrossing; ///< true if offer crossing, not payment - bool const isDefaultPath; ///< true if Strand is default path - size_t const strandSize; ///< Length of Strand + ReadView const& view; ///< Current ReadView + AccountID const strandSrc; ///< Strand source account + AccountID const strandDst; ///< Strand destination account + Issue const strandDeliver; ///< Issue strand delivers + boost::optional const limitQuality; ///< Worst accepted quality + bool const isFirst; ///< true if Step is first in Strand + bool const isLast = false; ///< true if Step is last in Strand + bool const ownerPaysTransferFee; ///< true if owner, not sender, pays fee + bool const offerCrossing; ///< true if offer crossing, not payment + bool const isDefaultPath; ///< true if Strand is default path + size_t const strandSize; ///< Length of Strand /** The previous step in the strand. Needed to check the no ripple constraint */ Step const* const prevStep = nullptr; /** A strand may not include the same account node more than once in the same currency. In a direct step, an account will show up - at most twice: once as a src and once as a dst (hence the two element array). - The strandSrc and strandDst will only show up once each. + at most twice: once as a src and once as a dst (hence the two element + array). The strandSrc and strandDst will only show up once each. */ std::array, 2>& seenDirectIssues; /** A strand may not include an offer that output the same issue more @@ -514,7 +499,8 @@ struct StrandContext beast::Journal const j; /** StrandContext constructor. */ - StrandContext (ReadView const& view_, + StrandContext( + ReadView const& view_, std::vector> const& strand_, // A strand may not include an inner node that // replicates the source or destination. @@ -527,57 +513,53 @@ struct StrandContext bool offerCrossing_, bool isDefaultPath_, std::array, 2>& - seenDirectIssues_, ///< For detecting currency loops - boost::container::flat_set& seenBookOuts_, ///< For detecting book loops - beast::Journal j_); ///< Journal for logging + seenDirectIssues_, ///< For detecting currency loops + boost::container::flat_set& + seenBookOuts_, ///< For detecting book loops + beast::Journal j_); ///< Journal for logging }; /// @cond INTERNAL namespace test { // Needed for testing -bool directStepEqual (Step const& step, +bool +directStepEqual( + Step const& step, AccountID const& src, AccountID const& dst, Currency const& currency); -bool xrpEndpointStepEqual (Step const& step, AccountID const& acc); +bool +xrpEndpointStepEqual(Step const& step, AccountID const& acc); -bool bookStepEqual (Step const& step, ripple::Book const& book); -} +bool +bookStepEqual(Step const& step, ripple::Book const& book); +} // namespace test std::pair> -make_DirectStepI ( +make_DirectStepI( StrandContext const& ctx, AccountID const& src, AccountID const& dst, Currency const& c); std::pair> -make_BookStepII ( - StrandContext const& ctx, - Issue const& in, - Issue const& out); +make_BookStepII(StrandContext const& ctx, Issue const& in, Issue const& out); std::pair> -make_BookStepIX ( - StrandContext const& ctx, - Issue const& in); +make_BookStepIX(StrandContext const& ctx, Issue const& in); std::pair> -make_BookStepXI ( - StrandContext const& ctx, - Issue const& out); +make_BookStepXI(StrandContext const& ctx, Issue const& out); std::pair> -make_XRPEndpointStep ( - StrandContext const& ctx, - AccountID const& acc); +make_XRPEndpointStep(StrandContext const& ctx, AccountID const& acc); -template +template bool isDirectXrpToXrp(Strand const& strand); /// @endcond -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/StrandFlow.h b/src/ripple/app/paths/impl/StrandFlow.h index 44400ff89e..539803c630 100644 --- a/src/ripple/app/paths/impl/StrandFlow.h +++ b/src/ripple/app/paths/impl/StrandFlow.h @@ -27,8 +27,8 @@ #include #include #include -#include #include +#include #include @@ -40,19 +40,21 @@ namespace ripple { /** Result of flow() execution of a single Strand. */ -template +template struct StrandResult { - bool success; ///< Strand succeeded - TInAmt in = beast::zero; ///< Currency amount in - TOutAmt out = beast::zero; ///< Currency amount out - boost::optional sandbox; ///< Resulting Sandbox state - boost::container::flat_set ofrsToRm; ///< Offers to remove - // strand can be inactive if there is no more liquidity or too many offers have been consumed - bool inactive = false; ///< Strand should not considered as a further source of liquidity (dry) + bool success; ///< Strand succeeded + TInAmt in = beast::zero; ///< Currency amount in + TOutAmt out = beast::zero; ///< Currency amount out + boost::optional sandbox; ///< Resulting Sandbox state + boost::container::flat_set ofrsToRm; ///< Offers to remove + // strand can be inactive if there is no more liquidity or too many offers + // have been consumed + bool inactive = false; ///< Strand should not considered as a further + ///< source of liquidity (dry) /** Strand result constructor */ - StrandResult () = default; + StrandResult() = default; StrandResult( TInAmt const& in_, @@ -69,8 +71,7 @@ struct StrandResult { } - explicit - StrandResult(boost::container::flat_set ofrsToRm_) + explicit StrandResult(boost::container::flat_set ofrsToRm_) : success(false), ofrsToRm(std::move(ofrsToRm_)) { } @@ -87,9 +88,9 @@ struct StrandResult @return Actual amount in and out from the strand, errors, offers to remove, and payment sandbox */ -template -StrandResult -flow ( +template +StrandResult +flow( PaymentSandbox const& baseView, Strand const& strand, boost::optional const& maxIn, @@ -97,51 +98,51 @@ flow ( beast::Journal j) { using Result = StrandResult; - if (strand.empty ()) + if (strand.empty()) { - JLOG (j.warn()) << "Empty strand passed to Liquidity"; + JLOG(j.warn()) << "Empty strand passed to Liquidity"; return {}; } boost::container::flat_set ofrsToRm; - if (isDirectXrpToXrp (strand)) + if (isDirectXrpToXrp(strand)) { - return Result{std::move (ofrsToRm)}; + return Result{std::move(ofrsToRm)}; } try { - std::size_t const s = strand.size (); + std::size_t const s = strand.size(); - std::size_t limitingStep = strand.size (); - boost::optional sb (&baseView); + std::size_t limitingStep = strand.size(); + boost::optional sb(&baseView); // The "all funds" view determines if an offer becomes unfunded or is // found unfunded // These are the account balances before the strand executes - boost::optional afView (&baseView); + boost::optional afView(&baseView); EitherAmount limitStepOut; { - EitherAmount stepOut (out); + EitherAmount stepOut(out); for (auto i = s; i--;) { - auto r = strand[i]->rev (*sb, *afView, ofrsToRm, stepOut); - if (strand[i]->isZero (r.second)) + auto r = strand[i]->rev(*sb, *afView, ofrsToRm, stepOut); + if (strand[i]->isZero(r.second)) { - JLOG (j.trace()) << "Strand found dry in rev"; - return Result{std::move (ofrsToRm)}; + JLOG(j.trace()) << "Strand found dry in rev"; + return Result{std::move(ofrsToRm)}; } - if (i == 0 && maxIn && *maxIn < get (r.first)) + if (i == 0 && maxIn && *maxIn < get(r.first)) { // limiting - exceeded maxIn // Throw out previous results - sb.emplace (&baseView); + sb.emplace(&baseView); limitingStep = i; // re-execute the limiting step - r = strand[i]->fwd ( - *sb, *afView, ofrsToRm, EitherAmount (*maxIn)); + r = strand[i]->fwd( + *sb, *afView, ofrsToRm, EitherAmount(*maxIn)); limitStepOut = r.second; if (strand[i]->isZero(r.second)) @@ -149,7 +150,7 @@ flow ( JLOG(j.trace()) << "First step found dry"; return Result{std::move(ofrsToRm)}; } - if (get (r.first) != *maxIn) + if (get(r.first) != *maxIn) { // Something is very wrong // throwing out the sandbox can only increase liquidity @@ -159,30 +160,30 @@ flow ( << to_string(get(r.first)) << " maxIn: " << to_string(*maxIn); assert(0); - return Result{std::move (ofrsToRm)}; + return Result{std::move(ofrsToRm)}; } } - else if (!strand[i]->equalOut (r.second, stepOut)) + else if (!strand[i]->equalOut(r.second, stepOut)) { // limiting // Throw out previous results - sb.emplace (&baseView); - afView.emplace (&baseView); + sb.emplace(&baseView); + afView.emplace(&baseView); limitingStep = i; // re-execute the limiting step stepOut = r.second; - r = strand[i]->rev (*sb, *afView, ofrsToRm, stepOut); + r = strand[i]->rev(*sb, *afView, ofrsToRm, stepOut); limitStepOut = r.second; if (strand[i]->isZero(r.second)) { - // A tiny input amount can cause this step to output zero. - // I.e. 10^-80 IOU into an IOU -> XRP offer. + // A tiny input amount can cause this step to output + // zero. I.e. 10^-80 IOU into an IOU -> XRP offer. JLOG(j.trace()) << "Limiting step found dry"; return Result{std::move(ofrsToRm)}; } - if (!strand[i]->equalOut (r.second, stepOut)) + if (!strand[i]->equalOut(r.second, stepOut)) { // Something is very wrong // throwing out the sandbox can only increase liquidity @@ -192,10 +193,10 @@ flow ( << "Re-executed limiting step failed. r.second: " << r.second << " stepOut: " << stepOut; #else - JLOG (j.fatal()) << "Re-executed limiting step failed"; + JLOG(j.fatal()) << "Re-executed limiting step failed"; #endif - assert (0); - return Result{std::move (ofrsToRm)}; + assert(0); + return Result{std::move(ofrsToRm)}; } } @@ -205,7 +206,7 @@ flow ( } { - EitherAmount stepIn (limitStepOut); + EitherAmount stepIn(limitStepOut); for (auto i = limitingStep + 1; i < s; ++i) { auto const r = strand[i]->fwd(*sb, *afView, ofrsToRm, stepIn); @@ -216,42 +217,43 @@ flow ( JLOG(j.trace()) << "Non-limiting step found dry"; return Result{std::move(ofrsToRm)}; } - if (!strand[i]->equalIn (r.first, stepIn)) + if (!strand[i]->equalIn(r.first, stepIn)) { - // The limits should already have been found, so executing a strand forward - // from the limiting step should not find a new limit + // The limits should already have been found, so executing a + // strand forward from the limiting step should not find a + // new limit #ifndef NDEBUG JLOG(j.fatal()) << "Re-executed forward pass failed. r.first: " << r.first << " stepIn: " << stepIn; #else - JLOG (j.fatal()) << "Re-executed forward pass failed"; + JLOG(j.fatal()) << "Re-executed forward pass failed"; #endif - assert (0); - return Result{std::move (ofrsToRm)}; + assert(0); + return Result{std::move(ofrsToRm)}; } stepIn = r.second; } } - auto const strandIn = *strand.front ()->cachedIn (); - auto const strandOut = *strand.back ()->cachedOut (); + auto const strandIn = *strand.front()->cachedIn(); + auto const strandOut = *strand.back()->cachedOut(); #ifndef NDEBUG { // Check that the strand will execute as intended // Re-executing the strand will change the cached values - PaymentSandbox checkSB (&baseView); - PaymentSandbox checkAfView (&baseView); - EitherAmount stepIn (*strand[0]->cachedIn ()); + PaymentSandbox checkSB(&baseView); + PaymentSandbox checkAfView(&baseView); + EitherAmount stepIn(*strand[0]->cachedIn()); for (auto i = 0; i < s; ++i) { bool valid; - std::tie (valid, stepIn) = - strand[i]->validFwd (checkSB, checkAfView, stepIn); + std::tie(valid, stepIn) = + strand[i]->validFwd(checkSB, checkAfView, stepIn); if (!valid) { - JLOG (j.warn()) + JLOG(j.warn()) << "Strand re-execute check failed. Step: " << i; break; } @@ -273,12 +275,12 @@ flow ( } catch (FlowException const&) { - return Result{std::move (ofrsToRm)}; + return Result{std::move(ofrsToRm)}; } } /// @cond INTERNAL -template +template struct FlowResult { TInAmt in = beast::zero; @@ -287,34 +289,32 @@ struct FlowResult boost::container::flat_set removableOffers; TER ter = temUNKNOWN; - FlowResult () = default; + FlowResult() = default; - FlowResult (TInAmt const& in_, + FlowResult( + TInAmt const& in_, TOutAmt const& out_, PaymentSandbox&& sandbox_, boost::container::flat_set ofrsToRm) - : in (in_) - , out (out_) - , sandbox (std::move (sandbox_)) - , removableOffers(std::move (ofrsToRm)) - , ter (tesSUCCESS) + : in(in_) + , out(out_) + , sandbox(std::move(sandbox_)) + , removableOffers(std::move(ofrsToRm)) + , ter(tesSUCCESS) { } - FlowResult (TER ter_, boost::container::flat_set ofrsToRm) - : removableOffers(std::move (ofrsToRm)) - , ter (ter_) + FlowResult(TER ter_, boost::container::flat_set ofrsToRm) + : removableOffers(std::move(ofrsToRm)), ter(ter_) { } - FlowResult (TER ter_, + FlowResult( + TER ter_, TInAmt const& in_, TOutAmt const& out_, boost::container::flat_set ofrsToRm) - : in (in_) - , out (out_) - , removableOffers (std::move (ofrsToRm)) - , ter (ter_) + : in(in_), out(out_), removableOffers(std::move(ofrsToRm)), ter(ter_) { } }; @@ -355,53 +355,58 @@ private: std::vector next_; public: - ActiveStrands (std::vector const& strands) + ActiveStrands(std::vector const& strands) { - cur_.reserve (strands.size ()); - next_.reserve (strands.size ()); + cur_.reserve(strands.size()); + next_.reserve(strands.size()); for (auto& strand : strands) - next_.push_back (&strand); + next_.push_back(&strand); } // Start a new iteration in the search for liquidity // Set the current strands to the strands in `next_` void - activateNext () + activateNext() { // Swap, don't move, so we keep the reserve in next_ - cur_.clear (); - std::swap (cur_, next_); + cur_.clear(); + std::swap(cur_, next_); } void - push (Strand const* s) + push(Strand const* s) { - next_.push_back (s); + next_.push_back(s); } - auto begin () + auto + begin() { - return cur_.begin (); + return cur_.begin(); } - auto end () + auto + end() { - return cur_.end (); + return cur_.end(); } - auto begin () const + auto + begin() const { - return cur_.begin (); + return cur_.begin(); } - auto end () const + auto + end() const { - return cur_.end (); + return cur_.end(); } - auto size () const + auto + size() const { - return cur_.size (); + return cur_.size(); } void @@ -430,11 +435,13 @@ public: @param sendMaxST If present, the maximum STAmount to send @param j Journal to write journal messages to @param flowDebugInfo If pointer is non-null, write flow debug info here - @return Actual amount in and out from the strands, errors, and payment sandbox + @return Actual amount in and out from the strands, errors, and payment + sandbox */ template FlowResult -flow (PaymentSandbox const& baseView, +flow( + PaymentSandbox const& baseView, std::vector const& strands, TOutAmt const& outReq, bool partialPayment, @@ -442,9 +449,10 @@ flow (PaymentSandbox const& baseView, boost::optional const& limitQuality, boost::optional const& sendMaxST, beast::Journal j, - path::detail::FlowDebugInfo* flowDebugInfo=nullptr) + path::detail::FlowDebugInfo* flowDebugInfo = nullptr) { - // Used to track the strand that offers the best quality (output/input ratio) + // Used to track the strand that offers the best quality (output/input + // ratio) struct BestStrand { TInAmt in; @@ -453,16 +461,17 @@ flow (PaymentSandbox const& baseView, Strand const& strand; Quality quality; - BestStrand (TInAmt const& in_, + BestStrand( + TInAmt const& in_, TOutAmt const& out_, PaymentSandbox&& sb_, Strand const& strand_, Quality const& quality_) - : in (in_) - , out (out_) - , sb (std::move (sb_)) - , strand (strand_) - , quality (quality_) + : in(in_) + , out(out_) + , sb(std::move(sb_)) + , strand(strand_) + , quality(quality_) { } }; @@ -476,18 +485,18 @@ flow (PaymentSandbox const& baseView, // natural way. Using `make_optional`, allows us to work around this bug. TInAmt const sendMaxInit = sendMaxST ? toAmount(*sendMaxST) : TInAmt{beast::zero}; - boost::optional const sendMax = - boost::make_optional(sendMaxST && sendMaxInit >= beast::zero, sendMaxInit); + boost::optional const sendMax = boost::make_optional( + sendMaxST && sendMaxInit >= beast::zero, sendMaxInit); boost::optional remainingIn = boost::make_optional(!!sendMax, sendMaxInit); // boost::optional remainingIn{sendMax}; - TOutAmt remainingOut (outReq); + TOutAmt remainingOut(outReq); - PaymentSandbox sb (&baseView); + PaymentSandbox sb(&baseView); // non-dry strands - ActiveStrands activeStrands (strands); + ActiveStrands activeStrands(strands); // Keeping a running sum of the amount in the order they are processed // will not give the best precision. Keep a collection so they may be summed @@ -497,12 +506,11 @@ flow (PaymentSandbox const& baseView, boost::container::flat_multiset savedOuts; savedOuts.reserve(maxTries); - auto sum = [](auto const& col) - { - using TResult = std::decay_t; - if (col.empty ()) + auto sum = [](auto const& col) { + using TResult = std::decay_t; + if (col.empty()) return TResult{beast::zero}; - return std::accumulate (col.begin () + 1, col.end (), *col.begin ()); + return std::accumulate(col.begin() + 1, col.end(), *col.begin()); }; // These offers only need to be removed if the payment is not @@ -510,7 +518,7 @@ flow (PaymentSandbox const& baseView, boost::container::flat_set ofrsToRmOnFail; while (remainingOut > beast::zero && - (!remainingIn || *remainingIn > beast::zero)) + (!remainingIn || *remainingIn > beast::zero)) { ++curTry; if (curTry >= maxTries) @@ -522,10 +530,12 @@ flow (PaymentSandbox const& baseView, boost::container::flat_set ofrsToRm; boost::optional best; - if (flowDebugInfo) flowDebugInfo->newLiquidityPass(); - // Index of strand to mark as inactive (remove from the active list) if the - // liquidity is used. This is used for strands that consume too many offers - // Constructed as `false,0` to workaround a gcc warning about uninitialized variables + if (flowDebugInfo) + flowDebugInfo->newLiquidityPass(); + // Index of strand to mark as inactive (remove from the active list) if + // the liquidity is used. This is used for strands that consume too many + // offers Constructed as `false,0` to workaround a gcc warning about + // uninitialized variables boost::optional markInactiveOnUse{false, 0}; for (auto strand : activeStrands) { @@ -535,7 +545,7 @@ flow (PaymentSandbox const& baseView, if (!strandQ || *strandQ < *limitQuality) continue; } - auto f = flow ( + auto f = flow( sb, *strand, remainingIn, remainingOut, j); // rm bad offers even if the strand fails @@ -545,29 +555,28 @@ flow (PaymentSandbox const& baseView, continue; if (flowDebugInfo) - flowDebugInfo->pushLiquiditySrc(EitherAmount(f.in), EitherAmount(f.out)); + flowDebugInfo->pushLiquiditySrc( + EitherAmount(f.in), EitherAmount(f.out)); - assert (f.out <= remainingOut && f.sandbox && + assert( + f.out <= remainingOut && f.sandbox && (!remainingIn || f.in <= *remainingIn)); - Quality const q (f.out, f.in); + Quality const q(f.out, f.in); - JLOG (j.trace()) - << "New flow iter (iter, in, out): " - << curTry-1 << " " - << to_string(f.in) << " " - << to_string(f.out); + JLOG(j.trace()) + << "New flow iter (iter, in, out): " << curTry - 1 << " " + << to_string(f.in) << " " << to_string(f.out); if (limitQuality && q < *limitQuality) { - JLOG (j.trace()) + JLOG(j.trace()) << "Path rejected by limitQuality" - << " limit: " << *limitQuality - << " path q: " << q; + << " limit: " << *limitQuality << " path q: " << q; continue; } - activeStrands.push (strand); + activeStrands.push(strand); if (!best || best->quality < q || (best->quality == q && best->out < f.out)) @@ -595,37 +604,38 @@ flow (PaymentSandbox const& baseView, activeStrands.removeIndex(*markInactiveOnUse); markInactiveOnUse.reset(); } - savedIns.insert (best->in); - savedOuts.insert (best->out); - remainingOut = outReq - sum (savedOuts); + savedIns.insert(best->in); + savedOuts.insert(best->out); + remainingOut = outReq - sum(savedOuts); if (sendMax) - remainingIn = *sendMax - sum (savedIns); + remainingIn = *sendMax - sum(savedIns); if (flowDebugInfo) - flowDebugInfo->pushPass (EitherAmount (best->in), - EitherAmount (best->out), activeStrands.size ()); + flowDebugInfo->pushPass( + EitherAmount(best->in), + EitherAmount(best->out), + activeStrands.size()); - JLOG (j.trace()) - << "Best path: in: " << to_string (best->in) - << " out: " << to_string (best->out) - << " remainingOut: " << to_string (remainingOut); + JLOG(j.trace()) << "Best path: in: " << to_string(best->in) + << " out: " << to_string(best->out) + << " remainingOut: " << to_string(remainingOut); - best->sb.apply (sb); + best->sb.apply(sb); } else { - JLOG (j.trace()) << "All strands dry."; + JLOG(j.trace()) << "All strands dry."; } - best.reset (); // view in best must be destroyed before modifying base - // view - if (!ofrsToRm.empty ()) + best.reset(); // view in best must be destroyed before modifying base + // view + if (!ofrsToRm.empty()) { SetUnion(ofrsToRmOnFail, ofrsToRm); for (auto const& o : ofrsToRm) { - if (auto ok = sb.peek (keylet::offer (o))) - offerDelete (sb, ok, j); + if (auto ok = sb.peek(keylet::offer(o))) + offerDelete(sb, ok, j); } } @@ -633,18 +643,17 @@ flow (PaymentSandbox const& baseView, break; } - auto const actualOut = sum (savedOuts); - auto const actualIn = sum (savedIns); + auto const actualOut = sum(savedOuts); + auto const actualIn = sum(savedIns); - JLOG (j.trace()) - << "Total flow: in: " << to_string (actualIn) - << " out: " << to_string (actualOut); + JLOG(j.trace()) << "Total flow: in: " << to_string(actualIn) + << " out: " << to_string(actualOut); if (actualOut != outReq) { if (actualOut > outReq) { - assert (0); + assert(0); return {tefEXCEPTION, std::move(ofrsToRmOnFail)}; } if (!partialPayment) @@ -652,8 +661,11 @@ flow (PaymentSandbox const& baseView, // If we're offerCrossing a !partialPayment, then we're // handling tfFillOrKill. That case is handled below; not here. if (!offerCrossing) - return {tecPATH_PARTIAL, - actualIn, actualOut, std::move(ofrsToRmOnFail)}; + return { + tecPATH_PARTIAL, + actualIn, + actualOut, + std::move(ofrsToRmOnFail)}; } else if (actualOut == beast::zero) { @@ -665,15 +677,18 @@ flow (PaymentSandbox const& baseView, // If we're offer crossing and partialPayment is *not* true, then // we're handling a FillOrKill offer. In this case remainingIn must // be zero (all funds must be consumed) or else we kill the offer. - assert (remainingIn); + assert(remainingIn); if (remainingIn && *remainingIn != beast::zero) - return {tecPATH_PARTIAL, - actualIn, actualOut, std::move(ofrsToRmOnFail)}; + return { + tecPATH_PARTIAL, + actualIn, + actualOut, + std::move(ofrsToRmOnFail)}; } - return {actualIn, actualOut, std::move (sb), std::move(ofrsToRmOnFail)}; + return {actualIn, actualOut, std::move(sb), std::move(ofrsToRmOnFail)}; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/paths/impl/XRPEndpointStep.cpp b/src/ripple/app/paths/impl/XRPEndpointStep.cpp index 5eed536fba..05739788cf 100644 --- a/src/ripple/app/paths/impl/XRPEndpointStep.cpp +++ b/src/ripple/app/paths/impl/XRPEndpointStep.cpp @@ -19,8 +19,8 @@ #include #include -#include #include +#include #include #include #include @@ -36,8 +36,8 @@ namespace ripple { template -class XRPEndpointStep : public StepImp< - XRPAmount, XRPAmount, XRPEndpointStep> +class XRPEndpointStep + : public StepImp> { private: AccountID acc_; @@ -50,28 +50,27 @@ private: boost::optional cache_; boost::optional - cached () const + cached() const { if (!cache_) return boost::none; - return EitherAmount (*cache_); + return EitherAmount(*cache_); } public: - XRPEndpointStep ( - StrandContext const& ctx, - AccountID const& acc) - : acc_(acc) - , isLast_(ctx.isLast) - , j_ (ctx.j) {} + XRPEndpointStep(StrandContext const& ctx, AccountID const& acc) + : acc_(acc), isLast_(ctx.isLast), j_(ctx.j) + { + } - AccountID const& acc () const + AccountID const& + acc() const { return acc_; } - boost::optional> - directStepAccts () const override + boost::optional> + directStepAccts() const override { if (isLast_) return std::make_pair(xrpAccount(), acc_); @@ -79,15 +78,15 @@ public: } boost::optional - cachedIn () const override + cachedIn() const override { - return cached (); + return cached(); } boost::optional - cachedOut () const override + cachedOut() const override { - return cached (); + return cached(); } DebtDirection @@ -97,63 +96,62 @@ public: } std::pair, DebtDirection> - qualityUpperBound(ReadView const& v, DebtDirection prevStepDir) const override; + qualityUpperBound(ReadView const& v, DebtDirection prevStepDir) + const override; std::pair - revImp ( + revImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, XRPAmount const& out); std::pair - fwdImp ( + fwdImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, XRPAmount const& in); std::pair - validFwd ( - PaymentSandbox& sb, - ApplyView& afView, - EitherAmount const& in) override; + validFwd(PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) + override; // Check for errors and violations of frozen constraints. - TER check (StrandContext const& ctx) const; + TER + check(StrandContext const& ctx) const; protected: XRPAmount - xrpLiquidImpl (ReadView& sb, std::int32_t reserveReduction) const + xrpLiquidImpl(ReadView& sb, std::int32_t reserveReduction) const { - return ripple::xrpLiquid (sb, acc_, reserveReduction, j_); + return ripple::xrpLiquid(sb, acc_, reserveReduction, j_); } - std::string logStringImpl (char const* name) const + std::string + logStringImpl(char const* name) const { std::ostringstream ostr; - ostr << - name << ": " << - "\nAcc: " << acc_; - return ostr.str (); + ostr << name << ": " + << "\nAcc: " << acc_; + return ostr.str(); } private: template - friend bool operator==( - XRPEndpointStep

const& lhs, - XRPEndpointStep

const& rhs); + friend bool + operator==(XRPEndpointStep

const& lhs, XRPEndpointStep

const& rhs); - friend bool operator!=( - XRPEndpointStep const& lhs, - XRPEndpointStep const& rhs) + friend bool + operator!=(XRPEndpointStep const& lhs, XRPEndpointStep const& rhs) { - return ! (lhs == rhs); + return !(lhs == rhs); } - bool equal (Step const& rhs) const override + bool + equal(Step const& rhs) const override { - if (auto ds = dynamic_cast (&rhs)) + if (auto ds = dynamic_cast(&rhs)) { return *this == *ds; } @@ -176,23 +174,24 @@ public: using XRPEndpointStep::XRPEndpointStep; XRPAmount - xrpLiquid (ReadView& sb) const + xrpLiquid(ReadView& sb) const { - return xrpLiquidImpl (sb, 0);; + return xrpLiquidImpl(sb, 0); + ; } - std::string logString () const override + std::string + logString() const override { - return logStringImpl ("XRPEndpointPaymentStep"); + return logStringImpl("XRPEndpointPaymentStep"); } }; // Offer crossing XRPEndpointStep class (not a payment). -class XRPEndpointOfferCrossingStep : - public XRPEndpointStep +class XRPEndpointOfferCrossingStep + : public XRPEndpointStep { private: - // For historical reasons, offer crossing is allowed to dig further // into the XRP reserve than an ordinary payment. (I believe it's // because the trust line was created after the XRP was removed.) @@ -200,32 +199,31 @@ private: // // Note that reduced reserve only happens if the trust line does not // currently exist. - static std::int32_t computeReserveReduction ( - StrandContext const& ctx, AccountID const& acc) + static std::int32_t + computeReserveReduction(StrandContext const& ctx, AccountID const& acc) { - if (ctx.isFirst && - !ctx.view.read (keylet::line (acc, ctx.strandDeliver))) - return -1; + if (ctx.isFirst && !ctx.view.read(keylet::line(acc, ctx.strandDeliver))) + return -1; return 0; } public: - XRPEndpointOfferCrossingStep ( - StrandContext const& ctx, AccountID const& acc) - : XRPEndpointStep (ctx, acc) - , reserveReduction_ (computeReserveReduction (ctx, acc)) + XRPEndpointOfferCrossingStep(StrandContext const& ctx, AccountID const& acc) + : XRPEndpointStep(ctx, acc) + , reserveReduction_(computeReserveReduction(ctx, acc)) { } XRPAmount - xrpLiquid (ReadView& sb) const + xrpLiquid(ReadView& sb) const { - return xrpLiquidImpl (sb, reserveReduction_); + return xrpLiquidImpl(sb, reserveReduction_); } - std::string logString () const override + std::string + logString() const override { - return logStringImpl ("XRPEndpointOfferCrossingStep"); + return logStringImpl("XRPEndpointOfferCrossingStep"); } private: @@ -235,7 +233,9 @@ private: //------------------------------------------------------------------------------ template -inline bool operator==(XRPEndpointStep const& lhs, +inline bool +operator==( + XRPEndpointStep const& lhs, XRPEndpointStep const& rhs) { return lhs.acc_ == rhs.acc_ && lhs.isLast_ == rhs.isLast_; @@ -244,108 +244,110 @@ inline bool operator==(XRPEndpointStep const& lhs, template std::pair, DebtDirection> XRPEndpointStep::qualityUpperBound( - ReadView const& v, DebtDirection prevStepDir) const + ReadView const& v, + DebtDirection prevStepDir) const { - return {Quality{STAmount::uRateOne}, - this->debtDirection(v, StrandDirection::forward)}; + return { + Quality{STAmount::uRateOne}, + this->debtDirection(v, StrandDirection::forward)}; } template std::pair -XRPEndpointStep::revImp ( +XRPEndpointStep::revImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, XRPAmount const& out) { - auto const balance = static_cast(this)->xrpLiquid (sb); + auto const balance = static_cast(this)->xrpLiquid(sb); - auto const result = isLast_ ? out : std::min (balance, out); + auto const result = isLast_ ? out : std::min(balance, out); auto& sender = isLast_ ? xrpAccount() : acc_; auto& receiver = isLast_ ? acc_ : xrpAccount(); - auto ter = accountSend (sb, sender, receiver, toSTAmount (result), j_); + auto ter = accountSend(sb, sender, receiver, toSTAmount(result), j_); if (ter != tesSUCCESS) return {XRPAmount{beast::zero}, XRPAmount{beast::zero}}; - cache_.emplace (result); + cache_.emplace(result); return {result, result}; } template std::pair -XRPEndpointStep::fwdImp ( +XRPEndpointStep::fwdImp( PaymentSandbox& sb, ApplyView& afView, boost::container::flat_set& ofrsToRm, XRPAmount const& in) { - assert (cache_); - auto const balance = static_cast(this)->xrpLiquid (sb); + assert(cache_); + auto const balance = static_cast(this)->xrpLiquid(sb); - auto const result = isLast_ ? in : std::min (balance, in); + auto const result = isLast_ ? in : std::min(balance, in); auto& sender = isLast_ ? xrpAccount() : acc_; auto& receiver = isLast_ ? acc_ : xrpAccount(); - auto ter = accountSend (sb, sender, receiver, toSTAmount (result), j_); + auto ter = accountSend(sb, sender, receiver, toSTAmount(result), j_); if (ter != tesSUCCESS) return {XRPAmount{beast::zero}, XRPAmount{beast::zero}}; - cache_.emplace (result); + cache_.emplace(result); return {result, result}; } template std::pair -XRPEndpointStep::validFwd ( +XRPEndpointStep::validFwd( PaymentSandbox& sb, ApplyView& afView, EitherAmount const& in) { if (!cache_) { - JLOG (j_.error()) << "Expected valid cache in validFwd"; - return {false, EitherAmount (XRPAmount (beast::zero))}; + JLOG(j_.error()) << "Expected valid cache in validFwd"; + return {false, EitherAmount(XRPAmount(beast::zero))}; } - assert (in.native); + assert(in.native); auto const& xrpIn = in.xrp; - auto const balance = static_cast(this)->xrpLiquid (sb); + auto const balance = static_cast(this)->xrpLiquid(sb); if (!isLast_ && balance < xrpIn) { - JLOG (j_.warn()) << "XRPEndpointStep: Strand re-execute check failed." - << " Insufficient balance: " << to_string (balance) - << " Requested: " << to_string (xrpIn); - return {false, EitherAmount (balance)}; + JLOG(j_.warn()) << "XRPEndpointStep: Strand re-execute check failed." + << " Insufficient balance: " << to_string(balance) + << " Requested: " << to_string(xrpIn); + return {false, EitherAmount(balance)}; } if (xrpIn != *cache_) { - JLOG (j_.warn()) << "XRPEndpointStep: Strand re-execute check failed." - << " ExpectedIn: " << to_string (*cache_) - << " CachedIn: " << to_string (xrpIn); + JLOG(j_.warn()) << "XRPEndpointStep: Strand re-execute check failed." + << " ExpectedIn: " << to_string(*cache_) + << " CachedIn: " << to_string(xrpIn); } return {true, in}; } template TER -XRPEndpointStep::check (StrandContext const& ctx) const +XRPEndpointStep::check(StrandContext const& ctx) const { if (!acc_) { - JLOG (j_.debug()) << "XRPEndpointStep: specified bad account."; + JLOG(j_.debug()) << "XRPEndpointStep: specified bad account."; return temBAD_PATH; } - auto sleAcc = ctx.view.read (keylet::account (acc_)); + auto sleAcc = ctx.view.read(keylet::account(acc_)); if (!sleAcc) { - JLOG (j_.warn()) << "XRPEndpointStep: can't send or receive XRP from " - "non-existent account: " - << acc_; + JLOG(j_.warn()) << "XRPEndpointStep: can't send or receive XRP from " + "non-existent account: " + << acc_; return terNO_ACCOUNT; } @@ -354,9 +356,9 @@ XRPEndpointStep::check (StrandContext const& ctx) const return temBAD_PATH; } - auto& src = isLast_ ? xrpAccount () : acc_; + auto& src = isLast_ ? xrpAccount() : acc_; auto& dst = isLast_ ? acc_ : xrpAccount(); - auto ter = checkFreeze (ctx.view, src, dst, xrpCurrency ()); + auto ter = checkFreeze(ctx.view, src, dst, xrpCurrency()); if (ter != tesSUCCESS) return ter; @@ -377,47 +379,44 @@ XRPEndpointStep::check (StrandContext const& ctx) const //------------------------------------------------------------------------------ -namespace test -{ +namespace test { // Needed for testing -bool xrpEndpointStepEqual (Step const& step, AccountID const& acc) +bool +xrpEndpointStepEqual(Step const& step, AccountID const& acc) { if (auto xs = - dynamic_cast const*> (&step)) + dynamic_cast const*>(&step)) { - return xs->acc () == acc; + return xs->acc() == acc; } return false; } -} +} // namespace test //------------------------------------------------------------------------------ std::pair> -make_XRPEndpointStep ( - StrandContext const& ctx, - AccountID const& acc) +make_XRPEndpointStep(StrandContext const& ctx, AccountID const& acc) { TER ter = tefINTERNAL; std::unique_ptr r; if (ctx.offerCrossing) { auto offerCrossingStep = - std::make_unique (ctx, acc); - ter = offerCrossingStep->check (ctx); - r = std::move (offerCrossingStep); + std::make_unique(ctx, acc); + ter = offerCrossingStep->check(ctx); + r = std::move(offerCrossingStep); } - else // payment + else // payment { - auto paymentStep = - std::make_unique (ctx, acc); - ter = paymentStep->check (ctx); - r = std::move (paymentStep); + auto paymentStep = std::make_unique(ctx, acc); + ter = paymentStep->check(ctx); + r = std::move(paymentStep); } if (ter != tesSUCCESS) return {ter, nullptr}; - return {tesSUCCESS, std::move (r)}; + return {tesSUCCESS, std::move(r)}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/tx/apply.h b/src/ripple/app/tx/apply.h index cabfe66798..b71e7ed4a9 100644 --- a/src/ripple/app/tx/apply.h +++ b/src/ripple/app/tx/apply.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_TX_APPLY_H_INCLUDED #define RIPPLE_TX_APPLY_H_INCLUDED +#include #include #include #include #include -#include #include #include @@ -37,8 +37,7 @@ class HashRouter; @see checkValidity, forceValidity */ -enum class Validity -{ +enum class Validity { /// Signature is bad. Didn't do local checks. SigBad, /// Signature is good, but local checks fail. @@ -61,10 +60,11 @@ enum class Validity @see Validity */ std::pair -checkValidity(HashRouter& router, - STTx const& tx, Rules const& rules, - Config const& config); - +checkValidity( + HashRouter& router, + STTx const& tx, + Rules const& rules, + Config const& config); /** Sets the validity of a given transaction in the cache. @@ -76,8 +76,7 @@ checkValidity(HashRouter& router, @see checkValidity, Validity */ void -forceValidity(HashRouter& router, uint256 const& txid, - Validity validity); +forceValidity(HashRouter& router, uint256 const& txid, Validity validity); /** Apply a transaction to an `OpenView`. @@ -121,17 +120,18 @@ forceValidity(HashRouter& router, uint256 const& txid, whether or not the transaction was applied. */ std::pair -apply (Application& app, OpenView& view, - STTx const& tx, ApplyFlags flags, - beast::Journal journal); - +apply( + Application& app, + OpenView& view, + STTx const& tx, + ApplyFlags flags, + beast::Journal journal); /** Enum class for return value from `applyTransaction` @see applyTransaction */ -enum class ApplyResult -{ +enum class ApplyResult { /// Applied to this ledger Success, /// Should not be retried in this ledger @@ -148,10 +148,14 @@ enum class ApplyResult @see ApplyResult */ ApplyResult -applyTransaction(Application& app, OpenView& view, - STTx const& tx, bool retryAssured, ApplyFlags flags, +applyTransaction( + Application& app, + OpenView& view, + STTx const& tx, + bool retryAssured, + ApplyFlags flags, beast::Journal journal); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/applySteps.h b/src/ripple/app/tx/applySteps.h index d49de7bba3..38be91beb7 100644 --- a/src/ripple/app/tx/applySteps.h +++ b/src/ripple/app/tx/applySteps.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_TX_APPLYSTEPS_H_INCLUDED #define RIPPLE_TX_APPLYSTEPS_H_INCLUDED -#include #include +#include namespace ripple { @@ -31,8 +31,7 @@ class STTx; /** Return true if the transaction can claim a fee (tec), and the `ApplyFlags` do not allow soft failures. */ -inline -bool +inline bool isTecClaimHardFail(TER ter, ApplyFlags flags) { return isTecClaim(ter) && !(flags & tapRETRY); @@ -60,9 +59,8 @@ public: NotTEC const ter; /// Constructor - template - PreflightResult(Context const& ctx_, - NotTEC ter_) + template + PreflightResult(Context const& ctx_, NotTEC ter_) : tx(ctx_.tx) , rules(ctx_.rules) , flags(ctx_.flags) @@ -73,7 +71,8 @@ public: PreflightResult(PreflightResult const&) = default; /// Deleted copy assignment operator - PreflightResult& operator=(PreflightResult const&) = delete; + PreflightResult& + operator=(PreflightResult const&) = delete; }; /** Describes the results of the `preclaim` check @@ -101,21 +100,21 @@ public: bool const likelyToClaimFee; /// Constructor - template + template PreclaimResult(Context const& ctx_, TER ter_) : view(ctx_.view) , tx(ctx_.tx) , flags(ctx_.flags) , j(ctx_.j) , ter(ter_) - , likelyToClaimFee(ter == tesSUCCESS - || isTecClaimHardFail(ter, flags)) + , likelyToClaimFee(ter == tesSUCCESS || isTecClaimHardFail(ter, flags)) { } PreclaimResult(PreclaimResult const&) = default; /// Deleted copy assignment operator - PreclaimResult& operator=(PreclaimResult const&) = delete; + PreclaimResult& + operator=(PreclaimResult const&) = delete; }; /** Structure describing the consequences to the account @@ -128,8 +127,7 @@ struct TxConsequences { /// Describes how the transaction affects subsequent /// transactions - enum ConsequenceCategory - { + enum ConsequenceCategory { /// Moves currency around, creates offers, etc. normal = 0, /// Affects the ability of subsequent transactions @@ -146,23 +144,24 @@ struct TxConsequences XRPAmount const potentialSpend; /// Constructor - TxConsequences(ConsequenceCategory const category_, - XRPAmount const fee_, XRPAmount const spend_) - : category(category_) - , fee(fee_) - , potentialSpend(spend_) + TxConsequences( + ConsequenceCategory const category_, + XRPAmount const fee_, + XRPAmount const spend_) + : category(category_), fee(fee_), potentialSpend(spend_) { } /// Constructor TxConsequences(TxConsequences const&) = default; /// Deleted copy assignment operator - TxConsequences& operator=(TxConsequences const&) = delete; + TxConsequences& + operator=(TxConsequences const&) = delete; /// Constructor TxConsequences(TxConsequences&&) = default; /// Deleted copy assignment operator - TxConsequences& operator=(TxConsequences&&) = delete; - + TxConsequences& + operator=(TxConsequences&&) = delete; }; /** Gate a transaction based on static information. @@ -182,9 +181,12 @@ struct TxConsequences other things, the `TER` code. */ PreflightResult -preflight(Application& app, Rules const& rules, - STTx const& tx, ApplyFlags flags, - beast::Journal j); +preflight( + Application& app, + Rules const& rules, + STTx const& tx, + ApplyFlags flags, + beast::Journal j); /** Gate a transaction based on static ledger information. @@ -215,8 +217,10 @@ preflight(Application& app, Rules const& rules, this transaction. */ PreclaimResult -preclaim(PreflightResult const& preflightResult, - Application& app, OpenView const& view); +preclaim( + PreflightResult const& preflightResult, + Application& app, + OpenView const& view); /** Compute only the expected base fee for a transaction. @@ -235,8 +239,7 @@ preclaim(PreflightResult const& preflightResult, @return The base fee. */ FeeUnit64 -calculateBaseFee(ReadView const& view, - STTx const& tx); +calculateBaseFee(ReadView const& view, STTx const& tx); /** Determine the XRP balance consequences if a transaction consumes the maximum XRP allowed. @@ -273,9 +276,8 @@ calculateConsequences(PreflightResult const& preflightResult); whether or not the transaction was applied. */ std::pair -doApply(PreclaimResult const& preclaimResult, - Application& app, OpenView& view); +doApply(PreclaimResult const& preclaimResult, Application& app, OpenView& view); -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/ApplyContext.cpp b/src/ripple/app/tx/impl/ApplyContext.cpp index e3e1c1ef47..eb68fe5d39 100644 --- a/src/ripple/app/tx/impl/ApplyContext.cpp +++ b/src/ripple/app/tx/impl/ApplyContext.cpp @@ -22,22 +22,26 @@ #include #include #include -#include #include +#include #include namespace ripple { -ApplyContext::ApplyContext(Application& app_, - OpenView& base, STTx const& tx_, TER preclaimResult_, - FeeUnit64 baseFee_, ApplyFlags flags, - beast::Journal journal_) +ApplyContext::ApplyContext( + Application& app_, + OpenView& base, + STTx const& tx_, + TER preclaimResult_, + FeeUnit64 baseFee_, + ApplyFlags flags, + beast::Journal journal_) : app(app_) , tx(tx_) , preclaimResult(preclaimResult_) , baseFee(baseFee_) , journal(journal_) - , base_ (base) + , base_(base) , flags_(flags) { view_.emplace(&base_, flags_); @@ -62,16 +66,17 @@ ApplyContext::size() } void -ApplyContext::visit (std::function const&, - std::shared_ptr const&)> const& func) +ApplyContext::visit(std::function const&, + std::shared_ptr const&)> const& func) { view_->visit(base_, func); } TER -ApplyContext::failInvariantCheck (TER const result) +ApplyContext::failInvariantCheck(TER const result) { // If we already failed invariant checks before and we are now attempting to // only charge a fee, and even that fails the invariant checks something is @@ -83,7 +88,7 @@ ApplyContext::failInvariantCheck (TER const result) : TER{tecINVARIANT_FAILED}; } -template +template TER ApplyContext::checkInvariantsHelper( TER const result, @@ -95,15 +100,13 @@ ApplyContext::checkInvariantsHelper( auto checkers = getInvariantChecks(); // call each check's per-entry method - visit ( - [&checkers]( - uint256 const& index, - bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) - { - (..., std::get(checkers).visitEntry(isDelete, before, after)); - }); + visit([&checkers]( + uint256 const& index, + bool isDelete, + std::shared_ptr const& before, + std::shared_ptr const& after) { + (..., std::get(checkers).visitEntry(isDelete, before, after)); + }); // Note: do not replace this logic with a `...&&` fold expression. // The fold expression will only run until the first check fails (it @@ -111,28 +114,30 @@ ApplyContext::checkInvariantsHelper( // message won't be. Every failed invariant should write to the log, // not just the first one. std::array finalizers{ - {std::get( - checkers).finalize(tx, result, fee, *view_, journal)...}}; + {std::get(checkers).finalize( + tx, result, fee, *view_, journal)...}}; // call each check's finalizer to see that it passes - if (! std::all_of( finalizers.cbegin(), finalizers.cend(), - [](auto const& b) { return b; })) + if (!std::all_of( + finalizers.cbegin(), finalizers.cend(), [](auto const& b) { + return b; + })) { - JLOG(journal.fatal()) << - "Transaction has failed one or more invariants: " << - to_string(tx.getJson (JsonOptions::none)); + JLOG(journal.fatal()) + << "Transaction has failed one or more invariants: " + << to_string(tx.getJson(JsonOptions::none)); - return failInvariantCheck (result); + return failInvariantCheck(result); } } - catch(std::exception const& ex) + catch (std::exception const& ex) { - JLOG(journal.fatal()) << - "Transaction caused an exception in an invariant" << - ", ex: " << ex.what() << - ", tx: " << to_string(tx.getJson (JsonOptions::none)); + JLOG(journal.fatal()) + << "Transaction caused an exception in an invariant" + << ", ex: " << ex.what() + << ", tx: " << to_string(tx.getJson(JsonOptions::none)); - return failInvariantCheck (result); + return failInvariantCheck(result); } return result; @@ -141,10 +146,12 @@ ApplyContext::checkInvariantsHelper( TER ApplyContext::checkInvariants(TER const result, XRPAmount const fee) { - assert (isTesSuccess(result) || isTecClaim(result)); + assert(isTesSuccess(result) || isTecClaim(result)); - return checkInvariantsHelper(result, fee, + return checkInvariantsHelper( + result, + fee, std::make_index_sequence::value>{}); } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/ApplyContext.h b/src/ripple/app/tx/impl/ApplyContext.h index fe4b830856..97aca41cb3 100644 --- a/src/ripple/app/tx/impl/ApplyContext.h +++ b/src/ripple/app/tx/impl/ApplyContext.h @@ -21,11 +21,11 @@ #define RIPPLE_TX_APPLYCONTEXT_H_INCLUDED #include -#include #include -#include -#include #include +#include +#include +#include #include #include @@ -35,11 +35,14 @@ namespace ripple { class ApplyContext { public: - explicit - ApplyContext (Application& app, OpenView& base, - STTx const& tx, TER preclaimResult, - FeeUnit64 baseFee, ApplyFlags flags, - beast::Journal = beast::Journal{beast::Journal::getNullSink()}); + explicit ApplyContext( + Application& app, + OpenView& base, + STTx const& tx, + TER preclaimResult, + FeeUnit64 baseFee, + ApplyFlags flags, + beast::Journal = beast::Journal{beast::Journal::getNullSink()}); Application& app; STTx const& tx; @@ -68,7 +71,7 @@ public: /** Sets the DeliveredAmount field in the metadata */ void - deliver (STAmount const& amount) + deliver(STAmount const& amount) { view_->deliver(amount); } @@ -78,23 +81,22 @@ public: discard(); /** Apply the transaction result to the base. */ - void - apply (TER); + void apply(TER); /** Get the number of unapplied changes. */ std::size_t - size (); + size(); /** Visit unapplied changes. */ void - visit (std::function const& before, - std::shared_ptr const& after)> const& func); + visit(std::function const& before, + std::shared_ptr const& after)> const& func); void - destroyXRP (XRPAmount const& fee) + destroyXRP(XRPAmount const& fee) { view_->rawDestroyXRP(fee); } @@ -110,17 +112,20 @@ public: private: TER - failInvariantCheck (TER const result); + failInvariantCheck(TER const result); - template + template TER - checkInvariantsHelper(TER const result, XRPAmount const fee, std::index_sequence); + checkInvariantsHelper( + TER const result, + XRPAmount const fee, + std::index_sequence); OpenView& base_; ApplyFlags flags_; boost::optional view_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/BookTip.cpp b/src/ripple/app/tx/impl/BookTip.cpp index 5ca858ba6c..2ec2c7b44e 100644 --- a/src/ripple/app/tx/impl/BookTip.cpp +++ b/src/ripple/app/tx/impl/BookTip.cpp @@ -22,45 +22,44 @@ namespace ripple { -BookTip::BookTip (ApplyView& view, Book const& book) - : view_ (view) - , m_valid (false) - , m_book (getBookBase (book)) - , m_end (getQualityNext (m_book)) +BookTip::BookTip(ApplyView& view, Book const& book) + : view_(view) + , m_valid(false) + , m_book(getBookBase(book)) + , m_end(getQualityNext(m_book)) { } bool -BookTip::step (beast::Journal j) +BookTip::step(beast::Journal j) { if (m_valid) { if (m_entry) { - offerDelete (view_, m_entry, j); + offerDelete(view_, m_entry, j); m_entry = nullptr; } } - for(;;) + for (;;) { // See if there's an entry at or worse than current quality. Notice // that the quality is encoded only in the index of the first page // of a directory. - auto const first_page = - view_.succ (m_book, m_end); + auto const first_page = view_.succ(m_book, m_end); - if (! first_page) + if (!first_page) return false; unsigned int di = 0; std::shared_ptr dir; - if (dirFirst (view_, *first_page, dir, di, m_index, j)) + if (dirFirst(view_, *first_page, dir, di, m_index, j)) { m_dir = dir->key(); m_entry = view_.peek(keylet::offer(m_index)); - m_quality = Quality (getQuality (*first_page)); + m_quality = Quality(getQuality(*first_page)); m_valid = true; // Next query should start before this directory @@ -80,4 +79,4 @@ BookTip::step (beast::Journal j) return true; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/BookTip.h b/src/ripple/app/tx/impl/BookTip.h index 5a306b0f6f..d57700ad4f 100644 --- a/src/ripple/app/tx/impl/BookTip.h +++ b/src/ripple/app/tx/impl/BookTip.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_APP_BOOK_BOOKTIP_H_INCLUDED #define RIPPLE_APP_BOOK_BOOKTIP_H_INCLUDED -#include #include #include +#include #include @@ -48,7 +48,7 @@ private: public: /** Create the iterator. */ - BookTip (ApplyView& view, Book const& book); + BookTip(ApplyView& view, Book const& book); uint256 const& dir() const noexcept @@ -79,9 +79,9 @@ public: @return `true` if there is a next offer */ bool - step (beast::Journal j); + step(beast::Journal j); }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CancelCheck.cpp b/src/ripple/app/tx/impl/CancelCheck.cpp index 28be52ffe7..a074d9f87c 100644 --- a/src/ripple/app/tx/impl/CancelCheck.cpp +++ b/src/ripple/app/tx/impl/CancelCheck.cpp @@ -31,13 +31,13 @@ namespace ripple { NotTEC -CancelCheck::preflight (PreflightContext const& ctx) +CancelCheck::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled (featureChecks)) + if (!ctx.rules.enabled(featureChecks)) return temDISABLED; - NotTEC const ret {preflight1 (ctx)}; - if (! isTesSuccess (ret)) + NotTEC const ret{preflight1(ctx)}; + if (!isTesSuccess(ret)) return ret; if (ctx.tx.getFlags() & tfUniversalMask) @@ -47,14 +47,14 @@ CancelCheck::preflight (PreflightContext const& ctx) return temINVALID_FLAG; } - return preflight2 (ctx); + return preflight2(ctx); } TER -CancelCheck::preclaim (PreclaimContext const& ctx) +CancelCheck::preclaim(PreclaimContext const& ctx) { - auto const sleCheck = ctx.view.read (keylet::check (ctx.tx[sfCheckID])); - if (! sleCheck) + auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID])); + if (!sleCheck) { JLOG(ctx.j.warn()) << "Check does not exist."; return tecNO_ENTRY; @@ -68,17 +68,17 @@ CancelCheck::preclaim (PreclaimContext const& ctx) // ledger, because we definitively know the time that it closed but // we do not know the closing time of the ledger that is under // construction. - if (! optExpiry || - (ctx.view.parentCloseTime() < timepoint {duration {*optExpiry}})) + if (!optExpiry || + (ctx.view.parentCloseTime() < timepoint{duration{*optExpiry}})) { // If the check is not yet expired, then only the creator or the // destination may cancel the check. - AccountID const acctId {ctx.tx[sfAccount]}; + AccountID const acctId{ctx.tx[sfAccount]}; if (acctId != (*sleCheck)[sfAccount] && acctId != (*sleCheck)[sfDestination]) { JLOG(ctx.j.warn()) << "Check is not expired and canceler is " - "neither check source nor destination."; + "neither check source nor destination."; return tecNO_PERMISSION; } } @@ -86,35 +86,35 @@ CancelCheck::preclaim (PreclaimContext const& ctx) } TER -CancelCheck::doApply () +CancelCheck::doApply() { - uint256 const checkId {ctx_.tx[sfCheckID]}; - auto const sleCheck = view().peek (keylet::check (checkId)); - if (! sleCheck) + uint256 const checkId{ctx_.tx[sfCheckID]}; + auto const sleCheck = view().peek(keylet::check(checkId)); + if (!sleCheck) { // Error should have been caught in preclaim. JLOG(j_.warn()) << "Check does not exist."; return tecNO_ENTRY; } - AccountID const srcId {sleCheck->getAccountID (sfAccount)}; - AccountID const dstId {sleCheck->getAccountID (sfDestination)}; - auto viewJ = ctx_.app.journal ("View"); + AccountID const srcId{sleCheck->getAccountID(sfAccount)}; + AccountID const dstId{sleCheck->getAccountID(sfDestination)}; + auto viewJ = ctx_.app.journal("View"); // If the check is not written to self (and it shouldn't be), remove the // check from the destination account root. if (srcId != dstId) { - std::uint64_t const page {(*sleCheck)[sfDestinationNode]}; - if (! view().dirRemove (keylet::ownerDir(dstId), page, checkId, true)) + std::uint64_t const page{(*sleCheck)[sfDestinationNode]}; + if (!view().dirRemove(keylet::ownerDir(dstId), page, checkId, true)) { JLOG(j_.warn()) << "Unable to delete check from destination."; return tefBAD_LEDGER; } } { - std::uint64_t const page {(*sleCheck)[sfOwnerNode]}; - if (! view().dirRemove (keylet::ownerDir(srcId), page, checkId, true)) + std::uint64_t const page{(*sleCheck)[sfOwnerNode]}; + if (!view().dirRemove(keylet::ownerDir(srcId), page, checkId, true)) { JLOG(j_.warn()) << "Unable to delete check from owner."; return tefBAD_LEDGER; @@ -122,12 +122,12 @@ CancelCheck::doApply () } // If we succeeded, update the check owner's reserve. - auto const sleSrc = view().peek (keylet::account (srcId)); - adjustOwnerCount (view(), sleSrc, -1, viewJ); + auto const sleSrc = view().peek(keylet::account(srcId)); + adjustOwnerCount(view(), sleSrc, -1, viewJ); // Remove check from ledger. - view().erase (sleCheck); + view().erase(sleCheck); return tesSUCCESS; } -} // namespace ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CancelCheck.h b/src/ripple/app/tx/impl/CancelCheck.h index ddea7fd6ba..cec434c8a5 100644 --- a/src/ripple/app/tx/impl/CancelCheck.h +++ b/src/ripple/app/tx/impl/CancelCheck.h @@ -24,26 +24,23 @@ namespace ripple { -class CancelCheck - : public Transactor +class CancelCheck : public Transactor { public: - explicit CancelCheck (ApplyContext& ctx) - : Transactor (ctx) + explicit CancelCheck(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); + + static TER + preclaim(PreclaimContext const& ctx); - static TER - preclaim (PreclaimContext const& ctx); - - TER doApply () override; + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CancelOffer.cpp b/src/ripple/app/tx/impl/CancelOffer.cpp index 70da5b5cf5..16815707b2 100644 --- a/src/ripple/app/tx/impl/CancelOffer.cpp +++ b/src/ripple/app/tx/impl/CancelOffer.cpp @@ -19,32 +19,31 @@ #include #include -#include #include +#include namespace ripple { NotTEC -CancelOffer::preflight (PreflightContext const& ctx) +CancelOffer::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto const uTxFlags = ctx.tx.getFlags(); if (uTxFlags & tfUniversalMask) { - JLOG(ctx.j.trace()) << "Malformed transaction: " << - "Invalid flags set."; + JLOG(ctx.j.trace()) << "Malformed transaction: " + << "Invalid flags set."; return temINVALID_FLAG; } - auto const seq = ctx.tx.getFieldU32 (sfOfferSequence); - if (! seq) + auto const seq = ctx.tx.getFieldU32(sfOfferSequence); + if (!seq) { - JLOG(ctx.j.trace()) << - "CancelOffer::preflight: missing sequence"; + JLOG(ctx.j.trace()) << "CancelOffer::preflight: missing sequence"; return temBAD_SEQUENCE; } @@ -60,13 +59,13 @@ CancelOffer::preclaim(PreclaimContext const& ctx) auto const offerSequence = ctx.tx[sfOfferSequence]; auto const sle = ctx.view.read(keylet::account(id)); - if (! sle) + if (!sle) return terNO_ACCOUNT; if ((*sle)[sfSequence] <= offerSequence) { - JLOG(ctx.j.trace()) << "Malformed transaction: " << - "Sequence " << offerSequence << " is invalid."; + JLOG(ctx.j.trace()) << "Malformed transaction: " + << "Sequence " << offerSequence << " is invalid."; return temBAD_SEQUENCE; } @@ -76,27 +75,26 @@ CancelOffer::preclaim(PreclaimContext const& ctx) //------------------------------------------------------------------------------ TER -CancelOffer::doApply () +CancelOffer::doApply() { auto const offerSequence = ctx_.tx[sfOfferSequence]; auto const sle = view().read(keylet::account(account_)); - if (! sle) + if (!sle) return tefINTERNAL; - uint256 const offerIndex (getOfferIndex (account_, offerSequence)); + uint256 const offerIndex(getOfferIndex(account_, offerSequence)); - auto sleOffer = view().peek ( - keylet::offer(offerIndex)); + auto sleOffer = view().peek(keylet::offer(offerIndex)); if (sleOffer) { JLOG(j_.debug()) << "Trying to cancel offer #" << offerSequence; - return offerDelete (view(), sleOffer, ctx_.app.journal("View")); + return offerDelete(view(), sleOffer, ctx_.app.journal("View")); } JLOG(j_.debug()) << "Offer #" << offerSequence << " can't be found."; return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CancelOffer.h b/src/ripple/app/tx/impl/CancelOffer.h index ea37ff7f78..36efdeb271 100644 --- a/src/ripple/app/tx/impl/CancelOffer.h +++ b/src/ripple/app/tx/impl/CancelOffer.h @@ -27,26 +27,23 @@ namespace ripple { -class CancelOffer - : public Transactor +class CancelOffer : public Transactor { public: - explicit CancelOffer (ApplyContext& ctx) - : Transactor(ctx) + explicit CancelOffer(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER + static TER preclaim(PreclaimContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CancelTicket.cpp b/src/ripple/app/tx/impl/CancelTicket.cpp index e2185300c8..c69cd21072 100644 --- a/src/ripple/app/tx/impl/CancelTicket.cpp +++ b/src/ripple/app/tx/impl/CancelTicket.cpp @@ -19,53 +19,51 @@ #include #include +#include #include #include #include -#include namespace ripple { NotTEC -CancelTicket::preflight (PreflightContext const& ctx) +CancelTicket::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled(featureTickets)) + if (!ctx.rules.enabled(featureTickets)) return temDISABLED; if (ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - return preflight2 (ctx); + return preflight2(ctx); } TER -CancelTicket::doApply () +CancelTicket::doApply() { - uint256 const ticketId = ctx_.tx.getFieldH256 (sfTicketID); + uint256 const ticketId = ctx_.tx.getFieldH256(sfTicketID); // VFALCO This is highly suspicious, we're requiring that the // transaction provide the return value of getTicketIndex? - SLE::pointer sleTicket = view().peek (keylet::ticket(ticketId)); + SLE::pointer sleTicket = view().peek(keylet::ticket(ticketId)); if (!sleTicket) return tecNO_ENTRY; - auto const ticket_owner = - sleTicket->getAccountID (sfAccount); + auto const ticket_owner = sleTicket->getAccountID(sfAccount); - bool authorized = - account_ == ticket_owner; + bool authorized = account_ == ticket_owner; // The target can also always remove a ticket - if (!authorized && sleTicket->isFieldPresent (sfTarget)) - authorized = (account_ == sleTicket->getAccountID (sfTarget)); + if (!authorized && sleTicket->isFieldPresent(sfTarget)) + authorized = (account_ == sleTicket->getAccountID(sfTarget)); // And finally, anyone can remove an expired ticket - if (!authorized && sleTicket->isFieldPresent (sfExpiration)) + if (!authorized && sleTicket->isFieldPresent(sfExpiration)) { using tp = NetClock::time_point; using d = tp::duration; @@ -78,20 +76,20 @@ CancelTicket::doApply () if (!authorized) return tecNO_PERMISSION; - std::uint64_t const hint (sleTicket->getFieldU64 (sfOwnerNode)); + std::uint64_t const hint(sleTicket->getFieldU64(sfOwnerNode)); - if (! ctx_.view().dirRemove( + if (!ctx_.view().dirRemove( keylet::ownerDir(ticket_owner), hint, ticketId, false)) { return tefBAD_LEDGER; } - auto viewJ = ctx_.app.journal ("View"); - adjustOwnerCount(view(), view().peek( - keylet::account(ticket_owner)), -1, viewJ); - ctx_.view ().erase (sleTicket); + auto viewJ = ctx_.app.journal("View"); + adjustOwnerCount( + view(), view().peek(keylet::account(ticket_owner)), -1, viewJ); + ctx_.view().erase(sleTicket); return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CancelTicket.h b/src/ripple/app/tx/impl/CancelTicket.h index a82093dd58..9cf2bc7dcb 100644 --- a/src/ripple/app/tx/impl/CancelTicket.h +++ b/src/ripple/app/tx/impl/CancelTicket.h @@ -26,22 +26,20 @@ namespace ripple { -class CancelTicket - : public Transactor +class CancelTicket : public Transactor { public: - explicit CancelTicket (ApplyContext& ctx) - : Transactor(ctx) + explicit CancelTicket(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CashCheck.cpp b/src/ripple/app/tx/impl/CashCheck.cpp index d546b7779e..6fa5c8aeff 100644 --- a/src/ripple/app/tx/impl/CashCheck.cpp +++ b/src/ripple/app/tx/impl/CashCheck.cpp @@ -17,9 +17,9 @@ */ //============================================================================== -#include #include #include +#include #include #include #include @@ -32,13 +32,13 @@ namespace ripple { NotTEC -CashCheck::preflight (PreflightContext const& ctx) +CashCheck::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled (featureChecks)) + if (!ctx.rules.enabled(featureChecks)) return temDISABLED; - NotTEC const ret {preflight1 (ctx)}; - if (! isTesSuccess (ret)) + NotTEC const ret{preflight1(ctx)}; + if (!isTesSuccess(ret)) return ret; if (ctx.tx.getFlags() & tfUniversalMask) @@ -54,47 +54,48 @@ CashCheck::preflight (PreflightContext const& ctx) if (static_cast(optAmount) == static_cast(optDeliverMin)) { - JLOG(ctx.j.warn()) << "Malformed transaction: " - "does not specify exactly one of Amount and DeliverMin."; + JLOG(ctx.j.warn()) + << "Malformed transaction: " + "does not specify exactly one of Amount and DeliverMin."; return temMALFORMED; } // Make sure the amount is valid. - STAmount const value {optAmount ? *optAmount : *optDeliverMin}; - if (!isLegalNet (value) || value.signum() <= 0) + STAmount const value{optAmount ? *optAmount : *optDeliverMin}; + if (!isLegalNet(value) || value.signum() <= 0) { JLOG(ctx.j.warn()) << "Malformed transaction: bad amount: " - << value.getFullText(); + << value.getFullText(); return temBAD_AMOUNT; } if (badCurrency() == value.getCurrency()) { - JLOG(ctx.j.warn()) <<"Malformed transaction: Bad currency."; + JLOG(ctx.j.warn()) << "Malformed transaction: Bad currency."; return temBAD_CURRENCY; } - return preflight2 (ctx); + return preflight2(ctx); } TER -CashCheck::preclaim (PreclaimContext const& ctx) +CashCheck::preclaim(PreclaimContext const& ctx) { - auto const sleCheck = ctx.view.read (keylet::check (ctx.tx[sfCheckID])); - if (! sleCheck) + auto const sleCheck = ctx.view.read(keylet::check(ctx.tx[sfCheckID])); + if (!sleCheck) { JLOG(ctx.j.warn()) << "Check does not exist."; return tecNO_ENTRY; } // Only cash a check with this account as the destination. - AccountID const dstId {(*sleCheck)[sfDestination]}; + AccountID const dstId{(*sleCheck)[sfDestination]}; if (ctx.tx[sfAccount] != dstId) { JLOG(ctx.j.warn()) << "Cashing a check with wrong Destination."; return tecNO_PERMISSION; } - AccountID const srcId {(*sleCheck)[sfAccount]}; + AccountID const srcId{(*sleCheck)[sfAccount]}; if (srcId == dstId) { // They wrote a check to themselves. This should be caught when @@ -103,8 +104,8 @@ CashCheck::preclaim (PreclaimContext const& ctx) return tecINTERNAL; } { - auto const sleSrc = ctx.view.read (keylet::account (srcId)); - auto const sleDst = ctx.view.read (keylet::account (dstId)); + auto const sleSrc = ctx.view.read(keylet::account(srcId)); + auto const sleDst = ctx.view.read(keylet::account(dstId)); if (!sleSrc || !sleDst) { // If the check exists this should never occur. @@ -114,7 +115,7 @@ CashCheck::preclaim (PreclaimContext const& ctx) } if ((sleDst->getFlags() & lsfRequireDestTag) && - !sleCheck->isFieldPresent (sfDestinationTag)) + !sleCheck->isFieldPresent(sfDestinationTag)) { // The tag is basically account-specific information we don't // understand, but we can require someone to fill it in. @@ -133,7 +134,7 @@ CashCheck::preclaim (PreclaimContext const& ctx) // we do not know the closing time of the ledger that is under // construction. if (optExpiry && - (ctx.view.parentCloseTime() >= timepoint {duration {*optExpiry}})) + (ctx.view.parentCloseTime() >= timepoint{duration{*optExpiry}})) { JLOG(ctx.j.warn()) << "Cashing a check that has already expired."; return tecEXPIRED; @@ -142,20 +143,19 @@ CashCheck::preclaim (PreclaimContext const& ctx) { // Preflight verified exactly one of Amount or DeliverMin is present. // Make sure the requested amount is reasonable. - STAmount const value {[] (STTx const& tx) - { - auto const optAmount = tx[~sfAmount]; - return optAmount ? *optAmount : tx[sfDeliverMin]; - } (ctx.tx)}; + STAmount const value{[](STTx const& tx) { + auto const optAmount = tx[~sfAmount]; + return optAmount ? *optAmount : tx[sfDeliverMin]; + }(ctx.tx)}; - STAmount const sendMax {(*sleCheck)[sfSendMax]}; - Currency const currency {value.getCurrency()}; + STAmount const sendMax{(*sleCheck)[sfSendMax]}; + Currency const currency{value.getCurrency()}; if (currency != sendMax.getCurrency()) { JLOG(ctx.j.warn()) << "Check cash does not match check currency."; return temMALFORMED; } - AccountID const issuerId {value.getIssuer()}; + AccountID const issuerId{value.getIssuer()}; if (issuerId != sendMax.getIssuer()) { JLOG(ctx.j.warn()) << "Check cash does not match check issuer."; @@ -170,8 +170,12 @@ CashCheck::preclaim (PreclaimContext const& ctx) // Make sure the check owner holds at least value. If they have // less than value the check cannot be cashed. { - STAmount availableFunds {accountFunds (ctx.view, - (*sleCheck)[sfAccount], value, fhZERO_IF_FROZEN, ctx.j)}; + STAmount availableFunds{accountFunds( + ctx.view, + (*sleCheck)[sfAccount], + value, + fhZERO_IF_FROZEN, + ctx.j)}; // Note that src will have one reserve's worth of additional XRP // once the check is cashed, since the check's reserve will no @@ -189,23 +193,23 @@ CashCheck::preclaim (PreclaimContext const& ctx) } // An issuer can always accept their own currency. - if (! value.native() && (value.getIssuer() != dstId)) + if (!value.native() && (value.getIssuer() != dstId)) { - auto const sleTrustLine = ctx.view.read ( - keylet::line (dstId, issuerId, currency)); - if (! sleTrustLine) + auto const sleTrustLine = + ctx.view.read(keylet::line(dstId, issuerId, currency)); + if (!sleTrustLine) { JLOG(ctx.j.warn()) << "Cannot cash check for IOU without trustline."; return tecNO_LINE; } - auto const sleIssuer = ctx.view.read (keylet::account (issuerId)); - if (! sleIssuer) + auto const sleIssuer = ctx.view.read(keylet::account(issuerId)); + if (!sleIssuer) { JLOG(ctx.j.warn()) << "Can't receive IOUs from non-existent issuer: " - << to_string (issuerId); + << to_string(issuerId); return tecNO_ISSUER; } @@ -214,12 +218,13 @@ CashCheck::preclaim (PreclaimContext const& ctx) // Entries have a canonical representation, determined by a // lexicographical "greater than" comparison employing strict // weak ordering. Determine which entry we need to access. - bool const canonical_gt (dstId > issuerId); + bool const canonical_gt(dstId > issuerId); - bool const is_authorized ((*sleTrustLine)[sfFlags] & + bool const is_authorized( + (*sleTrustLine)[sfFlags] & (canonical_gt ? lsfLowAuth : lsfHighAuth)); - if (! is_authorized) + if (!is_authorized) { JLOG(ctx.j.warn()) << "Can't receive IOUs from issuer without auth."; @@ -233,10 +238,9 @@ CashCheck::preclaim (PreclaimContext const& ctx) // However, the trustline from destination to issuer may not // be frozen. - if (isFrozen (ctx.view, dstId, currency, issuerId)) + if (isFrozen(ctx.view, dstId, currency, issuerId)) { - JLOG(ctx.j.warn()) - << "Cashing a check to a frozen trustline."; + JLOG(ctx.j.warn()) << "Cashing a check to a frozen trustline."; return tecFROZEN; } } @@ -245,24 +249,23 @@ CashCheck::preclaim (PreclaimContext const& ctx) } TER -CashCheck::doApply () +CashCheck::doApply() { // Flow requires that we operate on a PaymentSandbox, rather than // directly on a View. - PaymentSandbox psb (&ctx_.view()); + PaymentSandbox psb(&ctx_.view()); - uint256 const checkKey {ctx_.tx[sfCheckID]}; - auto const sleCheck = psb.peek (keylet::check (checkKey)); - if (! sleCheck) + uint256 const checkKey{ctx_.tx[sfCheckID]}; + auto const sleCheck = psb.peek(keylet::check(checkKey)); + if (!sleCheck) { - JLOG(j_.fatal()) - << "Precheck did not verify check's existence."; + JLOG(j_.fatal()) << "Precheck did not verify check's existence."; return tecFAILED_PROCESSING; } - AccountID const srcId {sleCheck->getAccountID (sfAccount)}; - auto const sleSrc = psb.peek (keylet::account (srcId)); - auto const sleDst = psb.peek (keylet::account (account_)); + AccountID const srcId{sleCheck->getAccountID(sfAccount)}; + auto const sleSrc = psb.peek(keylet::account(srcId)); + auto const sleDst = psb.peek(keylet::account(account_)); if (!sleSrc || !sleDst) { @@ -281,12 +284,12 @@ CashCheck::doApply () // // If it is not a check to self (as should be the case), then there's // work to do... - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); auto const optDeliverMin = ctx_.tx[~sfDeliverMin]; - bool const doFix1623 {ctx_.view().rules().enabled (fix1623)}; + bool const doFix1623{ctx_.view().rules().enabled(fix1623)}; if (srcId != account_) { - STAmount const sendMax {sleCheck->getFieldAmount (sfSendMax)}; + STAmount const sendMax{sleCheck->getFieldAmount(sfSendMax)}; // Flow() doesn't do XRP to XRP transfers. if (sendMax.native()) @@ -299,30 +302,32 @@ CashCheck::doApply () // from src's directory, we allow them to send that additional // incremental reserve amount in the transfer. Hence the -1 // argument. - STAmount const srcLiquid {xrpLiquid (psb, srcId, -1, viewJ)}; + STAmount const srcLiquid{xrpLiquid(psb, srcId, -1, viewJ)}; // Now, how much do they need in order to be successful? - STAmount const xrpDeliver {optDeliverMin ? - std::max (*optDeliverMin, std::min (sendMax, srcLiquid)) : - ctx_.tx.getFieldAmount (sfAmount)}; + STAmount const xrpDeliver{ + optDeliverMin + ? std::max(*optDeliverMin, std::min(sendMax, srcLiquid)) + : ctx_.tx.getFieldAmount(sfAmount)}; if (srcLiquid < xrpDeliver) { // Vote no. However the transaction might succeed if applied // in a different order. JLOG(j_.trace()) << "Cash Check: Insufficient XRP: " - << srcLiquid.getFullText() - << " < " << xrpDeliver.getFullText(); + << srcLiquid.getFullText() << " < " + << xrpDeliver.getFullText(); return tecUNFUNDED_PAYMENT; } if (optDeliverMin && doFix1623) // Set the DeliveredAmount metadata. - ctx_.deliver (xrpDeliver); + ctx_.deliver(xrpDeliver); // The source account has enough XRP so make the ledger change. - if (TER const ter {transferXRP ( - psb, srcId, account_, xrpDeliver, viewJ)}; ter != tesSUCCESS) + if (TER const ter{ + transferXRP(psb, srcId, account_, xrpDeliver, viewJ)}; + ter != tesSUCCESS) { // The transfer failed. Return the error code. return ter; @@ -337,26 +342,29 @@ CashCheck::doApply () // maximum possible currency because there might be a gateway // transfer rate to account for. Since the transfer rate cannot // exceed 200%, we use 1/2 maxValue as our limit. - STAmount const flowDeliver {optDeliverMin ? - STAmount { optDeliverMin->issue(), - STAmount::cMaxValue / 2, STAmount::cMaxOffset } : - static_cast(ctx_.tx[sfAmount])}; + STAmount const flowDeliver{ + optDeliverMin + ? STAmount{optDeliverMin->issue(), STAmount::cMaxValue / 2, STAmount::cMaxOffset} + : static_cast(ctx_.tx[sfAmount])}; // Call the payment engine's flow() to do the actual work. - auto const result = flow (psb, flowDeliver, srcId, account_, + auto const result = flow( + psb, + flowDeliver, + srcId, + account_, STPathSet{}, true, // default path static_cast(optDeliverMin), // partial payment true, // owner pays transfer fee false, // offer crossing boost::none, - sleCheck->getFieldAmount (sfSendMax), + sleCheck->getFieldAmount(sfSendMax), viewJ); if (result.result() != tesSUCCESS) { - JLOG(ctx_.journal.warn()) - << "flow failed when cashing check."; + JLOG(ctx_.journal.warn()) << "flow failed when cashing check."; return result.result(); } @@ -365,12 +373,13 @@ CashCheck::doApply () { if (result.actualAmountOut < *optDeliverMin) { - JLOG(ctx_.journal.warn()) << "flow did not produce DeliverMin."; + JLOG(ctx_.journal.warn()) + << "flow did not produce DeliverMin."; return tecPATH_PARTIAL; } if (doFix1623) // Set the delivered_amount metadata. - ctx_.deliver (result.actualAmountOut); + ctx_.deliver(result.actualAmountOut); } } } @@ -379,8 +388,8 @@ CashCheck::doApply () // check link from destination directory. if (srcId != account_) { - std::uint64_t const page {(*sleCheck)[sfDestinationNode]}; - if (! ctx_.view().dirRemove( + std::uint64_t const page{(*sleCheck)[sfDestinationNode]}; + if (!ctx_.view().dirRemove( keylet::ownerDir(account_), page, checkKey, true)) { JLOG(j_.warn()) << "Unable to delete check from destination."; @@ -389,8 +398,8 @@ CashCheck::doApply () } // Remove check from check owner's directory. { - std::uint64_t const page {(*sleCheck)[sfOwnerNode]}; - if (! ctx_.view().dirRemove( + std::uint64_t const page{(*sleCheck)[sfOwnerNode]}; + if (!ctx_.view().dirRemove( keylet::ownerDir(srcId), page, checkKey, true)) { JLOG(j_.warn()) << "Unable to delete check from owner."; @@ -398,13 +407,13 @@ CashCheck::doApply () } } // If we succeeded, update the check owner's reserve. - adjustOwnerCount (psb, sleSrc, -1, viewJ); + adjustOwnerCount(psb, sleSrc, -1, viewJ); // Remove check from ledger. - psb.erase (sleCheck); + psb.erase(sleCheck); - psb.apply (ctx_.rawView()); + psb.apply(ctx_.rawView()); return tesSUCCESS; } -} // namespace ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CashCheck.h b/src/ripple/app/tx/impl/CashCheck.h index 1b6816ca2d..c8c863d3e1 100644 --- a/src/ripple/app/tx/impl/CashCheck.h +++ b/src/ripple/app/tx/impl/CashCheck.h @@ -24,26 +24,23 @@ namespace ripple { -class CashCheck - : public Transactor +class CashCheck : public Transactor { public: - explicit CashCheck (ApplyContext& ctx) - : Transactor (ctx) + explicit CashCheck(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); + + static TER + preclaim(PreclaimContext const& ctx); - static TER - preclaim (PreclaimContext const& ctx); - - TER doApply () override; + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/Change.cpp b/src/ripple/app/tx/impl/Change.cpp index 7653aaaa81..a75962b001 100644 --- a/src/ripple/app/tx/impl/Change.cpp +++ b/src/ripple/app/tx/impl/Change.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include #include #include #include +#include #include #include #include @@ -28,7 +28,7 @@ namespace ripple { NotTEC -Change::preflight (PreflightContext const& ctx) +Change::preflight(PreflightContext const& ctx) { auto const ret = preflight0(ctx); if (!isTesSuccess(ret)) @@ -42,22 +42,21 @@ Change::preflight (PreflightContext const& ctx) } // No point in going any further if the transaction fee is malformed. - auto const fee = ctx.tx.getFieldAmount (sfFee); - if (!fee.native () || fee != beast::zero) + auto const fee = ctx.tx.getFieldAmount(sfFee); + if (!fee.native() || fee != beast::zero) { JLOG(ctx.j.warn()) << "Change: invalid fee"; return temBAD_FEE; } - if (!ctx.tx.getSigningPubKey ().empty () || - !ctx.tx.getSignature ().empty () || - ctx.tx.isFieldPresent (sfSigners)) + if (!ctx.tx.getSigningPubKey().empty() || !ctx.tx.getSignature().empty() || + ctx.tx.isFieldPresent(sfSigners)) { JLOG(ctx.j.warn()) << "Change: Bad signature"; return temBAD_SIGNATURE; } - if (ctx.tx.getSequence () != 0 || ctx.tx.isFieldPresent (sfPreviousTxnID)) + if (ctx.tx.getSequence() != 0 || ctx.tx.isFieldPresent(sfPreviousTxnID)) { JLOG(ctx.j.warn()) << "Change: Bad sequence"; return temBAD_SEQUENCE; @@ -67,7 +66,7 @@ Change::preflight (PreflightContext const& ctx) } TER -Change::preclaim(PreclaimContext const &ctx) +Change::preclaim(PreclaimContext const& ctx) { // If tapOPEN_LEDGER is resurrected into ApplyFlags, // this block can be moved to preflight. @@ -77,22 +76,20 @@ Change::preclaim(PreclaimContext const &ctx) return temINVALID; } - if (ctx.tx.getTxnType() != ttAMENDMENT - && ctx.tx.getTxnType() != ttFEE) + if (ctx.tx.getTxnType() != ttAMENDMENT && ctx.tx.getTxnType() != ttFEE) return temUNKNOWN; return tesSUCCESS; } - TER Change::doApply() { - if (ctx_.tx.getTxnType () == ttAMENDMENT) - return applyAmendment (); + if (ctx_.tx.getTxnType() == ttAMENDMENT) + return applyAmendment(); assert(ctx_.tx.getTxnType() == ttFEE); - return applyFee (); + return applyFee(); } void @@ -105,12 +102,11 @@ Change::preCompute() TER Change::applyAmendment() { - uint256 amendment (ctx_.tx.getFieldH256 (sfAmendment)); + uint256 amendment(ctx_.tx.getFieldH256(sfAmendment)); auto const k = keylet::amendments(); - SLE::pointer amendmentObject = - view().peek (k); + SLE::pointer amendmentObject = view().peek(k); if (!amendmentObject) { @@ -118,14 +114,13 @@ Change::applyAmendment() view().insert(amendmentObject); } - STVector256 amendments = - amendmentObject->getFieldV256(sfAmendments); + STVector256 amendments = amendmentObject->getFieldV256(sfAmendments); - if (std::find (amendments.begin(), amendments.end(), - amendment) != amendments.end ()) + if (std::find(amendments.begin(), amendments.end(), amendment) != + amendments.end()) return tefALREADY; - auto flags = ctx_.tx.getFlags (); + auto flags = ctx_.tx.getFlags(); const bool gotMajority = (flags & tfGotMajority) != 0; const bool lostMajority = (flags & tfLostMajority) != 0; @@ -133,15 +128,16 @@ Change::applyAmendment() if (gotMajority && lostMajority) return temINVALID_FLAG; - STArray newMajorities (sfMajorities); + STArray newMajorities(sfMajorities); bool found = false; - if (amendmentObject->isFieldPresent (sfMajorities)) + if (amendmentObject->isFieldPresent(sfMajorities)) { - const STArray &oldMajorities = amendmentObject->getFieldArray (sfMajorities); + const STArray& oldMajorities = + amendmentObject->getFieldArray(sfMajorities); for (auto const& majority : oldMajorities) { - if (majority.getFieldH256 (sfAmendment) == amendment) + if (majority.getFieldH256(sfAmendment) == amendment) { if (gotMajority) return tefALREADY; @@ -150,53 +146,51 @@ Change::applyAmendment() else { // pass through - newMajorities.push_back (majority); + newMajorities.push_back(majority); } } } - if (! found && lostMajority) + if (!found && lostMajority) return tefALREADY; if (gotMajority) { // This amendment now has a majority - newMajorities.push_back (STObject (sfMajority)); - auto& entry = newMajorities.back (); - entry.emplace_back (STHash256 (sfAmendment, amendment)); - entry.emplace_back (STUInt32 (sfCloseTime, - view().parentCloseTime().time_since_epoch().count())); + newMajorities.push_back(STObject(sfMajority)); + auto& entry = newMajorities.back(); + entry.emplace_back(STHash256(sfAmendment, amendment)); + entry.emplace_back(STUInt32( + sfCloseTime, view().parentCloseTime().time_since_epoch().count())); - if (!ctx_.app.getAmendmentTable ().isSupported (amendment)) + if (!ctx_.app.getAmendmentTable().isSupported(amendment)) { - JLOG (j_.warn()) << - "Unsupported amendment " << amendment << - " received a majority."; + JLOG(j_.warn()) << "Unsupported amendment " << amendment + << " received a majority."; } } else if (!lostMajority) { // No flags, enable amendment - amendments.push_back (amendment); - amendmentObject->setFieldV256 (sfAmendments, amendments); + amendments.push_back(amendment); + amendmentObject->setFieldV256(sfAmendments, amendments); - ctx_.app.getAmendmentTable ().enable (amendment); + ctx_.app.getAmendmentTable().enable(amendment); - if (!ctx_.app.getAmendmentTable ().isSupported (amendment)) + if (!ctx_.app.getAmendmentTable().isSupported(amendment)) { - JLOG (j_.error()) << - "Unsupported amendment " << amendment << - " activated: server blocked."; - ctx_.app.getOPs ().setAmendmentBlocked (); + JLOG(j_.error()) << "Unsupported amendment " << amendment + << " activated: server blocked."; + ctx_.app.getOPs().setAmendmentBlocked(); } } - if (newMajorities.empty ()) - amendmentObject->makeFieldAbsent (sfMajorities); + if (newMajorities.empty()) + amendmentObject->makeFieldAbsent(sfMajorities); else - amendmentObject->setFieldArray (sfMajorities, newMajorities); + amendmentObject->setFieldArray(sfMajorities, newMajorities); - view().update (amendmentObject); + view().update(amendmentObject); return tesSUCCESS; } @@ -206,7 +200,7 @@ Change::applyFee() { auto const k = keylet::fees(); - SLE::pointer feeObject = view().peek (k); + SLE::pointer feeObject = view().peek(k); if (!feeObject) { @@ -214,19 +208,17 @@ Change::applyFee() view().insert(feeObject); } - feeObject->setFieldU64 ( - sfBaseFee, ctx_.tx.getFieldU64 (sfBaseFee)); - feeObject->setFieldU32 ( - sfReferenceFeeUnits, ctx_.tx.getFieldU32 (sfReferenceFeeUnits)); - feeObject->setFieldU32 ( - sfReserveBase, ctx_.tx.getFieldU32 (sfReserveBase)); - feeObject->setFieldU32 ( - sfReserveIncrement, ctx_.tx.getFieldU32 (sfReserveIncrement)); + feeObject->setFieldU64(sfBaseFee, ctx_.tx.getFieldU64(sfBaseFee)); + feeObject->setFieldU32( + sfReferenceFeeUnits, ctx_.tx.getFieldU32(sfReferenceFeeUnits)); + feeObject->setFieldU32(sfReserveBase, ctx_.tx.getFieldU32(sfReserveBase)); + feeObject->setFieldU32( + sfReserveIncrement, ctx_.tx.getFieldU32(sfReserveIncrement)); - view().update (feeObject); + view().update(feeObject); JLOG(j_.warn()) << "Fees have been changed"; return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/Change.h b/src/ripple/app/tx/impl/Change.h index a60bd9f11e..54e0aa4f92 100644 --- a/src/ripple/app/tx/impl/Change.h +++ b/src/ripple/app/tx/impl/Change.h @@ -29,41 +29,38 @@ namespace ripple { -class Change - : public Transactor +class Change : public Transactor { public: - explicit Change (ApplyContext& ctx) - : Transactor(ctx) + explicit Change(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - TER doApply () override; - void preCompute() override; + TER + doApply() override; + void + preCompute() override; - static - FeeUnit64 - calculateBaseFee ( - ReadView const& view, - STTx const& tx) + static FeeUnit64 + calculateBaseFee(ReadView const& view, STTx const& tx) { return FeeUnit64{0}; } - static - TER - preclaim(PreclaimContext const &ctx); + static TER + preclaim(PreclaimContext const& ctx); private: - TER applyAmendment (); + TER + applyAmendment(); - TER applyFee (); + TER + applyFee(); }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CreateCheck.cpp b/src/ripple/app/tx/impl/CreateCheck.cpp index b14b85bc47..ed3ad0122c 100644 --- a/src/ripple/app/tx/impl/CreateCheck.cpp +++ b/src/ripple/app/tx/impl/CreateCheck.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -29,13 +29,13 @@ namespace ripple { NotTEC -CreateCheck::preflight (PreflightContext const& ctx) +CreateCheck::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled (featureChecks)) + if (!ctx.rules.enabled(featureChecks)) return temDISABLED; - NotTEC const ret {preflight1 (ctx)}; - if (! isTesSuccess (ret)) + NotTEC const ret{preflight1(ctx)}; + if (!isTesSuccess(ret)) return ret; if (ctx.tx.getFlags() & tfUniversalMask) @@ -52,17 +52,17 @@ CreateCheck::preflight (PreflightContext const& ctx) } { - STAmount const sendMax {ctx.tx.getFieldAmount (sfSendMax)}; - if (!isLegalNet (sendMax) || sendMax.signum() <= 0) + STAmount const sendMax{ctx.tx.getFieldAmount(sfSendMax)}; + if (!isLegalNet(sendMax) || sendMax.signum() <= 0) { JLOG(ctx.j.warn()) << "Malformed transaction: bad sendMax amount: " - << sendMax.getFullText(); + << sendMax.getFullText(); return temBAD_AMOUNT; } if (badCurrency() == sendMax.getCurrency()) { - JLOG(ctx.j.warn()) <<"Malformed transaction: Bad currency."; + JLOG(ctx.j.warn()) << "Malformed transaction: Bad currency."; return temBAD_CURRENCY; } } @@ -76,22 +76,22 @@ CreateCheck::preflight (PreflightContext const& ctx) } } - return preflight2 (ctx); + return preflight2(ctx); } TER -CreateCheck::preclaim (PreclaimContext const& ctx) +CreateCheck::preclaim(PreclaimContext const& ctx) { - AccountID const dstId {ctx.tx[sfDestination]}; - auto const sleDst = ctx.view.read (keylet::account (dstId)); - if (! sleDst) + AccountID const dstId{ctx.tx[sfDestination]}; + auto const sleDst = ctx.view.read(keylet::account(dstId)); + if (!sleDst) { JLOG(ctx.j.warn()) << "Destination account does not exist."; return tecNO_DST; } if ((sleDst->getFlags() & lsfRequireDestTag) && - !ctx.tx.isFieldPresent (sfDestinationTag)) + !ctx.tx.isFieldPresent(sfDestinationTag)) { // The tag is basically account-specific information we don't // understand, but we can require someone to fill it in. @@ -100,12 +100,12 @@ CreateCheck::preclaim (PreclaimContext const& ctx) } { - STAmount const sendMax {ctx.tx[sfSendMax]}; - if (! sendMax.native()) + STAmount const sendMax{ctx.tx[sfSendMax]}; + if (!sendMax.native()) { // The currency may not be globally frozen - AccountID const& issuerId {sendMax.getIssuer()}; - if (isGlobalFrozen (ctx.view, issuerId)) + AccountID const& issuerId{sendMax.getIssuer()}; + if (isGlobalFrozen(ctx.view, issuerId)) { JLOG(ctx.j.warn()) << "Creating a check for frozen asset"; return tecFROZEN; @@ -115,14 +115,14 @@ CreateCheck::preclaim (PreclaimContext const& ctx) // // Note that we DO allow create check for a currency that the // account does not yet have a trustline to. - AccountID const srcId {ctx.tx.getAccountID (sfAccount)}; + AccountID const srcId{ctx.tx.getAccountID(sfAccount)}; if (issuerId != srcId) { // Check if the issuer froze the line - auto const sleTrust = ctx.view.read ( - keylet::line (srcId, issuerId, sendMax.getCurrency())); + auto const sleTrust = ctx.view.read( + keylet::line(srcId, issuerId, sendMax.getCurrency())); if (sleTrust && - sleTrust->isFlag ( + sleTrust->isFlag( (issuerId > srcId) ? lsfHighFreeze : lsfLowFreeze)) { JLOG(ctx.j.warn()) @@ -133,10 +133,10 @@ CreateCheck::preclaim (PreclaimContext const& ctx) if (issuerId != dstId) { // Check if dst froze the line. - auto const sleTrust = ctx.view.read ( - keylet::line (issuerId, dstId, sendMax.getCurrency())); + auto const sleTrust = ctx.view.read( + keylet::line(issuerId, dstId, sendMax.getCurrency())); if (sleTrust && - sleTrust->isFlag ( + sleTrust->isFlag( (dstId > issuerId) ? lsfHighFreeze : lsfLowFreeze)) { JLOG(ctx.j.warn()) @@ -156,7 +156,7 @@ CreateCheck::preclaim (PreclaimContext const& ctx) // we do not know the closing time of the ledger that is under // construction. if (optExpiry && - (ctx.view.parentCloseTime() >= timepoint {duration {*optExpiry}})) + (ctx.view.parentCloseTime() >= timepoint{duration{*optExpiry}})) { JLOG(ctx.j.warn()) << "Creating a check that has already expired."; return tecEXPIRED; @@ -166,79 +166,87 @@ CreateCheck::preclaim (PreclaimContext const& ctx) } TER -CreateCheck::doApply () +CreateCheck::doApply() { - auto const sle = view().peek (keylet::account (account_)); - if (! sle) + auto const sle = view().peek(keylet::account(account_)); + if (!sle) return tefINTERNAL; // A check counts against the reserve of the issuing account, but we // check the starting balance because we want to allow dipping into the // reserve to pay fees. { - STAmount const reserve {view().fees().accountReserve ( - sle->getFieldU32 (sfOwnerCount) + 1)}; + STAmount const reserve{ + view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1)}; if (mPriorBalance < reserve) return tecINSUFFICIENT_RESERVE; } - AccountID const dstAccountId {ctx_.tx[sfDestination]}; - std::uint32_t const seq {ctx_.tx.getSequence()}; + AccountID const dstAccountId{ctx_.tx[sfDestination]}; + std::uint32_t const seq{ctx_.tx.getSequence()}; auto sleCheck = - std::make_shared(ltCHECK, getCheckIndex (account_, seq)); + std::make_shared(ltCHECK, getCheckIndex(account_, seq)); - sleCheck->setAccountID (sfAccount, account_); - sleCheck->setAccountID (sfDestination, dstAccountId); - sleCheck->setFieldU32 (sfSequence, seq); - sleCheck->setFieldAmount (sfSendMax, ctx_.tx[sfSendMax]); + sleCheck->setAccountID(sfAccount, account_); + sleCheck->setAccountID(sfDestination, dstAccountId); + sleCheck->setFieldU32(sfSequence, seq); + sleCheck->setFieldAmount(sfSendMax, ctx_.tx[sfSendMax]); if (auto const srcTag = ctx_.tx[~sfSourceTag]) - sleCheck->setFieldU32 (sfSourceTag, *srcTag); + sleCheck->setFieldU32(sfSourceTag, *srcTag); if (auto const dstTag = ctx_.tx[~sfDestinationTag]) - sleCheck->setFieldU32 (sfDestinationTag, *dstTag); + sleCheck->setFieldU32(sfDestinationTag, *dstTag); if (auto const invoiceId = ctx_.tx[~sfInvoiceID]) - sleCheck->setFieldH256 (sfInvoiceID, *invoiceId); + sleCheck->setFieldH256(sfInvoiceID, *invoiceId); if (auto const expiry = ctx_.tx[~sfExpiration]) - sleCheck->setFieldU32 (sfExpiration, *expiry); + sleCheck->setFieldU32(sfExpiration, *expiry); - view().insert (sleCheck); + view().insert(sleCheck); - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); // If it's not a self-send (and it shouldn't be), add Check to the // destination's owner directory. if (dstAccountId != account_) { - auto const page = dirAdd (view(), keylet::ownerDir (dstAccountId), - sleCheck->key(), false, describeOwnerDir (dstAccountId), viewJ); + auto const page = dirAdd( + view(), + keylet::ownerDir(dstAccountId), + sleCheck->key(), + false, + describeOwnerDir(dstAccountId), + viewJ); - JLOG(j_.trace()) - << "Adding Check to destination directory " - << to_string (sleCheck->key()) - << ": " << (page ? "success" : "failure"); + JLOG(j_.trace()) << "Adding Check to destination directory " + << to_string(sleCheck->key()) << ": " + << (page ? "success" : "failure"); - if (! page) + if (!page) return tecDIR_FULL; - sleCheck->setFieldU64 (sfDestinationNode, *page); + sleCheck->setFieldU64(sfDestinationNode, *page); } { - auto const page = dirAdd (view(), keylet::ownerDir (account_), - sleCheck->key(), false, describeOwnerDir (account_), viewJ); + auto const page = dirAdd( + view(), + keylet::ownerDir(account_), + sleCheck->key(), + false, + describeOwnerDir(account_), + viewJ); - JLOG(j_.trace()) - << "Adding Check to owner directory " - << to_string (sleCheck->key()) - << ": " << (page ? "success" : "failure"); + JLOG(j_.trace()) << "Adding Check to owner directory " + << to_string(sleCheck->key()) << ": " + << (page ? "success" : "failure"); - if (! page) + if (!page) return tecDIR_FULL; - sleCheck->setFieldU64 (sfOwnerNode, *page); + sleCheck->setFieldU64(sfOwnerNode, *page); } // If we succeeded, the new entry counts against the creator's reserve. - adjustOwnerCount (view(), sle, 1, viewJ); + adjustOwnerCount(view(), sle, 1, viewJ); return tesSUCCESS; } -} // namespace ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CreateCheck.h b/src/ripple/app/tx/impl/CreateCheck.h index 5a180ed36b..aa2639bff2 100644 --- a/src/ripple/app/tx/impl/CreateCheck.h +++ b/src/ripple/app/tx/impl/CreateCheck.h @@ -24,26 +24,23 @@ namespace ripple { -class CreateCheck - : public Transactor +class CreateCheck : public Transactor { public: - explicit CreateCheck (ApplyContext& ctx) - : Transactor (ctx) + explicit CreateCheck(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); + + static TER + preclaim(PreclaimContext const& ctx); - static TER - preclaim (PreclaimContext const& ctx); - - TER doApply () override; + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CreateOffer.cpp b/src/ripple/app/tx/impl/CreateOffer.cpp index 14908d5edf..b39cb41449 100644 --- a/src/ripple/app/tx/impl/CreateOffer.cpp +++ b/src/ripple/app/tx/impl/CreateOffer.cpp @@ -17,15 +17,15 @@ */ //============================================================================== -#include #include #include +#include +#include #include #include #include -#include #include -#include +#include namespace ripple { @@ -34,101 +34,91 @@ CreateOffer::calculateMaxSpend(STTx const& tx) { auto const& saTakerGets = tx[sfTakerGets]; - return saTakerGets.native() ? - saTakerGets.xrp() : beast::zero; + return saTakerGets.native() ? saTakerGets.xrp() : beast::zero; } NotTEC -CreateOffer::preflight (PreflightContext const& ctx) +CreateOffer::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto& tx = ctx.tx; auto& j = ctx.j; - std::uint32_t const uTxFlags = tx.getFlags (); + std::uint32_t const uTxFlags = tx.getFlags(); if (uTxFlags & tfOfferCreateMask) { - JLOG(j.debug()) << - "Malformed transaction: Invalid flags set."; + JLOG(j.debug()) << "Malformed transaction: Invalid flags set."; return temINVALID_FLAG; } - bool const bImmediateOrCancel (uTxFlags & tfImmediateOrCancel); - bool const bFillOrKill (uTxFlags & tfFillOrKill); + bool const bImmediateOrCancel(uTxFlags & tfImmediateOrCancel); + bool const bFillOrKill(uTxFlags & tfFillOrKill); if (bImmediateOrCancel && bFillOrKill) { - JLOG(j.debug()) << - "Malformed transaction: both IoC and FoK set."; + JLOG(j.debug()) << "Malformed transaction: both IoC and FoK set."; return temINVALID_FLAG; } - bool const bHaveExpiration (tx.isFieldPresent (sfExpiration)); + bool const bHaveExpiration(tx.isFieldPresent(sfExpiration)); - if (bHaveExpiration && (tx.getFieldU32 (sfExpiration) == 0)) + if (bHaveExpiration && (tx.getFieldU32(sfExpiration) == 0)) { - JLOG(j.debug()) << - "Malformed offer: bad expiration"; + JLOG(j.debug()) << "Malformed offer: bad expiration"; return temBAD_EXPIRATION; } - bool const bHaveCancel (tx.isFieldPresent (sfOfferSequence)); + bool const bHaveCancel(tx.isFieldPresent(sfOfferSequence)); - if (bHaveCancel && (tx.getFieldU32 (sfOfferSequence) == 0)) + if (bHaveCancel && (tx.getFieldU32(sfOfferSequence) == 0)) { - JLOG(j.debug()) << - "Malformed offer: bad cancel sequence"; + JLOG(j.debug()) << "Malformed offer: bad cancel sequence"; return temBAD_SEQUENCE; } STAmount saTakerPays = tx[sfTakerPays]; STAmount saTakerGets = tx[sfTakerGets]; - if (!isLegalNet (saTakerPays) || !isLegalNet (saTakerGets)) + if (!isLegalNet(saTakerPays) || !isLegalNet(saTakerGets)) return temBAD_AMOUNT; - if (saTakerPays.native () && saTakerGets.native ()) + if (saTakerPays.native() && saTakerGets.native()) { - JLOG(j.debug()) << - "Malformed offer: redundant (XRP for XRP)"; + JLOG(j.debug()) << "Malformed offer: redundant (XRP for XRP)"; return temBAD_OFFER; } if (saTakerPays <= beast::zero || saTakerGets <= beast::zero) { - JLOG(j.debug()) << - "Malformed offer: bad amount"; + JLOG(j.debug()) << "Malformed offer: bad amount"; return temBAD_OFFER; } - auto const& uPaysIssuerID = saTakerPays.getIssuer (); - auto const& uPaysCurrency = saTakerPays.getCurrency (); + auto const& uPaysIssuerID = saTakerPays.getIssuer(); + auto const& uPaysCurrency = saTakerPays.getCurrency(); - auto const& uGetsIssuerID = saTakerGets.getIssuer (); - auto const& uGetsCurrency = saTakerGets.getCurrency (); + auto const& uGetsIssuerID = saTakerGets.getIssuer(); + auto const& uGetsCurrency = saTakerGets.getCurrency(); if (uPaysCurrency == uGetsCurrency && uPaysIssuerID == uGetsIssuerID) { - JLOG(j.debug()) << - "Malformed offer: redundant (IOU for IOU)"; + JLOG(j.debug()) << "Malformed offer: redundant (IOU for IOU)"; return temREDUNDANT; } // We don't allow a non-native currency to use the currency code XRP. if (badCurrency() == uPaysCurrency || badCurrency() == uGetsCurrency) { - JLOG(j.debug()) << - "Malformed offer: bad currency"; + JLOG(j.debug()) << "Malformed offer: bad currency"; return temBAD_CURRENCY; } - if (saTakerPays.native () != !uPaysIssuerID || - saTakerGets.native () != !uGetsIssuerID) + if (saTakerPays.native() != !uPaysIssuerID || + saTakerGets.native() != !uGetsIssuerID) { - JLOG(j.warn()) << - "Malformed offer: bad issuer"; + JLOG(j.warn()) << "Malformed offer: bad issuer"; return temBAD_ISSUER; } @@ -151,7 +141,7 @@ CreateOffer::preclaim(PreclaimContext const& ctx) auto const cancelSequence = ctx.tx[~sfOfferSequence]; auto const sleCreator = ctx.view.read(keylet::account(id)); - if (! sleCreator) + if (!sleCreator) return terNO_ACCOUNT; std::uint32_t const uAccountSequence = sleCreator->getFieldU32(sfSequence); @@ -161,16 +151,16 @@ CreateOffer::preclaim(PreclaimContext const& ctx) if (isGlobalFrozen(ctx.view, uPaysIssuerID) || isGlobalFrozen(ctx.view, uGetsIssuerID)) { - JLOG(ctx.j.info()) << - "Offer involves frozen asset"; + JLOG(ctx.j.info()) << "Offer involves frozen asset"; return tecFROZEN; } - else if (accountFunds(ctx.view, id, saTakerGets, - fhZERO_IF_FROZEN, viewJ) <= beast::zero) + else if ( + accountFunds(ctx.view, id, saTakerGets, fhZERO_IF_FROZEN, viewJ) <= + beast::zero) { - JLOG(ctx.j.debug()) << - "delay: Offers must be at least partially funded."; + JLOG(ctx.j.debug()) + << "delay: Offers must be at least partially funded."; return tecUNFUNDED_OFFER; } @@ -178,9 +168,8 @@ CreateOffer::preclaim(PreclaimContext const& ctx) // before the transaction sequence number. else if (cancelSequence && (uAccountSequence <= *cancelSequence)) { - JLOG(ctx.j.debug()) << - "uAccountSequenceNext=" << uAccountSequence << - " uOfferSequence=" << *cancelSequence; + JLOG(ctx.j.debug()) << "uAccountSequenceNext=" << uAccountSequence + << " uOfferSequence=" << *cancelSequence; return temBAD_SEQUENCE; } @@ -192,8 +181,7 @@ CreateOffer::preclaim(PreclaimContext const& ctx) // Expiration is defined in terms of the close time of the parent ledger, // because we definitively know the time that it closed but we do not // know the closing time of the ledger that is under construction. - if (expiration && - (ctx.view.parentCloseTime() >= tp{d{*expiration}})) + if (expiration && (ctx.view.parentCloseTime() >= tp{d{*expiration}})) { // Note that this will get checked again in applyGuts, but it saves // us a call to checkAcceptAsset and possible false negative. @@ -201,15 +189,19 @@ CreateOffer::preclaim(PreclaimContext const& ctx) // The return code change is attached to featureChecks as a convenience. // The change is not big enough to deserve its own amendment. return ctx.view.rules().enabled(featureDepositPreauth) - ? TER {tecEXPIRED} - : TER {tesSUCCESS}; + ? TER{tecEXPIRED} + : TER{tesSUCCESS}; } // Make sure that we are authorized to hold what the taker will pay us. if (!saTakerPays.native()) { - auto result = checkAcceptAsset(ctx.view, ctx.flags, - id, ctx.j, Issue(uPaysCurrency, uPaysIssuerID)); + auto result = checkAcceptAsset( + ctx.view, + ctx.flags, + id, + ctx.j, + Issue(uPaysCurrency, uPaysIssuerID)); if (result != tesSUCCESS) return result; } @@ -218,25 +210,24 @@ CreateOffer::preclaim(PreclaimContext const& ctx) } TER -CreateOffer::checkAcceptAsset(ReadView const& view, - ApplyFlags const flags, AccountID const id, - beast::Journal const j, Issue const& issue) +CreateOffer::checkAcceptAsset( + ReadView const& view, + ApplyFlags const flags, + AccountID const id, + beast::Journal const j, + Issue const& issue) { // Only valid for custom currencies - assert (!isXRP (issue.currency)); + assert(!isXRP(issue.currency)); - auto const issuerAccount = view.read( - keylet::account(issue.account)); + auto const issuerAccount = view.read(keylet::account(issue.account)); if (!issuerAccount) { - JLOG(j.warn()) << - "delay: can't receive IOUs from non-existent issuer: " << - to_string (issue.account); + JLOG(j.warn()) << "delay: can't receive IOUs from non-existent issuer: " + << to_string(issue.account); - return (flags & tapRETRY) - ? TER {terNO_ACCOUNT} - : TER {tecNO_ISSUER}; + return (flags & tapRETRY) ? TER{terNO_ACCOUNT} : TER{tecNO_ISSUER}; } // This code is attached to the DepositPreauth amendment as a matter of @@ -248,32 +239,28 @@ CreateOffer::checkAcceptAsset(ReadView const& view, if ((*issuerAccount)[sfFlags] & lsfRequireAuth) { - auto const trustLine = view.read( - keylet::line(id, issue.account, issue.currency)); + auto const trustLine = + view.read(keylet::line(id, issue.account, issue.currency)); if (!trustLine) { - return (flags & tapRETRY) - ? TER {terNO_LINE} - : TER {tecNO_LINE}; + return (flags & tapRETRY) ? TER{terNO_LINE} : TER{tecNO_LINE}; } // Entries have a canonical representation, determined by a // lexicographical "greater than" comparison employing strict weak // ordering. Determine which entry we need to access. - bool const canonical_gt (id > issue.account); + bool const canonical_gt(id > issue.account); - bool const is_authorized ((*trustLine)[sfFlags] & - (canonical_gt ? lsfLowAuth : lsfHighAuth)); + bool const is_authorized( + (*trustLine)[sfFlags] & (canonical_gt ? lsfLowAuth : lsfHighAuth)); if (!is_authorized) { - JLOG(j.debug()) << - "delay: can't receive IOUs from issuer without auth."; + JLOG(j.debug()) + << "delay: can't receive IOUs from issuer without auth."; - return (flags & tapRETRY) - ? TER {terNO_AUTH} - : TER {tecNO_AUTH}; + return (flags & tapRETRY) ? TER{terNO_AUTH} : TER{tecNO_AUTH}; } } @@ -281,51 +268,57 @@ CreateOffer::checkAcceptAsset(ReadView const& view, } bool -CreateOffer::dry_offer (ApplyView& view, Offer const& offer) +CreateOffer::dry_offer(ApplyView& view, Offer const& offer) { - if (offer.fully_consumed ()) + if (offer.fully_consumed()) return true; - auto const amount = accountFunds(view, offer.owner(), - offer.amount().out, fhZERO_IF_FROZEN, ctx_.app.journal ("View")); + auto const amount = accountFunds( + view, + offer.owner(), + offer.amount().out, + fhZERO_IF_FROZEN, + ctx_.app.journal("View")); return (amount <= beast::zero); } std::pair -CreateOffer::select_path ( - bool have_direct, OfferStream const& direct, - bool have_bridge, OfferStream const& leg1, OfferStream const& leg2) +CreateOffer::select_path( + bool have_direct, + OfferStream const& direct, + bool have_bridge, + OfferStream const& leg1, + OfferStream const& leg2) { // If we don't have any viable path, why are we here?! - assert (have_direct || have_bridge); + assert(have_direct || have_bridge); // If there's no bridged path, the direct is the best by default. if (!have_bridge) - return std::make_pair (true, direct.tip ().quality ()); + return std::make_pair(true, direct.tip().quality()); - Quality const bridged_quality (composed_quality ( - leg1.tip ().quality (), leg2.tip ().quality ())); + Quality const bridged_quality( + composed_quality(leg1.tip().quality(), leg2.tip().quality())); if (have_direct) { // We compare the quality of the composed quality of the bridged // offers and compare it against the direct offer to pick the best. - Quality const direct_quality (direct.tip ().quality ()); + Quality const direct_quality(direct.tip().quality()); if (bridged_quality < direct_quality) - return std::make_pair (true, direct_quality); + return std::make_pair(true, direct_quality); } // Either there was no direct offer, or it didn't have a better quality // than the bridge. - return std::make_pair (false, bridged_quality); + return std::make_pair(false, bridged_quality); } bool -CreateOffer::reachedOfferCrossingLimit (Taker const& taker) const +CreateOffer::reachedOfferCrossingLimit(Taker const& taker) const { auto const crossings = - taker.get_direct_crossings () + - (2 * taker.get_bridge_crossings ()); + taker.get_direct_crossings() + (2 * taker.get_bridge_crossings()); // The crossing limit is part of the Ripple protocol and // changing it is a transaction-processing change. @@ -333,41 +326,53 @@ CreateOffer::reachedOfferCrossingLimit (Taker const& taker) const } std::pair -CreateOffer::bridged_cross ( +CreateOffer::bridged_cross( Taker& taker, ApplyView& view, ApplyView& view_cancel, NetClock::time_point const when) { - auto const& takerAmount = taker.original_offer (); + auto const& takerAmount = taker.original_offer(); - assert (!isXRP (takerAmount.in) && !isXRP (takerAmount.out)); + assert(!isXRP(takerAmount.in) && !isXRP(takerAmount.out)); - if (isXRP (takerAmount.in) || isXRP (takerAmount.out)) - Throw ("Bridging with XRP and an endpoint."); + if (isXRP(takerAmount.in) || isXRP(takerAmount.out)) + Throw("Bridging with XRP and an endpoint."); - OfferStream offers_direct (view, view_cancel, - Book (taker.issue_in (), taker.issue_out ()), - when, stepCounter_, j_); + OfferStream offers_direct( + view, + view_cancel, + Book(taker.issue_in(), taker.issue_out()), + when, + stepCounter_, + j_); - OfferStream offers_leg1 (view, view_cancel, - Book (taker.issue_in (), xrpIssue ()), - when, stepCounter_, j_); + OfferStream offers_leg1( + view, + view_cancel, + Book(taker.issue_in(), xrpIssue()), + when, + stepCounter_, + j_); - OfferStream offers_leg2 (view, view_cancel, - Book (xrpIssue (), taker.issue_out ()), - when, stepCounter_, j_); + OfferStream offers_leg2( + view, + view_cancel, + Book(xrpIssue(), taker.issue_out()), + when, + stepCounter_, + j_); TER cross_result = tesSUCCESS; // Note the subtle distinction here: self-offers encountered in the // bridge are taken, but self-offers encountered in the direct book // are not. - bool have_bridge = offers_leg1.step () && offers_leg2.step (); - bool have_direct = step_account (offers_direct, taker); + bool have_bridge = offers_leg1.step() && offers_leg2.step(); + bool have_direct = step_account(offers_direct, taker); int count = 0; - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); // Modifying the order or logic of the operations in the loop will cause // a protocol breaking change. @@ -377,10 +382,8 @@ CreateOffer::bridged_cross ( bool leg2_consumed = false; bool direct_consumed = false; - auto const [use_direct, quality] = select_path ( - have_direct, offers_direct, - have_bridge, offers_leg1, offers_leg2); - + auto const [use_direct, quality] = select_path( + have_direct, offers_direct, have_bridge, offers_leg1, offers_leg2); // We are always looking at the best quality; we are done with // crossing as soon as we cross the quality boundary. @@ -394,66 +397,73 @@ CreateOffer::bridged_cross ( if (auto stream = j_.debug()) { stream << count << " Direct:"; - stream << " offer: " << offers_direct.tip (); - stream << " in: " << offers_direct.tip ().amount().in; - stream << " out: " << offers_direct.tip ().amount ().out; - stream << " owner: " << offers_direct.tip ().owner (); - stream << " funds: " << accountFunds(view, - offers_direct.tip ().owner (), - offers_direct.tip ().amount ().out, - fhIGNORE_FREEZE, viewJ); + stream << " offer: " << offers_direct.tip(); + stream << " in: " << offers_direct.tip().amount().in; + stream << " out: " << offers_direct.tip().amount().out; + stream << " owner: " << offers_direct.tip().owner(); + stream << " funds: " + << accountFunds( + view, + offers_direct.tip().owner(), + offers_direct.tip().amount().out, + fhIGNORE_FREEZE, + viewJ); } - cross_result = taker.cross(offers_direct.tip ()); + cross_result = taker.cross(offers_direct.tip()); - JLOG (j_.debug()) << "Direct Result: " << transToken (cross_result); + JLOG(j_.debug()) << "Direct Result: " << transToken(cross_result); - if (dry_offer (view, offers_direct.tip ())) + if (dry_offer(view, offers_direct.tip())) { direct_consumed = true; - have_direct = step_account (offers_direct, taker); + have_direct = step_account(offers_direct, taker); } } else { if (auto stream = j_.debug()) { - auto const owner1_funds_before = accountFunds(view, - offers_leg1.tip ().owner (), - offers_leg1.tip ().amount ().out, - fhIGNORE_FREEZE, viewJ); + auto const owner1_funds_before = accountFunds( + view, + offers_leg1.tip().owner(), + offers_leg1.tip().amount().out, + fhIGNORE_FREEZE, + viewJ); - auto const owner2_funds_before = accountFunds(view, - offers_leg2.tip ().owner (), - offers_leg2.tip ().amount ().out, - fhIGNORE_FREEZE, viewJ); + auto const owner2_funds_before = accountFunds( + view, + offers_leg2.tip().owner(), + offers_leg2.tip().amount().out, + fhIGNORE_FREEZE, + viewJ); stream << count << " Bridge:"; - stream << " offer1: " << offers_leg1.tip (); - stream << " in: " << offers_leg1.tip ().amount().in; - stream << " out: " << offers_leg1.tip ().amount ().out; - stream << " owner: " << offers_leg1.tip ().owner (); + stream << " offer1: " << offers_leg1.tip(); + stream << " in: " << offers_leg1.tip().amount().in; + stream << " out: " << offers_leg1.tip().amount().out; + stream << " owner: " << offers_leg1.tip().owner(); stream << " funds: " << owner1_funds_before; - stream << " offer2: " << offers_leg2.tip (); - stream << " in: " << offers_leg2.tip ().amount ().in; - stream << " out: " << offers_leg2.tip ().amount ().out; - stream << " owner: " << offers_leg2.tip ().owner (); + stream << " offer2: " << offers_leg2.tip(); + stream << " in: " << offers_leg2.tip().amount().in; + stream << " out: " << offers_leg2.tip().amount().out; + stream << " owner: " << offers_leg2.tip().owner(); stream << " funds: " << owner2_funds_before; } - cross_result = taker.cross(offers_leg1.tip (), offers_leg2.tip ()); + cross_result = taker.cross(offers_leg1.tip(), offers_leg2.tip()); - JLOG (j_.debug()) << "Bridge Result: " << transToken (cross_result); + JLOG(j_.debug()) << "Bridge Result: " << transToken(cross_result); - if (view.rules().enabled (fixTakerDryOfferRemoval)) + if (view.rules().enabled(fixTakerDryOfferRemoval)) { // have_bridge can be true the next time 'round only if // neither of the OfferStreams are dry. - leg1_consumed = dry_offer (view, offers_leg1.tip()); + leg1_consumed = dry_offer(view, offers_leg1.tip()); if (leg1_consumed) have_bridge &= offers_leg1.step(); - leg2_consumed = dry_offer (view, offers_leg2.tip()); + leg2_consumed = dry_offer(view, offers_leg2.tip()); if (leg2_consumed) have_bridge &= offers_leg2.step(); } @@ -461,15 +471,15 @@ CreateOffer::bridged_cross ( { // This old behavior may leave an empty offer in the book for // the second leg. - if (dry_offer (view, offers_leg1.tip ())) + if (dry_offer(view, offers_leg1.tip())) { leg1_consumed = true; - have_bridge = (have_bridge && offers_leg1.step ()); + have_bridge = (have_bridge && offers_leg1.step()); } - if (dry_offer (view, offers_leg2.tip ())) + if (dry_offer(view, offers_leg2.tip())) { leg2_consumed = true; - have_bridge = (have_bridge && offers_leg2.step ()); + have_bridge = (have_bridge && offers_leg2.step()); } } } @@ -486,7 +496,7 @@ CreateOffer::bridged_cross ( break; } - if (reachedOfferCrossingLimit (taker)) + if (reachedOfferCrossingLimit(taker)) { JLOG(j_.debug()) << "The offer crossing limit has been exceeded!"; break; @@ -494,41 +504,45 @@ CreateOffer::bridged_cross ( // Postcondition: If we aren't done, then we *must* have consumed at // least one offer fully. - assert (direct_consumed || leg1_consumed || leg2_consumed); + assert(direct_consumed || leg1_consumed || leg2_consumed); if (!direct_consumed && !leg1_consumed && !leg2_consumed) - Throw ("bridged crossing: nothing was fully consumed."); + Throw( + "bridged crossing: nothing was fully consumed."); } - return std::make_pair(cross_result, taker.remaining_offer ()); + return std::make_pair(cross_result, taker.remaining_offer()); } std::pair -CreateOffer::direct_cross ( +CreateOffer::direct_cross( Taker& taker, ApplyView& view, ApplyView& view_cancel, NetClock::time_point const when) { - OfferStream offers ( - view, view_cancel, - Book (taker.issue_in (), taker.issue_out ()), - when, stepCounter_, j_); + OfferStream offers( + view, + view_cancel, + Book(taker.issue_in(), taker.issue_out()), + when, + stepCounter_, + j_); - TER cross_result (tesSUCCESS); + TER cross_result(tesSUCCESS); int count = 0; - bool have_offer = step_account (offers, taker); + bool have_offer = step_account(offers, taker); // Modifying the order or logic of the operations in the loop will cause // a protocol breaking change. while (have_offer) { bool direct_consumed = false; - auto& offer (offers.tip()); + auto& offer(offers.tip()); // We are done with crossing as soon as we cross the quality boundary - if (taker.reject (offer.quality())) + if (taker.reject(offer.quality())) break; count++; @@ -537,23 +551,27 @@ CreateOffer::direct_cross ( { stream << count << " Direct:"; stream << " offer: " << offer; - stream << " in: " << offer.amount ().in; - stream << " out: " << offer.amount ().out; + stream << " in: " << offer.amount().in; + stream << " out: " << offer.amount().out; stream << "quality: " << offer.quality(); - stream << " owner: " << offer.owner (); - stream << " funds: " << accountFunds(view, - offer.owner (), offer.amount ().out, fhIGNORE_FREEZE, - ctx_.app.journal ("View")); + stream << " owner: " << offer.owner(); + stream << " funds: " + << accountFunds( + view, + offer.owner(), + offer.amount().out, + fhIGNORE_FREEZE, + ctx_.app.journal("View")); } - cross_result = taker.cross (offer); + cross_result = taker.cross(offer); - JLOG (j_.debug()) << "Direct Result: " << transToken (cross_result); + JLOG(j_.debug()) << "Direct Result: " << transToken(cross_result); - if (dry_offer (view, offer)) + if (dry_offer(view, offer)) { direct_consumed = true; - have_offer = step_account (offers, taker); + have_offer = step_account(offers, taker); } if (cross_result != tesSUCCESS) @@ -568,7 +586,7 @@ CreateOffer::direct_cross ( break; } - if (reachedOfferCrossingLimit (taker)) + if (reachedOfferCrossingLimit(taker)) { JLOG(j_.debug()) << "The offer crossing limit has been exceeded!"; break; @@ -576,31 +594,32 @@ CreateOffer::direct_cross ( // Postcondition: If we aren't done, then we *must* have consumed the // offer on the books fully! - assert (direct_consumed); + assert(direct_consumed); if (!direct_consumed) - Throw ("direct crossing: nothing was fully consumed."); + Throw( + "direct crossing: nothing was fully consumed."); } - return std::make_pair(cross_result, taker.remaining_offer ()); + return std::make_pair(cross_result, taker.remaining_offer()); } // Step through the stream for as long as possible, skipping any offers // that are from the taker or which cross the taker's threshold. // Return false if the is no offer in the book, true otherwise. bool -CreateOffer::step_account (OfferStream& stream, Taker const& taker) +CreateOffer::step_account(OfferStream& stream, Taker const& taker) { - while (stream.step ()) + while (stream.step()) { - auto const& offer = stream.tip (); + auto const& offer = stream.tip(); // This offer at the tip crosses the taker's threshold. We're done. - if (taker.reject (offer.quality ())) + if (taker.reject(offer.quality())) return true; // This offer at the tip is not from the taker. We're done. - if (offer.owner () != taker.account ()) + if (offer.owner() != taker.account()) return true; } @@ -612,17 +631,22 @@ CreateOffer::step_account (OfferStream& stream, Taker const& taker) // already on the books. Return the status and the amount of // the offer to left unfilled. std::pair -CreateOffer::takerCross ( +CreateOffer::takerCross( Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount) { NetClock::time_point const when{ctx_.view().parentCloseTime()}; - beast::WrappedSink takerSink (j_, "Taker "); + beast::WrappedSink takerSink(j_, "Taker "); - Taker taker (cross_type_, sb, account_, takerAmount, - ctx_.tx.getFlags(), beast::Journal (takerSink)); + Taker taker( + cross_type_, + sb, + account_, + takerAmount, + ctx_.tx.getFlags(), + beast::Journal(takerSink)); // If the taker is unfunded before we begin crossing // there's nothing to do - just return an error. @@ -631,30 +655,28 @@ CreateOffer::takerCross ( // charged fees can cause a user's available balance // to go to 0 (by causing it to dip below the reserve) // so we check this case again. - if (taker.unfunded ()) + if (taker.unfunded()) { - JLOG (j_.debug()) << - "Not crossing: taker is unfunded."; - return { tecUNFUNDED_OFFER, takerAmount }; + JLOG(j_.debug()) << "Not crossing: taker is unfunded."; + return {tecUNFUNDED_OFFER, takerAmount}; } try { if (cross_type_ == CrossType::IouToIou) - return bridged_cross (taker, sb, sbCancel, when); + return bridged_cross(taker, sb, sbCancel, when); - return direct_cross (taker, sb, sbCancel, when); + return direct_cross(taker, sb, sbCancel, when); } catch (std::exception const& e) { - JLOG (j_.error()) << - "Exception during offer crossing: " << e.what (); - return { tecINTERNAL, taker.remaining_offer () }; + JLOG(j_.error()) << "Exception during offer crossing: " << e.what(); + return {tecINTERNAL, taker.remaining_offer()}; } } std::pair -CreateOffer::flowCross ( +CreateOffer::flowCross( PaymentSandbox& psb, PaymentSandbox& psbCancel, Amounts const& takerAmount) @@ -667,34 +689,36 @@ CreateOffer::flowCross ( // We check this in preclaim, but when selling XRP charged fees can // cause a user's available balance to go to 0 (by causing it to dip // below the reserve) so we check this case again. - STAmount const inStartBalance = accountFunds ( - psb, account_, takerAmount.in, fhZERO_IF_FROZEN, j_); + STAmount const inStartBalance = + accountFunds(psb, account_, takerAmount.in, fhZERO_IF_FROZEN, j_); if (inStartBalance <= beast::zero) { // The account balance can't cover even part of the offer. - JLOG (j_.debug()) << - "Not crossing: taker is unfunded."; - return { tecUNFUNDED_OFFER, takerAmount }; + JLOG(j_.debug()) << "Not crossing: taker is unfunded."; + return {tecUNFUNDED_OFFER, takerAmount}; } // If the gateway has a transfer rate, accommodate that. The // gateway takes its cut without any special consent from the // offer taker. Set sendMax to allow for the gateway's cut. - Rate gatewayXferRate {QUALITY_ONE}; + Rate gatewayXferRate{QUALITY_ONE}; STAmount sendMax = takerAmount.in; - if (! sendMax.native() && (account_ != sendMax.getIssuer())) + if (!sendMax.native() && (account_ != sendMax.getIssuer())) { - gatewayXferRate = transferRate (psb, sendMax.getIssuer()); + gatewayXferRate = transferRate(psb, sendMax.getIssuer()); if (gatewayXferRate.value != QUALITY_ONE) { - sendMax = multiplyRound (takerAmount.in, - gatewayXferRate, takerAmount.in.issue(), true); + sendMax = multiplyRound( + takerAmount.in, + gatewayXferRate, + takerAmount.in.issue(), + true); } } // Payment flow code compares quality after the transfer rate is // included. Since transfer rate is incorporated compute threshold. - Quality threshold { takerAmount.out, sendMax }; + Quality threshold{takerAmount.out, sendMax}; // If we're creating a passive offer adjust the threshold so we only // cross offers that have a better quality than this one. @@ -714,8 +738,8 @@ CreateOffer::flowCross ( if (!takerAmount.in.native() & !takerAmount.out.native()) { STPath path; - path.emplace_back (boost::none, xrpCurrency(), boost::none); - paths.emplace_back (std::move(path)); + path.emplace_back(boost::none, xrpCurrency(), boost::none); + paths.emplace_back(std::move(path)); } // Special handling for the tfSell flag. STAmount deliver = takerAmount.out; @@ -725,40 +749,47 @@ CreateOffer::flowCross ( // specified. Since we don't know how much they might offer, // we allow delivery of the largest possible amount. if (deliver.native()) - deliver = STAmount { STAmount::cMaxNative }; + deliver = STAmount{STAmount::cMaxNative}; else // We can't use the maximum possible currency here because // there might be a gateway transfer rate to account for. // Since the transfer rate cannot exceed 200%, we use 1/2 // maxValue for our limit. - deliver = STAmount { takerAmount.out.issue(), - STAmount::cMaxValue / 2, STAmount::cMaxOffset }; + deliver = STAmount{ + takerAmount.out.issue(), + STAmount::cMaxValue / 2, + STAmount::cMaxOffset}; } // Call the payment engine's flow() to do the actual work. - auto const result = flow (psb, deliver, account_, account_, + auto const result = flow( + psb, + deliver, + account_, + account_, paths, true, // default path - ! (txFlags & tfFillOrKill), // partial payment + !(txFlags & tfFillOrKill), // partial payment true, // owner pays transfer fee true, // offer crossing threshold, - sendMax, j_); + sendMax, + j_); // If stale offers were found remove them. for (auto const& toRemove : result.removableOffers) { - if (auto otr = psb.peek (keylet::offer (toRemove))) - offerDelete (psb, otr, j_); - if (auto otr = psbCancel.peek (keylet::offer (toRemove))) - offerDelete (psbCancel, otr, j_); + if (auto otr = psb.peek(keylet::offer(toRemove))) + offerDelete(psb, otr, j_); + if (auto otr = psbCancel.peek(keylet::offer(toRemove))) + offerDelete(psbCancel, otr, j_); } // Determine the size of the final offer after crossing. - auto afterCross = takerAmount; // If !tesSUCCESS offer unchanged - if (isTesSuccess (result.result())) + auto afterCross = takerAmount; // If !tesSUCCESS offer unchanged + if (isTesSuccess(result.result())) { - STAmount const takerInBalance = accountFunds ( + STAmount const takerInBalance = accountFunds( psb, account_, takerAmount.in, fhZERO_IF_FROZEN, j_); if (takerInBalance <= beast::zero) @@ -770,8 +801,8 @@ CreateOffer::flowCross ( } else { - STAmount const rate { - Quality{takerAmount.out, takerAmount.in}.rate() }; + STAmount const rate{ + Quality{takerAmount.out, takerAmount.in}.rate()}; if (txFlags & tfSell) { @@ -785,8 +816,11 @@ CreateOffer::flowCross ( // gateway's transfer rate. STAmount nonGatewayAmountIn = result.actualAmountIn; if (gatewayXferRate.value != QUALITY_ONE) - nonGatewayAmountIn = divideRound (result.actualAmountIn, - gatewayXferRate, takerAmount.in.issue(), true); + nonGatewayAmountIn = divideRound( + result.actualAmountIn, + gatewayXferRate, + takerAmount.in.issue(), + true); afterCross.in -= nonGatewayAmountIn; @@ -797,8 +831,8 @@ CreateOffer::flowCross ( // what is a good threshold to check? afterCross.in.clear(); - afterCross.out = divRound (afterCross.in, - rate, takerAmount.out.issue(), true); + afterCross.out = divRound( + afterCross.in, rate, takerAmount.out.issue(), true); } else { @@ -806,61 +840,60 @@ CreateOffer::flowCross ( // remaining output. This too preserves the offer // Quality. afterCross.out -= result.actualAmountOut; - assert (afterCross.out >= beast::zero); + assert(afterCross.out >= beast::zero); if (afterCross.out < beast::zero) afterCross.out.clear(); - afterCross.in = mulRound (afterCross.out, - rate, takerAmount.in.issue(), true); + afterCross.in = mulRound( + afterCross.out, rate, takerAmount.in.issue(), true); } } } // Return how much of the offer is left. - return { tesSUCCESS, afterCross }; + return {tesSUCCESS, afterCross}; } catch (std::exception const& e) { - JLOG (j_.error()) << - "Exception during offer crossing: " << e.what (); + JLOG(j_.error()) << "Exception during offer crossing: " << e.what(); } - return { tecINTERNAL, takerAmount }; + return {tecINTERNAL, takerAmount}; } -enum class SBoxCmp -{ - same, - dustDiff, - offerDelDiff, - xrpRound, - diff -}; +enum class SBoxCmp { same, dustDiff, offerDelDiff, xrpRound, diff }; -static std::string to_string (SBoxCmp c) +static std::string +to_string(SBoxCmp c) { switch (c) { - case SBoxCmp::same: - return "same"; - case SBoxCmp::dustDiff: - return "dust diffs"; - case SBoxCmp::offerDelDiff: - return "offer del diffs"; - case SBoxCmp::xrpRound: - return "XRP round to zero"; - case SBoxCmp::diff: - return "different"; + case SBoxCmp::same: + return "same"; + case SBoxCmp::dustDiff: + return "dust diffs"; + case SBoxCmp::offerDelDiff: + return "offer del diffs"; + case SBoxCmp::xrpRound: + return "XRP round to zero"; + case SBoxCmp::diff: + return "different"; } return {}; } -static SBoxCmp compareSandboxes (char const* name, ApplyContext const& ctx, - detail::ApplyViewBase const& viewTaker, detail::ApplyViewBase const& viewFlow, +static SBoxCmp +compareSandboxes( + char const* name, + ApplyContext const& ctx, + detail::ApplyViewBase const& viewTaker, + detail::ApplyViewBase const& viewFlow, beast::Journal j) { SBoxCmp c = SBoxCmp::same; - CashDiff diff = cashFlowDiff ( - CashFilter::treatZeroOfferAsDeletion, viewTaker, - CashFilter::none, viewFlow); + CashDiff diff = cashFlowDiff( + CashFilter::treatZeroOfferAsDeletion, + viewTaker, + CashFilter::none, + viewFlow); if (diff.hasDiff()) { @@ -872,9 +905,9 @@ static SBoxCmp compareSandboxes (char const* name, ApplyContext const& ctx, if (int const side = diff.xrpRoundToZero()) { char const* const whichSide = side > 0 ? "; Flow" : "; Taker"; - j.stream (kWarning) << "FlowCross: " << name << " different" << - whichSide << " XRP rounded to zero. tx: " << - ctx.tx.getTransactionID(); + j.stream(kWarning) + << "FlowCross: " << name << " different" << whichSide + << " XRP rounded to zero. tx: " << ctx.tx.getTransactionID(); return SBoxCmp::xrpRound; } @@ -894,16 +927,23 @@ static SBoxCmp compareSandboxes (char const* name, ApplyContext const& ctx, c = SBoxCmp::offerDelDiff; s = kWarning; int sides = diff.rmLhsDeletedOffers() ? 1 : 0; - sides |= diff.rmRhsDeletedOffers() ? 2 : 0; + sides |= diff.rmRhsDeletedOffers() ? 2 : 0; if (!diff.hasDiff()) { char const* t = ""; switch (sides) { - case 1: t = "; Taker deleted more offers"; break; - case 2: t = "; Flow deleted more offers"; break; - case 3: t = "; Taker and Flow deleted different offers"; break; - default: break; + case 1: + t = "; Taker deleted more offers"; + break; + case 2: + t = "; Flow deleted more offers"; + break; + case 3: + t = "; Taker and Flow deleted different offers"; + break; + default: + break; } diffDesc = std::string(t) + txID; } @@ -913,40 +953,37 @@ static SBoxCmp compareSandboxes (char const* name, ApplyContext const& ctx, c = SBoxCmp::diff; std::stringstream ss; ss << "; common entries: " << diff.commonCount() - << "; Taker unique: " << diff.lhsOnlyCount() - << "; Flow unique: " << diff.rhsOnlyCount() << txID; + << "; Taker unique: " << diff.lhsOnlyCount() + << "; Flow unique: " << diff.rhsOnlyCount() << txID; diffDesc = ss.str(); } } - j.stream (s) << "FlowCross: " << name << " different" << diffDesc; + j.stream(s) << "FlowCross: " << name << " different" << diffDesc; } return c; } std::pair -CreateOffer::cross ( - Sandbox& sb, - Sandbox& sbCancel, - Amounts const& takerAmount) +CreateOffer::cross(Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount) { using beast::zero; // There are features for Flow offer crossing and for comparing results // between Taker and Flow offer crossing. Turn those into bools. - bool const useFlowCross { sb.rules().enabled (featureFlowCross) }; - bool const doCompare { sb.rules().enabled (featureCompareTakerFlowCross) }; + bool const useFlowCross{sb.rules().enabled(featureFlowCross)}; + bool const doCompare{sb.rules().enabled(featureCompareTakerFlowCross)}; - Sandbox sbTaker { &sb }; - Sandbox sbCancelTaker { &sbCancel }; + Sandbox sbTaker{&sb}; + Sandbox sbCancelTaker{&sbCancel}; auto const takerR = (!useFlowCross || doCompare) - ? takerCross (sbTaker, sbCancelTaker, takerAmount) - : std::make_pair (tecINTERNAL, takerAmount); + ? takerCross(sbTaker, sbCancelTaker, takerAmount) + : std::make_pair(tecINTERNAL, takerAmount); - PaymentSandbox psbFlow { &sb }; - PaymentSandbox psbCancelFlow { &sbCancel }; + PaymentSandbox psbFlow{&sb}; + PaymentSandbox psbCancelFlow{&sbCancel}; auto const flowR = (useFlowCross || doCompare) - ? flowCross (psbFlow, psbCancelFlow, takerAmount) - : std::make_pair (tecINTERNAL, takerAmount); + ? flowCross(psbFlow, psbCancelFlow, takerAmount) + : std::make_pair(tecINTERNAL, takerAmount); if (doCompare) { @@ -955,61 +992,70 @@ CreateOffer::cross ( { c = SBoxCmp::diff; j_.warn() << "FlowCross: Offer cross tec codes different. tx: " - << ctx_.tx.getTransactionID(); + << ctx_.tx.getTransactionID(); } - else if ((takerR.second.in == zero && flowR.second.in == zero) || - (takerR.second.out == zero && flowR.second.out == zero)) + else if ( + (takerR.second.in == zero && flowR.second.in == zero) || + (takerR.second.out == zero && flowR.second.out == zero)) { - c = compareSandboxes ("Both Taker and Flow fully crossed", - ctx_, sbTaker, psbFlow, j_); + c = compareSandboxes( + "Both Taker and Flow fully crossed", + ctx_, + sbTaker, + psbFlow, + j_); } else if (takerR.second.in == zero && takerR.second.out == zero) { - char const * crossType = "Taker fully crossed, Flow partially crossed"; + char const* crossType = + "Taker fully crossed, Flow partially crossed"; if (flowR.second.in == takerAmount.in && flowR.second.out == takerAmount.out) - crossType = "Taker fully crossed, Flow not crossed"; + crossType = "Taker fully crossed, Flow not crossed"; - c = compareSandboxes (crossType, ctx_, sbTaker, psbFlow, j_); + c = compareSandboxes(crossType, ctx_, sbTaker, psbFlow, j_); } else if (flowR.second.in == zero && flowR.second.out == zero) { - char const * crossType = + char const* crossType = "Taker partially crossed, Flow fully crossed"; if (takerR.second.in == takerAmount.in && takerR.second.out == takerAmount.out) - crossType = "Taker not crossed, Flow fully crossed"; + crossType = "Taker not crossed, Flow fully crossed"; - c = compareSandboxes (crossType, ctx_, sbTaker, psbFlow, j_); + c = compareSandboxes(crossType, ctx_, sbTaker, psbFlow, j_); } else if (ctx_.tx.getFlags() & tfFillOrKill) { - c = compareSandboxes ( + c = compareSandboxes( "FillOrKill offer", ctx_, sbCancelTaker, psbCancelFlow, j_); } - else if (takerR.second.in == takerAmount.in && - flowR.second.in == takerAmount.in && - takerR.second.out == takerAmount.out && - flowR.second.out == takerAmount.out) + else if ( + takerR.second.in == takerAmount.in && + flowR.second.in == takerAmount.in && + takerR.second.out == takerAmount.out && + flowR.second.out == takerAmount.out) { - char const * crossType = "Neither Taker nor Flow crossed"; - c = compareSandboxes (crossType, ctx_, sbTaker, psbFlow, j_); + char const* crossType = "Neither Taker nor Flow crossed"; + c = compareSandboxes(crossType, ctx_, sbTaker, psbFlow, j_); } - else if (takerR.second.in == takerAmount.in && - takerR.second.out == takerAmount.out) + else if ( + takerR.second.in == takerAmount.in && + takerR.second.out == takerAmount.out) { - char const * crossType = "Taker not crossed, Flow partially crossed"; - c = compareSandboxes (crossType, ctx_, sbTaker, psbFlow, j_); + char const* crossType = "Taker not crossed, Flow partially crossed"; + c = compareSandboxes(crossType, ctx_, sbTaker, psbFlow, j_); } - else if (flowR.second.in == takerAmount.in && - flowR.second.out == takerAmount.out) + else if ( + flowR.second.in == takerAmount.in && + flowR.second.out == takerAmount.out) { - char const * crossType = "Taker partially crossed, Flow not crossed"; - c = compareSandboxes (crossType, ctx_, sbTaker, psbFlow, j_); + char const* crossType = "Taker partially crossed, Flow not crossed"; + c = compareSandboxes(crossType, ctx_, sbTaker, psbFlow, j_); } else { - c = compareSandboxes ( + c = compareSandboxes( "Partial cross offer", ctx_, sbTaker, psbFlow, j_); // If we've gotten this far then the returned amounts matter. @@ -1019,8 +1065,8 @@ CreateOffer::cross ( using namespace beast::severities; Severity s = kInfo; std::string onlyDust = ", but only dust."; - if (! diffIsDust (takerR.second.in, flowR.second.in) || - (! diffIsDust (takerR.second.out, flowR.second.out))) + if (!diffIsDust(takerR.second.in, flowR.second.in) || + (!diffIsDust(takerR.second.out, flowR.second.out))) { char const* outSame = ""; if (takerR.second.out == flowR.second.out) @@ -1030,39 +1076,39 @@ CreateOffer::cross ( s = kWarning; std::stringstream ss; ss << outSame - << ". Taker in: " << takerR.second.in.getText() - << "; Taker out: " << takerR.second.out.getText() - << "; Flow in: " << flowR.second.in.getText() - << "; Flow out: " << flowR.second.out.getText() - << ". tx: " << ctx_.tx.getTransactionID(); + << ". Taker in: " << takerR.second.in.getText() + << "; Taker out: " << takerR.second.out.getText() + << "; Flow in: " << flowR.second.in.getText() + << "; Flow out: " << flowR.second.out.getText() + << ". tx: " << ctx_.tx.getTransactionID(); onlyDust = ss.str(); } - j_.stream (s) << "FlowCross: Partial cross amounts different" - << onlyDust; + j_.stream(s) + << "FlowCross: Partial cross amounts different" << onlyDust; } } - j_.error() << "FlowCross cmp result: " << to_string (c); + j_.error() << "FlowCross cmp result: " << to_string(c); } // Return one result or the other based on amendment. if (useFlowCross) { - psbFlow.apply (sb); - psbCancelFlow.apply (sbCancel); + psbFlow.apply(sb); + psbCancelFlow.apply(sbCancel); return flowR; } - sbTaker.apply (sb); - sbCancelTaker.apply (sbCancel); + sbTaker.apply(sb); + sbCancelTaker.apply(sbCancel); return takerR; } std::string -CreateOffer::format_amount (STAmount const& amount) +CreateOffer::format_amount(STAmount const& amount) { - std::string txt = amount.getText (); + std::string txt = amount.getText(); txt += "/"; - txt += to_string (amount.issue().currency); + txt += to_string(amount.issue().currency); return txt; } @@ -1070,10 +1116,8 @@ void CreateOffer::preCompute() { cross_type_ = CrossType::IouToIou; - bool const pays_xrp = - ctx_.tx.getFieldAmount (sfTakerPays).native (); - bool const gets_xrp = - ctx_.tx.getFieldAmount (sfTakerGets).native (); + bool const pays_xrp = ctx_.tx.getFieldAmount(sfTakerPays).native(); + bool const gets_xrp = ctx_.tx.getFieldAmount(sfTakerGets).native(); if (pays_xrp && !gets_xrp) cross_type_ = CrossType::IouToXrp; else if (gets_xrp && !pays_xrp) @@ -1083,16 +1127,16 @@ CreateOffer::preCompute() } std::pair -CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) +CreateOffer::applyGuts(Sandbox& sb, Sandbox& sbCancel) { using beast::zero; - std::uint32_t const uTxFlags = ctx_.tx.getFlags (); + std::uint32_t const uTxFlags = ctx_.tx.getFlags(); - bool const bPassive (uTxFlags & tfPassive); - bool const bImmediateOrCancel (uTxFlags & tfImmediateOrCancel); - bool const bFillOrKill (uTxFlags & tfFillOrKill); - bool const bSell (uTxFlags & tfSell); + bool const bPassive(uTxFlags & tfPassive); + bool const bImmediateOrCancel(uTxFlags & tfImmediateOrCancel); + bool const bFillOrKill(uTxFlags & tfFillOrKill); + bool const bSell(uTxFlags & tfSell); auto saTakerPays = ctx_.tx[sfTakerPays]; auto saTakerGets = ctx_.tx[sfTakerGets]; @@ -1102,12 +1146,12 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) // FIXME understand why we use SequenceNext instead of current transaction // sequence to determine the transaction. Why is the offer sequence // number insufficient? - auto const uSequence = ctx_.tx.getSequence (); + auto const uSequence = ctx_.tx.getSequence(); // This is the original rate of the offer, and is the rate at which // it will be placed, even if crossing offers change the amounts that // end up on the books. - auto uRate = getRate (saTakerGets, saTakerPays); + auto uRate = getRate(saTakerGets, saTakerPays); auto viewJ = ctx_.app.journal("View"); @@ -1116,8 +1160,8 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) // Process a cancellation request that's passed along with an offer. if (cancelSequence) { - auto const sleCancel = sb.peek( - keylet::offer(account_, *cancelSequence)); + auto const sleCancel = + sb.peek(keylet::offer(account_, *cancelSequence)); // It's not an error to not find the offer to cancel: it might have // been consumed or removed. If it is found, however, it's an error @@ -1125,7 +1169,7 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (sleCancel) { JLOG(j_.debug()) << "Create cancels order " << *cancelSequence; - result = offerDelete (sb, sleCancel, viewJ); + result = offerDelete(sb, sleCancel, viewJ); } } @@ -1136,17 +1180,18 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) // Expiration is defined in terms of the close time of the parent ledger, // because we definitively know the time that it closed but we do not // know the closing time of the ledger that is under construction. - if (expiration && - (ctx_.view().parentCloseTime() >= tp{d{*expiration}})) + if (expiration && (ctx_.view().parentCloseTime() >= tp{d{*expiration}})) { // If the offer has expired, the transaction has successfully // done nothing, so short circuit from here. // // The return code change is attached to featureDepositPreauth as a // convenience. The change is not big enough to deserve a fix code. - TER const ter {ctx_.view().rules().enabled( - featureDepositPreauth) ? TER {tecEXPIRED} : TER {tesSUCCESS}}; - return{ ter, true }; + TER const ter{ + ctx_.view().rules().enabled(featureDepositPreauth) + ? TER{tecEXPIRED} + : TER{tesSUCCESS}}; + return {ter, true}; } bool const bOpenLedger = ctx_.view().open(); @@ -1155,31 +1200,26 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (result == tesSUCCESS) { // If a tick size applies, round the offer to the tick size - auto const& uPaysIssuerID = saTakerPays.getIssuer (); - auto const& uGetsIssuerID = saTakerGets.getIssuer (); + auto const& uPaysIssuerID = saTakerPays.getIssuer(); + auto const& uGetsIssuerID = saTakerGets.getIssuer(); std::uint8_t uTickSize = Quality::maxTickSize; - if (!isXRP (uPaysIssuerID)) + if (!isXRP(uPaysIssuerID)) { - auto const sle = - sb.read(keylet::account(uPaysIssuerID)); - if (sle && sle->isFieldPresent (sfTickSize)) - uTickSize = std::min (uTickSize, - (*sle)[sfTickSize]); + auto const sle = sb.read(keylet::account(uPaysIssuerID)); + if (sle && sle->isFieldPresent(sfTickSize)) + uTickSize = std::min(uTickSize, (*sle)[sfTickSize]); } - if (!isXRP (uGetsIssuerID)) + if (!isXRP(uGetsIssuerID)) { - auto const sle = - sb.read(keylet::account(uGetsIssuerID)); - if (sle && sle->isFieldPresent (sfTickSize)) - uTickSize = std::min (uTickSize, - (*sle)[sfTickSize]); + auto const sle = sb.read(keylet::account(uGetsIssuerID)); + if (sle && sle->isFieldPresent(sfTickSize)) + uTickSize = std::min(uTickSize, (*sle)[sfTickSize]); } if (uTickSize < Quality::maxTickSize) { auto const rate = - Quality{saTakerGets, saTakerPays}.round - (uTickSize).rate(); + Quality{saTakerGets, saTakerPays}.round(uTickSize).rate(); // We round the side that's not exact, // just as if the offer happened to execute @@ -1187,47 +1227,43 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (bSell) { // this is a sell, round taker pays - saTakerPays = multiply ( - saTakerGets, rate, saTakerPays.issue()); + saTakerPays = multiply(saTakerGets, rate, saTakerPays.issue()); } else { // this is a buy, round taker gets - saTakerGets = divide ( - saTakerPays, rate, saTakerGets.issue()); + saTakerGets = divide(saTakerPays, rate, saTakerGets.issue()); } - if (! saTakerGets || ! saTakerPays) + if (!saTakerGets || !saTakerPays) { - JLOG (j_.debug()) << - "Offer rounded to zero"; - return { result, true }; + JLOG(j_.debug()) << "Offer rounded to zero"; + return {result, true}; } - uRate = getRate (saTakerGets, saTakerPays); + uRate = getRate(saTakerGets, saTakerPays); } // We reverse pays and gets because during crossing we are taking. - Amounts const takerAmount (saTakerGets, saTakerPays); + Amounts const takerAmount(saTakerGets, saTakerPays); // The amount of the offer that is unfilled after crossing has been // performed. It may be equal to the original amount (didn't cross), // empty (fully crossed), or something in-between. Amounts place_offer; - JLOG(j_.debug()) << "Attempting cross: " << - to_string (takerAmount.in.issue ()) << " -> " << - to_string (takerAmount.out.issue ()); + JLOG(j_.debug()) << "Attempting cross: " + << to_string(takerAmount.in.issue()) << " -> " + << to_string(takerAmount.out.issue()); if (auto stream = j_.trace()) { - stream << " mode: " << - (bPassive ? "passive " : "") << - (bSell ? "sell" : "buy"); - stream <<" in: " << format_amount (takerAmount.in); - stream << " out: " << format_amount (takerAmount.out); + stream << " mode: " << (bPassive ? "passive " : "") + << (bSell ? "sell" : "buy"); + stream << " in: " << format_amount(takerAmount.in); + stream << " out: " << format_amount(takerAmount.out); } - std::tie(result, place_offer) = cross (sb, sbCancel, takerAmount); + std::tie(result, place_offer) = cross(sb, sbCancel, takerAmount); // We expect the implementation of cross to succeed // or give a tec. @@ -1235,9 +1271,9 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (auto stream = j_.trace()) { - stream << "Cross result: " << transToken (result); - stream << " in: " << format_amount (place_offer.in); - stream << " out: " << format_amount (place_offer.out); + stream << "Cross result: " << transToken(result); + stream << " in: " << format_amount(place_offer.in); + stream << " out: " << format_amount(place_offer.out); } if (result == tecFAILED_PROCESSING && bOpenLedger) @@ -1245,12 +1281,12 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (result != tesSUCCESS) { - JLOG (j_.debug()) << "final result: " << transToken (result); - return { result, true }; + JLOG(j_.debug()) << "final result: " << transToken(result); + return {result, true}; } - assert (saTakerGets.issue () == place_offer.in.issue ()); - assert (saTakerPays.issue () == place_offer.out.issue ()); + assert(saTakerGets.issue() == place_offer.in.issue()); + assert(saTakerPays.issue() == place_offer.out.issue()); if (takerAmount != place_offer) crossed = true; @@ -1259,16 +1295,16 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) // never be negative. If it is, something went very very wrong. if (place_offer.in < zero || place_offer.out < zero) { - JLOG(j_.fatal()) << "Cross left offer negative!" << - " in: " << format_amount (place_offer.in) << - " out: " << format_amount (place_offer.out); - return { tefINTERNAL, true }; + JLOG(j_.fatal()) << "Cross left offer negative!" + << " in: " << format_amount(place_offer.in) + << " out: " << format_amount(place_offer.out); + return {tefINTERNAL, true}; } if (place_offer.in == zero || place_offer.out == zero) { JLOG(j_.debug()) << "Offer fully crossed!"; - return { result, true }; + return {result, true}; } // We now need to adjust the offer to reflect the amount left after @@ -1278,46 +1314,46 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) saTakerGets = place_offer.in; } - assert (saTakerPays > zero && saTakerGets > zero); + assert(saTakerPays > zero && saTakerGets > zero); if (result != tesSUCCESS) { - JLOG (j_.debug()) << "final result: " << transToken (result); - return { result, true }; + JLOG(j_.debug()) << "final result: " << transToken(result); + return {result, true}; } if (auto stream = j_.trace()) { stream << "Place" << (crossed ? " remaining " : " ") << "offer:"; - stream << " Pays: " << saTakerPays.getFullText (); - stream << " Gets: " << saTakerGets.getFullText (); + stream << " Pays: " << saTakerPays.getFullText(); + stream << " Gets: " << saTakerGets.getFullText(); } // For 'fill or kill' offers, failure to fully cross means that the // entire operation should be aborted, with only fees paid. if (bFillOrKill) { - JLOG (j_.trace()) << "Fill or Kill: offer killed"; - if (sb.rules().enabled (fix1578)) - return { tecKILLED, false }; - return { tesSUCCESS, false }; + JLOG(j_.trace()) << "Fill or Kill: offer killed"; + if (sb.rules().enabled(fix1578)) + return {tecKILLED, false}; + return {tesSUCCESS, false}; } // For 'immediate or cancel' offers, the amount remaining doesn't get // placed - it gets canceled and the operation succeeds. if (bImmediateOrCancel) { - JLOG (j_.trace()) << "Immediate or cancel: offer canceled"; - return { tesSUCCESS, true }; + JLOG(j_.trace()) << "Immediate or cancel: offer canceled"; + return {tesSUCCESS, true}; } - auto const sleCreator = sb.peek (keylet::account(account_)); - if (! sleCreator) - return { tefINTERNAL, false }; + auto const sleCreator = sb.peek(keylet::account(account_)); + if (!sleCreator) + return {tefINTERNAL, false}; { XRPAmount reserve = ctx_.view().fees().accountReserve( - sleCreator->getFieldU32 (sfOwnerCount) + 1); + sleCreator->getFieldU32(sfOwnerCount) + 1); if (mPriorBalance < reserve) { @@ -1329,85 +1365,89 @@ CreateOffer::applyGuts (Sandbox& sb, Sandbox& sbCancel) if (result != tesSUCCESS) { - JLOG (j_.debug()) << - "final result: " << transToken (result); + JLOG(j_.debug()) << "final result: " << transToken(result); } - return { result, true }; + return {result, true}; } } // We need to place the remainder of the offer into its order book. - auto const offer_index = getOfferIndex (account_, uSequence); + auto const offer_index = getOfferIndex(account_, uSequence); // Add offer to owner's directory. - auto const ownerNode = dirAdd(sb, keylet::ownerDir (account_), - offer_index, false, describeOwnerDir (account_), viewJ); + auto const ownerNode = dirAdd( + sb, + keylet::ownerDir(account_), + offer_index, + false, + describeOwnerDir(account_), + viewJ); if (!ownerNode) { - JLOG (j_.debug()) << - "final result: failed to add offer to owner's directory"; - return { tecDIR_FULL, true }; + JLOG(j_.debug()) + << "final result: failed to add offer to owner's directory"; + return {tecDIR_FULL, true}; } // Update owner count. adjustOwnerCount(sb, sleCreator, 1, viewJ); - JLOG (j_.trace()) << - "adding to book: " << to_string (saTakerPays.issue ()) << - " : " << to_string (saTakerGets.issue ()); + JLOG(j_.trace()) << "adding to book: " << to_string(saTakerPays.issue()) + << " : " << to_string(saTakerGets.issue()); - Book const book { saTakerPays.issue(), saTakerGets.issue() }; + Book const book{saTakerPays.issue(), saTakerGets.issue()}; // Add offer to order book, using the original rate // before any crossing occured. - auto dir = keylet::quality (keylet::book (book), uRate); - bool const bookExisted = static_cast(sb.peek (dir)); + auto dir = keylet::quality(keylet::book(book), uRate); + bool const bookExisted = static_cast(sb.peek(dir)); - auto const bookNode = dirAdd (sb, dir, offer_index, true, - [&](SLE::ref sle) - { - sle->setFieldH160 (sfTakerPaysCurrency, - saTakerPays.issue().currency); - sle->setFieldH160 (sfTakerPaysIssuer, - saTakerPays.issue().account); - sle->setFieldH160 (sfTakerGetsCurrency, - saTakerGets.issue().currency); - sle->setFieldH160 (sfTakerGetsIssuer, - saTakerGets.issue().account); - sle->setFieldU64 (sfExchangeRate, uRate); - }, viewJ); + auto const bookNode = dirAdd( + sb, + dir, + offer_index, + true, + [&](SLE::ref sle) { + sle->setFieldH160( + sfTakerPaysCurrency, saTakerPays.issue().currency); + sle->setFieldH160(sfTakerPaysIssuer, saTakerPays.issue().account); + sle->setFieldH160( + sfTakerGetsCurrency, saTakerGets.issue().currency); + sle->setFieldH160(sfTakerGetsIssuer, saTakerGets.issue().account); + sle->setFieldU64(sfExchangeRate, uRate); + }, + viewJ); if (!bookNode) { - JLOG (j_.debug()) << - "final result: failed to add offer to book"; - return { tecDIR_FULL, true }; + JLOG(j_.debug()) << "final result: failed to add offer to book"; + return {tecDIR_FULL, true}; } auto sleOffer = std::make_shared(ltOFFER, offer_index); - sleOffer->setAccountID (sfAccount, account_); - sleOffer->setFieldU32 (sfSequence, uSequence); - sleOffer->setFieldH256 (sfBookDirectory, dir.key); - sleOffer->setFieldAmount (sfTakerPays, saTakerPays); - sleOffer->setFieldAmount (sfTakerGets, saTakerGets); - sleOffer->setFieldU64 (sfOwnerNode, *ownerNode); - sleOffer->setFieldU64 (sfBookNode, *bookNode); + sleOffer->setAccountID(sfAccount, account_); + sleOffer->setFieldU32(sfSequence, uSequence); + sleOffer->setFieldH256(sfBookDirectory, dir.key); + sleOffer->setFieldAmount(sfTakerPays, saTakerPays); + sleOffer->setFieldAmount(sfTakerGets, saTakerGets); + sleOffer->setFieldU64(sfOwnerNode, *ownerNode); + sleOffer->setFieldU64(sfBookNode, *bookNode); if (expiration) - sleOffer->setFieldU32 (sfExpiration, *expiration); + sleOffer->setFieldU32(sfExpiration, *expiration); if (bPassive) - sleOffer->setFlag (lsfPassive); + sleOffer->setFlag(lsfPassive); if (bSell) - sleOffer->setFlag (lsfSell); + sleOffer->setFlag(lsfSell); sb.insert(sleOffer); if (!bookExisted) ctx_.app.getOrderBookDB().addOrderBook(book); - JLOG (j_.debug()) << "final result: success"; + JLOG(j_.debug()) << "final result: success"; - return { tesSUCCESS, true }; + return {tesSUCCESS, true}; } TER @@ -1415,12 +1455,12 @@ CreateOffer::doApply() { // This is the ledger view that we work against. Transactions are applied // as we go on processing transactions. - Sandbox sb (&ctx_.view()); + Sandbox sb(&ctx_.view()); // This is a ledger with just the fees paid and any unfunded or expired // offers we encounter removed. It's used when handling Fill-or-Kill offers, // if the order isn't going to be placed, to avoid wasting the work we did. - Sandbox sbCancel (&ctx_.view()); + Sandbox sbCancel(&ctx_.view()); auto const result = applyGuts(sb, sbCancel); if (result.second) @@ -1430,4 +1470,4 @@ CreateOffer::doApply() return result.first; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CreateOffer.h b/src/ripple/app/tx/impl/CreateOffer.h index 675badb0f0..e7838e0f1f 100644 --- a/src/ripple/app/tx/impl/CreateOffer.h +++ b/src/ripple/app/tx/impl/CreateOffer.h @@ -31,30 +31,25 @@ class PaymentSandbox; class Sandbox; /** Transactor specialized for creating offers in the ledger. */ -class CreateOffer - : public Transactor +class CreateOffer : public Transactor { public: /** Construct a Transactor subclass that creates an offer in the ledger. */ - explicit CreateOffer (ApplyContext& ctx) - : Transactor(ctx) - , stepCounter_ (1000, j_) + explicit CreateOffer(ApplyContext& ctx) + : Transactor(ctx), stepCounter_(1000, j_) { } /** Override default behavior provided by Transactor base class. */ - static - XRPAmount + static XRPAmount calculateMaxSpend(STTx const& tx); /** Enforce constraints beyond those of the Transactor base class. */ - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); /** Enforce constraints beyond those of the Transactor base class. */ - static - TER + static TER preclaim(PreclaimContext const& ctx); /** Gather information beyond what the Transactor base class gathers. */ @@ -67,33 +62,37 @@ public: private: std::pair - applyGuts (Sandbox& view, Sandbox& view_cancel); + applyGuts(Sandbox& view, Sandbox& view_cancel); // Determine if we are authorized to hold the asset we want to get. - static - TER - checkAcceptAsset(ReadView const& view, - ApplyFlags const flags, AccountID const id, - beast::Journal const j, Issue const& issue); + static TER + checkAcceptAsset( + ReadView const& view, + ApplyFlags const flags, + AccountID const id, + beast::Journal const j, + Issue const& issue); bool - dry_offer (ApplyView& view, Offer const& offer); + dry_offer(ApplyView& view, Offer const& offer); - static - std::pair - select_path ( - bool have_direct, OfferStream const& direct, - bool have_bridge, OfferStream const& leg1, OfferStream const& leg2); + static std::pair + select_path( + bool have_direct, + OfferStream const& direct, + bool have_bridge, + OfferStream const& leg1, + OfferStream const& leg2); std::pair - bridged_cross ( + bridged_cross( Taker& taker, ApplyView& view, ApplyView& view_cancel, NetClock::time_point const when); std::pair - direct_cross ( + direct_cross( Taker& taker, ApplyView& view, ApplyView& view_cancel, @@ -102,28 +101,24 @@ private: // Step through the stream for as long as possible, skipping any offers // that are from the taker or which cross the taker's threshold. // Return false if the is no offer in the book, true otherwise. - static - bool - step_account (OfferStream& stream, Taker const& taker); + static bool + step_account(OfferStream& stream, Taker const& taker); // True if the number of offers that have been crossed // exceeds the limit. bool - reachedOfferCrossingLimit (Taker const& taker) const; + reachedOfferCrossingLimit(Taker const& taker) const; // Fill offer as much as possible by consuming offers already on the books, // and adjusting account balances accordingly. // // Charges fees on top to taker. std::pair - takerCross ( - Sandbox& sb, - Sandbox& sbCancel, - Amounts const& takerAmount); + takerCross(Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount); // Use the payment flow code to perform offer crossing. std::pair - flowCross ( + flowCross( PaymentSandbox& psb, PaymentSandbox& psbCancel, Amounts const& takerAmount); @@ -133,14 +128,10 @@ private: // so the results can be compared. Eventually this layer will be // removed once flowCross is determined to be stable. std::pair - cross ( - Sandbox& sb, - Sandbox& sbCancel, - Amounts const& takerAmount); + cross(Sandbox& sb, Sandbox& sbCancel, Amounts const& takerAmount); - static - std::string - format_amount (STAmount const& amount); + static std::string + format_amount(STAmount const& amount); private: // What kind of offer we are placing @@ -150,6 +141,6 @@ private: OfferStream::StepCounter stepCounter_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/CreateTicket.cpp b/src/ripple/app/tx/impl/CreateTicket.cpp index 1897c8fe69..4d47ae59ef 100644 --- a/src/ripple/app/tx/impl/CreateTicket.cpp +++ b/src/ripple/app/tx/impl/CreateTicket.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -27,44 +27,43 @@ namespace ripple { NotTEC -CreateTicket::preflight (PreflightContext const& ctx) +CreateTicket::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled(featureTickets)) + if (!ctx.rules.enabled(featureTickets)) return temDISABLED; if (ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - if (ctx.tx.isFieldPresent (sfExpiration)) + if (ctx.tx.isFieldPresent(sfExpiration)) { - if (ctx.tx.getFieldU32 (sfExpiration) == 0) + if (ctx.tx.getFieldU32(sfExpiration) == 0) { - JLOG(ctx.j.warn()) << - "Malformed transaction: bad expiration"; + JLOG(ctx.j.warn()) << "Malformed transaction: bad expiration"; return temBAD_EXPIRATION; } } - return preflight2 (ctx); + return preflight2(ctx); } TER -CreateTicket::doApply () +CreateTicket::doApply() { auto const sle = view().peek(keylet::account(account_)); - if (! sle) + if (!sle) return tefINTERNAL; // A ticket counts against the reserve of the issuing account, but we // check the starting balance because we want to allow dipping into the // reserve to pay fees. { - auto const reserve = view().fees().accountReserve( - sle->getFieldU32(sfOwnerCount) + 1); + auto const reserve = + view().fees().accountReserve(sle->getFieldU32(sfOwnerCount) + 1); if (mPriorBalance < reserve) return tecINSUFFICIENT_RESERVE; @@ -72,27 +71,29 @@ CreateTicket::doApply () NetClock::time_point expiration{}; - if (ctx_.tx.isFieldPresent (sfExpiration)) + if (ctx_.tx.isFieldPresent(sfExpiration)) { - expiration = NetClock::time_point(NetClock::duration(ctx_.tx[sfExpiration])); + expiration = + NetClock::time_point(NetClock::duration(ctx_.tx[sfExpiration])); if (view().parentCloseTime() >= expiration) return tesSUCCESS; } - SLE::pointer sleTicket = std::make_shared(ltTICKET, - getTicketIndex (account_, ctx_.tx.getSequence ())); - sleTicket->setAccountID (sfAccount, account_); - sleTicket->setFieldU32 (sfSequence, ctx_.tx.getSequence ()); + SLE::pointer sleTicket = std::make_shared( + ltTICKET, getTicketIndex(account_, ctx_.tx.getSequence())); + sleTicket->setAccountID(sfAccount, account_); + sleTicket->setFieldU32(sfSequence, ctx_.tx.getSequence()); if (expiration != NetClock::time_point{}) - sleTicket->setFieldU32 (sfExpiration, expiration.time_since_epoch().count()); - view().insert (sleTicket); + sleTicket->setFieldU32( + sfExpiration, expiration.time_since_epoch().count()); + view().insert(sleTicket); - if (ctx_.tx.isFieldPresent (sfTarget)) + if (ctx_.tx.isFieldPresent(sfTarget)) { - AccountID const target_account (ctx_.tx.getAccountID (sfTarget)); + AccountID const target_account(ctx_.tx.getAccountID(sfTarget)); - SLE::pointer sleTarget = view().peek (keylet::account(target_account)); + SLE::pointer sleTarget = view().peek(keylet::account(target_account)); // Destination account does not exist. if (!sleTarget) @@ -101,17 +102,21 @@ CreateTicket::doApply () // The issuing account is the default account to which the ticket // applies so don't bother saving it if that's what's specified. if (target_account != account_) - sleTicket->setAccountID (sfTarget, target_account); + sleTicket->setAccountID(sfTarget, target_account); } - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); - auto const page = dirAdd(view(), keylet::ownerDir (account_), - sleTicket->key(), false, describeOwnerDir (account_), viewJ); + auto const page = dirAdd( + view(), + keylet::ownerDir(account_), + sleTicket->key(), + false, + describeOwnerDir(account_), + viewJ); - JLOG(j_.trace()) << - "Creating ticket " << to_string (sleTicket->key()) << - ": " << (page ? "success" : "failure"); + JLOG(j_.trace()) << "Creating ticket " << to_string(sleTicket->key()) + << ": " << (page ? "success" : "failure"); if (!page) return tecDIR_FULL; @@ -124,4 +129,4 @@ CreateTicket::doApply () return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/CreateTicket.h b/src/ripple/app/tx/impl/CreateTicket.h index 745963cfb6..e927cbd2f3 100644 --- a/src/ripple/app/tx/impl/CreateTicket.h +++ b/src/ripple/app/tx/impl/CreateTicket.h @@ -27,22 +27,20 @@ namespace ripple { -class CreateTicket - : public Transactor +class CreateTicket : public Transactor { public: - explicit CreateTicket (ApplyContext& ctx) - : Transactor(ctx) + explicit CreateTicket(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/DeleteAccount.cpp b/src/ripple/app/tx/impl/DeleteAccount.cpp index 01cd7d61b5..7f97871425 100644 --- a/src/ripple/app/tx/impl/DeleteAccount.cpp +++ b/src/ripple/app/tx/impl/DeleteAccount.cpp @@ -23,107 +23,125 @@ #include #include #include +#include #include #include -#include #include -#include +#include namespace ripple { NotTEC -DeleteAccount::preflight (PreflightContext const& ctx) +DeleteAccount::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled(featureDeletableAccounts)) + if (!ctx.rules.enabled(featureDeletableAccounts)) return temDISABLED; if (ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); + auto const ret = preflight1(ctx); - if (!isTesSuccess (ret)) + if (!isTesSuccess(ret)) return ret; if (ctx.tx[sfAccount] == ctx.tx[sfDestination]) // An account cannot be deleted and give itself the resulting XRP. return temDST_IS_SRC; - return preflight2 (ctx); + return preflight2(ctx); } FeeUnit64 -DeleteAccount::calculateBaseFee ( - ReadView const& view, - STTx const& tx) +DeleteAccount::calculateBaseFee(ReadView const& view, STTx const& tx) { // The fee required for AccountDelete is one owner reserve. But the // owner reserve is stored in drops. We need to convert it to fee units. - Fees const& fees {view.fees()}; - std::pair const mulDivResult { - mulDiv (fees.increment, safe_cast(fees.units), fees.base)}; + Fees const& fees{view.fees()}; + std::pair const mulDivResult{ + mulDiv(fees.increment, safe_cast(fees.units), fees.base)}; if (mulDivResult.first) return mulDivResult.second; // If mulDiv returns false then overflow happened. Punt by using the // standard calculation. - return Transactor::calculateBaseFee (view, tx); + return Transactor::calculateBaseFee(view, tx); } -namespace -{ +namespace { // Define a function pointer type that can be used to delete ledger node types. -using DeleterFuncPtr = TER(*)(Application& app, ApplyView& view, - AccountID const& account, uint256 const& delIndex, - std::shared_ptr const& sleDel, beast::Journal j); +using DeleterFuncPtr = TER (*)( + Application& app, + ApplyView& view, + AccountID const& account, + uint256 const& delIndex, + std::shared_ptr const& sleDel, + beast::Journal j); // Local function definitions that provides signature compatibility. TER -offerDelete (Application& app, ApplyView& view, - AccountID const& account, uint256 const& delIndex, - std::shared_ptr const& sleDel, beast::Journal j) +offerDelete( + Application& app, + ApplyView& view, + AccountID const& account, + uint256 const& delIndex, + std::shared_ptr const& sleDel, + beast::Journal j) { - return offerDelete (view, sleDel, j); + return offerDelete(view, sleDel, j); } TER -removeSignersFromLedger (Application& app, ApplyView& view, - AccountID const& account, uint256 const& delIndex, - std::shared_ptr const& sleDel, beast::Journal j) +removeSignersFromLedger( + Application& app, + ApplyView& view, + AccountID const& account, + uint256 const& delIndex, + std::shared_ptr const& sleDel, + beast::Journal j) { - return SetSignerList::removeFromLedger (app, view, account); + return SetSignerList::removeFromLedger(app, view, account); } TER -removeDepositPreauthFromLedger (Application& app, ApplyView& view, - AccountID const& account, uint256 const& delIndex, - std::shared_ptr const& sleDel, beast::Journal j) +removeDepositPreauthFromLedger( + Application& app, + ApplyView& view, + AccountID const& account, + uint256 const& delIndex, + std::shared_ptr const& sleDel, + beast::Journal j) { - return DepositPreauth::removeFromLedger ( - app, view, delIndex, j); + return DepositPreauth::removeFromLedger(app, view, delIndex, j); } -// Return nullptr if the LedgerEntryType represents an obligation that can't be deleted -// Otherwise return the pointer to the function that can delete the non-obligation -DeleterFuncPtr nonObligationDeleter(LedgerEntryType t) +// Return nullptr if the LedgerEntryType represents an obligation that can't be +// deleted Otherwise return the pointer to the function that can delete the +// non-obligation +DeleterFuncPtr +nonObligationDeleter(LedgerEntryType t) { - switch (t){ - case ltOFFER: return offerDelete; - case ltSIGNER_LIST: return removeSignersFromLedger; + switch (t) + { + case ltOFFER: + return offerDelete; + case ltSIGNER_LIST: + return removeSignersFromLedger; // case ltTICKET: return ???; - case ltDEPOSIT_PREAUTH: return removeDepositPreauthFromLedger; + case ltDEPOSIT_PREAUTH: + return removeDepositPreauthFromLedger; default: return nullptr; } } -} // namespace +} // namespace TER -DeleteAccount::preclaim (PreclaimContext const& ctx) +DeleteAccount::preclaim(PreclaimContext const& ctx) { - AccountID const account {ctx.tx[sfAccount]}; - AccountID const dst {ctx.tx[sfDestination]}; + AccountID const account{ctx.tx[sfAccount]}; + AccountID const dst{ctx.tx[sfDestination]}; auto sleDst = ctx.view.read(keylet::account(dst)); @@ -137,13 +155,13 @@ DeleteAccount::preclaim (PreclaimContext const& ctx) if (ctx.view.rules().enabled(featureDepositAuth) && (sleDst->getFlags() & lsfDepositAuth)) { - if (! ctx.view.exists (keylet::depositPreauth (dst, account))) + if (!ctx.view.exists(keylet::depositPreauth(dst, account))) return tecNO_PERMISSION; } - auto sleAccount = ctx.view.read (keylet::account(account)); + auto sleAccount = ctx.view.read(keylet::account(account)); assert(sleAccount); - if (! sleAccount) + if (!sleAccount) return terNO_ACCOUNT; // We don't allow an account to be deleted if its sequence number @@ -152,42 +170,48 @@ DeleteAccount::preclaim (PreclaimContext const& ctx) // // We look at the account's Sequence rather than the transaction's // Sequence in preparation for Tickets. - constexpr std::uint32_t seqDelta {255}; + constexpr std::uint32_t seqDelta{255}; if ((*sleAccount)[sfSequence] + seqDelta > ctx.view.seq()) return tecTOO_SOON; // Verify that the account does not own any objects that would prevent // the account from being deleted. - Keylet const ownerDirKeylet {keylet::ownerDir (account)}; + Keylet const ownerDirKeylet{keylet::ownerDir(account)}; if (dirIsEmpty(ctx.view, ownerDirKeylet)) return tesSUCCESS; - std::shared_ptr sleDirNode {}; - unsigned int uDirEntry {0}; - uint256 dirEntry {beast::zero}; + std::shared_ptr sleDirNode{}; + unsigned int uDirEntry{0}; + uint256 dirEntry{beast::zero}; - if (! cdirFirst ( - ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry, ctx.j)) - // Account has no directory at all. Looks good. - return tesSUCCESS; + if (!cdirFirst( + ctx.view, + ownerDirKeylet.key, + sleDirNode, + uDirEntry, + dirEntry, + ctx.j)) + // Account has no directory at all. Looks good. + return tesSUCCESS; - std::int32_t deletableDirEntryCount {0}; + std::int32_t deletableDirEntryCount{0}; do { // Make sure any directory node types that we find are the kind // we can delete. - Keylet const itemKeylet {ltCHILD, dirEntry}; - auto sleItem = ctx.view.read (itemKeylet); - if (! sleItem) + Keylet const itemKeylet{ltCHILD, dirEntry}; + auto sleItem = ctx.view.read(itemKeylet); + if (!sleItem) { // Directory node has an invalid index. Bail out. - JLOG (ctx.j.fatal()) << "DeleteAccount: directory node in ledger " - << ctx.view.seq() << " has index to object that is missing: " - << to_string (dirEntry); + JLOG(ctx.j.fatal()) + << "DeleteAccount: directory node in ledger " << ctx.view.seq() + << " has index to object that is missing: " + << to_string(dirEntry); return tefBAD_LEDGER; } - LedgerEntryType const nodeType { + LedgerEntryType const nodeType{ safe_cast((*sleItem)[sfLedgerEntryType])}; if (!nonObligationDeleter(nodeType)) @@ -198,14 +222,14 @@ DeleteAccount::preclaim (PreclaimContext const& ctx) if (++deletableDirEntryCount > maxDeletableDirEntries) return tefTOO_BIG; - } while (cdirNext ( + } while (cdirNext( ctx.view, ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry, ctx.j)); return tesSUCCESS; } TER -DeleteAccount::doApply () +DeleteAccount::doApply() { auto src = view().peek(keylet::account(account_)); assert(src); @@ -217,45 +241,46 @@ DeleteAccount::doApply () return tefBAD_LEDGER; // Delete all of the entries in the account directory. - Keylet const ownerDirKeylet {keylet::ownerDir (account_)}; - std::shared_ptr sleDirNode {}; - unsigned int uDirEntry {0}; - uint256 dirEntry {beast::zero}; + Keylet const ownerDirKeylet{keylet::ownerDir(account_)}; + std::shared_ptr sleDirNode{}; + unsigned int uDirEntry{0}; + uint256 dirEntry{beast::zero}; - if (view().exists(ownerDirKeylet) && dirFirst ( - view(), ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry, j_)) + if (view().exists(ownerDirKeylet) && + dirFirst( + view(), ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry, j_)) { do { // Choose the right way to delete each directory node. - Keylet const itemKeylet {ltCHILD, dirEntry}; - auto sleItem = view().peek (itemKeylet); - if (! sleItem) + Keylet const itemKeylet{ltCHILD, dirEntry}; + auto sleItem = view().peek(itemKeylet); + if (!sleItem) { // Directory node has an invalid index. Bail out. - JLOG (j_.fatal()) << "DeleteAccount: Directory node in ledger " + JLOG(j_.fatal()) + << "DeleteAccount: Directory node in ledger " << view().seq() << " has index to object that is missing: " - << to_string (dirEntry); + << to_string(dirEntry); return tefBAD_LEDGER; } - LedgerEntryType const nodeType { - safe_cast( - sleItem->getFieldU16(sfLedgerEntryType))}; + LedgerEntryType const nodeType{safe_cast( + sleItem->getFieldU16(sfLedgerEntryType))}; if (auto deleter = nonObligationDeleter(nodeType)) { - TER const result { + TER const result{ deleter(ctx_.app, view(), account_, dirEntry, sleItem, j_)}; - if (! isTesSuccess (result)) + if (!isTesSuccess(result)) return result; } else { - assert (! "Undeletable entry should be found in preclaim."); - JLOG (j_.error()) - << "DeleteAccount undeletable item not found in preclaim."; + assert(!"Undeletable entry should be found in preclaim."); + JLOG(j_.error()) + << "DeleteAccount undeletable item not found in preclaim."; return tecHAS_OBLIGATIONS; } @@ -275,32 +300,32 @@ DeleteAccount::doApply () // // 3. So we verify that uDirEntry is indeed 1. Then we jam it // back to zero to "un-invalidate" the iterator. - assert (uDirEntry == 1); + assert(uDirEntry == 1); if (uDirEntry != 1) { - JLOG (j_.error()) + JLOG(j_.error()) << "DeleteAccount iterator re-validation failed."; return tefBAD_LEDGER; } uDirEntry = 0; - } while (dirNext ( + } while (dirNext( view(), ownerDirKeylet.key, sleDirNode, uDirEntry, dirEntry, j_)); } // Transfer any XRP remaining after the fee is paid to the destination: (*dst)[sfBalance] = (*dst)[sfBalance] + mSourceBalance; (*src)[sfBalance] = (*src)[sfBalance] - mSourceBalance; - ctx_.deliver (mSourceBalance); + ctx_.deliver(mSourceBalance); - assert ((*src)[sfBalance] == XRPAmount(0)); + assert((*src)[sfBalance] == XRPAmount(0)); // If there's still an owner directory associated with the source account // delete it. if (view().exists(ownerDirKeylet) && !view().emptyDirDelete(ownerDirKeylet)) { JLOG(j_.error()) << "DeleteAccount cannot delete root dir node of " - << toBase58 (account_); + << toBase58(account_); return tecHAS_OBLIGATIONS; } @@ -314,4 +339,4 @@ DeleteAccount::doApply () return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/DeleteAccount.h b/src/ripple/app/tx/impl/DeleteAccount.h index 5b42d11a9f..d7c563526c 100644 --- a/src/ripple/app/tx/impl/DeleteAccount.h +++ b/src/ripple/app/tx/impl/DeleteAccount.h @@ -26,8 +26,7 @@ namespace ripple { -class DeleteAccount - : public Transactor +class DeleteAccount : public Transactor { public: // Set a reasonable upper limit on the number of deletable directory @@ -36,37 +35,31 @@ public: // // A limit is useful because if we go much past this limit the // transaction will fail anyway due to too much metadata (tecOVERSIZE). - static constexpr std::int32_t maxDeletableDirEntries {1000}; + static constexpr std::int32_t maxDeletableDirEntries{1000}; - explicit DeleteAccount (ApplyContext& ctx) - : Transactor(ctx) + explicit DeleteAccount(ApplyContext& ctx) : Transactor(ctx) { } - static - bool + static bool affectsSubsequentTransactionAuth(STTx const& tx) { return true; } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - FeeUnit64 - calculateBaseFee ( - ReadView const& view, - STTx const& tx); + static FeeUnit64 + calculateBaseFee(ReadView const& view, STTx const& tx); + + static TER + preclaim(PreclaimContext const& ctx); - static TER - preclaim (PreclaimContext const& ctx); - - TER doApply () override; + doApply() override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/DepositPreauth.cpp b/src/ripple/app/tx/impl/DepositPreauth.cpp index 10f5c0ebfe..cf85e3c598 100644 --- a/src/ripple/app/tx/impl/DepositPreauth.cpp +++ b/src/ripple/app/tx/impl/DepositPreauth.cpp @@ -19,22 +19,22 @@ #include #include +#include #include #include -#include #include -#include +#include namespace ripple { NotTEC -DepositPreauth::preflight (PreflightContext const& ctx) +DepositPreauth::preflight(PreflightContext const& ctx) { - if (! ctx.rules.enabled (featureDepositPreauth)) + if (!ctx.rules.enabled(featureDepositPreauth)) return temDISABLED; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto& tx = ctx.tx; @@ -42,8 +42,7 @@ DepositPreauth::preflight (PreflightContext const& ctx) if (tx.getFlags() & tfUniversalMask) { - JLOG(j.trace()) << - "Malformed transaction: Invalid flags set."; + JLOG(j.trace()) << "Malformed transaction: Invalid flags set."; return temINVALID_FLAG; } @@ -53,75 +52,73 @@ DepositPreauth::preflight (PreflightContext const& ctx) { // Either both fields are present or neither field is present. In // either case the transaction is malformed. - JLOG(j.trace()) << - "Malformed transaction: " - "Invalid Authorize and Unauthorize field combination."; + JLOG(j.trace()) + << "Malformed transaction: " + "Invalid Authorize and Unauthorize field combination."; return temMALFORMED; } // Make sure that the passed account is valid. - AccountID const target {optAuth ? *optAuth : *optUnauth}; + AccountID const target{optAuth ? *optAuth : *optUnauth}; if (target == beast::zero) { - JLOG(j.trace()) << - "Malformed transaction: Authorized or Unauthorized field zeroed."; + JLOG(j.trace()) << "Malformed transaction: Authorized or Unauthorized " + "field zeroed."; return temINVALID_ACCOUNT_ID; } // An account may not preauthorize itself. if (optAuth && (target == ctx.tx[sfAccount])) { - JLOG(j.trace()) << - "Malformed transaction: Attempting to DepositPreauth self."; + JLOG(j.trace()) + << "Malformed transaction: Attempting to DepositPreauth self."; return temCANNOT_PREAUTH_SELF; } - return preflight2 (ctx); + return preflight2(ctx); } TER DepositPreauth::preclaim(PreclaimContext const& ctx) { // Determine which operation we're performing: authorizing or unauthorizing. - if (ctx.tx.isFieldPresent (sfAuthorize)) + if (ctx.tx.isFieldPresent(sfAuthorize)) { // Verify that the Authorize account is present in the ledger. - AccountID const auth {ctx.tx[sfAuthorize]}; - if (! ctx.view.exists (keylet::account (auth))) + AccountID const auth{ctx.tx[sfAuthorize]}; + if (!ctx.view.exists(keylet::account(auth))) return tecNO_TARGET; // Verify that the Preauth entry they asked to add is not already // in the ledger. - if (ctx.view.exists ( - keylet::depositPreauth (ctx.tx[sfAccount], auth))) + if (ctx.view.exists(keylet::depositPreauth(ctx.tx[sfAccount], auth))) return tecDUPLICATE; } else { // Verify that the Preauth entry they asked to remove is in the ledger. - AccountID const unauth {ctx.tx[sfUnauthorize]}; - if (! ctx.view.exists ( - keylet::depositPreauth (ctx.tx[sfAccount], unauth))) + AccountID const unauth{ctx.tx[sfUnauthorize]}; + if (!ctx.view.exists(keylet::depositPreauth(ctx.tx[sfAccount], unauth))) return tecNO_ENTRY; } return tesSUCCESS; } TER -DepositPreauth::doApply () +DepositPreauth::doApply() { - if (ctx_.tx.isFieldPresent (sfAuthorize)) + if (ctx_.tx.isFieldPresent(sfAuthorize)) { - auto const sleOwner = view().peek (keylet::account (account_)); - if (! sleOwner) + auto const sleOwner = view().peek(keylet::account(account_)); + if (!sleOwner) return {tefINTERNAL}; // A preauth counts against the reserve of the issuing account, but we // check the starting balance because we want to allow dipping into the // reserve to pay fees. { - STAmount const reserve {view().fees().accountReserve ( - sleOwner->getFieldU32 (sfOwnerCount) + 1)}; + STAmount const reserve{view().fees().accountReserve( + sleOwner->getFieldU32(sfOwnerCount) + 1)}; if (mPriorBalance < reserve) return tecINSUFFICIENT_RESERVE; @@ -129,76 +126,79 @@ DepositPreauth::doApply () // Preclaim already verified that the Preauth entry does not yet exist. // Create and populate the Preauth entry. - AccountID const auth {ctx_.tx[sfAuthorize]}; + AccountID const auth{ctx_.tx[sfAuthorize]}; auto slePreauth = - std::make_shared(keylet::depositPreauth (account_, auth)); + std::make_shared(keylet::depositPreauth(account_, auth)); - slePreauth->setAccountID (sfAccount, account_); - slePreauth->setAccountID (sfAuthorize, auth); - view().insert (slePreauth); + slePreauth->setAccountID(sfAccount, account_); + slePreauth->setAccountID(sfAuthorize, auth); + view().insert(slePreauth); - auto viewJ = ctx_.app.journal ("View"); - auto const page = view().dirInsert (keylet::ownerDir (account_), - slePreauth->key(), describeOwnerDir (account_)); + auto viewJ = ctx_.app.journal("View"); + auto const page = view().dirInsert( + keylet::ownerDir(account_), + slePreauth->key(), + describeOwnerDir(account_)); - JLOG(j_.trace()) - << "Adding DepositPreauth to owner directory " - << to_string (slePreauth->key()) - << ": " << (page ? "success" : "failure"); + JLOG(j_.trace()) << "Adding DepositPreauth to owner directory " + << to_string(slePreauth->key()) << ": " + << (page ? "success" : "failure"); - if (! page) + if (!page) return tecDIR_FULL; - slePreauth->setFieldU64 (sfOwnerNode, *page); + slePreauth->setFieldU64(sfOwnerNode, *page); // If we succeeded, the new entry counts against the creator's reserve. - adjustOwnerCount (view(), sleOwner, 1, viewJ); + adjustOwnerCount(view(), sleOwner, 1, viewJ); } else { - AccountID const unauth {ctx_.tx[sfUnauthorize]}; - uint256 const preauthIndex {getDepositPreauthIndex (account_, unauth)}; + AccountID const unauth{ctx_.tx[sfUnauthorize]}; + uint256 const preauthIndex{getDepositPreauthIndex(account_, unauth)}; - return DepositPreauth::removeFromLedger ( + return DepositPreauth::removeFromLedger( ctx_.app, view(), preauthIndex, j_); } return tesSUCCESS; } TER -DepositPreauth::removeFromLedger (Application& app, - ApplyView& view, uint256 const& preauthIndex, beast::Journal j) +DepositPreauth::removeFromLedger( + Application& app, + ApplyView& view, + uint256 const& preauthIndex, + beast::Journal j) { // Verify that the Preauth entry they asked to remove is // in the ledger. - std::shared_ptr const slePreauth { - view.peek (keylet::depositPreauth (preauthIndex))}; - if (! slePreauth) + std::shared_ptr const slePreauth{ + view.peek(keylet::depositPreauth(preauthIndex))}; + if (!slePreauth) { JLOG(j.warn()) << "Selected DepositPreauth does not exist."; return tecNO_ENTRY; } - AccountID const account {(*slePreauth)[sfAccount]}; - std::uint64_t const page {(*slePreauth)[sfOwnerNode]}; - if (! view.dirRemove ( - keylet::ownerDir (account), page, preauthIndex, false)) + AccountID const account{(*slePreauth)[sfAccount]}; + std::uint64_t const page{(*slePreauth)[sfOwnerNode]}; + if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false)) { JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner."; return tefBAD_LEDGER; } // If we succeeded, update the DepositPreauth owner's reserve. - auto const sleOwner = view.peek (keylet::account (account)); - if (! sleOwner) + auto const sleOwner = view.peek(keylet::account(account)); + if (!sleOwner) return tefINTERNAL; - adjustOwnerCount (view, sleOwner, -1, app.journal ("View")); + adjustOwnerCount(view, sleOwner, -1, app.journal("View")); // Remove DepositPreauth from ledger. - view.erase (slePreauth); + view.erase(slePreauth); return tesSUCCESS; } -} // namespace ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/DepositPreauth.h b/src/ripple/app/tx/impl/DepositPreauth.h index cdb03148ba..2b73d1cceb 100644 --- a/src/ripple/app/tx/impl/DepositPreauth.h +++ b/src/ripple/app/tx/impl/DepositPreauth.h @@ -24,33 +24,31 @@ namespace ripple { -class DepositPreauth - : public Transactor +class DepositPreauth : public Transactor { public: - explicit DepositPreauth (ApplyContext& ctx) - : Transactor(ctx) + explicit DepositPreauth(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER + static TER preclaim(PreclaimContext const& ctx); - TER doApply () override; + TER + doApply() override; // Interface used by DeleteAccount - static - TER - removeFromLedger (Application& app, ApplyView& view, - uint256 const& delIndex, beast::Journal j); + static TER + removeFromLedger( + Application& app, + ApplyView& view, + uint256 const& delIndex, + beast::Journal j); }; -} // ripple +} // namespace ripple #endif - diff --git a/src/ripple/app/tx/impl/Escrow.cpp b/src/ripple/app/tx/impl/Escrow.cpp index e5eaa0097b..30a74c138c 100644 --- a/src/ripple/app/tx/impl/Escrow.cpp +++ b/src/ripple/app/tx/impl/Escrow.cpp @@ -20,25 +20,25 @@ #include #include -#include #include -#include #include +#include +#include #include #include #include #include -#include -#include #include #include #include +#include +#include // During an EscrowFinish, the transaction must specify both // a condition and a fulfillment. We track whether that // fulfillment matches and validates the condition. -#define SF_CF_INVALID SF_PRIVATE5 -#define SF_CF_VALID SF_PRIVATE6 +#define SF_CF_INVALID SF_PRIVATE5 +#define SF_CF_VALID SF_PRIVATE6 namespace ripple { @@ -84,7 +84,8 @@ namespace ripple { @param mark the cutoff point @return true if \a now refers to a time strictly after \a mark, else false. */ -static inline bool after (NetClock::time_point now, std::uint32_t mark) +static inline bool +after(NetClock::time_point now, std::uint32_t mark) { return now.time_since_epoch().count() > mark; } @@ -96,29 +97,29 @@ EscrowCreate::calculateMaxSpend(STTx const& tx) } NotTEC -EscrowCreate::preflight (PreflightContext const& ctx) +EscrowCreate::preflight(PreflightContext const& ctx) { if (ctx.rules.enabled(fix1543) && ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - if (! isXRP(ctx.tx[sfAmount])) + if (!isXRP(ctx.tx[sfAmount])) return temBAD_AMOUNT; if (ctx.tx[sfAmount] <= beast::zero) return temBAD_AMOUNT; // We must specify at least one timeout value - if (! ctx.tx[~sfCancelAfter] && ! ctx.tx[~sfFinishAfter]) - return temBAD_EXPIRATION; + if (!ctx.tx[~sfCancelAfter] && !ctx.tx[~sfFinishAfter]) + return temBAD_EXPIRATION; // If both finish and cancel times are specified then the cancel time must // be strictly after the finish time. if (ctx.tx[~sfCancelAfter] && ctx.tx[~sfFinishAfter] && - ctx.tx[sfCancelAfter] <= ctx.tx[sfFinishAfter]) + ctx.tx[sfCancelAfter] <= ctx.tx[sfFinishAfter]) return temBAD_EXPIRATION; if (ctx.rules.enabled(fix1571)) @@ -127,7 +128,7 @@ EscrowCreate::preflight (PreflightContext const& ctx) // immediately, which can be confusing. When creating an escrow, // we want to ensure that either a FinishAfter time is explicitly // specified or a completion condition is attached. - if (! ctx.tx[~sfFinishAfter] && ! ctx.tx[~sfCondition]) + if (!ctx.tx[~sfFinishAfter] && !ctx.tx[~sfCondition]) return temMALFORMED; } @@ -140,32 +141,33 @@ EscrowCreate::preflight (PreflightContext const& ctx) auto condition = Condition::deserialize(*cb, ec); if (!condition) { - JLOG(ctx.j.debug()) << - "Malformed condition during escrow creation: " << ec.message(); + JLOG(ctx.j.debug()) + << "Malformed condition during escrow creation: " + << ec.message(); return temMALFORMED; } // Conditions other than PrefixSha256 require the // "CryptoConditionsSuite" amendment: if (condition->type != Type::preimageSha256 && - !ctx.rules.enabled(featureCryptoConditionsSuite)) + !ctx.rules.enabled(featureCryptoConditionsSuite)) return temDISABLED; } - return preflight2 (ctx); + return preflight2(ctx); } TER EscrowCreate::doApply() { - auto const closeTime = ctx_.view ().info ().parentCloseTime; + auto const closeTime = ctx_.view().info().parentCloseTime; // Prior to fix1571, the cancel and finish times could be greater // than or equal to the parent ledgers' close time. // // With fix1571, we require that they both be strictly greater // than the parent ledgers' close time. - if (ctx_.view ().rules().enabled(fix1571)) + if (ctx_.view().rules().enabled(fix1571)) { if (ctx_.tx[~sfCancelAfter] && after(closeTime, ctx_.tx[sfCancelAfter])) return tecNO_PERMISSION; @@ -194,14 +196,14 @@ EscrowCreate::doApply() auto const account = ctx_.tx[sfAccount]; auto const sle = ctx_.view().peek(keylet::account(account)); - if (! sle) + if (!sle) return tefINTERNAL; // Check reserve and funds availability { auto const balance = STAmount((*sle)[sfBalance]).xrp(); - auto const reserve = ctx_.view().fees().accountReserve( - (*sle)[sfOwnerCount] + 1); + auto const reserve = + ctx_.view().fees().accountReserve((*sle)[sfOwnerCount] + 1); if (balance < reserve) return tecINSUFFICIENT_RESERVE; @@ -212,24 +214,24 @@ EscrowCreate::doApply() // Check destination account { - auto const sled = ctx_.view().read( - keylet::account(ctx_.tx[sfDestination])); - if (! sled) + auto const sled = + ctx_.view().read(keylet::account(ctx_.tx[sfDestination])); + if (!sled) return tecNO_DST; if (((*sled)[sfFlags] & lsfRequireDestTag) && - ! ctx_.tx[~sfDestinationTag]) + !ctx_.tx[~sfDestinationTag]) return tecDST_TAG_NEEDED; // Obeying the lsfDissalowXRP flag was a bug. Piggyback on // featureDepositAuth to remove the bug. - if (! ctx_.view().rules().enabled(featureDepositAuth) && - ((*sled)[sfFlags] & lsfDisallowXRP)) + if (!ctx_.view().rules().enabled(featureDepositAuth) && + ((*sled)[sfFlags] & lsfDisallowXRP)) return tecNO_TARGET; } // Create escrow in ledger - auto const slep = std::make_shared( - keylet::escrow(account, (*sle)[sfSequence] - 1)); + auto const slep = + std::make_shared(keylet::escrow(account, (*sle)[sfSequence] - 1)); (*slep)[sfAmount] = ctx_.tx[sfAmount]; (*slep)[sfAccount] = account; (*slep)[~sfCondition] = ctx_.tx[~sfCondition]; @@ -243,8 +245,13 @@ EscrowCreate::doApply() // Add escrow to sender's owner directory { - auto page = dirAdd(ctx_.view(), keylet::ownerDir(account), slep->key(), - false, describeOwnerDir(account), ctx_.app.journal ("View")); + auto page = dirAdd( + ctx_.view(), + keylet::ownerDir(account), + slep->key(), + false, + describeOwnerDir(account), + ctx_.app.journal("View")); if (!page) return tecDIR_FULL; (*slep)[sfOwnerNode] = *page; @@ -253,8 +260,13 @@ EscrowCreate::doApply() // If it's not a self-send, add escrow to recipient's owner directory. if (auto const dest = ctx_.tx[sfDestination]; dest != ctx_.tx[sfAccount]) { - auto page = dirAdd(ctx_.view(), keylet::ownerDir(dest), slep->key(), - false, describeOwnerDir(dest), ctx_.app.journal ("View")); + auto page = dirAdd( + ctx_.view(), + keylet::ownerDir(dest), + slep->key(), + false, + describeOwnerDir(dest), + ctx_.app.journal("View")); if (!page) return tecDIR_FULL; (*slep)[sfDestinationNode] = *page; @@ -270,9 +282,8 @@ EscrowCreate::doApply() //------------------------------------------------------------------------------ -static -bool -checkCondition (Slice f, Slice c) +static bool +checkCondition(Slice f, Slice c) { using namespace ripple::cryptoconditions; @@ -286,18 +297,18 @@ checkCondition (Slice f, Slice c) if (!fulfillment) return false; - return validate (*fulfillment, *condition); + return validate(*fulfillment, *condition); } NotTEC -EscrowFinish::preflight (PreflightContext const& ctx) +EscrowFinish::preflight(PreflightContext const& ctx) { if (ctx.rules.enabled(fix1543) && ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; } @@ -312,8 +323,8 @@ EscrowFinish::preflight (PreflightContext const& ctx) // Verify the transaction signature. If it doesn't work // then don't do any more work. { - auto const ret = preflight2 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight2(ctx); + if (!isTesSuccess(ret)) return ret; } @@ -322,17 +333,17 @@ EscrowFinish::preflight (PreflightContext const& ctx) auto& router = ctx.app.getHashRouter(); auto const id = ctx.tx.getTransactionID(); - auto const flags = router.getFlags (id); + auto const flags = router.getFlags(id); // If we haven't checked the condition, check it // now. Whether it passes or not isn't important // in preflight. if (!(flags & (SF_CF_INVALID | SF_CF_VALID))) { - if (checkCondition (*fb, *cb)) - router.setFlags (id, SF_CF_VALID); + if (checkCondition(*fb, *cb)) + router.setFlags(id, SF_CF_VALID); else - router.setFlags (id, SF_CF_INVALID); + router.setFlags(id, SF_CF_INVALID); } } @@ -340,39 +351,36 @@ EscrowFinish::preflight (PreflightContext const& ctx) } FeeUnit64 -EscrowFinish::calculateBaseFee ( - ReadView const& view, - STTx const& tx) +EscrowFinish::calculateBaseFee(ReadView const& view, STTx const& tx) { - FeeUnit64 extraFee{ 0 }; + FeeUnit64 extraFee{0}; if (auto const fb = tx[~sfFulfillment]) { - extraFee += safe_cast(view.fees().units) * - (32 + (fb->size() / 16)); + extraFee += + safe_cast(view.fees().units) * (32 + (fb->size() / 16)); } - return Transactor::calculateBaseFee (view, tx) + extraFee; + return Transactor::calculateBaseFee(view, tx) + extraFee; } TER EscrowFinish::doApply() { - auto const k = keylet::escrow( - ctx_.tx[sfOwner], ctx_.tx[sfOfferSequence]); + auto const k = keylet::escrow(ctx_.tx[sfOwner], ctx_.tx[sfOfferSequence]); auto const slep = ctx_.view().peek(k); - if (! slep) + if (!slep) return tecNO_TARGET; // If a cancel time is present, a finish operation should only succeed prior // to that time. fix1571 corrects a logic error in the check that would make // a finish only succeed strictly after the cancel time. - if (ctx_.view ().rules().enabled(fix1571)) + if (ctx_.view().rules().enabled(fix1571)) { auto const now = ctx_.view().info().parentCloseTime; // Too soon: can't execute before the finish time - if ((*slep)[~sfFinishAfter] && ! after(now, (*slep)[sfFinishAfter])) + if ((*slep)[~sfFinishAfter] && !after(now, (*slep)[sfFinishAfter])) return tecNO_PERMISSION; // Too late: can't execute after the cancel time @@ -384,39 +392,39 @@ EscrowFinish::doApply() // Too soon? if ((*slep)[~sfFinishAfter] && ctx_.view().info().parentCloseTime.time_since_epoch().count() <= - (*slep)[sfFinishAfter]) + (*slep)[sfFinishAfter]) return tecNO_PERMISSION; // Too late? if ((*slep)[~sfCancelAfter] && ctx_.view().info().parentCloseTime.time_since_epoch().count() <= - (*slep)[sfCancelAfter]) + (*slep)[sfCancelAfter]) return tecNO_PERMISSION; } // Check cryptocondition fulfillment { auto const id = ctx_.tx.getTransactionID(); - auto flags = ctx_.app.getHashRouter().getFlags (id); + auto flags = ctx_.app.getHashRouter().getFlags(id); auto const cb = ctx_.tx[~sfCondition]; // It's unlikely that the results of the check will // expire from the hash router, but if it happens, // simply re-run the check. - if (cb && ! (flags & (SF_CF_INVALID | SF_CF_VALID))) + if (cb && !(flags & (SF_CF_INVALID | SF_CF_VALID))) { auto const fb = ctx_.tx[~sfFulfillment]; if (!fb) return tecINTERNAL; - if (checkCondition (*fb, *cb)) + if (checkCondition(*fb, *cb)) flags = SF_CF_VALID; else flags = SF_CF_INVALID; - ctx_.app.getHashRouter().setFlags (id, flags); + ctx_.app.getHashRouter().setFlags(id, flags); } // If the check failed, then simply return an error @@ -444,7 +452,7 @@ EscrowFinish::doApply() // NOTE: Escrow payments cannot be used to fund accounts. AccountID const destID = (*slep)[sfDestination]; auto const sled = ctx_.view().peek(keylet::account(destID)); - if (! sled) + if (!sled) return tecNO_DST; if (ctx_.view().rules().enabled(featureDepositAuth)) @@ -458,7 +466,7 @@ EscrowFinish::doApply() // 2. If Account is deposit preauthorized by destination. if (account_ != destID) { - if (! view().exists (keylet::depositPreauth (destID, account_))) + if (!view().exists(keylet::depositPreauth(destID, account_))) return tecNO_PERMISSION; } } @@ -469,7 +477,7 @@ EscrowFinish::doApply() // Remove escrow from owner directory { auto const page = (*slep)[sfOwnerNode]; - if (! ctx_.view().dirRemove( + if (!ctx_.view().dirRemove( keylet::ownerDir(account), page, k.key, true)) { return tefBAD_LEDGER; @@ -479,7 +487,7 @@ EscrowFinish::doApply() // Remove escrow from recipient's owner directory, if present. if (auto const optPage = (*slep)[~sfDestinationNode]) { - if (! ctx_.view().dirRemove( + if (!ctx_.view().dirRemove( keylet::ownerDir(destID), *optPage, k.key, true)) { return tefBAD_LEDGER; @@ -491,8 +499,7 @@ EscrowFinish::doApply() ctx_.view().update(sled); // Adjust source owner count - auto const sle = ctx_.view().peek( - keylet::account(account)); + auto const sle = ctx_.view().peek(keylet::account(account)); adjustOwnerCount(ctx_.view(), sle, -1, ctx_.journal); ctx_.view().update(sle); @@ -505,16 +512,16 @@ EscrowFinish::doApply() //------------------------------------------------------------------------------ NotTEC -EscrowCancel::preflight (PreflightContext const& ctx) +EscrowCancel::preflight(PreflightContext const& ctx) { if (ctx.rules.enabled(fix1543) && ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - return preflight2 (ctx); + return preflight2(ctx); } TER @@ -522,19 +529,19 @@ EscrowCancel::doApply() { auto const k = keylet::escrow(ctx_.tx[sfOwner], ctx_.tx[sfOfferSequence]); auto const slep = ctx_.view().peek(k); - if (! slep) + if (!slep) return tecNO_TARGET; - if (ctx_.view ().rules().enabled(fix1571)) + if (ctx_.view().rules().enabled(fix1571)) { auto const now = ctx_.view().info().parentCloseTime; // No cancel time specified: can't execute at all. - if (! (*slep)[~sfCancelAfter]) + if (!(*slep)[~sfCancelAfter]) return tecNO_PERMISSION; // Too soon: can't execute before the cancel time. - if (! after(now, (*slep)[sfCancelAfter])) + if (!after(now, (*slep)[sfCancelAfter])) return tecNO_PERMISSION; } else @@ -542,7 +549,7 @@ EscrowCancel::doApply() // Too soon? if (!(*slep)[~sfCancelAfter] || ctx_.view().info().parentCloseTime.time_since_epoch().count() <= - (*slep)[sfCancelAfter]) + (*slep)[sfCancelAfter]) return tecNO_PERMISSION; } @@ -551,7 +558,7 @@ EscrowCancel::doApply() // Remove escrow from owner directory { auto const page = (*slep)[sfOwnerNode]; - if (! ctx_.view().dirRemove( + if (!ctx_.view().dirRemove( keylet::ownerDir(account), page, k.key, true)) { return tefBAD_LEDGER; @@ -561,16 +568,18 @@ EscrowCancel::doApply() // Remove escrow from recipient's owner directory, if present. if (auto const optPage = (*slep)[~sfDestinationNode]; optPage) { - if (! ctx_.view().dirRemove( - keylet::ownerDir((*slep)[sfDestination]), *optPage, k.key, true)) + if (!ctx_.view().dirRemove( + keylet::ownerDir((*slep)[sfDestination]), + *optPage, + k.key, + true)) { return tefBAD_LEDGER; } } // Transfer amount back to owner, decrement owner count - auto const sle = ctx_.view().peek( - keylet::account(account)); + auto const sle = ctx_.view().peek(keylet::account(account)); (*sle)[sfBalance] = (*sle)[sfBalance] + (*slep)[sfAmount]; adjustOwnerCount(ctx_.view(), sle, -1, ctx_.journal); ctx_.view().update(sle); @@ -581,5 +590,4 @@ EscrowCancel::doApply() return tesSUCCESS; } -} // ripple - +} // namespace ripple diff --git a/src/ripple/app/tx/impl/Escrow.h b/src/ripple/app/tx/impl/Escrow.h index 939a72749b..bc915bdcb5 100644 --- a/src/ripple/app/tx/impl/Escrow.h +++ b/src/ripple/app/tx/impl/Escrow.h @@ -24,23 +24,18 @@ namespace ripple { -class EscrowCreate - : public Transactor +class EscrowCreate : public Transactor { public: - explicit - EscrowCreate (ApplyContext& ctx) - : Transactor(ctx) + explicit EscrowCreate(ApplyContext& ctx) : Transactor(ctx) { } - static - XRPAmount + static XRPAmount calculateMaxSpend(STTx const& tx); - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); TER doApply() override; @@ -48,25 +43,18 @@ public: //------------------------------------------------------------------------------ -class EscrowFinish - : public Transactor +class EscrowFinish : public Transactor { public: - explicit - EscrowFinish (ApplyContext& ctx) - : Transactor(ctx) + explicit EscrowFinish(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - FeeUnit64 - calculateBaseFee ( - ReadView const& view, - STTx const& tx); + static FeeUnit64 + calculateBaseFee(ReadView const& view, STTx const& tx); TER doApply() override; @@ -74,24 +62,20 @@ public: //------------------------------------------------------------------------------ -class EscrowCancel - : public Transactor +class EscrowCancel : public Transactor { public: - explicit - EscrowCancel (ApplyContext& ctx) - : Transactor(ctx) + explicit EscrowCancel(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); TER doApply() override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/InvariantCheck.cpp b/src/ripple/app/tx/impl/InvariantCheck.cpp index 3301d5db9a..34e9d90ec7 100644 --- a/src/ripple/app/tx/impl/InvariantCheck.cpp +++ b/src/ripple/app/tx/impl/InvariantCheck.cpp @@ -46,7 +46,8 @@ TransactionFeeCheck::finalize( // We should never charge a negative fee if (fee.drops() < 0) { - JLOG(j.fatal()) << "Invariant failed: fee paid was negative: " << fee.drops(); + JLOG(j.fatal()) << "Invariant failed: fee paid was negative: " + << fee.drops(); return false; } @@ -54,7 +55,8 @@ TransactionFeeCheck::finalize( // entire XRP supply. if (fee >= INITIAL_XRP) { - JLOG(j.fatal()) << "Invariant failed: fee paid exceeds system limit: " << fee.drops(); + JLOG(j.fatal()) << "Invariant failed: fee paid exceeds system limit: " + << fee.drops(); return false; } @@ -62,8 +64,8 @@ TransactionFeeCheck::finalize( // authorizes. It's possible to charge less in some circumstances. if (fee > tx.getFieldAmount(sfFee).xrp()) { - JLOG(j.fatal()) << "Invariant failed: fee paid is " << fee.drops() << - " exceeds fee specified in transaction."; + JLOG(j.fatal()) << "Invariant failed: fee paid is " << fee.drops() + << " exceeds fee specified in transaction."; return false; } @@ -75,8 +77,8 @@ TransactionFeeCheck::finalize( void XRPNotCreated::visitEntry( bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { /* We go through all modified ledger entries, looking only at account roots, * escrow payments, and payment channels. We remove from the total any @@ -85,41 +87,44 @@ XRPNotCreated::visitEntry( * balance) and deletions are ignored for paychan and escrow because the * amount fields have not been adjusted for those in the case of deletion. */ - if(before) + if (before) { switch (before->getType()) { - case ltACCOUNT_ROOT: - drops_ -= (*before)[sfBalance].xrp().drops(); - break; - case ltPAYCHAN: - drops_ -= ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops(); - break; - case ltESCROW: - drops_ -= (*before)[sfAmount].xrp().drops(); - break; - default: - break; + case ltACCOUNT_ROOT: + drops_ -= (*before)[sfBalance].xrp().drops(); + break; + case ltPAYCHAN: + drops_ -= + ((*before)[sfAmount] - (*before)[sfBalance]).xrp().drops(); + break; + case ltESCROW: + drops_ -= (*before)[sfAmount].xrp().drops(); + break; + default: + break; } } - if(after) + if (after) { switch (after->getType()) { - case ltACCOUNT_ROOT: - drops_ += (*after)[sfBalance].xrp().drops(); - break; - case ltPAYCHAN: - if (! isDelete) - drops_ += ((*after)[sfAmount] - (*after)[sfBalance]).xrp().drops(); - break; - case ltESCROW: - if (! isDelete) - drops_ += (*after)[sfAmount].xrp().drops(); - break; - default: - break; + case ltACCOUNT_ROOT: + drops_ += (*after)[sfBalance].xrp().drops(); + break; + case ltPAYCHAN: + if (!isDelete) + drops_ += ((*after)[sfAmount] - (*after)[sfBalance]) + .xrp() + .drops(); + break; + case ltESCROW: + if (!isDelete) + drops_ += (*after)[sfAmount].xrp().drops(); + break; + default: + break; } } } @@ -136,17 +141,16 @@ XRPNotCreated::finalize( // transaction created XRP out of thin air. That's not possible. if (drops_ > 0) { - JLOG(j.fatal()) << - "Invariant failed: XRP net change was positive: " << drops_; + JLOG(j.fatal()) << "Invariant failed: XRP net change was positive: " + << drops_; return false; } // The negative of the net change should be equal to actual fee charged. if (-drops_ != fee.drops()) { - JLOG(j.fatal()) << - "Invariant failed: XRP net change of " << drops_ << - " doesn't match fee " << fee.drops(); + JLOG(j.fatal()) << "Invariant failed: XRP net change of " << drops_ + << " doesn't match fee " << fee.drops(); return false; } @@ -158,11 +162,10 @@ XRPNotCreated::finalize( void XRPBalanceChecks::visitEntry( bool, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { - auto isBad = [](STAmount const& balance) - { + auto isBad = [](STAmount const& balance) { if (!balance.native()) return true; @@ -180,11 +183,11 @@ XRPBalanceChecks::visitEntry( return false; }; - if(before && before->getType() == ltACCOUNT_ROOT) - bad_ |= isBad ((*before)[sfBalance]); + if (before && before->getType() == ltACCOUNT_ROOT) + bad_ |= isBad((*before)[sfBalance]); - if(after && after->getType() == ltACCOUNT_ROOT) - bad_ |= isBad ((*after)[sfBalance]); + if (after && after->getType() == ltACCOUNT_ROOT) + bad_ |= isBad((*after)[sfBalance]); } bool @@ -209,11 +212,10 @@ XRPBalanceChecks::finalize( void NoBadOffers::visitEntry( bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { - auto isBad = [](STAmount const& pays, STAmount const& gets) - { + auto isBad = [](STAmount const& pays, STAmount const& gets) { // An offer should never be negative if (pays < beast::zero) return true; @@ -225,10 +227,10 @@ NoBadOffers::visitEntry( return pays.native() && gets.native(); }; - if(before && before->getType() == ltOFFER) - bad_ |= isBad ((*before)[sfTakerPays], (*before)[sfTakerGets]); + if (before && before->getType() == ltOFFER) + bad_ |= isBad((*before)[sfTakerPays], (*before)[sfTakerGets]); - if(after && after->getType() == ltOFFER) + if (after && after->getType() == ltOFFER) bad_ |= isBad((*after)[sfTakerPays], (*after)[sfTakerGets]); } @@ -254,11 +256,10 @@ NoBadOffers::finalize( void NoZeroEscrow::visitEntry( bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { - auto isBad = [](STAmount const& amount) - { + auto isBad = [](STAmount const& amount) { if (!amount.native()) return true; @@ -271,10 +272,10 @@ NoZeroEscrow::visitEntry( return false; }; - if(before && before->getType() == ltESCROW) + if (before && before->getType() == ltESCROW) bad_ |= isBad((*before)[sfAmount]); - if(after && after->getType() == ltESCROW) + if (after && after->getType() == ltESCROW) bad_ |= isBad((*after)[sfAmount]); } @@ -300,8 +301,8 @@ NoZeroEscrow::finalize( void AccountRootsNotDeleted::visitEntry( bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const&) + std::shared_ptr const& before, + std::shared_ptr const&) { if (isDelete && before && before->getType() == ltACCOUNT_ROOT) accountsDeleted_++; @@ -341,8 +342,8 @@ AccountRootsNotDeleted::finalize( void LedgerEntryTypesMatch::visitEntry( bool, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { if (before && after && before->getType() != after->getType()) typeMismatch_ = true; @@ -351,23 +352,23 @@ LedgerEntryTypesMatch::visitEntry( { switch (after->getType()) { - case ltACCOUNT_ROOT: - case ltDIR_NODE: - case ltRIPPLE_STATE: - case ltTICKET: - case ltSIGNER_LIST: - case ltOFFER: - case ltLEDGER_HASHES: - case ltAMENDMENTS: - case ltFEE_SETTINGS: - case ltESCROW: - case ltPAYCHAN: - case ltCHECK: - case ltDEPOSIT_PREAUTH: - break; - default: - invalidTypeAdded_ = true; - break; + case ltACCOUNT_ROOT: + case ltDIR_NODE: + case ltRIPPLE_STATE: + case ltTICKET: + case ltSIGNER_LIST: + case ltOFFER: + case ltLEDGER_HASHES: + case ltAMENDMENTS: + case ltFEE_SETTINGS: + case ltESCROW: + case ltPAYCHAN: + case ltCHECK: + case ltDEPOSIT_PREAUTH: + break; + default: + invalidTypeAdded_ = true; + break; } } } @@ -380,7 +381,7 @@ LedgerEntryTypesMatch::finalize( ReadView const&, beast::Journal const& j) { - if ((! typeMismatch_) && (! invalidTypeAdded_)) + if ((!typeMismatch_) && (!invalidTypeAdded_)) return true; if (typeMismatch_) @@ -401,8 +402,8 @@ LedgerEntryTypesMatch::finalize( void NoXRPTrustLines::visitEntry( bool, - std::shared_ptr const&, - std::shared_ptr const& after) + std::shared_ptr const&, + std::shared_ptr const& after) { if (after && after->getType() == ltRIPPLE_STATE) { @@ -410,8 +411,8 @@ NoXRPTrustLines::visitEntry( // relying on .native() just in case native somehow // were systematically incorrect xrpTrustLine_ = - after->getFieldAmount (sfLowLimit).issue() == xrpIssue() || - after->getFieldAmount (sfHighLimit).issue() == xrpIssue(); + after->getFieldAmount(sfLowLimit).issue() == xrpIssue() || + after->getFieldAmount(sfHighLimit).issue() == xrpIssue(); } } @@ -423,7 +424,7 @@ NoXRPTrustLines::finalize( ReadView const&, beast::Journal const& j) { - if (! xrpTrustLine_) + if (!xrpTrustLine_) return true; JLOG(j.fatal()) << "Invariant failed: an XRP trust line was created"; @@ -435,8 +436,8 @@ NoXRPTrustLines::finalize( void ValidNewAccountRoot::visitEntry( bool, - std::shared_ptr const& before, - std::shared_ptr const& after) + std::shared_ptr const& before, + std::shared_ptr const& after) { if (!before && after->getType() == ltACCOUNT_ROOT) { @@ -466,7 +467,7 @@ ValidNewAccountRoot::finalize( // From this point on we know exactly one account was created. if (tx.getTxnType() == ttPAYMENT && result == tesSUCCESS) { - std::uint32_t const startingSeq { + std::uint32_t const startingSeq{ view.rules().enabled(featureDeletableAccounts) ? view.seq() : 1}; if (accountSeq_ != startingSeq) @@ -483,5 +484,4 @@ ValidNewAccountRoot::finalize( return false; } -} // ripple - +} // namespace ripple diff --git a/src/ripple/app/tx/impl/InvariantCheck.h b/src/ripple/app/tx/impl/InvariantCheck.h index f90dd20c57..4398a31d1b 100644 --- a/src/ripple/app/tx/impl/InvariantCheck.h +++ b/src/ripple/app/tx/impl/InvariantCheck.h @@ -21,14 +21,14 @@ #define RIPPLE_APP_TX_INVARIANTCHECK_H_INCLUDED #include +#include #include #include #include -#include +#include #include #include #include -#include namespace ripple { @@ -329,8 +329,7 @@ using InvariantChecks = std::tuple< NoXRPTrustLines, NoBadOffers, NoZeroEscrow, - ValidNewAccountRoot ->; + ValidNewAccountRoot>; /** * @brief get a tuple of all invariant checks @@ -340,13 +339,12 @@ using InvariantChecks = std::tuple< * * @see ripple::InvariantChecker_PROTOTYPE */ -inline -InvariantChecks +inline InvariantChecks getInvariantChecks() { return InvariantChecks{}; } -} //ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/Offer.h b/src/ripple/app/tx/impl/Offer.h index f61c06e47f..a4e8cb0394 100644 --- a/src/ripple/app/tx/impl/Offer.h +++ b/src/ripple/app/tx/impl/Offer.h @@ -23,8 +23,8 @@ #include #include #include -#include #include +#include #include #include @@ -38,17 +38,15 @@ protected: Issue issOut_; }; -template<> +template <> class TOfferBase { public: explicit TOfferBase() = default; }; - -template -class TOffer - : private TOfferBase +template +class TOffer : private TOfferBase { private: SLE::pointer m_entry; @@ -56,11 +54,13 @@ private: AccountID m_account; TAmounts m_amounts; - void setFieldAmounts (); + void + setFieldAmounts(); + public: TOffer() = default; - TOffer (SLE::pointer const& entry, Quality quality); + TOffer(SLE::pointer const& entry, Quality quality); /** Returns the quality of the offer. Conceptually, the quality is the ratio of output to input currency. @@ -72,14 +72,14 @@ public: original quality. */ Quality const - quality () const noexcept + quality() const noexcept { return m_quality; } /** Returns the account id of the offer's owner. */ AccountID const& - owner () const + owner() const { return m_account; } @@ -88,14 +88,14 @@ public: Some or all of the out amount may be unfunded. */ TAmounts const& - amount () const + amount() const { return m_amounts; } /** Returns `true` if no more funds can flow through this offer. */ bool - fully_consumed () const + fully_consumed() const { if (m_amounts.in <= beast::zero) return true; @@ -106,139 +106,144 @@ public: /** Adjusts the offer to indicate that we consumed some (or all) of it. */ void - consume (ApplyView& view, - TAmounts const& consumed) + consume(ApplyView& view, TAmounts const& consumed) { if (consumed.in > m_amounts.in) - Throw ("can't consume more than is available."); + Throw("can't consume more than is available."); if (consumed.out > m_amounts.out) - Throw ("can't produce more than is available."); + Throw("can't produce more than is available."); m_amounts -= consumed; - setFieldAmounts (); - view.update (m_entry); + setFieldAmounts(); + view.update(m_entry); } - std::string id () const + std::string + id() const { - return to_string (m_entry->key()); + return to_string(m_entry->key()); } - uint256 key () const + uint256 + key() const { return m_entry->key(); } - Issue issueIn () const; - Issue issueOut () const; + Issue + issueIn() const; + Issue + issueOut() const; }; -using Offer = TOffer <>; +using Offer = TOffer<>; -template -TOffer::TOffer (SLE::pointer const& entry, Quality quality) - : m_entry (entry) - , m_quality (quality) - , m_account (m_entry->getAccountID (sfAccount)) +template +TOffer::TOffer(SLE::pointer const& entry, Quality quality) + : m_entry(entry) + , m_quality(quality) + , m_account(m_entry->getAccountID(sfAccount)) { - auto const tp = m_entry->getFieldAmount (sfTakerPays); - auto const tg = m_entry->getFieldAmount (sfTakerGets); - m_amounts.in = toAmount (tp); - m_amounts.out = toAmount (tg); - this->issIn_ = tp.issue (); - this->issOut_ = tg.issue (); + auto const tp = m_entry->getFieldAmount(sfTakerPays); + auto const tg = m_entry->getFieldAmount(sfTakerGets); + m_amounts.in = toAmount(tp); + m_amounts.out = toAmount(tg); + this->issIn_ = tp.issue(); + this->issOut_ = tg.issue(); } -template<> -inline -TOffer::TOffer (SLE::pointer const& entry, Quality quality) - : m_entry (entry) - , m_quality (quality) - , m_account (m_entry->getAccountID (sfAccount)) - , m_amounts ( - m_entry->getFieldAmount (sfTakerPays), - m_entry->getFieldAmount (sfTakerGets)) +template <> +inline TOffer::TOffer( + SLE::pointer const& entry, + Quality quality) + : m_entry(entry) + , m_quality(quality) + , m_account(m_entry->getAccountID(sfAccount)) + , m_amounts( + m_entry->getFieldAmount(sfTakerPays), + m_entry->getFieldAmount(sfTakerGets)) { } - -template -void TOffer::setFieldAmounts () +template +void +TOffer::setFieldAmounts() { #ifdef _MSC_VER - assert(0); + assert(0); #else static_assert(sizeof(TOut) == -1, "Must be specialized"); #endif } -template<> -inline -void TOffer::setFieldAmounts () +template <> +inline void +TOffer::setFieldAmounts() { - m_entry->setFieldAmount (sfTakerPays, m_amounts.in); - m_entry->setFieldAmount (sfTakerGets, m_amounts.out); + m_entry->setFieldAmount(sfTakerPays, m_amounts.in); + m_entry->setFieldAmount(sfTakerGets, m_amounts.out); } -template<> -inline -void TOffer::setFieldAmounts () +template <> +inline void +TOffer::setFieldAmounts() { - m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in, issIn_)); - m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out, issOut_)); + m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_)); + m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_)); } -template<> -inline -void TOffer::setFieldAmounts () +template <> +inline void +TOffer::setFieldAmounts() { - m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in, issIn_)); - m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out)); + m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in, issIn_)); + m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out)); } -template<> -inline -void TOffer::setFieldAmounts () +template <> +inline void +TOffer::setFieldAmounts() { - m_entry->setFieldAmount (sfTakerPays, toSTAmount(m_amounts.in)); - m_entry->setFieldAmount (sfTakerGets, toSTAmount(m_amounts.out, issOut_)); + m_entry->setFieldAmount(sfTakerPays, toSTAmount(m_amounts.in)); + m_entry->setFieldAmount(sfTakerGets, toSTAmount(m_amounts.out, issOut_)); } -template -Issue TOffer::issueIn () const +template +Issue +TOffer::issueIn() const { return this->issIn_; } -template<> -inline -Issue TOffer::issueIn () const +template <> +inline Issue +TOffer::issueIn() const { - return m_amounts.in.issue (); + return m_amounts.in.issue(); } -template -Issue TOffer::issueOut () const +template +Issue +TOffer::issueOut() const { return this->issOut_; } -template<> -inline -Issue TOffer::issueOut () const +template <> +inline Issue +TOffer::issueOut() const { - return m_amounts.out.issue (); + return m_amounts.out.issue(); } -template -inline -std::ostream& -operator<< (std::ostream& os, TOffer const& offer) +template +inline std::ostream& +operator<<(std::ostream& os, TOffer const& offer) { - return os << offer.id (); + return os << offer.id(); } -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/OfferStream.cpp b/src/ripple/app/tx/impl/OfferStream.cpp index 7c2f83c239..9b2f3beed2 100644 --- a/src/ripple/app/tx/impl/OfferStream.cpp +++ b/src/ripple/app/tx/impl/OfferStream.cpp @@ -55,57 +55,56 @@ TOfferStreamBase::TOfferStreamBase( // Handle the case where a directory item with no corresponding ledger entry // is found. This shouldn't happen but if it does we clean it up. -template +template void -TOfferStreamBase::erase (ApplyView& view) +TOfferStreamBase::erase(ApplyView& view) { // NIKB NOTE This should be using ApplyView::dirRemove, which would // correctly remove the directory if its the last entry. // Unfortunately this is a protocol breaking change. - auto p = view.peek (keylet::page(tip_.dir())); + auto p = view.peek(keylet::page(tip_.dir())); if (p == nullptr) { - JLOG(j_.error()) << - "Missing directory " << tip_.dir() << - " for offer " << tip_.index(); + JLOG(j_.error()) << "Missing directory " << tip_.dir() << " for offer " + << tip_.index(); return; } - auto v (p->getFieldV256 (sfIndexes)); - auto it (std::find (v.begin(), v.end(), tip_.index())); + auto v(p->getFieldV256(sfIndexes)); + auto it(std::find(v.begin(), v.end(), tip_.index())); if (it == v.end()) { - JLOG(j_.error()) << - "Missing offer " << tip_.index() << - " for directory " << tip_.dir(); + JLOG(j_.error()) << "Missing offer " << tip_.index() + << " for directory " << tip_.dir(); return; } - v.erase (it); - p->setFieldV256 (sfIndexes, v); - view.update (p); + v.erase(it); + p->setFieldV256(sfIndexes, v); + view.update(p); - JLOG(j_.trace()) << - "Missing offer " << tip_.index() << - " removed from directory " << tip_.dir(); + JLOG(j_.trace()) << "Missing offer " << tip_.index() + << " removed from directory " << tip_.dir(); } -static -STAmount accountFundsHelper (ReadView const& view, +static STAmount +accountFundsHelper( + ReadView const& view, AccountID const& id, STAmount const& saDefault, Issue const&, FreezeHandling freezeHandling, beast::Journal j) { - return accountFunds (view, id, saDefault, freezeHandling, j); + return accountFunds(view, id, saDefault, freezeHandling, j); } -static -IOUAmount accountFundsHelper (ReadView const& view, +static IOUAmount +accountFundsHelper( + ReadView const& view, AccountID const& id, IOUAmount const& amtDefault, Issue const& issue, @@ -116,25 +115,26 @@ IOUAmount accountFundsHelper (ReadView const& view, // self funded return amtDefault; - return toAmount ( - accountHolds (view, id, issue.currency, issue.account, freezeHandling, j)); + return toAmount(accountHolds( + view, id, issue.currency, issue.account, freezeHandling, j)); } -static -XRPAmount accountFundsHelper (ReadView const& view, +static XRPAmount +accountFundsHelper( + ReadView const& view, AccountID const& id, XRPAmount const& amtDefault, Issue const& issue, FreezeHandling freezeHandling, beast::Journal j) { - return toAmount ( - accountHolds (view, id, issue.currency, issue.account, freezeHandling, j)); + return toAmount(accountHolds( + view, id, issue.currency, issue.account, freezeHandling, j)); } -template +template bool -TOfferStreamBase::step () +TOfferStreamBase::step() { // Modifying the order or logic of these // operations causes a protocol breaking change. @@ -142,57 +142,60 @@ TOfferStreamBase::step () if (!validBook_) return false; - for(;;) + for (;;) { ownerFunds_ = boost::none; // BookTip::step deletes the current offer from the view before // advancing to the next (unless the ledger entry is missing). - if (! tip_.step(j_)) + if (!tip_.step(j_)) return false; std::shared_ptr entry = tip_.entry(); // If we exceed the maximum number of allowed steps, we're done. - if (!counter_.step ()) + if (!counter_.step()) return false; // Remove if missing - if (! entry) + if (!entry) { - erase (view_); - erase (cancelView_); + erase(view_); + erase(cancelView_); continue; } // Remove if expired using d = NetClock::duration; using tp = NetClock::time_point; - if (entry->isFieldPresent (sfExpiration) && + if (entry->isFieldPresent(sfExpiration) && tp{d{(*entry)[sfExpiration]}} <= expire_) { - JLOG(j_.trace()) << - "Removing expired offer " << entry->key(); - permRmOffer (entry->key()); + JLOG(j_.trace()) << "Removing expired offer " << entry->key(); + permRmOffer(entry->key()); continue; } - offer_ = TOffer (entry, tip_.quality()); + offer_ = TOffer(entry, tip_.quality()); - auto const amount (offer_.amount()); + auto const amount(offer_.amount()); // Remove if either amount is zero if (amount.empty()) { - JLOG(j_.warn()) << - "Removing bad offer " << entry->key(); - permRmOffer (entry->key()); + JLOG(j_.warn()) << "Removing bad offer " << entry->key(); + permRmOffer(entry->key()); offer_ = TOffer{}; continue; } // Calculate owner funds - ownerFunds_ = accountFundsHelper (view_, offer_.owner (), amount.out, - offer_.issueOut (), fhZERO_IF_FROZEN, j_); + ownerFunds_ = accountFundsHelper( + view_, + offer_.owner(), + amount.out, + offer_.issueOut(), + fhZERO_IF_FROZEN, + j_); // Check for unfunded offer if (*ownerFunds_ <= beast::zero) @@ -200,20 +203,23 @@ TOfferStreamBase::step () // If the owner's balance in the pristine view is the same, // we haven't modified the balance and therefore the // offer is "found unfunded" versus "became unfunded" - auto const original_funds = - accountFundsHelper (cancelView_, offer_.owner (), amount.out, - offer_.issueOut (), fhZERO_IF_FROZEN, j_); + auto const original_funds = accountFundsHelper( + cancelView_, + offer_.owner(), + amount.out, + offer_.issueOut(), + fhZERO_IF_FROZEN, + j_); if (original_funds == *ownerFunds_) { - permRmOffer (entry->key()); - JLOG(j_.trace()) << - "Removing unfunded offer " << entry->key(); + permRmOffer(entry->key()); + JLOG(j_.trace()) << "Removing unfunded offer " << entry->key(); } else { - JLOG(j_.trace()) << - "Removing became unfunded offer " << entry->key(); + JLOG(j_.trace()) + << "Removing became unfunded offer " << entry->key(); } offer_ = TOffer{}; continue; @@ -226,16 +232,16 @@ TOfferStreamBase::step () } void -OfferStream::permRmOffer (uint256 const& offerIndex) +OfferStream::permRmOffer(uint256 const& offerIndex) { - offerDelete (cancelView_, - cancelView_.peek(keylet::offer(offerIndex)), j_); + offerDelete(cancelView_, cancelView_.peek(keylet::offer(offerIndex)), j_); } -template -void FlowOfferStream::permRmOffer (uint256 const& offerIndex) +template +void +FlowOfferStream::permRmOffer(uint256 const& offerIndex) { - permToRemove_.insert (offerIndex); + permToRemove_.insert(offerIndex); } template class FlowOfferStream; @@ -247,4 +253,4 @@ template class TOfferStreamBase; template class TOfferStreamBase; template class TOfferStreamBase; template class TOfferStreamBase; -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/OfferStream.h b/src/ripple/app/tx/impl/OfferStream.h index 31a17cedde..d8c739b36b 100644 --- a/src/ripple/app/tx/impl/OfferStream.h +++ b/src/ripple/app/tx/impl/OfferStream.h @@ -22,17 +22,17 @@ #include #include -#include #include +#include +#include #include #include -#include #include namespace ripple { -template +template class TOfferStreamBase { public: @@ -44,15 +44,13 @@ public: beast::Journal j_; public: - StepCounter (std::uint32_t limit, beast::Journal j) - : limit_ (limit) - , count_ (0) - , j_ (j) + StepCounter(std::uint32_t limit, beast::Journal j) + : limit_(limit), count_(0), j_(j) { } bool - step () + step() { if (count_ >= limit_) { @@ -62,7 +60,8 @@ public: count_++; return true; } - std::uint32_t count() const + std::uint32_t + count() const { return count_; } @@ -81,16 +80,19 @@ protected: StepCounter& counter_; void - erase (ApplyView& view); + erase(ApplyView& view); - virtual - void - permRmOffer (uint256 const& offerIndex) = 0; + virtual void + permRmOffer(uint256 const& offerIndex) = 0; public: - TOfferStreamBase (ApplyView& view, ApplyView& cancelView, - Book const& book, NetClock::time_point when, - StepCounter& counter, beast::Journal journal); + TOfferStreamBase( + ApplyView& view, + ApplyView& cancelView, + Book const& book, + NetClock::time_point when, + StepCounter& counter, + beast::Journal journal); virtual ~TOfferStreamBase() = default; @@ -99,7 +101,7 @@ public: Only valid if step() returned `true`. */ TOffer& - tip () const + tip() const { return const_cast(this)->offer_; } @@ -112,9 +114,10 @@ public: @return `true` if there is a valid offer. */ bool - step (); + step(); - TOut ownerFunds () const + TOut + ownerFunds() const { return *ownerFunds_; } @@ -141,7 +144,8 @@ class OfferStream : public TOfferStreamBase { protected: void - permRmOffer (uint256 const& offerIndex) override; + permRmOffer(uint256 const& offerIndex) override; + public: using TOfferStreamBase::TOfferStreamBase; }; @@ -177,14 +181,14 @@ public: // unintuitive. See the discussion in the comments for // BookOfferCrossingStep::limitSelfCrossQuality(). void - permRmOffer (uint256 const& offerIndex) override; + permRmOffer(uint256 const& offerIndex) override; - boost::container::flat_set const& permToRemove () const + boost::container::flat_set const& + permToRemove() const { return permToRemove_; } }; -} +} // namespace ripple #endif - diff --git a/src/ripple/app/tx/impl/PayChan.cpp b/src/ripple/app/tx/impl/PayChan.cpp index d64a7bb30f..a8bc20cb5c 100644 --- a/src/ripple/app/tx/impl/PayChan.cpp +++ b/src/ripple/app/tx/impl/PayChan.cpp @@ -18,18 +18,18 @@ //============================================================================== #include -#include #include #include +#include #include #include -#include #include #include #include #include -#include #include +#include +#include namespace ripple { @@ -97,33 +97,23 @@ namespace ripple { Channel The 256-bit ID of the channel. Balance (optional) - The total amount of XRP delivered after this claim is processed (optional, not - needed if just closing). - Amount (optional) - The amount of XRP the signature is for (not needed if equal to Balance or just - closing the line). - Signature (optional) - Authorization for the balance above, signed by the owner (optional, - not needed if closing or owner is performing the transaction). The - signature if for the following message: CLM\0 followed by the - 256-bit channel ID, and a 64-bit integer drops. - PublicKey (optional) - The public key that made the signature (optional, required if a signature - is present) - Flags - tfClose - Request that the channel be closed - tfRenew - Request that the channel's expiration be reset. Only the owner - may renew a channel. + The total amount of XRP delivered after this claim is processed + (optional, not needed if just closing). Amount (optional) The amount of XRP + the signature is for (not needed if equal to Balance or just closing the + line). Signature (optional) Authorization for the balance above, signed by + the owner (optional, not needed if closing or owner is performing the + transaction). The signature if for the following message: CLM\0 followed by + the 256-bit channel ID, and a 64-bit integer drops. PublicKey (optional) The + public key that made the signature (optional, required if a signature is + present) Flags tfClose Request that the channel be closed tfRenew Request + that the channel's expiration be reset. Only the owner may renew a channel. */ //------------------------------------------------------------------------------ -static -TER -closeChannel ( +static TER +closeChannel( std::shared_ptr const& slep, ApplyView& view, uint256 const& key, @@ -133,9 +123,10 @@ closeChannel ( // Remove PayChan from owner directory { auto const page = (*slep)[sfOwnerNode]; - if (! view.dirRemove(keylet::ownerDir(src), page, key, true)) + if (!view.dirRemove(keylet::ownerDir(src), page, key, true)) { - JLOG (j.fatal()) << "Could not remove paychan from src owner directory"; + JLOG(j.fatal()) + << "Could not remove paychan from src owner directory"; return tefBAD_LEDGER; } } @@ -147,40 +138,41 @@ closeChannel ( auto const dst = (*slep)[sfDestination]; if (!view.dirRemove(keylet::ownerDir(dst), *page, key, true)) { - JLOG (j.fatal()) << "Could not remove paychan from dst owner directory"; + JLOG(j.fatal()) + << "Could not remove paychan from dst owner directory"; return tefBAD_LEDGER; } } // Transfer amount back to owner, decrement owner count - auto const sle = view.peek (keylet::account (src)); - if (! sle) + auto const sle = view.peek(keylet::account(src)); + if (!sle) return tefINTERNAL; - assert ((*slep)[sfAmount] >= (*slep)[sfBalance]); + assert((*slep)[sfAmount] >= (*slep)[sfBalance]); (*sle)[sfBalance] = (*sle)[sfBalance] + (*slep)[sfAmount] - (*slep)[sfBalance]; adjustOwnerCount(view, sle, -1, j); - view.update (sle); + view.update(sle); // Remove PayChan from ledger - view.erase (slep); + view.erase(slep); return tesSUCCESS; } //------------------------------------------------------------------------------ NotTEC -PayChanCreate::preflight (PreflightContext const& ctx) +PayChanCreate::preflight(PreflightContext const& ctx) { if (ctx.rules.enabled(fix1543) && ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - if (!isXRP (ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::zero)) + if (!isXRP(ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::zero)) return temBAD_AMOUNT; if (ctx.tx[sfAccount] == ctx.tx[sfDestination]) @@ -189,22 +181,22 @@ PayChanCreate::preflight (PreflightContext const& ctx) if (!publicKeyType(ctx.tx[sfPublicKey])) return temMALFORMED; - return preflight2 (ctx); + return preflight2(ctx); } TER -PayChanCreate::preclaim(PreclaimContext const &ctx) +PayChanCreate::preclaim(PreclaimContext const& ctx) { auto const account = ctx.tx[sfAccount]; - auto const sle = ctx.view.read (keylet::account (account)); - if (! sle) + auto const sle = ctx.view.read(keylet::account(account)); + if (!sle) return terNO_ACCOUNT; // Check reserve and funds availability { auto const balance = (*sle)[sfBalance]; auto const reserve = - ctx.view.fees ().accountReserve ((*sle)[sfOwnerCount] + 1); + ctx.view.fees().accountReserve((*sle)[sfOwnerCount] + 1); if (balance < reserve) return tecINSUFFICIENT_RESERVE; @@ -217,7 +209,7 @@ PayChanCreate::preclaim(PreclaimContext const &ctx) { // Check destination account - auto const sled = ctx.view.read (keylet::account (dst)); + auto const sled = ctx.view.read(keylet::account(dst)); if (!sled) return tecNO_DST; if (((*sled)[sfFlags] & lsfRequireDestTag) && @@ -238,19 +230,19 @@ TER PayChanCreate::doApply() { auto const account = ctx_.tx[sfAccount]; - auto const sle = ctx_.view ().peek (keylet::account (account)); + auto const sle = ctx_.view().peek(keylet::account(account)); if (!sle) return tefINTERNAL; auto const dst = ctx_.tx[sfDestination]; // Create PayChan in ledger - auto const slep = std::make_shared ( - keylet::payChan (account, dst, (*sle)[sfSequence] - 1)); + auto const slep = std::make_shared( + keylet::payChan(account, dst, (*sle)[sfSequence] - 1)); // Funds held in this channel (*slep)[sfAmount] = ctx_.tx[sfAmount]; // Amount channel has already paid - (*slep)[sfBalance] = ctx_.tx[sfAmount].zeroed (); + (*slep)[sfBalance] = ctx_.tx[sfAmount].zeroed(); (*slep)[sfAccount] = account; (*slep)[sfDestination] = dst; (*slep)[sfSettleDelay] = ctx_.tx[sfSettleDelay]; @@ -259,12 +251,17 @@ PayChanCreate::doApply() (*slep)[~sfSourceTag] = ctx_.tx[~sfSourceTag]; (*slep)[~sfDestinationTag] = ctx_.tx[~sfDestinationTag]; - ctx_.view ().insert (slep); + ctx_.view().insert(slep); // Add PayChan to owner directory { - auto const page = dirAdd (ctx_.view(), keylet::ownerDir(account), slep->key(), - false, describeOwnerDir (account), ctx_.app.journal ("View")); + auto const page = dirAdd( + ctx_.view(), + keylet::ownerDir(account), + slep->key(), + false, + describeOwnerDir(account), + ctx_.app.journal("View")); if (!page) return tecDIR_FULL; (*slep)[sfOwnerNode] = *page; @@ -273,8 +270,13 @@ PayChanCreate::doApply() // Add PayChan to the recipient's owner directory if (ctx_.view().rules().enabled(fixPayChanRecipientOwnerDir)) { - auto const page = dirAdd(ctx_.view(), keylet::ownerDir(dst), slep->key(), - false, describeOwnerDir(dst), ctx_.app.journal("View")); + auto const page = dirAdd( + ctx_.view(), + keylet::ownerDir(dst), + slep->key(), + false, + describeOwnerDir(dst), + ctx_.app.journal("View")); if (!page) return tecDIR_FULL; (*slep)[sfDestinationNode] = *page; @@ -283,7 +285,7 @@ PayChanCreate::doApply() // Deduct owner's balance, increment owner count (*sle)[sfBalance] = (*sle)[sfBalance] - ctx_.tx[sfAmount]; adjustOwnerCount(ctx_.view(), sle, 1, ctx_.journal); - ctx_.view ().update (sle); + ctx_.view().update(sle); return tesSUCCESS; } @@ -291,26 +293,26 @@ PayChanCreate::doApply() //------------------------------------------------------------------------------ NotTEC -PayChanFund::preflight (PreflightContext const& ctx) +PayChanFund::preflight(PreflightContext const& ctx) { if (ctx.rules.enabled(fix1543) && ctx.tx.getFlags() & tfUniversalMask) return temINVALID_FLAG; - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - if (!isXRP (ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::zero)) + if (!isXRP(ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::zero)) return temBAD_AMOUNT; - return preflight2 (ctx); + return preflight2(ctx); } TER PayChanFund::doApply() { - Keylet const k (ltPAYCHAN, ctx_.tx[sfPayChannel]); - auto const slep = ctx_.view ().peek (k); + Keylet const k(ltPAYCHAN, ctx_.tx[sfPayChannel]); + auto const slep = ctx_.view().peek(k); if (!slep) return tecNO_ENTRY; @@ -321,11 +323,11 @@ PayChanFund::doApply() { auto const cancelAfter = (*slep)[~sfCancelAfter]; auto const closeTime = - ctx_.view ().info ().parentCloseTime.time_since_epoch ().count (); + ctx_.view().info().parentCloseTime.time_since_epoch().count(); if ((cancelAfter && closeTime >= *cancelAfter) || (expiration && closeTime >= *expiration)) - return closeChannel ( - slep, ctx_.view (), k.key, ctx_.app.journal ("View")); + return closeChannel( + slep, ctx_.view(), k.key, ctx_.app.journal("View")); } if (src != txAccount) @@ -335,26 +337,26 @@ PayChanFund::doApply() if (auto extend = ctx_.tx[~sfExpiration]) { auto minExpiration = - ctx_.view ().info ().parentCloseTime.time_since_epoch ().count () + - (*slep)[sfSettleDelay]; + ctx_.view().info().parentCloseTime.time_since_epoch().count() + + (*slep)[sfSettleDelay]; if (expiration && *expiration < minExpiration) minExpiration = *expiration; if (*extend < minExpiration) return temBAD_EXPIRATION; (*slep)[~sfExpiration] = *extend; - ctx_.view ().update (slep); + ctx_.view().update(slep); } - auto const sle = ctx_.view ().peek (keylet::account (txAccount)); - if (! sle) + auto const sle = ctx_.view().peek(keylet::account(txAccount)); + if (!sle) return tefINTERNAL; { // Check reserve and funds availability auto const balance = (*sle)[sfBalance]; auto const reserve = - ctx_.view ().fees ().accountReserve ((*sle)[sfOwnerCount]); + ctx_.view().fees().accountReserve((*sle)[sfOwnerCount]); if (balance < reserve) return tecINSUFFICIENT_RESERVE; @@ -371,10 +373,10 @@ PayChanFund::doApply() } (*slep)[sfAmount] = (*slep)[sfAmount] + ctx_.tx[sfAmount]; - ctx_.view ().update (slep); + ctx_.view().update(slep); (*sle)[sfBalance] = (*sle)[sfBalance] - ctx_.tx[sfAmount]; - ctx_.view ().update (sle); + ctx_.view().update(sle); return tesSUCCESS; } @@ -382,18 +384,18 @@ PayChanFund::doApply() //------------------------------------------------------------------------------ NotTEC -PayChanClaim::preflight (PreflightContext const& ctx) +PayChanClaim::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto const bal = ctx.tx[~sfBalance]; - if (bal && (!isXRP (*bal) || *bal <= beast::zero)) + if (bal && (!isXRP(*bal) || *bal <= beast::zero)) return temBAD_AMOUNT; auto const amt = ctx.tx[~sfAmount]; - if (amt && (!isXRP (*amt) || *amt <= beast::zero)) + if (amt && (!isXRP(*amt) || *amt <= beast::zero)) return temBAD_AMOUNT; if (bal && amt && *bal > *amt) @@ -418,30 +420,30 @@ PayChanClaim::preflight (PreflightContext const& ctx) // The signature isn't needed if txAccount == src, but if it's // present, check it - auto const reqBalance = bal->xrp (); + auto const reqBalance = bal->xrp(); auto const authAmt = amt ? amt->xrp() : reqBalance; if (reqBalance > authAmt) return temBAD_AMOUNT; - Keylet const k (ltPAYCHAN, ctx.tx[sfPayChannel]); + Keylet const k(ltPAYCHAN, ctx.tx[sfPayChannel]); if (!publicKeyType(ctx.tx[sfPublicKey])) return temMALFORMED; - PublicKey const pk (ctx.tx[sfPublicKey]); + PublicKey const pk(ctx.tx[sfPublicKey]); Serializer msg; - serializePayChanAuthorization (msg, k.key, authAmt); - if (!verify (pk, msg.slice (), *sig, /*canonical*/ true)) + serializePayChanAuthorization(msg, k.key, authAmt); + if (!verify(pk, msg.slice(), *sig, /*canonical*/ true)) return temBAD_SIGNATURE; } - return preflight2 (ctx); + return preflight2(ctx); } TER PayChanClaim::doApply() { - Keylet const k (ltPAYCHAN, ctx_.tx[sfPayChannel]); - auto const slep = ctx_.view ().peek (k); + Keylet const k(ltPAYCHAN, ctx_.tx[sfPayChannel]); + auto const slep = ctx_.view().peek(k); if (!slep) return tecNO_TARGET; @@ -453,11 +455,11 @@ PayChanClaim::doApply() { auto const cancelAfter = (*slep)[~sfCancelAfter]; auto const closeTime = - ctx_.view ().info ().parentCloseTime.time_since_epoch ().count (); + ctx_.view().info().parentCloseTime.time_since_epoch().count(); if ((cancelAfter && closeTime >= *cancelAfter) || (curExpiration && closeTime >= *curExpiration)) - return closeChannel ( - slep, ctx_.view (), k.key, ctx_.app.journal ("View")); + return closeChannel( + slep, ctx_.view(), k.key, ctx_.app.journal("View")); } if (txAccount != src && txAccount != dst) @@ -465,16 +467,16 @@ PayChanClaim::doApply() if (ctx_.tx[~sfBalance]) { - auto const chanBalance = slep->getFieldAmount (sfBalance).xrp (); - auto const chanFunds = slep->getFieldAmount (sfAmount).xrp (); - auto const reqBalance = ctx_.tx[sfBalance].xrp (); + auto const chanBalance = slep->getFieldAmount(sfBalance).xrp(); + auto const chanFunds = slep->getFieldAmount(sfAmount).xrp(); + auto const reqBalance = ctx_.tx[sfBalance].xrp(); if (txAccount == dst && !ctx_.tx[~sfSignature]) return temBAD_SIGNATURE; if (ctx_.tx[~sfSignature]) { - PublicKey const pk ((*slep)[sfPublicKey]); + PublicKey const pk((*slep)[sfPublicKey]); if (ctx_.tx[sfPublicKey] != pk) return temBAD_SIGNER; } @@ -486,13 +488,13 @@ PayChanClaim::doApply() // nothing requested return tecUNFUNDED_PAYMENT; - auto const sled = ctx_.view ().peek (keylet::account (dst)); + auto const sled = ctx_.view().peek(keylet::account(dst)); if (!sled) return tecNO_DST; // Obeying the lsfDisallowXRP flag was a bug. Piggyback on // featureDepositAuth to remove the bug. - bool const depositAuth {ctx_.view().rules().enabled(featureDepositAuth)}; + bool const depositAuth{ctx_.view().rules().enabled(featureDepositAuth)}; if (!depositAuth && (txAccount == src && (sled->getFlags() & lsfDisallowXRP))) return tecNO_TARGET; @@ -506,47 +508,46 @@ PayChanClaim::doApply() // 2. If Account is deposit preauthorized by destination. if (txAccount != dst) { - if (! view().exists (keylet::depositPreauth (dst, txAccount))) + if (!view().exists(keylet::depositPreauth(dst, txAccount))) return tecNO_PERMISSION; } } (*slep)[sfBalance] = ctx_.tx[sfBalance]; XRPAmount const reqDelta = reqBalance - chanBalance; - assert (reqDelta >= beast::zero); + assert(reqDelta >= beast::zero); (*sled)[sfBalance] = (*sled)[sfBalance] + reqDelta; - ctx_.view ().update (sled); - ctx_.view ().update (slep); + ctx_.view().update(sled); + ctx_.view().update(slep); } - if (ctx_.tx.getFlags () & tfRenew) + if (ctx_.tx.getFlags() & tfRenew) { if (src != txAccount) return tecNO_PERMISSION; (*slep)[~sfExpiration] = boost::none; - ctx_.view ().update (slep); + ctx_.view().update(slep); } - if (ctx_.tx.getFlags () & tfClose) + if (ctx_.tx.getFlags() & tfClose) { // Channel will close immediately if dry or the receiver closes if (dst == txAccount || (*slep)[sfBalance] == (*slep)[sfAmount]) - return closeChannel ( - slep, ctx_.view (), k.key, ctx_.app.journal ("View")); + return closeChannel( + slep, ctx_.view(), k.key, ctx_.app.journal("View")); auto const settleExpiration = - ctx_.view ().info ().parentCloseTime.time_since_epoch ().count () + - (*slep)[sfSettleDelay]; + ctx_.view().info().parentCloseTime.time_since_epoch().count() + + (*slep)[sfSettleDelay]; if (!curExpiration || *curExpiration > settleExpiration) { (*slep)[~sfExpiration] = settleExpiration; - ctx_.view ().update (slep); + ctx_.view().update(slep); } } return tesSUCCESS; } -} // ripple - +} // namespace ripple diff --git a/src/ripple/app/tx/impl/PayChan.h b/src/ripple/app/tx/impl/PayChan.h index f223dcebf2..f82a7841f3 100644 --- a/src/ripple/app/tx/impl/PayChan.h +++ b/src/ripple/app/tx/impl/PayChan.h @@ -24,23 +24,18 @@ namespace ripple { -class PayChanCreate - : public Transactor +class PayChanCreate : public Transactor { public: - explicit - PayChanCreate (ApplyContext& ctx) - : Transactor(ctx) + explicit PayChanCreate(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER - preclaim(PreclaimContext const &ctx); + static TER + preclaim(PreclaimContext const& ctx); TER doApply() override; @@ -48,19 +43,15 @@ public: //------------------------------------------------------------------------------ -class PayChanFund - : public Transactor +class PayChanFund : public Transactor { public: - explicit - PayChanFund (ApplyContext& ctx) - : Transactor(ctx) + explicit PayChanFund(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); TER doApply() override; @@ -68,24 +59,20 @@ public: //------------------------------------------------------------------------------ -class PayChanClaim - : public Transactor +class PayChanClaim : public Transactor { public: - explicit - PayChanClaim (ApplyContext& ctx) - : Transactor(ctx) + explicit PayChanClaim(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); TER doApply() override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/Payment.cpp b/src/ripple/app/tx/impl/Payment.cpp index 2dcfbeef4a..982c846ca6 100644 --- a/src/ripple/app/tx/impl/Payment.cpp +++ b/src/ripple/app/tx/impl/Payment.cpp @@ -17,14 +17,14 @@ */ //============================================================================== -#include #include +#include #include #include #include +#include #include #include -#include namespace ripple { @@ -45,160 +45,164 @@ Payment::calculateMaxSpend(STTx const& tx) } NotTEC -Payment::preflight (PreflightContext const& ctx) +Payment::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto& tx = ctx.tx; auto& j = ctx.j; - std::uint32_t const uTxFlags = tx.getFlags (); + std::uint32_t const uTxFlags = tx.getFlags(); if (uTxFlags & tfPaymentMask) { - JLOG(j.trace()) << "Malformed transaction: " << - "Invalid flags set."; + JLOG(j.trace()) << "Malformed transaction: " + << "Invalid flags set."; return temINVALID_FLAG; } bool const partialPaymentAllowed = uTxFlags & tfPartialPayment; bool const limitQuality = uTxFlags & tfLimitQuality; bool const defaultPathsAllowed = !(uTxFlags & tfNoRippleDirect); - bool const bPaths = tx.isFieldPresent (sfPaths); - bool const bMax = tx.isFieldPresent (sfSendMax); + bool const bPaths = tx.isFieldPresent(sfPaths); + bool const bMax = tx.isFieldPresent(sfSendMax); - STAmount const saDstAmount (tx.getFieldAmount (sfAmount)); + STAmount const saDstAmount(tx.getFieldAmount(sfAmount)); STAmount maxSourceAmount; auto const account = tx.getAccountID(sfAccount); if (bMax) - maxSourceAmount = tx.getFieldAmount (sfSendMax); - else if (saDstAmount.native ()) + maxSourceAmount = tx.getFieldAmount(sfSendMax); + else if (saDstAmount.native()) maxSourceAmount = saDstAmount; else - maxSourceAmount = STAmount ( - { saDstAmount.getCurrency (), account }, - saDstAmount.mantissa(), saDstAmount.exponent (), + maxSourceAmount = STAmount( + {saDstAmount.getCurrency(), account}, + saDstAmount.mantissa(), + saDstAmount.exponent(), saDstAmount < beast::zero); - auto const& uSrcCurrency = maxSourceAmount.getCurrency (); - auto const& uDstCurrency = saDstAmount.getCurrency (); + auto const& uSrcCurrency = maxSourceAmount.getCurrency(); + auto const& uDstCurrency = saDstAmount.getCurrency(); // isZero() is XRP. FIX! - bool const bXRPDirect = uSrcCurrency.isZero () && uDstCurrency.isZero (); + bool const bXRPDirect = uSrcCurrency.isZero() && uDstCurrency.isZero(); - if (!isLegalNet (saDstAmount) || !isLegalNet (maxSourceAmount)) + if (!isLegalNet(saDstAmount) || !isLegalNet(maxSourceAmount)) return temBAD_AMOUNT; - auto const uDstAccountID = tx.getAccountID (sfDestination); + auto const uDstAccountID = tx.getAccountID(sfDestination); if (!uDstAccountID) { - JLOG(j.trace()) << "Malformed transaction: " << - "Payment destination account not specified."; + JLOG(j.trace()) << "Malformed transaction: " + << "Payment destination account not specified."; return temDST_NEEDED; } if (bMax && maxSourceAmount <= beast::zero) { - JLOG(j.trace()) << "Malformed transaction: " << - "bad max amount: " << maxSourceAmount.getFullText (); + JLOG(j.trace()) << "Malformed transaction: " + << "bad max amount: " << maxSourceAmount.getFullText(); return temBAD_AMOUNT; } if (saDstAmount <= beast::zero) { - JLOG(j.trace()) << "Malformed transaction: "<< - "bad dst amount: " << saDstAmount.getFullText (); + JLOG(j.trace()) << "Malformed transaction: " + << "bad dst amount: " << saDstAmount.getFullText(); return temBAD_AMOUNT; } if (badCurrency() == uSrcCurrency || badCurrency() == uDstCurrency) { - JLOG(j.trace()) <<"Malformed transaction: " << - "Bad currency."; + JLOG(j.trace()) << "Malformed transaction: " + << "Bad currency."; return temBAD_CURRENCY; } if (account == uDstAccountID && uSrcCurrency == uDstCurrency && !bPaths) { // You're signing yourself a payment. // If bPaths is true, you might be trying some arbitrage. - JLOG(j.trace()) << "Malformed transaction: " << - "Redundant payment from " << to_string (account) << - " to self without path for " << to_string (uDstCurrency); + JLOG(j.trace()) << "Malformed transaction: " + << "Redundant payment from " << to_string(account) + << " to self without path for " + << to_string(uDstCurrency); return temREDUNDANT; } if (bXRPDirect && bMax) { // Consistent but redundant transaction. - JLOG(j.trace()) << "Malformed transaction: " << - "SendMax specified for XRP to XRP."; + JLOG(j.trace()) << "Malformed transaction: " + << "SendMax specified for XRP to XRP."; return temBAD_SEND_XRP_MAX; } if (bXRPDirect && bPaths) { // XRP is sent without paths. - JLOG(j.trace()) << "Malformed transaction: " << - "Paths specified for XRP to XRP."; + JLOG(j.trace()) << "Malformed transaction: " + << "Paths specified for XRP to XRP."; return temBAD_SEND_XRP_PATHS; } if (bXRPDirect && partialPaymentAllowed) { // Consistent but redundant transaction. - JLOG(j.trace()) << "Malformed transaction: " << - "Partial payment specified for XRP to XRP."; + JLOG(j.trace()) << "Malformed transaction: " + << "Partial payment specified for XRP to XRP."; return temBAD_SEND_XRP_PARTIAL; } if (bXRPDirect && limitQuality) { // Consistent but redundant transaction. - JLOG(j.trace()) << "Malformed transaction: " << - "Limit quality specified for XRP to XRP."; + JLOG(j.trace()) << "Malformed transaction: " + << "Limit quality specified for XRP to XRP."; return temBAD_SEND_XRP_LIMIT; } if (bXRPDirect && !defaultPathsAllowed) { // Consistent but redundant transaction. - JLOG(j.trace()) << "Malformed transaction: " << - "No ripple direct specified for XRP to XRP."; + JLOG(j.trace()) << "Malformed transaction: " + << "No ripple direct specified for XRP to XRP."; return temBAD_SEND_XRP_NO_DIRECT; } auto const deliverMin = tx[~sfDeliverMin]; if (deliverMin) { - if (! partialPaymentAllowed) + if (!partialPaymentAllowed) { JLOG(j.trace()) << "Malformed transaction: Partial payment not " - "specified for " << jss::DeliverMin.c_str() << "."; + "specified for " + << jss::DeliverMin.c_str() << "."; return temBAD_AMOUNT; } auto const dMin = *deliverMin; if (!isLegalNet(dMin) || dMin <= beast::zero) { - JLOG(j.trace()) << "Malformed transaction: Invalid " << - jss::DeliverMin.c_str() << " amount. " << - dMin.getFullText(); + JLOG(j.trace()) + << "Malformed transaction: Invalid " << jss::DeliverMin.c_str() + << " amount. " << dMin.getFullText(); return temBAD_AMOUNT; } if (dMin.issue() != saDstAmount.issue()) { - JLOG(j.trace()) << "Malformed transaction: Dst issue differs " - "from " << jss::DeliverMin.c_str() << ". " << - dMin.getFullText(); + JLOG(j.trace()) + << "Malformed transaction: Dst issue differs " + "from " + << jss::DeliverMin.c_str() << ". " << dMin.getFullText(); return temBAD_AMOUNT; } if (dMin > saDstAmount) { - JLOG(j.trace()) << "Malformed transaction: Dst amount less than " << - jss::DeliverMin.c_str() << ". " << - dMin.getFullText(); + JLOG(j.trace()) + << "Malformed transaction: Dst amount less than " + << jss::DeliverMin.c_str() << ". " << dMin.getFullText(); return temBAD_AMOUNT; } } - return preflight2 (ctx); + return preflight2(ctx); } TER @@ -221,21 +225,19 @@ Payment::preclaim(PreclaimContext const& ctx) // Destination account does not exist. if (!saDstAmount.native()) { - JLOG(ctx.j.trace()) << - "Delay transaction: Destination account does not exist."; + JLOG(ctx.j.trace()) + << "Delay transaction: Destination account does not exist."; // Another transaction could create the account and then this // transaction would succeed. return tecNO_DST; } - else if (ctx.view.open() - && partialPaymentAllowed) + else if (ctx.view.open() && partialPaymentAllowed) { // You cannot fund an account with a partial payment. // Make retry work smaller, by rejecting this. - JLOG(ctx.j.trace()) << - "Delay transaction: Partial payment not allowed to create account."; - + JLOG(ctx.j.trace()) << "Delay transaction: Partial payment not " + "allowed to create account."; // Another transaction could create the account and then this // transaction would succeed. @@ -245,9 +247,9 @@ Payment::preclaim(PreclaimContext const& ctx) { // accountReserve is the minimum amount that an account can have. // Reserve is not scaled by load. - JLOG(ctx.j.trace()) << - "Delay transaction: Destination account does not exist. " << - "Insufficent payment to create account."; + JLOG(ctx.j.trace()) + << "Delay transaction: Destination account does not exist. " + << "Insufficent payment to create account."; // TODO: dedupe // Another transaction could create the account and then this @@ -255,7 +257,8 @@ Payment::preclaim(PreclaimContext const& ctx) return tecNO_DST_INSUF_XRP; } } - else if ((sleDst->getFlags() & lsfRequireDestTag) && + else if ( + (sleDst->getFlags() & lsfRequireDestTag) && !ctx.tx.isFieldPresent(sfDestinationTag)) { // The tag is basically account-specific information we don't @@ -263,7 +266,8 @@ Payment::preclaim(PreclaimContext const& ctx) // We didn't make this test for a newly-formed account because there's // no way for this field to be set. - JLOG(ctx.j.trace()) << "Malformed transaction: DestinationTag required."; + JLOG(ctx.j.trace()) + << "Malformed transaction: DestinationTag required."; return tecDST_TAG_NEEDED; } @@ -278,7 +282,7 @@ Payment::preclaim(PreclaimContext const& ctx) auto pathTooBig = spsPaths.size() > MaxPathSize; - if(!pathTooBig) + if (!pathTooBig) for (auto const& path : spsPaths) if (path.size() > MaxPathLength) { @@ -288,53 +292,52 @@ Payment::preclaim(PreclaimContext const& ctx) if (ctx.view.open() && pathTooBig) { - return telBAD_PATH_COUNT; // Too many paths for proposed ledger. + return telBAD_PATH_COUNT; // Too many paths for proposed ledger. } } return tesSUCCESS; } - TER -Payment::doApply () +Payment::doApply() { auto const deliverMin = ctx_.tx[~sfDeliverMin]; // Ripple if source or destination is non-native or if there are paths. - std::uint32_t const uTxFlags = ctx_.tx.getFlags (); + std::uint32_t const uTxFlags = ctx_.tx.getFlags(); bool const partialPaymentAllowed = uTxFlags & tfPartialPayment; bool const limitQuality = uTxFlags & tfLimitQuality; bool const defaultPathsAllowed = !(uTxFlags & tfNoRippleDirect); auto const paths = ctx_.tx.isFieldPresent(sfPaths); auto const sendMax = ctx_.tx[~sfSendMax]; - AccountID const uDstAccountID (ctx_.tx.getAccountID (sfDestination)); - STAmount const saDstAmount (ctx_.tx.getFieldAmount (sfAmount)); + AccountID const uDstAccountID(ctx_.tx.getAccountID(sfDestination)); + STAmount const saDstAmount(ctx_.tx.getFieldAmount(sfAmount)); STAmount maxSourceAmount; if (sendMax) maxSourceAmount = *sendMax; - else if (saDstAmount.native ()) + else if (saDstAmount.native()) maxSourceAmount = saDstAmount; else - maxSourceAmount = STAmount ( - {saDstAmount.getCurrency (), account_}, - saDstAmount.mantissa(), saDstAmount.exponent (), + maxSourceAmount = STAmount( + {saDstAmount.getCurrency(), account_}, + saDstAmount.mantissa(), + saDstAmount.exponent(), saDstAmount < beast::zero); - JLOG(j_.trace()) << - "maxSourceAmount=" << maxSourceAmount.getFullText () << - " saDstAmount=" << saDstAmount.getFullText (); + JLOG(j_.trace()) << "maxSourceAmount=" << maxSourceAmount.getFullText() + << " saDstAmount=" << saDstAmount.getFullText(); // Open a ledger for editing. auto const k = keylet::account(uDstAccountID); - SLE::pointer sleDst = view().peek (k); + SLE::pointer sleDst = view().peek(k); if (!sleDst) { - std::uint32_t const seqno { - view().rules().enabled(featureDeletableAccounts) ? - view().seq() : 1}; + std::uint32_t const seqno{ + view().rules().enabled(featureDeletableAccounts) ? view().seq() + : 1}; // Create the account. sleDst = std::make_shared(k); @@ -348,7 +351,7 @@ Payment::doApply () // Tell the engine that we are intending to change the destination // account. The source account gets always charged a fee so it's always // marked as modified. - view().update (sleDst); + view().update(sleDst); } // Determine whether the destination requires deposit authorization. @@ -357,7 +360,7 @@ Payment::doApply () bool const depositPreauth = view().rules().enabled(featureDepositPreauth); - bool const bRipple = paths || sendMax || !saDstAmount.native (); + bool const bRipple = paths || sendMax || !saDstAmount.native(); // If the destination has lsfDepositAuth set, then only direct XRP // payments (no intermediate steps) are allowed to the destination. @@ -377,14 +380,14 @@ Payment::doApply () // 2. If Account is deposit preauthorized by destination. if (uDstAccountID != account_) { - if (! view().exists ( - keylet::depositPreauth (uDstAccountID, account_))) + if (!view().exists( + keylet::depositPreauth(uDstAccountID, account_))) return tecNO_PERMISSION; } } // Copy paths into an editable class. - STPathSet spsPaths = ctx_.tx.getFieldPathSet (sfPaths); + STPathSet spsPaths = ctx_.tx.getFieldPathSet(sfPaths); path::RippleCalc::Input rcInput; rcInput.partialPaymentAllowed = partialPaymentAllowed; @@ -395,10 +398,9 @@ Payment::doApply () path::RippleCalc::Output rc; { PaymentSandbox pv(&view()); - JLOG(j_.debug()) - << "Entering RippleCalc in payment: " - << ctx_.tx.getTransactionID(); - rc = path::RippleCalc::rippleCalculate ( + JLOG(j_.debug()) << "Entering RippleCalc in payment: " + << ctx_.tx.getTransactionID(); + rc = path::RippleCalc::rippleCalculate( pv, maxSourceAmount, saDstAmount, @@ -415,38 +417,36 @@ Payment::doApply () // TODO: is this right? If the amount is the correct amount, was // the delivered amount previously set? - if (rc.result () == tesSUCCESS && - rc.actualAmountOut != saDstAmount) + if (rc.result() == tesSUCCESS && rc.actualAmountOut != saDstAmount) { - if (deliverMin && rc.actualAmountOut < - *deliverMin) - rc.setResult (tecPATH_PARTIAL); + if (deliverMin && rc.actualAmountOut < *deliverMin) + rc.setResult(tecPATH_PARTIAL); else - ctx_.deliver (rc.actualAmountOut); + ctx_.deliver(rc.actualAmountOut); } - auto terResult = rc.result (); + auto terResult = rc.result(); // Because of its overhead, if RippleCalc // fails with a retry code, claim a fee // instead. Maybe the user will be more // careful with their path spec next time. - if (isTerRetry (terResult)) + if (isTerRetry(terResult)) terResult = tecPATH_DRY; return terResult; } - assert (saDstAmount.native ()); + assert(saDstAmount.native()); // Direct XRP payment. auto const sleSrc = view().peek(keylet::account(account_)); - if (! sleSrc) + if (!sleSrc) return tefINTERNAL; // uOwnerCount is the number of entries in this ledger for this // account that require a reserve. - auto const uOwnerCount = sleSrc->getFieldU32 (sfOwnerCount); + auto const uOwnerCount = sleSrc->getFieldU32(sfOwnerCount); // This is the total reserve in drops. auto const reserve = view().fees().accountReserve(uOwnerCount); @@ -454,17 +454,16 @@ Payment::doApply () // mPriorBalance is the balance on the sending account BEFORE the // fees were charged. We want to make sure we have enough reserve // to send. Allow final spend to use reserve for fee. - auto const mmm = std::max(reserve, - ctx_.tx.getFieldAmount (sfFee).xrp ()); + auto const mmm = std::max(reserve, ctx_.tx.getFieldAmount(sfFee).xrp()); - if (mPriorBalance < saDstAmount.xrp () + mmm) + if (mPriorBalance < saDstAmount.xrp() + mmm) { // Vote no. However the transaction might succeed, if applied in // a different order. - JLOG(j_.trace()) << "Delay transaction: Insufficient funds: " << - " " << to_string (mPriorBalance) << - " / " << to_string (saDstAmount.xrp () + mmm) << - " (" << to_string (reserve) << ")"; + JLOG(j_.trace()) << "Delay transaction: Insufficient funds: " + << " " << to_string(mPriorBalance) << " / " + << to_string(saDstAmount.xrp() + mmm) << " (" + << to_string(reserve) << ")"; return tecUNFUNDED_PAYMENT; } @@ -493,15 +492,14 @@ Payment::doApply () // to get the account un-wedged. if (uDstAccountID != account_) { - if (! view().exists ( - keylet::depositPreauth (uDstAccountID, account_))) + if (!view().exists(keylet::depositPreauth(uDstAccountID, account_))) { // Get the base reserve. - XRPAmount const dstReserve {view().fees().accountReserve (0)}; + XRPAmount const dstReserve{view().fees().accountReserve(0)}; if (saDstAmount > dstReserve || - sleDst->getFieldAmount (sfBalance) > dstReserve) - return tecNO_PERMISSION; + sleDst->getFieldAmount(sfBalance) > dstReserve) + return tecNO_PERMISSION; } } } @@ -518,4 +516,4 @@ Payment::doApply () return tesSUCCESS; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/Payment.h b/src/ripple/app/tx/impl/Payment.h index b4f455fcc6..33d0cac0c8 100644 --- a/src/ripple/app/tx/impl/Payment.h +++ b/src/ripple/app/tx/impl/Payment.h @@ -29,8 +29,7 @@ namespace ripple { // See https://ripple.com/wiki/Transaction_Format#Payment_.280.29 -class Payment - : public Transactor +class Payment : public Transactor { /* The largest number of paths we allow */ static std::size_t const MaxPathSize = 6; @@ -39,26 +38,23 @@ class Payment static std::size_t const MaxPathLength = 8; public: - explicit Payment (ApplyContext& ctx) - : Transactor(ctx) + explicit Payment(ApplyContext& ctx) : Transactor(ctx) { } - static - XRPAmount + static XRPAmount calculateMaxSpend(STTx const& tx); - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER + static TER preclaim(PreclaimContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/SetAccount.cpp b/src/ripple/app/tx/impl/SetAccount.cpp index aac7d109f1..0ee305d344 100644 --- a/src/ripple/app/tx/impl/SetAccount.cpp +++ b/src/ripple/app/tx/impl/SetAccount.cpp @@ -20,12 +20,12 @@ #include #include #include +#include #include #include #include #include #include -#include namespace ripple { @@ -33,32 +33,32 @@ bool SetAccount::affectsSubsequentTransactionAuth(STTx const& tx) { auto const uTxFlags = tx.getFlags(); - if(uTxFlags & (tfRequireAuth | tfOptionalAuth)) + if (uTxFlags & (tfRequireAuth | tfOptionalAuth)) return true; auto const uSetFlag = tx[~sfSetFlag]; - if(uSetFlag && (*uSetFlag == asfRequireAuth || - *uSetFlag == asfDisableMaster || - *uSetFlag == asfAccountTxnID)) - return true; + if (uSetFlag && + (*uSetFlag == asfRequireAuth || *uSetFlag == asfDisableMaster || + *uSetFlag == asfAccountTxnID)) + return true; auto const uClearFlag = tx[~sfClearFlag]; - return uClearFlag && (*uClearFlag == asfRequireAuth || - *uClearFlag == asfDisableMaster || - *uClearFlag == asfAccountTxnID); + return uClearFlag && + (*uClearFlag == asfRequireAuth || *uClearFlag == asfDisableMaster || + *uClearFlag == asfAccountTxnID); } NotTEC -SetAccount::preflight (PreflightContext const& ctx) +SetAccount::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto& tx = ctx.tx; auto& j = ctx.j; - std::uint32_t const uTxFlags = tx.getFlags (); + std::uint32_t const uTxFlags = tx.getFlags(); if (uTxFlags & tfAccountSetMask) { @@ -66,8 +66,8 @@ SetAccount::preflight (PreflightContext const& ctx) return temINVALID_FLAG; } - std::uint32_t const uSetFlag = tx.getFieldU32 (sfSetFlag); - std::uint32_t const uClearFlag = tx.getFieldU32 (sfClearFlag); + std::uint32_t const uSetFlag = tx.getFieldU32(sfSetFlag); + std::uint32_t const uClearFlag = tx.getFieldU32(sfClearFlag); if ((uSetFlag != 0) && (uSetFlag == uClearFlag)) { @@ -78,8 +78,10 @@ SetAccount::preflight (PreflightContext const& ctx) // // RequireAuth // - bool bSetRequireAuth = (uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth); - bool bClearRequireAuth = (uTxFlags & tfOptionalAuth) || (uClearFlag == asfRequireAuth); + bool bSetRequireAuth = + (uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth); + bool bClearRequireAuth = + (uTxFlags & tfOptionalAuth) || (uClearFlag == asfRequireAuth); if (bSetRequireAuth && bClearRequireAuth) { @@ -90,8 +92,10 @@ SetAccount::preflight (PreflightContext const& ctx) // // RequireDestTag // - bool bSetRequireDest = (uTxFlags & TxFlag::requireDestTag) || (uSetFlag == asfRequireDest); - bool bClearRequireDest = (uTxFlags & tfOptionalDestTag) || (uClearFlag == asfRequireDest); + bool bSetRequireDest = + (uTxFlags & TxFlag::requireDestTag) || (uSetFlag == asfRequireDest); + bool bClearRequireDest = + (uTxFlags & tfOptionalDestTag) || (uClearFlag == asfRequireDest); if (bSetRequireDest && bClearRequireDest) { @@ -102,8 +106,10 @@ SetAccount::preflight (PreflightContext const& ctx) // // DisallowXRP // - bool bSetDisallowXRP = (uTxFlags & tfDisallowXRP) || (uSetFlag == asfDisallowXRP); - bool bClearDisallowXRP = (uTxFlags & tfAllowXRP) || (uClearFlag == asfDisallowXRP); + bool bSetDisallowXRP = + (uTxFlags & tfDisallowXRP) || (uSetFlag == asfDisallowXRP); + bool bClearDisallowXRP = + (uTxFlags & tfAllowXRP) || (uClearFlag == asfDisallowXRP); if (bSetDisallowXRP && bClearDisallowXRP) { @@ -112,30 +118,32 @@ SetAccount::preflight (PreflightContext const& ctx) } // TransferRate - if (tx.isFieldPresent (sfTransferRate)) + if (tx.isFieldPresent(sfTransferRate)) { - std::uint32_t uRate = tx.getFieldU32 (sfTransferRate); + std::uint32_t uRate = tx.getFieldU32(sfTransferRate); if (uRate && (uRate < QUALITY_ONE)) { - JLOG(j.trace()) << "Malformed transaction: Transfer rate too small."; + JLOG(j.trace()) + << "Malformed transaction: Transfer rate too small."; return temBAD_TRANSFER_RATE; } if (uRate > 2 * QUALITY_ONE) { - JLOG(j.trace()) << "Malformed transaction: Transfer rate too large."; + JLOG(j.trace()) + << "Malformed transaction: Transfer rate too large."; return temBAD_TRANSFER_RATE; } } // TickSize - if (tx.isFieldPresent (sfTickSize)) + if (tx.isFieldPresent(sfTickSize)) { auto uTickSize = tx[sfTickSize]; if (uTickSize && ((uTickSize < Quality::minTickSize) || - (uTickSize > Quality::maxTickSize))) + (uTickSize > Quality::maxTickSize))) { JLOG(j.trace()) << "Malformed transaction: Bad tick size."; return temBAD_TICK_SIZE; @@ -144,7 +152,7 @@ SetAccount::preflight (PreflightContext const& ctx) if (auto const mk = tx[~sfMessageKey]) { - if (mk->size() && ! publicKeyType ({mk->data(), mk->size()})) + if (mk->size() && !publicKeyType({mk->data(), mk->size()})) { JLOG(j.trace()) << "Invalid message key specified."; return telBAD_PUBLIC_KEY; @@ -169,7 +177,7 @@ SetAccount::preclaim(PreclaimContext const& ctx) std::uint32_t const uTxFlags = ctx.tx.getFlags(); auto const sle = ctx.view.read(keylet::account(id)); - if (! sle) + if (!sle) return terNO_ACCOUNT; std::uint32_t const uFlagsIn = sle->getFieldU32(sfFlags); @@ -177,18 +185,18 @@ SetAccount::preclaim(PreclaimContext const& ctx) std::uint32_t const uSetFlag = ctx.tx.getFieldU32(sfSetFlag); // legacy AccountSet flags - bool bSetRequireAuth = (uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth); + bool bSetRequireAuth = + (uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth); // // RequireAuth // if (bSetRequireAuth && !(uFlagsIn & lsfRequireAuth)) { - if (!dirIsEmpty(ctx.view, - keylet::ownerDir(id))) + if (!dirIsEmpty(ctx.view, keylet::ownerDir(id))) { JLOG(ctx.j.trace()) << "Retry: Owner directory not empty."; - return (ctx.flags & tapRETRY) ? TER {terOWNERS} : TER {tecOWNERS}; + return (ctx.flags & tapRETRY) ? TER{terOWNERS} : TER{tecOWNERS}; } } @@ -196,35 +204,40 @@ SetAccount::preclaim(PreclaimContext const& ctx) } TER -SetAccount::doApply () +SetAccount::doApply() { auto const sle = view().peek(keylet::account(account_)); - if (! sle) + if (!sle) return tefINTERNAL; - std::uint32_t const uFlagsIn = sle->getFieldU32 (sfFlags); + std::uint32_t const uFlagsIn = sle->getFieldU32(sfFlags); std::uint32_t uFlagsOut = uFlagsIn; - STTx const& tx {ctx_.tx}; - std::uint32_t const uSetFlag {tx.getFieldU32 (sfSetFlag)}; - std::uint32_t const uClearFlag {tx.getFieldU32 (sfClearFlag)}; + STTx const& tx{ctx_.tx}; + std::uint32_t const uSetFlag{tx.getFieldU32(sfSetFlag)}; + std::uint32_t const uClearFlag{tx.getFieldU32(sfClearFlag)}; // legacy AccountSet flags - std::uint32_t const uTxFlags {tx.getFlags ()}; - bool const bSetRequireDest {(uTxFlags & TxFlag::requireDestTag) || (uSetFlag == asfRequireDest)}; - bool const bClearRequireDest {(uTxFlags & tfOptionalDestTag) || (uClearFlag == asfRequireDest)}; - bool const bSetRequireAuth {(uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth)}; - bool const bClearRequireAuth {(uTxFlags & tfOptionalAuth) || (uClearFlag == asfRequireAuth)}; - bool const bSetDisallowXRP {(uTxFlags & tfDisallowXRP) || (uSetFlag == asfDisallowXRP)}; - bool const bClearDisallowXRP {(uTxFlags & tfAllowXRP) || (uClearFlag == asfDisallowXRP)}; + std::uint32_t const uTxFlags{tx.getFlags()}; + bool const bSetRequireDest{ + (uTxFlags & TxFlag::requireDestTag) || (uSetFlag == asfRequireDest)}; + bool const bClearRequireDest{ + (uTxFlags & tfOptionalDestTag) || (uClearFlag == asfRequireDest)}; + bool const bSetRequireAuth{ + (uTxFlags & tfRequireAuth) || (uSetFlag == asfRequireAuth)}; + bool const bClearRequireAuth{ + (uTxFlags & tfOptionalAuth) || (uClearFlag == asfRequireAuth)}; + bool const bSetDisallowXRP{ + (uTxFlags & tfDisallowXRP) || (uSetFlag == asfDisallowXRP)}; + bool const bClearDisallowXRP{ + (uTxFlags & tfAllowXRP) || (uClearFlag == asfDisallowXRP)}; - bool const sigWithMaster {[&tx, &acct = account_] () - { + bool const sigWithMaster{[&tx, &acct = account_]() { auto const spk = tx.getSigningPubKey(); - if (publicKeyType (makeSlice (spk))) + if (publicKeyType(makeSlice(spk))) { - PublicKey const signingPubKey (makeSlice (spk)); + PublicKey const signingPubKey(makeSlice(spk)); if (calcAccountID(signingPubKey) == acct) return true; @@ -288,8 +301,8 @@ SetAccount::doApply () return tecNEED_MASTER_KEY; } - if ((!sle->isFieldPresent (sfRegularKey)) && - (!view().peek (keylet::signers (account_)))) + if ((!sle->isFieldPresent(sfRegularKey)) && + (!view().peek(keylet::signers(account_)))) { // Account has no regular key or multi-signer signer list. return tecNO_ALTERNATIVE_KEY; @@ -354,16 +367,16 @@ SetAccount::doApply () // // Track transaction IDs signed by this account in its root // - if ((uSetFlag == asfAccountTxnID) && !sle->isFieldPresent (sfAccountTxnID)) + if ((uSetFlag == asfAccountTxnID) && !sle->isFieldPresent(sfAccountTxnID)) { JLOG(j_.trace()) << "Set AccountTxnID."; - sle->makeFieldPresent (sfAccountTxnID); - } + sle->makeFieldPresent(sfAccountTxnID); + } - if ((uClearFlag == asfAccountTxnID) && sle->isFieldPresent (sfAccountTxnID)) + if ((uClearFlag == asfAccountTxnID) && sle->isFieldPresent(sfAccountTxnID)) { JLOG(j_.trace()) << "Clear AccountTxnID."; - sle->makeFieldAbsent (sfAccountTxnID); + sle->makeFieldAbsent(sfAccountTxnID); } // @@ -386,120 +399,120 @@ SetAccount::doApply () // // EmailHash // - if (tx.isFieldPresent (sfEmailHash)) + if (tx.isFieldPresent(sfEmailHash)) { - uint128 const uHash = tx.getFieldH128 (sfEmailHash); + uint128 const uHash = tx.getFieldH128(sfEmailHash); if (!uHash) { JLOG(j_.trace()) << "unset email hash"; - sle->makeFieldAbsent (sfEmailHash); + sle->makeFieldAbsent(sfEmailHash); } else { JLOG(j_.trace()) << "set email hash"; - sle->setFieldH128 (sfEmailHash, uHash); + sle->setFieldH128(sfEmailHash, uHash); } } // // WalletLocator // - if (tx.isFieldPresent (sfWalletLocator)) + if (tx.isFieldPresent(sfWalletLocator)) { - uint256 const uHash = tx.getFieldH256 (sfWalletLocator); + uint256 const uHash = tx.getFieldH256(sfWalletLocator); if (!uHash) { JLOG(j_.trace()) << "unset wallet locator"; - sle->makeFieldAbsent (sfWalletLocator); + sle->makeFieldAbsent(sfWalletLocator); } else { JLOG(j_.trace()) << "set wallet locator"; - sle->setFieldH256 (sfWalletLocator, uHash); + sle->setFieldH256(sfWalletLocator, uHash); } } // // MessageKey // - if (tx.isFieldPresent (sfMessageKey)) + if (tx.isFieldPresent(sfMessageKey)) { - Blob const messageKey = tx.getFieldVL (sfMessageKey); + Blob const messageKey = tx.getFieldVL(sfMessageKey); - if (messageKey.empty ()) + if (messageKey.empty()) { JLOG(j_.debug()) << "set message key"; - sle->makeFieldAbsent (sfMessageKey); + sle->makeFieldAbsent(sfMessageKey); } else { JLOG(j_.debug()) << "set message key"; - sle->setFieldVL (sfMessageKey, messageKey); + sle->setFieldVL(sfMessageKey, messageKey); } } // // Domain // - if (tx.isFieldPresent (sfDomain)) + if (tx.isFieldPresent(sfDomain)) { - Blob const domain = tx.getFieldVL (sfDomain); + Blob const domain = tx.getFieldVL(sfDomain); - if (domain.empty ()) + if (domain.empty()) { JLOG(j_.trace()) << "unset domain"; - sle->makeFieldAbsent (sfDomain); + sle->makeFieldAbsent(sfDomain); } else { JLOG(j_.trace()) << "set domain"; - sle->setFieldVL (sfDomain, domain); + sle->setFieldVL(sfDomain, domain); } } // // TransferRate // - if (tx.isFieldPresent (sfTransferRate)) + if (tx.isFieldPresent(sfTransferRate)) { - std::uint32_t uRate = tx.getFieldU32 (sfTransferRate); + std::uint32_t uRate = tx.getFieldU32(sfTransferRate); if (uRate == 0 || uRate == QUALITY_ONE) { JLOG(j_.trace()) << "unset transfer rate"; - sle->makeFieldAbsent (sfTransferRate); + sle->makeFieldAbsent(sfTransferRate); } else { JLOG(j_.trace()) << "set transfer rate"; - sle->setFieldU32 (sfTransferRate, uRate); + sle->setFieldU32(sfTransferRate, uRate); } } // // TickSize // - if (tx.isFieldPresent (sfTickSize)) + if (tx.isFieldPresent(sfTickSize)) { auto uTickSize = tx[sfTickSize]; if ((uTickSize == 0) || (uTickSize == Quality::maxTickSize)) { JLOG(j_.trace()) << "unset tick size"; - sle->makeFieldAbsent (sfTickSize); + sle->makeFieldAbsent(sfTickSize); } else { JLOG(j_.trace()) << "set tick size"; - sle->setFieldU8 (sfTickSize, uTickSize); + sle->setFieldU8(sfTickSize, uTickSize); } } if (uFlagsIn != uFlagsOut) - sle->setFieldU32 (sfFlags, uFlagsOut); + sle->setFieldU32(sfFlags, uFlagsOut); return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/SetAccount.h b/src/ripple/app/tx/impl/SetAccount.h index c67fbb6548..37bd02ddae 100644 --- a/src/ripple/app/tx/impl/SetAccount.h +++ b/src/ripple/app/tx/impl/SetAccount.h @@ -29,32 +29,28 @@ namespace ripple { -class SetAccount - : public Transactor +class SetAccount : public Transactor { static std::size_t const DOMAIN_BYTES_MAX = 256; public: - explicit SetAccount (ApplyContext& ctx) - : Transactor(ctx) + explicit SetAccount(ApplyContext& ctx) : Transactor(ctx) { } - static - bool + static bool affectsSubsequentTransactionAuth(STTx const& tx); - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER + static TER preclaim(PreclaimContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/SetRegularKey.cpp b/src/ripple/app/tx/impl/SetRegularKey.cpp index 96afc71f19..aee973a1cb 100644 --- a/src/ripple/app/tx/impl/SetRegularKey.cpp +++ b/src/ripple/app/tx/impl/SetRegularKey.cpp @@ -25,20 +25,18 @@ namespace ripple { FeeUnit64 -SetRegularKey::calculateBaseFee ( - ReadView const& view, - STTx const& tx) +SetRegularKey::calculateBaseFee(ReadView const& view, STTx const& tx) { auto const id = tx.getAccountID(sfAccount); auto const spk = tx.getSigningPubKey(); - if (publicKeyType (makeSlice (spk))) + if (publicKeyType(makeSlice(spk))) { - if (calcAccountID(PublicKey (makeSlice(spk))) == id) + if (calcAccountID(PublicKey(makeSlice(spk))) == id) { auto const sle = view.read(keylet::account(id)); - if (sle && (! (sle->getFlags () & lsfPasswordSpent))) + if (sle && (!(sle->getFlags() & lsfPasswordSpent))) { // flag is armed and they signed with the right account return FeeUnit64{0}; @@ -46,30 +44,28 @@ SetRegularKey::calculateBaseFee ( } } - return Transactor::calculateBaseFee (view, tx); + return Transactor::calculateBaseFee(view, tx); } NotTEC -SetRegularKey::preflight (PreflightContext const& ctx) +SetRegularKey::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; - std::uint32_t const uTxFlags = ctx.tx.getFlags (); + std::uint32_t const uTxFlags = ctx.tx.getFlags(); if (uTxFlags & tfUniversalMask) { - JLOG(ctx.j.trace()) << - "Malformed transaction: Invalid flags set."; + JLOG(ctx.j.trace()) << "Malformed transaction: Invalid flags set."; return temINVALID_FLAG; } - - if (ctx.rules.enabled(fixMasterKeyAsRegularKey) - && ctx.tx.isFieldPresent(sfRegularKey) - && (ctx.tx.getAccountID(sfRegularKey) == ctx.tx.getAccountID(sfAccount))) + if (ctx.rules.enabled(fixMasterKeyAsRegularKey) && + ctx.tx.isFieldPresent(sfRegularKey) && + (ctx.tx.getAccountID(sfRegularKey) == ctx.tx.getAccountID(sfAccount))) { return temBAD_REGKEY; } @@ -78,32 +74,30 @@ SetRegularKey::preflight (PreflightContext const& ctx) } TER -SetRegularKey::doApply () +SetRegularKey::doApply() { - auto const sle = view ().peek ( - keylet::account (account_)); - if (! sle) + auto const sle = view().peek(keylet::account(account_)); + if (!sle) return tefINTERNAL; - if (!minimumFee (ctx_.app, ctx_.baseFee, view ().fees (), view ().flags ())) - sle->setFlag (lsfPasswordSpent); + if (!minimumFee(ctx_.app, ctx_.baseFee, view().fees(), view().flags())) + sle->setFlag(lsfPasswordSpent); - if (ctx_.tx.isFieldPresent (sfRegularKey)) + if (ctx_.tx.isFieldPresent(sfRegularKey)) { - sle->setAccountID (sfRegularKey, - ctx_.tx.getAccountID (sfRegularKey)); + sle->setAccountID(sfRegularKey, ctx_.tx.getAccountID(sfRegularKey)); } else { // Account has disabled master key and no multi-signer signer list. - if (sle->isFlag (lsfDisableMaster) && - !view().peek (keylet::signers (account_))) + if (sle->isFlag(lsfDisableMaster) && + !view().peek(keylet::signers(account_))) return tecNO_ALTERNATIVE_KEY; - sle->makeFieldAbsent (sfRegularKey); + sle->makeFieldAbsent(sfRegularKey); } return tesSUCCESS; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/SetRegularKey.h b/src/ripple/app/tx/impl/SetRegularKey.h index b8001e83ec..12b882ae05 100644 --- a/src/ripple/app/tx/impl/SetRegularKey.h +++ b/src/ripple/app/tx/impl/SetRegularKey.h @@ -27,36 +27,29 @@ namespace ripple { -class SetRegularKey - : public Transactor +class SetRegularKey : public Transactor { public: - explicit SetRegularKey (ApplyContext& ctx) - : Transactor(ctx) + explicit SetRegularKey(ApplyContext& ctx) : Transactor(ctx) { } - static - bool + static bool affectsSubsequentTransactionAuth(STTx const& tx) { return true; } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - FeeUnit64 - calculateBaseFee ( - ReadView const& view, - STTx const& tx); + static FeeUnit64 + calculateBaseFee(ReadView const& view, STTx const& tx); - TER doApply () override; + TER + doApply() override; }; -} // ripple +} // namespace ripple #endif - diff --git a/src/ripple/app/tx/impl/SetSignerList.cpp b/src/ripple/app/tx/impl/SetSignerList.cpp index 76501ea6ed..8ae7cd31f9 100644 --- a/src/ripple/app/tx/impl/SetSignerList.cpp +++ b/src/ripple/app/tx/impl/SetSignerList.cpp @@ -37,11 +37,15 @@ namespace ripple { // setting the sfSignerListID to zero in all cases. static std::uint32_t const defaultSignerListID_ = 0; -std::tuple, - SetSignerList::Operation> -SetSignerList::determineOperation(STTx const& tx, - ApplyFlags flags, beast::Journal j) + SetSignerList::Operation> +SetSignerList::determineOperation( + STTx const& tx, + ApplyFlags flags, + beast::Journal j) { // Check the quorum. A non-zero quorum means we're creating or replacing // the list. A zero quorum means we're destroying the list. @@ -72,10 +76,10 @@ SetSignerList::determineOperation(STTx const& tx, } NotTEC -SetSignerList::preflight (PreflightContext const& ctx) +SetSignerList::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto const result = determineOperation(ctx.tx, ctx.flags, ctx.j); @@ -85,8 +89,8 @@ SetSignerList::preflight (PreflightContext const& ctx) if (std::get<3>(result) == unknown) { // Neither a set nor a destroy. Malformed. - JLOG(ctx.j.trace()) << - "Malformed transaction: Invalid signer set list format."; + JLOG(ctx.j.trace()) + << "Malformed transaction: Invalid signer set list format."; return temMALFORMED; } @@ -94,34 +98,33 @@ SetSignerList::preflight (PreflightContext const& ctx) { // Validate our settings. auto const account = ctx.tx.getAccountID(sfAccount); - NotTEC const ter = - validateQuorumAndSignerEntries(std::get<1>(result), - std::get<2>(result), account, ctx.j); + NotTEC const ter = validateQuorumAndSignerEntries( + std::get<1>(result), std::get<2>(result), account, ctx.j); if (ter != tesSUCCESS) { return ter; } } - return preflight2 (ctx); + return preflight2(ctx); } TER -SetSignerList::doApply () +SetSignerList::doApply() { // Perform the operation preCompute() decided on. switch (do_) { - case set: - return replaceSignerList (); + case set: + return replaceSignerList(); - case destroy: - return destroySignerList (); + case destroy: + return destroySignerList(); - default: - break; + default: + break; } - assert (false); // Should not be possible to get here. + assert(false); // Should not be possible to get here. return temMALFORMED; } @@ -147,7 +150,7 @@ SetSignerList::preCompute() // is valid until the featureMultiSignReserve amendment passes. Once it // passes then just 1 OwnerCount is associated with a SignerList. static int -signerCountBasedOwnerCountDelta (std::size_t entryCount) +signerCountBasedOwnerCountDelta(std::size_t entryCount) { // We always compute the full change in OwnerCount, taking into account: // o The fact that we're adding/removing a SignerList and @@ -163,19 +166,22 @@ signerCountBasedOwnerCountDelta (std::size_t entryCount) // // The static_cast should always be safe since entryCount should always // be in the range from 1 to 8. We've got a lot of room to grow. - assert (entryCount >= STTx::minMultiSigners); - assert (entryCount <= STTx::maxMultiSigners); + assert(entryCount >= STTx::minMultiSigners); + assert(entryCount <= STTx::maxMultiSigners); return 2 + static_cast(entryCount); } static TER -removeSignersFromLedger ( - Application& app, ApplyView& view, Keylet const& accountKeylet, - Keylet const& ownerDirKeylet, Keylet const& signerListKeylet) +removeSignersFromLedger( + Application& app, + ApplyView& view, + Keylet const& accountKeylet, + Keylet const& ownerDirKeylet, + Keylet const& signerListKeylet) { // We have to examine the current SignerList so we know how much to // reduce the OwnerCount. - SLE::pointer signers = view.peek (signerListKeylet); + SLE::pointer signers = view.peek(signerListKeylet); // If the signer list doesn't exist we've already succeeded in deleting it. if (!signers) @@ -187,52 +193,56 @@ removeSignersFromLedger ( int removeFromOwnerCount = -1; if ((signers->getFlags() & lsfOneOwnerCount) == 0) { - STArray const& actualList = signers->getFieldArray (sfSignerEntries); + STArray const& actualList = signers->getFieldArray(sfSignerEntries); removeFromOwnerCount = - signerCountBasedOwnerCountDelta (actualList.size()) * -1; + signerCountBasedOwnerCountDelta(actualList.size()) * -1; } // Remove the node from the account directory. auto const hint = (*signers)[sfOwnerNode]; - if (! view.dirRemove( - ownerDirKeylet, hint, signerListKeylet.key, false)) + if (!view.dirRemove(ownerDirKeylet, hint, signerListKeylet.key, false)) { return tefBAD_LEDGER; } - adjustOwnerCount(view, - view.peek(accountKeylet), removeFromOwnerCount, app.journal("View")); + adjustOwnerCount( + view, + view.peek(accountKeylet), + removeFromOwnerCount, + app.journal("View")); - view.erase (signers); + view.erase(signers); return tesSUCCESS; } TER -SetSignerList::removeFromLedger ( - Application& app, ApplyView& view, AccountID const& account) +SetSignerList::removeFromLedger( + Application& app, + ApplyView& view, + AccountID const& account) { - auto const accountKeylet = keylet::account (account); - auto const ownerDirKeylet = keylet::ownerDir (account); - auto const signerListKeylet = keylet::signers (account); + auto const accountKeylet = keylet::account(account); + auto const ownerDirKeylet = keylet::ownerDir(account); + auto const signerListKeylet = keylet::signers(account); - return removeSignersFromLedger ( + return removeSignersFromLedger( app, view, accountKeylet, ownerDirKeylet, signerListKeylet); } NotTEC -SetSignerList::validateQuorumAndSignerEntries ( +SetSignerList::validateQuorumAndSignerEntries( std::uint32_t quorum, - std::vector const& signers, - AccountID const& account, - beast::Journal j) + std::vector const& signers, + AccountID const& account, + beast::Journal j) { // Reject if there are too many or too few entries in the list. { - std::size_t const signerCount = signers.size (); - if ((signerCount < STTx::minMultiSigners) - || (signerCount > STTx::maxMultiSigners)) + std::size_t const signerCount = signers.size(); + if ((signerCount < STTx::minMultiSigners) || + (signerCount > STTx::maxMultiSigners)) { JLOG(j.trace()) << "Too many or too few signers in signer list."; return temMALFORMED; @@ -241,8 +251,7 @@ SetSignerList::validateQuorumAndSignerEntries ( // Make sure there are no duplicate signers. assert(std::is_sorted(signers.begin(), signers.end())); - if (std::adjacent_find ( - signers.begin (), signers.end ()) != signers.end ()) + if (std::adjacent_find(signers.begin(), signers.end()) != signers.end()) { JLOG(j.trace()) << "Duplicate signers in signer list"; return temBAD_SIGNER; @@ -250,7 +259,7 @@ SetSignerList::validateQuorumAndSignerEntries ( // Make sure no signers reference this account. Also make sure the // quorum can be reached. - std::uint64_t allSignersWeight (0); + std::uint64_t allSignersWeight(0); for (auto const& signer : signers) { std::uint32_t const weight = signer.weight; @@ -280,36 +289,36 @@ SetSignerList::validateQuorumAndSignerEntries ( } TER -SetSignerList::replaceSignerList () +SetSignerList::replaceSignerList() { - auto const accountKeylet = keylet::account (account_); - auto const ownerDirKeylet = keylet::ownerDir (account_); - auto const signerListKeylet = keylet::signers (account_); + auto const accountKeylet = keylet::account(account_); + auto const ownerDirKeylet = keylet::ownerDir(account_); + auto const signerListKeylet = keylet::signers(account_); // This may be either a create or a replace. Preemptively remove any // old signer list. May reduce the reserve, so this is done before // checking the reserve. - if (TER const ter = removeSignersFromLedger ( - ctx_.app, view(), accountKeylet, ownerDirKeylet, signerListKeylet)) - return ter; + if (TER const ter = removeSignersFromLedger( + ctx_.app, view(), accountKeylet, ownerDirKeylet, signerListKeylet)) + return ter; auto const sle = view().peek(accountKeylet); if (!sle) return tefINTERNAL; // Compute new reserve. Verify the account has funds to meet the reserve. - std::uint32_t const oldOwnerCount {(*sle)[sfOwnerCount]}; + std::uint32_t const oldOwnerCount{(*sle)[sfOwnerCount]}; // The required reserve changes based on featureMultiSignReserve... - int addedOwnerCount {1}; - std::uint32_t flags {lsfOneOwnerCount}; - if (! ctx_.view().rules().enabled(featureMultiSignReserve)) + int addedOwnerCount{1}; + std::uint32_t flags{lsfOneOwnerCount}; + if (!ctx_.view().rules().enabled(featureMultiSignReserve)) { - addedOwnerCount = signerCountBasedOwnerCountDelta (signers_.size ()); + addedOwnerCount = signerCountBasedOwnerCountDelta(signers_.size()); flags = 0; } - XRPAmount const newReserve { + XRPAmount const newReserve{ view().fees().accountReserve(oldOwnerCount + addedOwnerCount)}; // We check the reserve against the starting balance because we want to @@ -320,21 +329,26 @@ SetSignerList::replaceSignerList () // Everything's ducky. Add the ltSIGNER_LIST to the ledger. auto signerList = std::make_shared(signerListKeylet); - view().insert (signerList); - writeSignersToSLE (signerList, flags); + view().insert(signerList); + writeSignersToSLE(signerList, flags); - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); // Add the signer list to the account's directory. - auto const page = dirAdd (ctx_.view (), ownerDirKeylet, - signerListKeylet.key, false, describeOwnerDir (account_), viewJ); + auto const page = dirAdd( + ctx_.view(), + ownerDirKeylet, + signerListKeylet.key, + false, + describeOwnerDir(account_), + viewJ); - JLOG(j_.trace()) << "Create signer list for account " << - toBase58 (account_) << ": " << (page ? "success" : "failure"); + JLOG(j_.trace()) << "Create signer list for account " << toBase58(account_) + << ": " << (page ? "success" : "failure"); if (!page) return tecDIR_FULL; - signerList->setFieldU64 (sfOwnerNode, *page); + signerList->setFieldU64(sfOwnerNode, *page); // If we succeeded, the new entry counts against the // creator's reserve. @@ -343,48 +357,49 @@ SetSignerList::replaceSignerList () } TER -SetSignerList::destroySignerList () +SetSignerList::destroySignerList() { - auto const accountKeylet = keylet::account (account_); + auto const accountKeylet = keylet::account(account_); // Destroying the signer list is only allowed if either the master key // is enabled or there is a regular key. - SLE::pointer ledgerEntry = view().peek (accountKeylet); - if (! ledgerEntry) + SLE::pointer ledgerEntry = view().peek(accountKeylet); + if (!ledgerEntry) return tefINTERNAL; - if ((ledgerEntry->isFlag (lsfDisableMaster)) && - (!ledgerEntry->isFieldPresent (sfRegularKey))) - return tecNO_ALTERNATIVE_KEY; + if ((ledgerEntry->isFlag(lsfDisableMaster)) && + (!ledgerEntry->isFieldPresent(sfRegularKey))) + return tecNO_ALTERNATIVE_KEY; - auto const ownerDirKeylet = keylet::ownerDir (account_); - auto const signerListKeylet = keylet::signers (account_); + auto const ownerDirKeylet = keylet::ownerDir(account_); + auto const signerListKeylet = keylet::signers(account_); return removeSignersFromLedger( ctx_.app, view(), accountKeylet, ownerDirKeylet, signerListKeylet); } void -SetSignerList::writeSignersToSLE ( - SLE::pointer const& ledgerEntry, std::uint32_t flags) const +SetSignerList::writeSignersToSLE( + SLE::pointer const& ledgerEntry, + std::uint32_t flags) const { // Assign the quorum, default SignerListID, and flags. - ledgerEntry->setFieldU32 (sfSignerQuorum, quorum_); - ledgerEntry->setFieldU32 (sfSignerListID, defaultSignerListID_); - if (flags) // Only set flags if they are non-default (default is zero). - ledgerEntry->setFieldU32 (sfFlags, flags); + ledgerEntry->setFieldU32(sfSignerQuorum, quorum_); + ledgerEntry->setFieldU32(sfSignerListID, defaultSignerListID_); + if (flags) // Only set flags if they are non-default (default is zero). + ledgerEntry->setFieldU32(sfFlags, flags); // Create the SignerListArray one SignerEntry at a time. - STArray toLedger (signers_.size ()); + STArray toLedger(signers_.size()); for (auto const& entry : signers_) { toLedger.emplace_back(sfSignerEntry); STObject& obj = toLedger.back(); - obj.reserve (2); - obj.setAccountID (sfAccount, entry.account); - obj.setFieldU16 (sfSignerWeight, entry.weight); + obj.reserve(2); + obj.setAccountID(sfAccount, entry.account); + obj.setFieldU16(sfSignerWeight, entry.weight); } // Assign the SignerEntries. - ledgerEntry->setFieldArray (sfSignerEntries, toLedger); + ledgerEntry->setFieldArray(sfSignerEntries, toLedger); } -} // namespace ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/SetSignerList.h b/src/ripple/app/tx/impl/SetSignerList.h index 1219d2bb25..0434a2a852 100644 --- a/src/ripple/app/tx/impl/SetSignerList.h +++ b/src/ripple/app/tx/impl/SetSignerList.h @@ -21,13 +21,13 @@ #define RIPPLE_TX_SETSIGNERLIST_H_INCLUDED #include -#include #include -#include -#include -#include -#include +#include #include +#include +#include +#include +#include #include #include #include @@ -42,59 +42,62 @@ class SetSignerList : public Transactor { private: // Values determined during preCompute for use later. - enum Operation {unknown, set, destroy}; - Operation do_ {unknown}; - std::uint32_t quorum_ {0}; + enum Operation { unknown, set, destroy }; + Operation do_{unknown}; + std::uint32_t quorum_{0}; std::vector signers_; public: - explicit SetSignerList (ApplyContext& ctx) - : Transactor(ctx) + explicit SetSignerList(ApplyContext& ctx) : Transactor(ctx) { } - static - bool + static bool affectsSubsequentTransactionAuth(STTx const& tx) { return true; } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - TER doApply () override; - void preCompute() override; + TER + doApply() override; + void + preCompute() override; // Interface used by DeleteAccount - static - TER - removeFromLedger ( - Application& app, ApplyView& view, AccountID const& account); + static TER + removeFromLedger( + Application& app, + ApplyView& view, + AccountID const& account); private: - static - std::tuple, - Operation> - determineOperation(STTx const& tx, - ApplyFlags flags, beast::Journal j); + Operation> + determineOperation(STTx const& tx, ApplyFlags flags, beast::Journal j); - static - NotTEC validateQuorumAndSignerEntries ( + static NotTEC + validateQuorumAndSignerEntries( std::uint32_t quorum, - std::vector const& signers, - AccountID const& account, - beast::Journal j); + std::vector const& signers, + AccountID const& account, + beast::Journal j); - TER replaceSignerList (); - TER destroySignerList (); + TER + replaceSignerList(); + TER + destroySignerList(); - void writeSignersToSLE ( - SLE::pointer const& ledgerEntry, std::uint32_t flags) const; + void + writeSignersToSLE(SLE::pointer const& ledgerEntry, std::uint32_t flags) + const; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/SetTrust.cpp b/src/ripple/app/tx/impl/SetTrust.cpp index da002f5ad3..afacee9a6b 100644 --- a/src/ripple/app/tx/impl/SetTrust.cpp +++ b/src/ripple/app/tx/impl/SetTrust.cpp @@ -19,71 +19,66 @@ #include #include -#include -#include -#include -#include #include +#include +#include +#include +#include namespace ripple { NotTEC -SetTrust::preflight (PreflightContext const& ctx) +SetTrust::preflight(PreflightContext const& ctx) { - auto const ret = preflight1 (ctx); - if (!isTesSuccess (ret)) + auto const ret = preflight1(ctx); + if (!isTesSuccess(ret)) return ret; auto& tx = ctx.tx; auto& j = ctx.j; - std::uint32_t const uTxFlags = tx.getFlags (); + std::uint32_t const uTxFlags = tx.getFlags(); if (uTxFlags & tfTrustSetMask) { - JLOG(j.trace()) << - "Malformed transaction: Invalid flags set."; + JLOG(j.trace()) << "Malformed transaction: Invalid flags set."; return temINVALID_FLAG; } - STAmount const saLimitAmount (tx.getFieldAmount (sfLimitAmount)); + STAmount const saLimitAmount(tx.getFieldAmount(sfLimitAmount)); - if (!isLegalNet (saLimitAmount)) + if (!isLegalNet(saLimitAmount)) return temBAD_AMOUNT; - if (saLimitAmount.native ()) + if (saLimitAmount.native()) { - JLOG(j.trace()) << - "Malformed transaction: specifies native limit " << - saLimitAmount.getFullText (); + JLOG(j.trace()) << "Malformed transaction: specifies native limit " + << saLimitAmount.getFullText(); return temBAD_LIMIT; } - if (badCurrency() == saLimitAmount.getCurrency ()) + if (badCurrency() == saLimitAmount.getCurrency()) { - JLOG(j.trace()) << - "Malformed transaction: specifies XRP as IOU"; + JLOG(j.trace()) << "Malformed transaction: specifies XRP as IOU"; return temBAD_CURRENCY; } if (saLimitAmount < beast::zero) { - JLOG(j.trace()) << - "Malformed transaction: Negative credit limit."; + JLOG(j.trace()) << "Malformed transaction: Negative credit limit."; return temBAD_LIMIT; } // Check if destination makes sense. - auto const& issuer = saLimitAmount.getIssuer (); + auto const& issuer = saLimitAmount.getIssuer(); if (!issuer || issuer == noAccount()) { - JLOG(j.trace()) << - "Malformed transaction: no destination account."; + JLOG(j.trace()) << "Malformed transaction: no destination account."; return temDST_NEEDED; } - return preflight2 (ctx); + return preflight2(ctx); } TER @@ -91,9 +86,8 @@ SetTrust::preclaim(PreclaimContext const& ctx) { auto const id = ctx.tx[sfAccount]; - auto const sle = ctx.view.read( - keylet::account(id)); - if (! sle) + auto const sle = ctx.view.read(keylet::account(id)); + if (!sle) return terNO_ACCOUNT; std::uint32_t const uTxFlags = ctx.tx.getFlags(); @@ -102,8 +96,7 @@ SetTrust::preclaim(PreclaimContext const& ctx) if (bSetAuth && !(sle->getFieldU32(sfFlags) & lsfRequireAuth)) { - JLOG(ctx.j.trace()) << - "Retry: Auth not required."; + JLOG(ctx.j.trace()) << "Retry: Auth not required."; return tefNO_AUTH_REQUIRED; } @@ -117,13 +110,13 @@ SetTrust::preclaim(PreclaimContext const& ctx) // Prevent trustline to self from being created, // unless one has somehow already been created // (in which case doApply will clean it up). - auto const sleDelete = ctx.view.read( - keylet::line(id, uDstAccountID, currency)); + auto const sleDelete = + ctx.view.read(keylet::line(id, uDstAccountID, currency)); if (!sleDelete) { - JLOG(ctx.j.trace()) << - "Malformed transaction: Can not extend credit to self."; + JLOG(ctx.j.trace()) + << "Malformed transaction: Can not extend credit to self."; return temDST_IS_SRC; } } @@ -132,26 +125,25 @@ SetTrust::preclaim(PreclaimContext const& ctx) } TER -SetTrust::doApply () +SetTrust::doApply() { TER terResult = tesSUCCESS; - STAmount const saLimitAmount (ctx_.tx.getFieldAmount (sfLimitAmount)); - bool const bQualityIn (ctx_.tx.isFieldPresent (sfQualityIn)); - bool const bQualityOut (ctx_.tx.isFieldPresent (sfQualityOut)); + STAmount const saLimitAmount(ctx_.tx.getFieldAmount(sfLimitAmount)); + bool const bQualityIn(ctx_.tx.isFieldPresent(sfQualityIn)); + bool const bQualityOut(ctx_.tx.isFieldPresent(sfQualityOut)); - Currency const currency (saLimitAmount.getCurrency ()); - AccountID uDstAccountID (saLimitAmount.getIssuer ()); + Currency const currency(saLimitAmount.getCurrency()); + AccountID uDstAccountID(saLimitAmount.getIssuer()); // true, iff current is high account. bool const bHigh = account_ > uDstAccountID; - auto const sle = view().peek( - keylet::account(account_)); - if (! sle) + auto const sle = view().peek(keylet::account(account_)); + if (!sle) return tefINTERNAL; - std::uint32_t const uOwnerCount = sle->getFieldU32 (sfOwnerCount); + std::uint32_t const uOwnerCount = sle->getFieldU32(sfOwnerCount); // The reserve that is required to create the line. Note // that although the reserve increases with every item @@ -171,25 +163,26 @@ SetTrust::doApply () // well. A person with no intention of using the gateway // could use the extra XRP for their own purposes. - XRPAmount const reserveCreate ((uOwnerCount < 2) - ? XRPAmount (beast::zero) - : view().fees().accountReserve(uOwnerCount + 1)); + XRPAmount const reserveCreate( + (uOwnerCount < 2) ? XRPAmount(beast::zero) + : view().fees().accountReserve(uOwnerCount + 1)); - std::uint32_t uQualityIn (bQualityIn ? ctx_.tx.getFieldU32 (sfQualityIn) : 0); - std::uint32_t uQualityOut (bQualityOut ? ctx_.tx.getFieldU32 (sfQualityOut) : 0); + std::uint32_t uQualityIn(bQualityIn ? ctx_.tx.getFieldU32(sfQualityIn) : 0); + std::uint32_t uQualityOut( + bQualityOut ? ctx_.tx.getFieldU32(sfQualityOut) : 0); if (bQualityOut && QUALITY_ONE == uQualityOut) uQualityOut = 0; - std::uint32_t const uTxFlags = ctx_.tx.getFlags (); + std::uint32_t const uTxFlags = ctx_.tx.getFlags(); bool const bSetAuth = (uTxFlags & tfSetfAuth); bool const bSetNoRipple = (uTxFlags & tfSetNoRipple); - bool const bClearNoRipple = (uTxFlags & tfClearNoRipple); + bool const bClearNoRipple = (uTxFlags & tfClearNoRipple); bool const bSetFreeze = (uTxFlags & tfSetFreeze); bool const bClearFreeze = (uTxFlags & tfClearFreeze); - auto viewJ = ctx_.app.journal ("View"); + auto viewJ = ctx_.app.journal("View"); if (account_ == uDstAccountID) { @@ -197,62 +190,62 @@ SetTrust::doApply () // trust line to oneself to be deleted. If no such trust // lines exist now, why not remove this code and simply // return an error? - SLE::pointer sleDelete = view().peek ( - keylet::line(account_, uDstAccountID, currency)); + SLE::pointer sleDelete = + view().peek(keylet::line(account_, uDstAccountID, currency)); - JLOG(j_.warn()) << - "Clearing redundant line."; + JLOG(j_.warn()) << "Clearing redundant line."; - return trustDelete (view(), - sleDelete, account_, uDstAccountID, viewJ); + return trustDelete(view(), sleDelete, account_, uDstAccountID, viewJ); } - SLE::pointer sleDst = - view().peek (keylet::account(uDstAccountID)); + SLE::pointer sleDst = view().peek(keylet::account(uDstAccountID)); if (!sleDst) { - JLOG(j_.trace()) << - "Delay transaction: Destination account does not exist."; + JLOG(j_.trace()) + << "Delay transaction: Destination account does not exist."; return tecNO_DST; } STAmount saLimitAllow = saLimitAmount; - saLimitAllow.setIssuer (account_); + saLimitAllow.setIssuer(account_); - SLE::pointer sleRippleState = view().peek ( - keylet::line(account_, uDstAccountID, currency)); + SLE::pointer sleRippleState = + view().peek(keylet::line(account_, uDstAccountID, currency)); if (sleRippleState) { - STAmount saLowBalance; - STAmount saLowLimit; - STAmount saHighBalance; - STAmount saHighLimit; - std::uint32_t uLowQualityIn; - std::uint32_t uLowQualityOut; - std::uint32_t uHighQualityIn; - std::uint32_t uHighQualityOut; - auto const& uLowAccountID = !bHigh ? account_ : uDstAccountID; - auto const& uHighAccountID = bHigh ? account_ : uDstAccountID; - SLE::ref sleLowAccount = !bHigh ? sle : sleDst; - SLE::ref sleHighAccount = bHigh ? sle : sleDst; + STAmount saLowBalance; + STAmount saLowLimit; + STAmount saHighBalance; + STAmount saHighLimit; + std::uint32_t uLowQualityIn; + std::uint32_t uLowQualityOut; + std::uint32_t uHighQualityIn; + std::uint32_t uHighQualityOut; + auto const& uLowAccountID = !bHigh ? account_ : uDstAccountID; + auto const& uHighAccountID = bHigh ? account_ : uDstAccountID; + SLE::ref sleLowAccount = !bHigh ? sle : sleDst; + SLE::ref sleHighAccount = bHigh ? sle : sleDst; // // Balances // - saLowBalance = sleRippleState->getFieldAmount (sfBalance); - saHighBalance = -saLowBalance; + saLowBalance = sleRippleState->getFieldAmount(sfBalance); + saHighBalance = -saLowBalance; // // Limits // - sleRippleState->setFieldAmount (!bHigh ? sfLowLimit : sfHighLimit, saLimitAllow); + sleRippleState->setFieldAmount( + !bHigh ? sfLowLimit : sfHighLimit, saLimitAllow); - saLowLimit = !bHigh ? saLimitAllow : sleRippleState->getFieldAmount (sfLowLimit); - saHighLimit = bHigh ? saLimitAllow : sleRippleState->getFieldAmount (sfHighLimit); + saLowLimit = + !bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfLowLimit); + saHighLimit = + bHigh ? saLimitAllow : sleRippleState->getFieldAmount(sfHighLimit); // // Quality in @@ -262,31 +255,41 @@ SetTrust::doApply () { // Not setting. Just get it. - uLowQualityIn = sleRippleState->getFieldU32 (sfLowQualityIn); - uHighQualityIn = sleRippleState->getFieldU32 (sfHighQualityIn); + uLowQualityIn = sleRippleState->getFieldU32(sfLowQualityIn); + uHighQualityIn = sleRippleState->getFieldU32(sfHighQualityIn); } else if (uQualityIn) { // Setting. - sleRippleState->setFieldU32 (!bHigh ? sfLowQualityIn : sfHighQualityIn, uQualityIn); + sleRippleState->setFieldU32( + !bHigh ? sfLowQualityIn : sfHighQualityIn, uQualityIn); - uLowQualityIn = !bHigh ? uQualityIn : sleRippleState->getFieldU32 (sfLowQualityIn); - uHighQualityIn = bHigh ? uQualityIn : sleRippleState->getFieldU32 (sfHighQualityIn); + uLowQualityIn = !bHigh + ? uQualityIn + : sleRippleState->getFieldU32(sfLowQualityIn); + uHighQualityIn = bHigh + ? uQualityIn + : sleRippleState->getFieldU32(sfHighQualityIn); } else { // Clearing. - sleRippleState->makeFieldAbsent (!bHigh ? sfLowQualityIn : sfHighQualityIn); + sleRippleState->makeFieldAbsent( + !bHigh ? sfLowQualityIn : sfHighQualityIn); - uLowQualityIn = !bHigh ? 0 : sleRippleState->getFieldU32 (sfLowQualityIn); - uHighQualityIn = bHigh ? 0 : sleRippleState->getFieldU32 (sfHighQualityIn); + uLowQualityIn = + !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityIn); + uHighQualityIn = + bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityIn); } - if (QUALITY_ONE == uLowQualityIn) uLowQualityIn = 0; + if (QUALITY_ONE == uLowQualityIn) + uLowQualityIn = 0; - if (QUALITY_ONE == uHighQualityIn) uHighQualityIn = 0; + if (QUALITY_ONE == uHighQualityIn) + uHighQualityIn = 0; // // Quality out @@ -296,30 +299,38 @@ SetTrust::doApply () { // Not setting. Just get it. - uLowQualityOut = sleRippleState->getFieldU32 (sfLowQualityOut); - uHighQualityOut = sleRippleState->getFieldU32 (sfHighQualityOut); + uLowQualityOut = sleRippleState->getFieldU32(sfLowQualityOut); + uHighQualityOut = sleRippleState->getFieldU32(sfHighQualityOut); } else if (uQualityOut) { // Setting. - sleRippleState->setFieldU32 (!bHigh ? sfLowQualityOut : sfHighQualityOut, uQualityOut); + sleRippleState->setFieldU32( + !bHigh ? sfLowQualityOut : sfHighQualityOut, uQualityOut); - uLowQualityOut = !bHigh ? uQualityOut : sleRippleState->getFieldU32 (sfLowQualityOut); - uHighQualityOut = bHigh ? uQualityOut : sleRippleState->getFieldU32 (sfHighQualityOut); + uLowQualityOut = !bHigh + ? uQualityOut + : sleRippleState->getFieldU32(sfLowQualityOut); + uHighQualityOut = bHigh + ? uQualityOut + : sleRippleState->getFieldU32(sfHighQualityOut); } else { // Clearing. - sleRippleState->makeFieldAbsent (!bHigh ? sfLowQualityOut : sfHighQualityOut); + sleRippleState->makeFieldAbsent( + !bHigh ? sfLowQualityOut : sfHighQualityOut); - uLowQualityOut = !bHigh ? 0 : sleRippleState->getFieldU32 (sfLowQualityOut); - uHighQualityOut = bHigh ? 0 : sleRippleState->getFieldU32 (sfHighQualityOut); + uLowQualityOut = + !bHigh ? 0 : sleRippleState->getFieldU32(sfLowQualityOut); + uHighQualityOut = + bHigh ? 0 : sleRippleState->getFieldU32(sfHighQualityOut); } - std::uint32_t const uFlagsIn (sleRippleState->getFieldU32 (sfFlags)); - std::uint32_t uFlagsOut (uFlagsIn); + std::uint32_t const uFlagsIn(sleRippleState->getFieldU32(sfFlags)); + std::uint32_t uFlagsOut(uFlagsIn); if (bSetNoRipple && !bClearNoRipple) { @@ -335,40 +346,43 @@ SetTrust::doApply () uFlagsOut &= ~(bHigh ? lsfHighNoRipple : lsfLowNoRipple); } - if (bSetFreeze && !bClearFreeze && !sle->isFlag (lsfNoFreeze)) + if (bSetFreeze && !bClearFreeze && !sle->isFlag(lsfNoFreeze)) { - uFlagsOut |= (bHigh ? lsfHighFreeze : lsfLowFreeze); + uFlagsOut |= (bHigh ? lsfHighFreeze : lsfLowFreeze); } else if (bClearFreeze && !bSetFreeze) { - uFlagsOut &= ~(bHigh ? lsfHighFreeze : lsfLowFreeze); + uFlagsOut &= ~(bHigh ? lsfHighFreeze : lsfLowFreeze); } - if (QUALITY_ONE == uLowQualityOut) uLowQualityOut = 0; + if (QUALITY_ONE == uLowQualityOut) + uLowQualityOut = 0; - if (QUALITY_ONE == uHighQualityOut) uHighQualityOut = 0; + if (QUALITY_ONE == uHighQualityOut) + uHighQualityOut = 0; - bool const bLowDefRipple = sleLowAccount->getFlags() & lsfDefaultRipple; - bool const bHighDefRipple = sleHighAccount->getFlags() & lsfDefaultRipple; + bool const bLowDefRipple = sleLowAccount->getFlags() & lsfDefaultRipple; + bool const bHighDefRipple = + sleHighAccount->getFlags() & lsfDefaultRipple; - bool const bLowReserveSet = uLowQualityIn || uLowQualityOut || - ((uFlagsOut & lsfLowNoRipple) == 0) != bLowDefRipple || - (uFlagsOut & lsfLowFreeze) || - saLowLimit || saLowBalance > beast::zero; - bool const bLowReserveClear = !bLowReserveSet; + bool const bLowReserveSet = uLowQualityIn || uLowQualityOut || + ((uFlagsOut & lsfLowNoRipple) == 0) != bLowDefRipple || + (uFlagsOut & lsfLowFreeze) || saLowLimit || + saLowBalance > beast::zero; + bool const bLowReserveClear = !bLowReserveSet; - bool const bHighReserveSet = uHighQualityIn || uHighQualityOut || - ((uFlagsOut & lsfHighNoRipple) == 0) != bHighDefRipple || - (uFlagsOut & lsfHighFreeze) || - saHighLimit || saHighBalance > beast::zero; - bool const bHighReserveClear = !bHighReserveSet; + bool const bHighReserveSet = uHighQualityIn || uHighQualityOut || + ((uFlagsOut & lsfHighNoRipple) == 0) != bHighDefRipple || + (uFlagsOut & lsfHighFreeze) || saHighLimit || + saHighBalance > beast::zero; + bool const bHighReserveClear = !bHighReserveSet; - bool const bDefault = bLowReserveClear && bHighReserveClear; + bool const bDefault = bLowReserveClear && bHighReserveClear; - bool const bLowReserved = (uFlagsIn & lsfLowReserve); - bool const bHighReserved = (uFlagsIn & lsfHighReserve); + bool const bLowReserved = (uFlagsIn & lsfLowReserve); + bool const bHighReserved = (uFlagsIn & lsfHighReserve); - bool bReserveIncrease = false; + bool bReserveIncrease = false; if (bSetAuth) { @@ -378,8 +392,7 @@ SetTrust::doApply () if (bLowReserveSet && !bLowReserved) { // Set reserve for low account. - adjustOwnerCount(view(), - sleLowAccount, 1, viewJ); + adjustOwnerCount(view(), sleLowAccount, 1, viewJ); uFlagsOut |= lsfLowReserve; if (!bHigh) @@ -389,45 +402,42 @@ SetTrust::doApply () if (bLowReserveClear && bLowReserved) { // Clear reserve for low account. - adjustOwnerCount(view(), - sleLowAccount, -1, viewJ); + adjustOwnerCount(view(), sleLowAccount, -1, viewJ); uFlagsOut &= ~lsfLowReserve; } if (bHighReserveSet && !bHighReserved) { // Set reserve for high account. - adjustOwnerCount(view(), - sleHighAccount, 1, viewJ); + adjustOwnerCount(view(), sleHighAccount, 1, viewJ); uFlagsOut |= lsfHighReserve; if (bHigh) - bReserveIncrease = true; + bReserveIncrease = true; } if (bHighReserveClear && bHighReserved) { // Clear reserve for high account. - adjustOwnerCount(view(), - sleHighAccount, -1, viewJ); + adjustOwnerCount(view(), sleHighAccount, -1, viewJ); uFlagsOut &= ~lsfHighReserve; } if (uFlagsIn != uFlagsOut) - sleRippleState->setFieldU32 (sfFlags, uFlagsOut); + sleRippleState->setFieldU32(sfFlags, uFlagsOut); if (bDefault || badCurrency() == currency) { // Delete. - terResult = trustDelete (view(), - sleRippleState, uLowAccountID, uHighAccountID, viewJ); + terResult = trustDelete( + view(), sleRippleState, uLowAccountID, uHighAccountID, viewJ); } // Reserve is not scaled by load. else if (bReserveIncrease && mPriorBalance < reserveCreate) { - JLOG(j_.trace()) << - "Delay transaction: Insufficent reserve to add trust line."; + JLOG(j_.trace()) + << "Delay transaction: Insufficent reserve to add trust line."; // Another transaction could provide XRP to the account and then // this transaction would succeed. @@ -435,43 +445,46 @@ SetTrust::doApply () } else { - view().update (sleRippleState); + view().update(sleRippleState); JLOG(j_.trace()) << "Modify ripple line"; } } // Line does not exist. - else if (! saLimitAmount && // Setting default limit. - (! bQualityIn || ! uQualityIn) && // Not setting quality in or setting default quality in. - (! bQualityOut || ! uQualityOut) && // Not setting quality out or setting default quality out. - (! bSetAuth)) + else if ( + !saLimitAmount && // Setting default limit. + (!bQualityIn || !uQualityIn) && // Not setting quality in or setting + // default quality in. + (!bQualityOut || !uQualityOut) && // Not setting quality out or setting + // default quality out. + (!bSetAuth)) { - JLOG(j_.trace()) << - "Redundant: Setting non-existent ripple line to defaults."; + JLOG(j_.trace()) + << "Redundant: Setting non-existent ripple line to defaults."; return tecNO_LINE_REDUNDANT; } - else if (mPriorBalance < reserveCreate) // Reserve is not scaled by load. + else if (mPriorBalance < reserveCreate) // Reserve is not scaled by load. { - JLOG(j_.trace()) << - "Delay transaction: Line does not exist. Insufficent reserve to create line."; + JLOG(j_.trace()) << "Delay transaction: Line does not exist. " + "Insufficent reserve to create line."; - // Another transaction could create the account and then this transaction would succeed. + // Another transaction could create the account and then this + // transaction would succeed. terResult = tecNO_LINE_INSUF_RESERVE; } else { // Zero balance in currency. - STAmount saBalance ({currency, noAccount()}); + STAmount saBalance({currency, noAccount()}); - uint256 index (getRippleStateIndex ( - account_, uDstAccountID, currency)); + uint256 index(getRippleStateIndex(account_, uDstAccountID, currency)); - JLOG(j_.trace()) << - "doTrustSet: Creating ripple line: " << - to_string (index); + JLOG(j_.trace()) << "doTrustSet: Creating ripple line: " + << to_string(index); // Create a new ripple line. - terResult = trustCreate (view(), + terResult = trustCreate( + view(), bHigh, account_, uDstAccountID, @@ -481,12 +494,13 @@ SetTrust::doApply () bSetNoRipple && !bClearNoRipple, bSetFreeze && !bClearFreeze, saBalance, - saLimitAllow, // Limit for who is being charged. + saLimitAllow, // Limit for who is being charged. uQualityIn, - uQualityOut, viewJ); + uQualityOut, + viewJ); } return terResult; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/SetTrust.h b/src/ripple/app/tx/impl/SetTrust.h index 5ef0a0a4bc..df5c6552ce 100644 --- a/src/ripple/app/tx/impl/SetTrust.h +++ b/src/ripple/app/tx/impl/SetTrust.h @@ -20,35 +20,31 @@ #ifndef RIPPLE_TX_SETTRUST_H_INCLUDED #define RIPPLE_TX_SETTRUST_H_INCLUDED -#include #include #include #include +#include #include namespace ripple { -class SetTrust - : public Transactor +class SetTrust : public Transactor { public: - explicit SetTrust (ApplyContext& ctx) - : Transactor(ctx) + explicit SetTrust(ApplyContext& ctx) : Transactor(ctx) { } - static - NotTEC - preflight (PreflightContext const& ctx); + static NotTEC + preflight(PreflightContext const& ctx); - static - TER + static TER preclaim(PreclaimContext const& ctx); - TER doApply () override; + TER + doApply() override; }; -} // ripple +} // namespace ripple #endif - diff --git a/src/ripple/app/tx/impl/SignerEntries.cpp b/src/ripple/app/tx/impl/SignerEntries.cpp index e15634a9f1..b914f6ad04 100644 --- a/src/ripple/app/tx/impl/SignerEntries.cpp +++ b/src/ripple/app/tx/impl/SignerEntries.cpp @@ -19,49 +19,51 @@ #include #include -#include #include +#include #include namespace ripple { std::pair, NotTEC> -SignerEntries::deserialize ( - STObject const& obj, beast::Journal journal, std::string const& annotation) +SignerEntries::deserialize( + STObject const& obj, + beast::Journal journal, + std::string const& annotation) { std::pair, NotTEC> s; - if (!obj.isFieldPresent (sfSignerEntries)) + if (!obj.isFieldPresent(sfSignerEntries)) { - JLOG(journal.trace()) << - "Malformed " << annotation << ": Need signer entry array."; + JLOG(journal.trace()) + << "Malformed " << annotation << ": Need signer entry array."; s.second = temMALFORMED; return s; } auto& accountVec = s.first; - accountVec.reserve (STTx::maxMultiSigners); + accountVec.reserve(STTx::maxMultiSigners); - STArray const& sEntries (obj.getFieldArray (sfSignerEntries)); + STArray const& sEntries(obj.getFieldArray(sfSignerEntries)); for (STObject const& sEntry : sEntries) { // Validate the SignerEntry. - if (sEntry.getFName () != sfSignerEntry) + if (sEntry.getFName() != sfSignerEntry) { - JLOG(journal.trace()) << - "Malformed " << annotation << ": Expected SignerEntry."; + JLOG(journal.trace()) + << "Malformed " << annotation << ": Expected SignerEntry."; s.second = temMALFORMED; return s; } // Extract SignerEntry fields. - AccountID const account = sEntry.getAccountID (sfAccount); - std::uint16_t const weight = sEntry.getFieldU16 (sfSignerWeight); - accountVec.emplace_back (account, weight); + AccountID const account = sEntry.getAccountID(sfAccount); + std::uint16_t const weight = sEntry.getFieldU16(sfSignerWeight); + accountVec.emplace_back(account, weight); } s.second = tesSUCCESS; return s; } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/SignerEntries.h b/src/ripple/app/tx/impl/SignerEntries.h index fab916039c..835b826bd5 100644 --- a/src/ripple/app/tx/impl/SignerEntries.h +++ b/src/ripple/app/tx/impl/SignerEntries.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_TX_IMPL_SIGNER_ENTRIES_H_INCLUDED #define RIPPLE_TX_IMPL_SIGNER_ENTRIES_H_INCLUDED -#include // NotTEC -#include // STTx::maxMultiSigners -#include // AccountID -#include // temMALFORMED -#include // beast::Journal +#include // NotTEC +#include // beast::Journal +#include // STTx::maxMultiSigners +#include // temMALFORMED +#include // AccountID namespace ripple { @@ -42,32 +42,33 @@ public: AccountID account; std::uint16_t weight; - SignerEntry (AccountID const& inAccount, std::uint16_t inWeight) - : account (inAccount) - , weight (inWeight) - { } + SignerEntry(AccountID const& inAccount, std::uint16_t inWeight) + : account(inAccount), weight(inWeight) + { + } // For sorting to look for duplicate accounts - friend bool operator< (SignerEntry const& lhs, SignerEntry const& rhs) + friend bool + operator<(SignerEntry const& lhs, SignerEntry const& rhs) { return lhs.account < rhs.account; } - friend bool operator== (SignerEntry const& lhs, SignerEntry const& rhs) + friend bool + operator==(SignerEntry const& lhs, SignerEntry const& rhs) { return lhs.account == rhs.account; } }; // Deserialize a SignerEntries array from the network or from the ledger. - static - std::pair, NotTEC> - deserialize ( + static std::pair, NotTEC> + deserialize( STObject const& obj, beast::Journal journal, std::string const& annotation); }; -} // ripple +} // namespace ripple -#endif // RIPPLE_TX_IMPL_SIGNER_ENTRIES_H_INCLUDED +#endif // RIPPLE_TX_IMPL_SIGNER_ENTRIES_H_INCLUDED diff --git a/src/ripple/app/tx/impl/Taker.cpp b/src/ripple/app/tx/impl/Taker.cpp index 1d9fb9ab61..f463ce4119 100644 --- a/src/ripple/app/tx/impl/Taker.cpp +++ b/src/ripple/app/tx/impl/Taker.cpp @@ -18,54 +18,60 @@ //============================================================================== #include -#include #include +#include namespace ripple { -static -std::string -format_amount (STAmount const& amount) +static std::string +format_amount(STAmount const& amount) { - std::string txt = amount.getText (); + std::string txt = amount.getText(); txt += "/"; - txt += to_string (amount.issue().currency); + txt += to_string(amount.issue().currency); return txt; } -BasicTaker::BasicTaker ( - CrossType cross_type, AccountID const& account, Amounts const& amount, - Quality const& quality, std::uint32_t flags, Rate const& rate_in, - Rate const& rate_out, beast::Journal journal) - : account_ (account) - , quality_ (quality) - , threshold_ (quality_) - , sell_ (flags & tfSell) - , original_ (amount) - , remaining_ (amount) - , issue_in_ (remaining_.in.issue ()) - , issue_out_ (remaining_.out.issue ()) - , m_rate_in (rate_in) - , m_rate_out (rate_out) - , cross_type_ (cross_type) - , journal_ (journal) +BasicTaker::BasicTaker( + CrossType cross_type, + AccountID const& account, + Amounts const& amount, + Quality const& quality, + std::uint32_t flags, + Rate const& rate_in, + Rate const& rate_out, + beast::Journal journal) + : account_(account) + , quality_(quality) + , threshold_(quality_) + , sell_(flags & tfSell) + , original_(amount) + , remaining_(amount) + , issue_in_(remaining_.in.issue()) + , issue_out_(remaining_.out.issue()) + , m_rate_in(rate_in) + , m_rate_out(rate_out) + , cross_type_(cross_type) + , journal_(journal) { - assert (remaining_.in > beast::zero); - assert (remaining_.out > beast::zero); + assert(remaining_.in > beast::zero); + assert(remaining_.out > beast::zero); - assert (m_rate_in.value != 0); - assert (m_rate_out.value != 0); + assert(m_rate_in.value != 0); + assert(m_rate_out.value != 0); // If we are dealing with a particular flavor, make sure that it's the // flavor we expect: - assert (cross_type != CrossType::XrpToIou || - (isXRP (issue_in ()) && !isXRP (issue_out ()))); + assert( + cross_type != CrossType::XrpToIou || + (isXRP(issue_in()) && !isXRP(issue_out()))); - assert (cross_type != CrossType::IouToXrp || - (!isXRP (issue_in ()) && isXRP (issue_out ()))); + assert( + cross_type != CrossType::IouToXrp || + (!isXRP(issue_in()) && isXRP(issue_out()))); // And make sure we're not crossing XRP for XRP - assert (!isXRP (issue_in ()) || !isXRP (issue_out ())); + assert(!isXRP(issue_in()) || !isXRP(issue_out())); // If this is a passive order, we adjust the quality so as to prevent offers // at the same quality level from being consumed. @@ -74,16 +80,16 @@ BasicTaker::BasicTaker ( } Rate -BasicTaker::effective_rate ( - Rate const& rate, Issue const &issue, - AccountID const& from, AccountID const& to) +BasicTaker::effective_rate( + Rate const& rate, + Issue const& issue, + AccountID const& from, + AccountID const& to) { // If there's a transfer rate, the issuer is not involved // and the sender isn't the same as the recipient, return // the actual transfer rate. - if (rate != parityRate && - from != to && - from != issue.account && + if (rate != parityRate && from != to && from != issue.account && to != issue.account) { return rate; @@ -93,9 +99,9 @@ BasicTaker::effective_rate ( } bool -BasicTaker::unfunded () const +BasicTaker::unfunded() const { - if (get_funds (account(), remaining_.in) > beast::zero) + if (get_funds(account(), remaining_.in) > beast::zero) return false; JLOG(journal_.debug()) << "Unfunded: taker is out of funds."; @@ -103,12 +109,13 @@ BasicTaker::unfunded () const } bool -BasicTaker::done () const +BasicTaker::done() const { // We are done if we have consumed all the input currency if (remaining_.in <= beast::zero) { - JLOG(journal_.debug()) << "Done: all the input currency has been consumed."; + JLOG(journal_.debug()) + << "Done: all the input currency has been consumed."; return true; } @@ -121,7 +128,7 @@ BasicTaker::done () const } // We are done if the taker is out of funds - if (unfunded ()) + if (unfunded()) { JLOG(journal_.debug()) << "Done: taker out of funds."; return true; @@ -131,59 +138,58 @@ BasicTaker::done () const } Amounts -BasicTaker::remaining_offer () const +BasicTaker::remaining_offer() const { // If the taker is done, then there's no offer to place. - if (done ()) - return Amounts (remaining_.in.zeroed(), remaining_.out.zeroed()); + if (done()) + return Amounts(remaining_.in.zeroed(), remaining_.out.zeroed()); // Avoid math altogether if we didn't cross. if (original_ == remaining_) - return original_; + return original_; if (sell_) { - assert (remaining_.in > beast::zero); + assert(remaining_.in > beast::zero); // We scale the output based on the remaining input: - return Amounts (remaining_.in, divRound ( - remaining_.in, quality_.rate (), issue_out_, true)); + return Amounts( + remaining_.in, + divRound(remaining_.in, quality_.rate(), issue_out_, true)); } - assert (remaining_.out > beast::zero); + assert(remaining_.out > beast::zero); // We scale the input based on the remaining output: - return Amounts (mulRound ( - remaining_.out, quality_.rate (), issue_in_, true), + return Amounts( + mulRound(remaining_.out, quality_.rate(), issue_in_, true), remaining_.out); } Amounts const& -BasicTaker::original_offer () const +BasicTaker::original_offer() const { return original_; } // TODO: the presence of 'output' is an artifact caused by the fact that // Amounts carry issue information which should be decoupled. -static -STAmount -qual_div (STAmount const& amount, Quality const& quality, STAmount const& output) +static STAmount +qual_div(STAmount const& amount, Quality const& quality, STAmount const& output) { - auto result = divide (amount, quality.rate (), output.issue ()); - return std::min (result, output); + auto result = divide(amount, quality.rate(), output.issue()); + return std::min(result, output); } -static -STAmount -qual_mul (STAmount const& amount, Quality const& quality, STAmount const& output) +static STAmount +qual_mul(STAmount const& amount, Quality const& quality, STAmount const& output) { - auto result = multiply (amount, quality.rate (), output.issue ()); - return std::min (result, output); + auto result = multiply(amount, quality.rate(), output.issue()); + return std::min(result, output); } void -BasicTaker::log_flow (char const* description, Flow const& flow) +BasicTaker::log_flow(char const* description, Flow const& flow) { auto stream = journal_.debug(); if (!stream) @@ -191,56 +197,58 @@ BasicTaker::log_flow (char const* description, Flow const& flow) stream << description; - if (isXRP (issue_in ())) - stream << " order in: " << format_amount (flow.order.in); + if (isXRP(issue_in())) + stream << " order in: " << format_amount(flow.order.in); else - stream << " order in: " << format_amount (flow.order.in) << - " (issuer: " << format_amount (flow.issuers.in) << ")"; + stream << " order in: " << format_amount(flow.order.in) + << " (issuer: " << format_amount(flow.issuers.in) << ")"; - if (isXRP (issue_out ())) - stream << " order out: " << format_amount (flow.order.out); + if (isXRP(issue_out())) + stream << " order out: " << format_amount(flow.order.out); else - stream << " order out: " << format_amount (flow.order.out) << - " (issuer: " << format_amount (flow.issuers.out) << ")"; + stream << " order out: " << format_amount(flow.order.out) + << " (issuer: " << format_amount(flow.issuers.out) << ")"; } BasicTaker::Flow -BasicTaker::flow_xrp_to_iou ( - Amounts const& order, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, +BasicTaker::flow_xrp_to_iou( + Amounts const& order, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, Rate const& rate_out) { Flow f; f.order = order; - f.issuers.out = multiply (f.order.out, rate_out); + f.issuers.out = multiply(f.order.out, rate_out); - log_flow ("flow_xrp_to_iou", f); + log_flow("flow_xrp_to_iou", f); // Clamp on owner balance if (owner_funds < f.issuers.out) { f.issuers.out = owner_funds; - f.order.out = divide (f.issuers.out, rate_out); - f.order.in = qual_mul (f.order.out, quality, f.order.in); - log_flow ("(clamped on owner balance)", f); + f.order.out = divide(f.issuers.out, rate_out); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + log_flow("(clamped on owner balance)", f); } // Clamp if taker wants to limit the output if (!sell_ && remaining_.out < f.order.out) { f.order.out = remaining_.out; - f.order.in = qual_mul (f.order.out, quality, f.order.in); - f.issuers.out = multiply (f.order.out, rate_out); - log_flow ("(clamped on taker output)", f); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + f.issuers.out = multiply(f.order.out, rate_out); + log_flow("(clamped on taker output)", f); } // Clamp on the taker's funds if (taker_funds < f.order.in) { f.order.in = taker_funds; - f.order.out = qual_div (f.order.in, quality, f.order.out); - f.issuers.out = multiply (f.order.out, rate_out); - log_flow ("(clamped on taker funds)", f); + f.order.out = qual_div(f.order.in, quality, f.order.out); + f.issuers.out = multiply(f.order.out, rate_out); + log_flow("(clamped on taker funds)", f); } // Clamp on remaining offer if we are not handling the second leg @@ -248,33 +256,35 @@ BasicTaker::flow_xrp_to_iou ( if (cross_type_ == CrossType::XrpToIou && (remaining_.in < f.order.in)) { f.order.in = remaining_.in; - f.order.out = qual_div (f.order.in, quality, f.order.out); - f.issuers.out = multiply (f.order.out, rate_out); - log_flow ("(clamped on taker input)", f); + f.order.out = qual_div(f.order.in, quality, f.order.out); + f.issuers.out = multiply(f.order.out, rate_out); + log_flow("(clamped on taker input)", f); } return f; } BasicTaker::Flow -BasicTaker::flow_iou_to_xrp ( - Amounts const& order, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, +BasicTaker::flow_iou_to_xrp( + Amounts const& order, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, Rate const& rate_in) { Flow f; f.order = order; - f.issuers.in = multiply (f.order.in, rate_in); + f.issuers.in = multiply(f.order.in, rate_in); - log_flow ("flow_iou_to_xrp", f); + log_flow("flow_iou_to_xrp", f); // Clamp on owner's funds if (owner_funds < f.order.out) { f.order.out = owner_funds; - f.order.in = qual_mul (f.order.out, quality, f.order.in); - f.issuers.in = multiply (f.order.in, rate_in); - log_flow ("(clamped on owner funds)", f); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + f.issuers.in = multiply(f.order.in, rate_in); + log_flow("(clamped on owner funds)", f); } // Clamp if taker wants to limit the output and we are not the @@ -284,9 +294,9 @@ BasicTaker::flow_iou_to_xrp ( if (remaining_.out < f.order.out) { f.order.out = remaining_.out; - f.order.in = qual_mul (f.order.out, quality, f.order.in); - f.issuers.in = multiply (f.order.in, rate_in); - log_flow ("(clamped on taker output)", f); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + f.issuers.in = multiply(f.order.in, rate_in); + log_flow("(clamped on taker output)", f); } } @@ -294,74 +304,77 @@ BasicTaker::flow_iou_to_xrp ( if (remaining_.in < f.order.in) { f.order.in = remaining_.in; - f.issuers.in = multiply (f.order.in, rate_in); - f.order.out = qual_div (f.order.in, quality, f.order.out); - log_flow ("(clamped on taker input)", f); + f.issuers.in = multiply(f.order.in, rate_in); + f.order.out = qual_div(f.order.in, quality, f.order.out); + log_flow("(clamped on taker input)", f); } // Clamp on the taker's input balance if (taker_funds < f.issuers.in) { f.issuers.in = taker_funds; - f.order.in = divide (f.issuers.in, rate_in); - f.order.out = qual_div (f.order.in, quality, f.order.out); - log_flow ("(clamped on taker funds)", f); + f.order.in = divide(f.issuers.in, rate_in); + f.order.out = qual_div(f.order.in, quality, f.order.out); + log_flow("(clamped on taker funds)", f); } return f; } BasicTaker::Flow -BasicTaker::flow_iou_to_iou ( - Amounts const& order, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, - Rate const& rate_in, Rate const& rate_out) +BasicTaker::flow_iou_to_iou( + Amounts const& order, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, + Rate const& rate_in, + Rate const& rate_out) { Flow f; f.order = order; - f.issuers.in = multiply (f.order.in, rate_in); - f.issuers.out = multiply (f.order.out, rate_out); + f.issuers.in = multiply(f.order.in, rate_in); + f.issuers.out = multiply(f.order.out, rate_out); - log_flow ("flow_iou_to_iou", f); + log_flow("flow_iou_to_iou", f); // Clamp on owner balance if (owner_funds < f.issuers.out) { f.issuers.out = owner_funds; - f.order.out = divide (f.issuers.out, rate_out); - f.order.in = qual_mul (f.order.out, quality, f.order.in); - f.issuers.in = multiply (f.order.in, rate_in); - log_flow ("(clamped on owner funds)", f); + f.order.out = divide(f.issuers.out, rate_out); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + f.issuers.in = multiply(f.order.in, rate_in); + log_flow("(clamped on owner funds)", f); } // Clamp on taker's offer if (!sell_ && remaining_.out < f.order.out) { f.order.out = remaining_.out; - f.order.in = qual_mul (f.order.out, quality, f.order.in); - f.issuers.out = multiply (f.order.out, rate_out); - f.issuers.in = multiply (f.order.in, rate_in); - log_flow ("(clamped on taker output)", f); + f.order.in = qual_mul(f.order.out, quality, f.order.in); + f.issuers.out = multiply(f.order.out, rate_out); + f.issuers.in = multiply(f.order.in, rate_in); + log_flow("(clamped on taker output)", f); } // Clamp on the taker's input offer if (remaining_.in < f.order.in) { f.order.in = remaining_.in; - f.issuers.in = multiply (f.order.in, rate_in); - f.order.out = qual_div (f.order.in, quality, f.order.out); - f.issuers.out = multiply (f.order.out, rate_out); - log_flow ("(clamped on taker input)", f); + f.issuers.in = multiply(f.order.in, rate_in); + f.order.out = qual_div(f.order.in, quality, f.order.out); + f.issuers.out = multiply(f.order.out, rate_out); + log_flow("(clamped on taker input)", f); } // Clamp on the taker's input balance if (taker_funds < f.issuers.in) { f.issuers.in = taker_funds; - f.order.in = divide (f.issuers.in, rate_in); - f.order.out = qual_div (f.order.in, quality, f.order.out); - f.issuers.out = multiply (f.order.out, rate_out); - log_flow ("(clamped on taker funds)", f); + f.order.in = divide(f.issuers.in, rate_in); + f.order.out = qual_div(f.order.in, quality, f.order.out); + f.issuers.out = multiply(f.order.out, rate_out); + log_flow("(clamped on taker funds)", f); } return f; @@ -369,69 +382,86 @@ BasicTaker::flow_iou_to_iou ( // Calculates the direct flow through the specified offer BasicTaker::Flow -BasicTaker::do_cross (Amounts offer, Quality quality, AccountID const& owner) +BasicTaker::do_cross(Amounts offer, Quality quality, AccountID const& owner) { - auto const owner_funds = get_funds (owner, offer.out); - auto const taker_funds = get_funds (account (), offer.in); + auto const owner_funds = get_funds(owner, offer.out); + auto const taker_funds = get_funds(account(), offer.in); Flow result; if (cross_type_ == CrossType::XrpToIou) { - result = flow_xrp_to_iou (offer, quality, owner_funds, taker_funds, - out_rate (owner, account ())); + result = flow_xrp_to_iou( + offer, + quality, + owner_funds, + taker_funds, + out_rate(owner, account())); } else if (cross_type_ == CrossType::IouToXrp) { - result = flow_iou_to_xrp (offer, quality, owner_funds, taker_funds, - in_rate (owner, account ())); + result = flow_iou_to_xrp( + offer, + quality, + owner_funds, + taker_funds, + in_rate(owner, account())); } else { - result = flow_iou_to_iou (offer, quality, owner_funds, taker_funds, - in_rate (owner, account ()), out_rate (owner, account ())); + result = flow_iou_to_iou( + offer, + quality, + owner_funds, + taker_funds, + in_rate(owner, account()), + out_rate(owner, account())); } - if (!result.sanity_check ()) - Throw ("Computed flow fails sanity check."); + if (!result.sanity_check()) + Throw("Computed flow fails sanity check."); remaining_.out -= result.order.out; remaining_.in -= result.order.in; - assert (remaining_.in >= beast::zero); + assert(remaining_.in >= beast::zero); return result; } // Calculates the bridged flow through the specified offers std::pair -BasicTaker::do_cross ( - Amounts offer1, Quality quality1, AccountID const& owner1, - Amounts offer2, Quality quality2, AccountID const& owner2) +BasicTaker::do_cross( + Amounts offer1, + Quality quality1, + AccountID const& owner1, + Amounts offer2, + Quality quality2, + AccountID const& owner2) { - assert (!offer1.in.native ()); - assert (offer1.out.native ()); - assert (offer2.in.native ()); - assert (!offer2.out.native ()); + assert(!offer1.in.native()); + assert(offer1.out.native()); + assert(offer2.in.native()); + assert(!offer2.out.native()); // If the taker owns the first leg of the offer, then the taker's available // funds aren't the limiting factor for the input - the offer itself is. - auto leg1_in_funds = get_funds (account (), offer1.in); + auto leg1_in_funds = get_funds(account(), offer1.in); - if (account () == owner1) + if (account() == owner1) { JLOG(journal_.trace()) << "The taker owns the first leg of a bridge."; - leg1_in_funds = std::max (leg1_in_funds, offer1.in); + leg1_in_funds = std::max(leg1_in_funds, offer1.in); } // If the taker owns the second leg of the offer, then the taker's available // funds are not the limiting factor for the output - the offer itself is. - auto leg2_out_funds = get_funds (owner2, offer2.out); + auto leg2_out_funds = get_funds(owner2, offer2.out); - if (account () == owner2) + if (account() == owner2) { JLOG(journal_.trace()) << "The taker owns the second leg of a bridge."; - leg2_out_funds = std::max (leg2_out_funds, offer2.out); + leg2_out_funds = std::max(leg2_out_funds, offer2.out); } // The amount available to flow via XRP is the amount that the owner of the @@ -442,37 +472,39 @@ BasicTaker::do_cross ( // since all the owner is doing is taking out XRP of his left pocket // and putting it in his right pocket. In that case, we set the available // XRP to the largest of the two offers. - auto xrp_funds = get_funds (owner1, offer1.out); + auto xrp_funds = get_funds(owner1, offer1.out); if (owner1 == owner2) { - JLOG(journal_.trace()) << - "The bridge endpoints are owned by the same account."; - xrp_funds = std::max (offer1.out, offer2.in); + JLOG(journal_.trace()) + << "The bridge endpoints are owned by the same account."; + xrp_funds = std::max(offer1.out, offer2.in); } if (auto stream = journal_.debug()) { stream << "Available bridge funds:"; - stream << " leg1 in: " << format_amount (leg1_in_funds); - stream << " leg2 out: " << format_amount (leg2_out_funds); - stream << " xrp: " << format_amount (xrp_funds); + stream << " leg1 in: " << format_amount(leg1_in_funds); + stream << " leg2 out: " << format_amount(leg2_out_funds); + stream << " xrp: " << format_amount(xrp_funds); } - auto const leg1_rate = in_rate (owner1, account ()); - auto const leg2_rate = out_rate (owner2, account ()); + auto const leg1_rate = in_rate(owner1, account()); + auto const leg2_rate = out_rate(owner2, account()); // Attempt to determine the maximal flow that can be achieved across each // leg independent of the other. - auto flow1 = flow_iou_to_xrp (offer1, quality1, xrp_funds, leg1_in_funds, leg1_rate); + auto flow1 = + flow_iou_to_xrp(offer1, quality1, xrp_funds, leg1_in_funds, leg1_rate); - if (!flow1.sanity_check ()) - Throw ("Computed flow1 fails sanity check."); + if (!flow1.sanity_check()) + Throw("Computed flow1 fails sanity check."); - auto flow2 = flow_xrp_to_iou (offer2, quality2, leg2_out_funds, xrp_funds, leg2_rate); + auto flow2 = + flow_xrp_to_iou(offer2, quality2, leg2_out_funds, xrp_funds, leg2_rate); - if (!flow2.sanity_check ()) - Throw ("Computed flow2 fails sanity check."); + if (!flow2.sanity_check()) + Throw("Computed flow2 fails sanity check."); // We now have the maximal flows across each leg individually. We need to // equalize them, so that the amount of XRP that flows out of the first leg @@ -482,101 +514,111 @@ BasicTaker::do_cross ( { // Adjust the second leg of the offer down: flow2.order.in = flow1.order.out; - flow2.order.out = qual_div (flow2.order.in, quality2, flow2.order.out); - flow2.issuers.out = multiply (flow2.order.out, leg2_rate); - log_flow ("Balancing: adjusted second leg down", flow2); + flow2.order.out = qual_div(flow2.order.in, quality2, flow2.order.out); + flow2.issuers.out = multiply(flow2.order.out, leg2_rate); + log_flow("Balancing: adjusted second leg down", flow2); } else if (flow1.order.out > flow2.order.in) { // Adjust the first leg of the offer down: flow1.order.out = flow2.order.in; - flow1.order.in = qual_mul (flow1.order.out, quality1, flow1.order.in); - flow1.issuers.in = multiply (flow1.order.in, leg1_rate); - log_flow ("Balancing: adjusted first leg down", flow2); + flow1.order.in = qual_mul(flow1.order.out, quality1, flow1.order.in); + flow1.issuers.in = multiply(flow1.order.in, leg1_rate); + log_flow("Balancing: adjusted first leg down", flow2); } if (flow1.order.out != flow2.order.in) - Throw ("Bridged flow is out of balance."); + Throw("Bridged flow is out of balance."); remaining_.out -= flow2.order.out; remaining_.in -= flow1.order.in; - return std::make_pair (flow1, flow2); + return std::make_pair(flow1, flow2); } //============================================================================== -Taker::Taker (CrossType cross_type, ApplyView& view, - AccountID const& account, Amounts const& offer, - std::uint32_t flags, - beast::Journal journal) - : BasicTaker (cross_type, account, offer, Quality(offer), flags, - calculateRate(view, offer.in.getIssuer(), account), - calculateRate(view, offer.out.getIssuer(), account), journal) - , view_ (view) - , xrp_flow_ (0) - , direct_crossings_ (0) - , bridge_crossings_ (0) +Taker::Taker( + CrossType cross_type, + ApplyView& view, + AccountID const& account, + Amounts const& offer, + std::uint32_t flags, + beast::Journal journal) + : BasicTaker( + cross_type, + account, + offer, + Quality(offer), + flags, + calculateRate(view, offer.in.getIssuer(), account), + calculateRate(view, offer.out.getIssuer(), account), + journal) + , view_(view) + , xrp_flow_(0) + , direct_crossings_(0) + , bridge_crossings_(0) { - assert (issue_in () == offer.in.issue ()); - assert (issue_out () == offer.out.issue ()); + assert(issue_in() == offer.in.issue()); + assert(issue_out() == offer.out.issue()); if (auto stream = journal_.debug()) { - stream << "Crossing as: " << to_string (account); + stream << "Crossing as: " << to_string(account); - if (isXRP (issue_in ())) - stream << " Offer in: " << format_amount (offer.in); + if (isXRP(issue_in())) + stream << " Offer in: " << format_amount(offer.in); else - stream << " Offer in: " << format_amount (offer.in) << - " (issuer: " << issue_in ().account << ")"; + stream << " Offer in: " << format_amount(offer.in) + << " (issuer: " << issue_in().account << ")"; - if (isXRP (issue_out ())) - stream << " Offer out: " << format_amount (offer.out); + if (isXRP(issue_out())) + stream << " Offer out: " << format_amount(offer.out); else - stream << " Offer out: " << format_amount (offer.out) << - " (issuer: " << issue_out ().account << ")"; + stream << " Offer out: " << format_amount(offer.out) + << " (issuer: " << issue_out().account << ")"; - stream << - " Balance: " << format_amount (get_funds (account, offer.in)); + stream << " Balance: " + << format_amount(get_funds(account, offer.in)); } } void -Taker::consume_offer (Offer& offer, Amounts const& order) +Taker::consume_offer(Offer& offer, Amounts const& order) { if (order.in < beast::zero) - Throw ("flow with negative input."); + Throw("flow with negative input."); if (order.out < beast::zero) - Throw ("flow with negative output."); + Throw("flow with negative output."); JLOG(journal_.debug()) << "Consuming from offer " << offer; if (auto stream = journal_.trace()) { - auto const& available = offer.amount (); + auto const& available = offer.amount(); - stream << " in:" << format_amount (available.in); + stream << " in:" << format_amount(available.in); stream << " out:" << format_amount(available.out); } - offer.consume (view_, order); + offer.consume(view_, order); } STAmount -Taker::get_funds (AccountID const& account, STAmount const& amount) const +Taker::get_funds(AccountID const& account, STAmount const& amount) const { return accountFunds(view_, account, amount, fhZERO_IF_FROZEN, journal_); } -TER Taker::transferXRP ( +TER +Taker::transferXRP( AccountID const& from, AccountID const& to, STAmount const& amount) { - if (!isXRP (amount)) - Throw ("Using transferXRP with IOU"); + if (!isXRP(amount)) + Throw("Using transferXRP with IOU"); if (from == to) return tesSUCCESS; @@ -585,16 +627,17 @@ TER Taker::transferXRP ( if (amount == beast::zero) return tesSUCCESS; - return ripple::transferXRP (view_, from, to, amount, journal_); + return ripple::transferXRP(view_, from, to, amount, journal_); } -TER Taker::redeemIOU ( +TER +Taker::redeemIOU( AccountID const& account, STAmount const& amount, Issue const& issue) { - if (isXRP (amount)) - Throw ("Using redeemIOU with XRP"); + if (isXRP(amount)) + Throw("Using redeemIOU with XRP"); if (account == issue.account) return tesSUCCESS; @@ -605,24 +648,25 @@ TER Taker::redeemIOU ( // If we are trying to redeem some amount, then the account // must have a credit balance. - if (get_funds (account, amount) <= beast::zero) - Throw ("redeemIOU has no funds to redeem"); + if (get_funds(account, amount) <= beast::zero) + Throw("redeemIOU has no funds to redeem"); - auto ret = ripple::redeemIOU (view_, account, amount, issue, journal_); + auto ret = ripple::redeemIOU(view_, account, amount, issue, journal_); - if (get_funds (account, amount) < beast::zero) - Throw ("redeemIOU redeemed more funds than available"); + if (get_funds(account, amount) < beast::zero) + Throw("redeemIOU redeemed more funds than available"); return ret; } -TER Taker::issueIOU ( +TER +Taker::issueIOU( AccountID const& account, STAmount const& amount, Issue const& issue) { - if (isXRP (amount)) - Throw ("Using issueIOU with XRP"); + if (isXRP(amount)) + Throw("Using issueIOU with XRP"); if (account == issue.account) return tesSUCCESS; @@ -631,53 +675,57 @@ TER Taker::issueIOU ( if (amount == beast::zero) return tesSUCCESS; - return ripple::issueIOU (view_, account, amount, issue, journal_); + return ripple::issueIOU(view_, account, amount, issue, journal_); } // Performs funds transfers to fill the given offer and adjusts offer. TER -Taker::fill (BasicTaker::Flow const& flow, Offer& offer) +Taker::fill(BasicTaker::Flow const& flow, Offer& offer) { // adjust offer - consume_offer (offer, flow.order); + consume_offer(offer, flow.order); TER result = tesSUCCESS; - if (cross_type () != CrossType::XrpToIou) + if (cross_type() != CrossType::XrpToIou) { - assert (!isXRP (flow.order.in)); - - if(result == tesSUCCESS) - result = redeemIOU (account (), flow.issuers.in, flow.issuers.in.issue ()); + assert(!isXRP(flow.order.in)); if (result == tesSUCCESS) - result = issueIOU (offer.owner (), flow.order.in, flow.order.in.issue ()); + result = + redeemIOU(account(), flow.issuers.in, flow.issuers.in.issue()); + + if (result == tesSUCCESS) + result = + issueIOU(offer.owner(), flow.order.in, flow.order.in.issue()); } else { - assert (isXRP (flow.order.in)); + assert(isXRP(flow.order.in)); if (result == tesSUCCESS) - result = transferXRP (account (), offer.owner (), flow.order.in); + result = transferXRP(account(), offer.owner(), flow.order.in); } // Now send funds from the account whose offer we're taking - if (cross_type () != CrossType::IouToXrp) + if (cross_type() != CrossType::IouToXrp) { - assert (!isXRP (flow.order.out)); - - if(result == tesSUCCESS) - result = redeemIOU (offer.owner (), flow.issuers.out, flow.issuers.out.issue ()); + assert(!isXRP(flow.order.out)); if (result == tesSUCCESS) - result = issueIOU (account (), flow.order.out, flow.order.out.issue ()); + result = redeemIOU( + offer.owner(), flow.issuers.out, flow.issuers.out.issue()); + + if (result == tesSUCCESS) + result = + issueIOU(account(), flow.order.out, flow.order.out.issue()); } else { - assert (isXRP (flow.order.out)); + assert(isXRP(flow.order.out)); if (result == tesSUCCESS) - result = transferXRP (offer.owner (), account (), flow.order.out); + result = transferXRP(offer.owner(), account(), flow.order.out); } if (result == tesSUCCESS) @@ -688,38 +736,44 @@ Taker::fill (BasicTaker::Flow const& flow, Offer& offer) // Performs bridged funds transfers to fill the given offers and adjusts offers. TER -Taker::fill ( - BasicTaker::Flow const& flow1, Offer& leg1, - BasicTaker::Flow const& flow2, Offer& leg2) +Taker::fill( + BasicTaker::Flow const& flow1, + Offer& leg1, + BasicTaker::Flow const& flow2, + Offer& leg2) { // Adjust offers accordingly - consume_offer (leg1, flow1.order); - consume_offer (leg2, flow2.order); + consume_offer(leg1, flow1.order); + consume_offer(leg2, flow2.order); TER result = tesSUCCESS; // Taker to leg1: IOU - if (leg1.owner () != account ()) + if (leg1.owner() != account()) { if (result == tesSUCCESS) - result = redeemIOU (account (), flow1.issuers.in, flow1.issuers.in.issue ()); + result = redeemIOU( + account(), flow1.issuers.in, flow1.issuers.in.issue()); if (result == tesSUCCESS) - result = issueIOU (leg1.owner (), flow1.order.in, flow1.order.in.issue ()); + result = + issueIOU(leg1.owner(), flow1.order.in, flow1.order.in.issue()); } // leg1 to leg2: bridging over XRP if (result == tesSUCCESS) - result = transferXRP (leg1.owner (), leg2.owner (), flow1.order.out); + result = transferXRP(leg1.owner(), leg2.owner(), flow1.order.out); // leg2 to Taker: IOU - if (leg2.owner () != account ()) + if (leg2.owner() != account()) { if (result == tesSUCCESS) - result = redeemIOU (leg2.owner (), flow2.issuers.out, flow2.issuers.out.issue ()); + result = redeemIOU( + leg2.owner(), flow2.issuers.out, flow2.issuers.out.issue()); if (result == tesSUCCESS) - result = issueIOU (account (), flow2.order.out, flow2.order.out.issue ()); + result = + issueIOU(account(), flow2.order.out, flow2.order.out.issue()); } if (result == tesSUCCESS) @@ -732,43 +786,45 @@ Taker::fill ( } TER -Taker::cross (Offer& offer) +Taker::cross(Offer& offer) { // In direct crossings, at least one leg must not be XRP. - if (isXRP (offer.amount ().in) && isXRP (offer.amount ().out)) + if (isXRP(offer.amount().in) && isXRP(offer.amount().out)) return tefINTERNAL; - auto const amount = do_cross ( - offer.amount (), offer.quality (), offer.owner ()); + auto const amount = + do_cross(offer.amount(), offer.quality(), offer.owner()); - return fill (amount, offer); + return fill(amount, offer); } TER -Taker::cross (Offer& leg1, Offer& leg2) +Taker::cross(Offer& leg1, Offer& leg2) { // In bridged crossings, XRP must can't be the input to the first leg // or the output of the second leg. - if (isXRP (leg1.amount ().in) || isXRP (leg2.amount ().out)) + if (isXRP(leg1.amount().in) || isXRP(leg2.amount().out)) return tefINTERNAL; - auto ret = do_cross ( - leg1.amount (), leg1.quality (), leg1.owner (), - leg2.amount (), leg2.quality (), leg2.owner ()); + auto ret = do_cross( + leg1.amount(), + leg1.quality(), + leg1.owner(), + leg2.amount(), + leg2.quality(), + leg2.owner()); - return fill (ret.first, leg1, ret.second, leg2); + return fill(ret.first, leg1, ret.second, leg2); } Rate -Taker::calculateRate ( +Taker::calculateRate( ApplyView const& view, - AccountID const& issuer, - AccountID const& account) + AccountID const& issuer, + AccountID const& account) { - return isXRP (issuer) || (account == issuer) - ? parityRate - : transferRate (view, issuer); + return isXRP(issuer) || (account == issuer) ? parityRate + : transferRate(view, issuer); } -} // ripple - +} // namespace ripple diff --git a/src/ripple/app/tx/impl/Taker.h b/src/ripple/app/tx/impl/Taker.h index 944c388bff..4a516dccec 100644 --- a/src/ripple/app/tx/impl/Taker.h +++ b/src/ripple/app/tx/impl/Taker.h @@ -21,24 +21,19 @@ #define RIPPLE_APP_BOOK_TAKER_H_INCLUDED #include +#include #include #include #include #include #include #include -#include #include namespace ripple { /** The flavor of an offer crossing */ -enum class CrossType -{ - XrpToIou, - IouToXrp, - IouToIou -}; +enum class CrossType { XrpToIou, IouToXrp, IouToIou }; /** State for the active party during order book or payment operations. */ class BasicTaker @@ -78,71 +73,86 @@ protected: Amounts order; Amounts issuers; - bool sanity_check () const + bool + sanity_check() const { using beast::zero; - if (isXRP (order.in) && isXRP (order.out)) + if (isXRP(order.in) && isXRP(order.out)) return false; - return order.in >= zero && - order.out >= zero && - issuers.in >= zero && - issuers.out >= zero; + return order.in >= zero && order.out >= zero && + issuers.in >= zero && issuers.out >= zero; } }; private: void - log_flow (char const* description, Flow const& flow); + log_flow(char const* description, Flow const& flow); Flow - flow_xrp_to_iou (Amounts const& offer, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, + flow_xrp_to_iou( + Amounts const& offer, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, Rate const& rate_out); Flow - flow_iou_to_xrp (Amounts const& offer, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, + flow_iou_to_xrp( + Amounts const& offer, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, Rate const& rate_in); Flow - flow_iou_to_iou (Amounts const& offer, Quality quality, - STAmount const& owner_funds, STAmount const& taker_funds, - Rate const& rate_in, Rate const& rate_out); + flow_iou_to_iou( + Amounts const& offer, + Quality quality, + STAmount const& owner_funds, + STAmount const& taker_funds, + Rate const& rate_in, + Rate const& rate_out); // Calculates the transfer rate that we should use when calculating // flows for a particular issue between two accounts. - static - Rate - effective_rate (Rate const& rate, Issue const &issue, - AccountID const& from, AccountID const& to); + static Rate + effective_rate( + Rate const& rate, + Issue const& issue, + AccountID const& from, + AccountID const& to); // The transfer rate for the input currency between the given accounts Rate - in_rate (AccountID const& from, AccountID const& to) const + in_rate(AccountID const& from, AccountID const& to) const { - return effective_rate (m_rate_in, original_.in.issue (), from, to); + return effective_rate(m_rate_in, original_.in.issue(), from, to); } // The transfer rate for the output currency between the given accounts Rate - out_rate (AccountID const& from, AccountID const& to) const + out_rate(AccountID const& from, AccountID const& to) const { - return effective_rate (m_rate_out, original_.out.issue (), from, to); + return effective_rate(m_rate_out, original_.out.issue(), from, to); } public: - BasicTaker () = delete; - BasicTaker (BasicTaker const&) = delete; + BasicTaker() = delete; + BasicTaker(BasicTaker const&) = delete; - BasicTaker ( - CrossType cross_type, AccountID const& account, Amounts const& amount, - Quality const& quality, std::uint32_t flags, Rate const& rate_in, + BasicTaker( + CrossType cross_type, + AccountID const& account, + Amounts const& amount, + Quality const& quality, + std::uint32_t flags, + Rate const& rate_in, Rate const& rate_out, beast::Journal journal = beast::Journal{beast::Journal::getNullSink()}); - virtual ~BasicTaker () = default; + virtual ~BasicTaker() = default; /** Returns the amount remaining on the offer. This is the amount at which the offer should be placed. It may either @@ -151,112 +161,117 @@ public: It is always at the original offer quality (quality_) */ Amounts - remaining_offer () const; + remaining_offer() const; /** Returns the amount that the offer was originally placed at. */ Amounts const& - original_offer () const; + original_offer() const; /** Returns the account identifier of the taker. */ AccountID const& - account () const noexcept + account() const noexcept { return account_; } /** Returns `true` if the quality does not meet the taker's requirements. */ bool - reject (Quality const& quality) const noexcept + reject(Quality const& quality) const noexcept { return quality < threshold_; } /** Returns the type of crossing that is being performed */ CrossType - cross_type () const + cross_type() const { return cross_type_; } /** Returns the Issue associated with the input of the offer */ Issue const& - issue_in () const + issue_in() const { return issue_in_; } /** Returns the Issue associated with the output of the offer */ Issue const& - issue_out () const + issue_out() const { return issue_out_; } /** Returns `true` if the taker has run out of funds. */ bool - unfunded () const; + unfunded() const; /** Returns `true` if order crossing should not continue. Order processing is stopped if the taker's order quantities have been reached, or if the taker has run out of input funds. */ bool - done () const; + done() const; /** Perform direct crossing through given offer. @return an `Amounts` describing the flow achieved during cross */ BasicTaker::Flow - do_cross (Amounts offer, Quality quality, AccountID const& owner); + do_cross(Amounts offer, Quality quality, AccountID const& owner); /** Perform bridged crossing through given offers. @return a pair of `Amounts` describing the flow achieved during cross */ std::pair - do_cross ( - Amounts offer1, Quality quality1, AccountID const& owner1, - Amounts offer2, Quality quality2, AccountID const& owner2); + do_cross( + Amounts offer1, + Quality quality1, + AccountID const& owner1, + Amounts offer2, + Quality quality2, + AccountID const& owner2); - virtual - STAmount - get_funds (AccountID const& account, STAmount const& funds) const = 0; + virtual STAmount + get_funds(AccountID const& account, STAmount const& funds) const = 0; }; //------------------------------------------------------------------------------ -class Taker - : public BasicTaker +class Taker : public BasicTaker { public: - Taker () = delete; - Taker (Taker const&) = delete; + Taker() = delete; + Taker(Taker const&) = delete; - Taker (CrossType cross_type, ApplyView& view, - AccountID const& account, Amounts const& offer, - std::uint32_t flags, - beast::Journal journal); - ~Taker () = default; + Taker( + CrossType cross_type, + ApplyView& view, + AccountID const& account, + Amounts const& offer, + std::uint32_t flags, + beast::Journal journal); + ~Taker() = default; void - consume_offer (Offer& offer, Amounts const& order); + consume_offer(Offer& offer, Amounts const& order); STAmount - get_funds (AccountID const& account, STAmount const& funds) const override; + get_funds(AccountID const& account, STAmount const& funds) const override; STAmount const& - get_xrp_flow () const + get_xrp_flow() const { return xrp_flow_; } std::uint32_t - get_direct_crossings () const + get_direct_crossings() const { return direct_crossings_; } std::uint32_t - get_bridge_crossings () const + get_bridge_crossings() const { return bridge_crossings_; } @@ -267,35 +282,46 @@ public: */ /** @{ */ TER - cross (Offer& offer); + cross(Offer& offer); TER - cross (Offer& leg1, Offer& leg2); + cross(Offer& leg1, Offer& leg2); /** @} */ private: - static - Rate - calculateRate (ApplyView const& view, + static Rate + calculateRate( + ApplyView const& view, AccountID const& issuer, - AccountID const& account); + AccountID const& account); TER - fill (BasicTaker::Flow const& flow, Offer& offer); + fill(BasicTaker::Flow const& flow, Offer& offer); TER - fill ( - BasicTaker::Flow const& flow1, Offer& leg1, - BasicTaker::Flow const& flow2, Offer& leg2); + fill( + BasicTaker::Flow const& flow1, + Offer& leg1, + BasicTaker::Flow const& flow2, + Offer& leg2); TER - transferXRP (AccountID const& from, AccountID const& to, STAmount const& amount); + transferXRP( + AccountID const& from, + AccountID const& to, + STAmount const& amount); TER - redeemIOU (AccountID const& account, STAmount const& amount, Issue const& issue); + redeemIOU( + AccountID const& account, + STAmount const& amount, + Issue const& issue); TER - issueIOU (AccountID const& account, STAmount const& amount, Issue const& issue); + issueIOU( + AccountID const& account, + STAmount const& amount, + Issue const& issue); private: // The underlying ledger entry we are dealing with @@ -311,6 +337,6 @@ private: std::uint32_t bridge_crossings_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/Transactor.cpp b/src/ripple/app/tx/impl/Transactor.cpp index 9a30151446..0a5c7ff7f4 100644 --- a/src/ripple/app/tx/impl/Transactor.cpp +++ b/src/ripple/app/tx/impl/Transactor.cpp @@ -20,18 +20,18 @@ #include #include #include -#include #include -#include +#include #include +#include #include #include #include #include #include -#include #include #include +#include namespace ripple { @@ -43,8 +43,8 @@ preflight0(PreflightContext const& ctx) if (txID == beast::zero) { - JLOG(ctx.j.warn()) << - "applyTransaction: transaction id may not be zero"; + JLOG(ctx.j.warn()) + << "applyTransaction: transaction id may not be zero"; return temINVALID; } @@ -53,7 +53,7 @@ preflight0(PreflightContext const& ctx) /** Performs early sanity checks on the account and fee fields */ NotTEC -preflight1 (PreflightContext const& ctx) +preflight1(PreflightContext const& ctx) { auto const ret = preflight0(ctx); if (!isTesSuccess(ret)) @@ -67,8 +67,8 @@ preflight1 (PreflightContext const& ctx) } // No point in going any further if the transaction fee is malformed. - auto const fee = ctx.tx.getFieldAmount (sfFee); - if (!fee.native () || fee.negative () || !isLegalAmount (fee.xrp ())) + auto const fee = ctx.tx.getFieldAmount(sfFee); + if (!fee.native() || fee.negative() || !isLegalAmount(fee.xrp())) { JLOG(ctx.j.debug()) << "preflight1: invalid fee"; return temBAD_FEE; @@ -76,7 +76,7 @@ preflight1 (PreflightContext const& ctx) auto const spk = ctx.tx.getSigningPubKey(); - if (!spk.empty () && !publicKeyType (makeSlice (spk))) + if (!spk.empty() && !publicKeyType(makeSlice(spk))) { JLOG(ctx.j.debug()) << "preflight1: invalid signing key"; return temBAD_SIGNATURE; @@ -87,14 +87,13 @@ preflight1 (PreflightContext const& ctx) /** Checks whether the signature appears valid */ NotTEC -preflight2 (PreflightContext const& ctx) +preflight2(PreflightContext const& ctx) { - auto const sigValid = checkValidity(ctx.app.getHashRouter(), - ctx.tx, ctx.rules, ctx.app.config()); + auto const sigValid = checkValidity( + ctx.app.getHashRouter(), ctx.tx, ctx.rules, ctx.app.config()); if (sigValid.first == Validity::SigBad) { - JLOG(ctx.j.debug()) << - "preflight2: bad signature. " << sigValid.second; + JLOG(ctx.j.debug()) << "preflight2: bad signature. " << sigValid.second; return temINVALID; } return tesSUCCESS; @@ -102,30 +101,24 @@ preflight2 (PreflightContext const& ctx) //------------------------------------------------------------------------------ -PreflightContext::PreflightContext(Application& app_, STTx const& tx_, - Rules const& rules_, ApplyFlags flags_, - beast::Journal j_) - : app(app_) - , tx(tx_) - , rules(rules_) - , flags(flags_) - , j(j_) +PreflightContext::PreflightContext( + Application& app_, + STTx const& tx_, + Rules const& rules_, + ApplyFlags flags_, + beast::Journal j_) + : app(app_), tx(tx_), rules(rules_), flags(flags_), j(j_) { } //------------------------------------------------------------------------------ -Transactor::Transactor( - ApplyContext& ctx) - : ctx_ (ctx) - , j_ (ctx.journal) +Transactor::Transactor(ApplyContext& ctx) : ctx_(ctx), j_(ctx.journal) { } FeeUnit64 -Transactor::calculateBaseFee ( - ReadView const& view, - STTx const& tx) +Transactor::calculateBaseFee(ReadView const& view, STTx const& tx) { // Returns the fee in fee units. @@ -136,8 +129,8 @@ Transactor::calculateBaseFee ( // Each signer adds one more baseFee to the minimum required fee // for the transaction. - std::size_t const signerCount = tx.isFieldPresent (sfSigners) ? - tx.getFieldArray (sfSigners).size() : 0; + std::size_t const signerCount = + tx.isFieldPresent(sfSigners) ? tx.getFieldArray(sfSigners).size() : 0; return baseFee + (signerCount * baseFee); } @@ -149,11 +142,13 @@ Transactor::calculateFeePaid(STTx const& tx) } XRPAmount -Transactor::minimumFee (Application& app, FeeUnit64 baseFee, - Fees const& fees, ApplyFlags flags) +Transactor::minimumFee( + Application& app, + FeeUnit64 baseFee, + Fees const& fees, + ApplyFlags flags) { - return scaleFeeLoad (baseFee, app.getFeeTrack (), - fees, flags & tapUNLIMITED); + return scaleFeeLoad(baseFee, app.getFeeTrack(), fees, flags & tapUNLIMITED); } XRPAmount @@ -163,21 +158,20 @@ Transactor::calculateMaxSpend(STTx const& tx) } TER -Transactor::checkFee (PreclaimContext const& ctx, - FeeUnit64 baseFee) +Transactor::checkFee(PreclaimContext const& ctx, FeeUnit64 baseFee) { auto const feePaid = calculateFeePaid(ctx.tx); - if (!isLegalAmount (feePaid) || feePaid < beast::zero) + if (!isLegalAmount(feePaid) || feePaid < beast::zero) return temBAD_FEE; - auto const feeDue = minimumFee(ctx.app, - baseFee, ctx.view.fees(), ctx.flags); + auto const feeDue = + minimumFee(ctx.app, baseFee, ctx.view.fees(), ctx.flags); // Only check fee is sufficient when the ledger is open. if (ctx.view.open() && feePaid < feeDue) { - JLOG(ctx.j.trace()) << "Insufficient fee paid: " << - to_string (feePaid) << "/" << to_string (feeDue); + JLOG(ctx.j.trace()) << "Insufficient fee paid: " << to_string(feePaid) + << "/" << to_string(feeDue); return telINSUF_FEE_P; } @@ -186,16 +180,16 @@ Transactor::checkFee (PreclaimContext const& ctx, auto const id = ctx.tx.getAccountID(sfAccount); auto const sle = ctx.view.read(keylet::account(id)); - if (! sle) + if (!sle) return terNO_ACCOUNT; auto const balance = (*sle)[sfBalance].xrp(); if (balance < feePaid) { - JLOG(ctx.j.trace()) << "Insufficient balance:" << - " balance=" << to_string(balance) << - " paid=" << to_string(feePaid); + JLOG(ctx.j.trace()) << "Insufficient balance:" + << " balance=" << to_string(balance) + << " paid=" << to_string(feePaid); if ((balance > beast::zero) && !ctx.view.open()) { @@ -209,19 +203,20 @@ Transactor::checkFee (PreclaimContext const& ctx, return tesSUCCESS; } -TER Transactor::payFee () +TER +Transactor::payFee() { auto const feePaid = calculateFeePaid(ctx_.tx); auto const sle = view().peek(keylet::account(account_)); - if (! sle) + if (!sle) return tefINTERNAL; // Deduct the fee, so it's not available during the transaction. // Will only write the account back if the transaction succeeds. mSourceBalance -= feePaid; - sle->setFieldAmount (sfBalance, mSourceBalance); + sle->setFieldAmount(sfBalance, mSourceBalance); // VFALCO Should we call view().rawDestroyXRP() here as well? @@ -229,82 +224,84 @@ TER Transactor::payFee () } NotTEC -Transactor::checkSeq (PreclaimContext const& ctx) +Transactor::checkSeq(PreclaimContext const& ctx) { auto const id = ctx.tx.getAccountID(sfAccount); - auto const sle = ctx.view.read( - keylet::account(id)); + auto const sle = ctx.view.read(keylet::account(id)); if (!sle) { - JLOG(ctx.j.trace()) << - "applyTransaction: delay: source account does not exist " << - toBase58(ctx.tx.getAccountID(sfAccount)); + JLOG(ctx.j.trace()) + << "applyTransaction: delay: source account does not exist " + << toBase58(ctx.tx.getAccountID(sfAccount)); return terNO_ACCOUNT; } - std::uint32_t const t_seq = ctx.tx.getSequence (); - std::uint32_t const a_seq = sle->getFieldU32 (sfSequence); + std::uint32_t const t_seq = ctx.tx.getSequence(); + std::uint32_t const a_seq = sle->getFieldU32(sfSequence); if (t_seq != a_seq) { if (a_seq < t_seq) { - JLOG(ctx.j.trace()) << - "applyTransaction: has future sequence number " << - "a_seq=" << a_seq << " t_seq=" << t_seq; + JLOG(ctx.j.trace()) + << "applyTransaction: has future sequence number " + << "a_seq=" << a_seq << " t_seq=" << t_seq; return terPRE_SEQ; } - if (ctx.view.txExists(ctx.tx.getTransactionID ())) + if (ctx.view.txExists(ctx.tx.getTransactionID())) return tefALREADY; - JLOG(ctx.j.trace()) << "applyTransaction: has past sequence number " << - "a_seq=" << a_seq << " t_seq=" << t_seq; + JLOG(ctx.j.trace()) << "applyTransaction: has past sequence number " + << "a_seq=" << a_seq << " t_seq=" << t_seq; return tefPAST_SEQ; } - if (ctx.tx.isFieldPresent (sfAccountTxnID) && - (sle->getFieldH256 (sfAccountTxnID) != ctx.tx.getFieldH256 (sfAccountTxnID))) + if (ctx.tx.isFieldPresent(sfAccountTxnID) && + (sle->getFieldH256(sfAccountTxnID) != + ctx.tx.getFieldH256(sfAccountTxnID))) return tefWRONG_PRIOR; - if (ctx.tx.isFieldPresent (sfLastLedgerSequence) && - (ctx.view.seq() > ctx.tx.getFieldU32 (sfLastLedgerSequence))) + if (ctx.tx.isFieldPresent(sfLastLedgerSequence) && + (ctx.view.seq() > ctx.tx.getFieldU32(sfLastLedgerSequence))) return tefMAX_LEDGER; return tesSUCCESS; } void -Transactor::setSeq () +Transactor::setSeq() { auto const sle = view().peek(keylet::account(account_)); - if (! sle) + if (!sle) return; - std::uint32_t const t_seq = ctx_.tx.getSequence (); + std::uint32_t const t_seq = ctx_.tx.getSequence(); - sle->setFieldU32 (sfSequence, t_seq + 1); + sle->setFieldU32(sfSequence, t_seq + 1); - if (sle->isFieldPresent (sfAccountTxnID)) - sle->setFieldH256 (sfAccountTxnID, ctx_.tx.getTransactionID ()); + if (sle->isFieldPresent(sfAccountTxnID)) + sle->setFieldH256(sfAccountTxnID, ctx_.tx.getTransactionID()); } // check stuff before you bother to lock the ledger -void Transactor::preCompute () +void +Transactor::preCompute() { account_ = ctx_.tx.getAccountID(sfAccount); assert(account_ != beast::zero); } -TER Transactor::apply () +TER +Transactor::apply() { preCompute(); // If the transactor requires a valid account and the transaction doesn't // list one, preflight will have already a flagged a failure. - auto const sle = view().peek (keylet::account(account_)); + auto const sle = view().peek(keylet::account(account_)); // sle must exist except for transactions // that allow zero account. @@ -312,56 +309,55 @@ TER Transactor::apply () if (sle) { - mPriorBalance = STAmount ((*sle)[sfBalance]).xrp (); - mSourceBalance = mPriorBalance; + mPriorBalance = STAmount((*sle)[sfBalance]).xrp(); + mSourceBalance = mPriorBalance; setSeq(); - auto result = payFee (); + auto result = payFee(); - if (result != tesSUCCESS) + if (result != tesSUCCESS) return result; - view().update (sle); + view().update(sle); } - return doApply (); + return doApply(); } NotTEC -Transactor::checkSign (PreclaimContext const& ctx) +Transactor::checkSign(PreclaimContext const& ctx) { // If the pk is empty, then we must be multi-signing. - if (ctx.tx.getSigningPubKey().empty ()) - return checkMultiSign (ctx); + if (ctx.tx.getSigningPubKey().empty()) + return checkMultiSign(ctx); - return checkSingleSign (ctx); + return checkSingleSign(ctx); } NotTEC -Transactor::checkSingleSign (PreclaimContext const& ctx) +Transactor::checkSingleSign(PreclaimContext const& ctx) { // Check that the value in the signing key slot is a public key. auto const pkSigner = ctx.tx.getSigningPubKey(); if (!publicKeyType(makeSlice(pkSigner))) { - JLOG(ctx.j.trace()) << - "checkSingleSign: signing public key type is unknown"; - return tefBAD_AUTH; // FIXME: should be better error! + JLOG(ctx.j.trace()) + << "checkSingleSign: signing public key type is unknown"; + return tefBAD_AUTH; // FIXME: should be better error! } // Look up the account. auto const idSigner = calcAccountID(PublicKey(makeSlice(pkSigner))); auto const idAccount = ctx.tx.getAccountID(sfAccount); auto const sleAccount = ctx.view.read(keylet::account(idAccount)); - if (! sleAccount) + if (!sleAccount) return terNO_ACCOUNT; bool const isMasterDisabled = sleAccount->isFlag(lsfDisableMaster); if (ctx.view.rules().enabled(fixMasterKeyAsRegularKey)) { - // Signed with regular key. if ((*sleAccount)[~sfRegularKey] == idSigner) { @@ -382,7 +378,6 @@ Transactor::checkSingleSign (PreclaimContext const& ctx) // Signed with any other key. return tefBAD_AUTH; - } if (idSigner == idAccount) @@ -398,16 +393,16 @@ Transactor::checkSingleSign (PreclaimContext const& ctx) else if (sleAccount->isFieldPresent(sfRegularKey)) { // Signing key does not match master or regular key. - JLOG(ctx.j.trace()) << - "checkSingleSign: Not authorized to use account."; + JLOG(ctx.j.trace()) + << "checkSingleSign: Not authorized to use account."; return tefBAD_AUTH; } else { // No regular key on account and signing key does not match master key. // FIXME: Why differentiate this case from tefBAD_AUTH? - JLOG(ctx.j.trace()) << - "checkSingleSign: Not authorized to use account."; + JLOG(ctx.j.trace()) + << "checkSingleSign: Not authorized to use account."; return tefBAD_AUTH_MASTER; } @@ -415,32 +410,32 @@ Transactor::checkSingleSign (PreclaimContext const& ctx) } NotTEC -Transactor::checkMultiSign (PreclaimContext const& ctx) +Transactor::checkMultiSign(PreclaimContext const& ctx) { auto const id = ctx.tx.getAccountID(sfAccount); // Get mTxnAccountID's SignerList and Quorum. std::shared_ptr sleAccountSigners = - ctx.view.read (keylet::signers(id)); + ctx.view.read(keylet::signers(id)); // If the signer list doesn't exist the account is not multi-signing. if (!sleAccountSigners) { - JLOG(ctx.j.trace()) << - "applyTransaction: Invalid: Not a multi-signing account."; + JLOG(ctx.j.trace()) + << "applyTransaction: Invalid: Not a multi-signing account."; return tefNOT_MULTI_SIGNING; } // We have plans to support multiple SignerLists in the future. The // presence and defaulted value of the SignerListID field will enable that. - assert (sleAccountSigners->isFieldPresent (sfSignerListID)); - assert (sleAccountSigners->getFieldU32 (sfSignerListID) == 0); + assert(sleAccountSigners->isFieldPresent(sfSignerListID)); + assert(sleAccountSigners->getFieldU32(sfSignerListID) == 0); auto accountSigners = - SignerEntries::deserialize (*sleAccountSigners, ctx.j, "ledger"); + SignerEntries::deserialize(*sleAccountSigners, ctx.j, "ledger"); if (accountSigners.second != tesSUCCESS) return accountSigners.second; // Get the array of transaction signers. - STArray const& txSigners (ctx.tx.getFieldArray (sfSigners)); + STArray const& txSigners(ctx.tx.getFieldArray(sfSigners)); // Walk the accountSigners performing a variety of checks and see if // the quorum is met. @@ -449,43 +444,43 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) // matching multi-signers to account signers should be a simple // linear walk. *All* signers must be valid or the transaction fails. std::uint32_t weightSum = 0; - auto iter = accountSigners.first.begin (); + auto iter = accountSigners.first.begin(); for (auto const& txSigner : txSigners) { - AccountID const txSignerAcctID = txSigner.getAccountID (sfAccount); + AccountID const txSignerAcctID = txSigner.getAccountID(sfAccount); // Attempt to match the SignerEntry with a Signer; while (iter->account < txSignerAcctID) { - if (++iter == accountSigners.first.end ()) + if (++iter == accountSigners.first.end()) { - JLOG(ctx.j.trace()) << - "applyTransaction: Invalid SigningAccount.Account."; + JLOG(ctx.j.trace()) + << "applyTransaction: Invalid SigningAccount.Account."; return tefBAD_SIGNATURE; } } if (iter->account != txSignerAcctID) { // The SigningAccount is not in the SignerEntries. - JLOG(ctx.j.trace()) << - "applyTransaction: Invalid SigningAccount.Account."; + JLOG(ctx.j.trace()) + << "applyTransaction: Invalid SigningAccount.Account."; return tefBAD_SIGNATURE; } // We found the SigningAccount in the list of valid signers. Now we // need to compute the accountID that is associated with the signer's // public key. - auto const spk = txSigner.getFieldVL (sfSigningPubKey); + auto const spk = txSigner.getFieldVL(sfSigningPubKey); - if (!publicKeyType (makeSlice(spk))) + if (!publicKeyType(makeSlice(spk))) { - JLOG(ctx.j.trace()) << - "checkMultiSign: signing public key type is unknown"; + JLOG(ctx.j.trace()) + << "checkMultiSign: signing public key type is unknown"; return tefBAD_SIGNATURE; } AccountID const signingAcctIDFromPubKey = - calcAccountID(PublicKey (makeSlice(spk))); + calcAccountID(PublicKey(makeSlice(spk))); // Verify that the signingAcctID and the signingAcctIDFromPubKey // belong together. Here is are the rules: @@ -512,8 +507,7 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) // In any of these cases we need to know whether the account is in // the ledger. Determine that now. - auto sleTxSignerRoot = - ctx.view.read (keylet::account(txSignerAcctID)); + auto sleTxSignerRoot = ctx.view.read(keylet::account(txSignerAcctID)); if (signingAcctIDFromPubKey == txSignerAcctID) { @@ -522,12 +516,12 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) { // Master Key. Account may not have asfDisableMaster set. std::uint32_t const signerAccountFlags = - sleTxSignerRoot->getFieldU32 (sfFlags); + sleTxSignerRoot->getFieldU32(sfFlags); if (signerAccountFlags & lsfDisableMaster) { - JLOG(ctx.j.trace()) << - "applyTransaction: Signer:Account lsfDisableMaster."; + JLOG(ctx.j.trace()) + << "applyTransaction: Signer:Account lsfDisableMaster."; return tefMASTER_DISABLED; } } @@ -538,22 +532,22 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) // Public key must hash to the account's regular key. if (!sleTxSignerRoot) { - JLOG(ctx.j.trace()) << - "applyTransaction: Non-phantom signer lacks account root."; + JLOG(ctx.j.trace()) << "applyTransaction: Non-phantom signer " + "lacks account root."; return tefBAD_SIGNATURE; } - if (!sleTxSignerRoot->isFieldPresent (sfRegularKey)) + if (!sleTxSignerRoot->isFieldPresent(sfRegularKey)) { - JLOG(ctx.j.trace()) << - "applyTransaction: Account lacks RegularKey."; + JLOG(ctx.j.trace()) + << "applyTransaction: Account lacks RegularKey."; return tefBAD_SIGNATURE; } if (signingAcctIDFromPubKey != - sleTxSignerRoot->getAccountID (sfRegularKey)) + sleTxSignerRoot->getAccountID(sfRegularKey)) { - JLOG(ctx.j.trace()) << - "applyTransaction: Account doesn't match RegularKey."; + JLOG(ctx.j.trace()) + << "applyTransaction: Account doesn't match RegularKey."; return tefBAD_SIGNATURE; } } @@ -562,10 +556,10 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) } // Cannot perform transaction if quorum is not met. - if (weightSum < sleAccountSigners->getFieldU32 (sfSignerQuorum)) + if (weightSum < sleAccountSigners->getFieldU32(sfSignerQuorum)) { - JLOG(ctx.j.trace()) << - "applyTransaction: Signers failed to meet quorum."; + JLOG(ctx.j.trace()) + << "applyTransaction: Signers failed to meet quorum."; return tefBAD_QUORUM; } @@ -575,17 +569,20 @@ Transactor::checkMultiSign (PreclaimContext const& ctx) //------------------------------------------------------------------------------ -static -void removeUnfundedOffers (ApplyView& view, std::vector const& offers, beast::Journal viewJ) +static void +removeUnfundedOffers( + ApplyView& view, + std::vector const& offers, + beast::Journal viewJ) { int removed = 0; for (auto const& index : offers) { - if (auto const sleOffer = view.peek (keylet::offer (index))) + if (auto const sleOffer = view.peek(keylet::offer(index))) { // offer is unfunded - offerDelete (view, sleOffer, viewJ); + offerDelete(view, sleOffer, viewJ); if (++removed == unfundedOfferRemoveLimit) return; } @@ -598,14 +595,14 @@ Transactor::reset(XRPAmount fee) { ctx_.discard(); - auto const txnAcct = view().peek( - keylet::account(ctx_.tx.getAccountID(sfAccount))); - if (! txnAcct) + auto const txnAcct = + view().peek(keylet::account(ctx_.tx.getAccountID(sfAccount))); + if (!txnAcct) // The account should never be missing from the ledger. But if it // is missing then we can't very well charge it a fee, can we? return beast::zero; - auto const balance = txnAcct->getFieldAmount (sfBalance).xrp (); + auto const balance = txnAcct->getFieldAmount(sfBalance).xrp(); // balance should have already been checked in checkFee / preFlight. assert(balance != beast::zero && (!view().open() || balance >= fee)); @@ -617,10 +614,10 @@ Transactor::reset(XRPAmount fee) // Since we reset the context, we need to charge the fee and update // the account's sequence number again. - txnAcct->setFieldAmount (sfBalance, balance - fee); - txnAcct->setFieldU32 (sfSequence, ctx_.tx.getSequence() + 1); + txnAcct->setFieldAmount(sfBalance, balance - fee); + txnAcct->setFieldU32(sfSequence, ctx_.tx.getSequence() + 1); - view().update (txnAcct); + view().update(txnAcct); return fee; } @@ -629,22 +626,21 @@ Transactor::reset(XRPAmount fee) std::pair Transactor::operator()() { - JLOG(j_.trace()) << "apply: " << ctx_.tx.getTransactionID (); + JLOG(j_.trace()) << "apply: " << ctx_.tx.getTransactionID(); #ifdef DEBUG { Serializer ser; - ctx_.tx.add (ser); + ctx_.tx.add(ser); SerialIter sit(ser.slice()); - STTx s2 (sit); + STTx s2(sit); - if (! s2.isEquivalent(ctx_.tx)) + if (!s2.isEquivalent(ctx_.tx)) { - JLOG(j_.fatal()) << - "Transaction serdes mismatch"; - JLOG(j_.info()) << to_string(ctx_.tx.getJson (JsonOptions::none)); - JLOG(j_.fatal()) << s2.getJson (JsonOptions::none); - assert (false); + JLOG(j_.fatal()) << "Transaction serdes mismatch"; + JLOG(j_.info()) << to_string(ctx_.tx.getJson(JsonOptions::none)); + JLOG(j_.fatal()) << s2.getJson(JsonOptions::none); + assert(false); } } #endif @@ -660,8 +656,8 @@ Transactor::operator()() if (auto stream = j_.trace()) stream << "preclaim result: " << transToken(result); - bool applied = isTesSuccess (result); - auto fee = ctx_.tx.getFieldAmount(sfFee).xrp (); + bool applied = isTesSuccess(result); + auto fee = ctx_.tx.getFieldAmount(sfFee).xrp(); if (ctx_.size() > oversizeMetaDataCap) result = tecOVERSIZE; @@ -674,8 +670,9 @@ Transactor::operator()() ctx_.discard(); applied = false; } - else if ((result == tecOVERSIZE) || (result == tecKILLED) || - (isTecClaimHardFail (result, view().flags()))) + else if ( + (result == tecOVERSIZE) || (result == tecKILLED) || + (isTecClaimHardFail(result, view().flags()))) { JLOG(j_.trace()) << "reapplying because of " << transToken(result); @@ -683,25 +680,23 @@ Transactor::operator()() if ((result == tecOVERSIZE) || (result == tecKILLED)) { - ctx_.visit ( - [&removedOffers]( - uint256 const& index, - bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) + ctx_.visit([&removedOffers]( + uint256 const& index, + bool isDelete, + std::shared_ptr const& before, + std::shared_ptr const& after) { + if (isDelete) { - if (isDelete) + assert(before && after); + if (before && after && (before->getType() == ltOFFER) && + (before->getFieldAmount(sfTakerPays) == + after->getFieldAmount(sfTakerPays))) { - assert (before && after); - if (before && after && - (before->getType() == ltOFFER) && - (before->getFieldAmount(sfTakerPays) == after->getFieldAmount(sfTakerPays))) - { - // Removal of offer found or made unfunded - removedOffers.push_back (index); - } + // Removal of offer found or made unfunded + removedOffers.push_back(index); } - }); + } + }); } // Reset the context, potentially adjusting the fee @@ -709,7 +704,8 @@ Transactor::operator()() // If necessary, remove any offers found unfunded during processing if ((result == tecOVERSIZE) || (result == tecKILLED)) - removeUnfundedOffers (view(), removedOffers, ctx_.app.journal ("View")); + removeUnfundedOffers( + view(), removedOffers, ctx_.app.journal("View")); applied = true; } @@ -746,22 +742,23 @@ Transactor::operator()() // *never* trigger but if it, somehow, happens, don't allow a tx // that charges a negative fee. if (fee < beast::zero) - Throw ("fee charged is negative!"); + Throw("fee charged is negative!"); // Charge whatever fee they specified. The fee has already been // deducted from the balance of the account that issued the // transaction. We just need to account for it in the ledger // header. if (!view().open() && fee != beast::zero) - ctx_.destroyXRP (fee); + ctx_.destroyXRP(fee); // Once we call apply, we will no longer be able to look at view() ctx_.apply(result); } - JLOG(j_.trace()) << (applied ? "applied" : "not applied") << transToken(result); + JLOG(j_.trace()) << (applied ? "applied" : "not applied") + << transToken(result); - return { result, applied }; + return {result, applied}; } -} +} // namespace ripple diff --git a/src/ripple/app/tx/impl/Transactor.h b/src/ripple/app/tx/impl/Transactor.h index e5a4c90f89..b32dcf7890 100644 --- a/src/ripple/app/tx/impl/Transactor.h +++ b/src/ripple/app/tx/impl/Transactor.h @@ -38,11 +38,15 @@ public: ApplyFlags flags; beast::Journal const j; - PreflightContext(Application& app_, STTx const& tx_, - Rules const& rules_, ApplyFlags flags_, - beast::Journal j_); + PreflightContext( + Application& app_, + STTx const& tx_, + Rules const& rules_, + ApplyFlags flags_, + beast::Journal j_); - PreflightContext& operator=(PreflightContext const&) = delete; + PreflightContext& + operator=(PreflightContext const&) = delete; }; /** State information when determining if a tx is likely to claim a fee. */ @@ -56,10 +60,13 @@ public: ApplyFlags flags; beast::Journal const j; - PreclaimContext(Application& app_, ReadView const& view_, - TER preflightResult_, STTx const& tx_, - ApplyFlags flags_, - beast::Journal j_ = beast::Journal{beast::Journal::getNullSink()}) + PreclaimContext( + Application& app_, + ReadView const& view_, + TER preflightResult_, + STTx const& tx_, + ApplyFlags flags_, + beast::Journal j_ = beast::Journal{beast::Journal::getNullSink()}) : app(app_) , view(view_) , preflightResult(preflightResult_) @@ -69,7 +76,8 @@ public: { } - PreclaimContext& operator=(PreclaimContext const&) = delete; + PreclaimContext& + operator=(PreclaimContext const&) = delete; }; struct TxConsequences; @@ -81,13 +89,14 @@ protected: ApplyContext& ctx_; beast::Journal const j_; - AccountID account_; - XRPAmount mPriorBalance; // Balance before fees. - XRPAmount mSourceBalance; // Balance after fees. + AccountID account_; + XRPAmount mPriorBalance; // Balance before fees. + XRPAmount mSourceBalance; // Balance after fees. virtual ~Transactor() = default; - Transactor (Transactor const&) = delete; - Transactor& operator= (Transactor const&) = delete; + Transactor(Transactor const&) = delete; + Transactor& + operator=(Transactor const&) = delete; public: /** Process the transaction. */ @@ -116,43 +125,33 @@ public: comes with it. */ - static - NotTEC - checkSeq (PreclaimContext const& ctx); + static NotTEC + checkSeq(PreclaimContext const& ctx); - static - TER - checkFee (PreclaimContext const& ctx, FeeUnit64 baseFee); + static TER + checkFee(PreclaimContext const& ctx, FeeUnit64 baseFee); - static - NotTEC - checkSign (PreclaimContext const& ctx); + static NotTEC + checkSign(PreclaimContext const& ctx); // Returns the fee in fee units, not scaled for load. - static - FeeUnit64 - calculateBaseFee ( - ReadView const& view, - STTx const& tx); + static FeeUnit64 + calculateBaseFee(ReadView const& view, STTx const& tx); - static - bool + static bool affectsSubsequentTransactionAuth(STTx const& tx) { return false; } - static - XRPAmount + static XRPAmount calculateFeePaid(STTx const& tx); - static - XRPAmount + static XRPAmount calculateMaxSpend(STTx const& tx); - static - TER - preclaim(PreclaimContext const &ctx) + static TER + preclaim(PreclaimContext const& ctx) { // Most transactors do nothing // after checkSeq/Fee/Sign. @@ -164,12 +163,13 @@ protected: TER apply(); - explicit - Transactor (ApplyContext& ctx); + explicit Transactor(ApplyContext& ctx); - virtual void preCompute(); + virtual void + preCompute(); - virtual TER doApply () = 0; + virtual TER + doApply() = 0; /** Compute the minimum fee required to process a transaction with a given baseFee based on the current server load. @@ -180,18 +180,25 @@ protected: @param fees Fee settings from the current ledger @param flags Transaction processing fees */ - static - XRPAmount - minimumFee (Application& app, FeeUnit64 baseFee, - Fees const& fees, ApplyFlags flags); + static XRPAmount + minimumFee( + Application& app, + FeeUnit64 baseFee, + Fees const& fees, + ApplyFlags flags); private: - XRPAmount reset(XRPAmount fee); + XRPAmount + reset(XRPAmount fee); - void setSeq (); - TER payFee (); - static NotTEC checkSingleSign (PreclaimContext const& ctx); - static NotTEC checkMultiSign (PreclaimContext const& ctx); + void + setSeq(); + TER + payFee(); + static NotTEC + checkSingleSign(PreclaimContext const& ctx); + static NotTEC + checkMultiSign(PreclaimContext const& ctx); }; /** Performs early sanity checks on the txid */ @@ -200,12 +207,12 @@ preflight0(PreflightContext const& ctx); /** Performs early sanity checks on the account and fee fields */ NotTEC -preflight1 (PreflightContext const& ctx); +preflight1(PreflightContext const& ctx); /** Checks whether the signature appears valid */ NotTEC -preflight2 (PreflightContext const& ctx); +preflight2(PreflightContext const& ctx); -} +} // namespace ripple #endif diff --git a/src/ripple/app/tx/impl/apply.cpp b/src/ripple/app/tx/impl/apply.cpp index 0c56f8fb17..e423332756 100644 --- a/src/ripple/app/tx/impl/apply.cpp +++ b/src/ripple/app/tx/impl/apply.cpp @@ -17,26 +17,28 @@ */ //============================================================================== -#include +#include #include #include -#include +#include #include namespace ripple { // These are the same flags defined as SF_PRIVATE1-4 in HashRouter.h -#define SF_SIGBAD SF_PRIVATE1 // Signature is bad -#define SF_SIGGOOD SF_PRIVATE2 // Signature is good -#define SF_LOCALBAD SF_PRIVATE3 // Local checks failed -#define SF_LOCALGOOD SF_PRIVATE4 // Local checks passed +#define SF_SIGBAD SF_PRIVATE1 // Signature is bad +#define SF_SIGGOOD SF_PRIVATE2 // Signature is good +#define SF_LOCALBAD SF_PRIVATE3 // Local checks failed +#define SF_LOCALGOOD SF_PRIVATE4 // Local checks passed //------------------------------------------------------------------------------ std::pair -checkValidity(HashRouter& router, - STTx const& tx, Rules const& rules, - Config const& config) +checkValidity( + HashRouter& router, + STTx const& tx, + Rules const& rules, + Config const& config) { auto const id = tx.getTransactionID(); auto const flags = router.getFlags(id); @@ -48,12 +50,12 @@ checkValidity(HashRouter& router, { // Don't know signature state. Check it. auto const requireCanonicalSig = - rules.enabled(featureRequireFullyCanonicalSig) ? - STTx::RequireFullyCanonicalSig::yes : - STTx::RequireFullyCanonicalSig::no; + rules.enabled(featureRequireFullyCanonicalSig) + ? STTx::RequireFullyCanonicalSig::yes + : STTx::RequireFullyCanonicalSig::no; auto const sigVerify = tx.checkSign(requireCanonicalSig); - if (! sigVerify.first) + if (!sigVerify.first) { router.setFlags(id, SF_SIGBAD); return {Validity::SigBad, sigVerify.second}; @@ -84,30 +86,32 @@ checkValidity(HashRouter& router, } void -forceValidity(HashRouter& router, uint256 const& txid, - Validity validity) +forceValidity(HashRouter& router, uint256 const& txid, Validity validity) { int flags = 0; switch (validity) { - case Validity::Valid: - flags |= SF_LOCALGOOD; - [[fallthrough]]; - case Validity::SigGoodOnly: - flags |= SF_SIGGOOD; - [[fallthrough]]; - case Validity::SigBad: - // would be silly to call directly - break; + case Validity::Valid: + flags |= SF_LOCALGOOD; + [[fallthrough]]; + case Validity::SigGoodOnly: + flags |= SF_SIGGOOD; + [[fallthrough]]; + case Validity::SigBad: + // would be silly to call directly + break; } if (flags) router.setFlags(txid, flags); } std::pair -apply (Application& app, OpenView& view, - STTx const& tx, ApplyFlags flags, - beast::Journal j) +apply( + Application& app, + OpenView& view, + STTx const& tx, + ApplyFlags flags, + beast::Journal j) { auto pfresult = preflight(app, view.rules(), tx, flags, j); auto pcresult = preclaim(pfresult, app, view); @@ -115,46 +119,48 @@ apply (Application& app, OpenView& view, } ApplyResult -applyTransaction (Application& app, OpenView& view, +applyTransaction( + Application& app, + OpenView& view, STTx const& txn, - bool retryAssured, ApplyFlags flags, - beast::Journal j) + bool retryAssured, + ApplyFlags flags, + beast::Journal j) { // Returns false if the transaction has need not be retried. if (retryAssured) flags = flags | tapRETRY; - JLOG (j.debug()) << "TXN " << txn.getTransactionID () - << (retryAssured ? "/retry" : "/final"); + JLOG(j.debug()) << "TXN " << txn.getTransactionID() + << (retryAssured ? "/retry" : "/final"); try { auto const result = apply(app, view, txn, flags, j); if (result.second) { - JLOG (j.debug()) - << "Transaction applied: " << transHuman (result.first); + JLOG(j.debug()) + << "Transaction applied: " << transHuman(result.first); return ApplyResult::Success; } - if (isTefFailure (result.first) || isTemMalformed (result.first) || - isTelLocal (result.first)) + if (isTefFailure(result.first) || isTemMalformed(result.first) || + isTelLocal(result.first)) { // failure - JLOG (j.debug()) - << "Transaction failure: " << transHuman (result.first); + JLOG(j.debug()) + << "Transaction failure: " << transHuman(result.first); return ApplyResult::Fail; } - JLOG (j.debug()) - << "Transaction retry: " << transHuman (result.first); + JLOG(j.debug()) << "Transaction retry: " << transHuman(result.first); return ApplyResult::Retry; } catch (std::exception const&) { - JLOG (j.warn()) << "Throws"; + JLOG(j.warn()) << "Throws"; return ApplyResult::Fail; } } -} // ripple +} // namespace ripple diff --git a/src/ripple/app/tx/impl/applySteps.cpp b/src/ripple/app/tx/impl/applySteps.cpp index 30cf785f43..fddf922165 100644 --- a/src/ripple/app/tx/impl/applySteps.cpp +++ b/src/ripple/app/tx/impl/applySteps.cpp @@ -30,46 +30,66 @@ #include #include #include +#include #include #include #include #include #include -#include namespace ripple { -static -NotTEC -invoke_preflight (PreflightContext const& ctx) +static NotTEC +invoke_preflight(PreflightContext const& ctx) { - switch(ctx.tx.getTxnType()) + switch (ctx.tx.getTxnType()) { - case ttACCOUNT_SET: return SetAccount ::preflight(ctx); - case ttCHECK_CANCEL: return CancelCheck ::preflight(ctx); - case ttCHECK_CASH: return CashCheck ::preflight(ctx); - case ttCHECK_CREATE: return CreateCheck ::preflight(ctx); - case ttDEPOSIT_PREAUTH: return DepositPreauth ::preflight(ctx); - case ttOFFER_CANCEL: return CancelOffer ::preflight(ctx); - case ttOFFER_CREATE: return CreateOffer ::preflight(ctx); - case ttESCROW_CREATE: return EscrowCreate ::preflight(ctx); - case ttESCROW_FINISH: return EscrowFinish ::preflight(ctx); - case ttESCROW_CANCEL: return EscrowCancel ::preflight(ctx); - case ttPAYCHAN_CLAIM: return PayChanClaim ::preflight(ctx); - case ttPAYCHAN_CREATE: return PayChanCreate ::preflight(ctx); - case ttPAYCHAN_FUND: return PayChanFund ::preflight(ctx); - case ttPAYMENT: return Payment ::preflight(ctx); - case ttREGULAR_KEY_SET: return SetRegularKey ::preflight(ctx); - case ttSIGNER_LIST_SET: return SetSignerList ::preflight(ctx); - case ttTICKET_CANCEL: return CancelTicket ::preflight(ctx); - case ttTICKET_CREATE: return CreateTicket ::preflight(ctx); - case ttTRUST_SET: return SetTrust ::preflight(ctx); - case ttACCOUNT_DELETE: return DeleteAccount ::preflight(ctx); - case ttAMENDMENT: - case ttFEE: return Change ::preflight(ctx); - default: - assert(false); - return temUNKNOWN; + case ttACCOUNT_SET: + return SetAccount ::preflight(ctx); + case ttCHECK_CANCEL: + return CancelCheck ::preflight(ctx); + case ttCHECK_CASH: + return CashCheck ::preflight(ctx); + case ttCHECK_CREATE: + return CreateCheck ::preflight(ctx); + case ttDEPOSIT_PREAUTH: + return DepositPreauth ::preflight(ctx); + case ttOFFER_CANCEL: + return CancelOffer ::preflight(ctx); + case ttOFFER_CREATE: + return CreateOffer ::preflight(ctx); + case ttESCROW_CREATE: + return EscrowCreate ::preflight(ctx); + case ttESCROW_FINISH: + return EscrowFinish ::preflight(ctx); + case ttESCROW_CANCEL: + return EscrowCancel ::preflight(ctx); + case ttPAYCHAN_CLAIM: + return PayChanClaim ::preflight(ctx); + case ttPAYCHAN_CREATE: + return PayChanCreate ::preflight(ctx); + case ttPAYCHAN_FUND: + return PayChanFund ::preflight(ctx); + case ttPAYMENT: + return Payment ::preflight(ctx); + case ttREGULAR_KEY_SET: + return SetRegularKey ::preflight(ctx); + case ttSIGNER_LIST_SET: + return SetSignerList ::preflight(ctx); + case ttTICKET_CANCEL: + return CancelTicket ::preflight(ctx); + case ttTICKET_CREATE: + return CreateTicket ::preflight(ctx); + case ttTRUST_SET: + return SetTrust ::preflight(ctx); + case ttACCOUNT_DELETE: + return DeleteAccount ::preflight(ctx); + case ttAMENDMENT: + case ttFEE: + return Change ::preflight(ctx); + default: + assert(false); + return temUNKNOWN; } } @@ -77,9 +97,8 @@ invoke_preflight (PreflightContext const& ctx) compile-time polymorphism of (presumably) static class functions for Transactor and derived classes. */ -template -static -TER +template +static TER invoke_preclaim(PreclaimContext const& ctx) { // If the transactor requires a valid account and the transaction doesn't @@ -93,8 +112,7 @@ invoke_preclaim(PreclaimContext const& ctx) if (result != tesSUCCESS) return result; - result = T::checkFee(ctx, - calculateBaseFee(ctx.view, ctx.tx)); + result = T::checkFee(ctx, calculateBaseFee(ctx.view, ctx.tx)); if (result != tesSUCCESS) return result; @@ -103,220 +121,353 @@ invoke_preclaim(PreclaimContext const& ctx) if (result != tesSUCCESS) return result; - } return T::preclaim(ctx); } -static -TER -invoke_preclaim (PreclaimContext const& ctx) +static TER +invoke_preclaim(PreclaimContext const& ctx) { - switch(ctx.tx.getTxnType()) + switch (ctx.tx.getTxnType()) { - case ttACCOUNT_SET: return invoke_preclaim(ctx); - case ttCHECK_CANCEL: return invoke_preclaim(ctx); - case ttCHECK_CASH: return invoke_preclaim(ctx); - case ttCHECK_CREATE: return invoke_preclaim(ctx); - case ttDEPOSIT_PREAUTH: return invoke_preclaim(ctx); - case ttOFFER_CANCEL: return invoke_preclaim(ctx); - case ttOFFER_CREATE: return invoke_preclaim(ctx); - case ttESCROW_CREATE: return invoke_preclaim(ctx); - case ttESCROW_FINISH: return invoke_preclaim(ctx); - case ttESCROW_CANCEL: return invoke_preclaim(ctx); - case ttPAYCHAN_CLAIM: return invoke_preclaim(ctx); - case ttPAYCHAN_CREATE: return invoke_preclaim(ctx); - case ttPAYCHAN_FUND: return invoke_preclaim(ctx); - case ttPAYMENT: return invoke_preclaim(ctx); - case ttREGULAR_KEY_SET: return invoke_preclaim(ctx); - case ttSIGNER_LIST_SET: return invoke_preclaim(ctx); - case ttTICKET_CANCEL: return invoke_preclaim(ctx); - case ttTICKET_CREATE: return invoke_preclaim(ctx); - case ttTRUST_SET: return invoke_preclaim(ctx); - case ttACCOUNT_DELETE: return invoke_preclaim(ctx); - case ttAMENDMENT: - case ttFEE: return invoke_preclaim(ctx); - default: - assert(false); - return temUNKNOWN; + case ttACCOUNT_SET: + return invoke_preclaim(ctx); + case ttCHECK_CANCEL: + return invoke_preclaim(ctx); + case ttCHECK_CASH: + return invoke_preclaim(ctx); + case ttCHECK_CREATE: + return invoke_preclaim(ctx); + case ttDEPOSIT_PREAUTH: + return invoke_preclaim(ctx); + case ttOFFER_CANCEL: + return invoke_preclaim(ctx); + case ttOFFER_CREATE: + return invoke_preclaim(ctx); + case ttESCROW_CREATE: + return invoke_preclaim(ctx); + case ttESCROW_FINISH: + return invoke_preclaim(ctx); + case ttESCROW_CANCEL: + return invoke_preclaim(ctx); + case ttPAYCHAN_CLAIM: + return invoke_preclaim(ctx); + case ttPAYCHAN_CREATE: + return invoke_preclaim(ctx); + case ttPAYCHAN_FUND: + return invoke_preclaim(ctx); + case ttPAYMENT: + return invoke_preclaim(ctx); + case ttREGULAR_KEY_SET: + return invoke_preclaim(ctx); + case ttSIGNER_LIST_SET: + return invoke_preclaim(ctx); + case ttTICKET_CANCEL: + return invoke_preclaim(ctx); + case ttTICKET_CREATE: + return invoke_preclaim(ctx); + case ttTRUST_SET: + return invoke_preclaim(ctx); + case ttACCOUNT_DELETE: + return invoke_preclaim(ctx); + case ttAMENDMENT: + case ttFEE: + return invoke_preclaim(ctx); + default: + assert(false); + return temUNKNOWN; } } -static -FeeUnit64 -invoke_calculateBaseFee( - ReadView const& view, - STTx const& tx) +static FeeUnit64 +invoke_calculateBaseFee(ReadView const& view, STTx const& tx) { switch (tx.getTxnType()) { - case ttACCOUNT_SET: return SetAccount::calculateBaseFee(view, tx); - case ttCHECK_CANCEL: return CancelCheck::calculateBaseFee(view, tx); - case ttCHECK_CASH: return CashCheck::calculateBaseFee(view, tx); - case ttCHECK_CREATE: return CreateCheck::calculateBaseFee(view, tx); - case ttDEPOSIT_PREAUTH: return DepositPreauth::calculateBaseFee(view, tx); - case ttOFFER_CANCEL: return CancelOffer::calculateBaseFee(view, tx); - case ttOFFER_CREATE: return CreateOffer::calculateBaseFee(view, tx); - case ttESCROW_CREATE: return EscrowCreate::calculateBaseFee(view, tx); - case ttESCROW_FINISH: return EscrowFinish::calculateBaseFee(view, tx); - case ttESCROW_CANCEL: return EscrowCancel::calculateBaseFee(view, tx); - case ttPAYCHAN_CLAIM: return PayChanClaim::calculateBaseFee(view, tx); - case ttPAYCHAN_CREATE: return PayChanCreate::calculateBaseFee(view, tx); - case ttPAYCHAN_FUND: return PayChanFund::calculateBaseFee(view, tx); - case ttPAYMENT: return Payment::calculateBaseFee(view, tx); - case ttREGULAR_KEY_SET: return SetRegularKey::calculateBaseFee(view, tx); - case ttSIGNER_LIST_SET: return SetSignerList::calculateBaseFee(view, tx); - case ttTICKET_CANCEL: return CancelTicket::calculateBaseFee(view, tx); - case ttTICKET_CREATE: return CreateTicket::calculateBaseFee(view, tx); - case ttTRUST_SET: return SetTrust::calculateBaseFee(view, tx); - case ttACCOUNT_DELETE: return DeleteAccount::calculateBaseFee(view, tx); - case ttAMENDMENT: - case ttFEE: return Change::calculateBaseFee(view, tx); - default: - assert(false); - return FeeUnit64{0}; + case ttACCOUNT_SET: + return SetAccount::calculateBaseFee(view, tx); + case ttCHECK_CANCEL: + return CancelCheck::calculateBaseFee(view, tx); + case ttCHECK_CASH: + return CashCheck::calculateBaseFee(view, tx); + case ttCHECK_CREATE: + return CreateCheck::calculateBaseFee(view, tx); + case ttDEPOSIT_PREAUTH: + return DepositPreauth::calculateBaseFee(view, tx); + case ttOFFER_CANCEL: + return CancelOffer::calculateBaseFee(view, tx); + case ttOFFER_CREATE: + return CreateOffer::calculateBaseFee(view, tx); + case ttESCROW_CREATE: + return EscrowCreate::calculateBaseFee(view, tx); + case ttESCROW_FINISH: + return EscrowFinish::calculateBaseFee(view, tx); + case ttESCROW_CANCEL: + return EscrowCancel::calculateBaseFee(view, tx); + case ttPAYCHAN_CLAIM: + return PayChanClaim::calculateBaseFee(view, tx); + case ttPAYCHAN_CREATE: + return PayChanCreate::calculateBaseFee(view, tx); + case ttPAYCHAN_FUND: + return PayChanFund::calculateBaseFee(view, tx); + case ttPAYMENT: + return Payment::calculateBaseFee(view, tx); + case ttREGULAR_KEY_SET: + return SetRegularKey::calculateBaseFee(view, tx); + case ttSIGNER_LIST_SET: + return SetSignerList::calculateBaseFee(view, tx); + case ttTICKET_CANCEL: + return CancelTicket::calculateBaseFee(view, tx); + case ttTICKET_CREATE: + return CreateTicket::calculateBaseFee(view, tx); + case ttTRUST_SET: + return SetTrust::calculateBaseFee(view, tx); + case ttACCOUNT_DELETE: + return DeleteAccount::calculateBaseFee(view, tx); + case ttAMENDMENT: + case ttFEE: + return Change::calculateBaseFee(view, tx); + default: + assert(false); + return FeeUnit64{0}; } } -template -static -TxConsequences +template +static TxConsequences invoke_calculateConsequences(STTx const& tx) { - auto const category = T::affectsSubsequentTransactionAuth(tx) ? - TxConsequences::blocker : TxConsequences::normal; + auto const category = T::affectsSubsequentTransactionAuth(tx) + ? TxConsequences::blocker + : TxConsequences::normal; auto const feePaid = T::calculateFeePaid(tx); auto const maxSpend = T::calculateMaxSpend(tx); - return{ category, feePaid, maxSpend }; + return {category, feePaid, maxSpend}; } -static -TxConsequences +static TxConsequences invoke_calculateConsequences(STTx const& tx) { switch (tx.getTxnType()) { - case ttACCOUNT_SET: return invoke_calculateConsequences(tx); - case ttCHECK_CANCEL: return invoke_calculateConsequences(tx); - case ttCHECK_CASH: return invoke_calculateConsequences(tx); - case ttCHECK_CREATE: return invoke_calculateConsequences(tx); - case ttDEPOSIT_PREAUTH: return invoke_calculateConsequences(tx); - case ttOFFER_CANCEL: return invoke_calculateConsequences(tx); - case ttOFFER_CREATE: return invoke_calculateConsequences(tx); - case ttESCROW_CREATE: return invoke_calculateConsequences(tx); - case ttESCROW_FINISH: return invoke_calculateConsequences(tx); - case ttESCROW_CANCEL: return invoke_calculateConsequences(tx); - case ttPAYCHAN_CLAIM: return invoke_calculateConsequences(tx); - case ttPAYCHAN_CREATE: return invoke_calculateConsequences(tx); - case ttPAYCHAN_FUND: return invoke_calculateConsequences(tx); - case ttPAYMENT: return invoke_calculateConsequences(tx); - case ttREGULAR_KEY_SET: return invoke_calculateConsequences(tx); - case ttSIGNER_LIST_SET: return invoke_calculateConsequences(tx); - case ttTICKET_CANCEL: return invoke_calculateConsequences(tx); - case ttTICKET_CREATE: return invoke_calculateConsequences(tx); - case ttTRUST_SET: return invoke_calculateConsequences(tx); - case ttACCOUNT_DELETE: return invoke_calculateConsequences(tx); - case ttAMENDMENT: - case ttFEE: - [[fallthrough]]; - default: - assert(false); - return { TxConsequences::blocker, Transactor::calculateFeePaid(tx), - beast::zero }; + case ttACCOUNT_SET: + return invoke_calculateConsequences(tx); + case ttCHECK_CANCEL: + return invoke_calculateConsequences(tx); + case ttCHECK_CASH: + return invoke_calculateConsequences(tx); + case ttCHECK_CREATE: + return invoke_calculateConsequences(tx); + case ttDEPOSIT_PREAUTH: + return invoke_calculateConsequences(tx); + case ttOFFER_CANCEL: + return invoke_calculateConsequences(tx); + case ttOFFER_CREATE: + return invoke_calculateConsequences(tx); + case ttESCROW_CREATE: + return invoke_calculateConsequences(tx); + case ttESCROW_FINISH: + return invoke_calculateConsequences(tx); + case ttESCROW_CANCEL: + return invoke_calculateConsequences(tx); + case ttPAYCHAN_CLAIM: + return invoke_calculateConsequences(tx); + case ttPAYCHAN_CREATE: + return invoke_calculateConsequences(tx); + case ttPAYCHAN_FUND: + return invoke_calculateConsequences(tx); + case ttPAYMENT: + return invoke_calculateConsequences(tx); + case ttREGULAR_KEY_SET: + return invoke_calculateConsequences(tx); + case ttSIGNER_LIST_SET: + return invoke_calculateConsequences(tx); + case ttTICKET_CANCEL: + return invoke_calculateConsequences(tx); + case ttTICKET_CREATE: + return invoke_calculateConsequences(tx); + case ttTRUST_SET: + return invoke_calculateConsequences(tx); + case ttACCOUNT_DELETE: + return invoke_calculateConsequences(tx); + case ttAMENDMENT: + case ttFEE: + [[fallthrough]]; + default: + assert(false); + return { + TxConsequences::blocker, + Transactor::calculateFeePaid(tx), + beast::zero}; } } -static -std::pair -invoke_apply (ApplyContext& ctx) +static std::pair +invoke_apply(ApplyContext& ctx) { - switch(ctx.tx.getTxnType()) + switch (ctx.tx.getTxnType()) { - case ttACCOUNT_SET: { SetAccount p(ctx); return p(); } - case ttCHECK_CANCEL: { CancelCheck p(ctx); return p(); } - case ttCHECK_CASH: { CashCheck p(ctx); return p(); } - case ttCHECK_CREATE: { CreateCheck p(ctx); return p(); } - case ttDEPOSIT_PREAUTH: { DepositPreauth p(ctx); return p(); } - case ttOFFER_CANCEL: { CancelOffer p(ctx); return p(); } - case ttOFFER_CREATE: { CreateOffer p(ctx); return p(); } - case ttESCROW_CREATE: { EscrowCreate p(ctx); return p(); } - case ttESCROW_FINISH: { EscrowFinish p(ctx); return p(); } - case ttESCROW_CANCEL: { EscrowCancel p(ctx); return p(); } - case ttPAYCHAN_CLAIM: { PayChanClaim p(ctx); return p(); } - case ttPAYCHAN_CREATE: { PayChanCreate p(ctx); return p(); } - case ttPAYCHAN_FUND: { PayChanFund p(ctx); return p(); } - case ttPAYMENT: { Payment p(ctx); return p(); } - case ttREGULAR_KEY_SET : { SetRegularKey p(ctx); return p(); } - case ttSIGNER_LIST_SET : { SetSignerList p(ctx); return p(); } - case ttTICKET_CANCEL: { CancelTicket p(ctx); return p(); } - case ttTICKET_CREATE: { CreateTicket p(ctx); return p(); } - case ttTRUST_SET: { SetTrust p(ctx); return p(); } - case ttACCOUNT_DELETE: { DeleteAccount p(ctx); return p(); } - case ttAMENDMENT: - case ttFEE: { Change p(ctx); return p(); } - default: - assert(false); - return { temUNKNOWN, false }; + case ttACCOUNT_SET: { + SetAccount p(ctx); + return p(); + } + case ttCHECK_CANCEL: { + CancelCheck p(ctx); + return p(); + } + case ttCHECK_CASH: { + CashCheck p(ctx); + return p(); + } + case ttCHECK_CREATE: { + CreateCheck p(ctx); + return p(); + } + case ttDEPOSIT_PREAUTH: { + DepositPreauth p(ctx); + return p(); + } + case ttOFFER_CANCEL: { + CancelOffer p(ctx); + return p(); + } + case ttOFFER_CREATE: { + CreateOffer p(ctx); + return p(); + } + case ttESCROW_CREATE: { + EscrowCreate p(ctx); + return p(); + } + case ttESCROW_FINISH: { + EscrowFinish p(ctx); + return p(); + } + case ttESCROW_CANCEL: { + EscrowCancel p(ctx); + return p(); + } + case ttPAYCHAN_CLAIM: { + PayChanClaim p(ctx); + return p(); + } + case ttPAYCHAN_CREATE: { + PayChanCreate p(ctx); + return p(); + } + case ttPAYCHAN_FUND: { + PayChanFund p(ctx); + return p(); + } + case ttPAYMENT: { + Payment p(ctx); + return p(); + } + case ttREGULAR_KEY_SET: { + SetRegularKey p(ctx); + return p(); + } + case ttSIGNER_LIST_SET: { + SetSignerList p(ctx); + return p(); + } + case ttTICKET_CANCEL: { + CancelTicket p(ctx); + return p(); + } + case ttTICKET_CREATE: { + CreateTicket p(ctx); + return p(); + } + case ttTRUST_SET: { + SetTrust p(ctx); + return p(); + } + case ttACCOUNT_DELETE: { + DeleteAccount p(ctx); + return p(); + } + case ttAMENDMENT: + case ttFEE: { + Change p(ctx); + return p(); + } + default: + assert(false); + return {temUNKNOWN, false}; } } PreflightResult -preflight(Application& app, Rules const& rules, - STTx const& tx, ApplyFlags flags, - beast::Journal j) +preflight( + Application& app, + Rules const& rules, + STTx const& tx, + ApplyFlags flags, + beast::Journal j) { - PreflightContext const pfctx(app, tx, - rules, flags, j); + PreflightContext const pfctx(app, tx, rules, flags, j); try { - return{ pfctx, invoke_preflight(pfctx) }; + return {pfctx, invoke_preflight(pfctx)}; } catch (std::exception const& e) { - JLOG(j.fatal()) << - "apply: " << e.what(); - return{ pfctx, tefEXCEPTION }; + JLOG(j.fatal()) << "apply: " << e.what(); + return {pfctx, tefEXCEPTION}; } } PreclaimResult -preclaim (PreflightResult const& preflightResult, - Application& app, OpenView const& view) +preclaim( + PreflightResult const& preflightResult, + Application& app, + OpenView const& view) { boost::optional ctx; if (preflightResult.rules != view.rules()) { - auto secondFlight = preflight(app, view.rules(), - preflightResult.tx, preflightResult.flags, - preflightResult.j); - ctx.emplace(app, view, secondFlight.ter, secondFlight.tx, - secondFlight.flags, secondFlight.j); + auto secondFlight = preflight( + app, + view.rules(), + preflightResult.tx, + preflightResult.flags, + preflightResult.j); + ctx.emplace( + app, + view, + secondFlight.ter, + secondFlight.tx, + secondFlight.flags, + secondFlight.j); } else { ctx.emplace( - app, view, preflightResult.ter, preflightResult.tx, - preflightResult.flags, preflightResult.j); + app, + view, + preflightResult.ter, + preflightResult.tx, + preflightResult.flags, + preflightResult.j); } try { if (ctx->preflightResult != tesSUCCESS) - return { *ctx, ctx->preflightResult }; - return{ *ctx, invoke_preclaim(*ctx) }; + return {*ctx, ctx->preflightResult}; + return {*ctx, invoke_preclaim(*ctx)}; } catch (std::exception const& e) { - JLOG(ctx->j.fatal()) << - "apply: " << e.what(); - return{ *ctx, tefEXCEPTION }; + JLOG(ctx->j.fatal()) << "apply: " << e.what(); + return {*ctx, tefEXCEPTION}; } } FeeUnit64 -calculateBaseFee(ReadView const& view, - STTx const& tx) +calculateBaseFee(ReadView const& view, STTx const& tx) { return invoke_calculateBaseFee(view, tx); } @@ -326,39 +477,41 @@ calculateConsequences(PreflightResult const& preflightResult) { assert(preflightResult.ter == tesSUCCESS); if (preflightResult.ter != tesSUCCESS) - return{ TxConsequences::blocker, + return { + TxConsequences::blocker, Transactor::calculateFeePaid(preflightResult.tx), - beast::zero }; + beast::zero}; return invoke_calculateConsequences(preflightResult.tx); } std::pair -doApply(PreclaimResult const& preclaimResult, - Application& app, OpenView& view) +doApply(PreclaimResult const& preclaimResult, Application& app, OpenView& view) { if (preclaimResult.view.seq() != view.seq()) { // Logic error from the caller. Don't have enough // info to recover. - return{ tefEXCEPTION, false }; + return {tefEXCEPTION, false}; } try { if (!preclaimResult.likelyToClaimFee) - return{ preclaimResult.ter, false }; - ApplyContext ctx(app, view, - preclaimResult.tx, preclaimResult.ter, - calculateBaseFee(view, preclaimResult.tx), - preclaimResult.flags, preclaimResult.j); + return {preclaimResult.ter, false}; + ApplyContext ctx( + app, + view, + preclaimResult.tx, + preclaimResult.ter, + calculateBaseFee(view, preclaimResult.tx), + preclaimResult.flags, + preclaimResult.j); return invoke_apply(ctx); } catch (std::exception const& e) { - JLOG(preclaimResult.j.fatal()) << - "apply: " << e.what(); - return { tefEXCEPTION, false }; + JLOG(preclaimResult.j.fatal()) << "apply: " << e.what(); + return {tefEXCEPTION, false}; } } -} // ripple - +} // namespace ripple diff --git a/src/ripple/basics/Archive.h b/src/ripple/basics/Archive.h index 7eead5d7e3..7ae6950f46 100644 --- a/src/ripple/basics/Archive.h +++ b/src/ripple/basics/Archive.h @@ -36,6 +36,6 @@ extractTarLz4( boost::filesystem::path const& src, boost::filesystem::path const& dst); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/BasicConfig.h b/src/ripple/basics/BasicConfig.h index f68bdccb45..50f576469b 100644 --- a/src/ripple/basics/BasicConfig.h +++ b/src/ripple/basics/BasicConfig.h @@ -21,11 +21,11 @@ #define RIPPLE_BASICS_BASICCONFIG_H_INCLUDED #include -#include #include #include #include #include +#include #include #include #include @@ -40,20 +40,18 @@ using IniFileSections = std::map>; /** Holds a collection of configuration values. A configuration file contains zero or more sections. */ -class Section - : public beast::unit_test::detail::const_container < - std::map > +class Section : public beast::unit_test::detail::const_container< + std::map> { private: std::string name_; - std::vector lines_; - std::vector values_; + std::vector lines_; + std::vector values_; bool had_trailing_comments_ = false; public: /** Create an empty section. */ - explicit - Section (std::string const& name = ""); + explicit Section(std::string const& name = ""); /** Returns the name of this section. */ std::string const& @@ -65,7 +63,7 @@ public: /** Returns all the lines in the section. This includes everything. */ - std::vector const& + std::vector const& lines() const { return lines_; @@ -74,7 +72,7 @@ public: /** Returns all the values in the section. Values are non-empty lines which are not key/value pairs. */ - std::vector const& + std::vector const& values() const { return values_; @@ -84,12 +82,12 @@ public: * Set the legacy value for this section. */ void - legacy (std::string value) + legacy(std::string value) { - if (lines_.empty ()) - lines_.emplace_back (std::move (value)); + if (lines_.empty()) + lines_.emplace_back(std::move(value)); else - lines_[0] = std::move (value); + lines_[0] = std::move(value); } /** @@ -99,12 +97,12 @@ public: an empty string. */ std::string - legacy () const + legacy() const { - if (lines_.empty ()) + if (lines_.empty()) return ""; - if (lines_.size () > 1) - Throw ( + if (lines_.size() > 1) + Throw( "A legacy value must have exactly one line. Section: " + name_); return lines_[0]; } @@ -113,7 +111,7 @@ public: The previous value is discarded. */ void - set (std::string const& key, std::string const& value); + set(std::string const& key, std::string const& value); /** Append a set of lines to this section. Lines containing key/value pairs are added to the map, @@ -121,28 +119,28 @@ public: added to the lines list. */ void - append (std::vector const& lines); + append(std::vector const& lines); /** Append a line to this section. */ void - append (std::string const& line) + append(std::string const& line) { - append (std::vector{ line }); + append(std::vector{line}); } /** Returns `true` if a key with the given name exists. */ bool - exists (std::string const& name) const; + exists(std::string const& name) const; /** Retrieve a key/value pair. @return A pair with bool `true` if the string was found. */ - std::pair - find (std::string const& name) const; + std::pair + find(std::string const& name) const; template boost::optional - get (std::string const& name) const + get(std::string const& name) const { auto const iter = cont().find(name); if (iter == cont().end()) @@ -151,7 +149,7 @@ public: } /// Returns a value if present, else another value. - template + template T value_or(std::string const& name, T const& other) const { @@ -161,11 +159,14 @@ public: // indicates if trailing comments were seen // during the appending of any lines/values - bool had_trailing_comments() const { return had_trailing_comments_; } + bool + had_trailing_comments() const + { + return had_trailing_comments_; + } - friend - std::ostream& - operator<< (std::ostream&, Section const& section); + friend std::ostream& + operator<<(std::ostream&, Section const& section); }; //------------------------------------------------------------------------------ @@ -177,31 +178,31 @@ public: class BasicConfig { private: - std::map map_; + std::map map_; public: /** Returns `true` if a section with the given name exists. */ bool - exists (std::string const& name) const; + exists(std::string const& name) const; /** Returns the section with the given name. If the section does not exist, an empty section is returned. */ /** @{ */ Section& - section (std::string const& name); + section(std::string const& name); Section const& - section (std::string const& name) const; + section(std::string const& name) const; Section const& - operator[] (std::string const& name) const + operator[](std::string const& name) const { return section(name); } Section& - operator[] (std::string const& name) + operator[](std::string const& name) { return section(name); } @@ -212,13 +213,15 @@ public: The previous value, if any, is overwritten. */ void - overwrite (std::string const& section, std::string const& key, + overwrite( + std::string const& section, + std::string const& key, std::string const& value); /** Remove all the key/value pairs from the section. */ void - deprecatedClearSection (std::string const& section); + deprecatedClearSection(std::string const& section); /** * Set a value that is not a key/value pair. @@ -243,20 +246,22 @@ public: std::string legacy(std::string const& sectionName) const; - friend - std::ostream& - operator<< (std::ostream& ss, BasicConfig const& c); + friend std::ostream& + operator<<(std::ostream& ss, BasicConfig const& c); // indicates if trailing comments were seen // in any loaded Sections - bool had_trailing_comments() const { - return std::any_of(map_.cbegin(), map_.cend(), - [](auto s){ return s.second.had_trailing_comments(); }); + bool + had_trailing_comments() const + { + return std::any_of(map_.cbegin(), map_.cend(), [](auto s) { + return s.second.had_trailing_comments(); + }); } protected: void - build (IniFileSections const& ifs); + build(IniFileSections const& ifs); }; //------------------------------------------------------------------------------ @@ -268,12 +273,12 @@ protected: */ template bool -set (T& target, std::string const& name, Section const& section) +set(T& target, std::string const& name, Section const& section) { bool found_and_valid = false; try { - auto const val = section.get (name); + auto const val = section.get(name); if ((found_and_valid = val.is_initialized())) target = *val; } @@ -290,8 +295,10 @@ set (T& target, std::string const& name, Section const& section) */ template bool -set (T& target, T const& defaultValue, - std::string const& name, Section const& section) +set(T& target, + T const& defaultValue, + std::string const& name, + Section const& section) { bool found_and_valid = set(target, name, section); if (!found_and_valid) @@ -306,12 +313,13 @@ set (T& target, T const& defaultValue, // NOTE This routine might be more clumsy than the previous two template T -get (Section const& section, - std::string const& name, T const& defaultValue = T{}) +get(Section const& section, + std::string const& name, + T const& defaultValue = T{}) { try { - return section.value_or (name, defaultValue); + return section.value_or(name, defaultValue); } catch (boost::bad_lexical_cast&) { @@ -319,15 +327,13 @@ get (Section const& section, return defaultValue; } -inline -std::string -get (Section const& section, - std::string const& name, const char* defaultValue) +inline std::string +get(Section const& section, std::string const& name, const char* defaultValue) { bool found_and_valid = false; try { - auto const val = section.get (name); + auto const val = section.get(name); if ((found_and_valid = val.is_initialized())) return *val; } @@ -339,25 +345,22 @@ get (Section const& section, template bool -get_if_exists (Section const& section, - std::string const& name, T& v) +get_if_exists(Section const& section, std::string const& name, T& v) { return set(v, name, section); } template <> -inline -bool -get_if_exists (Section const& section, - std::string const& name, bool& v) +inline bool +get_if_exists(Section const& section, std::string const& name, bool& v) { int intVal = 0; auto stat = get_if_exists(section, name, intVal); if (stat) - v = bool (intVal); + v = bool(intVal); return stat; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/Blob.h b/src/ripple/basics/Blob.h index 068fc2d677..9949cbd040 100644 --- a/src/ripple/basics/Blob.h +++ b/src/ripple/basics/Blob.h @@ -27,8 +27,8 @@ namespace ripple { /** Storage for linear binary data. Blocks of binary data appear often in various idioms and structures. */ -using Blob = std::vector ; +using Blob = std::vector; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/Buffer.h b/src/ripple/basics/Buffer.h index d19afa661d..706b8b627f 100644 --- a/src/ripple/basics/Buffer.h +++ b/src/ripple/basics/Buffer.h @@ -44,10 +44,8 @@ public: Buffer() = default; /** Create an uninitialized buffer with the given size. */ - explicit - Buffer (std::size_t size) - : p_ (size ? new std::uint8_t[size] : nullptr) - , size_ (size) + explicit Buffer(std::size_t size) + : p_(size ? new std::uint8_t[size] : nullptr), size_(size) { } @@ -57,26 +55,25 @@ public: size is non-zero, it must not be null. @param size size of the existing memory block. */ - Buffer (void const* data, std::size_t size) - : Buffer (size) + Buffer(void const* data, std::size_t size) : Buffer(size) { if (size) std::memcpy(p_.get(), data, size); } /** Copy-construct */ - Buffer (Buffer const& other) - : Buffer (other.p_.get(), other.size_) + Buffer(Buffer const& other) : Buffer(other.p_.get(), other.size_) { } /** Copy assign */ - Buffer& operator= (Buffer const& other) + Buffer& + operator=(Buffer const& other) { if (this != &other) { - if (auto p = alloc (other.size_)) - std::memcpy (p, other.p_.get(), size_); + if (auto p = alloc(other.size_)) + std::memcpy(p, other.p_.get(), size_); } return *this; } @@ -84,9 +81,8 @@ public: /** Move-construct. The other buffer is reset. */ - Buffer (Buffer&& other) noexcept - : p_ (std::move(other.p_)) - , size_ (other.size_) + Buffer(Buffer&& other) noexcept + : p_(std::move(other.p_)), size_(other.size_) { other.size_ = 0; } @@ -94,7 +90,8 @@ public: /** Move-assign. The other buffer is reset. */ - Buffer& operator= (Buffer&& other) noexcept + Buffer& + operator=(Buffer&& other) noexcept { if (this != &other) { @@ -106,22 +103,21 @@ public: } /** Construct from a slice */ - explicit - Buffer (Slice s) - : Buffer (s.data(), s.size()) + explicit Buffer(Slice s) : Buffer(s.data(), s.size()) { } /** Assign from slice */ - Buffer& operator= (Slice s) + Buffer& + operator=(Slice s) { // Ensure the slice isn't a subset of the buffer. - assert (s.size() == 0 || size_ == 0 || - s.data() < p_.get() || + assert( + s.size() == 0 || size_ == 0 || s.data() < p_.get() || s.data() >= p_.get() + size_); - if (auto p = alloc (s.size())) - std::memcpy (p, s.data(), s.size()); + if (auto p = alloc(s.size())) + std::memcpy(p, s.data(), s.size()); return *this; } @@ -133,16 +129,16 @@ public: } bool - empty () const noexcept + empty() const noexcept { return 0 == size_; } operator Slice() const noexcept { - if (! size_) + if (!size_) return Slice{}; - return Slice{ p_.get(), size_ }; + return Slice{p_.get(), size_}; } /** Return a pointer to beginning of the storage. @@ -177,7 +173,7 @@ public: Existing data, if any, is discarded. */ std::uint8_t* - alloc (std::size_t n) + alloc(std::size_t n) { if (n != size_) { @@ -219,7 +215,8 @@ public: } }; -inline bool operator==(Buffer const& lhs, Buffer const& rhs) noexcept +inline bool +operator==(Buffer const& lhs, Buffer const& rhs) noexcept { if (lhs.size() != rhs.size()) return false; @@ -230,11 +227,12 @@ inline bool operator==(Buffer const& lhs, Buffer const& rhs) noexcept return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0; } -inline bool operator!=(Buffer const& lhs, Buffer const& rhs) noexcept +inline bool +operator!=(Buffer const& lhs, Buffer const& rhs) noexcept { return !(lhs == rhs); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/ByteUtilities.h b/src/ripple/basics/ByteUtilities.h index 83d6e8e480..8013398eba 100644 --- a/src/ripple/basics/ByteUtilities.h +++ b/src/ripple/basics/ByteUtilities.h @@ -22,20 +22,22 @@ namespace ripple { - template - constexpr auto kilobytes(T value) noexcept - { - return value * 1024; - } - - template - constexpr auto megabytes(T value) noexcept - { - return kilobytes(kilobytes(value)); - } - - static_assert(kilobytes(2) == 2048, "kilobytes(2) == 2048"); - static_assert(megabytes(3) == 3145728, "megabytes(3) == 3145728"); +template +constexpr auto +kilobytes(T value) noexcept +{ + return value * 1024; } +template +constexpr auto +megabytes(T value) noexcept +{ + return kilobytes(kilobytes(value)); +} + +static_assert(kilobytes(2) == 2048, "kilobytes(2) == 2048"); +static_assert(megabytes(3) == 3145728, "megabytes(3) == 3145728"); +} // namespace ripple + #endif diff --git a/src/ripple/basics/CompressionAlgorithms.h b/src/ripple/basics/CompressionAlgorithms.h index 3cd67c753d..401d043621 100644 --- a/src/ripple/basics/CompressionAlgorithms.h +++ b/src/ripple/basics/CompressionAlgorithms.h @@ -21,8 +21,8 @@ #define RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED #include -#include #include +#include namespace ripple { @@ -31,7 +31,8 @@ namespace compression_algorithms { /** Convenience wrapper for Throw * @param message Message to log/throw */ -inline void doThrow(const char *message) +inline void +doThrow(const char* message) { Throw(message); } @@ -44,24 +45,24 @@ inline void doThrow(const char *message) * @param bf Compressed buffer allocator * @return Size of compressed data, or zero if failed to compress */ -template +template std::size_t -lz4Compress(void const* in, - std::size_t inSize, BufferFactory&& bf) +lz4Compress(void const* in, std::size_t inSize, BufferFactory&& bf) { if (inSize > UINT32_MAX) doThrow("lz4 compress: invalid size"); auto const outCapacity = LZ4_compressBound(inSize); - // Request the caller to allocate and return the buffer to hold compressed data + // Request the caller to allocate and return the buffer to hold compressed + // data auto compressed = bf(outCapacity); auto compressedSize = LZ4_compress_default( - reinterpret_cast(in), - reinterpret_cast(compressed), - inSize, - outCapacity); + reinterpret_cast(in), + reinterpret_cast(compressed), + inSize, + outCapacity); if (compressedSize == 0) doThrow("lz4 compress: failed"); @@ -75,13 +76,18 @@ lz4Compress(void const* in, * @param decompressedSize Size of the decompressed buffer * @return size of the decompressed data */ -inline -std::size_t -lz4Decompress(std::uint8_t const* in, std::size_t inSize, - std::uint8_t* decompressed, std::size_t decompressedSize) +inline std::size_t +lz4Decompress( + std::uint8_t const* in, + std::size_t inSize, + std::uint8_t* decompressed, + std::size_t decompressedSize) { - auto ret = LZ4_decompress_safe(reinterpret_cast(in), - reinterpret_cast(decompressed), inSize, decompressedSize); + auto ret = LZ4_decompress_safe( + reinterpret_cast(in), + reinterpret_cast(decompressed), + inSize, + decompressedSize); if (ret <= 0 || ret != decompressedSize) doThrow("lz4 decompress: failed"); @@ -97,10 +103,13 @@ lz4Decompress(std::uint8_t const* in, std::size_t inSize, * @param decompressedSize Size of the decompressed buffer * @return size of the decompressed data */ -template +template std::size_t -lz4Decompress(InputStream& in, std::size_t inSize, - std::uint8_t* decompressed, std::size_t decompressedSize) +lz4Decompress( + InputStream& in, + std::size_t inSize, + std::uint8_t* decompressed, + std::size_t decompressedSize) { std::vector compressed; std::uint8_t const* chunk = nullptr; @@ -123,7 +132,9 @@ lz4Decompress(InputStream& in, std::size_t inSize, compressed.resize(inSize); } - chunkSize = chunkSize < (inSize - copiedInSize) ? chunkSize : (inSize - copiedInSize); + chunkSize = chunkSize < (inSize - copiedInSize) + ? chunkSize + : (inSize - copiedInSize); std::copy(chunk, chunk + chunkSize, compressed.data() + copiedInSize); @@ -140,14 +151,15 @@ lz4Decompress(InputStream& in, std::size_t inSize, if (in.ByteCount() > (currentBytes + copiedInSize)) in.BackUp(in.ByteCount() - currentBytes - copiedInSize); - if ((copiedInSize == 0 && chunkSize < inSize) || (copiedInSize > 0 && copiedInSize != inSize)) + if ((copiedInSize == 0 && chunkSize < inSize) || + (copiedInSize > 0 && copiedInSize != inSize)) doThrow("lz4 decompress: insufficient input size"); return lz4Decompress(chunk, inSize, decompressed, decompressedSize); } -} // compression +} // namespace compression_algorithms -} // ripple +} // namespace ripple -#endif //RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED +#endif // RIPPLED_COMPRESSIONALGORITHMS_H_INCLUDED diff --git a/src/ripple/basics/CountedObject.h b/src/ripple/basics/CountedObject.h index 01bc053532..cf57b2a309 100644 --- a/src/ripple/basics/CountedObject.h +++ b/src/ripple/basics/CountedObject.h @@ -31,12 +31,14 @@ namespace ripple { class CountedObjects { public: - static CountedObjects& getInstance () noexcept; + static CountedObjects& + getInstance() noexcept; - using Entry = std::pair ; - using List = std::vector ; + using Entry = std::pair; + using List = std::vector; - List getCounts (int minimumThreshold) const; + List + getCounts(int minimumThreshold) const; public: /** Implementation for @ref CountedObject. @@ -46,47 +48,53 @@ public: class CounterBase { public: - CounterBase () noexcept; + CounterBase() noexcept; - virtual ~CounterBase () noexcept; + virtual ~CounterBase() noexcept; - int increment () noexcept + int + increment() noexcept { return ++m_count; } - int decrement () noexcept + int + decrement() noexcept { return --m_count; } - int getCount () const noexcept + int + getCount() const noexcept { - return m_count.load (); + return m_count.load(); } - CounterBase* getNext () const noexcept + CounterBase* + getNext() const noexcept { return m_next; } - virtual char const* getName () const = 0; + virtual char const* + getName() const = 0; private: - virtual void checkPureVirtual () const = 0; + virtual void + checkPureVirtual() const = 0; protected: - std::atomic m_count; + std::atomic m_count; CounterBase* m_next; }; private: - CountedObjects () noexcept; - ~CountedObjects () noexcept = default; + CountedObjects() noexcept; + ~CountedObjects() noexcept = default; private: - std::atomic m_count; - std::atomic m_head; + std::atomic m_count; + std::atomic m_head; }; //------------------------------------------------------------------------------ @@ -102,39 +110,47 @@ template class CountedObject { public: - CountedObject () noexcept + CountedObject() noexcept { - getCounter ().increment (); + getCounter().increment(); } - CountedObject (CountedObject const&) noexcept + CountedObject(CountedObject const&) noexcept { - getCounter ().increment (); + getCounter().increment(); } - CountedObject& operator=(CountedObject const&) noexcept = default; + CountedObject& + operator=(CountedObject const&) noexcept = default; - ~CountedObject () noexcept + ~CountedObject() noexcept { - getCounter ().decrement (); + getCounter().decrement(); } private: class Counter : public CountedObjects::CounterBase { public: - Counter () noexcept { } - - char const* getName () const override + Counter() noexcept { - return Object::getCountedObjectName (); } - void checkPureVirtual () const override { } + char const* + getName() const override + { + return Object::getCountedObjectName(); + } + + void + checkPureVirtual() const override + { + } }; private: - static Counter& getCounter() noexcept + static Counter& + getCounter() noexcept { static_assert(std::is_nothrow_constructible{}, ""); static Counter c; @@ -142,6 +158,6 @@ private: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/DecayingSample.h b/src/ripple/basics/DecayingSample.h index a55b7e0a65..20a02572db 100644 --- a/src/ripple/basics/DecayingSample.h +++ b/src/ripple/basics/DecayingSample.h @@ -35,23 +35,22 @@ public: using value_type = typename Clock::duration::rep; using time_point = typename Clock::time_point; - DecayingSample () = delete; + DecayingSample() = delete; /** @param now Start time of DecayingSample. */ - explicit DecayingSample (time_point now) - : m_value (value_type()) - , m_when (now) + explicit DecayingSample(time_point now) : m_value(value_type()), m_when(now) { } /** Add a new sample. The value is first aged according to the specified time. */ - value_type add (value_type value, time_point now) + value_type + add(value_type value, time_point now) { - decay (now); + decay(now); m_value += value; return m_value / Window; } @@ -59,23 +58,26 @@ public: /** Retrieve the current value in normalized units. The samples are first aged according to the specified time. */ - value_type value (time_point now) + value_type + value(time_point now) { - decay (now); + decay(now); return m_value / Window; } private: // Apply exponential decay based on the specified time. - void decay (time_point now) + void + decay(time_point now) { if (now == m_when) return; if (m_value != value_type()) { - std::size_t elapsed = std::chrono::duration_cast< - std::chrono::seconds>(now - m_when).count(); + std::size_t elapsed = + std::chrono::duration_cast(now - m_when) + .count(); // A span larger than four times the window decays the // value to an insignificant amount so just reset it. @@ -112,40 +114,35 @@ class DecayWindow public: using time_point = typename Clock::time_point; - explicit - DecayWindow (time_point now) - : value_(0) - , when_(now) + explicit DecayWindow(time_point now) : value_(0), when_(now) { } void - add (double value, time_point now) + add(double value, time_point now) { decay(now); value_ += value; } double - value (time_point now) + value(time_point now) { decay(now); return value_ / HalfLife; } private: - static_assert(HalfLife > 0, - "half life must be positive"); + static_assert(HalfLife > 0, "half life must be positive"); void - decay (time_point now) + decay(time_point now) { if (now <= when_) return; using namespace std::chrono; - auto const elapsed = - duration(now - when_).count(); - value_ *= std::pow(2.0, - elapsed / HalfLife); + auto const elapsed = duration(now - when_).count(); + value_ *= std::pow(2.0, -elapsed / HalfLife); when_ = now; } @@ -153,6 +150,6 @@ private: time_point when_; }; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/FeeUnits.h b/src/ripple/basics/FeeUnits.h index 2af068313d..90116eed2a 100644 --- a/src/ripple/basics/FeeUnits.h +++ b/src/ripple/basics/FeeUnits.h @@ -26,10 +26,10 @@ #include #include -#include -#include #include +#include #include +#include namespace ripple { @@ -47,12 +47,10 @@ struct feelevelTag; used for calculations in this header. */ struct unitlessTag; -template +template using enable_if_unit_t = typename std::enable_if_t< - std::is_class_v && - std::is_object_v && - std::is_object_v ->; + std::is_class_v && std::is_object_v && + std::is_object_v>; /** `is_usable_unit_v` is checked to ensure that only values with known valid type tags can be used (sometimes transparently) in @@ -61,76 +59,69 @@ using enable_if_unit_t = typename std::enable_if_t< should not be added automatically unless determined to be appropriate. */ -template > +template > constexpr bool is_usable_unit_v = std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v; -template -class TaggedFee - : private boost::totally_ordered > - , private boost::additive > - , private boost::equality_comparable, T> - , private boost::dividable , T> - , private boost::modable , T> - , private boost::unit_steppable > +template +class TaggedFee : private boost::totally_ordered>, + private boost::additive>, + private boost::equality_comparable, T>, + private boost::dividable, T>, + private boost::modable, T>, + private boost::unit_steppable> { public: using unit_type = UnitTag; using value_type = T; + private: value_type fee_; protected: - template + template static constexpr bool is_compatible_v = - std::is_arithmetic_v && std::is_arithmetic_v && - std::is_convertible_v; + std::is_arithmetic_v&& std::is_arithmetic_v&& + std::is_convertible_v; - template > + template > static constexpr bool is_compatiblefee_v = - is_compatible_v && + is_compatible_v&& std::is_same_v; - template + template using enable_if_compatible_t = typename std::enable_if_t>; - template + template using enable_if_compatiblefee_t = typename std::enable_if_t>; public: - TaggedFee () = default; - constexpr TaggedFee (TaggedFee const& other) = default; - constexpr TaggedFee& operator= (TaggedFee const& other) = default; + TaggedFee() = default; + constexpr TaggedFee(TaggedFee const& other) = default; + constexpr TaggedFee& + operator=(TaggedFee const& other) = default; - constexpr - explicit - TaggedFee (beast::Zero) - : fee_ (0) + constexpr explicit TaggedFee(beast::Zero) : fee_(0) { } - constexpr - TaggedFee& - operator= (beast::Zero) + constexpr TaggedFee& operator=(beast::Zero) { fee_ = 0; return *this; } - constexpr - explicit - TaggedFee (value_type fee) - : fee_ (fee) + constexpr explicit TaggedFee(value_type fee) : fee_(fee) { } TaggedFee& - operator= (value_type fee) + operator=(value_type fee) { fee_ = fee; return *this; @@ -139,47 +130,44 @@ public: /** Instances with the same unit, and a type that is "safe" to covert to this one can be converted implicitly */ - template && - is_safetocasttovalue_v >> - constexpr - TaggedFee(TaggedFee const& fee) - : TaggedFee (safe_cast (fee.fee())) + template < + class Other, + class = std::enable_if_t< + is_compatible_v && + is_safetocasttovalue_v>> + constexpr TaggedFee(TaggedFee const& fee) + : TaggedFee(safe_cast(fee.fee())) { } - constexpr - TaggedFee - operator* (value_type const& rhs) const + constexpr TaggedFee + operator*(value_type const& rhs) const { - return TaggedFee{ fee_ * rhs }; + return TaggedFee{fee_ * rhs}; } - friend - constexpr - TaggedFee + friend constexpr TaggedFee operator*(value_type lhs, TaggedFee const& rhs) { // multiplication is commutative return rhs * lhs; } - constexpr - value_type - operator/ (TaggedFee const& rhs) const + constexpr value_type + operator/(TaggedFee const& rhs) const { return fee_ / rhs.fee_; } TaggedFee& - operator+= (TaggedFee const& other) + operator+=(TaggedFee const& other) { fee_ += other.fee(); return *this; } TaggedFee& - operator-= (TaggedFee const& other) + operator-=(TaggedFee const& other) { fee_ -= other.fee(); return *this; @@ -200,34 +188,33 @@ public: } TaggedFee& - operator*= (value_type const& rhs) + operator*=(value_type const& rhs) { fee_ *= rhs; return *this; } TaggedFee& - operator/= (value_type const& rhs) + operator/=(value_type const& rhs) { fee_ /= rhs; return *this; } - template - std::enable_if_t, - TaggedFee&> - operator%= (value_type const& rhs) + template + std::enable_if_t, TaggedFee&> + operator%=(value_type const& rhs) { fee_ %= rhs; return *this; } TaggedFee - operator- () const + operator-() const { - static_assert( std::is_signed_v, - "- operator illegal on unsigned fee types"); - return TaggedFee{ -fee_ }; + static_assert( + std::is_signed_v, "- operator illegal on unsigned fee types"); + return TaggedFee{-fee_}; } bool @@ -236,8 +223,7 @@ public: return fee_ == other.fee_; } - template > + template > bool operator==(TaggedFee const& other) const { @@ -250,8 +236,7 @@ public: return fee_ == other; } - template > + template > bool operator!=(TaggedFee const& other) const { @@ -265,33 +250,28 @@ public: } /** Returns true if the amount is not zero */ - explicit - constexpr - operator bool() const noexcept + explicit constexpr operator bool() const noexcept { return fee_ != 0; } /** Return the sign of the amount */ - constexpr - int + constexpr int signum() const noexcept { return (fee_ < 0) ? -1 : (fee_ ? 1 : 0); } /** Returns the number of drops */ - constexpr - value_type - fee () const + constexpr value_type + fee() const { return fee_; } - template - constexpr - double - decimalFromReference (TaggedFee reference) const + template + constexpr double + decimalFromReference(TaggedFee reference) const { return static_cast(fee_) / reference.fee(); } @@ -300,14 +280,15 @@ public: // known valid type tags can be converted to JSON. At the time // of implementation, that includes all known tags, but more may // be added in the future. - std::enable_if_t, - Json::Value> + std::enable_if_t, Json::Value> jsonClipped() const { if constexpr (std::is_integral_v) { - using jsontype = std::conditional_t, - Json::Int, Json::UInt>; + using jsontype = std::conditional_t< + std::is_signed_v, + Json::Int, + Json::UInt>; constexpr auto min = std::numeric_limits::min(); constexpr auto max = std::numeric_limits::max(); @@ -328,169 +309,177 @@ public: function unless the type has been abstracted away, e.g. in a templated function. */ - constexpr - value_type - value () const + constexpr value_type + value() const { return fee_; } - friend - std::istream& - operator>> (std::istream& s, TaggedFee& val) + friend std::istream& + operator>>(std::istream& s, TaggedFee& val) { s >> val.fee_; return s; } - }; // Output Fees as just their numeric value. -template +template std::basic_ostream& -operator<<(std::basic_ostream& os, - const TaggedFee& q) +operator<<(std::basic_ostream& os, const TaggedFee& q) { return os << q.value(); } -template +template std::string -to_string (TaggedFee const& amount) +to_string(TaggedFee const& amount) { - return std::to_string (amount.fee ()); + return std::to_string(amount.fee()); } -template > +template > constexpr bool can_muldiv_source_v = std::is_convertible_v; -template > +template > constexpr bool can_muldiv_dest_v = - can_muldiv_source_v && // Dest is also a source - std::is_convertible_v && + can_muldiv_source_v&& // Dest is also a source + std::is_convertible_v && sizeof(typename Dest::value_type) >= sizeof(std::uint64_t); -template, class = enable_if_unit_t> constexpr bool can_muldiv_sources_v = - can_muldiv_source_v && - can_muldiv_source_v && - std::is_same_v; + can_muldiv_source_v&& can_muldiv_source_v&& std:: + is_same_v; -template, class = enable_if_unit_t, class = enable_if_unit_t> constexpr bool can_muldiv_v = - can_muldiv_sources_v && - can_muldiv_dest_v; - // Source and Dest can be the same by default + can_muldiv_sources_v&& can_muldiv_dest_v; +// Source and Dest can be the same by default -template, class = enable_if_unit_t, class = enable_if_unit_t> -constexpr bool can_muldiv_commute_v = - can_muldiv_v && - ! std::is_same_v; +constexpr bool can_muldiv_commute_v = can_muldiv_v && + !std::is_same_v; -template +template using enable_muldiv_source_t = - typename std::enable_if_t< can_muldiv_source_v >; + typename std::enable_if_t>; -template -using enable_muldiv_dest_t = - typename std::enable_if_t< can_muldiv_dest_v >; +template +using enable_muldiv_dest_t = typename std::enable_if_t>; -template +template using enable_muldiv_sources_t = - typename std::enable_if_t< can_muldiv_sources_v >; + typename std::enable_if_t>; -template +template using enable_muldiv_t = - typename std::enable_if_t< can_muldiv_v >; + typename std::enable_if_t>; -template +template using enable_muldiv_commute_t = - typename std::enable_if_t< can_muldiv_commute_v >; + typename std::enable_if_t>; -template +template TaggedFee scalar(T value) { - return TaggedFee{ value }; + return TaggedFee{value}; } -template > +template < + class Source1, + class Source2, + class Dest, + class = enable_muldiv_t> std::pair mulDivU(Source1 value, Dest mul, Source2 div) { // Fees can never be negative in any context. - if(value.value() < 0 || mul.value() < 0 || div.value() < 0) + if (value.value() < 0 || mul.value() < 0 || div.value() < 0) { // split the asserts so if one hits, the user can tell which // without a debugger. assert(value.value() >= 0); assert(mul.value() >= 0); assert(div.value() >= 0); - return { false, Dest{ 0 } }; + return {false, Dest{0}}; } using desttype = typename Dest::value_type; - constexpr auto max = - std::numeric_limits::max(); + constexpr auto max = std::numeric_limits::max(); // Shortcuts, since these happen a lot in the real world if (value == div) - return { true, mul }; + return {true, mul}; if (mul.value() == div.value()) { if (value.value() > max) - return{ false, Dest{max} }; - return { true, - Dest{ static_cast(value.value()) } }; + return {false, Dest{max}}; + return {true, Dest{static_cast(value.value())}}; } using namespace boost::multiprecision; uint128_t product; - product = multiply(product, + product = multiply( + product, static_cast(value.value()), static_cast(mul.value())); auto quotient = product / div.value(); if (quotient > max) - return { false, Dest{ max } }; + return {false, Dest{max}}; - return { true, Dest{ static_cast(quotient) } }; + return {true, Dest{static_cast(quotient)}}; } -} // feeunit +} // namespace feeunit -template +template using FeeUnit = feeunit::TaggedFee; using FeeUnit32 = FeeUnit; using FeeUnit64 = FeeUnit; -template +template using FeeLevel = feeunit::TaggedFee; using FeeLevel64 = FeeLevel; using FeeLevelDouble = FeeLevel; -template > +template < + class Source1, + class Source2, + class Dest, + class = feeunit::enable_muldiv_t> std::pair mulDiv(Source1 value, Dest mul, Source2 div) { return feeunit::mulDivU(value, mul, div); } -template > +template < + class Source1, + class Source2, + class Dest, + class = feeunit::enable_muldiv_commute_t> std::pair mulDiv(Dest value, Source1 mul, Source2 div) { @@ -498,20 +487,16 @@ mulDiv(Dest value, Source1 mul, Source2 div) return feeunit::mulDivU(mul, value, div); } -template > +template > std::pair -mulDiv(std::uint64_t value, - Dest mul, - std::uint64_t div) +mulDiv(std::uint64_t value, Dest mul, std::uint64_t div) { // Give the scalars a non-tag so the // unit-handling version gets called. return feeunit::mulDivU(feeunit::scalar(value), mul, feeunit::scalar(div)); } -template > +template > std::pair mulDiv(Dest value, std::uint64_t mul, std::uint64_t div) { @@ -519,21 +504,23 @@ mulDiv(Dest value, std::uint64_t mul, std::uint64_t div) return mulDiv(mul, value, div); } -template > +template < + class Source1, + class Source2, + class = feeunit::enable_muldiv_sources_t> std::pair -mulDiv(Source1 value, - std::uint64_t mul, - Source2 div) +mulDiv(Source1 value, std::uint64_t mul, Source2 div) { // Give the scalars a dimensionless unit so the // unit-handling version gets called. auto unitresult = feeunit::mulDivU(value, feeunit::scalar(mul), div); - return { unitresult.first, unitresult.second.value() }; + return {unitresult.first, unitresult.second.value()}; } -template > +template < + class Source1, + class Source2, + class = feeunit::enable_muldiv_sources_t> std::pair mulDiv(std::uint64_t value, Source1 mul, Source2 div) { @@ -542,35 +529,29 @@ mulDiv(std::uint64_t value, Source1 mul, Source2 div) } template -constexpr -std::enable_if_t -< +constexpr std::enable_if_t< std::is_same_v && - std::is_integral_v && - std::is_integral_v, - Dest -> + std::is_integral_v && + std::is_integral_v, + Dest> safe_cast(Src s) noexcept { // Dest may not have an explicit value constructor - return Dest{ safe_cast(s.value()) }; + return Dest{safe_cast(s.value())}; } template -constexpr -std::enable_if_t -< +constexpr std::enable_if_t< std::is_same_v && - std::is_integral_v && - std::is_integral_v, - Dest -> + std::is_integral_v && + std::is_integral_v, + Dest> unsafe_cast(Src s) noexcept { // Dest may not have an explicit value constructor - return Dest{ unsafe_cast(s.value()) }; + return Dest{unsafe_cast(s.value())}; } -} // ripple +} // namespace ripple -#endif // BASICS_FEES_H_INCLUDED +#endif // BASICS_FEES_H_INCLUDED diff --git a/src/ripple/basics/FileUtilities.h b/src/ripple/basics/FileUtilities.h index de8c9f9306..6970ba7520 100644 --- a/src/ripple/basics/FileUtilities.h +++ b/src/ripple/basics/FileUtilities.h @@ -20,21 +20,24 @@ #ifndef RIPPLE_BASICS_FILEUTILITIES_H_INCLUDED #define RIPPLE_BASICS_FILEUTILITIES_H_INCLUDED -#include #include #include +#include -namespace ripple -{ +namespace ripple { -std::string getFileContents(boost::system::error_code& ec, +std::string +getFileContents( + boost::system::error_code& ec, boost::filesystem::path const& sourcePath, boost::optional maxSize = boost::none); -void writeFileContents(boost::system::error_code& ec, +void +writeFileContents( + boost::system::error_code& ec, boost::filesystem::path const& destPath, std::string const& contents); -} +} // namespace ripple #endif diff --git a/src/ripple/basics/IOUAmount.h b/src/ripple/basics/IOUAmount.h index c888c15807..46a37d23a1 100644 --- a/src/ripple/basics/IOUAmount.h +++ b/src/ripple/basics/IOUAmount.h @@ -38,9 +38,8 @@ namespace ripple { if the amount exceeds the largest representable amount, but underflows will silently trunctate to zero. */ -class IOUAmount - : private boost::totally_ordered - , private boost::additive +class IOUAmount : private boost::totally_ordered, + private boost::additive { private: std::int64_t mantissa_; @@ -53,27 +52,26 @@ private: that are too small to be represented normalize to 0. */ void - normalize (); + normalize(); public: - IOUAmount () = default; - IOUAmount (IOUAmount const& other) = default; - IOUAmount&operator= (IOUAmount const& other) = default; + IOUAmount() = default; + IOUAmount(IOUAmount const& other) = default; + IOUAmount& + operator=(IOUAmount const& other) = default; - IOUAmount (beast::Zero) + IOUAmount(beast::Zero) { *this = beast::zero; } - IOUAmount (std::int64_t mantissa, int exponent) - : mantissa_ (mantissa) - , exponent_ (exponent) + IOUAmount(std::int64_t mantissa, int exponent) + : mantissa_(mantissa), exponent_(exponent) { - normalize (); + normalize(); } - IOUAmount& - operator= (beast::Zero) + IOUAmount& operator=(beast::Zero) { // The -100 is used to allow 0 to sort less than small positive values // which will have a large negative exponent. @@ -83,34 +81,32 @@ public: } IOUAmount& - operator+= (IOUAmount const& other); + operator+=(IOUAmount const& other); IOUAmount& - operator-= (IOUAmount const& other) + operator-=(IOUAmount const& other) { *this += -other; return *this; } IOUAmount - operator- () const + operator-() const { - return { -mantissa_, exponent_ }; + return {-mantissa_, exponent_}; } bool operator==(IOUAmount const& other) const { - return exponent_ == other.exponent_ && - mantissa_ == other.mantissa_; + return exponent_ == other.exponent_ && mantissa_ == other.mantissa_; } bool operator<(IOUAmount const& other) const; /** Returns true if the amount is not zero */ - explicit - operator bool() const noexcept + explicit operator bool() const noexcept { return mantissa_ != 0; } @@ -136,7 +132,7 @@ public: }; std::string -to_string (IOUAmount const& amount); +to_string(IOUAmount const& amount); /* Return num*amt/den This function keeps more precision than computing @@ -144,12 +140,12 @@ to_string (IOUAmount const& amount); dividing by den. */ IOUAmount -mulRatio ( +mulRatio( IOUAmount const& amt, std::uint32_t num, std::uint32_t den, bool roundUp); -} +} // namespace ripple #endif diff --git a/src/ripple/basics/KeyCache.h b/src/ripple/basics/KeyCache.h index 73a1d196ff..9f62663104 100644 --- a/src/ripple/basics/KeyCache.h +++ b/src/ripple/basics/KeyCache.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_BASICS_KEYCACHE_H_INCLUDED #define RIPPLE_BASICS_KEYCACHE_H_INCLUDED -#include #include +#include #include #include #include @@ -36,29 +36,31 @@ namespace ripple { */ template < class Key, - class Hash = hardened_hash <>, - class KeyEqual = std::equal_to , - //class Allocator = std::allocator >, - class Mutex = std::mutex -> + class Hash = hardened_hash<>, + class KeyEqual = std::equal_to, + // class Allocator = std::allocator >, + class Mutex = std::mutex> class KeyCache { public: using key_type = Key; - using clock_type = beast::abstract_clock ; + using clock_type = beast::abstract_clock; private: struct Stats { template - Stats (std::string const& prefix, Handler const& handler, + Stats( + std::string const& prefix, + Handler const& handler, beast::insight::Collector::ptr const& collector) - : hook (collector->make_hook (handler)) - , size (collector->make_gauge (prefix, "size")) - , hit_rate (collector->make_gauge (prefix, "hit_rate")) - , hits (0) - , misses (0) - { } + : hook(collector->make_hook(handler)) + , size(collector->make_gauge(prefix, "size")) + , hit_rate(collector->make_gauge(prefix, "hit_rate")) + , hits(0) + , misses(0) + { + } beast::insight::Hook hook; beast::insight::Gauge size; @@ -70,15 +72,15 @@ private: struct Entry { - explicit Entry (clock_type::time_point const& last_access_) - : last_access (last_access_) + explicit Entry(clock_type::time_point const& last_access_) + : last_access(last_access_) { } clock_type::time_point last_access; }; - using map_type = hardened_hash_map ; + using map_type = hardened_hash_map; using iterator = typename map_type::iterator; public: @@ -99,62 +101,71 @@ public: @param size The initial target size. @param age The initial expiration time. */ - KeyCache (std::string const& name, clock_type& clock, - beast::insight::Collector::ptr const& collector, size_type target_size = 0, - std::chrono::seconds expiration = std::chrono::minutes{2}) - : m_stats (name, - std::bind (&KeyCache::collect_metrics, this), - collector) - , m_clock (clock) - , m_name (name) - , m_target_size (target_size) - , m_target_age (expiration) + KeyCache( + std::string const& name, + clock_type& clock, + beast::insight::Collector::ptr const& collector, + size_type target_size = 0, + std::chrono::seconds expiration = std::chrono::minutes{2}) + : m_stats(name, std::bind(&KeyCache::collect_metrics, this), collector) + , m_clock(clock) + , m_name(name) + , m_target_size(target_size) + , m_target_age(expiration) { } // VFALCO TODO Use a forwarding constructor call here - KeyCache (std::string const& name, clock_type& clock, + KeyCache( + std::string const& name, + clock_type& clock, size_type target_size = 0, std::chrono::seconds expiration = std::chrono::minutes{2}) - : m_stats (name, - std::bind (&KeyCache::collect_metrics, this), - beast::insight::NullCollector::New ()) - , m_clock (clock) - , m_name (name) - , m_target_size (target_size) - , m_target_age (expiration) + : m_stats( + name, + std::bind(&KeyCache::collect_metrics, this), + beast::insight::NullCollector::New()) + , m_clock(clock) + , m_name(name) + , m_target_size(target_size) + , m_target_age(expiration) { } //-------------------------------------------------------------------------- /** Retrieve the name of this object. */ - std::string const& name () const + std::string const& + name() const { return m_name; } /** Return the clock associated with the cache. */ - clock_type& clock () + clock_type& + clock() { return m_clock; } /** Returns the number of items in the container. */ - size_type size () const + size_type + size() const { - std::lock_guard lock (m_mutex); - return m_map.size (); + std::lock_guard lock(m_mutex); + return m_map.size(); } /** Empty the cache */ - void clear () + void + clear() { - std::lock_guard lock (m_mutex); - m_map.clear (); + std::lock_guard lock(m_mutex); + m_map.clear(); } - void reset () + void + reset() { std::lock_guard lock(m_mutex); m_map.clear(); @@ -162,15 +173,17 @@ public: m_stats.misses = 0; } - void setTargetSize (size_type s) + void + setTargetSize(size_type s) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_target_size = s; } - void setTargetAge (std::chrono::seconds s) + void + setTargetAge(std::chrono::seconds s) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_target_age = s; } @@ -178,11 +191,12 @@ public: Does not update the last access time. */ template - bool exists (KeyComparable const& key) const + bool + exists(KeyComparable const& key) const { - std::lock_guard lock (m_mutex); - typename map_type::const_iterator const iter (m_map.find (key)); - if (iter != m_map.end ()) + std::lock_guard lock(m_mutex); + typename map_type::const_iterator const iter(m_map.find(key)); + if (iter != m_map.end()) { ++m_stats.hits; return true; @@ -195,13 +209,15 @@ public: The last access time is refreshed in all cases. @return `true` If the key was newly inserted. */ - bool insert (Key const& key) + bool + insert(Key const& key) { - std::lock_guard lock (m_mutex); - clock_type::time_point const now (m_clock.now ()); - auto [it, inserted] = m_map.emplace ( - std::piecewise_construct, std::forward_as_tuple (key), - std::forward_as_tuple (now)); + std::lock_guard lock(m_mutex); + clock_type::time_point const now(m_clock.now()); + auto [it, inserted] = m_map.emplace( + std::piecewise_construct, + std::forward_as_tuple(key), + std::forward_as_tuple(now)); if (!inserted) { it->second.last_access = now; @@ -214,16 +230,17 @@ public: @return `true` If the key was found. */ template - bool touch_if_exists (KeyComparable const& key) + bool + touch_if_exists(KeyComparable const& key) { - std::lock_guard lock (m_mutex); - iterator const iter (m_map.find (key)); - if (iter == m_map.end ()) + std::lock_guard lock(m_mutex); + iterator const iter(m_map.find(key)); + if (iter == m_map.end()) { ++m_stats.misses; return false; } - iter->second.last_access = m_clock.now (); + iter->second.last_access = m_clock.now(); ++m_stats.hits; return true; } @@ -232,10 +249,11 @@ public: @param key The key to remove. @return `false` If the key was not found. */ - bool erase (key_type const& key) + bool + erase(key_type const& key) { - std::lock_guard lock (m_mutex); - if (m_map.erase (key) > 0) + std::lock_guard lock(m_mutex); + if (m_map.erase(key) > 0) { ++m_stats.hits; return true; @@ -245,32 +263,30 @@ public: } /** Remove stale entries from the cache. */ - void sweep () + void + sweep() { - clock_type::time_point const now (m_clock.now ()); + clock_type::time_point const now(m_clock.now()); clock_type::time_point when_expire; - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - if (m_target_size == 0 || - (m_map.size () <= m_target_size)) + if (m_target_size == 0 || (m_map.size() <= m_target_size)) { when_expire = now - m_target_age; } else { - when_expire = now - - m_target_age * m_target_size / m_map.size(); + when_expire = now - m_target_age * m_target_size / m_map.size(); - clock_type::duration const minimumAge ( - std::chrono::seconds (1)); + clock_type::duration const minimumAge(std::chrono::seconds(1)); if (when_expire > (now - minimumAge)) when_expire = now - minimumAge; } - iterator it = m_map.begin (); + iterator it = m_map.begin(); - while (it != m_map.end ()) + while (it != m_map.end()) { if (it->second.last_access > now) { @@ -279,7 +295,7 @@ public: } else if (it->second.last_access <= when_expire) { - it = m_map.erase (it); + it = m_map.erase(it); } else { @@ -289,23 +305,24 @@ public: } private: - void collect_metrics () + void + collect_metrics() { - m_stats.size.set (size ()); + m_stats.size.set(size()); { - beast::insight::Gauge::value_type hit_rate (0); + beast::insight::Gauge::value_type hit_rate(0); { - std::lock_guard lock (m_mutex); - auto const total (m_stats.hits + m_stats.misses); + std::lock_guard lock(m_mutex); + auto const total(m_stats.hits + m_stats.misses); if (total != 0) hit_rate = (m_stats.hits * 100) / total; } - m_stats.hit_rate.set (hit_rate); + m_stats.hit_rate.set(hit_rate); } } }; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/LocalValue.h b/src/ripple/basics/LocalValue.h index 77c41d2d23..41556f34f2 100644 --- a/src/ripple/basics/LocalValue.h +++ b/src/ripple/basics/LocalValue.h @@ -37,7 +37,8 @@ struct LocalValues struct BasicValue { virtual ~BasicValue() = default; - virtual void* get() = 0; + virtual void* + get() = 0; }; template @@ -46,9 +47,12 @@ struct LocalValues T t_; Value() = default; - explicit Value(T const& t) : t_(t) {} + explicit Value(T const& t) : t_(t) + { + } - void* get() override + void* + get() override { return &t_; } @@ -57,42 +61,41 @@ struct LocalValues // Keys are the address of a LocalValue. std::unordered_map> values; - static - inline - void + static inline void cleanup(LocalValues* lvs) { - if (lvs && ! lvs->onCoro) + if (lvs && !lvs->onCoro) delete lvs; } }; -template +template boost::thread_specific_ptr& getLocalValues() { - static boost::thread_specific_ptr< - detail::LocalValues> tsp(&detail::LocalValues::cleanup); + static boost::thread_specific_ptr tsp( + &detail::LocalValues::cleanup); return tsp; } -} // detail +} // namespace detail template class LocalValue { public: template - LocalValue(Args&&... args) - : t_(std::forward(args)...) + LocalValue(Args&&... args) : t_(std::forward(args)...) { } /** Stores instance of T specific to the calling coroutine or thread. */ - T& operator*(); + T& + operator*(); /** Stores instance of T specific to the calling coroutine or thread. */ - T* operator->() + T* + operator->() { return &**this; } @@ -106,7 +109,7 @@ T& LocalValue::operator*() { auto lvs = detail::getLocalValues().get(); - if (! lvs) + if (!lvs) { lvs = new detail::LocalValues(); lvs->onCoro = false; @@ -119,10 +122,11 @@ LocalValue::operator*() return *reinterpret_cast(iter->second->get()); } - return *reinterpret_cast(lvs->values.emplace(this, - std::make_unique>(t_)). - first->second->get()); + return *reinterpret_cast( + lvs->values + .emplace(this, std::make_unique>(t_)) + .first->second->get()); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/Log.h b/src/ripple/basics/Log.h index 9f93ef53ee..690ab082f8 100644 --- a/src/ripple/basics/Log.h +++ b/src/ripple/basics/Log.h @@ -21,8 +21,8 @@ #define RIPPLE_BASICS_LOG_H_INCLUDED #include -#include #include +#include #include #include #include @@ -32,17 +32,16 @@ namespace ripple { // DEPRECATED use beast::severities::Severity instead -enum LogSeverity -{ - lsINVALID = -1, // used to indicate an invalid severity - lsTRACE = 0, // Very low-level progress information, details inside - // an operation - lsDEBUG = 1, // Function-level progress information, operations - lsINFO = 2, // Server-level progress information, major operations - lsWARNING = 3, // Conditions that warrant human attention, may indicate - // a problem - lsERROR = 4, // A condition that indicates a problem - lsFATAL = 5 // A severe condition that indicates a server problem +enum LogSeverity { + lsINVALID = -1, // used to indicate an invalid severity + lsTRACE = 0, // Very low-level progress information, details inside + // an operation + lsDEBUG = 1, // Function-level progress information, operations + lsINFO = 2, // Server-level progress information, major operations + lsWARNING = 3, // Conditions that warrant human attention, may indicate + // a problem + lsERROR = 4, // A condition that indicates a problem + lsFATAL = 5 // A severe condition that indicates a server problem }; /** Manages partitions for logging. */ @@ -56,15 +55,18 @@ private: std::string partition_; public: - Sink (std::string const& partition, - beast::severities::Severity thresh, Logs& logs); + Sink( + std::string const& partition, + beast::severities::Severity thresh, + Logs& logs); - Sink (Sink const&) = delete; - Sink& operator= (Sink const&) = delete; + Sink(Sink const&) = delete; + Sink& + operator=(Sink const&) = delete; void - write (beast::severities::Severity level, - std::string const& text) override; + write(beast::severities::Severity level, std::string const& text) + override; }; /** Manages a system file containing logged output. @@ -81,18 +83,19 @@ private: A system file may be associated later with @ref open. @see open */ - File (); + File(); /** Destroy the object. If a system file is associated, it will be flushed and closed. */ - ~File () = default; + ~File() = default; /** Determine if a system file is associated with the log. @return `true` if a system file is associated and opened for writing. */ - bool isOpen () const noexcept; + bool + isOpen() const noexcept; /** Associate a system file with the log. If the file does not exist an attempt is made to create it @@ -106,49 +109,58 @@ private: // can be either wchar or char based depending on platform. // TODO Replace with beast::File // - bool open (boost::filesystem::path const& path); + bool + open(boost::filesystem::path const& path); /** Close and re-open the system file associated with the log This assists in interoperating with external log management tools. @return `true` if the file was opened. */ - bool closeAndReopen (); + bool + closeAndReopen(); /** Close the system file if it is open. */ - void close (); + void + close(); /** write to the log file. Does nothing if there is no associated system file. */ - void write (char const* text); + void + write(char const* text); /** write to the log file and append an end of line marker. Does nothing if there is no associated system file. */ - void writeln (char const* text); + void + writeln(char const* text); /** Write to the log file using std::string. */ /** @{ */ - void write (std::string const& str) + void + write(std::string const& str) { - write (str.c_str ()); + write(str.c_str()); } - void writeln (std::string const& str) + void + writeln(std::string const& str) { - writeln (str.c_str ()); + writeln(str.c_str()); } /** @} */ private: - std::unique_ptr m_stream; + std::unique_ptr m_stream; boost::filesystem::path m_path; }; std::mutex mutable mutex_; - std::map , - boost::beast::iless> sinks_; + boost::beast::iless> + sinks_; beast::severities::Severity thresh_; File file_; bool silent_ = false; @@ -156,35 +168,39 @@ private: public: Logs(beast::severities::Severity level); - Logs (Logs const&) = delete; - Logs& operator= (Logs const&) = delete; + Logs(Logs const&) = delete; + Logs& + operator=(Logs const&) = delete; virtual ~Logs() = default; bool - open (boost::filesystem::path const& pathToLogFile); + open(boost::filesystem::path const& pathToLogFile); beast::Journal::Sink& - get (std::string const& name); + get(std::string const& name); beast::Journal::Sink& - operator[] (std::string const& name); + operator[](std::string const& name); beast::Journal - journal (std::string const& name); + journal(std::string const& name); beast::severities::Severity threshold() const; void - threshold (beast::severities::Severity thresh); + threshold(beast::severities::Severity thresh); std::vector> partition_severities() const; void - write (beast::severities::Severity level, std::string const& partition, - std::string const& text, bool console); + write( + beast::severities::Severity level, + std::string const& partition, + std::string const& text, + bool console); std::string rotate(); @@ -195,51 +211,53 @@ public: * @param bSilent Set flag accordingly. */ void - silent (bool bSilent) + silent(bool bSilent) { silent_ = bSilent; } - virtual - std::unique_ptr - makeSink(std::string const& partition, + virtual std::unique_ptr + makeSink( + std::string const& partition, beast::severities::Severity startingLevel); public: - static - LogSeverity - fromSeverity (beast::severities::Severity level); + static LogSeverity + fromSeverity(beast::severities::Severity level); - static - beast::severities::Severity - toSeverity (LogSeverity level); + static beast::severities::Severity + toSeverity(LogSeverity level); - static - std::string - toString (LogSeverity s); + static std::string + toString(LogSeverity s); - static - LogSeverity - fromString (std::string const& s); + static LogSeverity + fromString(std::string const& s); private: - enum - { + enum { // Maximum line length for log messages. // If the message exceeds this length it will be truncated with elipses. maximumMessageCharacters = 12 * 1024 }; - static - void - format (std::string& output, std::string const& message, - beast::severities::Severity severity, std::string const& partition); + static void + format( + std::string& output, + std::string const& message, + beast::severities::Severity severity, + std::string const& partition); }; // Wraps a Journal::Stream to skip evaluation of // expensive argument lists if the stream is not active. #ifndef JLOG -#define JLOG(x) if (!x) { } else x +#define JLOG(x) \ + if (!x) \ + { \ + } \ + else \ + x #endif //------------------------------------------------------------------------------ @@ -251,8 +269,7 @@ private: @return unique_ptr to the previous Sink. nullptr if there was no Sink. */ std::unique_ptr -setDebugLogSink( - std::unique_ptr sink); +setDebugLogSink(std::unique_ptr sink); /** Returns a debug journal. The journal may drain to a null sink, so its output @@ -262,6 +279,6 @@ setDebugLogSink( beast::Journal debugLog(); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/PerfLog.h b/src/ripple/basics/PerfLog.h index 72c314660c..44d5c940e6 100644 --- a/src/ripple/basics/PerfLog.h +++ b/src/ripple/basics/PerfLog.h @@ -21,15 +21,17 @@ #define RIPPLE_BASICS_PERFLOG_H #include -#include #include +#include #include #include #include #include #include -namespace beast { class Journal; } +namespace beast { +class Journal; +} namespace ripple { namespace perf { @@ -59,7 +61,7 @@ public: { boost::filesystem::path perfLog; // log_interval is in milliseconds to support faster testing. - milliseconds logInterval {seconds(1)}; + milliseconds logInterval{seconds(1)}; }; virtual ~PerfLog() = default; @@ -70,8 +72,8 @@ public: * @param method RPC command * @param requestId Unique identifier to track command */ - virtual void rpcStart(std::string const& method, - std::uint64_t requestId) = 0; + virtual void + rpcStart(std::string const& method, std::uint64_t requestId) = 0; /** * Log successful finish of RPC call @@ -79,8 +81,8 @@ public: * @param method RPC command * @param requestId Unique identifier to track command */ - virtual void rpcFinish(std::string const& method, - std::uint64_t requestId) = 0; + virtual void + rpcFinish(std::string const& method, std::uint64_t requestId) = 0; /** * Log errored RPC call @@ -88,15 +90,16 @@ public: * @param method RPC command * @param requestId Unique identifier to track command */ - virtual void rpcError(std::string const& method, - std::uint64_t requestId) = 0; + virtual void + rpcError(std::string const& method, std::uint64_t requestId) = 0; /** * Log queued job * * @param type Job type */ - virtual void jobQueue(JobType const type) = 0; + virtual void + jobQueue(JobType const type) = 0; /** * Log job executing @@ -106,7 +109,9 @@ public: * @param startTime Time that execution began * @param instance JobQueue worker thread instance */ - virtual void jobStart(JobType const type, + virtual void + jobStart( + JobType const type, microseconds dur, steady_time_point startTime, int instance) = 0; @@ -118,53 +123,58 @@ public: * @param dur Duration running in microseconds * @param instance Jobqueue worker thread instance */ - virtual void jobFinish(JobType const type, - microseconds dur, int instance) = 0; + virtual void + jobFinish(JobType const type, microseconds dur, int instance) = 0; /** * Render performance counters in Json * * @return Counters Json object */ - virtual Json::Value countersJson() const = 0; + virtual Json::Value + countersJson() const = 0; /** * Render currently executing jobs and RPC calls and durations in Json * * @return Current executing jobs and RPC calls and durations */ - virtual Json::Value currentJson() const = 0; + virtual Json::Value + currentJson() const = 0; /** * Ensure enough room to store each currently executing job * * @param resize Number of JobQueue worker threads */ - virtual void resizeJobs(int const resize) = 0; + virtual void + resizeJobs(int const resize) = 0; /** * Rotate perf log file */ - virtual void rotate() = 0; + virtual void + rotate() = 0; }; -} //perf +} // namespace perf class Section; class Stoppable; namespace perf { -PerfLog::Setup setup_PerfLog(Section const& section, - boost::filesystem::path const& configDir); +PerfLog::Setup +setup_PerfLog(Section const& section, boost::filesystem::path const& configDir); -std::unique_ptr make_PerfLog( +std::unique_ptr +make_PerfLog( PerfLog::Setup const& setup, Stoppable& parent, beast::Journal journal, std::function&& signalStop); -} // perf -} // ripple +} // namespace perf +} // namespace ripple -#endif //RIPPLE_BASICS_PERFLOG_H +#endif // RIPPLE_BASICS_PERFLOG_H diff --git a/src/ripple/basics/RangeSet.h b/src/ripple/basics/RangeSet.h index 4e00a4627e..cbd25815f8 100644 --- a/src/ripple/basics/RangeSet.h +++ b/src/ripple/basics/RangeSet.h @@ -29,8 +29,7 @@ #include -namespace ripple -{ +namespace ripple { /** A closed interval over the domain T. @@ -69,7 +68,6 @@ range(T low, T high) template using RangeSet = boost::icl::interval_set>; - /** Convert a ClosedInterval to a styled string The styled string is @@ -80,7 +78,8 @@ using RangeSet = boost::icl::interval_set>; @return The style string */ template -std::string to_string(ClosedInterval const & ci) +std::string +to_string(ClosedInterval const& ci) { if (ci.first() == ci.last()) return std::to_string(ci.first()); @@ -96,14 +95,15 @@ std::string to_string(ClosedInterval const & ci) @return The styled string */ template -std::string to_string(RangeSet const & rs) +std::string +to_string(RangeSet const& rs) { using ripple::to_string; if (rs.empty()) return "empty"; std::string res = ""; - for (auto const & interval : rs) + for (auto const& interval : rs) { if (!res.empty()) res += ","; @@ -127,7 +127,7 @@ from_string(RangeSet& rs, std::string const& s) { std::vector intervals; std::vector tokens; - bool result {true}; + bool result{true}; boost::split(tokens, s, boost::algorithm::is_any_of(",")); for (auto const& t : tokens) @@ -135,32 +135,30 @@ from_string(RangeSet& rs, std::string const& s) boost::split(intervals, t, boost::algorithm::is_any_of("-")); switch (intervals.size()) { - case 1: - { - T front; - if (!beast::lexicalCastChecked(front, intervals.front())) - result = false; - else - rs.insert(front); - break; - } - case 2: - { - T front; - if (!beast::lexicalCastChecked(front, intervals.front())) - result = false; - else - { - T back; - if (!beast::lexicalCastChecked(back, intervals.back())) + case 1: { + T front; + if (!beast::lexicalCastChecked(front, intervals.front())) result = false; else - rs.insert(range(front, back)); + rs.insert(front); + break; } - break; - } - default: - result = false; + case 2: { + T front; + if (!beast::lexicalCastChecked(front, intervals.front())) + result = false; + else + { + T back; + if (!beast::lexicalCastChecked(back, intervals.back())) + result = false; + else + rs.insert(range(front, back)); + } + break; + } + default: + result = false; } if (!result) @@ -183,11 +181,11 @@ from_string(RangeSet& rs, std::string const& s) */ template boost::optional -prevMissing(RangeSet const & rs, T t, T minVal = 0) +prevMissing(RangeSet const& rs, T t, T minVal = 0) { if (rs.empty() || t == minVal) return boost::none; - RangeSet tgt{ ClosedInterval{minVal, t - 1} }; + RangeSet tgt{ClosedInterval{minVal, t - 1}}; tgt -= rs; if (tgt.empty()) return boost::none; @@ -196,5 +194,4 @@ prevMissing(RangeSet const & rs, T t, T minVal = 0) } // namespace ripple - #endif diff --git a/src/ripple/basics/Resolver.h b/src/ripple/basics/Resolver.h index 1bc0d7766d..abf0400570 100644 --- a/src/ripple/basics/Resolver.h +++ b/src/ripple/basics/Resolver.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_BASICS_RESOLVER_H_INCLUDED #define RIPPLE_BASICS_RESOLVER_H_INCLUDED -#include #include +#include #include @@ -30,19 +30,22 @@ namespace ripple { class Resolver { public: - using HandlerType = std::function < - void (std::string, std::vector ) >; + using HandlerType = + std::function)>; - virtual ~Resolver () = 0; + virtual ~Resolver() = 0; /** Issue an asynchronous stop request. */ - virtual void stop_async () = 0; + virtual void + stop_async() = 0; /** Issue a synchronous stop request. */ - virtual void stop () = 0; + virtual void + stop() = 0; /** Issue a synchronous start request. */ - virtual void start () = 0; + virtual void + start() = 0; /** resolve all hostnames on the list @param names the names to be resolved @@ -50,17 +53,19 @@ public: */ /** @{ */ template - void resolve (std::vector const& names, Handler handler) + void + resolve(std::vector const& names, Handler handler) { - resolve (names, HandlerType (handler)); + resolve(names, HandlerType(handler)); } - virtual void resolve ( - std::vector const& names, + virtual void + resolve( + std::vector const& names, HandlerType const& handler) = 0; /** @} */ }; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/ResolverAsio.h b/src/ripple/basics/ResolverAsio.h index 99ebee89e2..191cdc097a 100644 --- a/src/ripple/basics/ResolverAsio.h +++ b/src/ripple/basics/ResolverAsio.h @@ -31,11 +31,10 @@ class ResolverAsio : public Resolver public: explicit ResolverAsio() = default; - static - std::unique_ptr New ( - boost::asio::io_service&, beast::Journal); + static std::unique_ptr + New(boost::asio::io_service&, beast::Journal); }; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/Slice.h b/src/ripple/basics/Slice.h index c710ce3fe9..126e8ab1ce 100644 --- a/src/ripple/basics/Slice.h +++ b/src/ripple/basics/Slice.h @@ -29,8 +29,8 @@ #include #include #include -#include #include +#include namespace ripple { @@ -52,13 +52,13 @@ public: /** Default constructed Slice has length 0. */ Slice() noexcept = default; - Slice (Slice const&) noexcept = default; - Slice& operator= (Slice const&) noexcept = default; + Slice(Slice const&) noexcept = default; + Slice& + operator=(Slice const&) noexcept = default; /** Create a slice pointing to existing memory. */ - Slice (void const* data, std::size_t size) noexcept - : data_ (reinterpret_cast(data)) - , size_ (size) + Slice(void const* data, std::size_t size) noexcept + : data_(reinterpret_cast(data)), size_(size) { } @@ -100,24 +100,23 @@ public: /** Advance the buffer. */ /** @{ */ Slice& - operator+= (std::size_t n) + operator+=(std::size_t n) { if (n > size_) - Throw ("too small"); + Throw("too small"); data_ += n; size_ -= n; return *this; } Slice - operator+ (std::size_t n) const + operator+(std::size_t n) const { Slice temp = *this; return temp += n; } /** @} */ - const_iterator begin() const noexcept { @@ -146,16 +145,14 @@ public: //------------------------------------------------------------------------------ template -inline -void -hash_append (Hasher& h, Slice const& v) +inline void +hash_append(Hasher& h, Slice const& v) { h(v.data(), v.size()); } -inline -bool -operator== (Slice const& lhs, Slice const& rhs) noexcept +inline bool +operator==(Slice const& lhs, Slice const& rhs) noexcept { if (lhs.size() != rhs.size()) return false; @@ -166,25 +163,25 @@ operator== (Slice const& lhs, Slice const& rhs) noexcept return std::memcmp(lhs.data(), rhs.data(), lhs.size()) == 0; } -inline -bool -operator!= (Slice const& lhs, Slice const& rhs) noexcept +inline bool +operator!=(Slice const& lhs, Slice const& rhs) noexcept { return !(lhs == rhs); } -inline -bool -operator< (Slice const& lhs, Slice const& rhs) noexcept +inline bool +operator<(Slice const& lhs, Slice const& rhs) noexcept { return std::lexicographical_compare( - lhs.data(), lhs.data() + lhs.size(), - rhs.data(), rhs.data() + rhs.size()); + lhs.data(), + lhs.data() + lhs.size(), + rhs.data(), + rhs.data() + rhs.size()); } - template -Stream& operator<<(Stream& s, Slice const& v) +Stream& +operator<<(Stream& s, Slice const& v) { s << strHex(v); return s; @@ -192,33 +189,29 @@ Stream& operator<<(Stream& s, Slice const& v) template std::enable_if_t< - std::is_same::value || - std::is_same::value, - Slice -> -makeSlice (std::array const& a) + std::is_same::value || std::is_same::value, + Slice> +makeSlice(std::array const& a) { return Slice(a.data(), a.size()); } template std::enable_if_t< - std::is_same::value || - std::is_same::value, - Slice -> -makeSlice (std::vector const& v) + std::is_same::value || std::is_same::value, + Slice> +makeSlice(std::vector const& v) { return Slice(v.data(), v.size()); } template Slice -makeSlice (std::basic_string const& s) +makeSlice(std::basic_string const& s) { return Slice(s.data(), s.size()); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/StringUtilities.h b/src/ripple/basics/StringUtilities.h index 7f28b2b617..e3954f664c 100644 --- a/src/ripple/basics/StringUtilities.h +++ b/src/ripple/basics/StringUtilities.h @@ -30,22 +30,24 @@ #include namespace ripple { -inline static std::string sqlEscape (std::string const& strSrc) +inline static std::string +sqlEscape(std::string const& strSrc) { - static boost::format f ("X'%s'"); - return str (boost::format (f) % strHex (strSrc)); + static boost::format f("X'%s'"); + return str(boost::format(f) % strHex(strSrc)); } -inline static std::string sqlEscape (Blob const& vecSrc) +inline static std::string +sqlEscape(Blob const& vecSrc) { - size_t size = vecSrc.size (); + size_t size = vecSrc.size(); if (size == 0) return "X''"; - std::string j (size * 2 + 3, 0); + std::string j(size * 2 + 3, 0); - unsigned char* oPtr = reinterpret_cast (&*j.begin ()); + unsigned char* oPtr = reinterpret_cast(&*j.begin()); const unsigned char* iPtr = &vecSrc[0]; *oPtr++ = 'X'; @@ -54,15 +56,16 @@ inline static std::string sqlEscape (Blob const& vecSrc) for (int i = size; i != 0; --i) { unsigned char c = *iPtr++; - *oPtr++ = charHex (c >> 4); - *oPtr++ = charHex (c & 15); + *oPtr++ = charHex(c >> 4); + *oPtr++ = charHex(c & 15); } *oPtr++ = '\''; return j; } -uint64_t uintFromHex (std::string const& strSrc); +uint64_t +uintFromHex(std::string const& strSrc); template boost::optional @@ -105,16 +108,14 @@ strUnHex(std::size_t strSize, Iterator begin, Iterator end) return {std::move(out)}; } -inline -boost::optional -strUnHex (std::string const& strSrc) +inline boost::optional +strUnHex(std::string const& strSrc) { return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend()); } -inline -boost::optional -strViewUnHex (boost::string_view const& strSrc) +inline boost::optional +strViewUnHex(boost::string_view const& strSrc) { return strUnHex(strSrc.size(), strSrc.cbegin(), strSrc.cend()); } @@ -131,21 +132,22 @@ struct parsedURL std::string path; bool - operator == (parsedURL const& other) const + operator==(parsedURL const& other) const { - return scheme == other.scheme && - domain == other.domain && - port == other.port && - path == other.path; + return scheme == other.scheme && domain == other.domain && + port == other.port && path == other.path; } }; -bool parseUrl (parsedURL& pUrl, std::string const& strUrl); +bool +parseUrl(parsedURL& pUrl, std::string const& strUrl); -std::string trim_whitespace (std::string str); +std::string +trim_whitespace(std::string str); -boost::optional to_uint64(std::string const& s); +boost::optional +to_uint64(std::string const& s); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/Sustain.h b/src/ripple/basics/Sustain.h index 50710d68ed..c8032831f0 100644 --- a/src/ripple/basics/Sustain.h +++ b/src/ripple/basics/Sustain.h @@ -26,10 +26,13 @@ namespace ripple { // "Sustain" is a system for a buddy process that monitors the main process // and relaunches it on a fault. -bool HaveSustain (); -std::string StopSustain (); -std::string DoSustain (); +bool +HaveSustain(); +std::string +StopSustain(); +std::string +DoSustain(); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/TaggedCache.h b/src/ripple/basics/TaggedCache.h index 4898e9831d..35658d2c6a 100644 --- a/src/ripple/basics/TaggedCache.h +++ b/src/ripple/basics/TaggedCache.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED #define RIPPLE_BASICS_TAGGEDCACHE_H_INCLUDED -#include #include #include +#include #include #include #include @@ -47,11 +47,10 @@ namespace ripple { template < class Key, class T, - class Hash = hardened_hash <>, - class KeyEqual = std::equal_to , - //class Allocator = std::allocator >, - class Mutex = std::recursive_mutex -> + class Hash = hardened_hash<>, + class KeyEqual = std::equal_to, + // class Allocator = std::allocator >, + class Mutex = std::recursive_mutex> class TaggedCache { public: @@ -59,103 +58,120 @@ public: using key_type = Key; using mapped_type = T; // VFALCO TODO Use std::shared_ptr, std::weak_ptr - using weak_mapped_ptr = std::weak_ptr ; - using mapped_ptr = std::shared_ptr ; - using clock_type = beast::abstract_clock ; + using weak_mapped_ptr = std::weak_ptr; + using mapped_ptr = std::shared_ptr; + using clock_type = beast::abstract_clock; public: - TaggedCache (std::string const& name, int size, - clock_type::duration expiration, clock_type& clock, beast::Journal journal, - beast::insight::Collector::ptr const& collector = beast::insight::NullCollector::New ()) - : m_journal (journal) - , m_clock (clock) - , m_stats (name, - std::bind (&TaggedCache::collect_metrics, this), - collector) - , m_name (name) - , m_target_size (size) - , m_target_age (expiration) - , m_cache_count (0) - , m_hits (0) - , m_misses (0) + TaggedCache( + std::string const& name, + int size, + clock_type::duration expiration, + clock_type& clock, + beast::Journal journal, + beast::insight::Collector::ptr const& collector = + beast::insight::NullCollector::New()) + : m_journal(journal) + , m_clock(clock) + , m_stats( + name, + std::bind(&TaggedCache::collect_metrics, this), + collector) + , m_name(name) + , m_target_size(size) + , m_target_age(expiration) + , m_cache_count(0) + , m_hits(0) + , m_misses(0) { } public: /** Return the clock associated with the cache. */ - clock_type& clock () + clock_type& + clock() { return m_clock; } - int getTargetSize () const + int + getTargetSize() const { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); return m_target_size; } - void setTargetSize (int s) + void + setTargetSize(int s) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_target_size = s; if (s > 0) - m_cache.rehash (static_cast ((s + (s >> 2)) / m_cache.max_load_factor () + 1)); + m_cache.rehash(static_cast( + (s + (s >> 2)) / m_cache.max_load_factor() + 1)); - JLOG(m_journal.debug()) << - m_name << " target size set to " << s; + JLOG(m_journal.debug()) << m_name << " target size set to " << s; } - clock_type::duration getTargetAge () const + clock_type::duration + getTargetAge() const { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); return m_target_age; } - void setTargetAge (clock_type::duration s) + void + setTargetAge(clock_type::duration s) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_target_age = s; - JLOG(m_journal.debug()) << - m_name << " target age set to " << m_target_age.count(); + JLOG(m_journal.debug()) + << m_name << " target age set to " << m_target_age.count(); } - int getCacheSize () const + int + getCacheSize() const { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); return m_cache_count; } - int getTrackSize () const + int + getTrackSize() const { - std::lock_guard lock (m_mutex); - return m_cache.size (); + std::lock_guard lock(m_mutex); + return m_cache.size(); } - float getHitRate () + float + getHitRate() { - std::lock_guard lock (m_mutex); - auto const total = static_cast (m_hits + m_misses); - return m_hits * (100.0f / std::max (1.0f, total)); + std::lock_guard lock(m_mutex); + auto const total = static_cast(m_hits + m_misses); + return m_hits * (100.0f / std::max(1.0f, total)); } - void clear () + void + clear() { - std::lock_guard lock (m_mutex); - m_cache.clear (); + std::lock_guard lock(m_mutex); + m_cache.clear(); m_cache_count = 0; } - void reset () + void + reset() { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); m_cache.clear(); m_cache_count = 0; m_hits = 0; m_misses = 0; } - void sweep () + void + sweep() { int cacheRemovals = 0; int mapRemovals = 0; @@ -164,46 +180,48 @@ public: // Keep references to all the stuff we sweep // so that we can destroy them outside the lock. // - std::vector stuffToSweep; + std::vector stuffToSweep; { - clock_type::time_point const now (m_clock.now()); + clock_type::time_point const now(m_clock.now()); clock_type::time_point when_expire; - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); if (m_target_size == 0 || - (static_cast (m_cache.size ()) <= m_target_size)) + (static_cast(m_cache.size()) <= m_target_size)) { when_expire = now - m_target_age; } else { - when_expire = now - m_target_age*m_target_size/m_cache.size(); + when_expire = + now - m_target_age * m_target_size / m_cache.size(); - clock_type::duration const minimumAge ( - std::chrono::seconds (1)); + clock_type::duration const minimumAge(std::chrono::seconds(1)); if (when_expire > (now - minimumAge)) when_expire = now - minimumAge; - JLOG(m_journal.trace()) << - m_name << " is growing fast " << m_cache.size () << " of " << m_target_size << - " aging at " << (now - when_expire).count() << " of " << m_target_age.count(); + JLOG(m_journal.trace()) + << m_name << " is growing fast " << m_cache.size() << " of " + << m_target_size << " aging at " + << (now - when_expire).count() << " of " + << m_target_age.count(); } - stuffToSweep.reserve (m_cache.size ()); + stuffToSweep.reserve(m_cache.size()); - cache_iterator cit = m_cache.begin (); + cache_iterator cit = m_cache.begin(); - while (cit != m_cache.end ()) + while (cit != m_cache.end()) { - if (cit->second.isWeak ()) + if (cit->second.isWeak()) { // weak - if (cit->second.isExpired ()) + if (cit->second.isExpired()) { ++mapRemovals; - cit = m_cache.erase (cit); + cit = m_cache.erase(cit); } else { @@ -215,16 +233,16 @@ public: // strong, expired --m_cache_count; ++cacheRemovals; - if (cit->second.ptr.unique ()) + if (cit->second.ptr.unique()) { - stuffToSweep.push_back (cit->second.ptr); + stuffToSweep.push_back(cit->second.ptr); ++mapRemovals; - cit = m_cache.erase (cit); + cit = m_cache.erase(cit); } else { // remains weakly cached - cit->second.ptr.reset (); + cit->second.ptr.reset(); ++cit; } } @@ -239,38 +257,40 @@ public: if (mapRemovals || cacheRemovals) { - JLOG(m_journal.trace()) << - m_name << ": cache = " << m_cache.size () << - "-" << cacheRemovals << ", map-=" << mapRemovals; + JLOG(m_journal.trace()) + << m_name << ": cache = " << m_cache.size() << "-" + << cacheRemovals << ", map-=" << mapRemovals; } // At this point stuffToSweep will go out of scope outside the lock // and decrement the reference count on each strong pointer. } - bool del (const key_type& key, bool valid) + bool + del(const key_type& key, bool valid) { - // Remove from cache, if !valid, remove from map too. Returns true if removed from cache - std::lock_guard lock (m_mutex); + // Remove from cache, if !valid, remove from map too. Returns true if + // removed from cache + std::lock_guard lock(m_mutex); - cache_iterator cit = m_cache.find (key); + cache_iterator cit = m_cache.find(key); - if (cit == m_cache.end ()) + if (cit == m_cache.end()) return false; Entry& entry = cit->second; bool ret = false; - if (entry.isCached ()) + if (entry.isCached()) { --m_cache_count; - entry.ptr.reset (); + entry.ptr.reset(); ret = true; } - if (!valid || entry.isExpired ()) - m_cache.erase (cit); + if (!valid || entry.isExpired()) + m_cache.erase(cit); return ret; } @@ -289,23 +309,25 @@ public: @return `true` If the key already existed. */ private: - template bool canonicalize( const key_type& key, - std::conditional_t const, std::shared_ptr>& data - ) + std::conditional_t< + replace, + std::shared_ptr const, + std::shared_ptr>& data) { // Return canonical value, store if needed, refresh in cache // Return values: true=we had the data already - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - cache_iterator cit = m_cache.find (key); + cache_iterator cit = m_cache.find(key); - if (cit == m_cache.end ()) + if (cit == m_cache.end()) { - m_cache.emplace (std::piecewise_construct, + m_cache.emplace( + std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(m_clock.now(), data)); ++m_cache_count; @@ -313,9 +335,9 @@ private: } Entry& entry = cit->second; - entry.touch (m_clock.now()); + entry.touch(m_clock.now()); - if (entry.isCached ()) + if (entry.isCached()) { if constexpr (replace) { @@ -330,7 +352,7 @@ private: return true; } - mapped_ptr cachedData = entry.lock (); + mapped_ptr cachedData = entry.lock(); if (cachedData) { @@ -357,60 +379,65 @@ private: } public: - bool canonicalize_replace_cache(const key_type& key, std::shared_ptr const& data) + bool + canonicalize_replace_cache( + const key_type& key, + std::shared_ptr const& data) { return canonicalize(key, data); } - bool canonicalize_replace_client(const key_type& key, std::shared_ptr& data) + bool + canonicalize_replace_client(const key_type& key, std::shared_ptr& data) { return canonicalize(key, data); } - std::shared_ptr fetch (const key_type& key) + std::shared_ptr + fetch(const key_type& key) { // fetch us a shared pointer to the stored data object - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - cache_iterator cit = m_cache.find (key); + cache_iterator cit = m_cache.find(key); - if (cit == m_cache.end ()) + if (cit == m_cache.end()) { ++m_misses; - return mapped_ptr (); + return mapped_ptr(); } Entry& entry = cit->second; - entry.touch (m_clock.now()); + entry.touch(m_clock.now()); - if (entry.isCached ()) + if (entry.isCached()) { ++m_hits; return entry.ptr; } - entry.ptr = entry.lock (); + entry.ptr = entry.lock(); - if (entry.isCached ()) + if (entry.isCached()) { // independent of cache size, so not counted as a hit ++m_cache_count; return entry.ptr; } - m_cache.erase (cit); + m_cache.erase(cit); ++m_misses; - return mapped_ptr (); + return mapped_ptr(); } /** Insert the element into the container. If the key already exists, nothing happens. @return `true` If the element was inserted */ - bool insert (key_type const& key, T const& value) + bool + insert(key_type const& key, T const& value) { - mapped_ptr p (std::make_shared ( - std::cref (value))); + mapped_ptr p(std::make_shared(std::cref(value))); return canonicalize_replace_client(key, p); } @@ -419,10 +446,11 @@ public: // Perhaps it should work like standard containers, which // simply return an iterator. // - bool retrieve (const key_type& key, T& data) + bool + retrieve(const key_type& key, T& data) { // retrieve the value of the stored data - mapped_ptr entry = fetch (key); + mapped_ptr entry = fetch(key); if (!entry) return false; @@ -436,42 +464,43 @@ public: @param key The key to refresh. @return `true` if the key was found and the object is cached. */ - bool refreshIfPresent (const key_type& key) + bool + refreshIfPresent(const key_type& key) { bool found = false; // If present, make current in cache - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - cache_iterator cit = m_cache.find (key); + cache_iterator cit = m_cache.find(key); - if (cit != m_cache.end ()) + if (cit != m_cache.end()) { Entry& entry = cit->second; - if (! entry.isCached ()) + if (!entry.isCached()) { // Convert weak to strong. - entry.ptr = entry.lock (); + entry.ptr = entry.lock(); - if (entry.isCached ()) + if (entry.isCached()) { // We just put the object back in cache ++m_cache_count; - entry.touch (m_clock.now()); + entry.touch(m_clock.now()); found = true; } else { // Couldn't get strong pointer, // object fell out of the cache so remove the entry. - m_cache.erase (cit); + m_cache.erase(cit); } } else { // It's cached so update the timer - entry.touch (m_clock.now()); + entry.touch(m_clock.now()); found = true; } } @@ -483,39 +512,42 @@ public: return found; } - mutex_type& peekMutex () + mutex_type& + peekMutex() { return m_mutex; } - std::vector getKeys () const + std::vector + getKeys() const { - std::vector v; + std::vector v; { - std::lock_guard lock (m_mutex); - v.reserve (m_cache.size()); + std::lock_guard lock(m_mutex); + v.reserve(m_cache.size()); for (auto const& _ : m_cache) - v.push_back (_.first); + v.push_back(_.first); } return v; } private: - void collect_metrics () + void + collect_metrics() { - m_stats.size.set (getCacheSize ()); + m_stats.size.set(getCacheSize()); { - beast::insight::Gauge::value_type hit_rate (0); + beast::insight::Gauge::value_type hit_rate(0); { - std::lock_guard lock (m_mutex); - auto const total (m_hits + m_misses); + std::lock_guard lock(m_mutex); + auto const total(m_hits + m_misses); if (total != 0) hit_rate = (m_hits * 100) / total; } - m_stats.hit_rate.set (hit_rate); + m_stats.hit_rate.set(hit_rate); } } @@ -523,12 +555,15 @@ private: struct Stats { template - Stats (std::string const& prefix, Handler const& handler, + Stats( + std::string const& prefix, + Handler const& handler, beast::insight::Collector::ptr const& collector) - : hook (collector->make_hook (handler)) - , size (collector->make_gauge (prefix, "size")) - , hit_rate (collector->make_gauge (prefix, "hit_rate")) - { } + : hook(collector->make_hook(handler)) + , size(collector->make_gauge(prefix, "size")) + , hit_rate(collector->make_gauge(prefix, "hit_rate")) + { + } beast::insight::Hook hook; beast::insight::Gauge size; @@ -542,22 +577,41 @@ private: weak_mapped_ptr weak_ptr; clock_type::time_point last_access; - Entry (clock_type::time_point const& last_access_, + Entry( + clock_type::time_point const& last_access_, mapped_ptr const& ptr_) - : ptr (ptr_) - , weak_ptr (ptr_) - , last_access (last_access_) + : ptr(ptr_), weak_ptr(ptr_), last_access(last_access_) { } - bool isWeak () const { return ptr == nullptr; } - bool isCached () const { return ptr != nullptr; } - bool isExpired () const { return weak_ptr.expired (); } - mapped_ptr lock () { return weak_ptr.lock (); } - void touch (clock_type::time_point const& now) { last_access = now; } + bool + isWeak() const + { + return ptr == nullptr; + } + bool + isCached() const + { + return ptr != nullptr; + } + bool + isExpired() const + { + return weak_ptr.expired(); + } + mapped_ptr + lock() + { + return weak_ptr.lock(); + } + void + touch(clock_type::time_point const& now) + { + last_access = now; + } }; - using cache_type = hardened_hash_map ; + using cache_type = hardened_hash_map; using cache_iterator = typename cache_type::iterator; beast::Journal m_journal; @@ -582,6 +636,6 @@ private: std::uint64_t m_misses; }; -} +} // namespace ripple #endif diff --git a/src/ripple/basics/ToString.h b/src/ripple/basics/ToString.h index ae34d8ece4..93cb0ff335 100644 --- a/src/ripple/basics/ToString.h +++ b/src/ripple/basics/ToString.h @@ -32,33 +32,36 @@ namespace ripple { */ template -typename std::enable_if::value, - std::string>::type +typename std::enable_if::value, std::string>::type to_string(T t) { return std::to_string(t); } -inline std::string to_string(bool b) +inline std::string +to_string(bool b) { return b ? "true" : "false"; } -inline std::string to_string(char c) +inline std::string +to_string(char c) { return std::string(1, c); } -inline std::string to_string(std::string s) +inline std::string +to_string(std::string s) { return s; } -inline std::string to_string(char const* s) +inline std::string +to_string(char const* s) { return s; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/UnorderedContainers.h b/src/ripple/basics/UnorderedContainers.h index 1535cbaef7..2758551b99 100644 --- a/src/ripple/basics/UnorderedContainers.h +++ b/src/ripple/basics/UnorderedContainers.h @@ -28,65 +28,88 @@ #include /** -* Use hash_* containers for keys that do not need a cryptographically secure -* hashing algorithm. -* -* Use hardened_hash_* containers for keys that do need a secure hashing algorithm. -* -* The cryptographic security of containers where a hash function is used as a -* template parameter depends entirely on that hash function and not at all on -* what container it is. -*/ + * Use hash_* containers for keys that do not need a cryptographically secure + * hashing algorithm. + * + * Use hardened_hash_* containers for keys that do need a secure hashing + * algorithm. + * + * The cryptographic security of containers where a hash function is used as a + * template parameter depends entirely on that hash function and not at all on + * what container it is. + */ -namespace ripple -{ +namespace ripple { // hash containers -template , - class Pred = std::equal_to, - class Allocator = std::allocator>> -using hash_map = std::unordered_map ; +template < + class Key, + class Value, + class Hash = beast::uhash<>, + class Pred = std::equal_to, + class Allocator = std::allocator>> +using hash_map = std::unordered_map; -template , - class Pred = std::equal_to, - class Allocator = std::allocator>> -using hash_multimap = std::unordered_multimap ; +template < + class Key, + class Value, + class Hash = beast::uhash<>, + class Pred = std::equal_to, + class Allocator = std::allocator>> +using hash_multimap = + std::unordered_multimap; -template , - class Pred = std::equal_to, - class Allocator = std::allocator> -using hash_set = std::unordered_set ; +template < + class Value, + class Hash = beast::uhash<>, + class Pred = std::equal_to, + class Allocator = std::allocator> +using hash_set = std::unordered_set; -template , - class Pred = std::equal_to, - class Allocator = std::allocator> -using hash_multiset = std::unordered_multiset ; +template < + class Value, + class Hash = beast::uhash<>, + class Pred = std::equal_to, + class Allocator = std::allocator> +using hash_multiset = std::unordered_multiset; // hardened_hash containers using strong_hash = beast::xxhasher; -template , - class Pred = std::equal_to, - class Allocator = std::allocator>> -using hardened_hash_map = std::unordered_map ; +template < + class Key, + class Value, + class Hash = hardened_hash, + class Pred = std::equal_to, + class Allocator = std::allocator>> +using hardened_hash_map = std::unordered_map; -template , - class Pred = std::equal_to, - class Allocator = std::allocator>> -using hardened_hash_multimap = std::unordered_multimap ; +template < + class Key, + class Value, + class Hash = hardened_hash, + class Pred = std::equal_to, + class Allocator = std::allocator>> +using hardened_hash_multimap = + std::unordered_multimap; -template , - class Pred = std::equal_to, - class Allocator = std::allocator> -using hardened_hash_set = std::unordered_set ; +template < + class Value, + class Hash = hardened_hash, + class Pred = std::equal_to, + class Allocator = std::allocator> +using hardened_hash_set = std::unordered_set; -template , - class Pred = std::equal_to, - class Allocator = std::allocator> -using hardened_hash_multiset = std::unordered_multiset ; +template < + class Value, + class Hash = hardened_hash, + class Pred = std::equal_to, + class Allocator = std::allocator> +using hardened_hash_multiset = + std::unordered_multiset; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/UptimeClock.h b/src/ripple/basics/UptimeClock.h index 9fbc9c6cd6..7cafb94b53 100644 --- a/src/ripple/basics/UptimeClock.h +++ b/src/ripple/basics/UptimeClock.h @@ -37,22 +37,22 @@ namespace ripple { class UptimeClock { public: - using rep = int; - using period = std::ratio<1>; - using duration = std::chrono::duration; + using rep = int; + using period = std::ratio<1>; + using duration = std::chrono::duration; using time_point = std::chrono::time_point; static constexpr bool is_steady = std::chrono::system_clock::is_steady; explicit UptimeClock() = default; - static time_point now(); // seconds since rippled program start + static time_point + now(); // seconds since rippled program start private: - static std::atomic now_; + static std::atomic now_; static std::atomic stop_; - struct update_thread - : private std::thread + struct update_thread : private std::thread { ~update_thread(); update_thread(update_thread&&) = default; @@ -60,9 +60,10 @@ private: using std::thread::thread; }; - static update_thread start_clock(); + static update_thread + start_clock(); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/XRPAmount.h b/src/ripple/basics/XRPAmount.h index fefab4fb22..a9611814d8 100644 --- a/src/ripple/basics/XRPAmount.h +++ b/src/ripple/basics/XRPAmount.h @@ -26,9 +26,9 @@ #include #include +#include #include #include -#include #include #include @@ -41,63 +41,54 @@ namespace feeunit { of the code uses. */ struct dropTag; -} // feeunit +} // namespace feeunit -class XRPAmount - : private boost::totally_ordered - , private boost::additive - , private boost::equality_comparable - , private boost::additive +class XRPAmount : private boost::totally_ordered, + private boost::additive, + private boost::equality_comparable, + private boost::additive { public: using unit_type = feeunit::dropTag; using value_type = std::int64_t; + private: value_type drops_; public: - XRPAmount () = default; - constexpr XRPAmount (XRPAmount const& other) = default; - constexpr XRPAmount& operator= (XRPAmount const& other) = default; + XRPAmount() = default; + constexpr XRPAmount(XRPAmount const& other) = default; + constexpr XRPAmount& + operator=(XRPAmount const& other) = default; - constexpr - XRPAmount (beast::Zero) - : drops_ (0) + constexpr XRPAmount(beast::Zero) : drops_(0) { } - constexpr - XRPAmount& - operator= (beast::Zero) + constexpr XRPAmount& operator=(beast::Zero) { drops_ = 0; return *this; } - constexpr - explicit - XRPAmount (value_type drops) - : drops_ (drops) + constexpr explicit XRPAmount(value_type drops) : drops_(drops) { } XRPAmount& - operator= (value_type drops) + operator=(value_type drops) { drops_ = drops; return *this; } - constexpr - XRPAmount + constexpr XRPAmount operator*(value_type const& rhs) const { - return XRPAmount{ drops_ * rhs }; + return XRPAmount{drops_ * rhs}; } - friend - constexpr - XRPAmount + friend constexpr XRPAmount operator*(value_type lhs, XRPAmount const& rhs) { // multiplication is commutative @@ -105,44 +96,44 @@ public: } XRPAmount& - operator+= (XRPAmount const& other) + operator+=(XRPAmount const& other) { drops_ += other.drops(); return *this; } XRPAmount& - operator-= (XRPAmount const& other) + operator-=(XRPAmount const& other) { drops_ -= other.drops(); return *this; } XRPAmount& - operator+= (value_type const& rhs) + operator+=(value_type const& rhs) { drops_ += rhs; return *this; } XRPAmount& - operator-= (value_type const& rhs) + operator-=(value_type const& rhs) { drops_ -= rhs; return *this; } XRPAmount& - operator*= (value_type const& rhs) + operator*=(value_type const& rhs) { drops_ *= rhs; return *this; } XRPAmount - operator- () const + operator-() const { - return XRPAmount{ -drops_ }; + return XRPAmount{-drops_}; } bool @@ -164,32 +155,27 @@ public: } /** Returns true if the amount is not zero */ - explicit - constexpr - operator bool() const noexcept + explicit constexpr operator bool() const noexcept { return drops_ != 0; } /** Return the sign of the amount */ - constexpr - int + constexpr int signum() const noexcept { return (drops_ < 0) ? -1 : (drops_ ? 1 : 0); } /** Returns the number of drops */ - constexpr - value_type - drops () const + constexpr value_type + drops() const { return drops_; } - constexpr - double - decimalXRP () const; + constexpr double + decimalXRP() const; template boost::optional @@ -198,7 +184,7 @@ public: if ((drops_ > std::numeric_limits::max()) || (!std::numeric_limits::is_signed && drops_ < 0) || (std::numeric_limits::is_signed && - drops_ < std::numeric_limits::lowest())) + drops_ < std::numeric_limits::lowest())) { return boost::none; } @@ -222,8 +208,8 @@ public: Json::Value jsonClipped() const { - static_assert(std::is_signed_v && - std::is_integral_v, + static_assert( + std::is_signed_v && std::is_integral_v, "Expected XRPAmount to be a signed integral type"); constexpr auto min = std::numeric_limits::min(); @@ -240,54 +226,45 @@ public: function unless the type has been abstracted away, e.g. in a templated function. */ - constexpr - value_type - value () const + constexpr value_type + value() const { return drops_; } - friend - std::istream& - operator>> (std::istream& s, XRPAmount& val) + friend std::istream& + operator>>(std::istream& s, XRPAmount& val) { s >> val.drops_; return s; } - }; /** Number of drops per 1 XRP */ -constexpr -XRPAmount -DROPS_PER_XRP{1'000'000}; +constexpr XRPAmount DROPS_PER_XRP{1'000'000}; -constexpr -double -XRPAmount::decimalXRP () const +constexpr double +XRPAmount::decimalXRP() const { return static_cast(drops_) / DROPS_PER_XRP.drops(); } // Output XRPAmount as just the drops value. -template +template std::basic_ostream& -operator<<(std::basic_ostream& os, - const XRPAmount& q) +operator<<(std::basic_ostream& os, const XRPAmount& q) { return os << q.drops(); } -inline -std::string -to_string (XRPAmount const& amount) +inline std::string +to_string(XRPAmount const& amount) { - return std::to_string (amount.drops ()); + return std::to_string(amount.drops()); } -inline -XRPAmount -mulRatio ( +inline XRPAmount +mulRatio( XRPAmount const& amt, std::uint32_t num, std::uint32_t den, @@ -296,10 +273,10 @@ mulRatio ( using namespace boost::multiprecision; if (!den) - Throw ("division by zero"); + Throw("division by zero"); - int128_t const amt128 (amt.drops ()); - auto const neg = amt.drops () < 0; + int128_t const amt128(amt.drops()); + auto const neg = amt.drops() < 0; auto const m = amt128 * num; auto r = m / den; if (m % den) @@ -309,11 +286,11 @@ mulRatio ( if (neg && !roundUp) r -= 1; } - if (r > std::numeric_limits::max ()) - Throw ("XRP mulRatio overflow"); - return XRPAmount (r.convert_to ()); + if (r > std::numeric_limits::max()) + Throw("XRP mulRatio overflow"); + return XRPAmount(r.convert_to()); } -} +} // namespace ripple -#endif // RIPPLE_BASICS_XRPAMOUNT_H_INCLUDED +#endif // RIPPLE_BASICS_XRPAMOUNT_H_INCLUDED diff --git a/src/ripple/basics/algorithm.h b/src/ripple/basics/algorithm.h index cc9d4aaf38..673d5e955b 100644 --- a/src/ripple/basics/algorithm.h +++ b/src/ripple/basics/algorithm.h @@ -33,23 +33,26 @@ namespace ripple { // Note: This algorithm is evolved from std::set_intersection. template void -generalized_set_intersection(InputIter1 first1, InputIter1 last1, - InputIter2 first2, InputIter2 last2, - Action action, Comp comp) +generalized_set_intersection( + InputIter1 first1, + InputIter1 last1, + InputIter2 first2, + InputIter2 last2, + Action action, + Comp comp) { while (first1 != last1 && first2 != last2) { - - if (comp(*first1, *first2)) // if *first1 < *first2 - ++first1; // then reduce first range + if (comp(*first1, *first2)) // if *first1 < *first2 + ++first1; // then reduce first range else { - if (!comp(*first2, *first1)) // if *first1 == *first2 - { // then this is an intersection - action(*first1, *first2); // do the action - ++first1; // reduce first range + if (!comp(*first2, *first1)) // if *first1 == *first2 + { // then this is an intersection + action(*first1, *first2); // do the action + ++first1; // reduce first range } - ++first2; // Reduce second range because *first2 <= *first1 + ++first2; // Reduce second range because *first2 <= *first1 } } } @@ -58,22 +61,28 @@ generalized_set_intersection(InputIter1 first1, InputIter1 last1, // comp. // Effects: Eliminates all the elements i in the range [first1, last1) which are -// equivalent to some value in [first2, last2) or for which pred(i) returns true. +// equivalent to some value in [first2, last2) or for which pred(i) returns +// true. // Returns: A FwdIter1 E such that [first1, E) is the range of elements not // removed by this algorithm. -// Note: This algorithm is evolved from std::remove_if and std::set_intersection. +// Note: This algorithm is evolved from std::remove_if and +// std::set_intersection. template FwdIter1 -remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, - InputIter2 first2, InputIter2 last2, - Pred pred, Comp comp) +remove_if_intersect_or_match( + FwdIter1 first1, + FwdIter1 last1, + InputIter2 first2, + InputIter2 last2, + Pred pred, + Comp comp) { // [original-first1, current-first1) is the set of elements to be preserved. // [current-first1, i) is the set of elements that have been removed. // [i, last1) is the set of elements not tested yet. - + // Test each *i in [first1, last1) against [first2, last2) and pred for (auto i = first1; i != last1;) { @@ -105,6 +114,6 @@ remove_if_intersect_or_match(FwdIter1 first1, FwdIter1 last1, return first1; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/base64.h b/src/ripple/basics/base64.h index 516b588b33..ef34192d0b 100644 --- a/src/ripple/basics/base64.h +++ b/src/ripple/basics/base64.h @@ -62,20 +62,18 @@ namespace ripple { std::string -base64_encode (std::uint8_t const* data, - std::size_t len); +base64_encode(std::uint8_t const* data, std::size_t len); -inline -std::string +inline std::string base64_encode(std::string const& s) { - return base64_encode (reinterpret_cast < - std::uint8_t const*> (s.data()), s.size()); + return base64_encode( + reinterpret_cast(s.data()), s.size()); } std::string base64_decode(std::string const& data); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/base_uint.h b/src/ripple/basics/base_uint.h index 7635e0e9bf..56c4266584 100644 --- a/src/ripple/basics/base_uint.h +++ b/src/ripple/basics/base_uint.h @@ -25,8 +25,8 @@ #ifndef RIPPLE_BASICS_BASE_UINT_H_INCLUDED #define RIPPLE_BASICS_BASE_UINT_H_INCLUDED -#include #include +#include #include #include #include @@ -38,24 +38,22 @@ namespace ripple { -namespace detail -{ +namespace detail { template > -struct is_contiguous_container - : std::false_type -{}; +struct is_contiguous_container : std::false_type +{ +}; template -struct is_contiguous_container().size()), - decltype(std::declval().data()), - typename Container::value_type - >> - : std::true_type -{}; +struct is_contiguous_container< + Container, + std::void_t< + decltype(std::declval().size()), + decltype(std::declval().data()), + typename Container::value_type>> : std::true_type +{ +}; } // namespace detail @@ -64,10 +62,12 @@ struct is_contiguous_container class base_uint { - static_assert ((Bits % 32) == 0, + static_assert( + (Bits % 32) == 0, "The length of a base_uint in bits must be a multiple of 32."); - static_assert (Bits >= 64, + static_assert( + Bits >= 64, "The length of a base_uint in bits must be at least 64."); static constexpr std::size_t WIDTH = Bits / 32; @@ -86,34 +86,66 @@ public: static std::size_t constexpr bytes = Bits / 8; static_assert(sizeof(data_) == bytes, ""); - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - using value_type = unsigned char; - using pointer = value_type*; - using reference = value_type&; - using const_pointer = value_type const*; - using const_reference = value_type const&; - using iterator = pointer; - using const_iterator = const_pointer; - using reverse_iterator = std::reverse_iterator; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using value_type = unsigned char; + using pointer = value_type*; + using reference = value_type&; + using const_pointer = value_type const*; + using const_reference = value_type const&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; - using tag_type = Tag; + using tag_type = Tag; - pointer data() { return reinterpret_cast(data_.data ()); } - const_pointer data() const { return reinterpret_cast(data_.data ()); } + pointer + data() + { + return reinterpret_cast(data_.data()); + } + const_pointer + data() const + { + return reinterpret_cast(data_.data()); + } - - iterator begin() { return data(); } - iterator end() { return data()+bytes; } - const_iterator begin() const { return data(); } - const_iterator end() const { return data()+bytes; } - const_iterator cbegin() const { return data(); } - const_iterator cend() const { return data()+bytes; } + iterator + begin() + { + return data(); + } + iterator + end() + { + return data() + bytes; + } + const_iterator + begin() const + { + return data(); + } + const_iterator + end() const + { + return data() + bytes; + } + const_iterator + cbegin() const + { + return data(); + } + const_iterator + cend() const + { + return data() + bytes; + } /** Value hashing function. - The seed prevents crafted inputs from causing degenerate parent containers. + The seed prevents crafted inputs from causing degenerate parent + containers. */ - using hasher = hardened_hash <>; + using hasher = hardened_hash<>; //-------------------------------------------------------------------------- @@ -130,9 +162,9 @@ private: explicit VoidHelper() = default; }; - explicit base_uint (void const* data, VoidHelper) + explicit base_uint(void const* data, VoidHelper) { - memcpy (data_.data (), data, bytes); + memcpy(data_.data(), data, bytes); } public: @@ -146,33 +178,30 @@ public: *this = beast::zero; } - explicit base_uint (std::uint64_t b) + explicit base_uint(std::uint64_t b) { *this = b; } - template ::value && - std::is_trivially_copyable::value - >> + template < + class Container, + class = std::enable_if_t< + detail::is_contiguous_container::value && + std::is_trivially_copyable::value>> explicit base_uint(Container const& c) { - assert(c.size()*sizeof(typename Container::value_type) == size()); + assert(c.size() * sizeof(typename Container::value_type) == size()); std::memcpy(data_.data(), c.data(), size()); } template - std::enable_if_t - < - detail::is_contiguous_container::value && - std::is_trivially_copyable::value, - base_uint& - > + std::enable_if_t< + detail::is_contiguous_container::value && + std::is_trivially_copyable::value, + base_uint&> operator=(Container const& c) { - assert(c.size()*sizeof(typename Container::value_type) == size()); + assert(c.size() * sizeof(typename Container::value_type) == size()); std::memcpy(data_.data(), c.data(), size()); return *this; } @@ -181,12 +210,13 @@ public: The buffer pointed to by `data` must be at least Bits/8 bytes. */ static base_uint - fromVoid (void const* data) + fromVoid(void const* data) { - return base_uint (data, VoidHelper ()); + return base_uint(data, VoidHelper()); } - int signum() const + int + signum() const { for (int i = 0; i < WIDTH; i++) if (data_[i] != 0) @@ -195,12 +225,14 @@ public: return 0; } - bool operator! () const + bool + operator!() const { return *this == beast::zero; } - const base_uint operator~ () const + const base_uint + operator~() const { base_uint ret; @@ -210,7 +242,8 @@ public: return ret; } - base_uint& operator= (std::uint64_t uHost) + base_uint& + operator=(std::uint64_t uHost) { *this = beast::zero; union @@ -220,12 +253,13 @@ public: }; // Put in least significant bits. ul = boost::endian::native_to_big(uHost); - data_[WIDTH-2] = u[0]; - data_[WIDTH-1] = u[1]; + data_[WIDTH - 2] = u[0]; + data_[WIDTH - 1] = u[1]; return *this; } - base_uint& operator^= (const base_uint& b) + base_uint& + operator^=(const base_uint& b) { for (int i = 0; i < WIDTH; i++) data_[i] ^= b.data_[i]; @@ -233,7 +267,8 @@ public: return *this; } - base_uint& operator&= (const base_uint& b) + base_uint& + operator&=(const base_uint& b) { for (int i = 0; i < WIDTH; i++) data_[i] &= b.data_[i]; @@ -241,7 +276,8 @@ public: return *this; } - base_uint& operator|= (const base_uint& b) + base_uint& + operator|=(const base_uint& b) { for (int i = 0; i < WIDTH; i++) data_[i] |= b.data_[i]; @@ -249,13 +285,14 @@ public: return *this; } - base_uint& operator++ () + base_uint& + operator++() { // prefix operator for (int i = WIDTH - 1; i >= 0; --i) { - data_[i] = boost::endian::native_to_big - (boost::endian::big_to_native(data_[i]) + 1); + data_[i] = boost::endian::native_to_big( + boost::endian::big_to_native(data_[i]) + 1); if (data_[i] != 0) break; } @@ -263,22 +300,24 @@ public: return *this; } - const base_uint operator++ (int) + const base_uint + operator++(int) { // postfix operator const base_uint ret = *this; - ++ (*this); + ++(*this); return ret; } - base_uint& operator-- () + base_uint& + operator--() { for (int i = WIDTH - 1; i >= 0; --i) { auto prev = data_[i]; - data_[i] = - boost::endian::native_to_big(boost::endian::big_to_native(data_[i]) - 1); + data_[i] = boost::endian::native_to_big( + boost::endian::big_to_native(data_[i]) - 1); if (prev != 0) break; @@ -287,16 +326,18 @@ public: return *this; } - const base_uint operator-- (int) + const base_uint + operator--(int) { // postfix operator const base_uint ret = *this; - -- (*this); + --(*this); return ret; } - base_uint& operator+= (const base_uint& b) + base_uint& + operator+=(const base_uint& b) { std::uint64_t carry = 0; @@ -305,7 +346,8 @@ public: std::uint64_t n = carry + boost::endian::big_to_native(data_[i]) + boost::endian::big_to_native(b.data_[i]); - data_[i] = boost::endian::native_to_big (static_cast(n)); + data_[i] = + boost::endian::native_to_big(static_cast(n)); carry = n >> 32; } @@ -313,28 +355,29 @@ public: } template - friend void hash_append( - Hasher& h, base_uint const& a) noexcept + friend void + hash_append(Hasher& h, base_uint const& a) noexcept { // Do not allow any endian transformations on this memory - h(a.data_.data (), sizeof(a.data_)); + h(a.data_.data(), sizeof(a.data_)); } /** Parse a hex string into a base_uint The string must contain exactly bytes * 2 hex characters and must not have any leading or trailing whitespace. */ - bool SetHexExact (const char* psz) + bool + SetHexExact(const char* psz) { - unsigned char* pOut = begin (); + unsigned char* pOut = begin(); - for (int i = 0; i < sizeof (data_); ++i) + for (int i = 0; i < sizeof(data_); ++i) { auto hi = charUnHex(*psz++); if (hi == -1) return false; - auto lo = charUnHex (*psz++); + auto lo = charUnHex(*psz++); if (lo == -1) return false; @@ -359,7 +402,8 @@ public: When bStrict is false, the parsing is done in non-strict mode, and, if present, leading whitespace and the 0x prefix will be skipped. */ - bool SetHex (const char* psz, bool bStrict = false) + bool + SetHex(const char* psz, bool bStrict = false) { // Find beginning. auto pBegin = reinterpret_cast(psz); @@ -378,10 +422,10 @@ public: pEnd++; // Take only last digits of over long string. - if ((unsigned int) (pEnd - pBegin) > 2 * size ()) - pBegin = pEnd - 2 * size (); + if ((unsigned int)(pEnd - pBegin) > 2 * size()) + pBegin = pEnd - 2 * size(); - unsigned char* pOut = end () - ((pEnd - pBegin + 1) / 2); + unsigned char* pOut = end() - ((pEnd - pBegin + 1) / 2); *this = beast::zero; @@ -391,9 +435,7 @@ public: while (pBegin != pEnd) { auto cHigh = charUnHex(*pBegin++); - auto cLow = pBegin == pEnd - ? 0 - : charUnHex(*pBegin++); + auto cLow = pBegin == pEnd ? 0 : charUnHex(*pBegin++); if (cHigh == -1 || cLow == -1) return false; @@ -404,17 +446,20 @@ public: return !*pEnd; } - bool SetHex (std::string const& str, bool bStrict = false) + bool + SetHex(std::string const& str, bool bStrict = false) { - return SetHex (str.c_str (), bStrict); + return SetHex(str.c_str(), bStrict); } - bool SetHexExact (std::string const& str) + bool + SetHexExact(std::string const& str) { - return SetHexExact (str.c_str ()); + return SetHexExact(str.c_str()); } - constexpr static std::size_t size () + constexpr static std::size_t + size() { return bytes; } @@ -426,9 +471,21 @@ public: } // Deprecated. - bool isZero () const { return *this == beast::zero; } - bool isNonZero () const { return *this != beast::zero; } - void zero () { *this = beast::zero; } + bool + isZero() const + { + return *this == beast::zero; + } + bool + isNonZero() const + { + return *this != beast::zero; + } + void + zero() + { + *this = beast::zero; + } }; using uint128 = base_uint<128>; @@ -436,12 +493,12 @@ using uint160 = base_uint<160>; using uint256 = base_uint<256>; template -inline int compare ( - base_uint const& a, base_uint const& b) +inline int +compare(base_uint const& a, base_uint const& b) { - auto ret = std::mismatch (a.cbegin (), a.cend (), b.cbegin ()); + auto ret = std::mismatch(a.cbegin(), a.cend(), b.cbegin()); - if (ret.first == a.cend ()) + if (ret.first == a.cend()) return 0; // a > b @@ -453,142 +510,148 @@ inline int compare ( } template -inline bool operator< ( - base_uint const& a, base_uint const& b) +inline bool +operator<(base_uint const& a, base_uint const& b) { - return compare (a, b) < 0; + return compare(a, b) < 0; } template -inline bool operator<= ( - base_uint const& a, base_uint const& b) +inline bool +operator<=(base_uint const& a, base_uint const& b) { - return compare (a, b) <= 0; + return compare(a, b) <= 0; } template -inline bool operator> ( - base_uint const& a, base_uint const& b) +inline bool +operator>(base_uint const& a, base_uint const& b) { - return compare (a, b) > 0; + return compare(a, b) > 0; } template -inline bool operator>= ( - base_uint const& a, base_uint const& b) +inline bool +operator>=(base_uint const& a, base_uint const& b) { - return compare (a, b) >= 0; + return compare(a, b) >= 0; } template -inline bool operator== ( - base_uint const& a, base_uint const& b) +inline bool +operator==(base_uint const& a, base_uint const& b) { - return compare (a, b) == 0; + return compare(a, b) == 0; } template -inline bool operator!= ( - base_uint const& a, base_uint const& b) +inline bool +operator!=(base_uint const& a, base_uint const& b) { - return compare (a, b) != 0; + return compare(a, b) != 0; } //------------------------------------------------------------------------------ template -inline bool operator== (base_uint const& a, std::uint64_t b) +inline bool +operator==(base_uint const& a, std::uint64_t b) { return a == base_uint(b); } template -inline bool operator!= (base_uint const& a, std::uint64_t b) +inline bool +operator!=(base_uint const& a, std::uint64_t b) { return !(a == b); } //------------------------------------------------------------------------------ template -inline const base_uint operator^ ( - base_uint const& a, base_uint const& b) +inline const base_uint +operator^(base_uint const& a, base_uint const& b) { - return base_uint (a) ^= b; + return base_uint(a) ^= b; } template -inline const base_uint operator& ( - base_uint const& a, base_uint const& b) +inline const base_uint +operator&(base_uint const& a, base_uint const& b) { - return base_uint (a) &= b; + return base_uint(a) &= b; } template -inline const base_uint operator| ( - base_uint const& a, base_uint const& b) +inline const base_uint +operator|(base_uint const& a, base_uint const& b) { - return base_uint (a) |= b; + return base_uint(a) |= b; } template -inline const base_uint operator+ ( - base_uint const& a, base_uint const& b) +inline const base_uint +operator+(base_uint const& a, base_uint const& b) { - return base_uint (a) += b; + return base_uint(a) += b; } //------------------------------------------------------------------------------ template -inline std::string to_string (base_uint const& a) +inline std::string +to_string(base_uint const& a) { - return strHex (a.cbegin (), a.cend ()); + return strHex(a.cbegin(), a.cend()); } // Function templates that return a base_uint given text in hexadecimal. // Invoke like: // auto i = from_hex_text("AAAAA"); template -auto from_hex_text (char const* text) -> std::enable_if_t< - std::is_same>::value, T> +auto +from_hex_text(char const* text) -> std::enable_if_t< + std::is_same>::value, + T> { T ret; - ret.SetHex (text); + ret.SetHex(text); return ret; } template -auto from_hex_text (std::string const& text) -> std::enable_if_t< - std::is_same>::value, T> +auto +from_hex_text(std::string const& text) -> std::enable_if_t< + std::is_same>::value, + T> { T ret; - ret.SetHex (text); + ret.SetHex(text); return ret; } template -inline std::ostream& operator<< ( - std::ostream& out, base_uint const& u) +inline std::ostream& +operator<<(std::ostream& out, base_uint const& u) { - return out << to_string (u); + return out << to_string(u); } #ifndef __INTELLISENSE__ -static_assert(sizeof(uint128) == 128/8, "There should be no padding bytes"); -static_assert(sizeof(uint160) == 160/8, "There should be no padding bytes"); -static_assert(sizeof(uint256) == 256/8, "There should be no padding bytes"); +static_assert(sizeof(uint128) == 128 / 8, "There should be no padding bytes"); +static_assert(sizeof(uint160) == 160 / 8, "There should be no padding bytes"); +static_assert(sizeof(uint256) == 256 / 8, "There should be no padding bytes"); #endif -} // rippled +} // namespace ripple -namespace beast -{ +namespace beast { template struct is_uniquely_represented> : public std::true_type - { - explicit is_uniquely_represented() = default; - }; +{ + explicit is_uniquely_represented() = default; +}; -} // beast +} // namespace beast #endif diff --git a/src/ripple/basics/chrono.h b/src/ripple/basics/chrono.h index 703a091844..aa2ddc7e7c 100644 --- a/src/ripple/basics/chrono.h +++ b/src/ripple/basics/chrono.h @@ -32,10 +32,11 @@ namespace ripple { // A few handy aliases using days = std::chrono::duration< - int, std::ratio_multiply>>; + int, + std::ratio_multiply>>; -using weeks = std::chrono::duration< - int, std::ratio_multiply>>; +using weeks = std::chrono:: + duration>>; /** Clock for measuring Ripple Network Time. @@ -47,9 +48,9 @@ class NetClock public: explicit NetClock() = default; - using rep = std::uint32_t; - using period = std::ratio<1>; - using duration = std::chrono::duration; + using rep = std::uint32_t; + using period = std::ratio<1>; + using duration = std::chrono::duration; using time_point = std::chrono::time_point; static bool const is_steady = false; @@ -62,8 +63,7 @@ to_string(date::sys_time tp) return date::format("%Y-%b-%d %T %Z", tp); } -inline -std::string +inline std::string to_string(NetClock::time_point tp) { // 2000-01-01 00:00:00 UTC is 946684800s from 1970-01-01 00:00:00 UTC @@ -82,8 +82,7 @@ using Stopwatch = beast::abstract_clock; using TestStopwatch = beast::manual_clock; /** Returns an instance of a wall clock. */ -inline -Stopwatch& +inline Stopwatch& stopwatch() { return beast::get_abstract_clock< @@ -91,6 +90,6 @@ stopwatch() beast::basic_seconds_clock>(); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/contract.h b/src/ripple/basics/contract.h index b2f481fdb1..80aeee7a38 100644 --- a/src/ripple/basics/contract.h +++ b/src/ripple/basics/contract.h @@ -36,7 +36,7 @@ namespace ripple { /** Generates and logs a call stack */ void -LogThrow (std::string const& title); +LogThrow(std::string const& title); /** Rethrow the exception currently being handled. @@ -44,36 +44,33 @@ LogThrow (std::string const& title); control to the next matching exception handler, if any. Otherwise, std::terminate will be called. */ -[[noreturn]] -inline -void -Rethrow () +[[noreturn]] inline void +Rethrow() { - LogThrow ("Re-throwing exception"); + LogThrow("Re-throwing exception"); throw; } template -[[noreturn]] -inline -void -Throw (Args&&... args) +[[noreturn]] inline void +Throw(Args&&... args) { - static_assert (std::is_convertible::value, + static_assert( + std::is_convertible::value, "Exception must derive from std::exception."); E e(std::forward(args)...); - LogThrow (std::string("Throwing exception of type " + - beast::type_name() +": ") + e.what()); + LogThrow( + std::string( + "Throwing exception of type " + beast::type_name() + ": ") + + e.what()); throw e; } /** Called when faulty logic causes a broken invariant. */ -[[noreturn]] -void -LogicError ( - std::string const& how) noexcept; +[[noreturn]] void +LogicError(std::string const& how) noexcept; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/hardened_hash.h b/src/ripple/basics/hardened_hash.h index abf0ac6464..bc20409286 100644 --- a/src/ripple/basics/hardened_hash.h +++ b/src/ripple/basics/hardened_hash.h @@ -26,11 +26,11 @@ #include #include #include -#include #include #include #include #include +#include namespace ripple { @@ -47,9 +47,11 @@ make_seed_pair() noexcept std::mutex mutex; std::random_device rng; std::mt19937_64 gen; - std::uniform_int_distribution dist; + std::uniform_int_distribution dist; - state_t() : gen(rng()) {} + state_t() : gen(rng()) + { + } // state_t(state_t const&) = delete; // state_t& operator=(state_t const&) = delete; }; @@ -58,7 +60,7 @@ make_seed_pair() noexcept return {state.dist(state.gen), state.dist(state.gen)}; } -} +} // namespace detail /** * Seed functor once per construction @@ -99,9 +101,9 @@ private: public: using result_type = typename HashAlgorithm::result_type; - hardened_hash() - : m_seeds (detail::make_seed_pair<>()) - {} + hardened_hash() : m_seeds(detail::make_seed_pair<>()) + { + } template result_type @@ -113,6 +115,6 @@ public: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/impl/Archive.cpp b/src/ripple/basics/impl/Archive.cpp index 2adfed6768..47e3d1ab3e 100644 --- a/src/ripple/basics/impl/Archive.cpp +++ b/src/ripple/basics/impl/Archive.cpp @@ -34,12 +34,9 @@ extractTarLz4( Throw("Invalid source file"); using archive_ptr = - std::unique_ptr; - archive_ptr ar {archive_read_new(), - [](struct archive* a) - { - archive_read_free(a); - }}; + std::unique_ptr; + archive_ptr ar{ + archive_read_new(), [](struct archive* a) { archive_read_free(a); }}; if (!ar) Throw("Failed to allocate archive"); @@ -50,34 +47,32 @@ extractTarLz4( Throw(archive_error_string(ar.get())); // Examples suggest this block size - if (archive_read_open_filename( - ar.get(), src.string().c_str(), 10240) < ARCHIVE_OK) + if (archive_read_open_filename(ar.get(), src.string().c_str(), 10240) < + ARCHIVE_OK) { Throw(archive_error_string(ar.get())); } - archive_ptr aw {archive_write_disk_new(), - [](struct archive* a) - { - archive_write_free(a); - }}; + archive_ptr aw{archive_write_disk_new(), [](struct archive* a) { + archive_write_free(a); + }}; if (!aw) Throw("Failed to allocate archive"); if (archive_write_disk_set_options( - aw.get(), - ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | - ARCHIVE_EXTRACT_ACL | ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK) + aw.get(), + ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_ACL | + ARCHIVE_EXTRACT_FFLAGS) < ARCHIVE_OK) { Throw(archive_error_string(aw.get())); } - if(archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK) + if (archive_write_disk_set_standard_lookup(aw.get()) < ARCHIVE_OK) Throw(archive_error_string(aw.get())); int result; struct archive_entry* entry; - while(true) + while (true) { result = archive_read_next_header(ar.get(), &entry); if (result == ARCHIVE_EOF) @@ -92,7 +87,7 @@ extractTarLz4( if (archive_entry_size(entry) > 0) { - const void *buf; + const void* buf; size_t sz; la_int64_t offset; while (true) @@ -103,8 +98,8 @@ extractTarLz4( if (result < ARCHIVE_OK) Throw(archive_error_string(ar.get())); - if (archive_write_data_block( - aw.get(), buf, sz, offset) < ARCHIVE_OK) + if (archive_write_data_block(aw.get(), buf, sz, offset) < + ARCHIVE_OK) { Throw(archive_error_string(aw.get())); } @@ -116,4 +111,4 @@ extractTarLz4( } } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/BasicConfig.cpp b/src/ripple/basics/impl/BasicConfig.cpp index fa66568c9f..1fc4d71197 100644 --- a/src/ripple/basics/impl/BasicConfig.cpp +++ b/src/ripple/basics/impl/BasicConfig.cpp @@ -24,43 +24,41 @@ namespace ripple { -Section::Section (std::string const& name) - : name_(name) +Section::Section(std::string const& name) : name_(name) { } void -Section::set (std::string const& key, std::string const& value) +Section::set(std::string const& key, std::string const& value) { - auto const result = cont().emplace (key, value); - if (! result.second) + auto const result = cont().emplace(key, value); + if (!result.second) result.first->second = value; } void -Section::append (std::vector const& lines) +Section::append(std::vector const& lines) { // '=' - static boost::regex const re1 ( - "^" // start of line - "(?:\\s*)" // whitespace (optonal) - "([a-zA-Z][_a-zA-Z0-9]*)" // - "(?:\\s*)" // whitespace (optional) - "(?:=)" // '=' - "(?:\\s*)" // whitespace (optional) - "(.*\\S+)" // - "(?:\\s*)" // whitespace (optional) - , boost::regex_constants::optimize - ); + static boost::regex const re1( + "^" // start of line + "(?:\\s*)" // whitespace (optonal) + "([a-zA-Z][_a-zA-Z0-9]*)" // + "(?:\\s*)" // whitespace (optional) + "(?:=)" // '=' + "(?:\\s*)" // whitespace (optional) + "(.*\\S+)" // + "(?:\\s*)" // whitespace (optional) + , + boost::regex_constants::optimize); - lines_.reserve (lines_.size() + lines.size()); + lines_.reserve(lines_.size() + lines.size()); for (auto line : lines) { - auto remove_comment = [](std::string& val)->bool - { + auto remove_comment = [](std::string& val) -> bool { bool removed_trailing = false; auto comment = val.find('#'); - while(comment != std::string::npos) + while (comment != std::string::npos) { if (comment == 0) { @@ -69,11 +67,11 @@ Section::append (std::vector const& lines) val = ""; break; } - else if (val.at(comment-1) == '\\') + else if (val.at(comment - 1) == '\\') { // we have an escaped comment char. Erase the escape char // and keep looking - val.erase(comment-1,1); + val.erase(comment - 1, 1); } else { @@ -96,32 +94,32 @@ Section::append (std::vector const& lines) continue; boost::smatch match; - if (boost::regex_match (line, match, re1)) - set (match[1], match[2]); + if (boost::regex_match(line, match, re1)) + set(match[1], match[2]); else - values_.push_back (line); + values_.push_back(line); - lines_.push_back (std::move(line)); + lines_.push_back(std::move(line)); } } bool -Section::exists (std::string const& name) const +Section::exists(std::string const& name) const { - return cont().find (name) != cont().end(); + return cont().find(name) != cont().end(); } -std::pair -Section::find (std::string const& name) const +std::pair +Section::find(std::string const& name) const { - auto const iter = cont().find (name); + auto const iter = cont().find(name); if (iter == cont().end()) return {{}, false}; return {iter->second, true}; } std::ostream& -operator<< (std::ostream& os, Section const& section) +operator<<(std::ostream& os, Section const& section) { for (auto const& [k, v] : section.cont()) os << k << "=" << v << "\n"; @@ -131,38 +129,42 @@ operator<< (std::ostream& os, Section const& section) //------------------------------------------------------------------------------ bool -BasicConfig::exists (std::string const& name) const +BasicConfig::exists(std::string const& name) const { return map_.find(name) != map_.end(); } Section& -BasicConfig::section (std::string const& name) +BasicConfig::section(std::string const& name) { return map_[name]; } Section const& -BasicConfig::section (std::string const& name) const +BasicConfig::section(std::string const& name) const { static Section none(""); - auto const iter = map_.find (name); + auto const iter = map_.find(name); if (iter == map_.end()) return none; return iter->second; } void -BasicConfig::overwrite (std::string const& section, std::string const& key, +BasicConfig::overwrite( + std::string const& section, + std::string const& key, std::string const& value) { - auto const result = map_.emplace (std::piecewise_construct, - std::make_tuple(section), std::make_tuple(section)); - result.first->second.set (key, value); + auto const result = map_.emplace( + std::piecewise_construct, + std::make_tuple(section), + std::make_tuple(section)); + result.first->second.set(key, value); } void -BasicConfig::deprecatedClearSection (std::string const& section) +BasicConfig::deprecatedClearSection(std::string const& section) { auto i = map_.find(section); if (i != map_.end()) @@ -178,26 +180,28 @@ BasicConfig::legacy(std::string const& section, std::string value) std::string BasicConfig::legacy(std::string const& sectionName) const { - return section (sectionName).legacy (); + return section(sectionName).legacy(); } void -BasicConfig::build (IniFileSections const& ifs) +BasicConfig::build(IniFileSections const& ifs) { for (auto const& entry : ifs) { - auto const result = map_.emplace (std::piecewise_construct, - std::make_tuple(entry.first), std::make_tuple(entry.first)); - result.first->second.append (entry.second); + auto const result = map_.emplace( + std::piecewise_construct, + std::make_tuple(entry.first), + std::make_tuple(entry.first)); + result.first->second.append(entry.second); } } std::ostream& -operator<< (std::ostream& ss, BasicConfig const& c) +operator<<(std::ostream& ss, BasicConfig const& c) { for (auto const& [k, v] : c.map_) ss << "[" << k << "]\n" << v; return ss; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/CountedObject.cpp b/src/ripple/basics/impl/CountedObject.cpp index 14d08d802b..da42bf5b49 100644 --- a/src/ripple/basics/impl/CountedObject.cpp +++ b/src/ripple/basics/impl/CountedObject.cpp @@ -22,44 +22,44 @@ namespace ripple { -CountedObjects& CountedObjects::getInstance () noexcept +CountedObjects& +CountedObjects::getInstance() noexcept { static CountedObjects instance; return instance; } -CountedObjects::CountedObjects () noexcept - : m_count (0) - , m_head (nullptr) +CountedObjects::CountedObjects() noexcept : m_count(0), m_head(nullptr) { } -CountedObjects::List CountedObjects::getCounts (int minimumThreshold) const +CountedObjects::List +CountedObjects::getCounts(int minimumThreshold) const { List counts; // When other operations are concurrent, the count // might be temporarily less than the actual count. - int const count = m_count.load (); + int const count = m_count.load(); - counts.reserve (count); + counts.reserve(count); - CounterBase* counter = m_head.load (); + CounterBase* counter = m_head.load(); while (counter != nullptr) { - if (counter->getCount () >= minimumThreshold) + if (counter->getCount() >= minimumThreshold) { Entry entry; - entry.first = counter->getName (); - entry.second = counter->getCount (); + entry.first = counter->getName(); + entry.second = counter->getCount(); - counts.push_back (entry); + counts.push_back(entry); } - counter = counter->getNext (); + counter = counter->getNext(); } return counts; @@ -67,29 +67,27 @@ CountedObjects::List CountedObjects::getCounts (int minimumThreshold) const //------------------------------------------------------------------------------ -CountedObjects::CounterBase::CounterBase () noexcept - : m_count (0) +CountedObjects::CounterBase::CounterBase() noexcept : m_count(0) { // Insert ourselves at the front of the lock-free linked list - CountedObjects& instance = CountedObjects::getInstance (); + CountedObjects& instance = CountedObjects::getInstance(); CounterBase* head; do { - head = instance.m_head.load (); + head = instance.m_head.load(); m_next = head; - } - while (instance.m_head.exchange (this) != head); + } while (instance.m_head.exchange(this) != head); ++instance.m_count; } -CountedObjects::CounterBase::~CounterBase () noexcept +CountedObjects::CounterBase::~CounterBase() noexcept { // VFALCO NOTE If the counters are destroyed before the singleton, // undefined behavior will result if the singleton's member // functions are called. } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/FileUtilities.cpp b/src/ripple/basics/impl/FileUtilities.cpp index edcd611ca0..1a69abfdec 100644 --- a/src/ripple/basics/impl/FileUtilities.cpp +++ b/src/ripple/basics/impl/FileUtilities.cpp @@ -19,17 +19,18 @@ #include -namespace ripple -{ +namespace ripple { -std::string getFileContents(boost::system::error_code& ec, +std::string +getFileContents( + boost::system::error_code& ec, boost::filesystem::path const& sourcePath, boost::optional maxSize) { using namespace boost::filesystem; using namespace boost::system::errc; - path fullPath{ canonical(sourcePath, ec) }; + path fullPath{canonical(sourcePath, ec)}; if (ec) return {}; @@ -48,10 +49,11 @@ std::string getFileContents(boost::system::error_code& ec, return {}; } - const std::string result{ std::istreambuf_iterator{fileStream}, - std::istreambuf_iterator{} }; + const std::string result{ + std::istreambuf_iterator{fileStream}, + std::istreambuf_iterator{}}; - if (fileStream.bad ()) + if (fileStream.bad()) { ec = make_error_code(static_cast(errno)); return {}; @@ -60,7 +62,9 @@ std::string getFileContents(boost::system::error_code& ec, return result; } -void writeFileContents(boost::system::error_code& ec, +void +writeFileContents( + boost::system::error_code& ec, boost::filesystem::path const& destPath, std::string const& contents) { @@ -77,11 +81,11 @@ void writeFileContents(boost::system::error_code& ec, fileStream << contents; - if (fileStream.bad ()) + if (fileStream.bad()) { ec = make_error_code(static_cast(errno)); return; } } -} +} // namespace ripple diff --git a/src/ripple/basics/impl/IOUAmount.cpp b/src/ripple/basics/impl/IOUAmount.cpp index 7a2806f8f6..bb3c39ef31 100644 --- a/src/ripple/basics/impl/IOUAmount.cpp +++ b/src/ripple/basics/impl/IOUAmount.cpp @@ -17,12 +17,12 @@ */ //============================================================================== -#include #include +#include #include #include -#include #include +#include #include namespace ripple { @@ -35,7 +35,7 @@ static int const minExponent = -96; static int const maxExponent = 80; void -IOUAmount::normalize () +IOUAmount::normalize() { if (mantissa_ == 0) { @@ -57,7 +57,7 @@ IOUAmount::normalize () while (mantissa_ > maxMantissa) { if (exponent_ >= maxExponent) - Throw ("IOUAmount::normalize"); + Throw("IOUAmount::normalize"); mantissa_ /= 10; ++exponent_; @@ -70,14 +70,14 @@ IOUAmount::normalize () } if (exponent_ > maxExponent) - Throw ("value overflow"); + Throw("value overflow"); if (negative) mantissa_ = -mantissa_; } IOUAmount& -IOUAmount::operator+= (IOUAmount const& other) +IOUAmount::operator+=(IOUAmount const& other) { if (other == beast::zero) return *this; @@ -113,7 +113,7 @@ IOUAmount::operator+= (IOUAmount const& other) return *this; } - normalize (); + normalize(); return *this; } @@ -149,21 +149,21 @@ IOUAmount::operator<(IOUAmount const& other) const } std::string -to_string (IOUAmount const& amount) +to_string(IOUAmount const& amount) { // keep full internal accuracy, but make more human friendly if possible if (amount == beast::zero) return "0"; - int const exponent = amount.exponent (); - auto mantissa = amount.mantissa (); + int const exponent = amount.exponent(); + auto mantissa = amount.mantissa(); // Use scientific notation for exponents that are too small or too large if (((exponent != 0) && ((exponent < -25) || (exponent > -5)))) { - std::string ret = std::to_string (mantissa); - ret.append (1, 'e'); - ret.append (std::to_string (exponent)); + std::string ret = std::to_string(mantissa); + ret.append(1, 'e'); + ret.append(std::to_string(exponent)); return ret; } @@ -175,77 +175,71 @@ to_string (IOUAmount const& amount) negative = true; } - assert (exponent + 43 > 0); + assert(exponent + 43 > 0); size_t const pad_prefix = 27; size_t const pad_suffix = 23; - std::string const raw_value (std::to_string (mantissa)); + std::string const raw_value(std::to_string(mantissa)); std::string val; - val.reserve (raw_value.length () + pad_prefix + pad_suffix); - val.append (pad_prefix, '0'); - val.append (raw_value); - val.append (pad_suffix, '0'); + val.reserve(raw_value.length() + pad_prefix + pad_suffix); + val.append(pad_prefix, '0'); + val.append(raw_value); + val.append(pad_suffix, '0'); - size_t const offset (exponent + 43); + size_t const offset(exponent + 43); - auto pre_from (val.begin ()); - auto const pre_to (val.begin () + offset); + auto pre_from(val.begin()); + auto const pre_to(val.begin() + offset); - auto const post_from (val.begin () + offset); - auto post_to (val.end ()); + auto const post_from(val.begin() + offset); + auto post_to(val.end()); // Crop leading zeroes. Take advantage of the fact that there's always a // fixed amount of leading zeroes and skip them. - if (std::distance (pre_from, pre_to) > pad_prefix) + if (std::distance(pre_from, pre_to) > pad_prefix) pre_from += pad_prefix; - assert (post_to >= post_from); + assert(post_to >= post_from); - pre_from = std::find_if (pre_from, pre_to, - [](char c) - { - return c != '0'; - }); + pre_from = std::find_if(pre_from, pre_to, [](char c) { return c != '0'; }); // Crop trailing zeroes. Take advantage of the fact that there's always a // fixed amount of trailing zeroes and skip them. - if (std::distance (post_from, post_to) > pad_suffix) + if (std::distance(post_from, post_to) > pad_suffix) post_to -= pad_suffix; - assert (post_to >= post_from); + assert(post_to >= post_from); post_to = std::find_if( - std::make_reverse_iterator (post_to), - std::make_reverse_iterator (post_from), - [](char c) - { - return c != '0'; - }).base(); + std::make_reverse_iterator(post_to), + std::make_reverse_iterator(post_from), + [](char c) { return c != '0'; }) + .base(); std::string ret; if (negative) - ret.append (1, '-'); + ret.append(1, '-'); // Assemble the output: if (pre_from == pre_to) - ret.append (1, '0'); + ret.append(1, '0'); else ret.append(pre_from, pre_to); if (post_to != post_from) { - ret.append (1, '.'); - ret.append (post_from, post_to); + ret.append(1, '.'); + ret.append(post_from, post_to); } return ret; } IOUAmount -mulRatio ( +mulRatio( IOUAmount const& amt, std::uint32_t num, std::uint32_t den, @@ -254,19 +248,18 @@ mulRatio ( using namespace boost::multiprecision; if (!den) - Throw ("division by zero"); + Throw("division by zero"); // A vector with the value 10^index for indexes from 0 to 29 // The largest intermediate value we expect is 2^96, which // is less than 10^29 - static auto const powerTable = [] - { + static auto const powerTable = [] { std::vector result; - result.reserve (30); // 2^96 is largest intermediate result size - uint128_t cur (1); + result.reserve(30); // 2^96 is largest intermediate result size + uint128_t cur(1); for (int i = 0; i < 30; ++i) { - result.push_back (cur); + result.push_back(cur); cur *= 10; }; return result; @@ -274,12 +267,12 @@ mulRatio ( // Return floor(log10(v)) // Note: Returns -1 for v == 0 - static auto log10Floor = [](uint128_t const& v) - { - // Find the index of the first element >= the requested element, the index - // is the log of the element in the log table. - auto const l = std::lower_bound (powerTable.begin (), powerTable.end (), v); - int index = std::distance (powerTable.begin (), l); + static auto log10Floor = [](uint128_t const& v) { + // Find the index of the first element >= the requested element, the + // index is the log of the element in the log table. + auto const l = + std::lower_bound(powerTable.begin(), powerTable.end(), v); + int index = std::distance(powerTable.begin(), l); // If we're not equal, subtract to get the floor if (*l != v) --index; @@ -287,27 +280,28 @@ mulRatio ( }; // Return ceil(log10(v)) - static auto log10Ceil = [](uint128_t const& v) - { - // Find the index of the first element >= the requested element, the index - // is the log of the element in the log table. - auto const l = std::lower_bound (powerTable.begin (), powerTable.end (), v); - return int(std::distance (powerTable.begin (), l)); + static auto log10Ceil = [](uint128_t const& v) { + // Find the index of the first element >= the requested element, the + // index is the log of the element in the log table. + auto const l = + std::lower_bound(powerTable.begin(), powerTable.end(), v); + return int(std::distance(powerTable.begin(), l)); }; static auto const fl64 = - log10Floor (std::numeric_limits::max ()); + log10Floor(std::numeric_limits::max()); - bool const neg = amt.mantissa () < 0; - uint128_t const den128 (den); - // a 32 value * a 64 bit value and stored in a 128 bit value. This will never overflow + bool const neg = amt.mantissa() < 0; + uint128_t const den128(den); + // a 32 value * a 64 bit value and stored in a 128 bit value. This will + // never overflow uint128_t const mul = - uint128_t (neg ? -amt.mantissa () : amt.mantissa ()) * uint128_t (num); + uint128_t(neg ? -amt.mantissa() : amt.mantissa()) * uint128_t(num); auto low = mul / den128; - uint128_t rem (mul - low * den128); + uint128_t rem(mul - low * den128); - int exponent = amt.exponent (); + int exponent = amt.exponent(); if (rem) { @@ -317,7 +311,7 @@ mulRatio ( // and (rem/den128) is as large as possible. Scale by multiplying low // and rem by 10 and subtracting one from the exponent. We could do this // with a loop, but it's more efficient to use logarithms. - auto const roomToGrow = fl64 - log10Ceil (low); + auto const roomToGrow = fl64 - log10Ceil(low); if (roomToGrow > 0) { exponent -= roomToGrow; @@ -334,23 +328,23 @@ mulRatio ( // and adding one to the exponent until the low will fit in the 64-bit // mantissa. Use logarithms to avoid looping. bool hasRem = bool(rem); - auto const mustShrink = log10Ceil (low) - fl64; + auto const mustShrink = log10Ceil(low) - fl64; if (mustShrink > 0) { - uint128_t const sav (low); + uint128_t const sav(low); exponent += mustShrink; low /= powerTable[mustShrink]; if (!hasRem) hasRem = bool(sav - low * powerTable[mustShrink]); } - std::int64_t mantissa = low.convert_to (); + std::int64_t mantissa = low.convert_to(); // normalize before rounding if (neg) mantissa *= -1; - IOUAmount result (mantissa, exponent); + IOUAmount result(mantissa, exponent); if (hasRem) { @@ -359,25 +353,25 @@ mulRatio ( { if (!result) { - return IOUAmount (minMantissa, minExponent); + return IOUAmount(minMantissa, minExponent); } - // This addition cannot overflow because the mantissa is already normalized - return IOUAmount (result.mantissa () + 1, result.exponent ()); + // This addition cannot overflow because the mantissa is already + // normalized + return IOUAmount(result.mantissa() + 1, result.exponent()); } if (!roundUp && neg) { if (!result) { - return IOUAmount (-minMantissa, minExponent); + return IOUAmount(-minMantissa, minExponent); } // This subtraction cannot underflow because `result` is not zero - return IOUAmount (result.mantissa () - 1, result.exponent ()); + return IOUAmount(result.mantissa() - 1, result.exponent()); } } return result; } - -} +} // namespace ripple diff --git a/src/ripple/basics/impl/Log.cpp b/src/ripple/basics/impl/Log.cpp index 8118b6cdb0..c023bc1648 100644 --- a/src/ripple/basics/impl/Log.cpp +++ b/src/ripple/basics/impl/Log.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -30,50 +30,51 @@ namespace ripple { -Logs::Sink::Sink (std::string const& partition, - beast::severities::Severity thresh, Logs& logs) - : beast::Journal::Sink (thresh, false) - , logs_(logs) - , partition_(partition) +Logs::Sink::Sink( + std::string const& partition, + beast::severities::Severity thresh, + Logs& logs) + : beast::Journal::Sink(thresh, false), logs_(logs), partition_(partition) { } void -Logs::Sink::write (beast::severities::Severity level, std::string const& text) +Logs::Sink::write(beast::severities::Severity level, std::string const& text) { if (level < threshold()) return; - logs_.write (level, partition_, text, console()); + logs_.write(level, partition_, text, console()); } //------------------------------------------------------------------------------ -Logs::File::File() - : m_stream (nullptr) +Logs::File::File() : m_stream(nullptr) { } -bool Logs::File::isOpen () const noexcept +bool +Logs::File::isOpen() const noexcept { return m_stream != nullptr; } -bool Logs::File::open (boost::filesystem::path const& path) +bool +Logs::File::open(boost::filesystem::path const& path) { - close (); + close(); bool wasOpened = false; // VFALCO TODO Make this work with Unicode file paths - std::unique_ptr stream ( - new std::ofstream (path.c_str (), std::fstream::app)); + std::unique_ptr stream( + new std::ofstream(path.c_str(), std::fstream::app)); - if (stream->good ()) + if (stream->good()) { m_path = path; - m_stream = std::move (stream); + m_stream = std::move(stream); wasOpened = true; } @@ -81,25 +82,29 @@ bool Logs::File::open (boost::filesystem::path const& path) return wasOpened; } -bool Logs::File::closeAndReopen () +bool +Logs::File::closeAndReopen() { - close (); + close(); - return open (m_path); + return open(m_path); } -void Logs::File::close () +void +Logs::File::close() { m_stream = nullptr; } -void Logs::File::write (char const* text) +void +Logs::File::write(char const* text) { if (m_stream != nullptr) (*m_stream) << text; } -void Logs::File::writeln (char const* text) +void +Logs::File::writeln(char const* text) { if (m_stream != nullptr) { @@ -111,35 +116,34 @@ void Logs::File::writeln (char const* text) //------------------------------------------------------------------------------ Logs::Logs(beast::severities::Severity thresh) - : thresh_ (thresh) // default severity + : thresh_(thresh) // default severity { } bool -Logs::open (boost::filesystem::path const& pathToLogFile) +Logs::open(boost::filesystem::path const& pathToLogFile) { return file_.open(pathToLogFile); } beast::Journal::Sink& -Logs::get (std::string const& name) +Logs::get(std::string const& name) { - std::lock_guard lock (mutex_); - auto const result = - sinks_.emplace(name, makeSink(name, thresh_)); + std::lock_guard lock(mutex_); + auto const result = sinks_.emplace(name, makeSink(name, thresh_)); return *result.first->second; } beast::Journal::Sink& -Logs::operator[] (std::string const& name) +Logs::operator[](std::string const& name) { return get(name); } beast::Journal -Logs::journal (std::string const& name) +Logs::journal(std::string const& name) { - return beast::Journal (get(name)); + return beast::Journal(get(name)); } beast::severities::Severity @@ -149,166 +153,199 @@ Logs::threshold() const } void -Logs::threshold (beast::severities::Severity thresh) +Logs::threshold(beast::severities::Severity thresh) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); thresh_ = thresh; for (auto& sink : sinks_) - sink.second->threshold (thresh); + sink.second->threshold(thresh); } std::vector> Logs::partition_severities() const { std::vector> list; - std::lock_guard lock (mutex_); - list.reserve (sinks_.size()); + std::lock_guard lock(mutex_); + list.reserve(sinks_.size()); for (auto const& [name, sink] : sinks_) list.emplace_back(name, toString(fromSeverity(sink->threshold()))); return list; } void -Logs::write (beast::severities::Severity level, std::string const& partition, - std::string const& text, bool console) +Logs::write( + beast::severities::Severity level, + std::string const& partition, + std::string const& text, + bool console) { std::string s; - format (s, text, level, partition); - std::lock_guard lock (mutex_); - file_.writeln (s); - if (! silent_) + format(s, text, level, partition); + std::lock_guard lock(mutex_); + file_.writeln(s); + if (!silent_) std::cerr << s << '\n'; // VFALCO TODO Fix console output - //if (console) + // if (console) // out_.write_console(s); } std::string Logs::rotate() { - std::lock_guard lock (mutex_); - bool const wasOpened = file_.closeAndReopen (); + std::lock_guard lock(mutex_); + bool const wasOpened = file_.closeAndReopen(); if (wasOpened) return "The log file was closed and reopened."; return "The log file could not be closed and reopened."; } std::unique_ptr -Logs::makeSink(std::string const& name, - beast::severities::Severity threshold) +Logs::makeSink(std::string const& name, beast::severities::Severity threshold) { - return std::make_unique( - name, threshold, *this); + return std::make_unique(name, threshold, *this); } LogSeverity -Logs::fromSeverity (beast::severities::Severity level) +Logs::fromSeverity(beast::severities::Severity level) { using namespace beast::severities; switch (level) { - case kTrace: return lsTRACE; - case kDebug: return lsDEBUG; - case kInfo: return lsINFO; - case kWarning: return lsWARNING; - case kError: return lsERROR; + case kTrace: + return lsTRACE; + case kDebug: + return lsDEBUG; + case kInfo: + return lsINFO; + case kWarning: + return lsWARNING; + case kError: + return lsERROR; - default: - assert(false); - [[fallthrough]]; - case kFatal: - break; + default: + assert(false); + [[fallthrough]]; + case kFatal: + break; } return lsFATAL; } beast::severities::Severity -Logs::toSeverity (LogSeverity level) +Logs::toSeverity(LogSeverity level) { using namespace beast::severities; switch (level) { - case lsTRACE: return kTrace; - case lsDEBUG: return kDebug; - case lsINFO: return kInfo; - case lsWARNING: return kWarning; - case lsERROR: return kError; - default: - assert(false); - [[fallthrough]]; - case lsFATAL: - break; + case lsTRACE: + return kTrace; + case lsDEBUG: + return kDebug; + case lsINFO: + return kInfo; + case lsWARNING: + return kWarning; + case lsERROR: + return kError; + default: + assert(false); + [[fallthrough]]; + case lsFATAL: + break; } return kFatal; } std::string -Logs::toString (LogSeverity s) +Logs::toString(LogSeverity s) { switch (s) { - case lsTRACE: return "Trace"; - case lsDEBUG: return "Debug"; - case lsINFO: return "Info"; - case lsWARNING: return "Warning"; - case lsERROR: return "Error"; - case lsFATAL: return "Fatal"; - default: - assert (false); - return "Unknown"; + case lsTRACE: + return "Trace"; + case lsDEBUG: + return "Debug"; + case lsINFO: + return "Info"; + case lsWARNING: + return "Warning"; + case lsERROR: + return "Error"; + case lsFATAL: + return "Fatal"; + default: + assert(false); + return "Unknown"; } } LogSeverity -Logs::fromString (std::string const& s) +Logs::fromString(std::string const& s) { - if (boost::iequals (s, "trace")) + if (boost::iequals(s, "trace")) return lsTRACE; - if (boost::iequals (s, "debug")) + if (boost::iequals(s, "debug")) return lsDEBUG; - if (boost::iequals (s, "info") || boost::iequals (s, "information")) + if (boost::iequals(s, "info") || boost::iequals(s, "information")) return lsINFO; - if (boost::iequals (s, "warn") || boost::iequals (s, "warning") || boost::iequals (s, "warnings")) + if (boost::iequals(s, "warn") || boost::iequals(s, "warning") || + boost::iequals(s, "warnings")) return lsWARNING; - if (boost::iequals (s, "error") || boost::iequals (s, "errors")) + if (boost::iequals(s, "error") || boost::iequals(s, "errors")) return lsERROR; - if (boost::iequals (s, "fatal") || boost::iequals (s, "fatals")) + if (boost::iequals(s, "fatal") || boost::iequals(s, "fatals")) return lsFATAL; return lsINVALID; } void -Logs::format (std::string& output, std::string const& message, - beast::severities::Severity severity, std::string const& partition) +Logs::format( + std::string& output, + std::string const& message, + beast::severities::Severity severity, + std::string const& partition) { - output.reserve (message.size() + partition.size() + 100); + output.reserve(message.size() + partition.size() + 100); output = to_string(std::chrono::system_clock::now()); output += " "; - if (! partition.empty ()) + if (!partition.empty()) output += partition + ":"; using namespace beast::severities; switch (severity) { - case kTrace: output += "TRC "; break; - case kDebug: output += "DBG "; break; - case kInfo: output += "NFO "; break; - case kWarning: output += "WRN "; break; - case kError: output += "ERR "; break; - default: - assert(false); - [[fallthrough]]; - case kFatal: output += "FTL "; break; + case kTrace: + output += "TRC "; + break; + case kDebug: + output += "DBG "; + break; + case kInfo: + output += "NFO "; + break; + case kWarning: + output += "WRN "; + break; + case kError: + output += "ERR "; + break; + default: + assert(false); + [[fallthrough]]; + case kFatal: + output += "FTL "; + break; } output += message; @@ -316,21 +353,20 @@ Logs::format (std::string& output, std::string const& message, // Limit the maximum length of the output if (output.size() > maximumMessageCharacters) { - output.resize (maximumMessageCharacters - 3); + output.resize(maximumMessageCharacters - 3); output += "..."; } // Attempt to prevent sensitive information from appearing in log files by // redacting it with asterisks. - auto scrubber = [&output](char const* token) - { + auto scrubber = [&output](char const* token) { auto first = output.find(token); // If we have found the specified token, then attempt to isolate the // sensitive data (it's enclosed by double quotes) and mask it off: if (first != std::string::npos) { - first = output.find ('\"', first + std::strlen(token)); + first = output.find('\"', first + std::strlen(token)); if (first != std::string::npos) { @@ -339,18 +375,18 @@ Logs::format (std::string& output, std::string const& message, if (last == std::string::npos) last = output.size(); - output.replace (first, last - first, last - first, '*'); + output.replace(first, last - first, last - first, '*'); } } }; - scrubber ("\"seed\""); - scrubber ("\"seed_hex\""); - scrubber ("\"secret\""); - scrubber ("\"master_key\""); - scrubber ("\"master_seed\""); - scrubber ("\"master_seed_hex\""); - scrubber ("\"passphrase\""); + scrubber("\"seed\""); + scrubber("\"seed_hex\""); + scrubber("\"secret\""); + scrubber("\"master_key\""); + scrubber("\"master_seed\""); + scrubber("\"master_seed_hex\""); + scrubber("\"passphrase\""); } //------------------------------------------------------------------------------ @@ -363,16 +399,17 @@ private: std::mutex m_; public: - DebugSink () - : sink_ (beast::Journal::getNullSink()) + DebugSink() : sink_(beast::Journal::getNullSink()) { } - DebugSink (DebugSink const&) = delete; - DebugSink& operator=(DebugSink const&) = delete; + DebugSink(DebugSink const&) = delete; + DebugSink& + operator=(DebugSink const&) = delete; DebugSink(DebugSink&&) = delete; - DebugSink& operator=(DebugSink&&) = delete; + DebugSink& + operator=(DebugSink&&) = delete; std::unique_ptr set(std::unique_ptr sink) @@ -380,7 +417,7 @@ public: std::lock_guard _(m_); using std::swap; - swap (holder_, sink); + swap(holder_, sink); if (holder_) sink_ = *holder_; @@ -398,8 +435,7 @@ public: } }; -static -DebugSink& +static DebugSink& debugSink() { static DebugSink _; @@ -407,8 +443,7 @@ debugSink() } std::unique_ptr -setDebugLogSink( - std::unique_ptr sink) +setDebugLogSink(std::unique_ptr sink) { return debugSink().set(std::move(sink)); } @@ -416,7 +451,7 @@ setDebugLogSink( beast::Journal debugLog() { - return beast::Journal (debugSink().get()); + return beast::Journal(debugSink().get()); } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/PerfLogImp.cpp b/src/ripple/basics/impl/PerfLogImp.cpp index 2252261b91..8143865784 100644 --- a/src/ripple/basics/impl/PerfLogImp.cpp +++ b/src/ripple/basics/impl/PerfLogImp.cpp @@ -38,7 +38,8 @@ namespace ripple { namespace perf { -PerfLogImp::Counters::Counters(std::vector const& labels, +PerfLogImp::Counters::Counters( + std::vector const& labels, JobTypes const& jobTypes) { { @@ -59,7 +60,8 @@ PerfLogImp::Counters::Counters(std::vector const& labels, jq_.reserve(jobTypes.size()); for (auto const& [jobType, jobTypeInfo] : jobTypes) { - auto const inserted = jq_.emplace(jobType, Jq(jobTypeInfo.name())).second; + auto const inserted = + jq_.emplace(jobType, Jq(jobTypeInfo.name())).second; if (!inserted) { // Ensure that no other function populates this entry. @@ -80,10 +82,9 @@ PerfLogImp::Counters::countersJson() const Json::Value p(Json::objectValue); { auto const sync = - [&proc]() ->boost::optional { + [&proc]() -> boost::optional { std::lock_guard lock(proc.second.mut); - if (!proc.second.sync.started && - !proc.second.sync.finished && + if (!proc.second.sync.started && !proc.second.sync.finished && !proc.second.sync.errored) { return boost::none; @@ -111,8 +112,8 @@ PerfLogImp::Counters::countersJson() const totalRpcJson[jss::started] = std::to_string(totalRpc.sync.started); totalRpcJson[jss::finished] = std::to_string(totalRpc.sync.finished); totalRpcJson[jss::errored] = std::to_string(totalRpc.sync.errored); - totalRpcJson[jss::duration_us] = std::to_string( - totalRpc.sync.duration.count()); + totalRpcJson[jss::duration_us] = + std::to_string(totalRpc.sync.duration.count()); rpcobj[jss::total] = totalRpcJson; } @@ -123,11 +124,9 @@ PerfLogImp::Counters::countersJson() const { Json::Value j(Json::objectValue); { - auto const sync = - [&proc]() ->boost::optional { + auto const sync = [&proc]() -> boost::optional { std::lock_guard lock(proc.second.mut); - if (!proc.second.sync.queued && - !proc.second.sync.started && + if (!proc.second.sync.queued && !proc.second.sync.started && !proc.second.sync.finished) { return boost::none; @@ -143,11 +142,11 @@ PerfLogImp::Counters::countersJson() const totalJq.sync.started += sync->started; j[jss::finished] = std::to_string(sync->finished); totalJq.sync.finished += sync->finished; - j[jss::queued_duration_us] = std::to_string( - sync->queuedDuration.count()); + j[jss::queued_duration_us] = + std::to_string(sync->queuedDuration.count()); totalJq.sync.queuedDuration += sync->queuedDuration; - j[jss::running_duration_us] = std::to_string( - sync->runningDuration.count()); + j[jss::running_duration_us] = + std::to_string(sync->runningDuration.count()); totalJq.sync.runningDuration += sync->runningDuration; } jqobj[proc.second.label] = j; @@ -159,10 +158,10 @@ PerfLogImp::Counters::countersJson() const totalJqJson[jss::queued] = std::to_string(totalJq.sync.queued); totalJqJson[jss::started] = std::to_string(totalJq.sync.started); totalJqJson[jss::finished] = std::to_string(totalJq.sync.finished); - totalJqJson[jss::queued_duration_us] = std::to_string( - totalJq.sync.queuedDuration.count()); - totalJqJson[jss::running_duration_us] = std::to_string( - totalJq.sync.runningDuration.count()); + totalJqJson[jss::queued_duration_us] = + std::to_string(totalJq.sync.queuedDuration.count()); + totalJqJson[jss::running_duration_us] = + std::to_string(totalJq.sync.runningDuration.count()); jqobj[jss::total] = totalJqJson; } @@ -180,7 +179,7 @@ PerfLogImp::Counters::currentJson() const auto const present = steady_clock::now(); Json::Value jobsArray(Json::arrayValue); - auto const jobs = [this]{ + auto const jobs = [this] { std::lock_guard lock(jobsMutex_); return jobs_; }(); @@ -199,8 +198,8 @@ PerfLogImp::Counters::currentJson() const // label is const and created before multi-threading so needs no lock. jobj[jss::job] = e->second.label; jobj[jss::duration_us] = std::to_string( - std::chrono::duration_cast( - present - j.second).count()); + std::chrono::duration_cast(present - j.second) + .count()); jobsArray.append(jobj); } @@ -217,8 +216,8 @@ PerfLogImp::Counters::currentJson() const Json::Value methodobj(Json::objectValue); methodobj[jss::method] = m.first; methodobj[jss::duration_us] = std::to_string( - std::chrono::duration_cast( - present - m.second).count()); + std::chrono::duration_cast(present - m.second) + .count()); methodsArray.append(methodobj); } @@ -233,7 +232,7 @@ PerfLogImp::Counters::currentJson() const void PerfLogImp::openLog() { - if (! setup_.perfLog.empty()) + if (!setup_.perfLog.empty()) { if (logFile_.is_open()) logFile_.close(); @@ -246,7 +245,8 @@ PerfLogImp::openLog() if (ec) { JLOG(j_.fatal()) << "Unable to create performance log " - "directory " << logDir << ": " << ec.message(); + "directory " + << logDir << ": " << ec.message(); signalStop_(); return; } @@ -254,10 +254,10 @@ PerfLogImp::openLog() logFile_.open(setup_.perfLog.c_str(), std::ios::out | std::ios::app); - if (! logFile_) + if (!logFile_) { - JLOG(j_.fatal()) << "Unable to open performance log " << - setup_.perfLog << "."; + JLOG(j_.fatal()) + << "Unable to open performance log " << setup_.perfLog << "."; signalStop_(); } } @@ -282,7 +282,7 @@ PerfLogImp::run() openLog(); rotate_ = false; } - cond_.wait_until (lock, lastLog_ + setup_.logInterval); + cond_.wait_until(lock, lastLog_ + setup_.logInterval); } report(); } @@ -291,7 +291,7 @@ PerfLogImp::run() void PerfLogImp::report() { - if (! logFile_) + if (!logFile_) // If logFile_ is not writable do no further work. return; @@ -311,14 +311,15 @@ PerfLogImp::report() logFile_ << Json::Compact{std::move(report)} << std::endl; } -PerfLogImp::PerfLogImp(Setup const& setup, +PerfLogImp::PerfLogImp( + Setup const& setup, Stoppable& parent, beast::Journal journal, std::function&& signalStop) - : Stoppable ("PerfLogImp", parent) - , setup_ (setup) - , j_ (journal) - , signalStop_ (std::move (signalStop)) + : Stoppable("PerfLogImp", parent) + , setup_(setup) + , j_(journal) + , signalStop_(std::move(signalStop)) { openLog(); } @@ -344,13 +345,12 @@ PerfLogImp::rpcStart(std::string const& method, std::uint64_t const requestId) } std::lock_guard lock(counters_.methodsMutex_); counters_.methods_[requestId] = { - counter->first.c_str(), - steady_clock::now() - }; + counter->first.c_str(), steady_clock::now()}; } void -PerfLogImp::rpcEnd(std::string const& method, +PerfLogImp::rpcEnd( + std::string const& method, std::uint64_t const requestId, bool finish) { @@ -379,9 +379,8 @@ PerfLogImp::rpcEnd(std::string const& method, ++counter->second.sync.finished; else ++counter->second.sync.errored; - counter->second.sync.duration += - std::chrono::duration_cast( - steady_clock::now() - startTime); + counter->second.sync.duration += std::chrono::duration_cast( + steady_clock::now() - startTime); } void @@ -398,7 +397,8 @@ PerfLogImp::jobQueue(JobType const type) } void -PerfLogImp::jobStart(JobType const type, +PerfLogImp::jobStart( + JobType const type, microseconds dur, steady_time_point startTime, int instance) @@ -420,8 +420,7 @@ PerfLogImp::jobStart(JobType const type, } void -PerfLogImp::jobFinish(JobType const type, microseconds dur, - int instance) +PerfLogImp::jobFinish(JobType const type, microseconds dur, int instance) { auto counter = counters_.jq_.find(type); if (counter == counters_.jq_.end()) @@ -448,7 +447,6 @@ PerfLogImp::resizeJobs(int const resize) counters_.jobs_.resize(resize, {jtINVALID, steady_time_point()}); } - void PerfLogImp::rotate() { @@ -502,8 +500,8 @@ setup_PerfLog(Section const& section, boost::filesystem::path const& configDir) setup.perfLog = boost::filesystem::path(perfLog); if (setup.perfLog.is_relative()) { - setup.perfLog = boost::filesystem::absolute( - setup.perfLog, configDir); + setup.perfLog = + boost::filesystem::absolute(setup.perfLog, configDir); } } @@ -524,5 +522,5 @@ make_PerfLog( setup, parent, journal, std::move(signalStop)); } -} // perf -} // ripple +} // namespace perf +} // namespace ripple diff --git a/src/ripple/basics/impl/PerfLogImp.h b/src/ripple/basics/impl/PerfLogImp.h index 7c7eb550ee..943829b6e9 100644 --- a/src/ripple/basics/impl/PerfLogImp.h +++ b/src/ripple/basics/impl/PerfLogImp.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_BASICS_PERFLOGIMP_H #define RIPPLE_BASICS_PERFLOGIMP_H -#include #include +#include #include #include #include @@ -45,8 +45,7 @@ namespace perf { /** * Implementation class for PerfLog. */ -class PerfLogImp - : public PerfLog, Stoppable +class PerfLogImp : public PerfLog, Stoppable { /** * Track performance counters and currently executing tasks. @@ -66,11 +65,11 @@ class PerfLogImp { // Counters for each time a method starts and then either // finishes successfully or with an exception. - std::uint64_t started {0}; - std::uint64_t finished {0}; - std::uint64_t errored {0}; + std::uint64_t started{0}; + std::uint64_t finished{0}; + std::uint64_t errored{0}; // Cumulative duration of all finished and errored method calls. - microseconds duration {0}; + microseconds duration{0}; }; Sync sync; @@ -78,9 +77,9 @@ class PerfLogImp Rpc() = default; - Rpc(Rpc const& orig) - : sync (orig.sync) - {} + Rpc(Rpc const& orig) : sync(orig.sync) + { + } }; /** @@ -94,26 +93,25 @@ class PerfLogImp { // Counters for each time a job is enqueued, begins to run, // finishes. - std::uint64_t queued {0}; - std::uint64_t started {0}; - std::uint64_t finished {0}; + std::uint64_t queued{0}; + std::uint64_t started{0}; + std::uint64_t finished{0}; // Cumulative duration of all jobs' queued and running times. - microseconds queuedDuration {0}; - microseconds runningDuration {0}; + microseconds queuedDuration{0}; + microseconds runningDuration{0}; }; Sync sync; std::string const label; mutable std::mutex mut; - Jq(std::string const& labelArg) - : label (labelArg) - {} + Jq(std::string const& labelArg) : label(labelArg) + { + } - Jq(Jq const& orig) - : sync (orig.sync) - , label (orig.label) - {} + Jq(Jq const& orig) : sync(orig.sync), label(orig.label) + { + } }; // rpc_ and jq_ do not need mutex protection because all @@ -121,71 +119,79 @@ class PerfLogImp std::unordered_map rpc_; std::unordered_map, Jq> jq_; std::vector> jobs_; - int workers_ {0}; + int workers_{0}; mutable std::mutex jobsMutex_; std::unordered_map methods_; mutable std::mutex methodsMutex_; - Counters(std::vector const& labels, + Counters( + std::vector const& labels, JobTypes const& jobTypes); - Json::Value countersJson() const; - Json::Value currentJson() const; + Json::Value + countersJson() const; + Json::Value + currentJson() const; }; Setup const setup_; beast::Journal const j_; std::function const signalStop_; - Counters counters_ {ripple::RPC::getHandlerNames(), JobTypes::instance()}; + Counters counters_{ripple::RPC::getHandlerNames(), JobTypes::instance()}; std::ofstream logFile_; std::thread thread_; std::mutex mutex_; std::condition_variable cond_; system_time_point lastLog_; - std::string const hostname_ {boost::asio::ip::host_name()}; - bool stop_ {false}; - bool rotate_ {false}; + std::string const hostname_{boost::asio::ip::host_name()}; + bool stop_{false}; + bool rotate_{false}; - void openLog(); - void run(); - void report(); - void rpcEnd(std::string const& method, + void + openLog(); + void + run(); + void + report(); + void + rpcEnd( + std::string const& method, std::uint64_t const requestId, bool finish); public: - PerfLogImp(Setup const& setup, + PerfLogImp( + Setup const& setup, Stoppable& parent, beast::Journal journal, std::function&& signalStop); ~PerfLogImp() override; - void rpcStart( - std::string const& method, std::uint64_t const requestId) override; + void + rpcStart(std::string const& method, std::uint64_t const requestId) override; - void rpcFinish( - std::string const& method, - std::uint64_t const requestId) override + void + rpcFinish(std::string const& method, std::uint64_t const requestId) override { rpcEnd(method, requestId, true); } - void rpcError(std::string const& method, - std::uint64_t const requestId) override + void + rpcError(std::string const& method, std::uint64_t const requestId) override { rpcEnd(method, requestId, false); } - void jobQueue(JobType const type) override; - void jobStart( + void + jobQueue(JobType const type) override; + void + jobStart( JobType const type, microseconds dur, steady_time_point startTime, int instance) override; - void jobFinish( - JobType const type, - microseconds dur, - int instance) override; + void + jobFinish(JobType const type, microseconds dur, int instance) override; Json::Value countersJson() const override @@ -199,21 +205,29 @@ public: return counters_.currentJson(); } - void resizeJobs(int const resize) override; - void rotate() override; + void + resizeJobs(int const resize) override; + void + rotate() override; // Stoppable - void onPrepare() override {} + void + onPrepare() override + { + } // Called when application is ready to start threads. - void onStart() override; + void + onStart() override; // Called when the application begins shutdown. - void onStop() override; + void + onStop() override; // Called when all child Stoppable objects have stopped. - void onChildrenStopped() override; + void + onChildrenStopped() override; }; -} // perf -} // ripple +} // namespace perf +} // namespace ripple -#endif //RIPPLE_BASICS_PERFLOGIMP_H +#endif // RIPPLE_BASICS_PERFLOGIMP_H diff --git a/src/ripple/basics/impl/ResolverAsio.cpp b/src/ripple/basics/impl/ResolverAsio.cpp index b62ee945cc..d473953dd1 100644 --- a/src/ripple/basics/impl/ResolverAsio.cpp +++ b/src/ripple/basics/impl/ResolverAsio.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -40,15 +40,15 @@ template class AsyncObject { protected: - AsyncObject () - : m_pending (0) - { } + AsyncObject() : m_pending(0) + { + } public: - ~AsyncObject () + ~AsyncObject() { // Destroying the object with I/O pending? Not a clean exit! - assert (m_pending.load () == 0); + assert(m_pending.load() == 0); } /** RAII container that maintains the count of pending I/O. @@ -58,36 +58,38 @@ public: class CompletionCounter { public: - explicit CompletionCounter (Derived* owner) - : m_owner (owner) + explicit CompletionCounter(Derived* owner) : m_owner(owner) { ++m_owner->m_pending; } - CompletionCounter (CompletionCounter const& other) - : m_owner (other.m_owner) + CompletionCounter(CompletionCounter const& other) + : m_owner(other.m_owner) { ++m_owner->m_pending; } - ~CompletionCounter () + ~CompletionCounter() { if (--m_owner->m_pending == 0) - m_owner->asyncHandlersComplete (); + m_owner->asyncHandlersComplete(); } - CompletionCounter& operator= (CompletionCounter const&) = delete; + CompletionCounter& + operator=(CompletionCounter const&) = delete; private: Derived* m_owner; }; - void addReference () + void + addReference() { ++m_pending; } - void removeReference () + void + removeReference() { if (--m_pending == 0) (static_cast(this))->asyncHandlersComplete(); @@ -95,15 +97,14 @@ public: private: // The number of handlers pending. - std::atomic m_pending; + std::atomic m_pending; }; -class ResolverAsioImpl - : public ResolverAsio - , public AsyncObject +class ResolverAsioImpl : public ResolverAsio, + public AsyncObject { public: - using HostAndPort = std::pair ; + using HostAndPort = std::pair; beast::Journal m_journal; @@ -112,53 +113,54 @@ public: boost::asio::ip::tcp::resolver m_resolver; std::condition_variable m_cv; - std::mutex m_mut; + std::mutex m_mut; bool m_asyncHandlersCompleted; - std::atomic m_stop_called; - std::atomic m_stopped; + std::atomic m_stop_called; + std::atomic m_stopped; // Represents a unit of work for the resolver to do struct Work { - std::vector names; + std::vector names; HandlerType handler; template - Work (StringSequence const& names_, HandlerType const& handler_) - : handler (handler_) + Work(StringSequence const& names_, HandlerType const& handler_) + : handler(handler_) { - names.reserve(names_.size ()); + names.reserve(names_.size()); - std::reverse_copy (names_.begin (), names_.end (), - std::back_inserter (names)); + std::reverse_copy( + names_.begin(), names_.end(), std::back_inserter(names)); } }; - std::deque m_work; + std::deque m_work; - ResolverAsioImpl (boost::asio::io_service& io_service, + ResolverAsioImpl( + boost::asio::io_service& io_service, beast::Journal journal) - : m_journal (journal) - , m_io_service (io_service) - , m_strand (io_service) - , m_resolver (io_service) - , m_asyncHandlersCompleted (true) - , m_stop_called (false) - , m_stopped (true) + : m_journal(journal) + , m_io_service(io_service) + , m_strand(io_service) + , m_resolver(io_service) + , m_asyncHandlersCompleted(true) + , m_stop_called(false) + , m_stopped(true) { - } - ~ResolverAsioImpl () override + ~ResolverAsioImpl() override { - assert (m_work.empty ()); - assert (m_stopped); + assert(m_work.empty()); + assert(m_stopped); } //------------------------------------------------------------------------- // AsyncObject - void asyncHandlersComplete() + void + asyncHandlersComplete() { std::unique_lock lk{m_mut}; m_asyncHandlersCompleted = true; @@ -171,75 +173,81 @@ public: // //-------------------------------------------------------------------------- - void start () override + void + start() override { - assert (m_stopped == true); - assert (m_stop_called == false); + assert(m_stopped == true); + assert(m_stop_called == false); - if (m_stopped.exchange (false) == true) + if (m_stopped.exchange(false) == true) { { std::lock_guard lk{m_mut}; m_asyncHandlersCompleted = false; } - addReference (); + addReference(); } } - void stop_async () override + void + stop_async() override { - if (m_stop_called.exchange (true) == false) + if (m_stop_called.exchange(true) == false) { - m_io_service.dispatch (m_strand.wrap (std::bind ( - &ResolverAsioImpl::do_stop, - this, CompletionCounter (this)))); + m_io_service.dispatch(m_strand.wrap(std::bind( + &ResolverAsioImpl::do_stop, this, CompletionCounter(this)))); JLOG(m_journal.debug()) << "Queued a stop request"; } } - void stop () override + void + stop() override { - stop_async (); + stop_async(); JLOG(m_journal.debug()) << "Waiting to stop"; std::unique_lock lk{m_mut}; - m_cv.wait(lk, [this]{return m_asyncHandlersCompleted;}); + m_cv.wait(lk, [this] { return m_asyncHandlersCompleted; }); lk.unlock(); JLOG(m_journal.debug()) << "Stopped"; } - void resolve ( - std::vector const& names, - HandlerType const& handler) override + void + resolve(std::vector const& names, HandlerType const& handler) + override { - assert (m_stop_called == false); - assert (m_stopped == true); - assert (!names.empty()); + assert(m_stop_called == false); + assert(m_stopped == true); + assert(!names.empty()); // TODO NIKB use rvalue references to construct and move // reducing cost. - m_io_service.dispatch (m_strand.wrap (std::bind ( - &ResolverAsioImpl::do_resolve, this, - names, handler, CompletionCounter (this)))); + m_io_service.dispatch(m_strand.wrap(std::bind( + &ResolverAsioImpl::do_resolve, + this, + names, + handler, + CompletionCounter(this)))); } //------------------------------------------------------------------------- // Resolver - void do_stop (CompletionCounter) + void do_stop(CompletionCounter) { - assert (m_stop_called == true); + assert(m_stop_called == true); - if (m_stopped.exchange (true) == false) + if (m_stopped.exchange(true) == false) { - m_work.clear (); - m_resolver.cancel (); + m_work.clear(); + m_resolver.cancel(); - removeReference (); + removeReference(); } } - void do_finish ( + void + do_finish( std::string name, boost::system::error_code const& ec, HandlerType handler, @@ -249,7 +257,7 @@ public: if (ec == boost::asio::error::operation_aborted) return; - std::vector addresses; + std::vector addresses; // If we get an error message back, we don't return any // results that we may have gotten. @@ -257,28 +265,28 @@ public: { while (iter != boost::asio::ip::tcp::resolver::iterator()) { - addresses.push_back (beast::IPAddressConversion::from_asio (*iter)); + addresses.push_back( + beast::IPAddressConversion::from_asio(*iter)); ++iter; } } - handler (name, addresses); + handler(name, addresses); - m_io_service.post (m_strand.wrap (std::bind ( - &ResolverAsioImpl::do_work, this, - CompletionCounter (this)))); + m_io_service.post(m_strand.wrap(std::bind( + &ResolverAsioImpl::do_work, this, CompletionCounter(this)))); } - HostAndPort parseName(std::string const& str) + HostAndPort + parseName(std::string const& str) { // first attempt to parse as an endpoint (IP addr + port). // If that doesn't succeed, fall back to generic name + port parsing if (auto const result = beast::IP::Endpoint::from_string_checked(str)) { - return make_pair ( - result->address().to_string(), - std::to_string(result->port())); + return make_pair( + result->address().to_string(), std::to_string(result->port())); } // generic name/port parsing, which doesn't work for @@ -286,25 +294,24 @@ public: // a port separator // Attempt to find the first and last non-whitespace - auto const find_whitespace = std::bind ( - &std::isspace , + auto const find_whitespace = std::bind( + &std::isspace, std::placeholders::_1, - std::locale ()); + std::locale()); - auto host_first = std::find_if_not ( - str.begin (), str.end (), find_whitespace); + auto host_first = + std::find_if_not(str.begin(), str.end(), find_whitespace); - auto port_last = std::find_if_not ( - str.rbegin (), str.rend(), find_whitespace).base(); + auto port_last = + std::find_if_not(str.rbegin(), str.rend(), find_whitespace).base(); // This should only happen for all-whitespace strings if (host_first >= port_last) - return std::make_pair(std::string (), std::string ()); + return std::make_pair(std::string(), std::string()); // Attempt to find the first and last valid port separators - auto const find_port_separator = [](char const c) -> bool - { - if (std::isspace (static_cast(c))) + auto const find_port_separator = [](char const c) -> bool { + if (std::isspace(static_cast(c))) return true; if (c == ':') @@ -313,76 +320,82 @@ public: return false; }; - auto host_last = std::find_if ( - host_first, port_last, find_port_separator); + auto host_last = + std::find_if(host_first, port_last, find_port_separator); - auto port_first = std::find_if_not ( - host_last, port_last, find_port_separator); + auto port_first = + std::find_if_not(host_last, port_last, find_port_separator); - return make_pair ( - std::string (host_first, host_last), - std::string (port_first, port_last)); + return make_pair( + std::string(host_first, host_last), + std::string(port_first, port_last)); } - void do_work (CompletionCounter) + void do_work(CompletionCounter) { if (m_stop_called == true) return; // We don't have any work to do at this time - if (m_work.empty ()) + if (m_work.empty()) return; - std::string const name (m_work.front ().names.back()); - HandlerType handler (m_work.front ().handler); + std::string const name(m_work.front().names.back()); + HandlerType handler(m_work.front().handler); - m_work.front ().names.pop_back (); + m_work.front().names.pop_back(); - if (m_work.front ().names.empty ()) + if (m_work.front().names.empty()) m_work.pop_front(); - auto const [host, port] = parseName (name); + auto const [host, port] = parseName(name); - if (host.empty ()) + if (host.empty()) { - JLOG(m_journal.error()) << - "Unable to parse '" << name << "'"; + JLOG(m_journal.error()) << "Unable to parse '" << name << "'"; - m_io_service.post (m_strand.wrap (std::bind ( - &ResolverAsioImpl::do_work, this, - CompletionCounter (this)))); + m_io_service.post(m_strand.wrap(std::bind( + &ResolverAsioImpl::do_work, this, CompletionCounter(this)))); return; } - boost::asio::ip::tcp::resolver::query query ( - host, port); + boost::asio::ip::tcp::resolver::query query(host, port); - m_resolver.async_resolve (query, std::bind ( - &ResolverAsioImpl::do_finish, this, name, - std::placeholders::_1, handler, - std::placeholders::_2, - CompletionCounter (this))); + m_resolver.async_resolve( + query, + std::bind( + &ResolverAsioImpl::do_finish, + this, + name, + std::placeholders::_1, + handler, + std::placeholders::_2, + CompletionCounter(this))); } - void do_resolve (std::vector const& names, - HandlerType const& handler, CompletionCounter) + void + do_resolve( + std::vector const& names, + HandlerType const& handler, + CompletionCounter) { - assert (! names.empty()); + assert(!names.empty()); if (m_stop_called == false) { - m_work.emplace_back (names, handler); + m_work.emplace_back(names, handler); - JLOG(m_journal.debug()) << - "Queued new job with " << names.size() << - " tasks. " << m_work.size() << " jobs outstanding."; + JLOG(m_journal.debug()) + << "Queued new job with " << names.size() << " tasks. " + << m_work.size() << " jobs outstanding."; if (m_work.size() > 0) { - m_io_service.post (m_strand.wrap (std::bind ( - &ResolverAsioImpl::do_work, this, - CompletionCounter (this)))); + m_io_service.post(m_strand.wrap(std::bind( + &ResolverAsioImpl::do_work, + this, + CompletionCounter(this)))); } } } @@ -390,13 +403,12 @@ public: //----------------------------------------------------------------------------- -std::unique_ptr ResolverAsio::New ( - boost::asio::io_service& io_service, - beast::Journal journal) +std::unique_ptr +ResolverAsio::New(boost::asio::io_service& io_service, beast::Journal journal) { - return std::make_unique (io_service, journal); + return std::make_unique(io_service, journal); } //----------------------------------------------------------------------------- Resolver::~Resolver() = default; -} +} // namespace ripple diff --git a/src/ripple/basics/impl/StringUtilities.cpp b/src/ripple/basics/impl/StringUtilities.cpp index 05d3e25709..0481651d96 100644 --- a/src/ripple/basics/impl/StringUtilities.cpp +++ b/src/ripple/basics/impl/StringUtilities.cpp @@ -17,32 +17,33 @@ */ //============================================================================== -#include #include #include #include +#include #include -#include #include +#include #include #include #include namespace ripple { -uint64_t uintFromHex (std::string const& strSrc) +uint64_t +uintFromHex(std::string const& strSrc) { - uint64_t uValue (0); + uint64_t uValue(0); - if (strSrc.size () > 16) - Throw ("overlong 64-bit value"); + if (strSrc.size() > 16) + Throw("overlong 64-bit value"); for (auto c : strSrc) { - int ret = charUnHex (c); + int ret = charUnHex(c); if (ret == -1) - Throw ("invalid hex digit"); + Throw("invalid hex digit"); uValue = (uValue << 4) | ret; } @@ -50,10 +51,11 @@ uint64_t uintFromHex (std::string const& strSrc) return uValue; } -bool parseUrl (parsedURL& pUrl, std::string const& strUrl) +bool +parseUrl(parsedURL& pUrl, std::string const& strUrl) { // scheme://username:password@hostname:port/rest - static boost::regex reUrl ( + static boost::regex reUrl( "(?i)\\`\\s*" // required scheme "([[:alpha:]][-+.[:alpha:][:digit:]]*?):" @@ -72,38 +74,40 @@ bool parseUrl (parsedURL& pUrl, std::string const& strUrl) boost::smatch smMatch; // Bail if there is no match. - try { - if (! boost::regex_match (strUrl, smMatch, reUrl)) + try + { + if (!boost::regex_match(strUrl, smMatch, reUrl)) return false; - } catch (...) { + } + catch (...) + { return false; } pUrl.scheme = smMatch[1]; - boost::algorithm::to_lower (pUrl.scheme); + boost::algorithm::to_lower(pUrl.scheme); pUrl.username = smMatch[2]; pUrl.password = smMatch[3]; const std::string domain = smMatch[4]; // We need to use Endpoint to parse the domain to // strip surrounding brackets from IPv6 addresses, // e.g. [::1] => ::1. - const auto result = beast::IP::Endpoint::from_string_checked (domain); - pUrl.domain = result - ? result->address().to_string() - : domain; + const auto result = beast::IP::Endpoint::from_string_checked(domain); + pUrl.domain = result ? result->address().to_string() : domain; const std::string port = smMatch[5]; if (!port.empty()) { - pUrl.port = beast::lexicalCast (port); + pUrl.port = beast::lexicalCast(port); } pUrl.path = smMatch[6]; return true; } -std::string trim_whitespace (std::string str) +std::string +trim_whitespace(std::string str) { - boost::trim (str); + boost::trim(str); return str; } @@ -111,9 +115,9 @@ boost::optional to_uint64(std::string const& s) { std::uint64_t result; - if (beast::lexicalCastChecked (result, s)) + if (beast::lexicalCastChecked(result, s)) return result; return boost::none; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/Sustain.cpp b/src/ripple/basics/impl/Sustain.cpp index a9d3137962..88c2492d9a 100644 --- a/src/ripple/basics/impl/Sustain.cpp +++ b/src/ripple/basics/impl/Sustain.cpp @@ -17,16 +17,16 @@ */ //============================================================================== -#include #include +#include #include #include // For Sustain Linux variants // VFALCO TODO Rewrite Sustain to use beast::Process #ifdef __linux__ -#include #include +#include #include #include #endif @@ -42,117 +42,124 @@ namespace ripple { static auto const sleepBeforeWaiting = 10; static auto const sleepBetweenWaits = 1; -static pid_t pManager = safe_cast (0); -static pid_t pChild = safe_cast (0); +static pid_t pManager = safe_cast(0); +static pid_t pChild = safe_cast(0); -static void pass_signal (int a) +static void +pass_signal(int a) { - kill (pChild, a); + kill(pChild, a); } -static void stop_manager (int) +static void +stop_manager(int) { - kill (pChild, SIGINT); - _exit (0); + kill(pChild, SIGINT); + _exit(0); } -bool HaveSustain () +bool +HaveSustain() { return true; } -std::string StopSustain () +std::string +StopSustain() { - if (getppid () != pManager) + if (getppid() != pManager) return ""; - kill (pManager, SIGHUP); + kill(pManager, SIGHUP); return "Terminating monitor"; } -static -bool checkChild(pid_t pid, int options) +static bool +checkChild(pid_t pid, int options) { int i; - if (waitpid (pChild, &i, options) == -1) + if (waitpid(pChild, &i, options) == -1) return false; - return kill (pChild, 0) == 0; + return kill(pChild, 0) == 0; } -std::string DoSustain () +std::string +DoSustain() { - pManager = getpid (); - signal (SIGINT, stop_manager); - signal (SIGHUP, stop_manager); - signal (SIGUSR1, pass_signal); - signal (SIGUSR2, pass_signal); + pManager = getpid(); + signal(SIGINT, stop_manager); + signal(SIGHUP, stop_manager); + signal(SIGUSR1, pass_signal); + signal(SIGUSR2, pass_signal); // Number of times the child has exited in less than // 15 seconds. int fastExit = 0; - for (auto childCount = 1; ; ++childCount) + for (auto childCount = 1;; ++childCount) { - pChild = fork (); + pChild = fork(); if (pChild == -1) - _exit (0); + _exit(0); - auto cc = std::to_string (childCount); + auto cc = std::to_string(childCount); if (pChild == 0) { - beast::setCurrentThreadName ("rippled: main"); - signal (SIGINT, SIG_DFL); - signal (SIGHUP, SIG_DFL); - signal (SIGUSR1, SIG_DFL); - signal (SIGUSR2, SIG_DFL); + beast::setCurrentThreadName("rippled: main"); + signal(SIGINT, SIG_DFL); + signal(SIGHUP, SIG_DFL); + signal(SIGUSR1, SIG_DFL); + signal(SIGUSR2, SIG_DFL); return "Launching child " + cc; } - beast::setCurrentThreadName (("rippled: #" + cc).c_str()); + beast::setCurrentThreadName(("rippled: #" + cc).c_str()); - sleep (sleepBeforeWaiting); + sleep(sleepBeforeWaiting); // If the child has already terminated count this // as a fast exit and an indication that something // went wrong: - if (!checkChild (pChild, WNOHANG)) + if (!checkChild(pChild, WNOHANG)) { if (++fastExit == 5) - _exit (0); + _exit(0); } else { fastExit = 0; - while (checkChild (pChild, 0)) + while (checkChild(pChild, 0)) sleep(sleepBetweenWaits); - (void)rename ("core", - ("core." + std::to_string(pChild)).c_str()); + (void)rename("core", ("core." + std::to_string(pChild)).c_str()); } } } #else -bool HaveSustain () +bool +HaveSustain() { return false; } -std::string DoSustain () +std::string +DoSustain() { return ""; } -std::string StopSustain () +std::string +StopSustain() { return ""; } #endif -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/UptimeClock.cpp b/src/ripple/basics/impl/UptimeClock.cpp index f5aa0b2ad9..2dacaef46b 100644 --- a/src/ripple/basics/impl/UptimeClock.cpp +++ b/src/ripple/basics/impl/UptimeClock.cpp @@ -21,8 +21,8 @@ namespace ripple { -std::atomic UptimeClock::now_{0}; // seconds since start -std::atomic UptimeClock::stop_{false}; // stop update thread +std::atomic UptimeClock::now_{0}; // seconds since start +std::atomic UptimeClock::stop_{false}; // stop update thread // On rippled shutdown, cancel and wait for the update thread UptimeClock::update_thread::~update_thread() @@ -40,25 +40,24 @@ UptimeClock::update_thread::~update_thread() UptimeClock::update_thread UptimeClock::start_clock() { - return update_thread{[] - { - using namespace std; - using namespace std::chrono; + return update_thread{[] { + using namespace std; + using namespace std::chrono; - // Wake up every second and update now_ - auto next = system_clock::now() + 1s; - while (!stop_) - { - this_thread::sleep_until(next); - next += 1s; - ++now_; - } - }}; + // Wake up every second and update now_ + auto next = system_clock::now() + 1s; + while (!stop_) + { + this_thread::sleep_until(next); + next += 1s; + ++now_; + } + }}; } // This actually measures time since first use, instead of since rippled start. -// However the difference between these two epochs is a small fraction of a second -// and unimportant. +// However the difference between these two epochs is a small fraction of a +// second and unimportant. UptimeClock::time_point UptimeClock::now() @@ -70,4 +69,4 @@ UptimeClock::now() return time_point{duration{now_}}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/base64.cpp b/src/ripple/basics/impl/base64.cpp index c13eece486..c80147e3fd 100644 --- a/src/ripple/basics/impl/base64.cpp +++ b/src/ripple/basics/impl/base64.cpp @@ -63,54 +63,62 @@ namespace ripple { namespace base64 { -inline -char const* +inline char const* get_alphabet() { static char constexpr tab[] = { - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" - }; + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}; return &tab[0]; } -inline -signed char const* +inline signed char const* get_inverse() { static signed char constexpr tab[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0-15 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 16-31 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 32-47 - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 48-63 - -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79 - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 80-95 - -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111 - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 112-127 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 128-143 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 144-159 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 160-175 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 176-191 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 192-207 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 208-223 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 224-239 - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // 240-255 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 0-15 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 16-31 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, 62, -1, -1, -1, 63, // 32-47 + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, -1, -1, -1, -1, -1, -1, // 48-63 + -1, 0, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, // 64-79 + 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, -1, -1, -1, -1, -1, // 80-95 + -1, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, // 96-111 + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, -1, -1, -1, -1, -1, // 112-127 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 128-143 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 144-159 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 160-175 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 176-191 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 192-207 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 208-223 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, // 224-239 + -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1 // 240-255 }; return &tab[0]; } - /// Returns max chars needed to encode a base64 string -inline -std::size_t constexpr -encoded_size(std::size_t n) +inline std::size_t constexpr encoded_size(std::size_t n) { return 4 * ((n + 2) / 3); } /// Returns max bytes needed to decode a base64 string -inline -std::size_t constexpr -decoded_size(std::size_t n) +inline std::size_t constexpr decoded_size(std::size_t n) { return ((n / 4) * 3) + 2; } @@ -130,37 +138,37 @@ decoded_size(std::size_t n) std::size_t encode(void* dest, void const* src, std::size_t len) { - char* out = static_cast(dest); + char* out = static_cast(dest); char const* in = static_cast(src); auto const tab = base64::get_alphabet(); - for(auto n = len / 3; n--;) + for (auto n = len / 3; n--;) { - *out++ = tab[ (in[0] & 0xfc) >> 2]; + *out++ = tab[(in[0] & 0xfc) >> 2]; *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]; *out++ = tab[((in[2] & 0xc0) >> 6) + ((in[1] & 0x0f) << 2)]; - *out++ = tab[ in[2] & 0x3f]; + *out++ = tab[in[2] & 0x3f]; in += 3; } - switch(len % 3) + switch (len % 3) { - case 2: - *out++ = tab[ (in[0] & 0xfc) >> 2]; - *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]; - *out++ = tab[ (in[1] & 0x0f) << 2]; - *out++ = '='; - break; + case 2: + *out++ = tab[(in[0] & 0xfc) >> 2]; + *out++ = tab[((in[0] & 0x03) << 4) + ((in[1] & 0xf0) >> 4)]; + *out++ = tab[(in[1] & 0x0f) << 2]; + *out++ = '='; + break; - case 1: - *out++ = tab[ (in[0] & 0xfc) >> 2]; - *out++ = tab[((in[0] & 0x03) << 4)]; - *out++ = '='; - *out++ = '='; - break; + case 1: + *out++ = tab[(in[0] & 0xfc) >> 2]; + *out++ = tab[((in[0] & 0x03) << 4)]; + *out++ = '='; + *out++ = '='; + break; - case 0: - break; + case 0: + break; } return out - static_cast(dest); @@ -188,44 +196,44 @@ decode(void* dest, char const* src, std::size_t len) auto const inverse = base64::get_inverse(); - while(len-- && *in != '=') + while (len-- && *in != '=') { auto const v = inverse[*in]; - if(v == -1) + if (v == -1) break; ++in; c4[i] = v; - if(++i == 4) + if (++i == 4) { - c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4); + c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4); c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2); - c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; + c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; - for(i = 0; i < 3; i++) + for (i = 0; i < 3; i++) *out++ = c3[i]; i = 0; } } - if(i) + if (i) { - c3[0] = ( c4[0] << 2) + ((c4[1] & 0x30) >> 4); + c3[0] = (c4[0] << 2) + ((c4[1] & 0x30) >> 4); c3[1] = ((c4[1] & 0xf) << 4) + ((c4[2] & 0x3c) >> 2); - c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; + c3[2] = ((c4[2] & 0x3) << 6) + c4[3]; - for(j = 0; j < i - 1; j++) + for (j = 0; j < i - 1; j++) *out++ = c3[j]; } - return {out - static_cast(dest), + return { + out - static_cast(dest), in - reinterpret_cast(src)}; } -} // base64 +} // namespace base64 std::string -base64_encode (std::uint8_t const* data, - std::size_t len) +base64_encode(std::uint8_t const* data, std::size_t len) { std::string dest; dest.resize(base64::encoded_size(len)); @@ -238,10 +246,9 @@ base64_decode(std::string const& data) { std::string dest; dest.resize(base64::decoded_size(data.size())); - auto const result = base64::decode( - &dest[0], data.data(), data.size()); + auto const result = base64::decode(&dest[0], data.data(), data.size()); dest.resize(result.first); return dest; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/contract.cpp b/src/ripple/basics/impl/contract.cpp index 6e6f4b9e47..bf7df68258 100644 --- a/src/ripple/basics/impl/contract.cpp +++ b/src/ripple/basics/impl/contract.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include #include @@ -27,31 +27,29 @@ namespace ripple { namespace detail { -[[noreturn]] -void +[[noreturn]] void accessViolation() noexcept { // dereference memory location zero int volatile* j = 0; (void)*j; - std::abort (); + std::abort(); } -} // detail +} // namespace detail void -LogThrow (std::string const& title) +LogThrow(std::string const& title) { JLOG(debugLog().warn()) << title; } -[[noreturn]] -void -LogicError (std::string const& s) noexcept +[[noreturn]] void +LogicError(std::string const& s) noexcept { JLOG(debugLog().fatal()) << s; std::cerr << "Logic error: " << s << std::endl; detail::accessViolation(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/make_SSLContext.cpp b/src/ripple/basics/impl/make_SSLContext.cpp index fac4d2f3e3..80dc698b74 100644 --- a/src/ripple/basics/impl/make_SSLContext.cpp +++ b/src/ripple/basics/impl/make_SSLContext.cpp @@ -41,60 +41,65 @@ template struct custom_delete; template <> -struct custom_delete +struct custom_delete { explicit custom_delete() = default; - void operator() (RSA* rsa) const + void + operator()(RSA* rsa) const { - RSA_free (rsa); + RSA_free(rsa); } }; template <> -struct custom_delete +struct custom_delete { explicit custom_delete() = default; - void operator() (EVP_PKEY* evp_pkey) const + void + operator()(EVP_PKEY* evp_pkey) const { - EVP_PKEY_free (evp_pkey); + EVP_PKEY_free(evp_pkey); } }; template <> -struct custom_delete +struct custom_delete { explicit custom_delete() = default; - void operator() (X509* x509) const + void + operator()(X509* x509) const { - X509_free (x509); + X509_free(x509); } }; template <> -struct custom_delete +struct custom_delete { explicit custom_delete() = default; - void operator() (DH* dh) const + void + operator()(DH* dh) const { - DH_free (dh); + DH_free(dh); } }; template -using custom_delete_unique_ptr = std::unique_ptr >; +using custom_delete_unique_ptr = std::unique_ptr>; // RSA -using rsa_ptr = custom_delete_unique_ptr ; +using rsa_ptr = custom_delete_unique_ptr; -static rsa_ptr rsa_generate_key (int n_bits) +static rsa_ptr +rsa_generate_key(int n_bits) { #if OPENSSL_VERSION_NUMBER >= 0x00908000L - BIGNUM *bn = BN_new(); + BIGNUM* bn = BN_new(); BN_set_word(bn, RSA_F4); RSA* rsa = RSA_new(); @@ -106,86 +111,92 @@ static rsa_ptr rsa_generate_key (int n_bits) BN_free(bn); #else - RSA* rsa = RSA_generate_key (n_bits, RSA_F4, nullptr, nullptr); + RSA* rsa = RSA_generate_key(n_bits, RSA_F4, nullptr, nullptr); #endif if (rsa == nullptr) - LogicError ("RSA_generate_key failed"); + LogicError("RSA_generate_key failed"); - return rsa_ptr (rsa); + return rsa_ptr(rsa); } // EVP_PKEY -using evp_pkey_ptr = custom_delete_unique_ptr ; +using evp_pkey_ptr = custom_delete_unique_ptr; -static evp_pkey_ptr evp_pkey_new() +static evp_pkey_ptr +evp_pkey_new() { EVP_PKEY* evp_pkey = EVP_PKEY_new(); if (evp_pkey == nullptr) - LogicError ("EVP_PKEY_new failed"); + LogicError("EVP_PKEY_new failed"); - return evp_pkey_ptr (evp_pkey); + return evp_pkey_ptr(evp_pkey); } -static void evp_pkey_assign_rsa (EVP_PKEY* evp_pkey, rsa_ptr rsa) +static void +evp_pkey_assign_rsa(EVP_PKEY* evp_pkey, rsa_ptr rsa) { - if (! EVP_PKEY_assign_RSA (evp_pkey, rsa.get())) - LogicError ("EVP_PKEY_assign_RSA failed"); + if (!EVP_PKEY_assign_RSA(evp_pkey, rsa.get())) + LogicError("EVP_PKEY_assign_RSA failed"); rsa.release(); } // X509 -using x509_ptr = custom_delete_unique_ptr ; +using x509_ptr = custom_delete_unique_ptr; -static x509_ptr x509_new() +static x509_ptr +x509_new() { X509* x509 = X509_new(); if (x509 == nullptr) - LogicError ("X509_new failed"); + LogicError("X509_new failed"); - X509_set_version (x509, NID_X509); + X509_set_version(x509, NID_X509); - int const margin = 60 * 60; // 3600, one hour + int const margin = 60 * 60; // 3600, one hour int const length = 10 * 365.25 * 24 * 60 * 60; // 315576000, ten years - X509_gmtime_adj (X509_get_notBefore (x509), -margin); - X509_gmtime_adj (X509_get_notAfter (x509), length); + X509_gmtime_adj(X509_get_notBefore(x509), -margin); + X509_gmtime_adj(X509_get_notAfter(x509), length); - return x509_ptr (x509); + return x509_ptr(x509); } -static void x509_set_pubkey (X509* x509, EVP_PKEY* evp_pkey) +static void +x509_set_pubkey(X509* x509, EVP_PKEY* evp_pkey) { - X509_set_pubkey (x509, evp_pkey); + X509_set_pubkey(x509, evp_pkey); } -static void x509_sign (X509* x509, EVP_PKEY* evp_pkey) +static void +x509_sign(X509* x509, EVP_PKEY* evp_pkey) { - if (! X509_sign (x509, evp_pkey, EVP_sha1())) - LogicError ("X509_sign failed"); + if (!X509_sign(x509, evp_pkey, EVP_sha1())) + LogicError("X509_sign failed"); } -static void ssl_ctx_use_certificate (SSL_CTX* const ctx, x509_ptr cert) +static void +ssl_ctx_use_certificate(SSL_CTX* const ctx, x509_ptr cert) { - if (SSL_CTX_use_certificate (ctx, cert.get()) <= 0) - LogicError ("SSL_CTX_use_certificate failed"); + if (SSL_CTX_use_certificate(ctx, cert.get()) <= 0) + LogicError("SSL_CTX_use_certificate failed"); } -static void ssl_ctx_use_privatekey (SSL_CTX* const ctx, evp_pkey_ptr key) +static void +ssl_ctx_use_privatekey(SSL_CTX* const ctx, evp_pkey_ptr key) { - if (SSL_CTX_use_PrivateKey (ctx, key.get()) <= 0) - LogicError ("SSL_CTX_use_PrivateKey failed"); + if (SSL_CTX_use_PrivateKey(ctx, key.get()) <= 0) + LogicError("SSL_CTX_use_PrivateKey failed"); } #ifdef SSL_FLAGS_NO_RENEGOTIATE_CIPHERS -static -bool -disallowRenegotiation (SSL const* ssl, bool isNew) +static bool +disallowRenegotiation(SSL const* ssl, bool isNew) { // Track when SSL connections have last negotiated and // do not allow a connection to renegotiate more than @@ -193,53 +204,51 @@ disallowRenegotiation (SSL const* ssl, bool isNew) struct StaticData { std::mutex lock; - beast::aged_unordered_set set; + beast::aged_unordered_set set; - StaticData() - : set (ripple::stopwatch()) + StaticData() : set(ripple::stopwatch()) { } }; static StaticData sd; - std::lock_guard lock (sd.lock); - auto const expired (sd.set.clock().now() - std::chrono::minutes(4)); + std::lock_guard lock(sd.lock); + auto const expired(sd.set.clock().now() - std::chrono::minutes(4)); // Remove expired entries - for (auto iter (sd.set.chronological.begin ()); - (iter != sd.set.chronological.end ()) && (iter.when () <= expired); - iter = sd.set.chronological.begin ()) + for (auto iter(sd.set.chronological.begin()); + (iter != sd.set.chronological.end()) && (iter.when() <= expired); + iter = sd.set.chronological.begin()) { - sd.set.erase (iter); + sd.set.erase(iter); } - auto iter = sd.set.find (ssl); - if (iter != sd.set.end ()) + auto iter = sd.set.find(ssl); + if (iter != sd.set.end()) { - if (! isNew) + if (!isNew) { // This is a renegotiation and the last negotiation was recent return true; } - sd.set.touch (iter); + sd.set.touch(iter); } else { - sd.set.emplace (ssl); + sd.set.emplace(ssl); } return false; } -static -void -info_handler (SSL const* ssl, int event, int) +static void +info_handler(SSL const* ssl, int event, int) { #if OPENSSL_VERSION_NUMBER < 0x10100000L if ((ssl->s3) && (event & SSL_CB_HANDSHAKE_START)) { - if (disallowRenegotiation (ssl, SSL_in_before (ssl))) + if (disallowRenegotiation(ssl, SSL_in_before(ssl))) ssl->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS; } #else @@ -248,137 +257,134 @@ info_handler (SSL const* ssl, int event, int) } #endif -static -std::string -error_message (std::string const& what, - boost::system::error_code const& ec) +static std::string +error_message(std::string const& what, boost::system::error_code const& ec) { std::stringstream ss; - ss << - what << ": " << - ec.message() << - " (" << ec.value() << ")"; + ss << what << ": " << ec.message() << " (" << ec.value() << ")"; return ss.str(); } -static -void -initAnonymous ( - boost::asio::ssl::context& context) +static void +initAnonymous(boost::asio::ssl::context& context) { using namespace openssl; evp_pkey_ptr pkey = evp_pkey_new(); - evp_pkey_assign_rsa (pkey.get(), rsa_generate_key (2048)); + evp_pkey_assign_rsa(pkey.get(), rsa_generate_key(2048)); x509_ptr cert = x509_new(); - x509_set_pubkey (cert.get(), pkey.get()); - x509_sign (cert.get(), pkey.get()); + x509_set_pubkey(cert.get(), pkey.get()); + x509_sign(cert.get(), pkey.get()); SSL_CTX* const ctx = context.native_handle(); - ssl_ctx_use_certificate (ctx, std::move(cert)); - ssl_ctx_use_privatekey (ctx, std::move(pkey)); + ssl_ctx_use_certificate(ctx, std::move(cert)); + ssl_ctx_use_privatekey(ctx, std::move(pkey)); } -static -void -initAuthenticated ( +static void +initAuthenticated( boost::asio::ssl::context& context, std::string const& key_file, std::string const& cert_file, std::string const& chain_file) { - SSL_CTX* const ssl = context.native_handle (); + SSL_CTX* const ssl = context.native_handle(); bool cert_set = false; - if (! cert_file.empty ()) + if (!cert_file.empty()) { boost::system::error_code ec; - context.use_certificate_file ( + context.use_certificate_file( cert_file, boost::asio::ssl::context::pem, ec); if (ec) { - LogicError (error_message ( - "Problem with SSL certificate file.", ec).c_str()); + LogicError(error_message("Problem with SSL certificate file.", ec) + .c_str()); } cert_set = true; } - if (! chain_file.empty ()) + if (!chain_file.empty()) { // VFALCO Replace fopen() with RAII - FILE* f = fopen (chain_file.c_str (), "r"); + FILE* f = fopen(chain_file.c_str(), "r"); if (!f) { - LogicError (error_message ( - "Problem opening SSL chain file.", boost::system::error_code (errno, - boost::system::generic_category())).c_str()); + LogicError(error_message( + "Problem opening SSL chain file.", + boost::system::error_code( + errno, boost::system::generic_category())) + .c_str()); } try { for (;;) { - X509* const x = PEM_read_X509 (f, nullptr, nullptr, nullptr); + X509* const x = PEM_read_X509(f, nullptr, nullptr, nullptr); if (x == nullptr) break; - if (! cert_set) + if (!cert_set) { - if (SSL_CTX_use_certificate (ssl, x) != 1) - LogicError ("Problem retrieving SSL certificate from chain file."); + if (SSL_CTX_use_certificate(ssl, x) != 1) + LogicError( + "Problem retrieving SSL certificate from chain " + "file."); cert_set = true; } - else if (SSL_CTX_add_extra_chain_cert (ssl, x) != 1) + else if (SSL_CTX_add_extra_chain_cert(ssl, x) != 1) { - X509_free (x); - LogicError ("Problem adding SSL chain certificate."); + X509_free(x); + LogicError("Problem adding SSL chain certificate."); } } - fclose (f); + fclose(f); } catch (std::exception const&) { - fclose (f); - LogicError ("Reading the SSL chain file generated an exception."); + fclose(f); + LogicError("Reading the SSL chain file generated an exception."); } } - if (! key_file.empty ()) + if (!key_file.empty()) { boost::system::error_code ec; - context.use_private_key_file (key_file, - boost::asio::ssl::context::pem, ec); + context.use_private_key_file( + key_file, boost::asio::ssl::context::pem, ec); if (ec) { - LogicError (error_message ( - "Problem using the SSL private key file.", ec).c_str()); + LogicError( + error_message("Problem using the SSL private key file.", ec) + .c_str()); } } - if (SSL_CTX_check_private_key (ssl) != 1) + if (SSL_CTX_check_private_key(ssl) != 1) { - LogicError ("Invalid key in SSL private key file."); + LogicError("Invalid key in SSL private key file."); } } std::shared_ptr -get_context (std::string const& cipherList) +get_context(std::string const& cipherList) { - auto c = std::make_shared ( + auto c = std::make_shared( boost::asio::ssl::context::sslv23); - c->set_options ( + c->set_options( boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::no_sslv3 | @@ -386,18 +392,16 @@ get_context (std::string const& cipherList) { auto const& l = !cipherList.empty() ? cipherList : defaultCipherList; - auto result = SSL_CTX_set_cipher_list ( - c->native_handle (), l.c_str()); + auto result = SSL_CTX_set_cipher_list(c->native_handle(), l.c_str()); if (result != 1) - LogicError ("SSL_CTX_set_cipher_list failed"); + LogicError("SSL_CTX_set_cipher_list failed"); } // These are the raw DH parameters that Ripple Labs has // chosen for Ripple, in the binary format needed by // d2i_DHparams. // - unsigned char const params[] = - { + unsigned char const params[] = { 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0x8f, 0xca, 0x66, 0x85, 0x33, 0xcb, 0xcf, 0x36, 0x27, 0xb2, 0x4c, 0xb8, 0x50, 0xb8, 0xf9, 0x53, 0xf8, 0xb9, 0x2d, 0x1c, 0xa2, 0xad, 0x86, 0x58, 0x29, 0x3b, 0x88, @@ -420,26 +424,26 @@ get_context (std::string const& cipherList) 0x8d, 0xed, 0x44, 0xaa, 0x47, 0xaa, 0x52, 0xa2, 0xdb, 0xb6, 0xf5, 0xa1, 0x88, 0x85, 0xa1, 0xd5, 0x87, 0xb8, 0x07, 0xd3, 0x97, 0xbe, 0x37, 0x74, 0x72, 0xf1, 0xa8, 0x29, 0xf1, 0xa7, 0x7d, 0x19, 0xc3, 0x27, 0x09, 0xcf, - 0x23, 0x02, 0x01, 0x02 - }; + 0x23, 0x02, 0x01, 0x02}; - unsigned char const *data = ¶ms[0]; + unsigned char const* data = ¶ms[0]; - custom_delete_unique_ptr const dh {d2i_DHparams (nullptr, &data, sizeof(params))}; + custom_delete_unique_ptr const dh{ + d2i_DHparams(nullptr, &data, sizeof(params))}; if (!dh) - LogicError ("d2i_DHparams returned nullptr."); + LogicError("d2i_DHparams returned nullptr."); - SSL_CTX_set_tmp_dh (c->native_handle (), dh.get()); + SSL_CTX_set_tmp_dh(c->native_handle(), dh.get()); #ifdef SSL_FLAGS_NO_RENEGOTIATE_CIPHERS - SSL_CTX_set_info_callback (c->native_handle (), info_handler); + SSL_CTX_set_info_callback(c->native_handle(), info_handler); #endif return c; } -} // detail -} // openssl +} // namespace detail +} // namespace openssl //------------------------------------------------------------------------------ std::shared_ptr @@ -454,16 +458,15 @@ make_SSLContext(std::string const& cipherList) } std::shared_ptr -make_SSLContextAuthed ( +make_SSLContextAuthed( std::string const& keyFile, std::string const& certFile, std::string const& chainFile, std::string const& cipherList) { auto context = openssl::detail::get_context(cipherList); - openssl::detail::initAuthenticated(*context, - keyFile, certFile, chainFile); + openssl::detail::initAuthenticated(*context, keyFile, certFile, chainFile); return context; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/mulDiv.cpp b/src/ripple/basics/impl/mulDiv.cpp index 232817aaea..20e72e0477 100644 --- a/src/ripple/basics/impl/mulDiv.cpp +++ b/src/ripple/basics/impl/mulDiv.cpp @@ -17,14 +17,13 @@ */ //============================================================================== -#include #include +#include #include #include #include -namespace ripple -{ +namespace ripple { std::pair mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div) @@ -39,9 +38,9 @@ mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div) auto constexpr limit = std::numeric_limits::max(); if (result > limit) - return { false, limit }; + return {false, limit}; - return { true, static_cast(result) }; + return {true, static_cast(result)}; } -} // ripple +} // namespace ripple diff --git a/src/ripple/basics/impl/strHex.cpp b/src/ripple/basics/impl/strHex.cpp index 4920cb78df..66aa8949f0 100644 --- a/src/ripple/basics/impl/strHex.cpp +++ b/src/ripple/basics/impl/strHex.cpp @@ -23,24 +23,26 @@ namespace ripple { -int charUnHex (unsigned char c) +int +charUnHex(unsigned char c) { struct HexTab { int hex[256]; - HexTab () + HexTab() { - std::fill (std::begin (hex), std::end (hex), -1); + std::fill(std::begin(hex), std::end(hex), -1); for (int i = 0; i < 10; ++i) - hex ['0'+i] = i; + hex['0' + i] = i; for (int i = 0; i < 6; ++i) { - hex ['A'+i] = 10 + i; - hex ['a'+i] = 10 + i; + hex['A' + i] = 10 + i; + hex['a' + i] = 10 + i; } } - int operator[] (unsigned char c) const + int + operator[](unsigned char c) const { return hex[c]; } @@ -51,4 +53,4 @@ int charUnHex (unsigned char c) return xtab[c]; } -} +} // namespace ripple diff --git a/src/ripple/basics/make_SSLContext.h b/src/ripple/basics/make_SSLContext.h index db5127885b..c752a07585 100644 --- a/src/ripple/basics/make_SSLContext.h +++ b/src/ripple/basics/make_SSLContext.h @@ -27,18 +27,16 @@ namespace ripple { /** Create a self-signed SSL context that allows anonymous Diffie Hellman. */ std::shared_ptr -make_SSLContext( - std::string const& cipherList); +make_SSLContext(std::string const& cipherList); /** Create an authenticated SSL context using the specified files. */ std::shared_ptr -make_SSLContextAuthed ( +make_SSLContextAuthed( std::string const& keyFile, std::string const& certFile, std::string const& chainFile, std::string const& cipherList); - -} +} // namespace ripple #endif diff --git a/src/ripple/basics/mulDiv.h b/src/ripple/basics/mulDiv.h index 1ee788387c..30579c255a 100644 --- a/src/ripple/basics/mulDiv.h +++ b/src/ripple/basics/mulDiv.h @@ -23,8 +23,7 @@ #include #include -namespace ripple -{ +namespace ripple { /** Return value*mul/div accurately. Computes the result of the multiplication and division in @@ -42,6 +41,6 @@ namespace ripple std::pair mulDiv(std::uint64_t value, std::uint64_t mul, std::uint64_t div); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/qalloc.h b/src/ripple/basics/qalloc.h index 3bdc60f45e..f7a1a14f58 100644 --- a/src/ripple/basics/qalloc.h +++ b/src/ripple/basics/qalloc.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_BASICS_QALLOC_H_INCLUDED #define RIPPLE_BASICS_QALLOC_H_INCLUDED -#include #include +#include #include #include #include @@ -51,14 +51,14 @@ private: public: block* next; - block (block const&) = delete; - block& operator= (block const&) = delete; + block(block const&) = delete; + block& + operator=(block const&) = delete; - explicit - block (std::size_t bytes); + explicit block(std::size_t bytes); void* - allocate (std::size_t bytes, std::size_t align); + allocate(std::size_t bytes, std::size_t align); bool deallocate(); @@ -71,19 +71,20 @@ public: static constexpr auto block_size = kilobytes(256); qalloc_impl() = default; - qalloc_impl (qalloc_impl const&) = delete; - qalloc_impl& operator= (qalloc_impl const&) = delete; + qalloc_impl(qalloc_impl const&) = delete; + qalloc_impl& + operator=(qalloc_impl const&) = delete; ~qalloc_impl(); void* - allocate (std::size_t bytes, std::size_t align); + allocate(std::size_t bytes, std::size_t align); void - deallocate (void* p); + deallocate(void* p); }; -} // detail +} // namespace detail template class qalloc_type @@ -92,19 +93,15 @@ private: template friend class qalloc_type; - std::shared_ptr< - detail::qalloc_impl<>> impl_; + std::shared_ptr> impl_; public: using value_type = T; using pointer = T*; using const_pointer = T const*; - using reference = typename - std::add_lvalue_reference::type; - using const_reference = typename - std::add_lvalue_reference::type; - using propagate_on_container_move_assignment = - std::true_type; + using reference = typename std::add_lvalue_reference::type; + using const_reference = typename std::add_lvalue_reference::type; + using propagate_on_container_move_assignment = std::true_type; template struct rebind @@ -114,47 +111,47 @@ public: using other = qalloc_type; }; - qalloc_type (qalloc_type const&) = default; - qalloc_type (qalloc_type&& other) noexcept = default; - qalloc_type& operator= (qalloc_type const&) = default; - qalloc_type& operator= (qalloc_type&&) noexcept = default; + qalloc_type(qalloc_type const&) = default; + qalloc_type(qalloc_type&& other) noexcept = default; + qalloc_type& + operator=(qalloc_type const&) = default; + qalloc_type& + operator=(qalloc_type&&) noexcept = default; qalloc_type(); template - qalloc_type (qalloc_type const& u); + qalloc_type(qalloc_type const& u); template U* - alloc (std::size_t n); + alloc(std::size_t n); template void - dealloc (U* p, std::size_t n); + dealloc(U* p, std::size_t n); T* - allocate (std::size_t n); + allocate(std::size_t n); void - deallocate (T* p, std::size_t n); + deallocate(T* p, std::size_t n); template bool - operator== (qalloc_type const& u); + operator==(qalloc_type const& u); template bool - operator!= (qalloc_type const& u); + operator!=(qalloc_type const& u); qalloc_type select_on_container_copy_construction() const; private: - qalloc_type - select_on_copy(std::true_type) const; + qalloc_type select_on_copy(std::true_type) const; - qalloc_type - select_on_copy(std::false_type) const; + qalloc_type select_on_copy(std::false_type) const; }; /** Allocator optimized for delete in temporal order. @@ -174,38 +171,29 @@ using qalloc = qalloc_type; namespace detail { template -qalloc_impl<_>::block::block (std::size_t bytes) - : bytes_ (bytes - sizeof(*this)) - , remain_ (bytes_) - , free_ (reinterpret_cast< - std::uint8_t*>(this + 1)) +qalloc_impl<_>::block::block(std::size_t bytes) + : bytes_(bytes - sizeof(*this)) + , remain_(bytes_) + , free_(reinterpret_cast(this + 1)) { } template void* -qalloc_impl<_>::block::allocate( - std::size_t bytes, std::size_t align) +qalloc_impl<_>::block::allocate(std::size_t bytes, std::size_t align) { - align = std::max(align, - std::alignment_of::value); - auto pad = [](void const* p, std::size_t a) - { - auto const i = reinterpret_cast< - std::uintptr_t>(p); + align = std::max(align, std::alignment_of::value); + auto pad = [](void const* p, std::size_t a) { + auto const i = reinterpret_cast(p); return (a - (i % a)) % a; }; - auto const n0 = - pad(free_ + sizeof(block*), align); - auto const n1 = - n0 + sizeof(block*) + bytes; + auto const n0 = pad(free_ + sizeof(block*), align); + auto const n1 = n0 + sizeof(block*) + bytes; if (remain_ < n1) return nullptr; - auto p = reinterpret_cast( - free_ + n0 + sizeof(block*)); - assert(pad(p - 1, - std::alignment_of::value) == 0); + auto p = reinterpret_cast(free_ + n0 + sizeof(block*)); + assert(pad(p - 1, std::alignment_of::value) == 0); p[-1] = this; ++count_; free_ += n1; @@ -221,8 +209,7 @@ qalloc_impl<_>::block::deallocate() if (count_ > 0) return false; remain_ = bytes_; - free_ = reinterpret_cast< - std::uint8_t*>(this + 1); + free_ = reinterpret_cast(this + 1); return true; } @@ -245,21 +232,18 @@ qalloc_impl<_>::~qalloc_impl() template void* -qalloc_impl<_>::allocate( - std::size_t bytes, std::size_t align) +qalloc_impl<_>::allocate(std::size_t bytes, std::size_t align) { if (used_) { - auto const p = - used_->allocate(bytes, align); + auto const p = used_->allocate(bytes, align); if (p) return p; used_ = nullptr; } if (free_) { - auto const p = - free_->allocate(bytes, align); + auto const p = free_->allocate(bytes, align); if (p) { used_ = free_; @@ -270,13 +254,12 @@ qalloc_impl<_>::allocate( std::size_t const adj_align = std::max(align, std::alignment_of::value); std::size_t const min_alloc = // align up - ((sizeof (block) + sizeof (block*) + bytes) + (adj_align - 1)) & + ((sizeof(block) + sizeof(block*) + bytes) + (adj_align - 1)) & ~(adj_align - 1); auto const n = std::max(block_size, min_alloc); - block* const b = - new(std::malloc(n)) block(n); - if (! b) - Throw (); + block* const b = new (std::malloc(n)) block(n); + if (!b) + Throw(); used_ = b; // VFALCO This has to succeed return used_->allocate(bytes, align); @@ -284,10 +267,9 @@ qalloc_impl<_>::allocate( template void -qalloc_impl<_>::deallocate (void* p) +qalloc_impl<_>::deallocate(void* p) { - auto const b = - reinterpret_cast(p)[-1]; + auto const b = reinterpret_cast(p)[-1]; if (b->deallocate()) { if (used_ == b) @@ -297,110 +279,94 @@ qalloc_impl<_>::deallocate (void* p) } } -} // detail +} // namespace detail //------------------------------------------------------------------------------ template qalloc_type::qalloc_type() - : impl_ (std::make_shared< - detail::qalloc_impl<>>()) + : impl_(std::make_shared>()) { } template template -qalloc_type::qalloc_type( - qalloc_type const& u) - : impl_ (u.impl_) +qalloc_type::qalloc_type(qalloc_type const& u) + : impl_(u.impl_) { } template template U* -qalloc_type::alloc (std::size_t n) +qalloc_type::alloc(std::size_t n) { - if (n > std::numeric_limits< - std::size_t>::max() / sizeof(U)) - Throw (); + if (n > std::numeric_limits::max() / sizeof(U)) + Throw(); auto const bytes = n * sizeof(U); - return static_cast( - impl_->allocate(bytes, - std::alignment_of::value)); + return static_cast(impl_->allocate(bytes, std::alignment_of::value)); } template template -inline -void -qalloc_type::dealloc( - U* p, std::size_t n) +inline void +qalloc_type::dealloc(U* p, std::size_t n) { impl_->deallocate(p); } template T* -qalloc_type::allocate (std::size_t n) +qalloc_type::allocate(std::size_t n) { return alloc(n); } template -inline -void -qalloc_type::deallocate( - T* p, std::size_t n) +inline void +qalloc_type::deallocate(T* p, std::size_t n) { dealloc(p, n); } template template -inline -bool -qalloc_type::operator==( - qalloc_type const& u) +inline bool +qalloc_type::operator==(qalloc_type const& u) { return impl_.get() == u.impl_.get(); } template template -inline -bool -qalloc_type::operator!=( - qalloc_type const& u) +inline bool +qalloc_type::operator!=(qalloc_type const& u) { - return ! (*this == u); + return !(*this == u); } template auto -qalloc_type::select_on_container_copy_construction() const -> - qalloc_type +qalloc_type::select_on_container_copy_construction() const + -> qalloc_type { - return select_on_copy( - std::integral_constant{}); + return select_on_copy(std::integral_constant{}); } template -auto -qalloc_type::select_on_copy(std::true_type) const -> - qalloc_type +auto qalloc_type::select_on_copy(std::true_type) const + -> qalloc_type { - return *this; // shared arena + return *this; // shared arena } template -auto -qalloc_type::select_on_copy(std::false_type) const -> - qalloc_type +auto qalloc_type::select_on_copy(std::false_type) const + -> qalloc_type { - return {}; // new arena + return {}; // new arena } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/random.h b/src/ripple/basics/random.h index 516eeefa75..e149503a00 100644 --- a/src/ripple/basics/random.h +++ b/src/ripple/basics/random.h @@ -20,38 +20,36 @@ #ifndef RIPPLE_BASICS_RANDOM_H_INCLUDED #define RIPPLE_BASICS_RANDOM_H_INCLUDED +#include // #include #include #include #include #include -#include #include #include -#include // +#include namespace ripple { #ifndef __INTELLISENSE__ -static_assert ( - std::is_integral ::value && - std::is_unsigned ::value, - "The Ripple default PRNG engine must return an unsigned integral type."); +static_assert( + std::is_integral::value && + std::is_unsigned::value, + "The Ripple default PRNG engine must return an unsigned integral type."); -static_assert ( +static_assert( std::numeric_limits::max() >= - std::numeric_limits::max(), - "The Ripple default PRNG engine return must be at least 64 bits wide."); + std::numeric_limits::max(), + "The Ripple default PRNG engine return must be at least 64 bits wide."); #endif namespace detail { - // Determines if a type can be called like an Engine template -using is_engine = - std::is_invocable_r; -} +using is_engine = std::is_invocable_r; +} // namespace detail /** Return the default random engine. @@ -63,13 +61,11 @@ using is_engine = Each thread gets its own instance of the engine which will be randomly seeded. */ -inline -beast::xor_shift_engine& -default_prng () +inline beast::xor_shift_engine& +default_prng() { // This is used to seed the thread-specific PRNGs on demand - static beast::xor_shift_engine seeder = [] - { + static beast::xor_shift_engine seeder = [] { std::random_device rng; std::uniform_int_distribution distribution{1}; return beast::xor_shift_engine(distribution(rng)); @@ -79,8 +75,7 @@ default_prng () static std::mutex m; // The thread-specific PRNGs: - thread_local beast::xor_shift_engine engine = [] - { + thread_local beast::xor_shift_engine engine = [] { std::uint64_t seed; { std::lock_guard lk(m); @@ -115,15 +110,11 @@ default_prng () /** @{ */ template std::enable_if_t< - std::is_integral::value && - detail::is_engine::value, -Integral> -rand_int ( - Engine& engine, - Integral min, - Integral max) + std::is_integral::value && detail::is_engine::value, + Integral> +rand_int(Engine& engine, Integral min, Integral max) { - assert (max > min); + assert(max > min); // This should have no state and constructing it should // be very cheap. If that turns out not to be the case @@ -133,73 +124,60 @@ rand_int ( template std::enable_if_t::value, Integral> -rand_int ( - Integral min, - Integral max) +rand_int(Integral min, Integral max) { - return rand_int (default_prng(), min, max); + return rand_int(default_prng(), min, max); } template std::enable_if_t< - std::is_integral::value && - detail::is_engine::value, -Integral> -rand_int ( - Engine& engine, - Integral max) + std::is_integral::value && detail::is_engine::value, + Integral> +rand_int(Engine& engine, Integral max) { - return rand_int (engine, Integral(0), max); + return rand_int(engine, Integral(0), max); } template std::enable_if_t::value, Integral> -rand_int (Integral max) +rand_int(Integral max) { - return rand_int (default_prng(), max); + return rand_int(default_prng(), max); } template std::enable_if_t< - std::is_integral::value && - detail::is_engine::value, -Integral> -rand_int ( - Engine& engine) + std::is_integral::value && detail::is_engine::value, + Integral> +rand_int(Engine& engine) { - return rand_int ( - engine, - std::numeric_limits::max()); + return rand_int(engine, std::numeric_limits::max()); } template std::enable_if_t::value, Integral> -rand_int () +rand_int() { - return rand_int ( - default_prng(), - std::numeric_limits::max()); + return rand_int(default_prng(), std::numeric_limits::max()); } /** @} */ /** Return a random boolean value */ /** @{ */ template -inline -bool -rand_bool (Engine& engine) +inline bool +rand_bool(Engine& engine) { - return rand_int (engine, 1) == 1; + return rand_int(engine, 1) == 1; } -inline -bool -rand_bool () +inline bool +rand_bool() { - return rand_bool (default_prng()); + return rand_bool(default_prng()); } /** @} */ -} // ripple +} // namespace ripple -#endif // RIPPLE_BASICS_RANDOM_H_INCLUDED +#endif // RIPPLE_BASICS_RANDOM_H_INCLUDED diff --git a/src/ripple/basics/safe_cast.h b/src/ripple/basics/safe_cast.h index 0e1d1aada8..d4bbefec30 100644 --- a/src/ripple/basics/safe_cast.h +++ b/src/ripple/basics/safe_cast.h @@ -29,58 +29,42 @@ namespace ripple { // the destination can hold all values of the source. This is particularly // handy when the source or destination is an enumeration type. -template +template static constexpr bool is_safetocasttovalue_v = - ( std::is_integral_v && - std::is_integral_v ) && - ( std::is_signed::value || - std::is_unsigned::value ) && - ( std::is_signed::value != - std::is_signed::value ? - sizeof(Dest) > sizeof(Src) : - sizeof(Dest) >= sizeof(Src) ); + (std::is_integral_v && std::is_integral_v)&&( + std::is_signed::value || std::is_unsigned::value) && + (std::is_signed::value != std::is_signed::value + ? sizeof(Dest) > sizeof(Src) + : sizeof(Dest) >= sizeof(Src)); template -inline -constexpr -std::enable_if_t -< - std::is_integral_v && std::is_integral_v, - Dest -> -safe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_integral_v, Dest> + safe_cast(Src s) noexcept { - static_assert(std::is_signed_v || std::is_unsigned_v, + static_assert( + std::is_signed_v || std::is_unsigned_v, "Cannot cast signed to unsigned"); - constexpr unsigned not_same = std::is_signed_v != - std::is_signed_v; - static_assert(sizeof(Dest) >= sizeof(Src) + not_same, + constexpr unsigned not_same = + std::is_signed_v != std::is_signed_v; + static_assert( + sizeof(Dest) >= sizeof(Src) + not_same, "Destination is too small to hold all values of source"); return static_cast(s); } template -inline -constexpr -std::enable_if_t -< - std::is_enum_v && std::is_integral_v, - Dest -> -safe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_integral_v, Dest> + safe_cast(Src s) noexcept { return static_cast(safe_cast>(s)); } template -inline -constexpr -std::enable_if_t -< - std::is_integral_v && std::is_enum_v, - Dest -> -safe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_enum_v, Dest> + safe_cast(Src s) noexcept { return safe_cast(static_cast>(s)); } @@ -90,47 +74,33 @@ safe_cast(Src s) noexcept // underlying types become safe, it can be converted to a safe_cast. template -inline -constexpr -std::enable_if_t -< - std::is_integral_v && std::is_integral_v, - Dest -> -unsafe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_integral_v, Dest> + unsafe_cast(Src s) noexcept { - static_assert(! is_safetocasttovalue_v, + static_assert( + !is_safetocasttovalue_v, "Only unsafe if casting signed to unsigned or " "destination is too small"); return static_cast(s); } template -inline -constexpr -std::enable_if_t -< - std::is_enum_v && std::is_integral_v, - Dest -> -unsafe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_integral_v, Dest> + unsafe_cast(Src s) noexcept { return static_cast(unsafe_cast>(s)); } template -inline -constexpr -std::enable_if_t -< - std::is_integral_v && std::is_enum_v, - Dest -> -unsafe_cast(Src s) noexcept +inline constexpr std:: + enable_if_t && std::is_enum_v, Dest> + unsafe_cast(Src s) noexcept { return unsafe_cast(static_cast>(s)); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/basics/strHex.h b/src/ripple/basics/strHex.h index 3620f2675e..f3618a359d 100644 --- a/src/ripple/basics/strHex.h +++ b/src/ripple/basics/strHex.h @@ -37,14 +37,12 @@ namespace ripple { @param iDigit 0-15 inclusive @return a character from '0'-'9' or 'A'-'F'. */ -inline -char -charHex (unsigned int digit) +inline char +charHex(unsigned int digit) { - static - char const xtab[] = "0123456789ABCDEF"; + static char const xtab[] = "0123456789ABCDEF"; - assert (digit < 16); + assert(digit < 16); return xtab[digit]; } @@ -55,13 +53,12 @@ charHex (unsigned int digit) @return an integer from 0 to 15 on success; -1 on failure. */ int -charUnHex (unsigned char c); +charUnHex(unsigned char c); -inline -int -charUnHex (char c) +inline int +charUnHex(char c) { - return charUnHex (static_cast(c)); + return charUnHex(static_cast(c)); } /** @} */ @@ -81,19 +78,21 @@ strHex(FwdIt begin, FwdIt end) } template ().begin())> -std::string strHex(T const& from) +std::string +strHex(T const& from) { return strHex(from.begin(), from.end()); } -inline std::string strHex (const std::uint64_t uiHost) +inline std::string +strHex(const std::uint64_t uiHost) { - uint64_t uBig = boost::endian::native_to_big (uiHost); + uint64_t uBig = boost::endian::native_to_big(uiHost); - auto const begin = (unsigned char*) &uBig; - auto const end = begin + sizeof(uBig); + auto const begin = (unsigned char*)&uBig; + auto const end = begin + sizeof(uBig); return strHex(begin, end); } -} +} // namespace ripple #endif diff --git a/src/ripple/basics/tagged_integer.h b/src/ripple/basics/tagged_integer.h index 4afe0847a9..fe11f88272 100644 --- a/src/ripple/basics/tagged_integer.h +++ b/src/ripple/basics/tagged_integer.h @@ -52,7 +52,6 @@ class tagged_integer tagged_integer, boost::shiftable>>>>> { - private: Int m_value; @@ -67,10 +66,7 @@ public: class = typename std::enable_if< std::is_integral::value && sizeof(OtherInt) <= sizeof(Int)>::type> - explicit - constexpr - tagged_integer(OtherInt value) noexcept - : m_value(value) + explicit constexpr tagged_integer(OtherInt value) noexcept : m_value(value) { static_assert( sizeof(tagged_integer) == sizeof(Int), @@ -78,13 +74,13 @@ public: } bool - operator<(const tagged_integer & rhs) const noexcept + operator<(const tagged_integer& rhs) const noexcept { return m_value < rhs.m_value; } bool - operator==(const tagged_integer & rhs) const noexcept + operator==(const tagged_integer& rhs) const noexcept { return m_value == rhs.m_value; } @@ -178,50 +174,46 @@ public: } tagged_integer& - operator++ () noexcept + operator++() noexcept { ++m_value; return *this; } tagged_integer& - operator-- () noexcept + operator--() noexcept { --m_value; return *this; } - explicit - operator Int() const noexcept + explicit operator Int() const noexcept { return m_value; } - friend - std::ostream& - operator<< (std::ostream& s, tagged_integer const& t) + friend std::ostream& + operator<<(std::ostream& s, tagged_integer const& t) { s << t.m_value; return s; } - friend - std::istream& - operator>> (std::istream& s, tagged_integer& t) + friend std::istream& + operator>>(std::istream& s, tagged_integer& t) { s >> t.m_value; return s; } - friend - std::string + friend std::string to_string(tagged_integer const& t) { return std::to_string(t.m_value); } }; -} // ripple +} // namespace ripple namespace beast { template @@ -231,6 +223,5 @@ struct is_contiguously_hashable, HashAlgorithm> explicit is_contiguously_hashable() = default; }; -} // beast +} // namespace beast #endif - diff --git a/src/ripple/beast/asio/io_latency_probe.h b/src/ripple/beast/asio/io_latency_probe.h index 2357408a10..b18fb823e3 100644 --- a/src/ripple/beast/asio/io_latency_probe.h +++ b/src/ripple/beast/asio/io_latency_probe.h @@ -47,30 +47,31 @@ private: bool m_cancel; public: - io_latency_probe (duration const& period, - boost::asio::io_service& ios) - : m_count (1) - , m_period (period) - , m_ios (ios) - , m_timer (m_ios) - , m_cancel (false) + io_latency_probe(duration const& period, boost::asio::io_service& ios) + : m_count(1) + , m_period(period) + , m_ios(ios) + , m_timer(m_ios) + , m_cancel(false) { } - ~io_latency_probe () + ~io_latency_probe() { - std::unique_lock lock (m_mutex); - cancel (lock, true); + std::unique_lock lock(m_mutex); + cancel(lock, true); } /** Return the io_service associated with the latency probe. */ /** @{ */ - boost::asio::io_service& get_io_service () + boost::asio::io_service& + get_io_service() { return m_ios; } - boost::asio::io_service const& get_io_service () const + boost::asio::io_service const& + get_io_service() const { return m_ios; } @@ -80,16 +81,18 @@ public: Any handlers which have already been queued will still be called. */ /** @{ */ - void cancel () + void + cancel() { - std::unique_lock lock (m_mutex); - cancel (lock, true); + std::unique_lock lock(m_mutex); + cancel(lock, true); } - void cancel_async () + void + cancel_async() { - std::unique_lock lock (m_mutex); - cancel (lock, false); + std::unique_lock lock(m_mutex); + cancel(lock, false); } /** @} */ @@ -98,14 +101,14 @@ public: void Handler (Duration d); */ template - void sample_one (Handler&& handler) + void + sample_one(Handler&& handler) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); if (m_cancel) - throw std::logic_error ("io_latency_probe is canceled"); - m_ios.post (sample_op ( - std::forward (handler), - Clock::now(), false, this)); + throw std::logic_error("io_latency_probe is canceled"); + m_ios.post(sample_op( + std::forward(handler), Clock::now(), false, this)); } /** Initiate continuous i/o latency sampling. @@ -113,21 +116,21 @@ public: void Handler (std::chrono::milliseconds); */ template - void sample (Handler&& handler) + void + sample(Handler&& handler) { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); if (m_cancel) - throw std::logic_error ("io_latency_probe is canceled"); - m_ios.post (sample_op ( - std::forward (handler), - Clock::now(), true, this)); + throw std::logic_error("io_latency_probe is canceled"); + m_ios.post(sample_op( + std::forward(handler), Clock::now(), true, this)); } private: - void cancel (std::unique_lock & lock, - bool wait) + void + cancel(std::unique_lock& lock, bool wait) { - if (! m_cancel) + if (!m_cancel) { --m_count; m_cancel = true; @@ -136,24 +139,25 @@ private: if (wait) #ifdef BOOST_NO_CXX11_LAMBDAS while (m_count != 0) - m_cond.wait (lock); + m_cond.wait(lock); #else - m_cond.wait (lock, [this] { - return this->m_count == 0; }); + m_cond.wait(lock, [this] { return this->m_count == 0; }); #endif } - void addref () + void + addref() { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); ++m_count; } - void release () + void + release() { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); if (--m_count == 0) - m_cond.notify_all (); + m_cond.notify_all(); } template @@ -164,48 +168,54 @@ private: bool m_repeat; io_latency_probe* m_probe; - sample_op (Handler const& handler, time_point const& start, - bool repeat, io_latency_probe* probe) - : m_handler (handler) - , m_start (start) - , m_repeat (repeat) - , m_probe (probe) + sample_op( + Handler const& handler, + time_point const& start, + bool repeat, + io_latency_probe* probe) + : m_handler(handler) + , m_start(start) + , m_repeat(repeat) + , m_probe(probe) { assert(m_probe); m_probe->addref(); } - sample_op (sample_op&& from) noexcept - : m_handler (std::move(from.m_handler)) - , m_start (from.m_start) - , m_repeat (from.m_repeat) - , m_probe (from.m_probe) + sample_op(sample_op&& from) noexcept + : m_handler(std::move(from.m_handler)) + , m_start(from.m_start) + , m_repeat(from.m_repeat) + , m_probe(from.m_probe) { assert(m_probe); from.m_probe = nullptr; } - sample_op (sample_op const&) = delete; - sample_op operator= (sample_op const&) = delete; - sample_op& operator= (sample_op&&) = delete; + sample_op(sample_op const&) = delete; + sample_op + operator=(sample_op const&) = delete; + sample_op& + operator=(sample_op&&) = delete; - ~sample_op () + ~sample_op() { - if(m_probe) + if (m_probe) m_probe->release(); } - void operator() () const + void + operator()() const { if (!m_probe) return; - typename Clock::time_point const now (Clock::now()); - typename Clock::duration const elapsed (now - m_start); + typename Clock::time_point const now(Clock::now()); + typename Clock::duration const elapsed(now - m_start); - m_handler (elapsed); + m_handler(elapsed); { - std::lock_guard lock (m_probe->m_mutex); + std::lock_guard lock(m_probe->m_mutex); if (m_probe->m_cancel) return; } @@ -215,7 +225,7 @@ private: // Calculate when we want to sample again, and // adjust for the expected latency. // - typename Clock::time_point const when ( + typename Clock::time_point const when( now + m_probe->m_period - 2 * elapsed); if (when <= now) @@ -223,29 +233,30 @@ private: // The latency is too high to maintain the desired // period so don't bother with a timer. // - m_probe->m_ios.post (sample_op ( - m_handler, now, m_repeat, m_probe)); + m_probe->m_ios.post( + sample_op(m_handler, now, m_repeat, m_probe)); } else { m_probe->m_timer.expires_from_now(when - now); - m_probe->m_timer.async_wait (sample_op ( - m_handler, now, m_repeat, m_probe)); + m_probe->m_timer.async_wait( + sample_op(m_handler, now, m_repeat, m_probe)); } } } - void operator () (boost::system::error_code const& ec) + void + operator()(boost::system::error_code const& ec) { if (!m_probe) return; - typename Clock::time_point const now (Clock::now()); - m_probe->m_ios.post (sample_op ( - m_handler, now, m_repeat, m_probe)); + typename Clock::time_point const now(Clock::now()); + m_probe->m_ios.post( + sample_op(m_handler, now, m_repeat, m_probe)); } }; }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/clock/abstract_clock.h b/src/ripple/beast/clock/abstract_clock.h index 8f4c319107..e543d6d7bb 100644 --- a/src/ripple/beast/clock/abstract_clock.h +++ b/src/ripple/beast/clock/abstract_clock.h @@ -70,7 +70,8 @@ public: abstract_clock(abstract_clock const&) = default; /** Returns the current time. */ - virtual time_point now() const = 0; + virtual time_point + now() const = 0; }; //------------------------------------------------------------------------------ @@ -78,8 +79,7 @@ public: namespace detail { template -struct abstract_clock_wrapper - : public abstract_clock +struct abstract_clock_wrapper : public abstract_clock { explicit abstract_clock_wrapper() = default; @@ -93,7 +93,7 @@ struct abstract_clock_wrapper } }; -} +} // namespace detail //------------------------------------------------------------------------------ @@ -102,7 +102,7 @@ struct abstract_clock_wrapper http://en.cppreference.com/w/cpp/concept/Clock @tparam Clock The actual concrete clock to use. */ -template +template abstract_clock& get_abstract_clock() { @@ -110,6 +110,6 @@ get_abstract_clock() return clock; } -} +} // namespace beast #endif diff --git a/src/ripple/beast/clock/basic_seconds_clock.h b/src/ripple/beast/clock/basic_seconds_clock.h index 25c184c4be..e26f970d6b 100644 --- a/src/ripple/beast/clock/basic_seconds_clock.h +++ b/src/ripple/beast/clock/basic_seconds_clock.h @@ -36,11 +36,13 @@ namespace detail { class seconds_clock_worker { public: - virtual void sample () = 0; + virtual void + sample() = 0; virtual ~seconds_clock_worker() = default; seconds_clock_worker() = default; seconds_clock_worker(seconds_clock_worker const&) = delete; - seconds_clock_worker& operator=(seconds_clock_worker const&) = delete; + seconds_clock_worker& + operator=(seconds_clock_worker const&) = delete; }; //------------------------------------------------------------------------------ @@ -51,11 +53,11 @@ class seconds_clock_thread public: using mutex = std::mutex; using cond_var = std::condition_variable; - using unique_lock = std::unique_lock ; + using unique_lock = std::unique_lock; using clock_type = std::chrono::steady_clock; using seconds = std::chrono::seconds; using thread = std::thread; - using workers = std::vector ; + using workers = std::vector; bool stop_; mutex mutex_; @@ -63,36 +65,37 @@ public: workers workers_; thread thread_; - seconds_clock_thread () - : stop_ (false) + seconds_clock_thread() : stop_(false) { - thread_ = thread (&seconds_clock_thread::run, this); + thread_ = thread(&seconds_clock_thread::run, this); } - ~seconds_clock_thread () + ~seconds_clock_thread() { stop(); } - void add (seconds_clock_worker& w) + void + add(seconds_clock_worker& w) { - std::lock_guard lock (mutex_); - workers_.push_back (&w); + std::lock_guard lock(mutex_); + workers_.push_back(&w); } - void remove (seconds_clock_worker& w) + void + remove(seconds_clock_worker& w) { - std::lock_guard lock (mutex_); - workers_.erase (std::find ( - workers_.begin (), workers_.end(), &w)); + std::lock_guard lock(mutex_); + workers_.erase(std::find(workers_.begin(), workers_.end(), &w)); } - void stop() + void + stop() { if (thread_.joinable()) { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); stop_ = true; } cond_.notify_all(); @@ -100,9 +103,11 @@ public: } } - void run() + void + run() { - unique_lock lock (mutex_);; + unique_lock lock(mutex_); + ; for (;;) { @@ -110,24 +115,24 @@ public: iter->sample(); using namespace std::chrono; - clock_type::time_point const when ( - date::floor ( - clock_type::now().time_since_epoch()) + - seconds (1)); + clock_type::time_point const when( + date::floor(clock_type::now().time_since_epoch()) + + seconds(1)); - if (cond_.wait_until (lock, when, [this]{ return stop_; })) + if (cond_.wait_until(lock, when, [this] { return stop_; })) return; } } - static seconds_clock_thread& instance () + static seconds_clock_thread& + instance() { static seconds_clock_thread singleton; return singleton; } }; -} +} // namespace detail //------------------------------------------------------------------------------ @@ -136,8 +141,7 @@ public: http://connect.microsoft.com/VisualStudio/feedback/details/786016/creating-a-global-c-object-that-used-thread-join-in-its-destructor-causes-a-lockup http://stackoverflow.com/questions/10915233/stdthreadjoin-hangs-if-called-after-main-exits-when-using-vs2012-rc */ -inline -void +inline void basic_seconds_clock_main_hook() { #ifdef _MSC_VER @@ -167,14 +171,15 @@ public: static bool const is_steady = Clock::is_steady; - static time_point now() + static time_point + now() { // Make sure the thread is constructed before the // worker otherwise we will crash during destruction // of objects with static storage duration. struct initializer { - initializer () + initializer() { detail::seconds_clock_thread::instance(); } @@ -186,8 +191,7 @@ public: time_point m_now; std::mutex mutex_; - worker() - : m_now(Clock::now()) + worker() : m_now(Clock::now()) { detail::seconds_clock_thread::instance().add(*this); } @@ -197,15 +201,17 @@ public: detail::seconds_clock_thread::instance().remove(*this); } - time_point now() + time_point + now() { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); return m_now; } - void sample() override + void + sample() override { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); m_now = Clock::now(); } }; @@ -216,6 +222,6 @@ public: } }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/clock/manual_clock.h b/src/ripple/beast/clock/manual_clock.h index 96baae4fcd..808b007100 100644 --- a/src/ripple/beast/clock/manual_clock.h +++ b/src/ripple/beast/clock/manual_clock.h @@ -35,8 +35,7 @@ namespace beast { http://en.cppreference.com/w/cpp/concept/Clock */ template -class manual_clock - : public abstract_clock +class manual_clock : public abstract_clock { public: using typename abstract_clock::rep; @@ -47,8 +46,7 @@ private: time_point now_; public: - explicit - manual_clock (time_point const& now = time_point(duration(0))) + explicit manual_clock(time_point const& now = time_point(duration(0))) : now_(now) { } @@ -61,9 +59,9 @@ public: /** Set the current time of the manual clock. */ void - set (time_point const& when) + set(time_point const& when) { - assert(! Clock::is_steady || when >= now_); + assert(!Clock::is_steady || when >= now_); now_ = when; } @@ -72,8 +70,7 @@ public: void set(Integer seconds_from_epoch) { - set(time_point(duration( - std::chrono::seconds(seconds_from_epoch)))); + set(time_point(duration(std::chrono::seconds(seconds_from_epoch)))); } /** Advance the clock by a duration. */ @@ -81,20 +78,19 @@ public: void advance(std::chrono::duration const& elapsed) { - assert(! Clock::is_steady || - (now_ + elapsed) >= now_); + assert(!Clock::is_steady || (now_ + elapsed) >= now_); now_ += elapsed; } /** Convenience for advancing the clock by one second. */ manual_clock& - operator++ () + operator++() { advance(std::chrono::seconds(1)); return *this; } }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/aged_container.h b/src/ripple/beast/container/aged_container.h index 484b59a845..38a50b4e84 100644 --- a/src/ripple/beast/container/aged_container.h +++ b/src/ripple/beast/container/aged_container.h @@ -25,12 +25,11 @@ namespace beast { template -struct is_aged_container - : std::false_type +struct is_aged_container : std::false_type { explicit is_aged_container() = default; }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/aged_container_utility.h b/src/ripple/beast/container/aged_container_utility.h index cbc609eea6..dc57c229fb 100644 --- a/src/ripple/beast/container/aged_container_utility.h +++ b/src/ripple/beast/container/aged_container_utility.h @@ -28,24 +28,21 @@ namespace beast { /** Expire aged container items past the specified age. */ template -typename std::enable_if < - is_aged_container ::value, - std::size_t ->::type -expire (AgedContainer& c, std::chrono::duration const& age) +typename std::enable_if::value, std::size_t>:: + type + expire(AgedContainer& c, std::chrono::duration const& age) { - std::size_t n (0); - auto const expired (c.clock().now() - age); - for (auto iter (c.chronological.cbegin()); - iter != c.chronological.cend() && - iter.when() <= expired;) + std::size_t n(0); + auto const expired(c.clock().now() - age); + for (auto iter(c.chronological.cbegin()); + iter != c.chronological.cend() && iter.when() <= expired;) { - iter = c.erase (iter); + iter = c.erase(iter); ++n; } return n; } -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/aged_map.h b/src/ripple/beast/container/aged_map.h index 051eed5c41..f675846fa6 100644 --- a/src/ripple/beast/container/aged_map.h +++ b/src/ripple/beast/container/aged_map.h @@ -32,11 +32,10 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Compare = std::less , - class Allocator = std::allocator > -> -using aged_map = detail::aged_ordered_container < - false, true, Key, T, Clock, Compare, Allocator>; + class Compare = std::less, + class Allocator = std::allocator>> +using aged_map = detail:: + aged_ordered_container; } diff --git a/src/ripple/beast/container/aged_multimap.h b/src/ripple/beast/container/aged_multimap.h index 8b475c1d8b..b4668851c3 100644 --- a/src/ripple/beast/container/aged_multimap.h +++ b/src/ripple/beast/container/aged_multimap.h @@ -32,11 +32,10 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Compare = std::less , - class Allocator = std::allocator > -> -using aged_multimap = detail::aged_ordered_container < - true, true, Key, T, Clock, Compare, Allocator>; + class Compare = std::less, + class Allocator = std::allocator>> +using aged_multimap = detail:: + aged_ordered_container; } diff --git a/src/ripple/beast/container/aged_multiset.h b/src/ripple/beast/container/aged_multiset.h index 378380e2b3..6d62bcbeb3 100644 --- a/src/ripple/beast/container/aged_multiset.h +++ b/src/ripple/beast/container/aged_multiset.h @@ -31,11 +31,10 @@ namespace beast { template < class Key, class Clock = std::chrono::steady_clock, - class Compare = std::less , - class Allocator = std::allocator -> -using aged_multiset = detail::aged_ordered_container < - true, false, Key, void, Clock, Compare, Allocator>; + class Compare = std::less, + class Allocator = std::allocator> +using aged_multiset = detail:: + aged_ordered_container; } diff --git a/src/ripple/beast/container/aged_set.h b/src/ripple/beast/container/aged_set.h index 1b07244b19..0a9d82f6cb 100644 --- a/src/ripple/beast/container/aged_set.h +++ b/src/ripple/beast/container/aged_set.h @@ -31,11 +31,10 @@ namespace beast { template < class Key, class Clock = std::chrono::steady_clock, - class Compare = std::less , - class Allocator = std::allocator -> -using aged_set = detail::aged_ordered_container < - false, false, Key, void, Clock, Compare, Allocator>; + class Compare = std::less, + class Allocator = std::allocator> +using aged_set = detail:: + aged_ordered_container; } diff --git a/src/ripple/beast/container/aged_unordered_map.h b/src/ripple/beast/container/aged_unordered_map.h index c78e2c9a23..1b6a147ef0 100644 --- a/src/ripple/beast/container/aged_unordered_map.h +++ b/src/ripple/beast/container/aged_unordered_map.h @@ -32,12 +32,18 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Hash = std::hash , - class KeyEqual = std::equal_to , - class Allocator = std::allocator > -> -using aged_unordered_map = detail::aged_unordered_container < - false, true, Key, T, Clock, Hash, KeyEqual, Allocator>; + class Hash = std::hash, + class KeyEqual = std::equal_to, + class Allocator = std::allocator>> +using aged_unordered_map = detail::aged_unordered_container< + false, + true, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>; } diff --git a/src/ripple/beast/container/aged_unordered_multimap.h b/src/ripple/beast/container/aged_unordered_multimap.h index ba708ae8a4..1298cd51d0 100644 --- a/src/ripple/beast/container/aged_unordered_multimap.h +++ b/src/ripple/beast/container/aged_unordered_multimap.h @@ -32,12 +32,18 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Hash = std::hash , - class KeyEqual = std::equal_to , - class Allocator = std::allocator > -> -using aged_unordered_multimap = detail::aged_unordered_container < - true, true, Key, T, Clock, Hash, KeyEqual, Allocator>; + class Hash = std::hash, + class KeyEqual = std::equal_to, + class Allocator = std::allocator>> +using aged_unordered_multimap = detail::aged_unordered_container< + true, + true, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>; } diff --git a/src/ripple/beast/container/aged_unordered_multiset.h b/src/ripple/beast/container/aged_unordered_multiset.h index c3806ef79f..5e9f682aa6 100644 --- a/src/ripple/beast/container/aged_unordered_multiset.h +++ b/src/ripple/beast/container/aged_unordered_multiset.h @@ -31,12 +31,18 @@ namespace beast { template < class Key, class Clock = std::chrono::steady_clock, - class Hash = std::hash , - class KeyEqual = std::equal_to , - class Allocator = std::allocator -> -using aged_unordered_multiset = detail::aged_unordered_container < - true, false, Key, void, Clock, Hash, KeyEqual, Allocator>; + class Hash = std::hash, + class KeyEqual = std::equal_to, + class Allocator = std::allocator> +using aged_unordered_multiset = detail::aged_unordered_container< + true, + false, + Key, + void, + Clock, + Hash, + KeyEqual, + Allocator>; } diff --git a/src/ripple/beast/container/aged_unordered_set.h b/src/ripple/beast/container/aged_unordered_set.h index 1ca61db327..adb411c344 100644 --- a/src/ripple/beast/container/aged_unordered_set.h +++ b/src/ripple/beast/container/aged_unordered_set.h @@ -31,12 +31,18 @@ namespace beast { template < class Key, class Clock = std::chrono::steady_clock, - class Hash = std::hash , - class KeyEqual = std::equal_to , - class Allocator = std::allocator -> -using aged_unordered_set = detail::aged_unordered_container < - false, false, Key, void, Clock, Hash, KeyEqual, Allocator>; + class Hash = std::hash, + class KeyEqual = std::equal_to, + class Allocator = std::allocator> +using aged_unordered_set = detail::aged_unordered_container< + false, + false, + Key, + void, + Clock, + Hash, + KeyEqual, + Allocator>; } diff --git a/src/ripple/beast/container/detail/aged_associative_container.h b/src/ripple/beast/container/detail/aged_associative_container.h index 42d04e1927..5ff7901552 100644 --- a/src/ripple/beast/container/detail/aged_associative_container.h +++ b/src/ripple/beast/container/detail/aged_associative_container.h @@ -32,27 +32,27 @@ struct aged_associative_container_extract_t explicit aged_associative_container_extract_t() = default; template - decltype (Value::first) const& - operator() (Value const& value) const + decltype(Value::first) const& + operator()(Value const& value) const { return value.first; } }; template <> -struct aged_associative_container_extract_t +struct aged_associative_container_extract_t { explicit aged_associative_container_extract_t() = default; template Value const& - operator() (Value const& value) const + operator()(Value const& value) const { return value; } }; -} -} +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/container/detail/aged_container_iterator.h b/src/ripple/beast/container/detail/aged_container_iterator.h index 86187bcd20..cd8677ce17 100644 --- a/src/ripple/beast/container/detail/aged_container_iterator.h +++ b/src/ripple/beast/container/detail/aged_container_iterator.h @@ -37,16 +37,14 @@ namespace detail { template < bool is_const, class Iterator, - class Base = - std::iterator < - typename std::iterator_traits ::iterator_category, - typename std::conditional ::type, - typename std::iterator_traits ::difference_type> -> -class aged_container_iterator - : public Base + class Base = std::iterator< + typename std::iterator_traits::iterator_category, + typename std::conditional< + is_const, + typename Iterator::value_type::stashed::value_type const, + typename Iterator::value_type::stashed::value_type>::type, + typename std::iterator_traits::difference_type>> +class aged_container_iterator : public Base { public: using time_point = typename Iterator::value_type::stashed::time_point; @@ -55,90 +53,109 @@ public: // Disable constructing a const_iterator from a non-const_iterator. // Converting between reverse and non-reverse iterators should be explicit. - template ::value == false>::type> - explicit aged_container_iterator (aged_container_iterator < - other_is_const, OtherIterator, OtherBase> const& other) - : m_iter (other.m_iter) + std::is_same::value == false>::type> + explicit aged_container_iterator( + aged_container_iterator const& + other) + : m_iter(other.m_iter) { } // Disable constructing a const_iterator from a non-const_iterator. - template ::type> - aged_container_iterator (aged_container_iterator < - other_is_const, Iterator, OtherBase> const& other) - : m_iter (other.m_iter) + aged_container_iterator( + aged_container_iterator const& + other) + : m_iter(other.m_iter) { } // Disable assigning a const_iterator to a non-const iterator template auto - operator= (aged_container_iterator < - other_is_const, OtherIterator, OtherBase> const& other) -> - typename std::enable_if < - other_is_const == false || is_const == true, - aged_container_iterator&>::type + operator=( + aged_container_iterator const& + other) -> + typename std::enable_if< + other_is_const == false || is_const == true, + aged_container_iterator&>::type { m_iter = other.m_iter; return *this; } template - bool operator== (aged_container_iterator < - other_is_const, OtherIterator, OtherBase> const& other) const + bool + operator==( + aged_container_iterator const& + other) const { return m_iter == other.m_iter; } template - bool operator!= (aged_container_iterator < - other_is_const, OtherIterator, OtherBase> const& other) const + bool + operator!=( + aged_container_iterator const& + other) const { return m_iter != other.m_iter; } - aged_container_iterator& operator++ () + aged_container_iterator& + operator++() { ++m_iter; return *this; } - aged_container_iterator operator++ (int) + aged_container_iterator + operator++(int) { - aged_container_iterator const prev (*this); + aged_container_iterator const prev(*this); ++m_iter; return prev; } - aged_container_iterator& operator-- () + aged_container_iterator& + operator--() { --m_iter; return *this; } - aged_container_iterator operator-- (int) + aged_container_iterator + operator--(int) { - aged_container_iterator const prev (*this); + aged_container_iterator const prev(*this); --m_iter; return prev; } - typename Base::reference operator* () const + typename Base::reference + operator*() const { return m_iter->value; } - typename Base::pointer operator-> () const + typename Base::pointer + operator->() const { return &m_iter->value; } - time_point const& when () const + time_point const& + when() const { return m_iter->when; } @@ -154,12 +171,12 @@ private: friend class aged_container_iterator; template - aged_container_iterator (OtherIterator const& iter) - : m_iter (iter) + aged_container_iterator(OtherIterator const& iter) : m_iter(iter) { } - Iterator const& iterator() const + Iterator const& + iterator() const { return m_iter; } @@ -167,8 +184,8 @@ private: Iterator m_iter; }; -} +} // namespace detail -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/detail/aged_ordered_container.h b/src/ripple/beast/container/detail/aged_ordered_container.h index 201c04b72d..a6cafa8973 100644 --- a/src/ripple/beast/container/detail/aged_ordered_container.h +++ b/src/ripple/beast/container/detail/aged_ordered_container.h @@ -20,11 +20,12 @@ #ifndef BEAST_CONTAINER_DETAIL_AGED_ORDERED_CONTAINER_H_INCLUDED #define BEAST_CONTAINER_DETAIL_AGED_ORDERED_CONTAINER_H_INCLUDED -#include -#include -#include -#include #include +#include +#include +#include +#include +#include // #include #include #include @@ -33,7 +34,6 @@ #include #include #include -#include // #include namespace beast { @@ -42,8 +42,7 @@ namespace detail { // Traits templates used to discern reverse_iterators, which are disallowed // for mutating operations. template -struct is_boost_reverse_iterator - : std::false_type +struct is_boost_reverse_iterator : std::false_type { explicit is_boost_reverse_iterator() = default; }; @@ -77,12 +76,9 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Compare = std::less , - class Allocator = std::allocator < - typename std::conditional , - Key>::type> -> + class Compare = std::less, + class Allocator = std::allocator< + typename std::conditional, Key>::type>> class aged_ordered_container { public: @@ -91,32 +87,29 @@ public: using duration = typename clock_type::duration; using key_type = Key; using mapped_type = T; - using value_type = typename std::conditional < - IsMap, std::pair , Key>::type; + using value_type = + typename std::conditional, Key>::type; using size_type = std::size_t; using difference_type = std::ptrdiff_t; // Introspection (for unit tests) using is_unordered = std::false_type; - using is_multi = std::integral_constant ; - using is_map = std::integral_constant ; + using is_multi = std::integral_constant; + using is_map = std::integral_constant; private: - static Key const& extract (value_type const& value) + static Key const& + extract(value_type const& value) { - return aged_associative_container_extract_t () (value); + return aged_associative_container_extract_t()(value); } // VFALCO TODO hoist to remove template argument dependencies struct element - : boost::intrusive::set_base_hook < - boost::intrusive::link_mode < - boost::intrusive::normal_link> - > - , boost::intrusive::list_base_hook < - boost::intrusive::link_mode < - boost::intrusive::normal_link> - > + : boost::intrusive::set_base_hook< + boost::intrusive::link_mode>, + boost::intrusive::list_base_hook< + boost::intrusive::link_mode> { // Stash types here so the iterator doesn't // need to see the container declaration. @@ -128,31 +121,22 @@ private: using time_point = typename aged_ordered_container::time_point; }; - element ( - time_point const& when_, - value_type const& value_) - : value (value_) - , when (when_) + element(time_point const& when_, value_type const& value_) + : value(value_), when(when_) { } - element ( - time_point const& when_, - value_type&& value_) - : value (std::move (value_)) - , when (when_) + element(time_point const& when_, value_type&& value_) + : value(std::move(value_)), when(when_) { } template < class... Args, - class = typename std::enable_if < - std::is_constructible ::value>::type - > - element (time_point const& when_, Args&&... args) - : value (std::forward (args)...) - , when (when_) + class = typename std::enable_if< + std::is_constructible::value>::type> + element(time_point const& when_, Args&&... args) + : value(std::forward(args)...), when(when_) { } @@ -162,9 +146,10 @@ private: // VFALCO TODO This should only be enabled for maps. class pair_value_compare - : public beast::detail::empty_base_optimization + : public beast::detail::empty_base_optimization #ifdef _LIBCPP_VERSION - , public std::binary_function + , + public std::binary_function #endif { public: @@ -174,25 +159,26 @@ private: using result_type = bool; #endif - bool operator() (value_type const& lhs, value_type const& rhs) const + bool + operator()(value_type const& lhs, value_type const& rhs) const { - return this->member() (lhs.first, rhs.first); + return this->member()(lhs.first, rhs.first); } - pair_value_compare () + pair_value_compare() { } - pair_value_compare (pair_value_compare const& other) - : beast::detail::empty_base_optimization (other) + pair_value_compare(pair_value_compare const& other) + : beast::detail::empty_base_optimization(other) { } private: friend aged_ordered_container; - pair_value_compare (Compare const& compare) - : beast::detail::empty_base_optimization (compare) + pair_value_compare(Compare const& compare) + : beast::detail::empty_base_optimization(compare) { } }; @@ -200,9 +186,10 @@ private: // Compares value_type against element, used in insert_check // VFALCO TODO hoist to remove template argument dependencies class KeyValueCompare - : public beast::detail::empty_base_optimization + : public beast::detail::empty_base_optimization #ifdef _LIBCPP_VERSION - , public std::binary_function + , + public std::binary_function #endif { public: @@ -212,10 +199,10 @@ private: using result_type = bool; #endif - KeyValueCompare () = default; + KeyValueCompare() = default; - KeyValueCompare (Compare const& compare) - : beast::detail::empty_base_optimization (compare) + KeyValueCompare(Compare const& compare) + : beast::detail::empty_base_optimization(compare) { } @@ -235,122 +222,119 @@ private: } #endif - bool operator() (Key const& k, element const& e) const + bool + operator()(Key const& k, element const& e) const { - return this->member() (k, extract (e.value)); + return this->member()(k, extract(e.value)); } - bool operator() (element const& e, Key const& k) const + bool + operator()(element const& e, Key const& k) const { - return this->member() (extract (e.value), k); + return this->member()(extract(e.value), k); } - bool operator() (element const& x, element const& y) const + bool + operator()(element const& x, element const& y) const { - return this->member() (extract (x.value), extract (y.value)); + return this->member()(extract(x.value), extract(y.value)); } - Compare& compare() + Compare& + compare() { - return beast::detail::empty_base_optimization ::member(); + return beast::detail::empty_base_optimization::member(); } - Compare const& compare() const + Compare const& + compare() const { - return beast::detail::empty_base_optimization ::member(); + return beast::detail::empty_base_optimization::member(); } }; - using list_type = typename boost::intrusive::make_list >::type; + using list_type = typename boost::intrusive:: + make_list>::type; - using cont_type = typename std::conditional < + using cont_type = typename std::conditional< IsMulti, - typename boost::intrusive::make_multiset , - boost::intrusive::compare - >::type, - typename boost::intrusive::make_set , - boost::intrusive::compare - >::type - >::type; + typename boost::intrusive::make_multiset< + element, + boost::intrusive::constant_time_size, + boost::intrusive::compare>::type, + typename boost::intrusive::make_set< + element, + boost::intrusive::constant_time_size, + boost::intrusive::compare>::type>::type; - using ElementAllocator = typename std::allocator_traits < - Allocator>::template rebind_alloc ; + using ElementAllocator = typename std::allocator_traits< + Allocator>::template rebind_alloc; - using ElementAllocatorTraits = std::allocator_traits ; + using ElementAllocatorTraits = std::allocator_traits; class config_t - : private KeyValueCompare - , public beast::detail::empty_base_optimization + : private KeyValueCompare, + public beast::detail::empty_base_optimization { public: - explicit config_t ( - clock_type& clock_) - : clock (clock_) + explicit config_t(clock_type& clock_) : clock(clock_) { } - config_t ( - clock_type& clock_, - Compare const& comp) - : KeyValueCompare (comp) - , clock (clock_) + config_t(clock_type& clock_, Compare const& comp) + : KeyValueCompare(comp), clock(clock_) { } - config_t ( - clock_type& clock_, - Allocator const& alloc_) - : beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + config_t(clock_type& clock_, Allocator const& alloc_) + : beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t ( + config_t( clock_type& clock_, Compare const& comp, Allocator const& alloc_) - : KeyValueCompare (comp) - , beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + : KeyValueCompare(comp) + , beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t (config_t const& other) - : KeyValueCompare (other.key_compare()) - , beast::detail::empty_base_optimization ( - ElementAllocatorTraits:: - select_on_container_copy_construction ( - other.alloc())) - , clock (other.clock) + config_t(config_t const& other) + : KeyValueCompare(other.key_compare()) + , beast::detail::empty_base_optimization( + ElementAllocatorTraits::select_on_container_copy_construction( + other.alloc())) + , clock(other.clock) { } - config_t (config_t const& other, Allocator const& alloc) - : KeyValueCompare (other.key_compare()) - , beast::detail::empty_base_optimization (alloc) - , clock (other.clock) + config_t(config_t const& other, Allocator const& alloc) + : KeyValueCompare(other.key_compare()) + , beast::detail::empty_base_optimization(alloc) + , clock(other.clock) { } - config_t (config_t&& other) - : KeyValueCompare (std::move (other.key_compare())) - , beast::detail::empty_base_optimization ( - std::move (other)) - , clock (other.clock) + config_t(config_t&& other) + : KeyValueCompare(std::move(other.key_compare())) + , beast::detail::empty_base_optimization( + std::move(other)) + , clock(other.clock) { } - config_t (config_t&& other, Allocator const& alloc) - : KeyValueCompare (std::move (other.key_compare())) - , beast::detail::empty_base_optimization (alloc) - , clock (other.clock) + config_t(config_t&& other, Allocator const& alloc) + : KeyValueCompare(std::move(other.key_compare())) + , beast::detail::empty_base_optimization(alloc) + , clock(other.clock) { } - config_t& operator= (config_t const& other) + config_t& + operator=(config_t const& other) { if (this != &other) { @@ -361,113 +345,122 @@ private: return *this; } - config_t& operator= (config_t&& other) + config_t& + operator=(config_t&& other) { - compare() = std::move (other.compare()); - alloc() = std::move (other.alloc()); + compare() = std::move(other.compare()); + alloc() = std::move(other.alloc()); clock = other.clock; return *this; } - Compare& compare () + Compare& + compare() { return KeyValueCompare::compare(); } - Compare const& compare () const + Compare const& + compare() const { return KeyValueCompare::compare(); } - KeyValueCompare& key_compare() + KeyValueCompare& + key_compare() { return *this; } - KeyValueCompare const& key_compare() const + KeyValueCompare const& + key_compare() const { return *this; } - ElementAllocator& alloc() + ElementAllocator& + alloc() { - return beast::detail::empty_base_optimization < + return beast::detail::empty_base_optimization< ElementAllocator>::member(); } - ElementAllocator const& alloc() const + ElementAllocator const& + alloc() const { - return beast::detail::empty_base_optimization < + return beast::detail::empty_base_optimization< ElementAllocator>::member(); } - std::reference_wrapper clock; + std::reference_wrapper clock; }; template - element* new_element (Args&&... args) + element* + new_element(Args&&... args) { struct Deleter { - std::reference_wrapper a_; - Deleter (ElementAllocator& a) - : a_(a) + std::reference_wrapper a_; + Deleter(ElementAllocator& a) : a_(a) { } void operator()(element* p) { - ElementAllocatorTraits::deallocate (a_.get(), p, 1); + ElementAllocatorTraits::deallocate(a_.get(), p, 1); } }; - std::unique_ptr p (ElementAllocatorTraits::allocate ( - m_config.alloc(), 1), Deleter(m_config.alloc())); - ElementAllocatorTraits::construct (m_config.alloc(), - p.get(), clock().now(), std::forward (args)...); + std::unique_ptr p( + ElementAllocatorTraits::allocate(m_config.alloc(), 1), + Deleter(m_config.alloc())); + ElementAllocatorTraits::construct( + m_config.alloc(), + p.get(), + clock().now(), + std::forward(args)...); return p.release(); } - void delete_element (element const* p) + void + delete_element(element const* p) { - ElementAllocatorTraits::destroy (m_config.alloc(), p); - ElementAllocatorTraits::deallocate ( + ElementAllocatorTraits::destroy(m_config.alloc(), p); + ElementAllocatorTraits::deallocate( m_config.alloc(), const_cast(p), 1); } - void unlink_and_delete_element (element const* p) + void + unlink_and_delete_element(element const* p) { - chronological.list.erase ( - chronological.list.iterator_to (*p)); - m_cont.erase (m_cont.iterator_to (*p)); - delete_element (p); + chronological.list.erase(chronological.list.iterator_to(*p)); + m_cont.erase(m_cont.iterator_to(*p)); + delete_element(p); } public: using key_compare = Compare; - using value_compare = typename std::conditional < - IsMap, - pair_value_compare, - Compare>::type; + using value_compare = + typename std::conditional::type; using allocator_type = Allocator; using reference = value_type&; using const_reference = value_type const&; - using pointer = typename std::allocator_traits < - Allocator>::pointer; - using const_pointer = typename std::allocator_traits < - Allocator>::const_pointer; + using pointer = typename std::allocator_traits::pointer; + using const_pointer = + typename std::allocator_traits::const_pointer; // A set iterator (IsMap==false) is always const // because the elements of a set are immutable. - using iterator = beast::detail::aged_container_iterator< - ! IsMap, typename cont_type::iterator>; - using const_iterator = beast::detail::aged_container_iterator< - true, typename cont_type::iterator>; - using reverse_iterator = beast::detail::aged_container_iterator< - ! IsMap, typename cont_type::reverse_iterator>; - using const_reverse_iterator = beast::detail::aged_container_iterator< - true, typename cont_type::reverse_iterator>; + using iterator = beast::detail:: + aged_container_iterator; + using const_iterator = beast::detail:: + aged_container_iterator; + using reverse_iterator = beast::detail:: + aged_container_iterator; + using const_reverse_iterator = beast::detail:: + aged_container_iterator; //-------------------------------------------------------------------------- // @@ -483,100 +476,117 @@ public: public: // A set iterator (IsMap==false) is always const // because the elements of a set are immutable. - using iterator = beast::detail::aged_container_iterator< - ! IsMap, typename list_type::iterator>; - using const_iterator = beast::detail::aged_container_iterator< - true, typename list_type::iterator>; + using iterator = beast::detail:: + aged_container_iterator; + using const_iterator = beast::detail:: + aged_container_iterator; using reverse_iterator = beast::detail::aged_container_iterator< - ! IsMap, typename list_type::reverse_iterator>; - using const_reverse_iterator = beast::detail::aged_container_iterator< - true, typename list_type::reverse_iterator>; + !IsMap, + typename list_type::reverse_iterator>; + using const_reverse_iterator = beast::detail:: + aged_container_iterator; - iterator begin () - { - return iterator (list.begin()); + iterator + begin() + { + return iterator(list.begin()); } - const_iterator begin () const + const_iterator + begin() const { - return const_iterator (list.begin ()); + return const_iterator(list.begin()); } - const_iterator cbegin() const + const_iterator + cbegin() const { - return const_iterator (list.begin ()); + return const_iterator(list.begin()); } - iterator end () + iterator + end() { - return iterator (list.end ()); + return iterator(list.end()); } - const_iterator end () const + const_iterator + end() const { - return const_iterator (list.end ()); + return const_iterator(list.end()); } - const_iterator cend () const + const_iterator + cend() const { - return const_iterator (list.end ()); + return const_iterator(list.end()); } - reverse_iterator rbegin () + reverse_iterator + rbegin() { - return reverse_iterator (list.rbegin()); + return reverse_iterator(list.rbegin()); } - const_reverse_iterator rbegin () const + const_reverse_iterator + rbegin() const { - return const_reverse_iterator (list.rbegin ()); + return const_reverse_iterator(list.rbegin()); } - const_reverse_iterator crbegin() const + const_reverse_iterator + crbegin() const { - return const_reverse_iterator (list.rbegin ()); + return const_reverse_iterator(list.rbegin()); } - reverse_iterator rend () + reverse_iterator + rend() { - return reverse_iterator (list.rend ()); + return reverse_iterator(list.rend()); } - const_reverse_iterator rend () const + const_reverse_iterator + rend() const { - return const_reverse_iterator (list.rend ()); + return const_reverse_iterator(list.rend()); } - const_reverse_iterator crend () const + const_reverse_iterator + crend() const { - return const_reverse_iterator (list.rend ()); + return const_reverse_iterator(list.rend()); } - iterator iterator_to (value_type& value) + iterator + iterator_to(value_type& value) { - static_assert (std::is_standard_layout ::value, + static_assert( + std::is_standard_layout::value, "must be standard layout"); - return list.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + return list.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } - const_iterator iterator_to (value_type const& value) const + const_iterator + iterator_to(value_type const& value) const { - static_assert (std::is_standard_layout ::value, + static_assert( + std::is_standard_layout::value, "must be standard layout"); - return list.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + return list.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } private: - chronological_t () + chronological_t() { } - chronological_t (chronological_t const&) = delete; - chronological_t (chronological_t&&) = delete; + chronological_t(chronological_t const&) = delete; + chronological_t(chronological_t&&) = delete; friend class aged_ordered_container; list_type mutable list; @@ -590,64 +600,84 @@ public: aged_ordered_container() = delete; - explicit aged_ordered_container (clock_type& clock); + explicit aged_ordered_container(clock_type& clock); - aged_ordered_container (clock_type& clock, - Compare const& comp); + aged_ordered_container(clock_type& clock, Compare const& comp); - aged_ordered_container (clock_type& clock, + aged_ordered_container(clock_type& clock, Allocator const& alloc); + + aged_ordered_container( + clock_type& clock, + Compare const& comp, Allocator const& alloc); - aged_ordered_container (clock_type& clock, - Compare const& comp, Allocator const& alloc); + template + aged_ordered_container(InputIt first, InputIt last, clock_type& clock); template - aged_ordered_container (InputIt first, InputIt last, clock_type& clock); - - template - aged_ordered_container (InputIt first, InputIt last, clock_type& clock, + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, Compare const& comp); template - aged_ordered_container (InputIt first, InputIt last, clock_type& clock, + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, Allocator const& alloc); template - aged_ordered_container (InputIt first, InputIt last, clock_type& clock, - Compare const& comp, Allocator const& alloc); - - aged_ordered_container (aged_ordered_container const& other); - - aged_ordered_container (aged_ordered_container const& other, + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Compare const& comp, Allocator const& alloc); - aged_ordered_container (aged_ordered_container&& other); + aged_ordered_container(aged_ordered_container const& other); - aged_ordered_container (aged_ordered_container&& other, + aged_ordered_container( + aged_ordered_container const& other, Allocator const& alloc); - aged_ordered_container (std::initializer_list init, + aged_ordered_container(aged_ordered_container&& other); + + aged_ordered_container( + aged_ordered_container&& other, + Allocator const& alloc); + + aged_ordered_container( + std::initializer_list init, clock_type& clock); - aged_ordered_container (std::initializer_list init, - clock_type& clock, Compare const& comp); + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Compare const& comp); - aged_ordered_container (std::initializer_list init, - clock_type& clock, Allocator const& alloc); + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Allocator const& alloc); - aged_ordered_container (std::initializer_list init, - clock_type& clock, Compare const& comp, Allocator const& alloc); + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Compare const& comp, + Allocator const& alloc); ~aged_ordered_container(); aged_ordered_container& - operator= (aged_ordered_container const& other); + operator=(aged_ordered_container const& other); aged_ordered_container& - operator= (aged_ordered_container&& other); + operator=(aged_ordered_container&& other); aged_ordered_container& - operator= (std::initializer_list init); + operator=(std::initializer_list init); allocator_type get_allocator() const @@ -677,31 +707,31 @@ public: class K, bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - at (K const& k); + class = typename std::enable_if::type> + typename std::conditional::type& + at(K const& k); template < class K, bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type const& - at (K const& k) const; + class = typename std::enable_if::type> + typename std::conditional::type const& + at(K const& k) const; template < bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - operator[] (Key const& key); + class = typename std::enable_if::type> + typename std::conditional::type& + operator[](Key const& key); template < bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - operator[] (Key&& key); + class = typename std::enable_if::type> + typename std::conditional::type& + operator[](Key&& key); //-------------------------------------------------------------------------- // @@ -710,95 +740,95 @@ public: //-------------------------------------------------------------------------- iterator - begin () + begin() { - return iterator (m_cont.begin()); + return iterator(m_cont.begin()); } const_iterator - begin () const + begin() const { - return const_iterator (m_cont.begin ()); + return const_iterator(m_cont.begin()); } const_iterator cbegin() const { - return const_iterator (m_cont.begin ()); + return const_iterator(m_cont.begin()); } iterator - end () + end() { - return iterator (m_cont.end ()); + return iterator(m_cont.end()); } const_iterator - end () const + end() const { - return const_iterator (m_cont.end ()); + return const_iterator(m_cont.end()); } const_iterator - cend () const + cend() const { - return const_iterator (m_cont.end ()); + return const_iterator(m_cont.end()); } reverse_iterator - rbegin () + rbegin() { - return reverse_iterator (m_cont.rbegin()); + return reverse_iterator(m_cont.rbegin()); } const_reverse_iterator - rbegin () const + rbegin() const { - return const_reverse_iterator (m_cont.rbegin ()); + return const_reverse_iterator(m_cont.rbegin()); } const_reverse_iterator crbegin() const { - return const_reverse_iterator (m_cont.rbegin ()); + return const_reverse_iterator(m_cont.rbegin()); } reverse_iterator - rend () + rend() { - return reverse_iterator (m_cont.rend ()); + return reverse_iterator(m_cont.rend()); } const_reverse_iterator - rend () const + rend() const { - return const_reverse_iterator (m_cont.rend ()); + return const_reverse_iterator(m_cont.rend()); } const_reverse_iterator - crend () const + crend() const { - return const_reverse_iterator (m_cont.rend ()); + return const_reverse_iterator(m_cont.rend()); } iterator - iterator_to (value_type& value) + iterator_to(value_type& value) { - static_assert (std::is_standard_layout ::value, - "must be standard layout"); - return m_cont.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + static_assert( + std::is_standard_layout::value, "must be standard layout"); + return m_cont.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } const_iterator - iterator_to (value_type const& value) const + iterator_to(value_type const& value) const { - static_assert (std::is_standard_layout ::value, - "must be standard layout"); - return m_cont.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + static_assert( + std::is_standard_layout::value, "must be standard layout"); + return m_cont.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } //-------------------------------------------------------------------------- @@ -837,183 +867,169 @@ public: // map, set template auto - insert (value_type const& value) -> - typename std::enable_if >::type; + insert(value_type const& value) -> + typename std::enable_if>::type; // multimap, multiset template auto - insert (value_type const& value) -> - typename std::enable_if ::type; + insert(value_type const& value) -> + typename std::enable_if::type; // set template auto - insert (value_type&& value) -> - typename std::enable_if >::type; + insert(value_type&& value) -> typename std:: + enable_if>::type; // multiset template auto - insert (value_type&& value) -> - typename std::enable_if ::type; + insert(value_type&& value) -> + typename std::enable_if::type; //--- // map, set template auto - insert (const_iterator hint, value_type const& value) -> - typename std::enable_if ::type; + insert(const_iterator hint, value_type const& value) -> + typename std::enable_if::type; // multimap, multiset template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type const& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type const& value) { // VFALCO TODO Figure out how to utilize 'hint' - return insert (value); + return insert(value); } // map, set template auto - insert (const_iterator hint, value_type&& value) -> - typename std::enable_if ::type; + insert(const_iterator hint, value_type&& value) -> + typename std::enable_if::type; // multimap, multiset template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type&& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type&& value) { // VFALCO TODO Figure out how to utilize 'hint' - return insert (std::move (value)); + return insert(std::move(value)); } // map, multimap - template < - class P, - bool maybe_map = IsMap - > - typename std::enable_if ::value, - typename std::conditional - >::type - >::type - insert (P&& value) + template + typename std::enable_if< + maybe_map && std::is_constructible::value, + typename std:: + conditional>::type>:: + type + insert(P&& value) { - return emplace (std::forward

(value)); + return emplace(std::forward

(value)); } // map, multimap - template < - class P, - bool maybe_map = IsMap - > - typename std::enable_if ::value, - typename std::conditional - >::type - >::type - insert (const_iterator hint, P&& value) + template + typename std::enable_if< + maybe_map && std::is_constructible::value, + typename std:: + conditional>::type>:: + type + insert(const_iterator hint, P&& value) { - return emplace_hint (hint, std::forward

(value)); + return emplace_hint(hint, std::forward

(value)); } template void - insert (InputIt first, InputIt last) + insert(InputIt first, InputIt last) { for (; first != last; ++first) - insert (cend(), *first); + insert(cend(), *first); } void - insert (std::initializer_list init) + insert(std::initializer_list init) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } // map, set template auto - emplace (Args&&... args) -> - typename std::enable_if >::type; + emplace(Args&&... args) -> + typename std::enable_if>::type; // multiset, multimap template auto - emplace (Args&&... args) -> - typename std::enable_if ::type; + emplace(Args&&... args) -> + typename std::enable_if::type; // map, set template auto - emplace_hint (const_iterator hint, Args&&... args) -> - typename std::enable_if >::type; + emplace_hint(const_iterator hint, Args&&... args) -> + typename std::enable_if>::type; // multiset, multimap template - typename std::enable_if ::type - emplace_hint (const_iterator /*hint*/, Args&&... args) + typename std::enable_if::type + emplace_hint(const_iterator /*hint*/, Args&&... args) { // VFALCO TODO Figure out how to utilize 'hint' - return emplace ( - std::forward (args)...); + return emplace(std::forward(args)...); } // enable_if prevents erase (reverse_iterator pos) from compiling - template ::value>> - beast::detail::aged_container_iterator - erase (beast::detail::aged_container_iterator pos); + template < + bool is_const, + class Iterator, + class Base, + class = std::enable_if_t::value>> + beast::detail::aged_container_iterator + erase(beast::detail::aged_container_iterator pos); // enable_if prevents erase (reverse_iterator first, reverse_iterator last) // from compiling - template ::value>> - beast::detail::aged_container_iterator - erase (beast::detail::aged_container_iterator first, - beast::detail::aged_container_iterator last); + beast::detail::aged_container_iterator + erase( + beast::detail::aged_container_iterator first, + beast::detail::aged_container_iterator last); template auto - erase (K const& k) -> - size_type; + erase(K const& k) -> size_type; void - swap (aged_ordered_container& other) noexcept; + swap(aged_ordered_container& other) noexcept; //-------------------------------------------------------------------------- // enable_if prevents touch (reverse_iterator pos) from compiling - template ::value>> void - touch (beast::detail::aged_container_iterator pos) + touch(beast::detail::aged_container_iterator pos) { - touch (pos, clock().now()); + touch(pos, clock().now()); } template size_type - touch (K const& k); + touch(K const& k); //-------------------------------------------------------------------------- // @@ -1024,86 +1040,81 @@ public: // VFALCO TODO Respect is_transparent (c++14) template size_type - count (K const& k) const + count(K const& k) const { - return m_cont.count (k, - std::cref (m_config.key_compare())); + return m_cont.count(k, std::cref(m_config.key_compare())); } // VFALCO TODO Respect is_transparent (c++14) template iterator - find (K const& k) + find(K const& k) { - return iterator (m_cont.find (k, - std::cref (m_config.key_compare()))); + return iterator(m_cont.find(k, std::cref(m_config.key_compare()))); } // VFALCO TODO Respect is_transparent (c++14) template const_iterator - find (K const& k) const + find(K const& k) const { - return const_iterator (m_cont.find (k, - std::cref (m_config.key_compare()))); + return const_iterator( + m_cont.find(k, std::cref(m_config.key_compare()))); } // VFALCO TODO Respect is_transparent (c++14) template - std::pair - equal_range (K const& k) + std::pair + equal_range(K const& k) { - auto const r (m_cont.equal_range (k, - std::cref (m_config.key_compare()))); - return std::make_pair (iterator (r.first), - iterator (r.second)); + auto const r(m_cont.equal_range(k, std::cref(m_config.key_compare()))); + return std::make_pair(iterator(r.first), iterator(r.second)); } // VFALCO TODO Respect is_transparent (c++14) template - std::pair - equal_range (K const& k) const + std::pair + equal_range(K const& k) const { - auto const r (m_cont.equal_range (k, - std::cref (m_config.key_compare()))); - return std::make_pair (const_iterator (r.first), - const_iterator (r.second)); + auto const r(m_cont.equal_range(k, std::cref(m_config.key_compare()))); + return std::make_pair( + const_iterator(r.first), const_iterator(r.second)); } // VFALCO TODO Respect is_transparent (c++14) template iterator - lower_bound (K const& k) + lower_bound(K const& k) { - return iterator (m_cont.lower_bound (k, - std::cref (m_config.key_compare()))); + return iterator( + m_cont.lower_bound(k, std::cref(m_config.key_compare()))); } // VFALCO TODO Respect is_transparent (c++14) template const_iterator - lower_bound (K const& k) const + lower_bound(K const& k) const { - return const_iterator (m_cont.lower_bound (k, - std::cref (m_config.key_compare()))); + return const_iterator( + m_cont.lower_bound(k, std::cref(m_config.key_compare()))); } // VFALCO TODO Respect is_transparent (c++14) template iterator - upper_bound (K const& k) + upper_bound(K const& k) { - return iterator (m_cont.upper_bound (k, - std::cref (m_config.key_compare()))); + return iterator( + m_cont.upper_bound(k, std::cref(m_config.key_compare()))); } // VFALCO TODO Respect is_transparent (c++14) template const_iterator - upper_bound (K const& k) const + upper_bound(K const& k) const { - return const_iterator (m_cont.upper_bound (k, - std::cref (m_config.key_compare()))); + return const_iterator( + m_cont.upper_bound(k, std::cref(m_config.key_compare()))); } //-------------------------------------------------------------------------- @@ -1122,7 +1133,7 @@ public: value_compare value_comp() const { - return value_compare (m_config.compare()); + return value_compare(m_config.compare()); } //-------------------------------------------------------------------------- @@ -1140,28 +1151,34 @@ public: bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator== ( - aged_ordered_container const& other) const; + operator==(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const; template < bool OtherIsMulti, bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator!= ( - aged_ordered_container const& other) const + operator!=(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { - return ! (this->operator== (other)); + return !(this->operator==(other)); } template < @@ -1169,16 +1186,19 @@ public: bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator< ( - aged_ordered_container const& other) const + operator<(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { - value_compare const comp (value_comp ()); - return std::lexicographical_compare ( + value_compare const comp(value_comp()); + return std::lexicographical_compare( cbegin(), cend(), other.cbegin(), other.cend(), comp); } @@ -1187,15 +1207,18 @@ public: bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator<= ( - aged_ordered_container const& other) const + operator<=(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { - return ! (other < *this); + return !(other < *this); } template < @@ -1203,13 +1226,16 @@ public: bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator> ( - aged_ordered_container const& other) const + operator>(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { return other < *this; } @@ -1219,35 +1245,43 @@ public: bool OtherIsMap, class OtherT, class OtherDuration, - class OtherAllocator - > + class OtherAllocator> bool - operator>= ( - aged_ordered_container const& other) const + operator>=(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { - return ! (*this < other); + return !(*this < other); } private: // enable_if prevents erase (reverse_iterator pos, now) from compiling - template ::value>> void - touch (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos, - typename clock_type::time_point const& now); + touch( + beast::detail::aged_container_iterator pos, + typename clock_type::time_point const& now); - template ::propagate_on_container_swap::value> - typename std::enable_if ::type - swap_data (aged_ordered_container& other) noexcept; + template < + bool maybe_propagate = std::allocator_traits< + Allocator>::propagate_on_container_swap::value> + typename std::enable_if::type + swap_data(aged_ordered_container& other) noexcept; - template ::propagate_on_container_swap::value> - typename std::enable_if ::type - swap_data (aged_ordered_container& other) noexcept; + template < + bool maybe_propagate = std::allocator_traits< + Allocator>::propagate_on_container_swap::value> + typename std::enable_if::type + swap_data(aged_ordered_container& other) noexcept; private: config_t m_config; @@ -1256,307 +1290,444 @@ private: //------------------------------------------------------------------------------ -template -aged_ordered_container :: -aged_ordered_container ( - clock_type& clock) - : m_config (clock) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container(clock_type& clock) + : m_config(clock) { } -template -aged_ordered_container :: -aged_ordered_container ( - clock_type& clock, - Compare const& comp) - : m_config (clock, comp) - , m_cont (comp) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container(clock_type& clock, Compare const& comp) + : m_config(clock, comp), m_cont(comp) { } -template -aged_ordered_container :: -aged_ordered_container ( - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container(clock_type& clock, Allocator const& alloc) + : m_config(clock, alloc) { } -template -aged_ordered_container :: -aged_ordered_container ( - clock_type& clock, - Compare const& comp, - Allocator const& alloc) - : m_config (clock, comp, alloc) - , m_cont (comp) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + clock_type& clock, + Compare const& comp, + Allocator const& alloc) + : m_config(clock, comp, alloc), m_cont(comp) { } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -aged_ordered_container :: -aged_ordered_container (InputIt first, InputIt last, - clock_type& clock) - : m_config (clock) +aged_ordered_container:: + aged_ordered_container(InputIt first, InputIt last, clock_type& clock) + : m_config(clock) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -aged_ordered_container :: -aged_ordered_container (InputIt first, InputIt last, - clock_type& clock, - Compare const& comp) - : m_config (clock, comp) - , m_cont (comp) +aged_ordered_container:: + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Compare const& comp) + : m_config(clock, comp), m_cont(comp) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -aged_ordered_container :: -aged_ordered_container (InputIt first, InputIt last, - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) +aged_ordered_container:: + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Allocator const& alloc) + : m_config(clock, alloc) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -aged_ordered_container :: -aged_ordered_container (InputIt first, InputIt last, - clock_type& clock, - Compare const& comp, - Allocator const& alloc) - : m_config (clock, comp, alloc) - , m_cont (comp) +aged_ordered_container:: + aged_ordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Compare const& comp, + Allocator const& alloc) + : m_config(clock, comp, alloc), m_cont(comp) { - insert (first, last); + insert(first, last); } -template -aged_ordered_container :: -aged_ordered_container (aged_ordered_container const& other) - : m_config (other.m_config) - , m_cont (other.m_cont.comp()) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container(aged_ordered_container const& other) + : m_config(other.m_config), m_cont(other.m_cont.comp()) { - insert (other.cbegin(), other.cend()); + insert(other.cbegin(), other.cend()); } -template -aged_ordered_container :: -aged_ordered_container (aged_ordered_container const& other, - Allocator const& alloc) - : m_config (other.m_config, alloc) - , m_cont (other.m_cont.comp()) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + aged_ordered_container const& other, + Allocator const& alloc) + : m_config(other.m_config, alloc), m_cont(other.m_cont.comp()) { - insert (other.cbegin(), other.cend()); + insert(other.cbegin(), other.cend()); } -template -aged_ordered_container :: -aged_ordered_container (aged_ordered_container&& other) - : m_config (std::move (other.m_config)) - , m_cont (std::move (other.m_cont)) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container(aged_ordered_container&& other) + : m_config(std::move(other.m_config)), m_cont(std::move(other.m_cont)) { - chronological.list = std::move (other.chronological.list); + chronological.list = std::move(other.chronological.list); } -template -aged_ordered_container :: -aged_ordered_container (aged_ordered_container&& other, - Allocator const& alloc) - : m_config (std::move (other.m_config), alloc) - , m_cont (std::move(other.m_cont.comp())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + aged_ordered_container&& other, + Allocator const& alloc) + : m_config(std::move(other.m_config), alloc) + , m_cont(std::move(other.m_cont.comp())) { - insert (other.cbegin(), other.cend()); - other.clear (); + insert(other.cbegin(), other.cend()); + other.clear(); } -template -aged_ordered_container :: -aged_ordered_container (std::initializer_list init, - clock_type& clock) - : m_config (clock) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + std::initializer_list init, + clock_type& clock) + : m_config(clock) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_ordered_container :: -aged_ordered_container (std::initializer_list init, - clock_type& clock, - Compare const& comp) - : m_config (clock, comp) - , m_cont (comp) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Compare const& comp) + : m_config(clock, comp), m_cont(comp) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_ordered_container :: -aged_ordered_container (std::initializer_list init, - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Allocator const& alloc) + : m_config(clock, alloc) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_ordered_container :: -aged_ordered_container (std::initializer_list init, - clock_type& clock, - Compare const& comp, - Allocator const& alloc) - : m_config (clock, comp, alloc) - , m_cont (comp) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + aged_ordered_container( + std::initializer_list init, + clock_type& clock, + Compare const& comp, + Allocator const& alloc) + : m_config(clock, comp, alloc), m_cont(comp) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_ordered_container :: -~aged_ordered_container() +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +aged_ordered_container:: + ~aged_ordered_container() { clear(); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> auto -aged_ordered_container :: -operator= (aged_ordered_container const& other) -> - aged_ordered_container& +aged_ordered_container:: +operator=(aged_ordered_container const& other) -> aged_ordered_container& { if (this != &other) { clear(); this->m_config = other.m_config; - insert (other.begin(), other.end()); + insert(other.begin(), other.end()); } return *this; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> auto -aged_ordered_container :: -operator= (aged_ordered_container&& other) -> - aged_ordered_container& +aged_ordered_container:: +operator=(aged_ordered_container&& other) -> aged_ordered_container& { clear(); - this->m_config = std::move (other.m_config); - insert (other.begin(), other.end()); + this->m_config = std::move(other.m_config); + insert(other.begin(), other.end()); other.clear(); return *this; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> auto -aged_ordered_container :: -operator= (std::initializer_list init) -> - aged_ordered_container& +aged_ordered_container:: +operator=(std::initializer_list init) -> aged_ordered_container& { - clear (); - insert (init); + clear(); + insert(init); return *this; } //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::conditional ::type& -aged_ordered_container :: -at (K const& k) +typename std::conditional::type& +aged_ordered_container::at( + K const& k) { - auto const iter (m_cont.find (k, - std::cref (m_config.key_compare()))); + auto const iter(m_cont.find(k, std::cref(m_config.key_compare()))); if (iter == m_cont.end()) - throw std::out_of_range ("key not found"); + throw std::out_of_range("key not found"); return iter->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::conditional ::type const& -aged_ordered_container :: -at (K const& k) const +typename std::conditional::type const& +aged_ordered_container::at( + K const& k) const { - auto const iter (m_cont.find (k, - std::cref (m_config.key_compare()))); + auto const iter(m_cont.find(k, std::cref(m_config.key_compare()))); if (iter == m_cont.end()) - throw std::out_of_range ("key not found"); + throw std::out_of_range("key not found"); return iter->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::conditional ::type& -aged_ordered_container :: -operator[] (Key const& key) +typename std::conditional::type& +aged_ordered_container:: +operator[](Key const& key) { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (key, - std::cref (m_config.key_compare()), d)); + auto const result( + m_cont.insert_check(key, std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element ( - std::piecewise_construct, std::forward_as_tuple (key), - std::forward_as_tuple ())); - m_cont.insert_commit (*p, d); - chronological.list.push_back (*p); + element* const p(new_element( + std::piecewise_construct, + std::forward_as_tuple(key), + std::forward_as_tuple())); + m_cont.insert_commit(*p, d); + chronological.list.push_back(*p); return p->value.second; } return result.first->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::conditional ::type& -aged_ordered_container :: -operator[] (Key&& key) +typename std::conditional::type& +aged_ordered_container:: +operator[](Key&& key) { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (key, - std::cref (m_config.key_compare()), d)); + auto const result( + m_cont.insert_check(key, std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element ( + element* const p(new_element( std::piecewise_construct, - std::forward_as_tuple (std::move (key)), - std::forward_as_tuple ())); - m_cont.insert_commit (*p, d); - chronological.list.push_back (*p); + std::forward_as_tuple(std::move(key)), + std::forward_as_tuple())); + m_cont.insert_commit(*p, d); + chronological.list.push_back(*p); return p->value.second; } return result.first->value.second; @@ -1564,268 +1735,335 @@ operator[] (Key&& key) //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> void -aged_ordered_container :: -clear() +aged_ordered_container:: + clear() { - for (auto iter (chronological.list.begin()); - iter != chronological.list.end();) - delete_element (&*iter++); + for (auto iter(chronological.list.begin()); + iter != chronological.list.end();) + delete_element(&*iter++); chronological.list.clear(); m_cont.clear(); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (value_type const& value) -> - typename std::enable_if >::type +aged_ordered_container:: + insert(value_type const& value) -> + typename std::enable_if>::type { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (value), - std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + extract(value), std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element (value)); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + element* const p(new_element(value)); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - return std::make_pair (iterator (result.first), false); + return std::make_pair(iterator(result.first), false); } // multimap, multiset -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (value_type const& value) -> - typename std::enable_if ::type +aged_ordered_container:: + insert(value_type const& value) -> + typename std::enable_if::type { - auto const before (m_cont.upper_bound ( - extract (value), std::cref (m_config.key_compare()))); - element* const p (new_element (value)); - chronological.list.push_back (*p); - auto const iter (m_cont.insert_before (before, *p)); - return iterator (iter); + auto const before( + m_cont.upper_bound(extract(value), std::cref(m_config.key_compare()))); + element* const p(new_element(value)); + chronological.list.push_back(*p); + auto const iter(m_cont.insert_before(before, *p)); + return iterator(iter); } // set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (value_type&& value) -> - typename std::enable_if >::type +aged_ordered_container:: + insert(value_type&& value) -> typename std:: + enable_if>::type { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (value), - std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + extract(value), std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element (std::move (value))); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + element* const p(new_element(std::move(value))); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - return std::make_pair (iterator (result.first), false); + return std::make_pair(iterator(result.first), false); } // multiset -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (value_type&& value) -> - typename std::enable_if ::type +aged_ordered_container:: + insert(value_type&& value) -> + typename std::enable_if::type { - auto const before (m_cont.upper_bound ( - extract (value), std::cref (m_config.key_compare()))); - element* const p (new_element (std::move (value))); - chronological.list.push_back (*p); - auto const iter (m_cont.insert_before (before, *p)); - return iterator (iter); + auto const before( + m_cont.upper_bound(extract(value), std::cref(m_config.key_compare()))); + element* const p(new_element(std::move(value))); + chronological.list.push_back(*p); + auto const iter(m_cont.insert_before(before, *p)); + return iterator(iter); } //--- // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (const_iterator hint, value_type const& value) -> - typename std::enable_if ::type +aged_ordered_container:: + insert(const_iterator hint, value_type const& value) -> + typename std::enable_if::type { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (hint.iterator(), - extract (value), std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + hint.iterator(), extract(value), std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element (value)); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return iterator (iter); + element* const p(new_element(value)); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return iterator(iter); } - return iterator (result.first); + return iterator(result.first); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -insert (const_iterator hint, value_type&& value) -> - typename std::enable_if ::type +aged_ordered_container:: + insert(const_iterator hint, value_type&& value) -> + typename std::enable_if::type { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (hint.iterator(), - extract (value), std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + hint.iterator(), extract(value), std::cref(m_config.key_compare()), d)); if (result.second) { - element* const p (new_element (std::move (value))); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return iterator (iter); + element* const p(new_element(std::move(value))); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return iterator(iter); } - return iterator (result.first); + return iterator(result.first); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -emplace (Args&&... args) -> - typename std::enable_if >::type +aged_ordered_container:: + emplace(Args&&... args) -> + typename std::enable_if>::type { // VFALCO NOTE Its unfortunate that we need to // construct element here - element* const p (new_element ( - std::forward (args)...)); + element* const p(new_element(std::forward(args)...)); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (p->value), - std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + extract(p->value), std::cref(m_config.key_compare()), d)); if (result.second) { - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - delete_element (p); - return std::make_pair (iterator (result.first), false); + delete_element(p); + return std::make_pair(iterator(result.first), false); } // multiset, multimap -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -emplace (Args&&... args) -> - typename std::enable_if ::type +aged_ordered_container:: + emplace(Args&&... args) -> + typename std::enable_if::type { - element* const p (new_element ( - std::forward (args)...)); - auto const before (m_cont.upper_bound (extract (p->value), - std::cref (m_config.key_compare()))); - chronological.list.push_back (*p); - auto const iter (m_cont.insert_before (before, *p)); - return iterator (iter); + element* const p(new_element(std::forward(args)...)); + auto const before(m_cont.upper_bound( + extract(p->value), std::cref(m_config.key_compare()))); + chronological.list.push_back(*p); + auto const iter(m_cont.insert_before(before, *p)); + return iterator(iter); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -emplace_hint (const_iterator hint, Args&&... args) -> - typename std::enable_if >::type +aged_ordered_container:: + emplace_hint(const_iterator hint, Args&&... args) -> + typename std::enable_if>::type { // VFALCO NOTE Its unfortunate that we need to // construct element here - element* const p (new_element ( - std::forward (args)...)); + element* const p(new_element(std::forward(args)...)); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (hint.iterator(), - extract (p->value), std::cref (m_config.key_compare()), d)); + auto const result(m_cont.insert_check( + hint.iterator(), + extract(p->value), + std::cref(m_config.key_compare()), + d)); if (result.second) { - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - delete_element (p); - return std::make_pair (iterator (result.first), false); + delete_element(p); + return std::make_pair(iterator(result.first), false); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -beast::detail::aged_container_iterator -aged_ordered_container :: -erase (beast::detail::aged_container_iterator pos) +beast::detail::aged_container_iterator +aged_ordered_container:: + erase(beast::detail::aged_container_iterator pos) { unlink_and_delete_element(&*((pos++).iterator())); - return beast::detail::aged_container_iterator < - false, Iterator, Base> (pos.iterator()); + return beast::detail::aged_container_iterator( + pos.iterator()); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -beast::detail::aged_container_iterator -aged_ordered_container :: -erase (beast::detail::aged_container_iterator first, - beast::detail::aged_container_iterator last) +beast::detail::aged_container_iterator +aged_ordered_container:: + erase( + beast::detail::aged_container_iterator first, + beast::detail::aged_container_iterator last) { for (; first != last;) unlink_and_delete_element(&*((first++).iterator())); - return beast::detail::aged_container_iterator < - false, Iterator, Base> (first.iterator()); + return beast::detail::aged_container_iterator( + first.iterator()); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -erase (K const& k) -> - size_type +aged_ordered_container:: + erase(K const& k) -> size_type { - auto iter (m_cont.find (k, - std::cref (m_config.key_compare()))); + auto iter(m_cont.find(k, std::cref(m_config.key_compare()))); if (iter == m_cont.end()) return 0; - size_type n (0); + size_type n(0); for (;;) { - auto p (&*iter++); - bool const done ( - m_config (*p, extract (iter->value))); - unlink_and_delete_element (p); + auto p(&*iter++); + bool const done(m_config(*p, extract(iter->value))); + unlink_and_delete_element(p); ++n; if (done) break; @@ -1833,33 +2071,44 @@ erase (K const& k) -> return n; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> void -aged_ordered_container :: -swap (aged_ordered_container& other) noexcept +aged_ordered_container::swap( + aged_ordered_container& other) noexcept { - swap_data (other); - std::swap (chronological, other.chronological); - std::swap (m_cont, other.m_cont); + swap_data(other); + std::swap(chronological, other.chronological); + std::swap(m_cont, other.m_cont); } //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template auto -aged_ordered_container :: -touch (K const& k) -> - size_type +aged_ordered_container:: + touch(K const& k) -> size_type { - auto const now (clock().now()); - size_type n (0); - auto const range (equal_range (k)); + auto const now(clock().now()); + size_type n(0); + auto const range(equal_range(k)); for (auto iter : range) { - touch (iter, now); + touch(iter, now); ++n; } return n; @@ -1867,117 +2116,201 @@ touch (K const& k) -> //------------------------------------------------------------------------------ -template -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +template < + bool OtherIsMulti, + bool OtherIsMap, + class OtherT, + class OtherDuration, + class OtherAllocator> bool -aged_ordered_container :: -operator== ( - aged_ordered_container const& other) const +aged_ordered_container:: +operator==(aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator> const& other) const { - using Other = aged_ordered_container ; + using Other = aged_ordered_container< + OtherIsMulti, + OtherIsMap, + Key, + OtherT, + OtherDuration, + Compare, + OtherAllocator>; if (size() != other.size()) return false; - std::equal_to eq; - return std::equal (cbegin(), cend(), other.cbegin(), other.cend(), - [&eq, &other](value_type const& lhs, - typename Other::value_type const& rhs) - { - return eq (extract (lhs), other.extract (rhs)); + std::equal_to eq; + return std::equal( + cbegin(), + cend(), + other.cbegin(), + other.cend(), + [&eq, &other]( + value_type const& lhs, typename Other::value_type const& rhs) { + return eq(extract(lhs), other.extract(rhs)); }); } //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template void -aged_ordered_container :: -touch (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos, +aged_ordered_container:: + touch( + beast::detail::aged_container_iterator pos, typename clock_type::time_point const& now) { - auto& e (*pos.iterator()); + auto& e(*pos.iterator()); e.when = now; - chronological.list.erase (chronological.list.iterator_to (e)); - chronological.list.push_back (e); + chronological.list.erase(chronological.list.iterator_to(e)); + chronological.list.push_back(e); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::enable_if ::type -aged_ordered_container :: -swap_data (aged_ordered_container& other) noexcept +typename std::enable_if::type +aged_ordered_container:: + swap_data(aged_ordered_container& other) noexcept { - std::swap (m_config.key_compare(), other.m_config.key_compare()); - std::swap (m_config.alloc(), other.m_config.alloc()); - std::swap (m_config.clock, other.m_config.clock); + std::swap(m_config.key_compare(), other.m_config.key_compare()); + std::swap(m_config.alloc(), other.m_config.alloc()); + std::swap(m_config.clock, other.m_config.clock); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> template -typename std::enable_if ::type -aged_ordered_container :: -swap_data (aged_ordered_container& other) noexcept +typename std::enable_if::type +aged_ordered_container:: + swap_data(aged_ordered_container& other) noexcept { - std::swap (m_config.key_compare(), other.m_config.key_compare()); - std::swap (m_config.clock, other.m_config.clock); + std::swap(m_config.key_compare(), other.m_config.key_compare()); + std::swap(m_config.clock, other.m_config.clock); } -} +} // namespace detail //------------------------------------------------------------------------------ -template -struct is_aged_container > - : std::true_type +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +struct is_aged_container> : std::true_type { explicit is_aged_container() = default; }; // Free functions -template -void swap ( - beast::detail::aged_ordered_container & lhs, - beast::detail::aged_ordered_container & rhs) noexcept +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator> +void +swap( + beast::detail::aged_ordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Compare, + Allocator>& lhs, + beast::detail::aged_ordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Compare, + Allocator>& rhs) noexcept { - lhs.swap (rhs); + lhs.swap(rhs); } /** Expire aged container items past the specified age. */ -template -std::size_t expire (detail::aged_ordered_container < - IsMulti, IsMap, Key, T, Clock, Compare, Allocator>& c, - std::chrono::duration const& age) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Compare, + class Allocator, + class Rep, + class Period> +std::size_t +expire( + detail::aged_ordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Compare, + Allocator>& c, + std::chrono::duration const& age) { - std::size_t n (0); - auto const expired (c.clock().now() - age); - for (auto iter (c.chronological.cbegin()); - iter != c.chronological.cend() && - iter.when() <= expired;) + std::size_t n(0); + auto const expired(c.clock().now() - age); + for (auto iter(c.chronological.cbegin()); + iter != c.chronological.cend() && iter.when() <= expired;) { - iter = c.erase (iter); + iter = c.erase(iter); ++n; } return n; } -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/detail/aged_unordered_container.h b/src/ripple/beast/container/detail/aged_unordered_container.h index 90f8b285fa..80ab9d7314 100644 --- a/src/ripple/beast/container/detail/aged_unordered_container.h +++ b/src/ripple/beast/container/detail/aged_unordered_container.h @@ -20,11 +20,11 @@ #ifndef BEAST_CONTAINER_DETAIL_AGED_UNORDERED_CONTAINER_H_INCLUDED #define BEAST_CONTAINER_DETAIL_AGED_UNORDERED_CONTAINER_H_INCLUDED -#include -#include -#include -#include #include +#include +#include +#include +#include #include #include #include @@ -78,13 +78,10 @@ template < class Key, class T, class Clock = std::chrono::steady_clock, - class Hash = std::hash , - class KeyEqual = std::equal_to , - class Allocator = std::allocator < - typename std::conditional , - Key>::type> -> + class Hash = std::hash, + class KeyEqual = std::equal_to, + class Allocator = std::allocator< + typename std::conditional, Key>::type>> class aged_unordered_container { public: @@ -93,32 +90,29 @@ public: using duration = typename clock_type::duration; using key_type = Key; using mapped_type = T; - using value_type = typename std::conditional , Key>::type; + using value_type = + typename std::conditional, Key>::type; using size_type = std::size_t; using difference_type = std::ptrdiff_t; // Introspection (for unit tests) using is_unordered = std::true_type; - using is_multi = std::integral_constant ; - using is_map = std::integral_constant ; + using is_multi = std::integral_constant; + using is_map = std::integral_constant; private: - static Key const& extract (value_type const& value) + static Key const& + extract(value_type const& value) { - return aged_associative_container_extract_t () (value); + return aged_associative_container_extract_t()(value); } // VFALCO TODO hoist to remove template argument dependencies struct element - : boost::intrusive::unordered_set_base_hook < - boost::intrusive::link_mode < - boost::intrusive::normal_link> - > - , boost::intrusive::list_base_hook < - boost::intrusive::link_mode < - boost::intrusive::normal_link> - > + : boost::intrusive::unordered_set_base_hook< + boost::intrusive::link_mode>, + boost::intrusive::list_base_hook< + boost::intrusive::link_mode> { // Stash types here so the iterator doesn't // need to see the container declaration. @@ -130,31 +124,22 @@ private: using time_point = typename aged_unordered_container::time_point; }; - element ( - time_point const& when_, - value_type const& value_) - : value (value_) - , when (when_) + element(time_point const& when_, value_type const& value_) + : value(value_), when(when_) { } - element ( - time_point const& when_, - value_type&& value_) - : value (std::move (value_)) - , when (when_) + element(time_point const& when_, value_type&& value_) + : value(std::move(value_)), when(when_) { } template < class... Args, - class = typename std::enable_if < - std::is_constructible ::value>::type - > - element (time_point const& when_, Args&&... args) - : value (std::forward (args)...) - , when (when_) + class = typename std::enable_if< + std::is_constructible::value>::type> + element(time_point const& when_, Args&&... args) + : value(std::forward(args)...), when(when_) { } @@ -163,10 +148,10 @@ private: }; // VFALCO TODO hoist to remove template argument dependencies - class ValueHash - : private beast::detail::empty_base_optimization + class ValueHash : private beast::detail::empty_base_optimization #ifdef _LIBCPP_VERSION - , public std::unary_function + , + public std::unary_function #endif { public: @@ -175,26 +160,29 @@ private: using result_type = size_t; #endif - ValueHash () + ValueHash() { } - ValueHash (Hash const& h) - : beast::detail::empty_base_optimization (h) + ValueHash(Hash const& h) + : beast::detail::empty_base_optimization(h) { } - std::size_t operator() (element const& e) const + std::size_t + operator()(element const& e) const { - return this->member() (extract (e.value)); + return this->member()(extract(e.value)); } - Hash& hash_function() + Hash& + hash_function() { return this->member(); } - Hash const& hash_function() const + Hash const& + hash_function() const { return this->member(); } @@ -203,9 +191,10 @@ private: // Compares value_type against element, used in find/insert_check // VFALCO TODO hoist to remove template argument dependencies class KeyValueEqual - : private beast::detail::empty_base_optimization + : private beast::detail::empty_base_optimization #ifdef _LIBCPP_VERSION - , public std::binary_function + , + public std::binary_function #endif { public: @@ -215,12 +204,12 @@ private: using result_type = bool; #endif - KeyValueEqual () + KeyValueEqual() { } - KeyValueEqual (KeyEqual const& keyEqual) - : beast::detail::empty_base_optimization (keyEqual) + KeyValueEqual(KeyEqual const& keyEqual) + : beast::detail::empty_base_optimization(keyEqual) { } @@ -240,179 +229,165 @@ private: } #endif - bool operator() (Key const& k, element const& e) const + bool + operator()(Key const& k, element const& e) const { - return this->member() (k, extract (e.value)); + return this->member()(k, extract(e.value)); } - bool operator() (element const& e, Key const& k) const + bool + operator()(element const& e, Key const& k) const { - return this->member() (extract (e.value), k); + return this->member()(extract(e.value), k); } - bool operator() (element const& lhs, element const& rhs) const + bool + operator()(element const& lhs, element const& rhs) const { - return this->member() (extract (lhs.value), extract (rhs.value)); + return this->member()(extract(lhs.value), extract(rhs.value)); } - KeyEqual& key_eq() + KeyEqual& + key_eq() { return this->member(); } - KeyEqual const& key_eq() const + KeyEqual const& + key_eq() const { return this->member(); } }; - using list_type = typename boost::intrusive::make_list >::type; + using list_type = typename boost::intrusive:: + make_list>::type; - using cont_type = typename std::conditional < + using cont_type = typename std::conditional< IsMulti, - typename boost::intrusive::make_unordered_multiset , - boost::intrusive::hash , - boost::intrusive::equal , - boost::intrusive::cache_begin - >::type, - typename boost::intrusive::make_unordered_set , - boost::intrusive::hash , - boost::intrusive::equal , - boost::intrusive::cache_begin - >::type - >::type; + typename boost::intrusive::make_unordered_multiset< + element, + boost::intrusive::constant_time_size, + boost::intrusive::hash, + boost::intrusive::equal, + boost::intrusive::cache_begin>::type, + typename boost::intrusive::make_unordered_set< + element, + boost::intrusive::constant_time_size, + boost::intrusive::hash, + boost::intrusive::equal, + boost::intrusive::cache_begin>::type>::type; using bucket_type = typename cont_type::bucket_type; using bucket_traits = typename cont_type::bucket_traits; - using ElementAllocator = typename std::allocator_traits < - Allocator>::template rebind_alloc ; + using ElementAllocator = typename std::allocator_traits< + Allocator>::template rebind_alloc; - using ElementAllocatorTraits = std::allocator_traits ; + using ElementAllocatorTraits = std::allocator_traits; - using BucketAllocator = typename std::allocator_traits < - Allocator>::template rebind_alloc ; + using BucketAllocator = typename std::allocator_traits< + Allocator>::template rebind_alloc; - using BucketAllocatorTraits = std::allocator_traits ; + using BucketAllocatorTraits = std::allocator_traits; class config_t - : private ValueHash - , private KeyValueEqual - , private beast::detail::empty_base_optimization + : private ValueHash, + private KeyValueEqual, + private beast::detail::empty_base_optimization { public: - explicit config_t ( - clock_type& clock_) - : clock (clock_) + explicit config_t(clock_type& clock_) : clock(clock_) { } - config_t ( - clock_type& clock_, - Hash const& hash) - : ValueHash (hash) - , clock (clock_) + config_t(clock_type& clock_, Hash const& hash) + : ValueHash(hash), clock(clock_) { } - config_t ( - clock_type& clock_, - KeyEqual const& keyEqual) - : KeyValueEqual (keyEqual) - , clock (clock_) + config_t(clock_type& clock_, KeyEqual const& keyEqual) + : KeyValueEqual(keyEqual), clock(clock_) { } - config_t ( - clock_type& clock_, - Allocator const& alloc_) - : beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + config_t(clock_type& clock_, Allocator const& alloc_) + : beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t ( - clock_type& clock_, - Hash const& hash, - KeyEqual const& keyEqual) - : ValueHash (hash) - , KeyValueEqual (keyEqual) - , clock (clock_) + config_t(clock_type& clock_, Hash const& hash, KeyEqual const& keyEqual) + : ValueHash(hash), KeyValueEqual(keyEqual), clock(clock_) { } - config_t ( - clock_type& clock_, - Hash const& hash, - Allocator const& alloc_) - : ValueHash (hash) - , beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + config_t(clock_type& clock_, Hash const& hash, Allocator const& alloc_) + : ValueHash(hash) + , beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t ( + config_t( clock_type& clock_, KeyEqual const& keyEqual, Allocator const& alloc_) - : KeyValueEqual (keyEqual) - , beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + : KeyValueEqual(keyEqual) + , beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t ( + config_t( clock_type& clock_, Hash const& hash, KeyEqual const& keyEqual, Allocator const& alloc_) - : ValueHash (hash) - , KeyValueEqual (keyEqual) - , beast::detail::empty_base_optimization (alloc_) - , clock (clock_) + : ValueHash(hash) + , KeyValueEqual(keyEqual) + , beast::detail::empty_base_optimization(alloc_) + , clock(clock_) { } - config_t (config_t const& other) - : ValueHash (other.hash_function()) - , KeyValueEqual (other.key_eq()) - , beast::detail::empty_base_optimization ( - ElementAllocatorTraits:: - select_on_container_copy_construction ( - other.alloc())) - , clock (other.clock) + config_t(config_t const& other) + : ValueHash(other.hash_function()) + , KeyValueEqual(other.key_eq()) + , beast::detail::empty_base_optimization( + ElementAllocatorTraits::select_on_container_copy_construction( + other.alloc())) + , clock(other.clock) { } - config_t (config_t const& other, Allocator const& alloc) - : ValueHash (other.hash_function()) - , KeyValueEqual (other.key_eq()) - , beast::detail::empty_base_optimization (alloc) - , clock (other.clock) + config_t(config_t const& other, Allocator const& alloc) + : ValueHash(other.hash_function()) + , KeyValueEqual(other.key_eq()) + , beast::detail::empty_base_optimization(alloc) + , clock(other.clock) { } - config_t (config_t&& other) - : ValueHash (std::move (other.hash_function())) - , KeyValueEqual (std::move (other.key_eq())) - , beast::detail::empty_base_optimization ( - std::move (other.alloc())) - , clock (other.clock) + config_t(config_t&& other) + : ValueHash(std::move(other.hash_function())) + , KeyValueEqual(std::move(other.key_eq())) + , beast::detail::empty_base_optimization( + std::move(other.alloc())) + , clock(other.clock) { } - config_t (config_t&& other, Allocator const& alloc) - : ValueHash (std::move (other.hash_function())) - , KeyValueEqual (std::move (other.key_eq())) - , beast::detail::empty_base_optimization (alloc) - , clock (other.clock) + config_t(config_t&& other, Allocator const& alloc) + : ValueHash(std::move(other.hash_function())) + , KeyValueEqual(std::move(other.key_eq())) + , beast::detail::empty_base_optimization(alloc) + , clock(other.clock) { } - config_t& operator= (config_t const& other) + config_t& + operator=(config_t const& other) { hash_function() = other.hash_function(); key_eq() = other.key_eq(); @@ -421,68 +396,79 @@ private: return *this; } - config_t& operator= (config_t&& other) + config_t& + operator=(config_t&& other) { - hash_function() = std::move (other.hash_function()); - key_eq() = std::move (other.key_eq()); - alloc() = std::move (other.alloc()); + hash_function() = std::move(other.hash_function()); + key_eq() = std::move(other.key_eq()); + alloc() = std::move(other.alloc()); clock = other.clock; return *this; } - ValueHash& value_hash() + ValueHash& + value_hash() { return *this; } - ValueHash const& value_hash() const + ValueHash const& + value_hash() const { return *this; } - Hash& hash_function() + Hash& + hash_function() { return ValueHash::hash_function(); } - Hash const& hash_function() const + Hash const& + hash_function() const { return ValueHash::hash_function(); } - KeyValueEqual& key_value_equal() + KeyValueEqual& + key_value_equal() { return *this; } - KeyValueEqual const& key_value_equal() const + KeyValueEqual const& + key_value_equal() const { return *this; } - KeyEqual& key_eq() + KeyEqual& + key_eq() { return key_value_equal().key_eq(); } - KeyEqual const& key_eq() const + KeyEqual const& + key_eq() const { return key_value_equal().key_eq(); } - ElementAllocator& alloc() + ElementAllocator& + alloc() { - return beast::detail::empty_base_optimization < + return beast::detail::empty_base_optimization< ElementAllocator>::member(); } - ElementAllocator const& alloc() const + ElementAllocator const& + alloc() const { - return beast::detail::empty_base_optimization < + return beast::detail::empty_base_optimization< ElementAllocator>::member(); } - std::reference_wrapper clock; + std::reference_wrapper clock; }; class Buckets @@ -490,66 +476,64 @@ private: public: using vec_type = std::vector< bucket_type, - typename std::allocator_traits :: - template rebind_alloc >; + typename std::allocator_traits::template rebind_alloc< + bucket_type>>; - Buckets () - : m_max_load_factor (1.f) - , m_vec () + Buckets() : m_max_load_factor(1.f), m_vec() { - m_vec.resize ( - cont_type::suggested_upper_bucket_count (0)); + m_vec.resize(cont_type::suggested_upper_bucket_count(0)); } - Buckets (Allocator const& alloc) - : m_max_load_factor (1.f) - , m_vec (alloc) + Buckets(Allocator const& alloc) : m_max_load_factor(1.f), m_vec(alloc) { - m_vec.resize ( - cont_type::suggested_upper_bucket_count (0)); + m_vec.resize(cont_type::suggested_upper_bucket_count(0)); } operator bucket_traits() { - return bucket_traits (&m_vec[0], m_vec.size()); + return bucket_traits(&m_vec[0], m_vec.size()); } - void clear() + void + clear() { m_vec.clear(); } - size_type max_bucket_count() const + size_type + max_bucket_count() const { return m_vec.max_size(); } - float& max_load_factor() + float& + max_load_factor() { return m_max_load_factor; } - float const& max_load_factor() const + float const& + max_load_factor() const { return m_max_load_factor; } // count is the number of buckets template - void rehash (size_type count, Container& c) + void + rehash(size_type count, Container& c) { - size_type const size (m_vec.size()); + size_type const size(m_vec.size()); if (count == size) return; if (count > m_vec.capacity()) { // Need two vectors otherwise we // will destroy non-empty buckets. - vec_type vec (m_vec.get_allocator()); - std::swap (m_vec, vec); - m_vec.resize (count); - c.rehash (bucket_traits ( - &m_vec[0], m_vec.size())); + vec_type vec(m_vec.get_allocator()); + std::swap(m_vec, vec); + m_vec.resize(count); + c.rehash(bucket_traits(&m_vec[0], m_vec.size())); return; } // Rehash in place. @@ -557,25 +541,24 @@ private: { // This should not reallocate since // we checked capacity earlier. - m_vec.resize (count); - c.rehash (bucket_traits ( - &m_vec[0], count)); + m_vec.resize(count); + c.rehash(bucket_traits(&m_vec[0], count)); return; } // Resize must happen after rehash otherwise // we might destroy non-empty buckets. - c.rehash (bucket_traits ( - &m_vec[0], count)); - m_vec.resize (count); + c.rehash(bucket_traits(&m_vec[0], count)); + m_vec.resize(count); } // Resize the buckets to accomodate at least n items. template - void resize (size_type n, Container& c) + void + resize(size_type n, Container& c) { - size_type const suggested ( - cont_type::suggested_upper_bucket_count (n)); - rehash (suggested, c); + size_type const suggested( + cont_type::suggested_upper_bucket_count(n)); + rehash(suggested, c); } private: @@ -584,43 +567,48 @@ private: }; template - element* new_element (Args&&... args) + element* + new_element(Args&&... args) { struct Deleter { - std::reference_wrapper a_; - Deleter (ElementAllocator& a) - : a_(a) + std::reference_wrapper a_; + Deleter(ElementAllocator& a) : a_(a) { } void operator()(element* p) { - ElementAllocatorTraits::deallocate (a_.get(), p, 1); + ElementAllocatorTraits::deallocate(a_.get(), p, 1); } }; - std::unique_ptr p (ElementAllocatorTraits::allocate ( - m_config.alloc(), 1), Deleter(m_config.alloc())); - ElementAllocatorTraits::construct (m_config.alloc(), - p.get(), clock().now(), std::forward (args)...); + std::unique_ptr p( + ElementAllocatorTraits::allocate(m_config.alloc(), 1), + Deleter(m_config.alloc())); + ElementAllocatorTraits::construct( + m_config.alloc(), + p.get(), + clock().now(), + std::forward(args)...); return p.release(); } - void delete_element (element const* p) + void + delete_element(element const* p) { - ElementAllocatorTraits::destroy (m_config.alloc(), p); - ElementAllocatorTraits::deallocate ( + ElementAllocatorTraits::destroy(m_config.alloc(), p); + ElementAllocatorTraits::deallocate( m_config.alloc(), const_cast(p), 1); } - void unlink_and_delete_element (element const* p) + void + unlink_and_delete_element(element const* p) { - chronological.list.erase ( - chronological.list.iterator_to (*p)); - m_cont.erase (m_cont.iterator_to (*p)); - delete_element (p); + chronological.list.erase(chronological.list.iterator_to(*p)); + m_cont.erase(m_cont.iterator_to(*p)); + delete_element(p); } public: @@ -629,22 +617,21 @@ public: using allocator_type = Allocator; using reference = value_type&; using const_reference = value_type const&; - using pointer = typename std::allocator_traits < - Allocator>::pointer; - using const_pointer = typename std::allocator_traits < - Allocator>::const_pointer; + using pointer = typename std::allocator_traits::pointer; + using const_pointer = + typename std::allocator_traits::const_pointer; // A set iterator (IsMap==false) is always const // because the elements of a set are immutable. - using iterator= beast::detail::aged_container_iterator ; - using const_iterator = beast::detail::aged_container_iterator ; + using iterator = beast::detail:: + aged_container_iterator; + using const_iterator = beast::detail:: + aged_container_iterator; - using local_iterator = beast::detail::aged_container_iterator ; - using const_local_iterator = beast::detail::aged_container_iterator ; + using local_iterator = beast::detail:: + aged_container_iterator; + using const_local_iterator = beast::detail:: + aged_container_iterator; //-------------------------------------------------------------------------- // @@ -660,100 +647,117 @@ public: public: // A set iterator (IsMap==false) is always const // because the elements of a set are immutable. - using iterator = beast::detail::aged_container_iterator < - ! IsMap, typename list_type::iterator>; - using const_iterator = beast::detail::aged_container_iterator < - true, typename list_type::iterator>; - using reverse_iterator = beast::detail::aged_container_iterator < - ! IsMap, typename list_type::reverse_iterator>; - using const_reverse_iterator = beast::detail::aged_container_iterator < - true, typename list_type::reverse_iterator>; + using iterator = beast::detail:: + aged_container_iterator; + using const_iterator = beast::detail:: + aged_container_iterator; + using reverse_iterator = beast::detail::aged_container_iterator< + !IsMap, + typename list_type::reverse_iterator>; + using const_reverse_iterator = beast::detail:: + aged_container_iterator; - iterator begin () - { - return iterator (list.begin()); + iterator + begin() + { + return iterator(list.begin()); } - const_iterator begin () const + const_iterator + begin() const { - return const_iterator (list.begin ()); + return const_iterator(list.begin()); } - const_iterator cbegin() const + const_iterator + cbegin() const { - return const_iterator (list.begin ()); + return const_iterator(list.begin()); } - iterator end () + iterator + end() { - return iterator (list.end ()); + return iterator(list.end()); } - const_iterator end () const + const_iterator + end() const { - return const_iterator (list.end ()); + return const_iterator(list.end()); } - const_iterator cend () const + const_iterator + cend() const { - return const_iterator (list.end ()); + return const_iterator(list.end()); } - reverse_iterator rbegin () + reverse_iterator + rbegin() { - return reverse_iterator (list.rbegin()); + return reverse_iterator(list.rbegin()); } - const_reverse_iterator rbegin () const + const_reverse_iterator + rbegin() const { - return const_reverse_iterator (list.rbegin ()); + return const_reverse_iterator(list.rbegin()); } - const_reverse_iterator crbegin() const + const_reverse_iterator + crbegin() const { - return const_reverse_iterator (list.rbegin ()); + return const_reverse_iterator(list.rbegin()); } - reverse_iterator rend () + reverse_iterator + rend() { - return reverse_iterator (list.rend ()); + return reverse_iterator(list.rend()); } - const_reverse_iterator rend () const + const_reverse_iterator + rend() const { - return const_reverse_iterator (list.rend ()); + return const_reverse_iterator(list.rend()); } - const_reverse_iterator crend () const + const_reverse_iterator + crend() const { - return const_reverse_iterator (list.rend ()); + return const_reverse_iterator(list.rend()); } - iterator iterator_to (value_type& value) + iterator + iterator_to(value_type& value) { - static_assert (std::is_standard_layout ::value, + static_assert( + std::is_standard_layout::value, "must be standard layout"); - return list.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + return list.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } - const_iterator iterator_to (value_type const& value) const + const_iterator + iterator_to(value_type const& value) const { - static_assert (std::is_standard_layout ::value, + static_assert( + std::is_standard_layout::value, "must be standard layout"); - return list.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + return list.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } private: - chronological_t () + chronological_t() { } - chronological_t (chronological_t const&) = delete; - chronological_t (chronological_t&&) = delete; + chronological_t(chronological_t const&) = delete; + chronological_t(chronological_t&&) = delete; friend class aged_unordered_container; list_type mutable list; @@ -767,117 +771,173 @@ public: aged_unordered_container() = delete; - explicit aged_unordered_container (clock_type& clock); + explicit aged_unordered_container(clock_type& clock); - aged_unordered_container (clock_type& clock, Hash const& hash); + aged_unordered_container(clock_type& clock, Hash const& hash); - aged_unordered_container (clock_type& clock, + aged_unordered_container(clock_type& clock, KeyEqual const& key_eq); + + aged_unordered_container(clock_type& clock, Allocator const& alloc); + + aged_unordered_container( + clock_type& clock, + Hash const& hash, KeyEqual const& key_eq); - aged_unordered_container (clock_type& clock, + aged_unordered_container( + clock_type& clock, + Hash const& hash, Allocator const& alloc); - aged_unordered_container (clock_type& clock, - Hash const& hash, KeyEqual const& key_eq); + aged_unordered_container( + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc); - aged_unordered_container (clock_type& clock, - Hash const& hash, Allocator const& alloc); - - aged_unordered_container (clock_type& clock, - KeyEqual const& key_eq, Allocator const& alloc); - - aged_unordered_container ( - clock_type& clock, Hash const& hash, KeyEqual const& key_eq, - Allocator const& alloc); + aged_unordered_container( + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc); template - aged_unordered_container (InputIt first, InputIt last, + aged_unordered_container(InputIt first, InputIt last, clock_type& clock); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + KeyEqual const& key_eq); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Allocator const& alloc); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + Allocator const& alloc); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc); + + template + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc); + + aged_unordered_container(aged_unordered_container const& other); + + aged_unordered_container( + aged_unordered_container const& other, + Allocator const& alloc); + + aged_unordered_container(aged_unordered_container&& other); + + aged_unordered_container( + aged_unordered_container&& other, + Allocator const& alloc); + + aged_unordered_container( + std::initializer_list init, clock_type& clock); - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, Hash const& hash); + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash); - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, KeyEqual const& key_eq); + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + KeyEqual const& key_eq); - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, Allocator const& alloc); - - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, Hash const& hash, KeyEqual const& key_eq); - - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, Hash const& hash, Allocator const& alloc); - - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, KeyEqual const& key_eq, - Allocator const& alloc); - - template - aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, Hash const& hash, KeyEqual const& key_eq, - Allocator const& alloc); - - aged_unordered_container (aged_unordered_container const& other); - - aged_unordered_container (aged_unordered_container const& other, + aged_unordered_container( + std::initializer_list init, + clock_type& clock, Allocator const& alloc); - aged_unordered_container (aged_unordered_container&& other); + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq); - aged_unordered_container (aged_unordered_container&& other, + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, Allocator const& alloc); - aged_unordered_container (std::initializer_list init, - clock_type& clock); + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc); - aged_unordered_container (std::initializer_list init, - clock_type& clock, Hash const& hash); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, KeyEqual const& key_eq); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, Allocator const& alloc); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, Hash const& hash, KeyEqual const& key_eq); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, Hash const& hash, Allocator const& alloc); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, KeyEqual const& key_eq, Allocator const& alloc); - - aged_unordered_container (std::initializer_list init, - clock_type& clock, Hash const& hash, KeyEqual const& key_eq, - Allocator const& alloc); + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc); ~aged_unordered_container(); - aged_unordered_container& operator= (aged_unordered_container const& other); + aged_unordered_container& + operator=(aged_unordered_container const& other); - aged_unordered_container& operator= (aged_unordered_container&& other); + aged_unordered_container& + operator=(aged_unordered_container&& other); - aged_unordered_container& operator= (std::initializer_list init); + aged_unordered_container& + operator=(std::initializer_list init); - allocator_type get_allocator() const + allocator_type + get_allocator() const { return m_config.alloc(); } - clock_type& clock() + clock_type& + clock() { return m_config.clock; } - clock_type const& clock() const + clock_type const& + clock() const { return m_config.clock; } @@ -892,31 +952,31 @@ public: class K, bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - at (K const& k); + class = typename std::enable_if::type> + typename std::conditional::type& + at(K const& k); template < class K, bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type const& - at (K const& k) const; + class = typename std::enable_if::type> + typename std::conditional::type const& + at(K const& k) const; template < bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - operator[] (Key const& key); + class = typename std::enable_if::type> + typename std::conditional::type& + operator[](Key const& key); template < bool maybe_multi = IsMulti, bool maybe_map = IsMap, - class = typename std::enable_if ::type> - typename std::conditional ::type& - operator[] (Key&& key); + class = typename std::enable_if::type> + typename std::conditional::type& + operator[](Key&& key); //-------------------------------------------------------------------------- // @@ -925,59 +985,59 @@ public: //-------------------------------------------------------------------------- iterator - begin () + begin() { - return iterator (m_cont.begin()); + return iterator(m_cont.begin()); } const_iterator - begin () const + begin() const { - return const_iterator (m_cont.begin ()); + return const_iterator(m_cont.begin()); } const_iterator cbegin() const { - return const_iterator (m_cont.begin ()); + return const_iterator(m_cont.begin()); } iterator - end () + end() { - return iterator (m_cont.end ()); + return iterator(m_cont.end()); } const_iterator - end () const + end() const { - return const_iterator (m_cont.end ()); + return const_iterator(m_cont.end()); } const_iterator - cend () const + cend() const { - return const_iterator (m_cont.end ()); + return const_iterator(m_cont.end()); } iterator - iterator_to (value_type& value) + iterator_to(value_type& value) { - static_assert (std::is_standard_layout ::value, - "must be standard layout"); - return m_cont.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + static_assert( + std::is_standard_layout::value, "must be standard layout"); + return m_cont.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } const_iterator - iterator_to (value_type const& value) const + iterator_to(value_type const& value) const { - static_assert (std::is_standard_layout ::value, - "must be standard layout"); - return m_cont.iterator_to (*reinterpret_cast ( - reinterpret_cast(&value)-((std::size_t) - std::addressof(((element*)0)->member)))); + static_assert( + std::is_standard_layout::value, "must be standard layout"); + return m_cont.iterator_to(*reinterpret_cast( + reinterpret_cast(&value) - + ((std::size_t)std::addressof(((element*)0)->member)))); } //-------------------------------------------------------------------------- @@ -986,17 +1046,20 @@ public: // //-------------------------------------------------------------------------- - bool empty() const noexcept + bool + empty() const noexcept { return m_cont.empty(); } - size_type size() const noexcept + size_type + size() const noexcept { return m_cont.size(); } - size_type max_size() const noexcept + size_type + max_size() const noexcept { return m_config.max_size(); } @@ -1007,191 +1070,168 @@ public: // //-------------------------------------------------------------------------- - void clear(); + void + clear(); // map, set template auto - insert (value_type const& value) -> - typename std::enable_if >::type; + insert(value_type const& value) -> + typename std::enable_if>::type; // multimap, multiset template auto - insert (value_type const& value) -> - typename std::enable_if ::type; + insert(value_type const& value) -> + typename std::enable_if::type; // map, set template auto - insert (value_type&& value) -> - typename std::enable_if >::type; + insert(value_type&& value) -> typename std:: + enable_if>::type; // multimap, multiset template auto - insert (value_type&& value) -> - typename std::enable_if ::type; + insert(value_type&& value) -> + typename std::enable_if::type; // map, set template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type const& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type const& value) { // Hint is ignored but we provide the interface so // callers may use ordered and unordered interchangeably. - return insert (value).first; + return insert(value).first; } // multimap, multiset template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type const& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type const& value) { // VFALCO TODO The hint could be used to let // the client order equal ranges - return insert (value); + return insert(value); } // map, set template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type&& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type&& value) { // Hint is ignored but we provide the interface so // callers may use ordered and unordered interchangeably. - return insert (std::move (value)).first; + return insert(std::move(value)).first; } // multimap, multiset template - typename std::enable_if ::type - insert (const_iterator /*hint*/, value_type&& value) + typename std::enable_if::type + insert(const_iterator /*hint*/, value_type&& value) { // VFALCO TODO The hint could be used to let // the client order equal ranges - return insert (std::move (value)); + return insert(std::move(value)); } // map, multimap - template < - class P, - bool maybe_map = IsMap - > - typename std::enable_if ::value, - typename std::conditional - >::type - >::type - insert (P&& value) + template + typename std::enable_if< + maybe_map && std::is_constructible::value, + typename std:: + conditional>::type>:: + type + insert(P&& value) { - return emplace (std::forward

(value)); + return emplace(std::forward

(value)); } // map, multimap - template < - class P, - bool maybe_map = IsMap - > - typename std::enable_if ::value, - typename std::conditional - >::type - >::type - insert (const_iterator hint, P&& value) + template + typename std::enable_if< + maybe_map && std::is_constructible::value, + typename std:: + conditional>::type>:: + type + insert(const_iterator hint, P&& value) { - return emplace_hint (hint, std::forward

(value)); + return emplace_hint(hint, std::forward

(value)); } template - void insert (InputIt first, InputIt last) + void + insert(InputIt first, InputIt last) { - insert (first, last, - typename std::iterator_traits < - InputIt>::iterator_category()); + insert( + first, + last, + typename std::iterator_traits::iterator_category()); } void - insert (std::initializer_list init) + insert(std::initializer_list init) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } // set, map template auto - emplace (Args&&... args) -> - typename std::enable_if >::type; + emplace(Args&&... args) -> + typename std::enable_if>::type; // multiset, multimap template auto - emplace (Args&&... args) -> - typename std::enable_if ::type; + emplace(Args&&... args) -> + typename std::enable_if::type; // set, map template auto - emplace_hint (const_iterator /*hint*/, Args&&... args) -> - typename std::enable_if >::type; + emplace_hint(const_iterator /*hint*/, Args&&... args) -> + typename std::enable_if>::type; // multiset, multimap template - typename std::enable_if ::type - emplace_hint (const_iterator /*hint*/, Args&&... args) + typename std::enable_if::type + emplace_hint(const_iterator /*hint*/, Args&&... args) { // VFALCO TODO The hint could be used for multi, to let // the client order equal ranges - return emplace ( - std::forward (args)...); + return emplace(std::forward(args)...); } template - beast::detail::aged_container_iterator - erase (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos); + beast::detail::aged_container_iterator + erase(beast::detail::aged_container_iterator pos); template - beast::detail::aged_container_iterator - erase (beast::detail::aged_container_iterator < - is_const, Iterator, Base> first, - beast::detail::aged_container_iterator < - is_const, Iterator, Base> last); + beast::detail::aged_container_iterator + erase( + beast::detail::aged_container_iterator first, + beast::detail::aged_container_iterator last); template auto - erase (K const& k) -> - size_type; + erase(K const& k) -> size_type; void - swap (aged_unordered_container& other) noexcept; + swap(aged_unordered_container& other) noexcept; template void - touch (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos) + touch(beast::detail::aged_container_iterator pos) { - touch (pos, clock().now()); + touch(pos, clock().now()); } template auto - touch (K const& k) -> - size_type; + touch(K const& k) -> size_type; //-------------------------------------------------------------------------- // @@ -1202,54 +1242,59 @@ public: // VFALCO TODO Respect is_transparent (c++14) template size_type - count (K const& k) const + count(K const& k) const { - return m_cont.count (k, std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal())); + return m_cont.count( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal())); } // VFALCO TODO Respect is_transparent (c++14) template iterator - find (K const& k) + find(K const& k) { - return iterator (m_cont.find (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); + return iterator(m_cont.find( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); } // VFALCO TODO Respect is_transparent (c++14) template const_iterator - find (K const& k) const + find(K const& k) const { - return const_iterator (m_cont.find (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); + return const_iterator(m_cont.find( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); } // VFALCO TODO Respect is_transparent (c++14) template - std::pair - equal_range (K const& k) + std::pair + equal_range(K const& k) { - auto const r (m_cont.equal_range (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); - return std::make_pair (iterator (r.first), - iterator (r.second)); + auto const r(m_cont.equal_range( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); + return std::make_pair(iterator(r.first), iterator(r.second)); } // VFALCO TODO Respect is_transparent (c++14) template - std::pair - equal_range (K const& k) const + std::pair + equal_range(K const& k) const { - auto const r (m_cont.equal_range (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); - return std::make_pair (const_iterator (r.first), - const_iterator (r.second)); + auto const r(m_cont.equal_range( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); + return std::make_pair( + const_iterator(r.first), const_iterator(r.second)); } //-------------------------------------------------------------------------- @@ -1258,56 +1303,65 @@ public: // //-------------------------------------------------------------------------- - local_iterator begin (size_type n) + local_iterator + begin(size_type n) { - return local_iterator (m_cont.begin (n)); + return local_iterator(m_cont.begin(n)); } - const_local_iterator begin (size_type n) const + const_local_iterator + begin(size_type n) const { - return const_local_iterator (m_cont.begin (n)); + return const_local_iterator(m_cont.begin(n)); } - const_local_iterator cbegin (size_type n) const + const_local_iterator + cbegin(size_type n) const { - return const_local_iterator (m_cont.begin (n)); + return const_local_iterator(m_cont.begin(n)); } - local_iterator end (size_type n) + local_iterator + end(size_type n) { - return local_iterator (m_cont.end (n)); + return local_iterator(m_cont.end(n)); } - const_local_iterator end (size_type n) const + const_local_iterator + end(size_type n) const { - return const_local_iterator (m_cont.end (n)); + return const_local_iterator(m_cont.end(n)); } - const_local_iterator cend (size_type n) const + const_local_iterator + cend(size_type n) const { - return const_local_iterator (m_cont.end (n)); + return const_local_iterator(m_cont.end(n)); } - size_type bucket_count() const + size_type + bucket_count() const { return m_cont.bucket_count(); } - size_type max_bucket_count() const + size_type + max_bucket_count() const { return m_buck.max_bucket_count(); } - size_type bucket_size (size_type n) const + size_type + bucket_size(size_type n) const { - return m_cont.bucket_size (n); + return m_cont.bucket_size(n); } - size_type bucket (Key const& k) const + size_type + bucket(Key const& k) const { - assert (bucket_count() != 0); - return m_cont.bucket (k, - std::cref (m_config.hash_function())); + assert(bucket_count() != 0); + return m_cont.bucket(k, std::cref(m_config.hash_function())); } //-------------------------------------------------------------------------- @@ -1316,33 +1370,35 @@ public: // //-------------------------------------------------------------------------- - float load_factor() const + float + load_factor() const { - return size() / - static_cast (m_cont.bucket_count()); + return size() / static_cast(m_cont.bucket_count()); } - float max_load_factor() const + float + max_load_factor() const { return m_buck.max_load_factor(); } - void max_load_factor (float ml) + void + max_load_factor(float ml) { - m_buck.max_load_factor () = - std::max (ml, m_buck.max_load_factor()); + m_buck.max_load_factor() = std::max(ml, m_buck.max_load_factor()); } - void rehash (size_type count) + void + rehash(size_type count) { - count = std::max (count, - size_type (size() / max_load_factor())); - m_buck.rehash (count, m_cont); + count = std::max(count, size_type(size() / max_load_factor())); + m_buck.rehash(count, m_cont); } - void reserve (size_type count) + void + reserve(size_type count) { - rehash (std::ceil (count / max_load_factor())); + rehash(std::ceil(count / max_load_factor())); } //-------------------------------------------------------------------------- @@ -1351,12 +1407,14 @@ public: // //-------------------------------------------------------------------------- - hasher const& hash_function() const + hasher const& + hash_function() const { return m_config.hash_function(); } - key_equal const& key_eq () const + key_equal const& + key_eq() const { return m_config.key_eq(); } @@ -1378,13 +1436,17 @@ public: class OtherDuration, class OtherHash, class OtherAllocator, - bool maybe_multi = IsMulti - > - typename std::enable_if ::type - operator== ( - aged_unordered_container const& other) const; + bool maybe_multi = IsMulti> + typename std::enable_if::type + operator==(aged_unordered_container< + false, + OtherIsMap, + OtherKey, + OtherT, + OtherDuration, + OtherHash, + KeyEqual, + OtherAllocator> const& other) const; template < bool OtherIsMap, @@ -1393,13 +1455,17 @@ public: class OtherDuration, class OtherHash, class OtherAllocator, - bool maybe_multi = IsMulti - > - typename std::enable_if ::type - operator== ( - aged_unordered_container const& other) const; + bool maybe_multi = IsMulti> + typename std::enable_if::type + operator==(aged_unordered_container< + true, + OtherIsMap, + OtherKey, + OtherT, + OtherDuration, + OtherHash, + KeyEqual, + OtherAllocator> const& other) const; template < bool OtherIsMulti, @@ -1408,102 +1474,104 @@ public: class OtherT, class OtherDuration, class OtherHash, - class OtherAllocator - > - bool operator!= ( - aged_unordered_container const& other) const + class OtherAllocator> + bool + operator!=(aged_unordered_container< + OtherIsMulti, + OtherIsMap, + OtherKey, + OtherT, + OtherDuration, + OtherHash, + KeyEqual, + OtherAllocator> const& other) const { - return ! (this->operator== (other)); + return !(this->operator==(other)); } private: bool - would_exceed (size_type additional) const + would_exceed(size_type additional) const { - return size() + additional > - bucket_count() * max_load_factor(); + return size() + additional > bucket_count() * max_load_factor(); } void - maybe_rehash (size_type additional) + maybe_rehash(size_type additional) { - if (would_exceed (additional)) - m_buck.resize (size() + additional, m_cont); - assert (load_factor() <= max_load_factor()); + if (would_exceed(additional)) + m_buck.resize(size() + additional, m_cont); + assert(load_factor() <= max_load_factor()); } // map, set template auto - insert_unchecked (value_type const& value) -> - typename std::enable_if >::type; + insert_unchecked(value_type const& value) -> + typename std::enable_if>::type; // multimap, multiset template auto - insert_unchecked (value_type const& value) -> - typename std::enable_if ::type; + insert_unchecked(value_type const& value) -> + typename std::enable_if::type; template void - insert_unchecked (InputIt first, InputIt last) + insert_unchecked(InputIt first, InputIt last) { for (; first != last; ++first) - insert_unchecked (*first); + insert_unchecked(*first); } template void - insert (InputIt first, InputIt last, - std::input_iterator_tag) + insert(InputIt first, InputIt last, std::input_iterator_tag) { for (; first != last; ++first) - insert (*first); + insert(*first); } template void - insert (InputIt first, InputIt last, - std::random_access_iterator_tag) + insert(InputIt first, InputIt last, std::random_access_iterator_tag) { - auto const n (std::distance (first, last)); - maybe_rehash (n); - insert_unchecked (first, last); + auto const n(std::distance(first, last)); + maybe_rehash(n); + insert_unchecked(first, last); } template void - touch (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos, - typename clock_type::time_point const& now) + touch( + beast::detail::aged_container_iterator pos, + typename clock_type::time_point const& now) { - auto& e (*pos.iterator()); + auto& e(*pos.iterator()); e.when = now; - chronological.list.erase (chronological.list.iterator_to (e)); - chronological.list.push_back (e); + chronological.list.erase(chronological.list.iterator_to(e)); + chronological.list.push_back(e); } - template ::propagate_on_container_swap::value> - typename std::enable_if ::type - swap_data (aged_unordered_container& other) noexcept + template < + bool maybe_propagate = std::allocator_traits< + Allocator>::propagate_on_container_swap::value> + typename std::enable_if::type + swap_data(aged_unordered_container& other) noexcept { - std::swap (m_config.key_compare(), other.m_config.key_compare()); - std::swap (m_config.alloc(), other.m_config.alloc()); - std::swap (m_config.clock, other.m_config.clock); + std::swap(m_config.key_compare(), other.m_config.key_compare()); + std::swap(m_config.alloc(), other.m_config.alloc()); + std::swap(m_config.clock, other.m_config.clock); } - template ::propagate_on_container_swap::value> - typename std::enable_if ::type - swap_data (aged_unordered_container& other) noexcept + template < + bool maybe_propagate = std::allocator_traits< + Allocator>::propagate_on_container_swap::value> + typename std::enable_if::type + swap_data(aged_unordered_container& other) noexcept { - std::swap (m_config.key_compare(), other.m_config.key_compare()); - std::swap (m_config.clock, other.m_config.clock); + std::swap(m_config.key_compare(), other.m_config.key_compare()); + std::swap(m_config.clock, other.m_config.clock); } private: @@ -1514,588 +1582,1129 @@ private: //------------------------------------------------------------------------------ -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock) - : m_config (clock) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::aged_unordered_container(clock_type& clock) + : m_config(clock) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - Hash const& hash) - : m_config (clock, hash) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::aged_unordered_container(clock_type& clock, Hash const& hash) + : m_config(clock, hash) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - KeyEqual const& key_eq) - : m_config (clock, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container(clock_type& clock, KeyEqual const& key_eq) + : m_config(clock, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container(clock_type& clock, Allocator const& alloc) + : m_config(clock, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq) - : m_config (clock, hash, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq) + : m_config(clock, hash, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - Hash const& hash, - Allocator const& alloc) - : m_config (clock, hash, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + clock_type& clock, + Hash const& hash, + Allocator const& alloc) + : m_config(clock, hash, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template -aged_unordered_container :: -aged_unordered_container ( - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, hash, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, hash, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock) - : m_config (clock) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container(InputIt first, InputIt last, clock_type& clock) + : m_config(clock) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - Hash const& hash) - : m_config (clock, hash) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash) + : m_config(clock, hash) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - KeyEqual const& key_eq) - : m_config (clock, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + KeyEqual const& key_eq) + : m_config(clock, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Allocator const& alloc) + : m_config(clock, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq) - : m_config (clock, hash, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq) + : m_config(clock, hash, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - Hash const& hash, - Allocator const& alloc) - : m_config (clock, hash, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + Allocator const& alloc) + : m_config(clock, hash, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -aged_unordered_container :: -aged_unordered_container (InputIt first, InputIt last, - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, hash, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + InputIt first, + InputIt last, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, hash, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (first, last); + insert(first, last); } -template -aged_unordered_container :: -aged_unordered_container (aged_unordered_container const& other) - : m_config (other.m_config) - , m_buck (m_config.alloc()) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::aged_unordered_container(aged_unordered_container const& other) + : m_config(other.m_config) + , m_buck(m_config.alloc()) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (other.cbegin(), other.cend()); + insert(other.cbegin(), other.cend()); } -template -aged_unordered_container :: -aged_unordered_container (aged_unordered_container const& other, - Allocator const& alloc) - : m_config (other.m_config, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + aged_unordered_container const& other, + Allocator const& alloc) + : m_config(other.m_config, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (other.cbegin(), other.cend()); + insert(other.cbegin(), other.cend()); } -template -aged_unordered_container :: -aged_unordered_container (aged_unordered_container&& other) - : m_config (std::move (other.m_config)) - , m_buck (std::move (other.m_buck)) - , m_cont (std::move (other.m_cont)) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::aged_unordered_container(aged_unordered_container&& other) + : m_config(std::move(other.m_config)) + , m_buck(std::move(other.m_buck)) + , m_cont(std::move(other.m_cont)) { - chronological.list = std::move (other.chronological.list); + chronological.list = std::move(other.chronological.list); } -template -aged_unordered_container :: -aged_unordered_container (aged_unordered_container&& other, - Allocator const& alloc) - : m_config (std::move (other.m_config), alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + aged_unordered_container&& other, + Allocator const& alloc) + : m_config(std::move(other.m_config), alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (other.cbegin(), other.cend()); - other.clear (); + insert(other.cbegin(), other.cend()); + other.clear(); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock) - : m_config (clock) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock) + : m_config(clock) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - Hash const& hash) - : m_config (clock, hash) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash) + : m_config(clock, hash) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - KeyEqual const& key_eq) - : m_config (clock, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + KeyEqual const& key_eq) + : m_config(clock, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - Allocator const& alloc) - : m_config (clock, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Allocator const& alloc) + : m_config(clock, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq) - : m_config (clock, hash, key_eq) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq) + : m_config(clock, hash, key_eq) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - Hash const& hash, - Allocator const& alloc) - : m_config (clock, hash, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, + Allocator const& alloc) + : m_config(clock, hash, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -aged_unordered_container (std::initializer_list init, - clock_type& clock, - Hash const& hash, - KeyEqual const& key_eq, - Allocator const& alloc) - : m_config (clock, hash, key_eq, alloc) - , m_buck (alloc) - , m_cont (m_buck, - std::cref (m_config.value_hash()), - std::cref (m_config.key_value_equal())) +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + aged_unordered_container( + std::initializer_list init, + clock_type& clock, + Hash const& hash, + KeyEqual const& key_eq, + Allocator const& alloc) + : m_config(clock, hash, key_eq, alloc) + , m_buck(alloc) + , m_cont( + m_buck, + std::cref(m_config.value_hash()), + std::cref(m_config.key_value_equal())) { - insert (init.begin(), init.end()); + insert(init.begin(), init.end()); } -template -aged_unordered_container :: -~aged_unordered_container() +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::~aged_unordered_container() { clear(); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> auto -aged_unordered_container :: -operator= (aged_unordered_container const& other) +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::operator=(aged_unordered_container const& other) -> aged_unordered_container& { if (this != &other) { - size_type const n (other.size()); + size_type const n(other.size()); clear(); m_config = other.m_config; - m_buck = Buckets (m_config.alloc()); - maybe_rehash (n); - insert_unchecked (other.begin(), other.end()); + m_buck = Buckets(m_config.alloc()); + maybe_rehash(n); + insert_unchecked(other.begin(), other.end()); } return *this; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> auto -aged_unordered_container :: -operator= (aged_unordered_container&& other) -> - aged_unordered_container& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::operator=(aged_unordered_container&& other) + -> aged_unordered_container& { - size_type const n (other.size()); + size_type const n(other.size()); clear(); - m_config = std::move (other.m_config); - m_buck = Buckets (m_config.alloc()); - maybe_rehash (n); - insert_unchecked (other.begin(), other.end()); + m_config = std::move(other.m_config); + m_buck = Buckets(m_config.alloc()); + maybe_rehash(n); + insert_unchecked(other.begin(), other.end()); other.clear(); return *this; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> auto -aged_unordered_container :: -operator= (std::initializer_list init) -> - aged_unordered_container& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::operator=(std::initializer_list init) + -> aged_unordered_container& { - clear (); - insert (init); + clear(); + insert(init); return *this; } //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -typename std::conditional ::type& -aged_unordered_container :: -at (K const& k) +typename std::conditional::type& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::at(K const& k) { - auto const iter (m_cont.find (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); + auto const iter(m_cont.find( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); if (iter == m_cont.end()) - throw std::out_of_range ("key not found"); + throw std::out_of_range("key not found"); return iter->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -typename std::conditional ::type const& -aged_unordered_container :: -at (K const& k) const +typename std::conditional::type const& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::at(K const& k) const { - auto const iter (m_cont.find (k, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); + auto const iter(m_cont.find( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); if (iter == m_cont.end()) - throw std::out_of_range ("key not found"); + throw std::out_of_range("key not found"); return iter->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -typename std::conditional ::type& -aged_unordered_container :: -operator[] (Key const& key) +typename std::conditional::type& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::operator[](Key const& key) { - maybe_rehash (1); + maybe_rehash(1); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (key, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + key, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - element* const p (new_element ( + element* const p(new_element( std::piecewise_construct, - std::forward_as_tuple (key), - std::forward_as_tuple ())); - m_cont.insert_commit (*p, d); - chronological.list.push_back (*p); + std::forward_as_tuple(key), + std::forward_as_tuple())); + m_cont.insert_commit(*p, d); + chronological.list.push_back(*p); return p->value.second; } return result.first->value.second; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -typename std::conditional ::type& -aged_unordered_container :: -operator[] (Key&& key) +typename std::conditional::type& +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::operator[](Key&& key) { - maybe_rehash (1); + maybe_rehash(1); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (key, - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + key, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - element* const p (new_element ( + element* const p(new_element( std::piecewise_construct, - std::forward_as_tuple (std::move (key)), - std::forward_as_tuple ())); - m_cont.insert_commit (*p, d); - chronological.list.push_back (*p); + std::forward_as_tuple(std::move(key)), + std::forward_as_tuple())); + m_cont.insert_commit(*p, d); + chronological.list.push_back(*p); return p->value.second; } return result.first->value.second; @@ -2103,267 +2712,420 @@ operator[] (Key&& key) //------------------------------------------------------------------------------ -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> void -aged_unordered_container :: -clear() +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::clear() { - for (auto iter (chronological.list.begin()); - iter != chronological.list.end();) - unlink_and_delete_element (&*iter++); + for (auto iter(chronological.list.begin()); + iter != chronological.list.end();) + unlink_and_delete_element(&*iter++); chronological.list.clear(); m_cont.clear(); m_buck.clear(); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert (value_type const& value) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert(value_type const& value) -> + typename std::enable_if>::type { - maybe_rehash (1); + maybe_rehash(1); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (value), - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + extract(value), + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - element* const p (new_element (value)); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + element* const p(new_element(value)); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - return std::make_pair (iterator (result.first), false); + return std::make_pair(iterator(result.first), false); } // multimap, multiset -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert (value_type const& value) -> - typename std::enable_if ::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert(value_type const& value) -> + typename std::enable_if::type { - maybe_rehash (1); - element* const p (new_element (value)); - chronological.list.push_back (*p); - auto const iter (m_cont.insert (*p)); - return iterator (iter); + maybe_rehash(1); + element* const p(new_element(value)); + chronological.list.push_back(*p); + auto const iter(m_cont.insert(*p)); + return iterator(iter); } // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert (value_type&& value) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert(value_type&& value) -> typename std:: + enable_if>::type { - maybe_rehash (1); + maybe_rehash(1); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (value), - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + extract(value), + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - element* const p (new_element (std::move (value))); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + element* const p(new_element(std::move(value))); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - return std::make_pair (iterator (result.first), false); + return std::make_pair(iterator(result.first), false); } // multimap, multiset -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert (value_type&& value) -> - typename std::enable_if ::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert(value_type&& value) -> + typename std::enable_if::type { - maybe_rehash (1); - element* const p (new_element (std::move (value))); - chronological.list.push_back (*p); - auto const iter (m_cont.insert (*p)); - return iterator (iter); + maybe_rehash(1); + element* const p(new_element(std::move(value))); + chronological.list.push_back(*p); + auto const iter(m_cont.insert(*p)); + return iterator(iter); } -#if 1 // Use insert() instead of insert_check() insert_commit() +#if 1 // Use insert() instead of insert_check() insert_commit() // set, map -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -emplace (Args&&... args) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::emplace(Args&&... args) -> + typename std::enable_if>::type { - maybe_rehash (1); + maybe_rehash(1); // VFALCO NOTE Its unfortunate that we need to // construct element here - element* const p (new_element (std::forward (args)...)); - auto const result (m_cont.insert (*p)); + element* const p(new_element(std::forward(args)...)); + auto const result(m_cont.insert(*p)); if (result.second) { - chronological.list.push_back (*p); - return std::make_pair (iterator (result.first), true); + chronological.list.push_back(*p); + return std::make_pair(iterator(result.first), true); } - delete_element (p); - return std::make_pair (iterator (result.first), false); + delete_element(p); + return std::make_pair(iterator(result.first), false); } -#else // As original, use insert_check() / insert_commit () pair. +#else // As original, use insert_check() / insert_commit () pair. // set, map -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -emplace (Args&&... args) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::emplace(Args&&... args) -> + typename std::enable_if>::type { - maybe_rehash (1); + maybe_rehash(1); // VFALCO NOTE Its unfortunate that we need to // construct element here - element* const p (new_element ( - std::forward (args)...)); + element* const p(new_element(std::forward(args)...)); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (p->value), - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + extract(p->value), + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - delete_element (p); - return std::make_pair (iterator (result.first), false); + delete_element(p); + return std::make_pair(iterator(result.first), false); } -#endif // 0 +#endif // 0 // multiset, multimap -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -emplace (Args&&... args) -> - typename std::enable_if ::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::emplace(Args&&... args) -> + typename std::enable_if::type { - maybe_rehash (1); - element* const p (new_element ( - std::forward (args)...)); - chronological.list.push_back (*p); - auto const iter (m_cont.insert (*p)); - return iterator (iter); + maybe_rehash(1); + element* const p(new_element(std::forward(args)...)); + chronological.list.push_back(*p); + auto const iter(m_cont.insert(*p)); + return iterator(iter); } // set, map -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -emplace_hint (const_iterator /*hint*/, Args&&... args) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::emplace_hint(const_iterator /*hint*/, Args&&... args) -> + typename std::enable_if>::type { - maybe_rehash (1); + maybe_rehash(1); // VFALCO NOTE Its unfortunate that we need to // construct element here - element* const p (new_element ( - std::forward (args)...)); + element* const p(new_element(std::forward(args)...)); typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (p->value), - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + extract(p->value), + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - delete_element (p); - return std::make_pair (iterator (result.first), false); + delete_element(p); + return std::make_pair(iterator(result.first), false); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -beast::detail::aged_container_iterator -aged_unordered_container :: -erase (beast::detail::aged_container_iterator < - is_const, Iterator, Base> pos) +beast::detail::aged_container_iterator +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + erase(beast::detail::aged_container_iterator pos) { unlink_and_delete_element(&*((pos++).iterator())); - return beast::detail::aged_container_iterator < - false, Iterator, Base> (pos.iterator()); + return beast::detail::aged_container_iterator( + pos.iterator()); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template -beast::detail::aged_container_iterator -aged_unordered_container :: -erase (beast::detail::aged_container_iterator < - is_const, Iterator, Base> first, - beast::detail::aged_container_iterator < - is_const, Iterator, Base> last) +beast::detail::aged_container_iterator +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: + erase( + beast::detail::aged_container_iterator first, + beast::detail::aged_container_iterator last) { for (; first != last;) unlink_and_delete_element(&*((first++).iterator())); - return beast::detail::aged_container_iterator < - false, Iterator, Base> (first.iterator()); + return beast::detail::aged_container_iterator( + first.iterator()); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -erase (K const& k) -> - size_type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::erase(K const& k) -> size_type { - auto iter (m_cont.find (k, std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()))); + auto iter(m_cont.find( + k, + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()))); if (iter == m_cont.end()) return 0; - size_type n (0); + size_type n(0); for (;;) { - auto p (&*iter++); - bool const done ( - m_config (*p, extract (iter->value))); - unlink_and_delete_element (p); + auto p(&*iter++); + bool const done(m_config(*p, extract(iter->value))); + unlink_and_delete_element(p); ++n; if (done) break; @@ -2371,40 +3133,72 @@ erase (K const& k) -> return n; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> void -aged_unordered_container :: -swap (aged_unordered_container& other) noexcept +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::swap(aged_unordered_container& other) noexcept { - swap_data (other); - std::swap (chronological, other.chronological); - std::swap (m_cont, other.m_cont); + swap_data(other); + std::swap(chronological, other.chronological); + std::swap(m_cont, other.m_cont); } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -touch (K const& k) -> - size_type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::touch(K const& k) -> size_type { - auto const now (clock().now()); - size_type n (0); - auto const range (equal_range (k)); + auto const now(clock().now()); + size_type n(0); + auto const range(equal_range(k)); for (auto iter : range) { - touch (iter, now); + touch(iter, now); ++n; } return n; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template < bool OtherIsMap, class OtherKey, @@ -2412,30 +3206,48 @@ template < class OtherDuration, class OtherHash, class OtherAllocator, - bool maybe_multi -> -typename std::enable_if ::type -aged_unordered_container < - IsMulti, IsMap, Key, T, Clock, Hash, KeyEqual, Allocator>:: -operator== ( - aged_unordered_container const& other) const + bool maybe_multi> +typename std::enable_if::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: +operator==(aged_unordered_container< + false, + OtherIsMap, + OtherKey, + OtherT, + OtherDuration, + OtherHash, + KeyEqual, + OtherAllocator> const& other) const { if (size() != other.size()) return false; - for (auto iter (cbegin()), last (cend()), olast (other.cend()); - iter != last; ++iter) + for (auto iter(cbegin()), last(cend()), olast(other.cend()); iter != last; + ++iter) { - auto oiter (other.find (extract (*iter))); + auto oiter(other.find(extract(*iter))); if (oiter == olast) return false; } return true; } -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template < bool OtherIsMap, class OtherKey, @@ -2443,32 +3255,42 @@ template < class OtherDuration, class OtherHash, class OtherAllocator, - bool maybe_multi -> -typename std::enable_if ::type -aged_unordered_container < - IsMulti, IsMap, Key, T, Clock, Hash, KeyEqual, Allocator>:: -operator== ( - aged_unordered_container const& other) const + bool maybe_multi> +typename std::enable_if::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>:: +operator==(aged_unordered_container< + true, + OtherIsMap, + OtherKey, + OtherT, + OtherDuration, + OtherHash, + KeyEqual, + OtherAllocator> const& other) const { if (size() != other.size()) return false; - using EqRng = std::pair ; - for (auto iter (cbegin()), last (cend()); iter != last;) + using EqRng = std::pair; + for (auto iter(cbegin()), last(cend()); iter != last;) { - auto const& k (extract (*iter)); - auto const eq (equal_range (k)); - auto const oeq (other.equal_range (k)); + auto const& k(extract(*iter)); + auto const eq(equal_range(k)); + auto const oeq(other.equal_range(k)); #if BEAST_NO_CXX14_IS_PERMUTATION - if (std::distance (eq.first, eq.second) != - std::distance (oeq.first, oeq.second) || - ! std::is_permutation (eq.first, eq.second, oeq.first)) + if (std::distance(eq.first, eq.second) != + std::distance(oeq.first, oeq.second) || + !std::is_permutation(eq.first, eq.second, oeq.first)) return false; #else - if (! std::is_permutation (eq.first, - eq.second, oeq.first, oeq.second)) + if (!std::is_permutation(eq.first, eq.second, oeq.first, oeq.second)) return false; #endif iter = eq.second; @@ -2479,95 +3301,172 @@ operator== ( //------------------------------------------------------------------------------ // map, set -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert_unchecked (value_type const& value) -> - typename std::enable_if >::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert_unchecked(value_type const& value) -> + typename std::enable_if>::type { typename cont_type::insert_commit_data d; - auto const result (m_cont.insert_check (extract (value), - std::cref (m_config.hash_function()), - std::cref (m_config.key_value_equal()), d)); + auto const result(m_cont.insert_check( + extract(value), + std::cref(m_config.hash_function()), + std::cref(m_config.key_value_equal()), + d)); if (result.second) { - element* const p (new_element (value)); - auto const iter (m_cont.insert_commit (*p, d)); - chronological.list.push_back (*p); - return std::make_pair (iterator (iter), true); + element* const p(new_element(value)); + auto const iter(m_cont.insert_commit(*p, d)); + chronological.list.push_back(*p); + return std::make_pair(iterator(iter), true); } - return std::make_pair (iterator (result.first), false); + return std::make_pair(iterator(result.first), false); } // multimap, multiset -template +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> template auto -aged_unordered_container :: -insert_unchecked (value_type const& value) -> - typename std::enable_if ::type +aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>::insert_unchecked(value_type const& value) -> + typename std::enable_if::type { - element* const p (new_element (value)); - chronological.list.push_back (*p); - auto const iter (m_cont.insert (*p)); - return iterator (iter); + element* const p(new_element(value)); + chronological.list.push_back(*p); + auto const iter(m_cont.insert(*p)); + return iterator(iter); } //------------------------------------------------------------------------------ -} +} // namespace detail //------------------------------------------------------------------------------ -template -struct is_aged_container > - : std::true_type +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +struct is_aged_container> : std::true_type { explicit is_aged_container() = default; }; // Free functions -template -void swap ( - beast::detail::aged_unordered_container & lhs, - beast::detail::aged_unordered_container & rhs) noexcept +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator> +void +swap( + beast::detail::aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>& lhs, + beast::detail::aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>& rhs) noexcept { - lhs.swap (rhs); + lhs.swap(rhs); } /** Expire aged container items past the specified age. */ -template -std::size_t expire (beast::detail::aged_unordered_container < - IsMulti, IsMap, Key, T, Clock, Hash, KeyEqual, Allocator>& c, - std::chrono::duration const& age) noexcept +template < + bool IsMulti, + bool IsMap, + class Key, + class T, + class Clock, + class Hash, + class KeyEqual, + class Allocator, + class Rep, + class Period> +std::size_t +expire( + beast::detail::aged_unordered_container< + IsMulti, + IsMap, + Key, + T, + Clock, + Hash, + KeyEqual, + Allocator>& c, + std::chrono::duration const& age) noexcept { - std::size_t n (0); - auto const expired (c.clock().now() - age); - for (auto iter (c.chronological.cbegin()); - iter != c.chronological.cend() && - iter.when() <= expired;) + std::size_t n(0); + auto const expired(c.clock().now() - age); + for (auto iter(c.chronological.cbegin()); + iter != c.chronological.cend() && iter.when() <= expired;) { - iter = c.erase (iter); + iter = c.erase(iter); ++n; } return n; } -} +} // namespace beast #endif diff --git a/src/ripple/beast/container/detail/empty_base_optimization.h b/src/ripple/beast/container/detail/empty_base_optimization.h index 3b1acb99b1..4cdc3a0e16 100644 --- a/src/ripple/beast/container/detail/empty_base_optimization.h +++ b/src/ripple/beast/container/detail/empty_base_optimization.h @@ -17,40 +17,43 @@ namespace beast { namespace detail { -template +template struct is_empty_base_optimization_derived - : std::integral_constant::value && - ! boost::is_final::value> + : std::integral_constant< + bool, + std::is_empty::value && !boost::is_final::value> { }; -template::value> +template < + class T, + int UniqueID = 0, + bool isDerived = is_empty_base_optimization_derived::value> class empty_base_optimization : private T { public: empty_base_optimization() = default; empty_base_optimization(empty_base_optimization&&) = default; empty_base_optimization(empty_base_optimization const&) = default; - empty_base_optimization& operator=(empty_base_optimization&&) = default; - empty_base_optimization& operator=(empty_base_optimization const&) = default; + empty_base_optimization& + operator=(empty_base_optimization&&) = default; + empty_base_optimization& + operator=(empty_base_optimization const&) = default; - template - explicit - empty_base_optimization(Arg1&& arg1, ArgN&&... argn) - : T(std::forward(arg1), - std::forward(argn)...) + template + explicit empty_base_optimization(Arg1&& arg1, ArgN&&... argn) + : T(std::forward(arg1), std::forward(argn)...) { } - T& member() noexcept + T& + member() noexcept { return *this; } - T const& member() const noexcept + T const& + member() const noexcept { return *this; } @@ -58,11 +61,8 @@ public: //------------------------------------------------------------------------------ -template< - class T, - int UniqueID -> -class empty_base_optimization +template +class empty_base_optimization { T t_; @@ -70,29 +70,31 @@ public: empty_base_optimization() = default; empty_base_optimization(empty_base_optimization&&) = default; empty_base_optimization(empty_base_optimization const&) = default; - empty_base_optimization& operator=(empty_base_optimization&&) = default; - empty_base_optimization& operator=(empty_base_optimization const&) = default; + empty_base_optimization& + operator=(empty_base_optimization&&) = default; + empty_base_optimization& + operator=(empty_base_optimization const&) = default; - template - explicit - empty_base_optimization(Arg1&& arg1, ArgN&&... argn) - : t_(std::forward(arg1), - std::forward(argn)...) + template + explicit empty_base_optimization(Arg1&& arg1, ArgN&&... argn) + : t_(std::forward(arg1), std::forward(argn)...) { } - T& member() noexcept + T& + member() noexcept { return t_; } - T const& member() const noexcept + T const& + member() const noexcept { return t_; } }; -} // detail -} // beast +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/core/CurrentThreadName.cpp b/src/ripple/beast/core/CurrentThreadName.cpp index e6afd10549..80d275a1ff 100644 --- a/src/ripple/beast/core/CurrentThreadName.cpp +++ b/src/ripple/beast/core/CurrentThreadName.cpp @@ -27,12 +27,13 @@ //------------------------------------------------------------------------------ #if BOOST_OS_WINDOWS -#include #include +#include namespace beast::detail { -inline void setCurrentThreadNameImpl (std::string_view name) +inline void +setCurrentThreadNameImpl(std::string_view name) { #if DEBUG && BOOST_COMP_MSVC // This technique is documented by Microsoft and works for all versions @@ -40,7 +41,7 @@ inline void setCurrentThreadNameImpl (std::string_view name) // under the Visual Studio debugger. For more details, see: // https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-set-a-thread-name-in-native-code -#pragma pack(push,8) +#pragma pack(push, 8) struct THREADNAME_INFO { DWORD dwType; @@ -58,62 +59,67 @@ inline void setCurrentThreadNameImpl (std::string_view name) ni.dwFlags = 0; #pragma warning(push) -#pragma warning(disable: 6320 6322) +#pragma warning(disable : 6320 6322) __try { - RaiseException (0x406d1388, 0, - sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni); + RaiseException( + 0x406d1388, 0, sizeof(ni) / sizeof(ULONG_PTR), (ULONG_PTR*)&ni); } __except (EXCEPTION_CONTINUE_EXECUTION) - {} + { + } #pragma warning(pop) #endif } -} // beast::detail -#endif // BOOST_OS_WINDOWS +} // namespace beast::detail +#endif // BOOST_OS_WINDOWS #if BOOST_OS_MACOS #include namespace beast::detail { -inline void setCurrentThreadNameImpl (std::string_view name) +inline void +setCurrentThreadNameImpl(std::string_view name) { pthread_setname_np(name.data()); } -} // beast::detail -#endif // BOOST_OS_MACOS +} // namespace beast::detail +#endif // BOOST_OS_MACOS #if BOOST_OS_LINUX #include namespace beast::detail { -inline void setCurrentThreadNameImpl (std::string_view name) +inline void +setCurrentThreadNameImpl(std::string_view name) { pthread_setname_np(pthread_self(), name.data()); } -} // beast::detail -#endif // BOOST_OS_LINUX +} // namespace beast::detail +#endif // BOOST_OS_LINUX namespace beast { namespace detail { thread_local std::string threadName; -} // detail +} // namespace detail -std::string getCurrentThreadName () +std::string +getCurrentThreadName() { return detail::threadName; } -void setCurrentThreadName (std::string_view name) +void +setCurrentThreadName(std::string_view name) { detail::threadName = name; detail::setCurrentThreadNameImpl(name); } -} // beast +} // namespace beast diff --git a/src/ripple/beast/core/CurrentThreadName.h b/src/ripple/beast/core/CurrentThreadName.h index be9bb69285..5adbb21088 100644 --- a/src/ripple/beast/core/CurrentThreadName.h +++ b/src/ripple/beast/core/CurrentThreadName.h @@ -24,15 +24,16 @@ #ifndef BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED #define BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED -#include #include +#include namespace beast { /** Changes the name of the caller thread. Different OSes may place different length or content limits on this name. */ -void setCurrentThreadName (std::string_view newThreadName); +void +setCurrentThreadName(std::string_view newThreadName); /** Returns the name of the caller thread. @@ -42,8 +43,9 @@ void setCurrentThreadName (std::string_view newThreadName); If no name has ever been set, then the empty string is returned. */ -std::string getCurrentThreadName (); +std::string +getCurrentThreadName(); -} +} // namespace beast #endif diff --git a/src/ripple/beast/core/LexicalCast.h b/src/ripple/beast/core/LexicalCast.h index 07ed38d83a..de1a6a396e 100644 --- a/src/ripple/beast/core/LexicalCast.h +++ b/src/ripple/beast/core/LexicalCast.h @@ -40,13 +40,13 @@ namespace detail { #if BOOST_COMP_MSVC #pragma warning(push) -#pragma warning(disable: 4800) -#pragma warning(disable: 4804) +#pragma warning(disable : 4800) +#pragma warning(disable : 4804) #endif template bool -parse_integral (Int& num, FwdIt first, FwdIt last, Accumulator accumulator) +parse_integral(Int& num, FwdIt first, FwdIt last, Accumulator accumulator) { num = 0; @@ -67,19 +67,19 @@ parse_integral (Int& num, FwdIt first, FwdIt last, Accumulator accumulator) template bool -parse_negative_integral (Int& num, FwdIt first, FwdIt last) +parse_negative_integral(Int& num, FwdIt first, FwdIt last) { - Int limit_value = std::numeric_limits ::min() / 10; - Int limit_digit = std::numeric_limits ::min() % 10; + Int limit_value = std::numeric_limits::min() / 10; + Int limit_digit = std::numeric_limits::min() % 10; if (limit_digit < 0) limit_digit = -limit_digit; - return parse_integral (num, first, last, - [limit_value, limit_digit](Int& value, Int digit) - { - assert ((digit >= 0) && (digit <= 9)); - if (value < limit_value || (value == limit_value && digit > limit_digit)) + return parse_integral( + num, first, last, [limit_value, limit_digit](Int& value, Int digit) { + assert((digit >= 0) && (digit <= 9)); + if (value < limit_value || + (value == limit_value && digit > limit_digit)) return false; value = (value * 10) - digit; return true; @@ -88,16 +88,16 @@ parse_negative_integral (Int& num, FwdIt first, FwdIt last) template bool -parse_positive_integral (Int& num, FwdIt first, FwdIt last) +parse_positive_integral(Int& num, FwdIt first, FwdIt last) { - Int limit_value = std::numeric_limits ::max() / 10; - Int limit_digit = std::numeric_limits ::max() % 10; + Int limit_value = std::numeric_limits::max() / 10; + Int limit_digit = std::numeric_limits::max() % 10; - return parse_integral (num, first, last, - [limit_value, limit_digit](Int& value, Int digit) - { - assert ((digit >= 0) && (digit <= 9)); - if (value > limit_value || (value == limit_value && digit > limit_digit)) + return parse_integral( + num, first, last, [limit_value, limit_digit](Int& value, Int digit) { + assert((digit >= 0) && (digit <= 9)); + if (value > limit_value || + (value == limit_value && digit > limit_digit)) return false; value = (value * 10) + digit; return true; @@ -106,31 +106,33 @@ parse_positive_integral (Int& num, FwdIt first, FwdIt last) template bool -parseSigned (IntType& result, FwdIt first, FwdIt last) +parseSigned(IntType& result, FwdIt first, FwdIt last) { - static_assert(std::is_signed::value, + static_assert( + std::is_signed::value, "You may only call parseSigned with a signed integral type."); if (first != last && *first == '-') - return parse_negative_integral (result, first + 1, last); + return parse_negative_integral(result, first + 1, last); if (first != last && *first == '+') - return parse_positive_integral (result, first + 1, last); + return parse_positive_integral(result, first + 1, last); - return parse_positive_integral (result, first, last); + return parse_positive_integral(result, first, last); } template bool -parseUnsigned (UIntType& result, FwdIt first, FwdIt last) +parseUnsigned(UIntType& result, FwdIt first, FwdIt last) { - static_assert(std::is_unsigned::value, + static_assert( + std::is_unsigned::value, "You may only call parseUnsigned with an unsigned integral type."); if (first != last && *first == '+') - return parse_positive_integral (result, first + 1, last); + return parse_positive_integral(result, first + 1, last); - return parse_positive_integral (result, first, last); + return parse_positive_integral(result, first, last); } //------------------------------------------------------------------------------ @@ -141,60 +143,59 @@ struct LexicalCast; // conversion to std::string template -struct LexicalCast +struct LexicalCast { explicit LexicalCast() = default; template - std::enable_if_t ::value, bool> - operator () (std::string& out, Arithmetic in) + std::enable_if_t::value, bool> + operator()(std::string& out, Arithmetic in) { - out = std::to_string (in); + out = std::to_string(in); return true; } template - std::enable_if_t ::value, bool> - operator () (std::string& out, Enumeration in) + std::enable_if_t::value, bool> + operator()(std::string& out, Enumeration in) { - out = std::to_string ( - static_cast > (in)); + out = std::to_string( + static_cast>(in)); return true; } }; // Parse std::string to number template -struct LexicalCast +struct LexicalCast { explicit LexicalCast() = default; - static_assert (std::is_integral ::value, + static_assert( + std::is_integral::value, "beast::LexicalCast can only be used with integral types"); template - std::enable_if_t ::value, bool> - operator () (Integral& out, std::string const& in) const + std::enable_if_t::value, bool> + operator()(Integral& out, std::string const& in) const { - return parseUnsigned (out, in.begin(), in.end()); + return parseUnsigned(out, in.begin(), in.end()); } template - std::enable_if_t ::value, bool> - operator () (Integral& out, std::string const& in) const + std::enable_if_t::value, bool> + operator()(Integral& out, std::string const& in) const { - return parseSigned (out, in.begin(), in.end()); + return parseSigned(out, in.begin(), in.end()); } bool - operator () (bool& out, std::string in) const + operator()(bool& out, std::string in) const { // Convert the input to lowercase - std::transform(in.begin (), in.end (), in.begin (), - [](auto c) - { - return std::tolower(static_cast(c)); - }); + std::transform(in.begin(), in.end(), in.begin(), [](auto c) { + return std::tolower(static_cast(c)); + }); if (in == "1" || in == "true") { @@ -216,26 +217,28 @@ struct LexicalCast // Conversion from null terminated char const* template -struct LexicalCast +struct LexicalCast { explicit LexicalCast() = default; - bool operator() (Out& out, char const* in) const + bool + operator()(Out& out, char const* in) const { - return LexicalCast ()(out, in); + return LexicalCast()(out, in); } }; // Conversion from null terminated char* // The string is not modified. template -struct LexicalCast +struct LexicalCast { explicit LexicalCast() = default; - bool operator() (Out& out, char* in) const + bool + operator()(Out& out, char* in) const { - return LexicalCast ()(out, in); + return LexicalCast()(out, in); } }; @@ -243,7 +246,7 @@ struct LexicalCast #pragma warning(pop) #endif -} // detail +} // namespace detail //------------------------------------------------------------------------------ @@ -259,9 +262,10 @@ struct BadLexicalCast : public std::bad_cast @return `false` if there was a parsing or range error */ template -bool lexicalCastChecked (Out& out, In in) +bool +lexicalCastChecked(Out& out, In in) { - return detail::LexicalCast () (out, in); + return detail::LexicalCast()(out, in); } /** Convert from one type to another, throw on error @@ -271,14 +275,15 @@ bool lexicalCastChecked (Out& out, In in) @return The new type. */ template -Out lexicalCastThrow (In in) +Out +lexicalCastThrow(In in) { Out out; - if (lexicalCastChecked (out, in)) + if (lexicalCastChecked(out, in)) return out; - throw BadLexicalCast (); + throw BadLexicalCast(); } /** Convert from one type to another. @@ -287,16 +292,17 @@ Out lexicalCastThrow (In in) @return The new type. */ template -Out lexicalCast (In in, Out defaultValue = Out ()) +Out +lexicalCast(In in, Out defaultValue = Out()) { Out out; - if (lexicalCastChecked (out, in)) + if (lexicalCastChecked(out, in)) return out; return defaultValue; } -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/core/List.h b/src/ripple/beast/core/List.h index 7c0a128daa..1daf5cda76 100644 --- a/src/ripple/beast/core/List.h +++ b/src/ripple/beast/core/List.h @@ -37,15 +37,15 @@ struct CopyConst { explicit CopyConst() = default; - using type = typename std::remove_const ::type; + using type = typename std::remove_const::type; }; template -struct CopyConst +struct CopyConst { explicit CopyConst() = default; - using type = typename std::remove_const ::type const; + using type = typename std::remove_const::type const; }; /** @} */ @@ -71,87 +71,96 @@ private: //------------------------------------------------------------------------------ template -class ListIterator : public std::iterator < - std::bidirectional_iterator_tag, std::size_t> +class ListIterator + : public std::iterator { public: - using value_type = typename beast::detail::CopyConst < - N, typename N::value_type>::type; + using value_type = + typename beast::detail::CopyConst::type; using pointer = value_type*; using reference = value_type&; using size_type = std::size_t; - ListIterator (N* node = nullptr) noexcept - : m_node (node) + ListIterator(N* node = nullptr) noexcept : m_node(node) { } template - ListIterator (ListIterator const& other) noexcept - : m_node (other.m_node) + ListIterator(ListIterator const& other) noexcept : m_node(other.m_node) { } template - bool operator== (ListIterator const& other) const noexcept + bool + operator==(ListIterator const& other) const noexcept { return m_node == other.m_node; } template - bool operator!= (ListIterator const& other) const noexcept + bool + operator!=(ListIterator const& other) const noexcept { - return ! ((*this) == other); + return !((*this) == other); } - reference operator* () const noexcept + reference + operator*() const noexcept { - return dereference (); + return dereference(); } - pointer operator-> () const noexcept + pointer + operator->() const noexcept { - return &dereference (); + return &dereference(); } - ListIterator& operator++ () noexcept + ListIterator& + operator++() noexcept { - increment (); + increment(); return *this; } - ListIterator operator++ (int) noexcept + ListIterator + operator++(int) noexcept { - ListIterator result (*this); - increment (); + ListIterator result(*this); + increment(); return result; } - ListIterator& operator-- () noexcept + ListIterator& + operator--() noexcept { - decrement (); + decrement(); return *this; } - ListIterator operator-- (int) noexcept + ListIterator + operator--(int) noexcept { - ListIterator result (*this); - decrement (); + ListIterator result(*this); + decrement(); return result; } private: - reference dereference () const noexcept + reference + dereference() const noexcept { - return static_cast (*m_node); + return static_cast(*m_node); } - void increment () noexcept + void + increment() noexcept { m_node = m_node->m_next; } - void decrement () noexcept + void + decrement() noexcept { m_node = m_node->m_prev; } @@ -159,7 +168,7 @@ private: N* m_node; }; -} +} // namespace detail /** Intrusive doubly linked list. @@ -260,8 +269,8 @@ private: @tparam T The base type of element which the list will store pointers to. - @tparam Tag An optional unique type name used to distinguish lists and nodes, - when the object can exist in multiple lists simultaneously. + @tparam Tag An optional unique type name used to distinguish lists and + nodes, when the object can exist in multiple lists simultaneously. @ingroup beast_core intrusive */ @@ -269,40 +278,43 @@ template class List { public: - using Node = typename detail::ListNode ; + using Node = typename detail::ListNode; - using value_type = T; - using pointer = value_type*; - using reference = value_type&; - using const_pointer = value_type const*; + using value_type = T; + using pointer = value_type*; + using reference = value_type&; + using const_pointer = value_type const*; using const_reference = value_type const&; - using size_type = std::size_t; + using size_type = std::size_t; using difference_type = std::ptrdiff_t; - using iterator = detail::ListIterator ; - using const_iterator = detail::ListIterator ; + using iterator = detail::ListIterator; + using const_iterator = detail::ListIterator; /** Create an empty list. */ - List () + List() { - m_head.m_prev = nullptr; // identifies the head - m_tail.m_next = nullptr; // identifies the tail - clear (); + m_head.m_prev = nullptr; // identifies the head + m_tail.m_next = nullptr; // identifies the tail + clear(); } List(List const&) = delete; - List& operator= (List const&) = delete; + List& + operator=(List const&) = delete; /** Determine if the list is empty. @return `true` if the list is empty. */ - bool empty () const noexcept + bool + empty() const noexcept { - return size () == 0; + return size() == 0; } /** Returns the number of elements in the list. */ - size_type size () const noexcept + size_type + size() const noexcept { return m_size; } @@ -311,90 +323,101 @@ public: @invariant The list may not be empty. @return A reference to the first element. */ - reference front () noexcept + reference + front() noexcept { - return element_from (m_head.m_next); + return element_from(m_head.m_next); } /** Obtain a const reference to the first element. @invariant The list may not be empty. @return A const reference to the first element. */ - const_reference front () const noexcept + const_reference + front() const noexcept { - return element_from (m_head.m_next); + return element_from(m_head.m_next); } /** Obtain a reference to the last element. @invariant The list may not be empty. @return A reference to the last element. */ - reference back () noexcept + reference + back() noexcept { - return element_from (m_tail.m_prev); + return element_from(m_tail.m_prev); } /** Obtain a const reference to the last element. @invariant The list may not be empty. @return A const reference to the last element. */ - const_reference back () const noexcept + const_reference + back() const noexcept { - return element_from (m_tail.m_prev); + return element_from(m_tail.m_prev); } /** Obtain an iterator to the beginning of the list. @return An iterator pointing to the beginning of the list. */ - iterator begin () noexcept + iterator + begin() noexcept { - return iterator (m_head.m_next); + return iterator(m_head.m_next); } /** Obtain a const iterator to the beginning of the list. @return A const iterator pointing to the beginning of the list. */ - const_iterator begin () const noexcept + const_iterator + begin() const noexcept { - return const_iterator (m_head.m_next); + return const_iterator(m_head.m_next); } /** Obtain a const iterator to the beginning of the list. @return A const iterator pointing to the beginning of the list. */ - const_iterator cbegin () const noexcept + const_iterator + cbegin() const noexcept { - return const_iterator (m_head.m_next); + return const_iterator(m_head.m_next); } /** Obtain a iterator to the end of the list. @return An iterator pointing to the end of the list. */ - iterator end () noexcept + iterator + end() noexcept { - return iterator (&m_tail); + return iterator(&m_tail); } /** Obtain a const iterator to the end of the list. @return A constiterator pointing to the end of the list. */ - const_iterator end () const noexcept + const_iterator + end() const noexcept { - return const_iterator (&m_tail); + return const_iterator(&m_tail); } /** Obtain a const iterator to the end of the list @return A constiterator pointing to the end of the list. */ - const_iterator cend () const noexcept + const_iterator + cend() const noexcept { - return const_iterator (&m_tail); + return const_iterator(&m_tail); } /** Clear the list. @note This does not free the elements. */ - void clear () noexcept + void + clear() noexcept { m_head.m_next = &m_tail; m_tail.m_prev = &m_head; @@ -407,15 +430,16 @@ public: @param element The element to insert. @return An iterator pointing to the newly inserted element. */ - iterator insert (iterator pos, T& element) noexcept + iterator + insert(iterator pos, T& element) noexcept { - Node* node = static_cast (&element); + Node* node = static_cast(&element); node->m_next = &*pos; node->m_prev = node->m_next->m_prev; node->m_next->m_prev = node; node->m_prev->m_next = node; ++m_size; - return iterator (node); + return iterator(node); } /** Insert another list into this one. @@ -423,9 +447,10 @@ public: @param pos The location to insert after. @param other The list to insert. */ - void insert (iterator pos, List& other) noexcept + void + insert(iterator pos, List& other) noexcept { - if (!other.empty ()) + if (!other.empty()) { Node* before = &*pos; other.m_head.m_next->m_prev = before->m_prev; @@ -433,7 +458,7 @@ public: other.m_tail.m_prev->m_next = before; before->m_prev = other.m_tail.m_prev; m_size += other.m_size; - other.clear (); + other.clear(); } } @@ -442,7 +467,8 @@ public: @param pos An iterator pointing to the element to remove. @return An iterator pointing to the next element after the one removed. */ - iterator erase (iterator pos) noexcept + iterator + erase(iterator pos) noexcept { Node* node = &*pos; ++pos; @@ -456,19 +482,21 @@ public: @invariant The element must not exist in the list. @param element The element to insert. */ - iterator push_front (T& element) noexcept + iterator + push_front(T& element) noexcept { - return insert (begin (), element); + return insert(begin(), element); } /** Remove the element at the beginning of the list. @invariant The list must not be empty. @return A reference to the popped element. */ - T& pop_front () noexcept + T& + pop_front() noexcept { - T& element (front ()); - erase (begin ()); + T& element(front()); + erase(begin()); return element; } @@ -476,47 +504,52 @@ public: @invariant The element must not exist in the list. @param element The element to append. */ - iterator push_back (T& element) noexcept + iterator + push_back(T& element) noexcept { - return insert (end (), element); + return insert(end(), element); } /** Remove the element at the end of the list. @invariant The list must not be empty. @return A reference to the popped element. */ - T& pop_back () noexcept + T& + pop_back() noexcept { - T& element (back ()); - erase (--end ()); + T& element(back()); + erase(--end()); return element; } /** Swap contents with another list. */ - void swap (List& other) noexcept + void + swap(List& other) noexcept { List temp; - temp.append (other); - other.append (*this); - append (temp); + temp.append(other); + other.append(*this); + append(temp); } /** Insert another list at the beginning of this list. The other list is cleared. @param list The other list to insert. */ - iterator prepend (List& list) noexcept + iterator + prepend(List& list) noexcept { - return insert (begin (), list); + return insert(begin(), list); } /** Append another list at the end of this list. The other list is cleared. @param list the other list to append. */ - iterator append (List& list) noexcept + iterator + append(List& list) noexcept { - return insert (end (), list); + return insert(end(), list); } /** Obtain an iterator from an element. @@ -524,9 +557,10 @@ public: @param element The element to obtain an iterator for. @return An iterator to the element. */ - iterator iterator_to (T& element) const noexcept + iterator + iterator_to(T& element) const noexcept { - return iterator (static_cast (&element)); + return iterator(static_cast(&element)); } /** Obtain a const iterator from an element. @@ -534,20 +568,23 @@ public: @param element The element to obtain an iterator for. @return A const iterator to the element. */ - const_iterator const_iterator_to (T const& element) const noexcept + const_iterator + const_iterator_to(T const& element) const noexcept { - return const_iterator (static_cast (&element)); + return const_iterator(static_cast(&element)); } private: - reference element_from (Node* node) noexcept + reference + element_from(Node* node) noexcept { - return *(static_cast (node)); + return *(static_cast(node)); } - const_reference element_from (Node const* node) const noexcept + const_reference + element_from(Node const* node) const noexcept { - return *(static_cast (node)); + return *(static_cast(node)); } private: @@ -556,6 +593,6 @@ private: Node m_tail; }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/core/LockFreeStack.h b/src/ripple/beast/core/LockFreeStack.h index eae0d11908..ff022b96a1 100644 --- a/src/ripple/beast/core/LockFreeStack.h +++ b/src/ripple/beast/core/LockFreeStack.h @@ -29,80 +29,88 @@ namespace beast { //------------------------------------------------------------------------------ template -class LockFreeStackIterator - : public std::iterator < - std::forward_iterator_tag, - typename Container::value_type, - typename Container::difference_type, - typename std::conditional ::type, - typename std::conditional ::type> +class LockFreeStackIterator : public std::iterator< + std::forward_iterator_tag, + typename Container::value_type, + typename Container::difference_type, + typename std::conditional< + IsConst, + typename Container::const_pointer, + typename Container::pointer>::type, + typename std::conditional< + IsConst, + typename Container::const_reference, + typename Container::reference>::type> { protected: using Node = typename Container::Node; - using NodePtr = typename std::conditional < - IsConst, Node const*, Node*>::type; + using NodePtr = + typename std::conditional::type; public: using value_type = typename Container::value_type; - using pointer = typename std::conditional ::type; - using reference = typename std::conditional ::type; - LockFreeStackIterator () - : m_node () + LockFreeStackIterator() : m_node() { } - LockFreeStackIterator (NodePtr node) - : m_node (node) + LockFreeStackIterator(NodePtr node) : m_node(node) { } template - explicit LockFreeStackIterator (LockFreeStackIterator const& other) - : m_node (other.m_node) + explicit LockFreeStackIterator( + LockFreeStackIterator const& other) + : m_node(other.m_node) { } - LockFreeStackIterator& operator= (NodePtr node) + LockFreeStackIterator& + operator=(NodePtr node) { m_node = node; - return static_cast (*this); + return static_cast(*this); } - LockFreeStackIterator& operator++ () + LockFreeStackIterator& + operator++() { - m_node = m_node->m_next.load (); - return static_cast (*this); + m_node = m_node->m_next.load(); + return static_cast(*this); } - LockFreeStackIterator operator++ (int) + LockFreeStackIterator + operator++(int) { - LockFreeStackIterator result (*this); + LockFreeStackIterator result(*this); m_node = m_node->m_next; return result; } - NodePtr node() const + NodePtr + node() const { return m_node; } - reference operator* () const + reference + operator*() const { - return *this->operator-> (); + return *this->operator->(); } - pointer operator-> () const + pointer + operator->() const { - return static_cast (m_node); + return static_cast(m_node); } private: @@ -112,15 +120,19 @@ private: //------------------------------------------------------------------------------ template -bool operator== (LockFreeStackIterator const& lhs, - LockFreeStackIterator const& rhs) +bool +operator==( + LockFreeStackIterator const& lhs, + LockFreeStackIterator const& rhs) { return lhs.node() == rhs.node(); } template -bool operator!= (LockFreeStackIterator const& lhs, - LockFreeStackIterator const& rhs) +bool +operator!=( + LockFreeStackIterator const& lhs, + LockFreeStackIterator const& rhs) { return lhs.node() != rhs.node(); } @@ -146,16 +158,17 @@ public: class Node { public: - Node () - : m_next (nullptr) - { } + Node() : m_next(nullptr) + { + } - explicit Node (Node* next) - : m_next (next) - { } + explicit Node(Node* next) : m_next(next) + { + } Node(Node const&) = delete; - Node& operator= (Node const&) = delete; + Node& + operator=(Node const&) = delete; private: friend class LockFreeStack; @@ -163,35 +176,34 @@ public: template friend class LockFreeStackIterator; - std::atomic m_next; + std::atomic m_next; }; public: - using value_type = Element; - using pointer = Element*; - using reference = Element&; - using const_pointer = Element const*; + using value_type = Element; + using pointer = Element*; + using reference = Element&; + using const_pointer = Element const*; using const_reference = Element const&; - using size_type = std::size_t; + using size_type = std::size_t; using difference_type = std::ptrdiff_t; - using iterator = LockFreeStackIterator < - LockFreeStack , false>; - using const_iterator = LockFreeStackIterator < - LockFreeStack , true>; + using iterator = LockFreeStackIterator, false>; + using const_iterator = + LockFreeStackIterator, true>; - LockFreeStack () - : m_end (nullptr) - , m_head (&m_end) + LockFreeStack() : m_end(nullptr), m_head(&m_end) { } LockFreeStack(LockFreeStack const&) = delete; - LockFreeStack& operator= (LockFreeStack const&) = delete; + LockFreeStack& + operator=(LockFreeStack const&) = delete; /** Returns true if the stack is empty. */ - bool empty() const + bool + empty() const { - return m_head.load () == &m_end; + return m_head.load() == &m_end; } /** Push a node onto the stack. @@ -206,18 +218,20 @@ public: are attempting to push, only one will receive `true`. */ // VFALCO NOTE Fix this, shouldn't it be a reference like intrusive list? - bool push_front (Node* node) + bool + push_front(Node* node) { bool first; - Node* old_head = m_head.load (std::memory_order_relaxed); + Node* old_head = m_head.load(std::memory_order_relaxed); do { first = (old_head == &m_end); node->m_next = old_head; - } - while (!m_head.compare_exchange_strong (old_head, node, - std::memory_order_release, - std::memory_order_relaxed)); + } while (!m_head.compare_exchange_strong( + old_head, + node, + std::memory_order_release, + std::memory_order_relaxed)); return first; } @@ -230,20 +244,22 @@ public: @return The element that was popped, or `nullptr` if the stack was empty. */ - Element* pop_front () + Element* + pop_front() { - Node* node = m_head.load (); + Node* node = m_head.load(); Node* new_head; do { if (node == &m_end) return nullptr; - new_head = node->m_next.load (); - } - while (!m_head.compare_exchange_strong (node, new_head, - std::memory_order_release, - std::memory_order_relaxed)); - return static_cast (node); + new_head = node->m_next.load(); + } while (!m_head.compare_exchange_strong( + node, + new_head, + std::memory_order_release, + std::memory_order_relaxed)); + return static_cast(node); } /** Return a forward iterator to the beginning or end of the stack. @@ -253,42 +269,48 @@ public: Caller is responsible for synchronization. */ /** @{ */ - iterator begin () + iterator + begin() { - return iterator (m_head.load ()); + return iterator(m_head.load()); } - iterator end () + iterator + end() { - return iterator (&m_end); + return iterator(&m_end); } - const_iterator begin () const + const_iterator + begin() const { - return const_iterator (m_head.load ()); + return const_iterator(m_head.load()); } - const_iterator end () const + const_iterator + end() const { - return const_iterator (&m_end); + return const_iterator(&m_end); } - const_iterator cbegin () const + const_iterator + cbegin() const { - return const_iterator (m_head.load ()); + return const_iterator(m_head.load()); } - const_iterator cend () const + const_iterator + cend() const { - return const_iterator (&m_end); + return const_iterator(&m_end); } /** @} */ private: Node m_end; - std::atomic m_head; + std::atomic m_head; }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/core/SemanticVersion.cpp b/src/ripple/beast/core/SemanticVersion.cpp index 6830b027c9..9d127a0951 100644 --- a/src/ripple/beast/core/SemanticVersion.cpp +++ b/src/ripple/beast/core/SemanticVersion.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include #include @@ -26,13 +26,14 @@ namespace beast { -std::string print_identifiers (SemanticVersion::identifier_list const& list) +std::string +print_identifiers(SemanticVersion::identifier_list const& list) { std::string ret; for (auto const& x : list) { - if (!ret.empty ()) + if (!ret.empty()) ret += "."; ret += x; } @@ -40,215 +41,222 @@ std::string print_identifiers (SemanticVersion::identifier_list const& list) return ret; } -bool isNumeric (std::string const& s) +bool +isNumeric(std::string const& s) { int n; // Must be convertible to an integer - if (!lexicalCastChecked (n, s)) + if (!lexicalCastChecked(n, s)) return false; // Must not have leading zeroes - return std::to_string (n) == s; + return std::to_string(n) == s; } -bool chop (std::string const& what, std::string& input) +bool +chop(std::string const& what, std::string& input) { - auto ret = input.find (what); + auto ret = input.find(what); if (ret != 0) return false; - input.erase (0, what.size ()); + input.erase(0, what.size()); return true; } -bool chopUInt (int& value, int limit, std::string& input) +bool +chopUInt(int& value, int limit, std::string& input) { // Must not be empty - if (input.empty ()) + if (input.empty()) return false; - auto left_iter = std::find_if_not (input.begin (), input.end (), - [](std::string::value_type c) - { - return std::isdigit (c, std::locale::classic()); + auto left_iter = std::find_if_not( + input.begin(), input.end(), [](std::string::value_type c) { + return std::isdigit(c, std::locale::classic()); }); - std::string item (input.begin (), left_iter); + std::string item(input.begin(), left_iter); // Must not be empty - if (item.empty ()) + if (item.empty()) return false; int n; // Must be convertible to an integer - if (!lexicalCastChecked (n, item)) + if (!lexicalCastChecked(n, item)) return false; // Must not have leading zeroes - if (std::to_string (n) != item) + if (std::to_string(n) != item) return false; // Must not be out of range if (n < 0 || n > limit) return false; - input.erase (input.begin (), left_iter); + input.erase(input.begin(), left_iter); value = n; return true; } -bool extract_identifier ( - std::string& value, bool allowLeadingZeroes, std::string& input) +bool +extract_identifier( + std::string& value, + bool allowLeadingZeroes, + std::string& input) { // Must not be empty - if (input.empty ()) + if (input.empty()) return false; // Must not have a leading 0 - if (!allowLeadingZeroes && input [0] == '0') + if (!allowLeadingZeroes && input[0] == '0') return false; - auto last = input.find_first_not_of ( + auto last = input.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"); // Must not be empty if (last == 0) return false; - value = input.substr (0, last); - input.erase (0, last); + value = input.substr(0, last); + input.erase(0, last); return true; } -bool extract_identifiers ( +bool +extract_identifiers( SemanticVersion::identifier_list& identifiers, bool allowLeadingZeroes, std::string& input) { - if (input.empty ()) + if (input.empty()) return false; - do { + do + { std::string s; - if (!extract_identifier (s, allowLeadingZeroes, input)) + if (!extract_identifier(s, allowLeadingZeroes, input)) return false; - identifiers.push_back (s); - } while (chop (".", input)); + identifiers.push_back(s); + } while (chop(".", input)); return true; } //------------------------------------------------------------------------------ -SemanticVersion::SemanticVersion () - : majorVersion (0) - , minorVersion (0) - , patchVersion (0) +SemanticVersion::SemanticVersion() + : majorVersion(0), minorVersion(0), patchVersion(0) { } -SemanticVersion::SemanticVersion (std::string const& version) - : SemanticVersion () +SemanticVersion::SemanticVersion(std::string const& version) : SemanticVersion() { - if (!parse (version)) - throw std::invalid_argument ("invalid version string"); + if (!parse(version)) + throw std::invalid_argument("invalid version string"); } -bool SemanticVersion::parse (std::string const& input) +bool +SemanticVersion::parse(std::string const& input) { // May not have leading or trailing whitespace - auto left_iter = std::find_if_not (input.begin (), input.end (), - [](std::string::value_type c) - { - return std::isspace (c, std::locale::classic()); + auto left_iter = std::find_if_not( + input.begin(), input.end(), [](std::string::value_type c) { + return std::isspace(c, std::locale::classic()); }); - auto right_iter = std::find_if_not (input.rbegin (), input.rend (), - [](std::string::value_type c) - { - return std::isspace (c, std::locale::classic()); - }).base (); + auto right_iter = std::find_if_not( + input.rbegin(), + input.rend(), + [](std::string::value_type c) { + return std::isspace(c, std::locale::classic()); + }) + .base(); // Must not be empty! if (left_iter >= right_iter) return false; - std::string version (left_iter, right_iter); + std::string version(left_iter, right_iter); // May not have leading or trailing whitespace if (version != input) return false; // Must have major version number - if (! chopUInt (majorVersion, std::numeric_limits ::max (), version)) + if (!chopUInt(majorVersion, std::numeric_limits::max(), version)) return false; - if (! chop (".", version)) + if (!chop(".", version)) return false; // Must have minor version number - if (! chopUInt (minorVersion, std::numeric_limits ::max (), version)) + if (!chopUInt(minorVersion, std::numeric_limits::max(), version)) return false; - if (! chop (".", version)) + if (!chop(".", version)) return false; // Must have patch version number - if (! chopUInt (patchVersion, std::numeric_limits ::max (), version)) + if (!chopUInt(patchVersion, std::numeric_limits::max(), version)) return false; // May have pre-release identifier list - if (chop ("-", version)) + if (chop("-", version)) { - if (!extract_identifiers (preReleaseIdentifiers, false, version)) + if (!extract_identifiers(preReleaseIdentifiers, false, version)) return false; // Must not be empty - if (preReleaseIdentifiers.empty ()) + if (preReleaseIdentifiers.empty()) return false; } // May have metadata identifier list - if (chop ("+", version)) + if (chop("+", version)) { - if (!extract_identifiers (metaData, true, version)) + if (!extract_identifiers(metaData, true, version)) return false; // Must not be empty - if (metaData.empty ()) + if (metaData.empty()) return false; } - return version.empty (); + return version.empty(); } -std::string SemanticVersion::print () const +std::string +SemanticVersion::print() const { std::string s; - s = std::to_string (majorVersion) + "." + - std::to_string (minorVersion) + "." + - std::to_string (patchVersion); + s = std::to_string(majorVersion) + "." + std::to_string(minorVersion) + + "." + std::to_string(patchVersion); - if (!preReleaseIdentifiers.empty ()) + if (!preReleaseIdentifiers.empty()) { s += "-"; - s += print_identifiers (preReleaseIdentifiers); + s += print_identifiers(preReleaseIdentifiers); } - if (!metaData.empty ()) + if (!metaData.empty()) { s += "+"; - s += print_identifiers (metaData); + s += print_identifiers(metaData); } return s; } -int compare (SemanticVersion const& lhs, SemanticVersion const& rhs) +int +compare(SemanticVersion const& lhs, SemanticVersion const& rhs) { if (lhs.majorVersion > rhs.majorVersion) return 1; @@ -265,38 +273,41 @@ int compare (SemanticVersion const& lhs, SemanticVersion const& rhs) else if (lhs.patchVersion < rhs.patchVersion) return -1; - if (lhs.isPreRelease () || rhs.isPreRelease ()) + if (lhs.isPreRelease() || rhs.isPreRelease()) { // Pre-releases have a lower precedence - if (lhs.isRelease () && rhs.isPreRelease ()) + if (lhs.isRelease() && rhs.isPreRelease()) return 1; - else if (lhs.isPreRelease () && rhs.isRelease ()) + else if (lhs.isPreRelease() && rhs.isRelease()) return -1; // Compare pre-release identifiers - for (int i = 0; i < std::max (lhs.preReleaseIdentifiers.size (), rhs.preReleaseIdentifiers.size ()); ++i) + for (int i = 0; i < + std::max(lhs.preReleaseIdentifiers.size(), + rhs.preReleaseIdentifiers.size()); + ++i) { // A larger list of identifiers has a higher precedence - if (i >= rhs.preReleaseIdentifiers.size ()) + if (i >= rhs.preReleaseIdentifiers.size()) return 1; - else if (i >= lhs.preReleaseIdentifiers.size ()) + else if (i >= lhs.preReleaseIdentifiers.size()) return -1; - std::string const& left (lhs.preReleaseIdentifiers [i]); - std::string const& right (rhs.preReleaseIdentifiers [i]); + std::string const& left(lhs.preReleaseIdentifiers[i]); + std::string const& right(rhs.preReleaseIdentifiers[i]); // Numeric identifiers have lower precedence - if (! isNumeric (left) && isNumeric (right)) + if (!isNumeric(left) && isNumeric(right)) return 1; - else if (isNumeric (left) && ! isNumeric (right)) + else if (isNumeric(left) && !isNumeric(right)) return -1; - if (isNumeric (left)) + if (isNumeric(left)) { - assert(isNumeric (right)); + assert(isNumeric(right)); - int const iLeft (lexicalCastThrow (left)); - int const iRight (lexicalCastThrow (right)); + int const iLeft(lexicalCastThrow(left)); + int const iRight(lexicalCastThrow(right)); if (iLeft > iRight) return 1; @@ -305,9 +316,9 @@ int compare (SemanticVersion const& lhs, SemanticVersion const& rhs) } else { - assert (! isNumeric (right)); + assert(!isNumeric(right)); - int result = left.compare (right); + int result = left.compare(right); if (result != 0) return result; @@ -320,4 +331,4 @@ int compare (SemanticVersion const& lhs, SemanticVersion const& rhs) return 0; } -} // beast +} // namespace beast diff --git a/src/ripple/beast/core/SemanticVersion.h b/src/ripple/beast/core/SemanticVersion.h index 83bdd7248d..5fe216f1ab 100644 --- a/src/ripple/beast/core/SemanticVersion.h +++ b/src/ripple/beast/core/SemanticVersion.h @@ -20,8 +20,8 @@ #ifndef BEAST_MODULE_CORE_DIAGNOSTIC_SEMANTICVERSION_H_INCLUDED #define BEAST_MODULE_CORE_DIAGNOSTIC_SEMANTICVERSION_H_INCLUDED -#include #include +#include namespace beast { @@ -44,70 +44,75 @@ public: identifier_list preReleaseIdentifiers; identifier_list metaData; - SemanticVersion (); + SemanticVersion(); - SemanticVersion (std::string const& version); + SemanticVersion(std::string const& version); /** Parse a semantic version string. The parsing is as strict as possible. @return `true` if the string was parsed. */ - bool parse (std::string const& input); + bool + parse(std::string const& input); /** Produce a string from semantic version components. */ - std::string print () const; + std::string + print() const; - inline bool isRelease () const noexcept + inline bool + isRelease() const noexcept { return preReleaseIdentifiers.empty(); } - inline bool isPreRelease () const noexcept + inline bool + isPreRelease() const noexcept { - return !isRelease (); + return !isRelease(); } }; /** Compare two SemanticVersions against each other. The comparison follows the rules as per the specification. */ -int compare (SemanticVersion const& lhs, SemanticVersion const& rhs); +int +compare(SemanticVersion const& lhs, SemanticVersion const& rhs); inline bool -operator== (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator==(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) == 0; + return compare(lhs, rhs) == 0; } inline bool -operator!= (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator!=(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) != 0; + return compare(lhs, rhs) != 0; } inline bool -operator>= (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator>=(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) >= 0; + return compare(lhs, rhs) >= 0; } inline bool -operator<= (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator<=(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) <= 0; + return compare(lhs, rhs) <= 0; } inline bool -operator> (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator>(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) > 0; + return compare(lhs, rhs) > 0; } inline bool -operator< (SemanticVersion const& lhs, SemanticVersion const& rhs) +operator<(SemanticVersion const& lhs, SemanticVersion const& rhs) { - return compare (lhs, rhs) < 0; + return compare(lhs, rhs) < 0; } -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/detail/mac_facade.h b/src/ripple/beast/crypto/detail/mac_facade.h index a2f6aa5fd8..31c2f8e8c0 100644 --- a/src/ripple/beast/crypto/detail/mac_facade.h +++ b/src/ripple/beast/crypto/detail/mac_facade.h @@ -22,8 +22,8 @@ #include #include -#include #include +#include namespace beast { namespace detail { @@ -36,14 +36,11 @@ private: Context ctx_; public: - static beast::endian const endian = - beast::endian::native; + static beast::endian const endian = beast::endian::native; - static std::size_t const digest_size = - Context::digest_size; + static std::size_t const digest_size = Context::digest_size; - using result_type = - std::array; + using result_type = std::array; mac_facade() noexcept { @@ -52,19 +49,16 @@ public: ~mac_facade() { - erase(std::integral_constant< - bool, Secure>{}); + erase(std::integral_constant{}); } void - operator()(void const* data, - std::size_t size) noexcept + operator()(void const* data, std::size_t size) noexcept { update(ctx_, data, size); } - explicit - operator result_type() noexcept + explicit operator result_type() noexcept { result_type digest; finish(ctx_, &digest[0]); @@ -72,21 +66,17 @@ public: } private: - inline - void - erase (std::false_type) noexcept + inline void erase(std::false_type) noexcept { } - inline - void - erase (std::true_type) noexcept + inline void erase(std::true_type) noexcept { secure_erase(&ctx_, sizeof(ctx_)); } }; -} // detail -} // beast +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/detail/ripemd_context.h b/src/ripple/beast/crypto/detail/ripemd_context.h index 86ff6c585e..8eb7a573dd 100644 --- a/src/ripple/beast/crypto/detail/ripemd_context.h +++ b/src/ripple/beast/crypto/detail/ripemd_context.h @@ -34,10 +34,10 @@ namespace detail { 1996 All Rights Reserved Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is + of this software and associated documentation files (the "Software"), to + deal in the Software without restriction, including without limitation the + rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in @@ -47,9 +47,9 @@ namespace detail { IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. */ // This implementation has been modified from the // original. It has been updated for C++11. @@ -69,81 +69,101 @@ struct ripemd160_context // ROL(x, n) cyclically rotates x over n bits to the left // x must be of an unsigned 32 bits type and 0 <= n < 32. -#define BEAST_RIPEMD_ROL(x, n) (((x) << (n)) | ((x) >> (32-(n)))) +#define BEAST_RIPEMD_ROL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) // the five basic functions F(), G() and H() -#define BEAST_RIPEMD_F(x, y, z) ((x) ^ (y) ^ (z)) -#define BEAST_RIPEMD_G(x, y, z) (((x) & (y)) | (~(x) & (z))) +#define BEAST_RIPEMD_F(x, y, z) ((x) ^ (y) ^ (z)) +#define BEAST_RIPEMD_G(x, y, z) (((x) & (y)) | (~(x) & (z))) #define BEAST_RIPEMD_H(x, y, z) (((x) | ~(y)) ^ (z)) -#define BEAST_RIPEMD_I(x, y, z) (((x) & (z)) | ((y) & ~(z))) -#define BEAST_RIPEMD_J(x, y, z) ((x) ^ ((y) | ~(z))) +#define BEAST_RIPEMD_I(x, y, z) (((x) & (z)) | ((y) & ~(z))) +#define BEAST_RIPEMD_J(x, y, z) ((x) ^ ((y) | ~(z))) // the ten basic operations FF() through III() -#define BEAST_RIPEMD_FF(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_GG(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x5a827999UL; \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_HH(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6ed9eba1UL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_II(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_JJ(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0xa953fd4eUL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_FFF(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_GGG(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x7a6d76e9UL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_HHH(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6d703ef3UL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_III(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x5c4dd124UL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } -#define BEAST_RIPEMD_JJJ(a, b, c, d, e, x, s) { \ - (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0x50a28be6UL;\ - (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ - (c) = BEAST_RIPEMD_ROL((c), 10); } +#define BEAST_RIPEMD_FF(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_GG(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x5a827999UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_HH(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6ed9eba1UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_II(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x8f1bbcdcUL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_JJ(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0xa953fd4eUL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_FFF(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_F((b), (c), (d)) + (x); \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_GGG(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_G((b), (c), (d)) + (x) + 0x7a6d76e9UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_HHH(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_H((b), (c), (d)) + (x) + 0x6d703ef3UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_III(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_I((b), (c), (d)) + (x) + 0x5c4dd124UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } +#define BEAST_RIPEMD_JJJ(a, b, c, d, e, x, s) \ + { \ + (a) += BEAST_RIPEMD_J((b), (c), (d)) + (x) + 0x50a28be6UL; \ + (a) = BEAST_RIPEMD_ROL((a), (s)) + (e); \ + (c) = BEAST_RIPEMD_ROL((c), 10); \ + } template -void ripemd_load (std::array& X, - unsigned char const* p) +void +ripemd_load(std::array& X, unsigned char const* p) { - for(int i = 0; i < 16; ++i) + for (int i = 0; i < 16; ++i) { - X[i] = - ((std::uint32_t) *((p)+3) << 24) | - ((std::uint32_t) *((p)+2) << 16) | - ((std::uint32_t) *((p)+1) << 8) | - ((std::uint32_t) * (p)); + X[i] = ((std::uint32_t) * ((p) + 3) << 24) | + ((std::uint32_t) * ((p) + 2) << 16) | + ((std::uint32_t) * ((p) + 1) << 8) | ((std::uint32_t) * (p)); p += 4; } } template -void ripemd_compress (ripemd160_context& ctx, - std::array& X) noexcept +void +ripemd_compress( + ripemd160_context& ctx, + std::array& X) noexcept { - std::uint32_t aa = ctx.h[0]; - std::uint32_t bb = ctx.h[1]; - std::uint32_t cc = ctx.h[2]; - std::uint32_t dd = ctx.h[3]; - std::uint32_t ee = ctx.h[4]; + std::uint32_t aa = ctx.h[0]; + std::uint32_t bb = ctx.h[1]; + std::uint32_t cc = ctx.h[2]; + std::uint32_t dd = ctx.h[3]; + std::uint32_t ee = ctx.h[4]; std::uint32_t aaa = ctx.h[0]; std::uint32_t bbb = ctx.h[1]; std::uint32_t ccc = ctx.h[2]; @@ -151,187 +171,187 @@ void ripemd_compress (ripemd160_context& ctx, std::uint32_t eee = ctx.h[4]; // round 1 - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[ 0], 11); - BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[ 1], 14); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[ 2], 15); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[ 3], 12); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[ 4], 5); - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[ 5], 8); - BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[ 6], 7); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[ 7], 9); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[ 8], 11); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[ 9], 13); + BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[0], 11); + BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[1], 14); + BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[2], 15); + BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[3], 12); + BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[4], 5); + BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[5], 8); + BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[6], 7); + BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[7], 9); + BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[8], 11); + BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[9], 13); BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[10], 14); BEAST_RIPEMD_FF(ee, aa, bb, cc, dd, X[11], 15); - BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[12], 6); - BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[13], 7); - BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[14], 9); - BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[15], 8); + BEAST_RIPEMD_FF(dd, ee, aa, bb, cc, X[12], 6); + BEAST_RIPEMD_FF(cc, dd, ee, aa, bb, X[13], 7); + BEAST_RIPEMD_FF(bb, cc, dd, ee, aa, X[14], 9); + BEAST_RIPEMD_FF(aa, bb, cc, dd, ee, X[15], 8); // round 2 - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[ 7], 7); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[ 4], 6); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[13], 8); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[ 1], 13); + BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[7], 7); + BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[4], 6); + BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[13], 8); + BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[1], 13); BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[10], 11); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[ 6], 9); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[15], 7); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[ 3], 15); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[12], 7); - BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[ 0], 12); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[ 9], 15); - BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[ 5], 9); - BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[ 2], 11); - BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[14], 7); + BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[6], 9); + BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[15], 7); + BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[3], 15); + BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[12], 7); + BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[0], 12); + BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[9], 15); + BEAST_RIPEMD_GG(dd, ee, aa, bb, cc, X[5], 9); + BEAST_RIPEMD_GG(cc, dd, ee, aa, bb, X[2], 11); + BEAST_RIPEMD_GG(bb, cc, dd, ee, aa, X[14], 7); BEAST_RIPEMD_GG(aa, bb, cc, dd, ee, X[11], 13); - BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[ 8], 12); + BEAST_RIPEMD_GG(ee, aa, bb, cc, dd, X[8], 12); // round 3 - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[ 3], 11); + BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[3], 11); BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[10], 13); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[14], 6); - BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[ 4], 7); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[ 9], 14); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[15], 9); - BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[ 8], 13); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[ 1], 15); - BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[ 2], 14); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[ 7], 8); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[ 0], 13); - BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[ 6], 6); - BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[13], 5); + BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[14], 6); + BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[4], 7); + BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[9], 14); + BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[15], 9); + BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[8], 13); + BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[1], 15); + BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[2], 14); + BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[7], 8); + BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[0], 13); + BEAST_RIPEMD_HH(cc, dd, ee, aa, bb, X[6], 6); + BEAST_RIPEMD_HH(bb, cc, dd, ee, aa, X[13], 5); BEAST_RIPEMD_HH(aa, bb, cc, dd, ee, X[11], 12); - BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[ 5], 7); - BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[12], 5); + BEAST_RIPEMD_HH(ee, aa, bb, cc, dd, X[5], 7); + BEAST_RIPEMD_HH(dd, ee, aa, bb, cc, X[12], 5); // round 4 - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[ 1], 11); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[ 9], 12); + BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[1], 11); + BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[9], 12); BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[11], 14); BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[10], 15); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[ 0], 14); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[ 8], 15); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[12], 9); - BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[ 4], 8); - BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[13], 9); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[ 3], 14); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[ 7], 5); - BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[15], 6); - BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[14], 8); - BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[ 5], 6); - BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[ 6], 5); - BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[ 2], 12); + BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[0], 14); + BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[8], 15); + BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[12], 9); + BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[4], 8); + BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[13], 9); + BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[3], 14); + BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[7], 5); + BEAST_RIPEMD_II(bb, cc, dd, ee, aa, X[15], 6); + BEAST_RIPEMD_II(aa, bb, cc, dd, ee, X[14], 8); + BEAST_RIPEMD_II(ee, aa, bb, cc, dd, X[5], 6); + BEAST_RIPEMD_II(dd, ee, aa, bb, cc, X[6], 5); + BEAST_RIPEMD_II(cc, dd, ee, aa, bb, X[2], 12); // round 5 - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[ 4], 9); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[ 0], 15); - BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[ 5], 5); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[ 9], 11); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[ 7], 6); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[12], 8); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[ 2], 13); + BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[4], 9); + BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[0], 15); + BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[5], 5); + BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[9], 11); + BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[7], 6); + BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[12], 8); + BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[2], 13); BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[10], 12); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[14], 5); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[ 1], 12); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[ 3], 13); - BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[ 8], 14); + BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[14], 5); + BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[1], 12); + BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[3], 13); + BEAST_RIPEMD_JJ(aa, bb, cc, dd, ee, X[8], 14); BEAST_RIPEMD_JJ(ee, aa, bb, cc, dd, X[11], 11); - BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[ 6], 8); - BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[15], 5); - BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[13], 6); + BEAST_RIPEMD_JJ(dd, ee, aa, bb, cc, X[6], 8); + BEAST_RIPEMD_JJ(cc, dd, ee, aa, bb, X[15], 5); + BEAST_RIPEMD_JJ(bb, cc, dd, ee, aa, X[13], 6); // parallel round 1 - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[ 5], 8); - BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[ 7], 9); - BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[ 0], 11); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[ 9], 13); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[ 2], 15); + BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[5], 8); + BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[14], 9); + BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[7], 9); + BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[0], 11); + BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[9], 13); + BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[2], 15); BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[11], 15); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[ 4], 5); - BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[ 6], 7); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); - BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[ 8], 11); - BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[ 1], 14); + BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[4], 5); + BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[13], 7); + BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[6], 7); + BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[15], 8); + BEAST_RIPEMD_JJJ(eee, aaa, bbb, ccc, ddd, X[8], 11); + BEAST_RIPEMD_JJJ(ddd, eee, aaa, bbb, ccc, X[1], 14); BEAST_RIPEMD_JJJ(ccc, ddd, eee, aaa, bbb, X[10], 14); - BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[ 3], 12); - BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); + BEAST_RIPEMD_JJJ(bbb, ccc, ddd, eee, aaa, X[3], 12); + BEAST_RIPEMD_JJJ(aaa, bbb, ccc, ddd, eee, X[12], 6); // parallel round 2 - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[ 6], 9); + BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[6], 9); BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[11], 13); - BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[ 3], 15); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[ 7], 7); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[ 0], 12); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[13], 8); - BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[ 5], 9); + BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[3], 15); + BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[7], 7); + BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[0], 12); + BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[13], 8); + BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[5], 9); BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[10], 11); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[14], 7); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[15], 7); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[ 8], 12); - BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[12], 7); - BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[ 4], 6); - BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[ 9], 15); - BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[ 1], 13); - BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[ 2], 11); + BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[14], 7); + BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[15], 7); + BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[8], 12); + BEAST_RIPEMD_III(ddd, eee, aaa, bbb, ccc, X[12], 7); + BEAST_RIPEMD_III(ccc, ddd, eee, aaa, bbb, X[4], 6); + BEAST_RIPEMD_III(bbb, ccc, ddd, eee, aaa, X[9], 15); + BEAST_RIPEMD_III(aaa, bbb, ccc, ddd, eee, X[1], 13); + BEAST_RIPEMD_III(eee, aaa, bbb, ccc, ddd, X[2], 11); // parallel round 3 - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[ 5], 7); - BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[ 1], 15); - BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[ 3], 11); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[ 7], 8); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[ 6], 6); - BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[ 9], 14); + BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[15], 9); + BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[5], 7); + BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[1], 15); + BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[3], 11); + BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[7], 8); + BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[14], 6); + BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[6], 6); + BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[9], 14); BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[11], 12); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[ 8], 13); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); - BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[ 2], 14); + BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[8], 13); + BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[12], 5); + BEAST_RIPEMD_HHH(ccc, ddd, eee, aaa, bbb, X[2], 14); BEAST_RIPEMD_HHH(bbb, ccc, ddd, eee, aaa, X[10], 13); - BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[ 0], 13); - BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[ 4], 7); - BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); + BEAST_RIPEMD_HHH(aaa, bbb, ccc, ddd, eee, X[0], 13); + BEAST_RIPEMD_HHH(eee, aaa, bbb, ccc, ddd, X[4], 7); + BEAST_RIPEMD_HHH(ddd, eee, aaa, bbb, ccc, X[13], 5); // parallel round 4 - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[ 8], 15); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[ 6], 5); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[ 4], 8); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[ 1], 11); - BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[ 3], 14); + BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[8], 15); + BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[6], 5); + BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[4], 8); + BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[1], 11); + BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[3], 14); BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[11], 14); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[ 0], 14); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[ 5], 6); - BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[ 2], 12); - BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); - BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[ 9], 12); - BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[ 7], 5); + BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[15], 6); + BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[0], 14); + BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[5], 6); + BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[12], 9); + BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[2], 12); + BEAST_RIPEMD_GGG(bbb, ccc, ddd, eee, aaa, X[13], 9); + BEAST_RIPEMD_GGG(aaa, bbb, ccc, ddd, eee, X[9], 12); + BEAST_RIPEMD_GGG(eee, aaa, bbb, ccc, ddd, X[7], 5); BEAST_RIPEMD_GGG(ddd, eee, aaa, bbb, ccc, X[10], 15); - BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); + BEAST_RIPEMD_GGG(ccc, ddd, eee, aaa, bbb, X[14], 8); // parallel round 5 - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[12] , 8); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[15] , 5); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[10] , 12); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[ 4] , 9); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[ 1] , 12); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[ 5] , 5); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[ 8] , 14); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[ 7] , 6); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[ 6] , 8); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[ 2] , 13); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[13] , 6); - BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[14] , 5); - BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[ 0] , 15); - BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[ 3] , 13); - BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[ 9] , 11); - BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[11] , 11); + BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[12], 8); + BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[15], 5); + BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[10], 12); + BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[4], 9); + BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[1], 12); + BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[5], 5); + BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[8], 14); + BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[7], 6); + BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[6], 8); + BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[2], 13); + BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[13], 6); + BEAST_RIPEMD_FFF(aaa, bbb, ccc, ddd, eee, X[14], 5); + BEAST_RIPEMD_FFF(eee, aaa, bbb, ccc, ddd, X[0], 15); + BEAST_RIPEMD_FFF(ddd, eee, aaa, bbb, ccc, X[3], 13); + BEAST_RIPEMD_FFF(ccc, ddd, eee, aaa, bbb, X[9], 11); + BEAST_RIPEMD_FFF(bbb, ccc, ddd, eee, aaa, X[11], 11); // combine results - ddd += cc + ctx.h[1]; // final result for h[0] + ddd += cc + ctx.h[1]; // final result for h[0] ctx.h[1] = ctx.h[2] + dd + eee; ctx.h[2] = ctx.h[3] + ee + aaa; ctx.h[3] = ctx.h[4] + aa + bbb; @@ -340,7 +360,8 @@ void ripemd_compress (ripemd160_context& ctx, } template -void init (ripemd160_context& ctx) noexcept +void +init(ripemd160_context& ctx) noexcept { ctx.len = 0; ctx.tot_len = 0; @@ -352,18 +373,18 @@ void init (ripemd160_context& ctx) noexcept } template -void update (ripemd160_context& ctx, - void const* message, std::size_t size) noexcept +void +update(ripemd160_context& ctx, void const* message, std::size_t size) noexcept { - auto const pm = reinterpret_cast< - unsigned char const*>(message); + auto const pm = reinterpret_cast(message); unsigned int block_nb; unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; + const unsigned char* shifted_message; tmp_len = ripemd160_context::block_size - ctx.len; rem_len = size < tmp_len ? size : tmp_len; std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < ripemd160_context::block_size) { + if (ctx.len + size < ripemd160_context::block_size) + { ctx.len += size; return; } @@ -375,22 +396,21 @@ void update (ripemd160_context& ctx, ripemd_compress(ctx, X); for (int i = 0; i < block_nb; ++i) { - ripemd_load(X, shifted_message + - i * ripemd160_context::block_size); + ripemd_load(X, shifted_message + i * ripemd160_context::block_size); ripemd_compress(ctx, X); } rem_len = new_len % ripemd160_context::block_size; - std::memcpy(ctx.block, &shifted_message[ - block_nb * ripemd160_context::block_size], - rem_len); + std::memcpy( + ctx.block, + &shifted_message[block_nb * ripemd160_context::block_size], + rem_len); ctx.len = rem_len; - ctx.tot_len += (block_nb + 1) * - ripemd160_context::block_size; + ctx.tot_len += (block_nb + 1) * ripemd160_context::block_size; } template -void finish (ripemd160_context& ctx, - void* digest) noexcept +void +finish(ripemd160_context& ctx, void* digest) noexcept { std::array X; X.fill(0); @@ -398,11 +418,10 @@ void finish (ripemd160_context& ctx, auto p = &ctx.block[0]; // uint8_t i goes into word X[i div 4] at pos. 8*(i mod 4) for (int i = 0; i < ctx.len; ++i) - X[i >> 2] ^= (std::uint32_t) *p++ << (8 * (i & 3)); + X[i >> 2] ^= (std::uint32_t)*p++ << (8 * (i & 3)); ctx.tot_len += ctx.len; // append the bit m_n == 1 - X[(ctx.tot_len>>2)&15] ^= - (uint32_t)1 << (8*(ctx.tot_len&3) + 7); + X[(ctx.tot_len >> 2) & 15] ^= (uint32_t)1 << (8 * (ctx.tot_len & 3) + 7); // length goes to next block? if ((ctx.tot_len & 63) > 55) { @@ -414,18 +433,17 @@ void finish (ripemd160_context& ctx, X[15] = (ctx.tot_len >> 29) | (0 << 3); ripemd_compress(ctx, X); - std::uint8_t* pd = reinterpret_cast< - std::uint8_t*>(digest); + std::uint8_t* pd = reinterpret_cast(digest); for (std::uint32_t i = 0; i < 20; i += 4) { - pd[i] = (std::uint8_t)(ctx.h[i>>2]); // implicit cast to uint8_t - pd[i+1] = (std::uint8_t)(ctx.h[i>>2] >> 8); // extracts the 8 least - pd[i+2] = (std::uint8_t)(ctx.h[i>>2] >> 16); // significant bits. - pd[i+3] = (std::uint8_t)(ctx.h[i>>2] >> 24); + pd[i] = (std::uint8_t)(ctx.h[i >> 2]); // implicit cast to uint8_t + pd[i + 1] = (std::uint8_t)(ctx.h[i >> 2] >> 8); // extracts the 8 least + pd[i + 2] = (std::uint8_t)(ctx.h[i >> 2] >> 16); // significant bits. + pd[i + 3] = (std::uint8_t)(ctx.h[i >> 2] >> 24); } } -} // detail -} // beast +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/detail/sha2_context.h b/src/ripple/beast/crypto/detail/sha2_context.h index 2e5948c38c..6fc8018fc6 100644 --- a/src/ripple/beast/crypto/detail/sha2_context.h +++ b/src/ripple/beast/crypto/detail/sha2_context.h @@ -93,107 +93,107 @@ struct sha512_context std::uint64_t h[8]; }; -#define BEAST_SHA2_SHFR(x, n) (x >> n) -#define BEAST_SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) -#define BEAST_SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) -#define BEAST_SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) +#define BEAST_SHA2_SHFR(x, n) (x >> n) +#define BEAST_SHA2_ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) +#define BEAST_SHA2_ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) +#define BEAST_SHA2_CH(x, y, z) ((x & y) ^ (~x & z)) #define BEAST_SHA2_MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) -#define BEAST_SHA256_F1(x) (BEAST_SHA2_ROTR(x, 2) ^ BEAST_SHA2_ROTR(x, 13) ^ BEAST_SHA2_ROTR(x, 22)) -#define BEAST_SHA256_F2(x) (BEAST_SHA2_ROTR(x, 6) ^ BEAST_SHA2_ROTR(x, 11) ^ BEAST_SHA2_ROTR(x, 25)) -#define BEAST_SHA256_F3(x) (BEAST_SHA2_ROTR(x, 7) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_SHFR(x, 3)) -#define BEAST_SHA256_F4(x) (BEAST_SHA2_ROTR(x, 17) ^ BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_SHFR(x, 10)) -#define BEAST_SHA512_F1(x) (BEAST_SHA2_ROTR(x, 28) ^ BEAST_SHA2_ROTR(x, 34) ^ BEAST_SHA2_ROTR(x, 39)) -#define BEAST_SHA512_F2(x) (BEAST_SHA2_ROTR(x, 14) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_ROTR(x, 41)) -#define BEAST_SHA512_F3(x) (BEAST_SHA2_ROTR(x, 1) ^ BEAST_SHA2_ROTR(x, 8) ^ BEAST_SHA2_SHFR(x, 7)) -#define BEAST_SHA512_F4(x) (BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_ROTR(x, 61) ^ BEAST_SHA2_SHFR(x, 6)) -#define BEAST_SHA2_PACK32(str, x) \ -{ \ - *(x) = \ - ((std::uint32_t) *((str) + 3) ) \ - | ((std::uint32_t) *((str) + 2) << 8) \ - | ((std::uint32_t) *((str) + 1) << 16) \ - | ((std::uint32_t) *((str) + 0) << 24); \ -} -#define BEAST_SHA2_UNPACK32(x, str) \ -{ \ - *((str) + 3) = (std::uint8_t) ((x) ); \ - *((str) + 2) = (std::uint8_t) ((x) >> 8); \ - *((str) + 1) = (std::uint8_t) ((x) >> 16); \ - *((str) + 0) = (std::uint8_t) ((x) >> 24); \ -} -#define BEAST_SHA2_PACK64(str, x) \ -{ \ - *(x) = \ - ((std::uint64_t) *((str) + 7) ) \ - | ((std::uint64_t) *((str) + 6) << 8) \ - | ((std::uint64_t) *((str) + 5) << 16) \ - | ((std::uint64_t) *((str) + 4) << 24) \ - | ((std::uint64_t) *((str) + 3) << 32) \ - | ((std::uint64_t) *((str) + 2) << 40) \ - | ((std::uint64_t) *((str) + 1) << 48) \ - | ((std::uint64_t) *((str) + 0) << 56); \ -} -#define BEAST_SHA2_UNPACK64(x, str) \ -{ \ - *((str) + 7) = (std::uint8_t) ((x) ); \ - *((str) + 6) = (std::uint8_t) ((x) >> 8); \ - *((str) + 5) = (std::uint8_t) ((x) >> 16); \ - *((str) + 4) = (std::uint8_t) ((x) >> 24); \ - *((str) + 3) = (std::uint8_t) ((x) >> 32); \ - *((str) + 2) = (std::uint8_t) ((x) >> 40); \ - *((str) + 1) = (std::uint8_t) ((x) >> 48); \ - *((str) + 0) = (std::uint8_t) ((x) >> 56); \ -} +#define BEAST_SHA256_F1(x) \ + (BEAST_SHA2_ROTR(x, 2) ^ BEAST_SHA2_ROTR(x, 13) ^ BEAST_SHA2_ROTR(x, 22)) +#define BEAST_SHA256_F2(x) \ + (BEAST_SHA2_ROTR(x, 6) ^ BEAST_SHA2_ROTR(x, 11) ^ BEAST_SHA2_ROTR(x, 25)) +#define BEAST_SHA256_F3(x) \ + (BEAST_SHA2_ROTR(x, 7) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_SHFR(x, 3)) +#define BEAST_SHA256_F4(x) \ + (BEAST_SHA2_ROTR(x, 17) ^ BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_SHFR(x, 10)) +#define BEAST_SHA512_F1(x) \ + (BEAST_SHA2_ROTR(x, 28) ^ BEAST_SHA2_ROTR(x, 34) ^ BEAST_SHA2_ROTR(x, 39)) +#define BEAST_SHA512_F2(x) \ + (BEAST_SHA2_ROTR(x, 14) ^ BEAST_SHA2_ROTR(x, 18) ^ BEAST_SHA2_ROTR(x, 41)) +#define BEAST_SHA512_F3(x) \ + (BEAST_SHA2_ROTR(x, 1) ^ BEAST_SHA2_ROTR(x, 8) ^ BEAST_SHA2_SHFR(x, 7)) +#define BEAST_SHA512_F4(x) \ + (BEAST_SHA2_ROTR(x, 19) ^ BEAST_SHA2_ROTR(x, 61) ^ BEAST_SHA2_SHFR(x, 6)) +#define BEAST_SHA2_PACK32(str, x) \ + { \ + *(x) = ((std::uint32_t) * ((str) + 3)) | \ + ((std::uint32_t) * ((str) + 2) << 8) | \ + ((std::uint32_t) * ((str) + 1) << 16) | \ + ((std::uint32_t) * ((str) + 0) << 24); \ + } +#define BEAST_SHA2_UNPACK32(x, str) \ + { \ + *((str) + 3) = (std::uint8_t)((x)); \ + *((str) + 2) = (std::uint8_t)((x) >> 8); \ + *((str) + 1) = (std::uint8_t)((x) >> 16); \ + *((str) + 0) = (std::uint8_t)((x) >> 24); \ + } +#define BEAST_SHA2_PACK64(str, x) \ + { \ + *(x) = ((std::uint64_t) * ((str) + 7)) | \ + ((std::uint64_t) * ((str) + 6) << 8) | \ + ((std::uint64_t) * ((str) + 5) << 16) | \ + ((std::uint64_t) * ((str) + 4) << 24) | \ + ((std::uint64_t) * ((str) + 3) << 32) | \ + ((std::uint64_t) * ((str) + 2) << 40) | \ + ((std::uint64_t) * ((str) + 1) << 48) | \ + ((std::uint64_t) * ((str) + 0) << 56); \ + } +#define BEAST_SHA2_UNPACK64(x, str) \ + { \ + *((str) + 7) = (std::uint8_t)((x)); \ + *((str) + 6) = (std::uint8_t)((x) >> 8); \ + *((str) + 5) = (std::uint8_t)((x) >> 16); \ + *((str) + 4) = (std::uint8_t)((x) >> 24); \ + *((str) + 3) = (std::uint8_t)((x) >> 32); \ + *((str) + 2) = (std::uint8_t)((x) >> 40); \ + *((str) + 1) = (std::uint8_t)((x) >> 48); \ + *((str) + 0) = (std::uint8_t)((x) >> 56); \ + } //------------------------------------------------------------------------------ // SHA256 template -void sha256_transform (sha256_context& ctx, +void +sha256_transform( + sha256_context& ctx, unsigned char const* message, - unsigned int block_nb) noexcept + unsigned int block_nb) noexcept { static unsigned long long const K[64] = { - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, - 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, - 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, - 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, - 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, - 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, - 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - }; + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, + 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, + 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, + 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, + 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, + 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, + 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; std::uint32_t w[64]; std::uint32_t wv[8]; std::uint32_t t1, t2; unsigned char const* sub_block; int i, j; - for (i = 0; i < (int) block_nb; i++) { + for (i = 0; i < (int)block_nb; i++) + { sub_block = message + (i << 6); for (j = 0; j < 16; j++) BEAST_SHA2_PACK32(&sub_block[j << 2], &w[j]); for (j = 16; j < 64; j++) - w[j] = BEAST_SHA256_F4( - w[j - 2]) + w[j - 7] + - BEAST_SHA256_F3(w[j - 15]) + - w[j - 16]; + w[j] = BEAST_SHA256_F4(w[j - 2]) + w[j - 7] + + BEAST_SHA256_F3(w[j - 15]) + w[j - 16]; for (j = 0; j < 8; j++) wv[j] = ctx.h[j]; - for (j = 0; j < 64; j++) { + for (j = 0; j < 64; j++) + { t1 = wv[7] + BEAST_SHA256_F2(wv[4]) + - BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + - K[j] + w[j]; - t2 = BEAST_SHA256_F1(wv[0]) + - BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); + BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + K[j] + w[j]; + t2 = BEAST_SHA256_F1(wv[0]) + BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; @@ -209,7 +209,8 @@ void sha256_transform (sha256_context& ctx, } template -void init (sha256_context& ctx) noexcept +void +init(sha256_context& ctx) noexcept { ctx.len = 0; ctx.tot_len = 0; @@ -224,18 +225,18 @@ void init (sha256_context& ctx) noexcept } template -void update (sha256_context& ctx, - void const* message, std::size_t size) noexcept +void +update(sha256_context& ctx, void const* message, std::size_t size) noexcept { - auto const pm = reinterpret_cast< - unsigned char const*>(message); + auto const pm = reinterpret_cast(message); unsigned int block_nb; unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; + const unsigned char* shifted_message; tmp_len = sha256_context::block_size - ctx.len; rem_len = size < tmp_len ? size : tmp_len; std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < sha256_context::block_size) { + if (ctx.len + size < sha256_context::block_size) + { ctx.len += size; return; } @@ -245,31 +246,31 @@ void update (sha256_context& ctx, sha256_transform(ctx, ctx.block, 1); sha256_transform(ctx, shifted_message, block_nb); rem_len = new_len % sha256_context::block_size; - std::memcpy(ctx.block, &shifted_message[ - block_nb << 6], rem_len); + std::memcpy(ctx.block, &shifted_message[block_nb << 6], rem_len); ctx.len = rem_len; ctx.tot_len += (block_nb + 1) << 6; } template -void finish (sha256_context& ctx, - void* digest) noexcept +void +finish(sha256_context& ctx, void* digest) noexcept { - auto const pd = reinterpret_cast< - unsigned char*>(digest); + auto const pd = reinterpret_cast(digest); unsigned int block_nb; unsigned int pm_len; unsigned int len_b; int i; - block_nb = (1 + ((sha256_context::block_size - 9) < - (ctx.len % sha256_context::block_size))); + block_nb = + (1 + + ((sha256_context::block_size - 9) < + (ctx.len % sha256_context::block_size))); len_b = (ctx.tot_len + ctx.len) << 3; pm_len = block_nb << 6; std::memset(ctx.block + ctx.len, 0, pm_len - ctx.len); ctx.block[ctx.len] = 0x80; BEAST_SHA2_UNPACK32(len_b, ctx.block + pm_len - 4); sha256_transform(ctx, ctx.block, block_nb); - for (i = 0 ; i < 8; i++) + for (i = 0; i < 8; i++) BEAST_SHA2_UNPACK32(ctx.h[i], &pd[i << 2]); } @@ -278,50 +279,39 @@ void finish (sha256_context& ctx, // SHA512 template -void sha512_transform (sha512_context& ctx, +void +sha512_transform( + sha512_context& ctx, unsigned char const* message, - unsigned int block_nb) noexcept + unsigned int block_nb) noexcept { static unsigned long long const K[80] = { - 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, - 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, - 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, - 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, - 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, - 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, - 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, - 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, - 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, - 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, - 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, - 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, - 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, - 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, - 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, - 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, - 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, - 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, - 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, - 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, + 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, + 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, + 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, + 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, + 0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, + 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, + 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, + 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, + 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, + 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, + 0x92722c851482353bULL, 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, + 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, + 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, + 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, + 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, + 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, + 0xc67178f2e372532bULL, 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, + 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, + 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, + 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; std::uint64_t w[80]; @@ -329,24 +319,21 @@ void sha512_transform (sha512_context& ctx, std::uint64_t t1, t2; unsigned char const* sub_block; int i, j; - for (i = 0; i < (int) block_nb; i++) + for (i = 0; i < (int)block_nb; i++) { sub_block = message + (i << 7); for (j = 0; j < 16; j++) BEAST_SHA2_PACK64(&sub_block[j << 3], &w[j]); for (j = 16; j < 80; j++) - w[j] = BEAST_SHA512_F4( - w[j - 2]) + w[j - 7] + - BEAST_SHA512_F3(w[j - 15]) + - w[j - 16]; + w[j] = BEAST_SHA512_F4(w[j - 2]) + w[j - 7] + + BEAST_SHA512_F3(w[j - 15]) + w[j - 16]; for (j = 0; j < 8; j++) wv[j] = ctx.h[j]; - for (j = 0; j < 80; j++) { + for (j = 0; j < 80; j++) + { t1 = wv[7] + BEAST_SHA512_F2(wv[4]) + - BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + - K[j] + w[j]; - t2 = BEAST_SHA512_F1(wv[0]) + - BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); + BEAST_SHA2_CH(wv[4], wv[5], wv[6]) + K[j] + w[j]; + t2 = BEAST_SHA512_F1(wv[0]) + BEAST_SHA2_MAJ(wv[0], wv[1], wv[2]); wv[7] = wv[6]; wv[6] = wv[5]; wv[5] = wv[4]; @@ -362,7 +349,8 @@ void sha512_transform (sha512_context& ctx, } template -void init (sha512_context& ctx) noexcept +void +init(sha512_context& ctx) noexcept { ctx.len = 0; ctx.tot_len = 0; @@ -377,18 +365,18 @@ void init (sha512_context& ctx) noexcept } template -void update (sha512_context& ctx, - void const* message, std::size_t size) noexcept +void +update(sha512_context& ctx, void const* message, std::size_t size) noexcept { - auto const pm = reinterpret_cast< - unsigned char const*>(message); + auto const pm = reinterpret_cast(message); unsigned int block_nb; unsigned int new_len, rem_len, tmp_len; - const unsigned char *shifted_message; + const unsigned char* shifted_message; tmp_len = sha512_context::block_size - ctx.len; rem_len = size < tmp_len ? size : tmp_len; std::memcpy(&ctx.block[ctx.len], pm, rem_len); - if (ctx.len + size < sha512_context::block_size) { + if (ctx.len + size < sha512_context::block_size) + { ctx.len += size; return; } @@ -398,35 +386,34 @@ void update (sha512_context& ctx, sha512_transform(ctx, ctx.block, 1); sha512_transform(ctx, shifted_message, block_nb); rem_len = new_len % sha512_context::block_size; - std::memcpy(ctx.block, &shifted_message[ - block_nb << 7], rem_len); + std::memcpy(ctx.block, &shifted_message[block_nb << 7], rem_len); ctx.len = rem_len; ctx.tot_len += (block_nb + 1) << 7; } template -void finish (sha512_context& ctx, - void* digest) noexcept +void +finish(sha512_context& ctx, void* digest) noexcept { - auto const pd = reinterpret_cast< - unsigned char*>(digest); + auto const pd = reinterpret_cast(digest); unsigned int block_nb; unsigned int pm_len; unsigned int len_b; int i; - block_nb = 1 + ((sha512_context::block_size - 17) < - (ctx.len % sha512_context::block_size)); + block_nb = 1 + + ((sha512_context::block_size - 17) < + (ctx.len % sha512_context::block_size)); len_b = (ctx.tot_len + ctx.len) << 3; pm_len = block_nb << 7; std::memset(ctx.block + ctx.len, 0, pm_len - ctx.len); ctx.block[ctx.len] = 0x80; BEAST_SHA2_UNPACK32(len_b, ctx.block + pm_len - 4); sha512_transform(ctx, ctx.block, block_nb); - for (i = 0 ; i < 8; i++) + for (i = 0; i < 8; i++) BEAST_SHA2_UNPACK64(ctx.h[i], &pd[i << 3]); } -} // detail -} // beast +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/ripemd.h b/src/ripple/beast/crypto/ripemd.h index 5db5a2742f..3111deee85 100644 --- a/src/ripple/beast/crypto/ripemd.h +++ b/src/ripple/beast/crypto/ripemd.h @@ -25,13 +25,11 @@ namespace beast { -using ripemd160_hasher = detail::mac_facade< - detail::ripemd160_context, false>; +using ripemd160_hasher = detail::mac_facade; // secure version -using ripemd160_hasher_s = detail::mac_facade< - detail::ripemd160_context, true>; +using ripemd160_hasher_s = detail::mac_facade; -} +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/secure_erase.h b/src/ripple/beast/crypto/secure_erase.h index 279fa8748c..4878f134fa 100644 --- a/src/ripple/beast/crypto/secure_erase.h +++ b/src/ripple/beast/crypto/secure_erase.h @@ -33,29 +33,28 @@ class secure_erase_impl private: struct base { - virtual void operator()( - void* dest, std::size_t bytes) const = 0; + virtual void + operator()(void* dest, std::size_t bytes) const = 0; virtual ~base() = default; base() = default; base(base const&) = delete; - base& operator=(base const&) = delete; + base& + operator=(base const&) = delete; }; struct impl : base { - void operator()( - void* dest, std::size_t bytes) const override + void + operator()(void* dest, std::size_t bytes) const override { char volatile* volatile p = - const_cast( - reinterpret_cast(dest)); + const_cast(reinterpret_cast(dest)); if (bytes == 0) return; do { *p = 0; - } - while(*p++ == 0 && --bytes); + } while (*p++ == 0 && --bytes); } }; @@ -63,29 +62,28 @@ private: base& erase_; public: - secure_erase_impl() - : erase_(*new(buf_) impl) + secure_erase_impl() : erase_(*new (buf_) impl) { } - void operator()( - void* dest, std::size_t bytes) const + void + operator()(void* dest, std::size_t bytes) const { return erase_(dest, bytes); } }; -} +} // namespace detail /** Guaranteed to fill memory with zeroes */ template void -secure_erase (void* dest, std::size_t bytes) +secure_erase(void* dest, std::size_t bytes) { static detail::secure_erase_impl const erase; erase(dest, bytes); } -} +} // namespace beast #endif diff --git a/src/ripple/beast/crypto/sha2.h b/src/ripple/beast/crypto/sha2.h index 72233ad227..c7cd187a51 100644 --- a/src/ripple/beast/crypto/sha2.h +++ b/src/ripple/beast/crypto/sha2.h @@ -25,20 +25,16 @@ namespace beast { -using sha256_hasher = detail::mac_facade< - detail::sha256_context, false>; +using sha256_hasher = detail::mac_facade; // secure version -using sha256_hasher_s = detail::mac_facade< - detail::sha256_context, true>; +using sha256_hasher_s = detail::mac_facade; -using sha512_hasher = detail::mac_facade< - detail::sha512_context, false>; +using sha512_hasher = detail::mac_facade; // secure version -using sha512_hasher_s = detail::mac_facade< - detail::sha512_context, true>; +using sha512_hasher_s = detail::mac_facade; -} +} // namespace beast #endif diff --git a/src/ripple/beast/hash/endian.h b/src/ripple/beast/hash/endian.h index 813f8c3097..cda8f4b0d4 100644 --- a/src/ripple/beast/hash/endian.h +++ b/src/ripple/beast/hash/endian.h @@ -27,28 +27,28 @@ namespace beast { // 1. Is this system big or little endian? // 2. Is the "desired endian" of some class or function the same as the // native endian? -enum class endian -{ +enum class endian { #ifdef _MSC_VER - big = 1, + big = 1, little = 0, native = little #else native = __BYTE_ORDER__, little = __ORDER_LITTLE_ENDIAN__, - big = __ORDER_BIG_ENDIAN__ + big = __ORDER_BIG_ENDIAN__ #endif }; #ifndef __INTELLISENSE__ -static_assert(endian::native == endian::little || - endian::native == endian::big, - "endian::native shall be one of endian::little or endian::big"); +static_assert( + endian::native == endian::little || endian::native == endian::big, + "endian::native shall be one of endian::little or endian::big"); -static_assert(endian::big != endian::little, - "endian::big and endian::little shall have different values"); +static_assert( + endian::big != endian::little, + "endian::big and endian::little shall have different values"); #endif -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/hash/hash_append.h b/src/ripple/beast/hash/hash_append.h index 6c2bdc8f09..762a51635b 100644 --- a/src/ripple/beast/hash/hash_append.h +++ b/src/ripple/beast/hash/hash_append.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -34,12 +35,11 @@ #include #include #include +#include #include #include -#include #include #include -#include namespace beast { @@ -47,29 +47,25 @@ namespace detail { template /*constexpr*/ -inline -void +inline void reverse_bytes(T& t) { - unsigned char* bytes = static_cast(std::memmove(std::addressof(t), - std::addressof(t), - sizeof(T))); - for (unsigned i = 0; i < sizeof(T)/2; ++i) - std::swap(bytes[i], bytes[sizeof(T)-1-i]); + unsigned char* bytes = static_cast( + std::memmove(std::addressof(t), std::addressof(t), sizeof(T))); + for (unsigned i = 0; i < sizeof(T) / 2; ++i) + std::swap(bytes[i], bytes[sizeof(T) - 1 - i]); } template /*constexpr*/ -inline -void +inline void maybe_reverse_bytes(T& t, std::false_type) { } template /*constexpr*/ -inline -void +inline void maybe_reverse_bytes(T& t, std::true_type) { reverse_bytes(t); @@ -77,15 +73,14 @@ maybe_reverse_bytes(T& t, std::true_type) template /*constexpr*/ -inline -void +inline void maybe_reverse_bytes(T& t, Hasher&) { - maybe_reverse_bytes(t, std::integral_constant{}); + maybe_reverse_bytes( + t, std::integral_constant{}); } -} // detail +} // namespace detail // is_uniquely_represented @@ -96,23 +91,22 @@ maybe_reverse_bytes(T& t, Hasher&) template struct is_uniquely_represented - : public std::integral_constant::value || - std::is_enum::value || - std::is_pointer::value> + : public std::integral_constant< + bool, + std::is_integral::value || std::is_enum::value || + std::is_pointer::value> { explicit is_uniquely_represented() = default; }; template -struct is_uniquely_represented - : public is_uniquely_represented +struct is_uniquely_represented : public is_uniquely_represented { explicit is_uniquely_represented() = default; }; template -struct is_uniquely_represented - : public is_uniquely_represented +struct is_uniquely_represented : public is_uniquely_represented { explicit is_uniquely_represented() = default; }; @@ -128,20 +122,23 @@ struct is_uniquely_represented template struct is_uniquely_represented> - : public std::integral_constant::value && - is_uniquely_represented::value && - sizeof(T) + sizeof(U) == sizeof(std::pair)> + : public std::integral_constant< + bool, + is_uniquely_represented::value && + is_uniquely_represented::value && + sizeof(T) + sizeof(U) == sizeof(std::pair)> { explicit is_uniquely_represented() = default; }; // is_uniquely_represented> -template +template struct is_uniquely_represented> - : public std::integral_constant::value...>::value && - static_sum::value == sizeof(std::tuple)> + : public std::integral_constant< + bool, + static_and::value...>::value && + static_sum::value == sizeof(std::tuple)> { explicit is_uniquely_represented() = default; }; @@ -149,8 +146,7 @@ struct is_uniquely_represented> // is_uniquely_represented template -struct is_uniquely_represented - : public is_uniquely_represented +struct is_uniquely_represented : public is_uniquely_represented { explicit is_uniquely_represented() = default; }; @@ -159,8 +155,10 @@ struct is_uniquely_represented template struct is_uniquely_represented> - : public std::integral_constant::value && - sizeof(T)*N == sizeof(std::array)> + : public std::integral_constant< + bool, + is_uniquely_represented::value && + sizeof(T) * N == sizeof(std::array)> { explicit is_uniquely_represented() = default; }; @@ -181,18 +179,20 @@ struct is_uniquely_represented> /** @{ */ template struct is_contiguously_hashable - : public std::integral_constant::value && - (sizeof(T) == 1 || - HashAlgorithm::endian == endian::native)> + : public std::integral_constant< + bool, + is_uniquely_represented::value && + (sizeof(T) == 1 || HashAlgorithm::endian == endian::native)> { explicit is_contiguously_hashable() = default; }; template struct is_contiguously_hashable - : public std::integral_constant::value && - (sizeof(T) == 1 || - HashAlgorithm::endian == endian::native)> + : public std::integral_constant< + bool, + is_uniquely_represented::value && + (sizeof(T) == 1 || HashAlgorithm::endian == endian::native)> { explicit is_contiguously_hashable() = default; }; @@ -228,23 +228,17 @@ struct is_contiguously_hashable // scalars template -inline -std::enable_if_t -< - is_contiguously_hashable::value -> +inline std::enable_if_t::value> hash_append(Hasher& h, T const& t) noexcept { h(std::addressof(t), sizeof(t)); } template -inline -std::enable_if_t -< +inline std::enable_if_t< !is_contiguously_hashable::value && - (std::is_integral::value || std::is_pointer::value || std::is_enum::value) -> + (std::is_integral::value || std::is_pointer::value || + std::is_enum::value)> hash_append(Hasher& h, T t) noexcept { detail::reverse_bytes(t); @@ -252,11 +246,7 @@ hash_append(Hasher& h, T t) noexcept } template -inline -std::enable_if_t -< - std::is_floating_point::value -> +inline std::enable_if_t::value> hash_append(Hasher& h, T t) noexcept { if (t == 0) @@ -266,8 +256,7 @@ hash_append(Hasher& h, T t) noexcept } template -inline -void +inline void hash_append(Hasher& h, std::nullptr_t) noexcept { void const* p = nullptr; @@ -278,59 +267,39 @@ hash_append(Hasher& h, std::nullptr_t) noexcept // Forward declarations for ADL purposes template -std::enable_if_t -< - !is_contiguously_hashable::value -> +std::enable_if_t::value> hash_append(Hasher& h, T (&a)[N]) noexcept; template -std::enable_if_t -< - !is_contiguously_hashable::value -> -hash_append(Hasher& h, std::basic_string const& s) noexcept; +std::enable_if_t::value> +hash_append( + Hasher& h, + std::basic_string const& s) noexcept; template -std::enable_if_t -< - is_contiguously_hashable::value -> -hash_append(Hasher& h, std::basic_string const& s) noexcept; +std::enable_if_t::value> +hash_append( + Hasher& h, + std::basic_string const& s) noexcept; template -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> -hash_append (Hasher& h, std::pair const& p) noexcept; +std::enable_if_t, Hasher>::value> +hash_append(Hasher& h, std::pair const& p) noexcept; template -std::enable_if_t -< - !is_contiguously_hashable::value -> +std::enable_if_t::value> hash_append(Hasher& h, std::vector const& v) noexcept; template -std::enable_if_t -< - is_contiguously_hashable::value -> +std::enable_if_t::value> hash_append(Hasher& h, std::vector const& v) noexcept; template -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> +std::enable_if_t, Hasher>::value> hash_append(Hasher& h, std::array const& a) noexcept; -template -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> +template +std::enable_if_t, Hasher>::value> hash_append(Hasher& h, std::tuple const& t) noexcept; template @@ -342,28 +311,23 @@ void hash_append(Hasher& h, std::unordered_set const& s); template -std::enable_if_t -< - !is_contiguously_hashable::value -> -hash_append(Hasher& h, boost::container::flat_set const& v) noexcept; +std::enable_if_t::value> +hash_append( + Hasher& h, + boost::container::flat_set const& v) noexcept; template -std::enable_if_t -< - is_contiguously_hashable::value -> -hash_append(Hasher& h, boost::container::flat_set const& v) noexcept; -template +std::enable_if_t::value> +hash_append( + Hasher& h, + boost::container::flat_set const& v) noexcept; +template void -hash_append (Hasher& h, T0 const& t0, T1 const& t1, T const& ...t) noexcept; +hash_append(Hasher& h, T0 const& t0, T1 const& t1, T const&... t) noexcept; // c-array template -std::enable_if_t -< - !is_contiguously_hashable::value -> +std::enable_if_t::value> hash_append(Hasher& h, T (&a)[N]) noexcept { for (auto const& t : a) @@ -373,12 +337,10 @@ hash_append(Hasher& h, T (&a)[N]) noexcept // basic_string template -inline -std::enable_if_t -< - !is_contiguously_hashable::value -> -hash_append(Hasher& h, std::basic_string const& s) noexcept +inline std::enable_if_t::value> +hash_append( + Hasher& h, + std::basic_string const& s) noexcept { for (auto c : s) hash_append(h, c); @@ -386,38 +348,29 @@ hash_append(Hasher& h, std::basic_string const& s) noexcep } template -inline -std::enable_if_t -< - is_contiguously_hashable::value -> -hash_append(Hasher& h, std::basic_string const& s) noexcept +inline std::enable_if_t::value> +hash_append( + Hasher& h, + std::basic_string const& s) noexcept { - h(s.data(), s.size()*sizeof(CharT)); + h(s.data(), s.size() * sizeof(CharT)); hash_append(h, s.size()); } // pair template -inline -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> -hash_append (Hasher& h, std::pair const& p) noexcept +inline std::enable_if_t< + !is_contiguously_hashable, Hasher>::value> +hash_append(Hasher& h, std::pair const& p) noexcept { - hash_append (h, p.first, p.second); + hash_append(h, p.first, p.second); } // vector template -inline -std::enable_if_t -< - !is_contiguously_hashable::value -> +inline std::enable_if_t::value> hash_append(Hasher& h, std::vector const& v) noexcept { for (auto const& t : v) @@ -426,24 +379,17 @@ hash_append(Hasher& h, std::vector const& v) noexcept } template -inline -std::enable_if_t -< - is_contiguously_hashable::value -> +inline std::enable_if_t::value> hash_append(Hasher& h, std::vector const& v) noexcept { - h(v.data(), v.size()*sizeof(T)); + h(v.data(), v.size() * sizeof(T)); hash_append(h, v.size()); } // array template -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> +std::enable_if_t, Hasher>::value> hash_append(Hasher& h, std::array const& a) noexcept { for (auto const& t : a) @@ -451,60 +397,54 @@ hash_append(Hasher& h, std::array const& a) noexcept } template -std::enable_if_t -< - !is_contiguously_hashable::value -> -hash_append(Hasher& h, boost::container::flat_set const& v) noexcept +std::enable_if_t::value> +hash_append( + Hasher& h, + boost::container::flat_set const& v) noexcept { for (auto const& t : v) hash_append(h, t); } template -std::enable_if_t -< - is_contiguously_hashable::value -> -hash_append(Hasher& h, boost::container::flat_set const& v) noexcept +std::enable_if_t::value> +hash_append( + Hasher& h, + boost::container::flat_set const& v) noexcept { - h(&(v.begin()), v.size()*sizeof(Key)); + h(&(v.begin()), v.size() * sizeof(Key)); } // tuple -namespace detail -{ +namespace detail { -inline -void +inline void for_each_item(...) noexcept { } template -inline -int +inline int hash_one(Hasher& h, T const& t) noexcept { hash_append(h, t); return 0; } -template -inline -void -tuple_hash(Hasher& h, std::tuple const& t, std::index_sequence) noexcept +template +inline void +tuple_hash( + Hasher& h, + std::tuple const& t, + std::index_sequence) noexcept { for_each_item(hash_one(h, std::get(t))...); } -} // detail +} // namespace detail -template -inline -std::enable_if_t -< - !is_contiguously_hashable, Hasher>::value -> +template +inline std::enable_if_t< + !is_contiguously_hashable, Hasher>::value> hash_append(Hasher& h, std::tuple const& t) noexcept { detail::tuple_hash(h, t, std::index_sequence_for{}); @@ -513,9 +453,8 @@ hash_append(Hasher& h, std::tuple const& t) noexcept // shared_ptr template -inline -void -hash_append (Hasher& h, std::shared_ptr const& p) noexcept +inline void +hash_append(Hasher& h, std::shared_ptr const& p) noexcept { hash_append(h, p.get()); } @@ -523,27 +462,26 @@ hash_append (Hasher& h, std::shared_ptr const& p) noexcept // chrono template -inline -void -hash_append (Hasher& h, std::chrono::duration const& d) noexcept +inline void +hash_append(Hasher& h, std::chrono::duration const& d) noexcept { hash_append(h, d.count()); } template -inline -void -hash_append (Hasher& h, std::chrono::time_point const& tp) noexcept +inline void +hash_append( + Hasher& h, + std::chrono::time_point const& tp) noexcept { hash_append(h, tp.time_since_epoch()); } // variadic -template -inline -void -hash_append (Hasher& h, T0 const& t0, T1 const& t1, T const& ...t) noexcept +template +inline void +hash_append(Hasher& h, T0 const& t0, T1 const& t1, T const&... t) noexcept { hash_append(h, t0); hash_append(h, t1, t...); @@ -552,13 +490,12 @@ hash_append (Hasher& h, T0 const& t0, T1 const& t1, T const& ...t) noexcept // error_code template -inline -void +inline void hash_append(HashAlgorithm& h, std::error_code const& ec) { hash_append(h, ec.value(), &ec.category()); } -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/hash/impl/xxhash.cpp b/src/ripple/beast/hash/impl/xxhash.cpp index 5b7f5960fd..76d5e7997f 100644 --- a/src/ripple/beast/hash/impl/xxhash.cpp +++ b/src/ripple/beast/hash/impl/xxhash.cpp @@ -36,28 +36,33 @@ You can contact the author at : //************************************** // Tuning parameters //************************************** -// Unaligned memory access is automatically enabled for "common" CPU, such as x86. -// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected. -// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance. -// You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32). -#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) -# define XXH_USE_UNALIGNED_ACCESS 1 +// Unaligned memory access is automatically enabled for "common" CPU, such as +// x86. For others CPU, the compiler will be more cautious, and insert extra +// code to ensure aligned access is respected. If you know your target CPU +// supports unaligned memory access, you want to force this option manually to +// improve performance. You can also enable this parameter if you know your +// input data will always be aligned (boundaries of 4, for U32). +#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || \ + defined(__x86_64__) || defined(_M_X64) +#define XXH_USE_UNALIGNED_ACCESS 1 #endif // XXH_ACCEPT_NULL_INPUT_POINTER : -// If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer. -// When this option is enabled, xxHash output for null input pointers will be the same as a null-length input. -// This option has a very small performance cost (only measurable on small inputs). -// By default, this option is disabled. To enable it, uncomment below define : -// #define XXH_ACCEPT_NULL_INPUT_POINTER 1 +// If the input pointer is a null pointer, xxHash default behavior is to trigger +// a memory access error, since it is a bad pointer. When this option is +// enabled, xxHash output for null input pointers will be the same as a +// null-length input. This option has a very small performance cost (only +// measurable on small inputs). By default, this option is disabled. To enable +// it, uncomment below define : #define XXH_ACCEPT_NULL_INPUT_POINTER 1 // XXH_FORCE_NATIVE_FORMAT : -// By default, xxHash library provides endian-independant Hash values, based on little-endian convention. -// Results are therefore identical for little-endian and big-endian CPU. -// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format. -// Should endian-independance be of no importance for your application, you may set the #define below to 1. -// It will improve speed for Big-endian CPU. -// This option has no impact on Little_Endian CPU. +// By default, xxHash library provides endian-independant Hash values, based on +// little-endian convention. Results are therefore identical for little-endian +// and big-endian CPU. This comes at a performance cost for big-endian CPU, +// since some swapping is required to emulate little-endian format. Should +// endian-independance be of no importance for your application, you may set the +// #define below to 1. It will improve speed for Big-endian CPU. This option has +// no impact on Little_Endian CPU. #define XXH_FORCE_NATIVE_FORMAT 0 //************************************** @@ -65,66 +70,75 @@ You can contact the author at : //************************************** // Disable some Visual warning messages #ifdef _MSC_VER // Visual Studio -# pragma warning(disable : 4127) // disable: C4127: conditional expression is constant +#pragma warning( \ + disable : 4127) // disable: C4127: conditional expression is constant #endif -#ifdef _MSC_VER // Visual Studio -# define FORCE_INLINE static __forceinline +#ifdef _MSC_VER // Visual Studio +#define FORCE_INLINE static __forceinline #else -# ifdef __GNUC__ -# define FORCE_INLINE static inline __attribute__((always_inline)) -# else -# define FORCE_INLINE static inline -# endif +#ifdef __GNUC__ +#define FORCE_INLINE static inline __attribute__((always_inline)) +#else +#define FORCE_INLINE static inline +#endif #endif //************************************** // Includes & Memory related functions //************************************** //#include "xxhash.h" -// Modify the local functions below should you wish to use some other memory routines -// for malloc(), free() +// Modify the local functions below should you wish to use some other memory +// routines for malloc(), free() #include -static void* XXH_malloc(size_t s) { return malloc(s); } -static void XXH_free (void* p) { free(p); } +static void* +XXH_malloc(size_t s) +{ + return malloc(s); +} +static void +XXH_free(void* p) +{ + free(p); +} // for memcpy() #include -static void* XXH_memcpy(void* dest, const void* src, size_t size) +static void* +XXH_memcpy(void* dest, const void* src, size_t size) { - return memcpy(dest,src,size); + return memcpy(dest, src, size); } - //************************************** // Basic Types //************************************** -#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 -# include -typedef uint8_t BYTE; +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 +#include +typedef uint8_t BYTE; typedef uint16_t U16; typedef uint32_t U32; -typedef int32_t S32; +typedef int32_t S32; typedef uint64_t U64; #else -typedef unsigned char BYTE; -typedef unsigned short U16; -typedef unsigned int U32; -typedef signed int S32; +typedef unsigned char BYTE; +typedef unsigned short U16; +typedef unsigned int U32; +typedef signed int S32; typedef unsigned long long U64; #endif -#if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS) -# define _PACKED __attribute__ ((packed)) +#if defined(__GNUC__) && !defined(XXH_USE_UNALIGNED_ACCESS) +#define _PACKED __attribute__((packed)) #else -# define _PACKED +#define _PACKED #endif #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) -# ifdef __IBMC__ -# pragma pack(1) -# else -# pragma pack(push, 1) -# endif +#ifdef __IBMC__ +#pragma pack(1) +#else +#pragma pack(push, 1) +#endif #endif namespace beast { @@ -140,122 +154,133 @@ typedef struct _U64_S } _PACKED U64_S; #if !defined(XXH_USE_UNALIGNED_ACCESS) && !defined(__GNUC__) -# pragma pack(pop) +#pragma pack(pop) #endif -#define A32(x) (((U32_S *)(x))->v) -#define A64(x) (((U64_S *)(x))->v) - +#define A32(x) (((U32_S*)(x))->v) +#define A64(x) (((U64_S*)(x))->v) //*************************************** // Compiler-specific Functions and Macros //*************************************** #define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) -// Note : although _rotl exists for minGW (GCC under windows), performance seems poor +// Note : although _rotl exists for minGW (GCC under windows), performance seems +// poor #if defined(_MSC_VER) -# define XXH_rotl32(x,r) _rotl(x,r) -# define XXH_rotl64(x,r) _rotl64(x,r) +#define XXH_rotl32(x, r) _rotl(x, r) +#define XXH_rotl64(x, r) _rotl64(x, r) #else -# define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r))) -# define XXH_rotl64(x,r) ((x << r) | (x >> (64 - r))) +#define XXH_rotl32(x, r) ((x << r) | (x >> (32 - r))) +#define XXH_rotl64(x, r) ((x << r) | (x >> (64 - r))) #endif -#if defined(_MSC_VER) // Visual Studio -# define XXH_swap32 _byteswap_ulong -# define XXH_swap64 _byteswap_uint64 +#if defined(_MSC_VER) // Visual Studio +#define XXH_swap32 _byteswap_ulong +#define XXH_swap64 _byteswap_uint64 #elif GCC_VERSION >= 403 -# define XXH_swap32 __builtin_bswap32 -# define XXH_swap64 __builtin_bswap64 +#define XXH_swap32 __builtin_bswap32 +#define XXH_swap64 __builtin_bswap64 #else -static inline U32 XXH_swap32 (U32 x) +static inline U32 +XXH_swap32(U32 x) { - return ((x << 24) & 0xff000000 ) | - ((x << 8) & 0x00ff0000 ) | - ((x >> 8) & 0x0000ff00 ) | - ((x >> 24) & 0x000000ff ); + return ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | + ((x >> 8) & 0x0000ff00) | ((x >> 24) & 0x000000ff); } -static inline U64 XXH_swap64 (U64 x) +static inline U64 +XXH_swap64(U64 x) { - return ((x << 56) & 0xff00000000000000ULL) | - ((x << 40) & 0x00ff000000000000ULL) | - ((x << 24) & 0x0000ff0000000000ULL) | - ((x << 8) & 0x000000ff00000000ULL) | - ((x >> 8) & 0x00000000ff000000ULL) | - ((x >> 24) & 0x0000000000ff0000ULL) | - ((x >> 40) & 0x000000000000ff00ULL) | - ((x >> 56) & 0x00000000000000ffULL); + return ((x << 56) & 0xff00000000000000ULL) | + ((x << 40) & 0x00ff000000000000ULL) | + ((x << 24) & 0x0000ff0000000000ULL) | + ((x << 8) & 0x000000ff00000000ULL) | + ((x >> 8) & 0x00000000ff000000ULL) | + ((x >> 24) & 0x0000000000ff0000ULL) | + ((x >> 40) & 0x000000000000ff00ULL) | + ((x >> 56) & 0x00000000000000ffULL); } #endif - //************************************** // Constants //************************************** -#define PRIME32_1 2654435761U -#define PRIME32_2 2246822519U -#define PRIME32_3 3266489917U -#define PRIME32_4 668265263U -#define PRIME32_5 374761393U +#define PRIME32_1 2654435761U +#define PRIME32_2 2246822519U +#define PRIME32_3 3266489917U +#define PRIME32_4 668265263U +#define PRIME32_5 374761393U #define PRIME64_1 11400714785074694791ULL #define PRIME64_2 14029467366897019727ULL -#define PRIME64_3 1609587929392839161ULL -#define PRIME64_4 9650029242287828579ULL -#define PRIME64_5 2870177450012600261ULL +#define PRIME64_3 1609587929392839161ULL +#define PRIME64_4 9650029242287828579ULL +#define PRIME64_5 2870177450012600261ULL //************************************** // Architecture Macros //************************************** -typedef enum { XXH_bigEndian=0, XXH_littleEndian=1 } XXH_endianess; -#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch +typedef enum { XXH_bigEndian = 0, XXH_littleEndian = 1 } XXH_endianess; +#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN + // externally, for example using a compiler + // switch static const int one = 1; -# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one)) +#define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one)) #endif - //************************************** // Macros //************************************** -#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations - +#define XXH_STATIC_ASSERT(c) \ + { \ + enum { XXH_static_assert = 1 / (!!(c)) }; \ + } // use only *after* variable declarations //**************************** // Memory reads //**************************** typedef enum { XXH_aligned, XXH_unaligned } XXH_alignment; -FORCE_INLINE U32 XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +FORCE_INLINE U32 +XXH_readLE32_align(const void* ptr, XXH_endianess endian, XXH_alignment align) { - if (align==XXH_unaligned) - return endian==XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); + if (align == XXH_unaligned) + return endian == XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr)); else - return endian==XXH_littleEndian ? *(U32*)ptr : XXH_swap32(*(U32*)ptr); + return endian == XXH_littleEndian ? *(U32*)ptr : XXH_swap32(*(U32*)ptr); } -FORCE_INLINE U32 XXH_readLE32(const void* ptr, XXH_endianess endian) +FORCE_INLINE U32 +XXH_readLE32(const void* ptr, XXH_endianess endian) { return XXH_readLE32_align(ptr, endian, XXH_unaligned); } -FORCE_INLINE U64 XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) +FORCE_INLINE U64 +XXH_readLE64_align(const void* ptr, XXH_endianess endian, XXH_alignment align) { - if (align==XXH_unaligned) - return endian==XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); + if (align == XXH_unaligned) + return endian == XXH_littleEndian ? A64(ptr) : XXH_swap64(A64(ptr)); else - return endian==XXH_littleEndian ? *(U64*)ptr : XXH_swap64(*(U64*)ptr); + return endian == XXH_littleEndian ? *(U64*)ptr : XXH_swap64(*(U64*)ptr); } -FORCE_INLINE U64 XXH_readLE64(const void* ptr, XXH_endianess endian) +FORCE_INLINE U64 +XXH_readLE64(const void* ptr, XXH_endianess endian) { return XXH_readLE64_align(ptr, endian, XXH_unaligned); } - //**************************** // Simple Hash Functions //**************************** -FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH_endianess endian, XXH_alignment align) +FORCE_INLINE U32 +XXH32_endian_align( + const void* input, + size_t len, + U32 seed, + XXH_endianess endian, + XXH_alignment align) { const BYTE* p = (const BYTE*)input; const BYTE* bEnd = p + len; @@ -263,14 +288,14 @@ FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH #define XXH_get32bits(p) XXH_readLE32_align(p, endian, align) #ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (p==NULL) + if (p == NULL) { - len=0; - bEnd=p=(const BYTE*)(size_t)16; + len = 0; + bEnd = p = (const BYTE*)(size_t)16; } #endif - if (len>=16) + if (len >= 16) { const BYTE* const limit = bEnd - 16; U32 v1 = seed + PRIME32_1 + PRIME32_2; @@ -283,42 +308,42 @@ FORCE_INLINE U32 XXH32_endian_align(const void* input, size_t len, U32 seed, XXH v1 += XXH_get32bits(p) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; - p+=4; + p += 4; v2 += XXH_get32bits(p) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; - p+=4; + p += 4; v3 += XXH_get32bits(p) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; - p+=4; + p += 4; v4 += XXH_get32bits(p) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; - p+=4; - } - while (p<=limit); + p += 4; + } while (p <= limit); - h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18); + h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + + XXH_rotl32(v4, 18); } else { - h32 = seed + PRIME32_5; + h32 = seed + PRIME32_5; } - h32 += (U32) len; + h32 += (U32)len; - while (p+4<=bEnd) + while (p + 4 <= bEnd) { h32 += XXH_get32bits(p) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4 ; - p+=4; + h32 = XXH_rotl32(h32, 17) * PRIME32_4; + p += 4; } - while (p=32) + if (len >= 32) { const BYTE* const limit = bEnd - 32; U64 v1 = seed + PRIME64_1 + PRIME64_2; @@ -386,25 +422,25 @@ FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH do { v1 += XXH_get64bits(p) * PRIME64_2; - p+=8; + p += 8; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; v2 += XXH_get64bits(p) * PRIME64_2; - p+=8; + p += 8; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; v3 += XXH_get64bits(p) * PRIME64_2; - p+=8; + p += 8; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; v4 += XXH_get64bits(p) * PRIME64_2; - p+=8; + p += 8; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; - } - while (p<=limit); + } while (p <= limit); - h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + + XXH_rotl64(v4, 18); v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); @@ -432,30 +468,30 @@ FORCE_INLINE U64 XXH64_endian_align(const void* input, size_t len, U64 seed, XXH } else { - h64 = seed + PRIME64_5; + h64 = seed + PRIME64_5; } - h64 += (U64) len; + h64 += (U64)len; - while (p+8<=bEnd) + while (p + 8 <= bEnd) { U64 k1 = XXH_get64bits(p); k1 *= PRIME64_2; - k1 = XXH_rotl64(k1,31); + k1 = XXH_rotl64(k1, 31); k1 *= PRIME64_1; h64 ^= k1; - h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; - p+=8; + h64 = XXH_rotl64(h64, 27) * PRIME64_1 + PRIME64_4; + p += 8; } - if (p+4<=bEnd) + if (p + 4 <= bEnd) { h64 ^= (U64)(XXH_get32bits(p)) * PRIME64_1; h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; - p+=4; + p += 4; } - while (p= sizeof(XXH_istate32_t), ""); // A compilation error here means XXH32_state_t is not large enough + static_assert( + sizeof(XXH32_state_t) >= sizeof(XXH_istate32_t), + ""); // A compilation error here means XXH32_state_t is not large + // enough return (XXH32_state_t*)XXH_malloc(sizeof(XXH32_state_t)); } -XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr) +XXH_errorcode +XXH32_freeState(XXH32_state_t* statePtr) { XXH_free(statePtr); return XXH_OK; }; -XXH64_state_t* XXH64_createState(void) +XXH64_state_t* +XXH64_createState(void) { - static_assert(sizeof(XXH64_state_t) >= sizeof(XXH_istate64_t), ""); // A compilation error here means XXH64_state_t is not large enough + static_assert( + sizeof(XXH64_state_t) >= sizeof(XXH_istate64_t), + ""); // A compilation error here means XXH64_state_t is not large + // enough return (XXH64_state_t*)XXH_malloc(sizeof(XXH64_state_t)); } -XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr) +XXH_errorcode +XXH64_freeState(XXH64_state_t* statePtr) { XXH_free(statePtr); return XXH_OK; }; - /*** Hash feed ***/ -XXH_errorcode XXH32_reset(XXH32_state_t* state_in, U32 seed) +XXH_errorcode +XXH32_reset(XXH32_state_t* state_in, U32 seed) { - XXH_istate32_t* state = (XXH_istate32_t*) state_in; + XXH_istate32_t* state = (XXH_istate32_t*)state_in; state->seed = seed; state->v1 = seed + PRIME32_1 + PRIME32_2; state->v2 = seed + PRIME32_2; @@ -568,9 +618,10 @@ XXH_errorcode XXH32_reset(XXH32_state_t* state_in, U32 seed) return XXH_OK; } -XXH_errorcode XXH64_reset(XXH64_state_t* state_in, unsigned long long seed) +XXH_errorcode +XXH64_reset(XXH64_state_t* state_in, unsigned long long seed) { - XXH_istate64_t* state = (XXH_istate64_t*) state_in; + XXH_istate64_t* state = (XXH_istate64_t*)state_in; state->seed = seed; state->v1 = seed + PRIME64_1 + PRIME64_2; state->v2 = seed + PRIME64_2; @@ -581,29 +632,35 @@ XXH_errorcode XXH64_reset(XXH64_state_t* state_in, unsigned long long seed) return XXH_OK; } - -FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const void* input, size_t len, XXH_endianess endian) +FORCE_INLINE XXH_errorcode +XXH32_update_endian( + XXH32_state_t* state_in, + const void* input, + size_t len, + XXH_endianess endian) { - XXH_istate32_t* state = (XXH_istate32_t *) state_in; + XXH_istate32_t* state = (XXH_istate32_t*)state_in; const BYTE* p = (const BYTE*)input; const BYTE* const bEnd = p + len; #ifdef XXH_ACCEPT_NULL_INPUT_POINTER - if (input==NULL) return XXH_ERROR; + if (input == NULL) + return XXH_ERROR; #endif state->total_len += len; - if (state->memsize + len < 16) // fill in tmp buffer + if (state->memsize + len < 16) // fill in tmp buffer { XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, len); state->memsize += (U32)len; return XXH_OK; } - if (state->memsize) // some data left from previous update + if (state->memsize) // some data left from previous update { - XXH_memcpy((BYTE*)(state->mem32) + state->memsize, input, 16-state->memsize); + XXH_memcpy( + (BYTE*)(state->mem32) + state->memsize, input, 16 - state->memsize); { const U32* p32 = state->mem32; state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; @@ -623,11 +680,11 @@ FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const v state->v4 *= PRIME32_1; p32++; } - p += 16-state->memsize; + p += 16 - state->memsize; state->memsize = 0; } - if (p <= bEnd-16) + if (p <= bEnd - 16) { const BYTE* const limit = bEnd - 16; U32 v1 = state->v1; @@ -640,21 +697,20 @@ FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const v v1 += XXH_readLE32(p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; - p+=4; + p += 4; v2 += XXH_readLE32(p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; - p+=4; + p += 4; v3 += XXH_readLE32(p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; - p+=4; + p += 4; v4 += XXH_readLE32(p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; - p+=4; - } - while (p<=limit); + p += 4; + } while (p <= limit); state->v1 = v1; state->v2 = v2; @@ -664,51 +720,52 @@ FORCE_INLINE XXH_errorcode XXH32_update_endian (XXH32_state_t* state_in, const v if (p < bEnd) { - XXH_memcpy(state->mem32, p, bEnd-p); - state->memsize = (int)(bEnd-p); + XXH_memcpy(state->mem32, p, bEnd - p); + state->memsize = (int)(bEnd - p); } return XXH_OK; } -XXH_errorcode XXH32_update (XXH32_state_t* state_in, const void* input, size_t len) +XXH_errorcode +XXH32_update(XXH32_state_t* state_in, const void* input, size_t len) { XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) return XXH32_update_endian(state_in, input, len, XXH_littleEndian); else return XXH32_update_endian(state_in, input, len, XXH_bigEndian); } - - -FORCE_INLINE U32 XXH32_digest_endian (const XXH32_state_t* state_in, XXH_endianess endian) +FORCE_INLINE U32 +XXH32_digest_endian(const XXH32_state_t* state_in, XXH_endianess endian) { - XXH_istate32_t* state = (XXH_istate32_t*) state_in; - const BYTE * p = (const BYTE*)state->mem32; + XXH_istate32_t* state = (XXH_istate32_t*)state_in; + const BYTE* p = (const BYTE*)state->mem32; BYTE* bEnd = (BYTE*)(state->mem32) + state->memsize; U32 h32; if (state->total_len >= 16) { - h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); + h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18); } else { - h32 = state->seed + PRIME32_5; + h32 = state->seed + PRIME32_5; } - h32 += (U32) state->total_len; + h32 += (U32)state->total_len; - while (p+4<=bEnd) + while (p + 4 <= bEnd) { h32 += XXH_readLE32(p, endian) * PRIME32_3; - h32 = XXH_rotl32(h32, 17) * PRIME32_4; - p+=4; + h32 = XXH_rotl32(h32, 17) * PRIME32_4; + p += 4; } - while (ptotal_len += len; - if (state->memsize + len < 32) // fill in tmp buffer + if (state->memsize + len < 32) // fill in tmp buffer { XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, len); state->memsize += (U32)len; return XXH_OK; } - if (state->memsize) // some data left from previous update + if (state->memsize) // some data left from previous update { - XXH_memcpy(((BYTE*)state->mem64) + state->memsize, input, 32-state->memsize); + XXH_memcpy( + ((BYTE*)state->mem64) + state->memsize, input, 32 - state->memsize); { const U64* p64 = state->mem64; state->v1 += XXH_readLE64(p64, endian) * PRIME64_2; @@ -777,11 +840,11 @@ FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const v state->v4 *= PRIME64_1; p64++; } - p += 32-state->memsize; + p += 32 - state->memsize; state->memsize = 0; } - if (p+32 <= bEnd) + if (p + 32 <= bEnd) { const BYTE* const limit = bEnd - 32; U64 v1 = state->v1; @@ -794,21 +857,20 @@ FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const v v1 += XXH_readLE64(p, endian) * PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; - p+=8; + p += 8; v2 += XXH_readLE64(p, endian) * PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; - p+=8; + p += 8; v3 += XXH_readLE64(p, endian) * PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; - p+=8; + p += 8; v4 += XXH_readLE64(p, endian) * PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; - p+=8; - } - while (p<=limit); + p += 8; + } while (p <= limit); state->v1 = v1; state->v2 = v2; @@ -818,29 +880,29 @@ FORCE_INLINE XXH_errorcode XXH64_update_endian (XXH64_state_t* state_in, const v if (p < bEnd) { - XXH_memcpy(state->mem64, p, bEnd-p); - state->memsize = (int)(bEnd-p); + XXH_memcpy(state->mem64, p, bEnd - p); + state->memsize = (int)(bEnd - p); } return XXH_OK; } -XXH_errorcode XXH64_update (XXH64_state_t* state_in, const void* input, size_t len) +XXH_errorcode +XXH64_update(XXH64_state_t* state_in, const void* input, size_t len) { XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN; - if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) + if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT) return XXH64_update_endian(state_in, input, len, XXH_littleEndian); else return XXH64_update_endian(state_in, input, len, XXH_bigEndian); } - - -FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state_in, XXH_endianess endian) +FORCE_INLINE U64 +XXH64_digest_endian(const XXH64_state_t* state_in, XXH_endianess endian) { - XXH_istate64_t * state = (XXH_istate64_t *) state_in; - const BYTE * p = (const BYTE*)state->mem64; + XXH_istate64_t* state = (XXH_istate64_t*)state_in; + const BYTE* p = (const BYTE*)state->mem64; BYTE* bEnd = (BYTE*)state->mem64 + state->memsize; U64 h64; @@ -851,58 +913,59 @@ FORCE_INLINE U64 XXH64_digest_endian (const XXH64_state_t* state_in, XXH_endiane U64 v3 = state->v3; U64 v4 = state->v4; - h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + XXH_rotl64(v4, 18); + h64 = XXH_rotl64(v1, 1) + XXH_rotl64(v2, 7) + XXH_rotl64(v3, 12) + + XXH_rotl64(v4, 18); v1 *= PRIME64_2; v1 = XXH_rotl64(v1, 31); v1 *= PRIME64_1; h64 ^= v1; - h64 = h64*PRIME64_1 + PRIME64_4; + h64 = h64 * PRIME64_1 + PRIME64_4; v2 *= PRIME64_2; v2 = XXH_rotl64(v2, 31); v2 *= PRIME64_1; h64 ^= v2; - h64 = h64*PRIME64_1 + PRIME64_4; + h64 = h64 * PRIME64_1 + PRIME64_4; v3 *= PRIME64_2; v3 = XXH_rotl64(v3, 31); v3 *= PRIME64_1; h64 ^= v3; - h64 = h64*PRIME64_1 + PRIME64_4; + h64 = h64 * PRIME64_1 + PRIME64_4; v4 *= PRIME64_2; v4 = XXH_rotl64(v4, 31); v4 *= PRIME64_1; h64 ^= v4; - h64 = h64*PRIME64_1 + PRIME64_4; + h64 = h64 * PRIME64_1 + PRIME64_4; } else { - h64 = state->seed + PRIME64_5; + h64 = state->seed + PRIME64_5; } - h64 += (U64) state->total_len; + h64 += (U64)state->total_len; - while (p+8<=bEnd) + while (p + 8 <= bEnd) { U64 k1 = XXH_readLE64(p, endian); k1 *= PRIME64_2; - k1 = XXH_rotl64(k1,31); + k1 = XXH_rotl64(k1, 31); k1 *= PRIME64_1; h64 ^= k1; - h64 = XXH_rotl64(h64,27) * PRIME64_1 + PRIME64_4; - p+=8; + h64 = XXH_rotl64(h64, 27) * PRIME64_1 + PRIME64_4; + p += 8; } - if (p+4<=bEnd) + if (p + 4 <= bEnd) { h64 ^= (U64)(XXH_readLE32(p, endian)) * PRIME64_1; h64 = XXH_rotl64(h64, 23) * PRIME64_2 + PRIME64_3; - p+=4; + p += 4; } - while (p /* size_t */ +#include /* size_t */ namespace beast { namespace detail { @@ -71,35 +72,38 @@ namespace detail { /***************************** Type *****************************/ -typedef enum { XXH_OK=0, XXH_ERROR } XXH_errorcode; - - +typedef enum { XXH_OK = 0, XXH_ERROR } XXH_errorcode; /***************************** Simple Hash Functions *****************************/ -unsigned int XXH32 (const void* input, size_t length, unsigned seed); -unsigned long long XXH64 (const void* input, size_t length, unsigned long long seed); +unsigned int +XXH32(const void* input, size_t length, unsigned seed); +unsigned long long +XXH64(const void* input, size_t length, unsigned long long seed); /* XXH32() : - Calculate the 32-bits hash of sequence "length" bytes stored at memory address "input". - The memory between input & input+length must be valid (allocated and read-accessible). - "seed" can be used to alter the result predictably. - This function successfully passes all SMHasher tests. - Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s -XXH64() : - Calculate the 64-bits hash of sequence of length "len" stored at memory address "input". + Calculate the 32-bits hash of sequence "length" bytes stored at memory +address "input". The memory between input & input+length must be valid +(allocated and read-accessible). "seed" can be used to alter the result +predictably. This function successfully passes all SMHasher tests. Speed on Core +2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s XXH64() : Calculate +the 64-bits hash of sequence of length "len" stored at memory address "input". */ - - /***************************** Advanced Hash Functions *****************************/ -typedef struct { long long ll[ 6]; } XXH32_state_t; -typedef struct { long long ll[11]; } XXH64_state_t; +typedef struct +{ + long long ll[6]; +} XXH32_state_t; +typedef struct +{ + long long ll[11]; +} XXH64_state_t; /* These structures allow static allocation of XXH states. @@ -108,47 +112,59 @@ States must then be initialized using XXHnn_reset() before first use. If you prefer dynamic allocation, please refer to functions below. */ -XXH32_state_t* XXH32_createState(void); -XXH_errorcode XXH32_freeState(XXH32_state_t* statePtr); +XXH32_state_t* +XXH32_createState(void); +XXH_errorcode +XXH32_freeState(XXH32_state_t* statePtr); -XXH64_state_t* XXH64_createState(void); -XXH_errorcode XXH64_freeState(XXH64_state_t* statePtr); +XXH64_state_t* +XXH64_createState(void); +XXH_errorcode +XXH64_freeState(XXH64_state_t* statePtr); /* These functions create and release memory for XXH state. States must then be initialized using XXHnn_reset() before first use. */ +XXH_errorcode +XXH32_reset(XXH32_state_t* statePtr, unsigned seed); +XXH_errorcode +XXH32_update(XXH32_state_t* statePtr, const void* input, size_t length); +unsigned int +XXH32_digest(const XXH32_state_t* statePtr); -XXH_errorcode XXH32_reset (XXH32_state_t* statePtr, unsigned seed); -XXH_errorcode XXH32_update (XXH32_state_t* statePtr, const void* input, size_t length); -unsigned int XXH32_digest (const XXH32_state_t* statePtr); - -XXH_errorcode XXH64_reset (XXH64_state_t* statePtr, unsigned long long seed); -XXH_errorcode XXH64_update (XXH64_state_t* statePtr, const void* input, size_t length); -unsigned long long XXH64_digest (const XXH64_state_t* statePtr); +XXH_errorcode +XXH64_reset(XXH64_state_t* statePtr, unsigned long long seed); +XXH_errorcode +XXH64_update(XXH64_state_t* statePtr, const void* input, size_t length); +unsigned long long +XXH64_digest(const XXH64_state_t* statePtr); /* -These functions calculate the xxHash of an input provided in multiple smaller packets, -as opposed to an input provided as a single block. +These functions calculate the xxHash of an input provided in multiple smaller +packets, as opposed to an input provided as a single block. -XXH state space must first be allocated, using either static or dynamic method provided above. +XXH state space must first be allocated, using either static or dynamic method +provided above. Start a new hash by initializing state with a seed, using XXHnn_reset(). Then, feed the hash state by calling XXHnn_update() as many times as necessary. Obviously, input must be valid, meaning allocated and read accessible. -The function returns an error code, with 0 meaning OK, and any other value meaning there is an error. +The function returns an error code, with 0 meaning OK, and any other value +meaning there is an error. Finally, you can produce a hash anytime, by using XXHnn_digest(). This function returns the final nn-bits hash. You can nonetheless continue feeding the hash state with more input, and therefore get some new hashes, by calling again XXHnn_digest(). -When you are done, don't forget to free XXH state space, using typically XXHnn_freeState(). +When you are done, don't forget to free XXH state space, using typically +XXHnn_freeState(). */ -} // detail -} // beast +} // namespace detail +} // namespace beast #endif diff --git a/src/ripple/beast/hash/meta.h b/src/ripple/beast/hash/meta.h index 5f9182bdfd..e249b0ed2c 100644 --- a/src/ripple/beast/hash/meta.h +++ b/src/ripple/beast/hash/meta.h @@ -24,42 +24,39 @@ namespace beast { -template struct static_and; +template +struct static_and; -template -struct static_and - : public std::integral_constant < - bool, b0 && static_and::value> +template +struct static_and + : public std::integral_constant::value> { explicit static_and() = default; }; template <> -struct static_and<> - : public std::true_type +struct static_and<> : public std::true_type { explicit static_and() = default; }; #ifndef __INTELLISENSE__ -static_assert( static_and::value, ""); +static_assert(static_and::value, ""); static_assert(!static_and::value, ""); #endif -template +template struct static_sum; -template -struct static_sum - : public std::integral_constant < - std::size_t, s0 + static_sum::value> +template +struct static_sum + : public std::integral_constant::value> { explicit static_sum() = default; }; template <> -struct static_sum<> - : public std::integral_constant +struct static_sum<> : public std::integral_constant { explicit static_sum() = default; }; @@ -69,12 +66,9 @@ static_assert(static_sum<5, 2, 17, 0>::value == 24, ""); #endif template -struct enable_if_lvalue - : public std::enable_if - < - std::is_same, U>::value && - std::is_lvalue_reference::value - > +struct enable_if_lvalue : public std::enable_if< + std::is_same, U>::value && + std::is_lvalue_reference::value> { explicit enable_if_lvalue() = default; }; @@ -134,8 +128,8 @@ struct enable_if_lvalue X x {}; Y const y {}; Unsafe u1 (x, y); // ok - Unsafe u2 (X (), y); // compiles, but u2.x_ becomes invalid at the end of the line. - Safe s1 (x, y); // ok + Unsafe u2 (X (), y); // compiles, but u2.x_ becomes invalid at + the end of the line. Safe s1 (x, y); // ok // Safe s2 (X (), y); // compile-time error } }; @@ -144,6 +138,6 @@ struct enable_if_lvalue template using enable_if_lvalue_t = typename enable_if_lvalue::type; -} // beast +} // namespace beast -#endif // BEAST_UTILITY_META_H_INCLUDED +#endif // BEAST_UTILITY_META_H_INCLUDED diff --git a/src/ripple/beast/hash/tests/hash_metrics.h b/src/ripple/beast/hash/tests/hash_metrics.h index 088cef3d63..62a7855427 100644 --- a/src/ripple/beast/hash/tests/hash_metrics.h +++ b/src/ripple/beast/hash/tests/hash_metrics.h @@ -22,8 +22,8 @@ #define BEAST_HASH_HASH_METRICS_H_INCLUDED #include -#include #include +#include #include #include #include @@ -38,10 +38,10 @@ namespace hash_metrics { /** Returns the fraction of duplicate items in the sequence. */ template float -collision_factor (FwdIter first, FwdIter last) +collision_factor(FwdIter first, FwdIter last) { - std::set s (first, last); - return 1 - static_cast (s.size()) / std::distance (first, last); + std::set s(first, last); + return 1 - static_cast(s.size()) / std::distance(first, last); } //------------------------------------------------------------------------------ @@ -49,19 +49,18 @@ collision_factor (FwdIter first, FwdIter last) /** Returns the deviation of the sequence from the ideal distribution. */ template float -distribution_factor (FwdIter first, FwdIter last) +distribution_factor(FwdIter first, FwdIter last) { using value_type = typename FwdIter::value_type; - static_assert (std::is_unsigned ::value, ""); + static_assert(std::is_unsigned::value, ""); const unsigned nbits = CHAR_BIT * sizeof(std::size_t); const unsigned rows = nbits / 4; unsigned counts[rows][16] = {}; - std::for_each (first, last, [&](typename FwdIter::value_type h) - { + std::for_each(first, last, [&](typename FwdIter::value_type h) { std::size_t mask = 0xF; for (unsigned i = 0; i < rows; ++i, mask <<= 4) - counts[i][(h & mask) >> 4*i] += 1; + counts[i][(h & mask) >> 4 * i] += 1; }); float mean_rows[rows] = {0}; float mean_cols[16] = {0}; @@ -82,8 +81,10 @@ distribution_factor (FwdIter first, FwdIter last) { for (unsigned j = 0; j < 16; ++j) { - dev[i][j].first = std::abs(counts[i][j] - mean_rows[i]) / mean_rows[i]; - dev[i][j].second = std::abs(counts[i][j] - mean_cols[j]) / mean_cols[j]; + dev[i][j].first = + std::abs(counts[i][j] - mean_rows[i]) / mean_rows[i]; + dev[i][j].second = + std::abs(counts[i][j] - mean_cols[j]) / mean_cols[j]; } } float max_err = 0; @@ -105,88 +106,88 @@ distribution_factor (FwdIter first, FwdIter last) namespace detail { template -inline -T +inline T sqr(T t) { - return t*t; + return t * t; } double -score (int const* bins, std::size_t const bincount, double const k) +score(int const* bins, std::size_t const bincount, double const k) { double const n = bincount; // compute rms^2 value double rms_sq = 0; - for(std::size_t i = 0; i < bincount; ++i) - rms_sq += sqr(bins[i]);; + for (std::size_t i = 0; i < bincount; ++i) + rms_sq += sqr(bins[i]); + ; rms_sq /= n; // compute fill factor - double const f = (sqr(k) - 1) / (n*rms_sq - k); + double const f = (sqr(k) - 1) / (n * rms_sq - k); // rescale to (0,1) with 0 = good, 1 = bad return 1 - (f / n); } template std::uint32_t -window (T* blob, int start, int count ) +window(T* blob, int start, int count) { std::size_t const len = sizeof(T); static_assert((len & 3) == 0, ""); - if(count == 0) + if (count == 0) return 0; int const nbits = len * CHAR_BIT; start %= nbits; int ndwords = len / 4; - std::uint32_t const* k = static_cast < - std::uint32_t const*>(static_cast(blob)); - int c = start & (32-1); + std::uint32_t const* k = + static_cast(static_cast(blob)); + int c = start & (32 - 1); int d = start / 32; - if(c == 0) + if (c == 0) return (k[d] & ((1 << count) - 1)); int ia = (d + 1) % ndwords; int ib = (d + 0) % ndwords; std::uint32_t a = k[ia]; std::uint32_t b = k[ib]; - std::uint32_t t = (a << (32-c)) | (b >> c); - t &= ((1 << count)-1); + std::uint32_t t = (a << (32 - c)) | (b >> c); + t &= ((1 << count) - 1); return t; } -} // detail +} // namespace detail /** Calculated a windowed metric using bins. TODO Need reference (SMHasher?) */ template double -windowed_score (FwdIter first, FwdIter last) +windowed_score(FwdIter first, FwdIter last) { - auto const size (std::distance (first, last)); + auto const size(std::distance(first, last)); int maxwidth = 20; // We need at least 5 keys per bin to reliably test distribution biases // down to 1%, so don't bother to test sparser distributions than that while (static_cast(size) / (1ull << maxwidth) < 5.0) maxwidth--; double worst = 0; - std::vector bins (1ull << maxwidth); + std::vector bins(1ull << maxwidth); int const hashbits = sizeof(std::size_t) * CHAR_BIT; for (int start = 0; start < hashbits; ++start) { int width = maxwidth; - bins.assign (1ull << width, 0); - for (auto iter (first); iter != last; ++iter) + bins.assign(1ull << width, 0); + for (auto iter(first); iter != last; ++iter) ++bins[detail::window(&*iter, start, width)]; // Test the distribution, then fold the bins in half, // repeat until we're down to 256 bins while (bins.size() >= 256) { - double score (detail::score ( - bins.data(), bins.size(), size)); + double score(detail::score(bins.data(), bins.size(), size)); worst = std::max(score, worst); if (--width < 8) break; - for (std::size_t i = 0, j = bins.size() / 2; j < bins.size(); ++i, ++j) + for (std::size_t i = 0, j = bins.size() / 2; j < bins.size(); + ++i, ++j) bins[i] += bins[j]; bins.resize(bins.size() / 2); } @@ -194,7 +195,7 @@ windowed_score (FwdIter first, FwdIter last) return worst; } -} // hash_metrics -} // beast +} // namespace hash_metrics +} // namespace beast #endif diff --git a/src/ripple/beast/hash/uhash.h b/src/ripple/beast/hash/uhash.h index 76aa6f15fe..921ea3c984 100644 --- a/src/ripple/beast/hash/uhash.h +++ b/src/ripple/beast/hash/uhash.h @@ -39,11 +39,11 @@ struct uhash operator()(T const& t) const noexcept { Hasher h; - hash_append (h, t); + hash_append(h, t); return static_cast(h); } }; -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/hash/xxhasher.h b/src/ripple/beast/hash/xxhasher.h index 12cca51cb1..d6f6335ace 100644 --- a/src/ripple/beast/hash/xxhasher.h +++ b/src/ripple/beast/hash/xxhasher.h @@ -22,8 +22,8 @@ #include #include -#include #include +#include namespace beast { @@ -31,7 +31,7 @@ class xxhasher { private: // requires 64-bit std::size_t - static_assert(sizeof(std::size_t)==8, ""); + static_assert(sizeof(std::size_t) == 8, ""); detail::XXH64_state_t state_; @@ -42,39 +42,37 @@ public: xxhasher() noexcept { - detail::XXH64_reset (&state_, 1); + detail::XXH64_reset(&state_, 1); } - template ::value>* = nullptr> - explicit - xxhasher (Seed seed) + template < + class Seed, + std::enable_if_t::value>* = nullptr> + explicit xxhasher(Seed seed) { - detail::XXH64_reset (&state_, seed); + detail::XXH64_reset(&state_, seed); } - template ::value>* = nullptr> - xxhasher (Seed seed, Seed) + template < + class Seed, + std::enable_if_t::value>* = nullptr> + xxhasher(Seed seed, Seed) { - detail::XXH64_reset (&state_, seed); + detail::XXH64_reset(&state_, seed); } void operator()(void const* key, std::size_t len) noexcept { - detail::XXH64_update (&state_, key, len); + detail::XXH64_update(&state_, key, len); } - explicit - operator std::size_t() noexcept + explicit operator std::size_t() noexcept { return detail::XXH64_digest(&state_); } }; -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Collector.h b/src/ripple/beast/insight/Collector.h index ec4fadadf3..a71b1092c8 100644 --- a/src/ripple/beast/insight/Collector.h +++ b/src/ripple/beast/insight/Collector.h @@ -44,7 +44,7 @@ namespace insight { class Collector { public: - using ptr = std::shared_ptr ; + using ptr = std::shared_ptr; virtual ~Collector() = 0; @@ -62,25 +62,29 @@ public: */ /** @{ */ template - Hook make_hook (Handler handler) + Hook + make_hook(Handler handler) { - return make_hook (HookImpl::HandlerType (handler)); + return make_hook(HookImpl::HandlerType(handler)); } - virtual Hook make_hook (HookImpl::HandlerType const& handler) = 0; + virtual Hook + make_hook(HookImpl::HandlerType const& handler) = 0; /** @} */ /** Create a counter with the specified name. @see Counter */ /** @{ */ - virtual Counter make_counter (std::string const& name) = 0; + virtual Counter + make_counter(std::string const& name) = 0; - Counter make_counter (std::string const& prefix, std::string const& name) + Counter + make_counter(std::string const& prefix, std::string const& name) { - if (prefix.empty ()) - return make_counter (name); - return make_counter (prefix + "." + name); + if (prefix.empty()) + return make_counter(name); + return make_counter(prefix + "." + name); } /** @} */ @@ -88,13 +92,15 @@ public: @see Event */ /** @{ */ - virtual Event make_event (std::string const& name) = 0; + virtual Event + make_event(std::string const& name) = 0; - Event make_event (std::string const& prefix, std::string const& name) + Event + make_event(std::string const& prefix, std::string const& name) { - if (prefix.empty ()) - return make_event (name); - return make_event (prefix + "." + name); + if (prefix.empty()) + return make_event(name); + return make_event(prefix + "." + name); } /** @} */ @@ -102,13 +108,15 @@ public: @see Gauge */ /** @{ */ - virtual Gauge make_gauge (std::string const& name) = 0; + virtual Gauge + make_gauge(std::string const& name) = 0; - Gauge make_gauge (std::string const& prefix, std::string const& name) + Gauge + make_gauge(std::string const& prefix, std::string const& name) { - if (prefix.empty ()) - return make_gauge (name); - return make_gauge (prefix + "." + name); + if (prefix.empty()) + return make_gauge(name); + return make_gauge(prefix + "." + name); } /** @} */ @@ -116,18 +124,20 @@ public: @see Meter */ /** @{ */ - virtual Meter make_meter (std::string const& name) = 0; + virtual Meter + make_meter(std::string const& name) = 0; - Meter make_meter (std::string const& prefix, std::string const& name) + Meter + make_meter(std::string const& prefix, std::string const& name) { - if (prefix.empty ()) - return make_meter (name); - return make_meter (prefix + "." + name); + if (prefix.empty()) + return make_meter(name); + return make_meter(prefix + "." + name); } /** @} */ }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Counter.h b/src/ripple/beast/insight/Counter.h index 8661a00306..069505e4ce 100644 --- a/src/ripple/beast/insight/Counter.h +++ b/src/ripple/beast/insight/Counter.h @@ -43,7 +43,7 @@ public: /** Create a null metric. A null metric reports no information. */ - Counter () + Counter() { } @@ -52,67 +52,66 @@ public: factory function in the Collector interface. @see Collector. */ - explicit Counter (std::shared_ptr const& impl) - : m_impl (impl) + explicit Counter(std::shared_ptr const& impl) : m_impl(impl) { } /** Increment the counter. */ /** @{ */ void - increment (value_type amount) const + increment(value_type amount) const { if (m_impl) - m_impl->increment (amount); + m_impl->increment(amount); } Counter const& - operator+= (value_type amount) const + operator+=(value_type amount) const { - increment (amount); + increment(amount); return *this; } Counter const& - operator-= (value_type amount) const + operator-=(value_type amount) const { - increment (-amount); + increment(-amount); return *this; } Counter const& - operator++ () const + operator++() const { - increment (1); + increment(1); return *this; } Counter const& - operator++ (int) const + operator++(int) const { - increment (1); + increment(1); return *this; } Counter const& - operator-- () const + operator--() const { - increment (-1); + increment(-1); return *this; } Counter const& - operator-- (int) const + operator--(int) const { - increment (-1); + increment(-1); return *this; } private: - std::shared_ptr m_impl; + std::shared_ptr m_impl; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/CounterImpl.h b/src/ripple/beast/insight/CounterImpl.h index 383032fdad..f369b32f77 100644 --- a/src/ripple/beast/insight/CounterImpl.h +++ b/src/ripple/beast/insight/CounterImpl.h @@ -28,17 +28,17 @@ namespace insight { class Counter; -class CounterImpl - : public std::enable_shared_from_this +class CounterImpl : public std::enable_shared_from_this { public: using value_type = std::int64_t; - virtual ~CounterImpl () = 0; - virtual void increment (value_type amount) = 0; + virtual ~CounterImpl() = 0; + virtual void + increment(value_type amount) = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Event.h b/src/ripple/beast/insight/Event.h index 200eadb7cb..7d2ee08d91 100644 --- a/src/ripple/beast/insight/Event.h +++ b/src/ripple/beast/insight/Event.h @@ -47,38 +47,40 @@ public: /** Create a null metric. A null metric reports no information. */ - Event () - { } + Event() + { + } /** Create the metric reference the specified implementation. Normally this won't be called directly. Instead, call the appropriate factory function in the Collector interface. @see Collector. */ - explicit Event (std::shared_ptr const& impl) - : m_impl (impl) - { } + explicit Event(std::shared_ptr const& impl) : m_impl(impl) + { + } /** Push an event notification. */ template void - notify (std::chrono::duration const& value) const + notify(std::chrono::duration const& value) const { using namespace std::chrono; if (m_impl) - m_impl->notify (date::ceil (value)); + m_impl->notify(date::ceil(value)); } - std::shared_ptr const& impl () const + std::shared_ptr const& + impl() const { return m_impl; } private: - std::shared_ptr m_impl; + std::shared_ptr m_impl; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/EventImpl.h b/src/ripple/beast/insight/EventImpl.h index 307615ba4d..5104d05c46 100644 --- a/src/ripple/beast/insight/EventImpl.h +++ b/src/ripple/beast/insight/EventImpl.h @@ -28,17 +28,17 @@ namespace insight { class Event; -class EventImpl - : public std::enable_shared_from_this +class EventImpl : public std::enable_shared_from_this { public: using value_type = std::chrono::milliseconds; - virtual ~EventImpl () = 0; - virtual void notify (value_type const& value) = 0; + virtual ~EventImpl() = 0; + virtual void + notify(value_type const& value) = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Gauge.h b/src/ripple/beast/insight/Gauge.h index d77c932e28..76f8408076 100644 --- a/src/ripple/beast/insight/Gauge.h +++ b/src/ripple/beast/insight/Gauge.h @@ -45,7 +45,7 @@ public: /** Create a null metric. A null metric reports no information. */ - Gauge () + Gauge() { } @@ -54,8 +54,7 @@ public: factory function in the Collector interface. @see Collector. */ - explicit Gauge (std::shared_ptr const& impl) - : m_impl (impl) + explicit Gauge(std::shared_ptr const& impl) : m_impl(impl) { } @@ -65,78 +64,84 @@ public: collection interval. */ /** @{ */ - void set (value_type value) const + void + set(value_type value) const { if (m_impl) - m_impl->set (value); + m_impl->set(value); } - Gauge const& operator= (value_type value) const - { set (value); return *this; } + Gauge const& + operator=(value_type value) const + { + set(value); + return *this; + } /** @} */ /** Adjust the value of the gauge. */ /** @{ */ - void increment (difference_type amount) const + void + increment(difference_type amount) const { if (m_impl) - m_impl->increment (amount); + m_impl->increment(amount); } Gauge const& - operator+= (difference_type amount) const + operator+=(difference_type amount) const { - increment (amount); + increment(amount); return *this; } Gauge const& - operator-= (difference_type amount) const + operator-=(difference_type amount) const { - increment (-amount); + increment(-amount); return *this; } Gauge const& - operator++ () const + operator++() const { - increment (1); + increment(1); return *this; } Gauge const& - operator++ (int) const + operator++(int) const { - increment (1); + increment(1); return *this; } Gauge const& - operator-- () const + operator--() const { - increment (-1); + increment(-1); return *this; } Gauge const& - operator-- (int) const + operator--(int) const { - increment (-1); + increment(-1); return *this; } /** @} */ - std::shared_ptr const& - impl () const + std::shared_ptr const& + impl() const { return m_impl; } private: - std::shared_ptr m_impl; + std::shared_ptr m_impl; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/GaugeImpl.h b/src/ripple/beast/insight/GaugeImpl.h index 07af0c9b54..78609fd432 100644 --- a/src/ripple/beast/insight/GaugeImpl.h +++ b/src/ripple/beast/insight/GaugeImpl.h @@ -28,19 +28,20 @@ namespace insight { class Gauge; -class GaugeImpl - : public std::enable_shared_from_this +class GaugeImpl : public std::enable_shared_from_this { public: using value_type = std::uint64_t; using difference_type = std::int64_t; - virtual ~GaugeImpl () = 0; - virtual void set (value_type value) = 0; - virtual void increment (difference_type amount) = 0; + virtual ~GaugeImpl() = 0; + virtual void + set(value_type value) = 0; + virtual void + increment(difference_type amount) = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Group.h b/src/ripple/beast/insight/Group.h index ffd52ccec0..a37656b81a 100644 --- a/src/ripple/beast/insight/Group.h +++ b/src/ripple/beast/insight/Group.h @@ -32,13 +32,14 @@ namespace insight { class Group : public Collector { public: - using ptr = std::shared_ptr ; + using ptr = std::shared_ptr; /** Returns the name of this group, for diagnostics. */ - virtual std::string const& name () const = 0; + virtual std::string const& + name() const = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Groups.h b/src/ripple/beast/insight/Groups.h index 67dc55d9ec..817ad87bd1 100644 --- a/src/ripple/beast/insight/Groups.h +++ b/src/ripple/beast/insight/Groups.h @@ -37,22 +37,22 @@ public: /** Find or create a new collector with a given name. */ /** @{ */ - virtual - Group::ptr const& - get (std::string const& name) = 0; + virtual Group::ptr const& + get(std::string const& name) = 0; Group::ptr const& - operator[] (std::string const& name) + operator[](std::string const& name) { - return get (name); + return get(name); } /** @} */ }; /** Create a group container that uses the specified collector. */ -std::unique_ptr make_Groups (Collector::ptr const& collector); +std::unique_ptr +make_Groups(Collector::ptr const& collector); -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Hook.h b/src/ripple/beast/insight/Hook.h index 55c4ebe37f..04647f4b63 100644 --- a/src/ripple/beast/insight/Hook.h +++ b/src/ripple/beast/insight/Hook.h @@ -34,28 +34,30 @@ public: /** Create a null hook. A null hook has no associated handler. */ - Hook () - { } + Hook() + { + } /** Create a hook referencing the specified implementation. Normally this won't be called directly. Instead, call the appropriate factory function in the Collector interface. @see Collector. */ - explicit Hook (std::shared_ptr const& impl) - : m_impl (impl) - { } + explicit Hook(std::shared_ptr const& impl) : m_impl(impl) + { + } - std::shared_ptr const& impl () const + std::shared_ptr const& + impl() const { return m_impl; } private: - std::shared_ptr m_impl; + std::shared_ptr m_impl; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/HookImpl.h b/src/ripple/beast/insight/HookImpl.h index d3c021d823..aca1f847fa 100644 --- a/src/ripple/beast/insight/HookImpl.h +++ b/src/ripple/beast/insight/HookImpl.h @@ -26,16 +26,15 @@ namespace beast { namespace insight { -class HookImpl - : public std::enable_shared_from_this +class HookImpl : public std::enable_shared_from_this { public: - using HandlerType = std::function ; + using HandlerType = std::function; - virtual ~HookImpl () = 0; + virtual ~HookImpl() = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/Insight.h b/src/ripple/beast/insight/Insight.h index c8f8f677c4..f34f731748 100644 --- a/src/ripple/beast/insight/Insight.h +++ b/src/ripple/beast/insight/Insight.h @@ -20,6 +20,7 @@ #ifndef BEAST_INSIGHT_H_INCLUDED #define BEAST_INSIGHT_H_INCLUDED +#include #include #include #include @@ -30,7 +31,6 @@ #include #include #include -#include #include #include diff --git a/src/ripple/beast/insight/Meter.h b/src/ripple/beast/insight/Meter.h index bdd6591605..376dcae052 100644 --- a/src/ripple/beast/insight/Meter.h +++ b/src/ripple/beast/insight/Meter.h @@ -42,55 +42,61 @@ public: /** Create a null metric. A null metric reports no information. */ - Meter () - { } + Meter() + { + } /** Create the metric reference the specified implementation. Normally this won't be called directly. Instead, call the appropriate factory function in the Collector interface. @see Collector. */ - explicit Meter (std::shared_ptr const& impl) - : m_impl (impl) - { } + explicit Meter(std::shared_ptr const& impl) : m_impl(impl) + { + } /** Increment the meter. */ /** @{ */ - void increment (value_type amount) const + void + increment(value_type amount) const { if (m_impl) - m_impl->increment (amount); + m_impl->increment(amount); } - Meter const& operator+= (value_type amount) const + Meter const& + operator+=(value_type amount) const { - increment (amount); + increment(amount); return *this; } - Meter const& operator++ () const + Meter const& + operator++() const { - increment (1); + increment(1); return *this; } - Meter const& operator++ (int) const + Meter const& + operator++(int) const { - increment (1); + increment(1); return *this; } /** @} */ - std::shared_ptr const& impl () const + std::shared_ptr const& + impl() const { return m_impl; } private: - std::shared_ptr m_impl; + std::shared_ptr m_impl; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/MeterImpl.h b/src/ripple/beast/insight/MeterImpl.h index b8812fb238..846dbe19a8 100644 --- a/src/ripple/beast/insight/MeterImpl.h +++ b/src/ripple/beast/insight/MeterImpl.h @@ -28,17 +28,17 @@ namespace insight { class Meter; -class MeterImpl - : public std::enable_shared_from_this +class MeterImpl : public std::enable_shared_from_this { public: using value_type = std::uint64_t; - virtual ~MeterImpl () = 0; - virtual void increment (value_type amount) = 0; + virtual ~MeterImpl() = 0; + virtual void + increment(value_type amount) = 0; }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/NullCollector.h b/src/ripple/beast/insight/NullCollector.h index 36a78c17a7..db98153836 100644 --- a/src/ripple/beast/insight/NullCollector.h +++ b/src/ripple/beast/insight/NullCollector.h @@ -31,10 +31,11 @@ class NullCollector : public Collector public: explicit NullCollector() = default; - static std::shared_ptr New (); + static std::shared_ptr + New(); }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/StatsDCollector.h b/src/ripple/beast/insight/StatsDCollector.h index b96731af5a..d95aee04d0 100644 --- a/src/ripple/beast/insight/StatsDCollector.h +++ b/src/ripple/beast/insight/StatsDCollector.h @@ -22,8 +22,8 @@ #include -#include #include +#include namespace beast { namespace insight { @@ -42,13 +42,13 @@ public: @param prefix A string pre-pended before each metric name. @param journal Destination for logging output. */ - static - std::shared_ptr - New (IP::Endpoint const& address, - std::string const& prefix, Journal journal); + static std::shared_ptr + New(IP::Endpoint const& address, + std::string const& prefix, + Journal journal); }; -} -} +} // namespace insight +} // namespace beast #endif diff --git a/src/ripple/beast/insight/impl/Collector.cpp b/src/ripple/beast/insight/impl/Collector.cpp index 69ff2b2361..e0f0e6ec64 100644 --- a/src/ripple/beast/insight/impl/Collector.cpp +++ b/src/ripple/beast/insight/impl/Collector.cpp @@ -24,4 +24,4 @@ namespace insight { Collector::~Collector() = default; } -} +} // namespace beast diff --git a/src/ripple/beast/insight/impl/Groups.cpp b/src/ripple/beast/insight/impl/Groups.cpp index 0edca6d67e..7877034b97 100644 --- a/src/ripple/beast/insight/impl/Groups.cpp +++ b/src/ripple/beast/insight/impl/Groups.cpp @@ -17,72 +17,75 @@ */ //============================================================================== +#include #include #include -#include -#include #include +#include namespace beast { namespace insight { namespace detail { -class GroupImp - : public std::enable_shared_from_this - , public Group +class GroupImp : public std::enable_shared_from_this, public Group { public: - std::string const m_name; Collector::ptr m_collector; - GroupImp (std::string const& name_, - Collector::ptr const& collector) - : m_name (name_) - , m_collector (collector) + GroupImp(std::string const& name_, Collector::ptr const& collector) + : m_name(name_), m_collector(collector) { } ~GroupImp() = default; - std::string const& name () const override + std::string const& + name() const override { return m_name; } - std::string make_name (std::string const& name) + std::string + make_name(std::string const& name) { return m_name + "." + name; } - Hook make_hook (HookImpl::HandlerType const& handler) override + Hook + make_hook(HookImpl::HandlerType const& handler) override { - return m_collector->make_hook (handler); + return m_collector->make_hook(handler); } - Counter make_counter (std::string const& name) override + Counter + make_counter(std::string const& name) override { - return m_collector->make_counter (make_name (name)); + return m_collector->make_counter(make_name(name)); } - Event make_event (std::string const& name) override + Event + make_event(std::string const& name) override { - return m_collector->make_event (make_name (name)); + return m_collector->make_event(make_name(name)); } - Gauge make_gauge (std::string const& name) override + Gauge + make_gauge(std::string const& name) override { - return m_collector->make_gauge (make_name (name)); + return m_collector->make_gauge(make_name(name)); } - Meter make_meter (std::string const& name) override + Meter + make_meter(std::string const& name) override { - return m_collector->make_meter (make_name (name)); + return m_collector->make_meter(make_name(name)); } private: - GroupImp& operator= (GroupImp const&); + GroupImp& + operator=(GroupImp const&); }; //------------------------------------------------------------------------------ @@ -90,39 +93,41 @@ private: class GroupsImp : public Groups { public: - using Items = std::unordered_map , uhash <>>; + using Items = + std::unordered_map, uhash<>>; Collector::ptr m_collector; Items m_items; - explicit GroupsImp (Collector::ptr const& collector) - : m_collector (collector) + explicit GroupsImp(Collector::ptr const& collector) : m_collector(collector) { } ~GroupsImp() = default; - Group::ptr const& get (std::string const& name) override + Group::ptr const& + get(std::string const& name) override { - std::pair result ( - m_items.emplace (name, Group::ptr ())); - Group::ptr& group (result.first->second); + std::pair result( + m_items.emplace(name, Group::ptr())); + Group::ptr& group(result.first->second); if (result.second) - group = std::make_shared (name, m_collector); + group = std::make_shared(name, m_collector); return group; } }; -} +} // namespace detail //------------------------------------------------------------------------------ Groups::~Groups() = default; -std::unique_ptr make_Groups (Collector::ptr const& collector) +std::unique_ptr +make_Groups(Collector::ptr const& collector) { - return std::make_unique (collector); + return std::make_unique(collector); } -} -} +} // namespace insight +} // namespace beast diff --git a/src/ripple/beast/insight/impl/Hook.cpp b/src/ripple/beast/insight/impl/Hook.cpp index 157d097768..53ae6475aa 100644 --- a/src/ripple/beast/insight/impl/Hook.cpp +++ b/src/ripple/beast/insight/impl/Hook.cpp @@ -24,4 +24,4 @@ namespace insight { HookImpl::~HookImpl() = default; } -} +} // namespace beast diff --git a/src/ripple/beast/insight/impl/Metric.cpp b/src/ripple/beast/insight/impl/Metric.cpp index 60ae1a7e7c..aa38e2f816 100644 --- a/src/ripple/beast/insight/impl/Metric.cpp +++ b/src/ripple/beast/insight/impl/Metric.cpp @@ -32,5 +32,5 @@ EventImpl::~EventImpl() = default; GaugeImpl::~GaugeImpl() = default; MeterImpl::~MeterImpl() = default; -} -} +} // namespace insight +} // namespace beast diff --git a/src/ripple/beast/insight/impl/NullCollector.cpp b/src/ripple/beast/insight/impl/NullCollector.cpp index be2d612f74..b859de552a 100644 --- a/src/ripple/beast/insight/impl/NullCollector.cpp +++ b/src/ripple/beast/insight/impl/NullCollector.cpp @@ -30,7 +30,8 @@ public: explicit NullHookImpl() = default; private: - NullHookImpl& operator= (NullHookImpl const&); + NullHookImpl& + operator=(NullHookImpl const&); }; //------------------------------------------------------------------------------ @@ -40,12 +41,13 @@ class NullCounterImpl : public CounterImpl public: explicit NullCounterImpl() = default; - void increment (value_type) override + void increment(value_type) override { } private: - NullCounterImpl& operator= (NullCounterImpl const&); + NullCounterImpl& + operator=(NullCounterImpl const&); }; //------------------------------------------------------------------------------ @@ -55,12 +57,14 @@ class NullEventImpl : public EventImpl public: explicit NullEventImpl() = default; - void notify (value_type const&) override + void + notify(value_type const&) override { } private: - NullEventImpl& operator= (NullEventImpl const&); + NullEventImpl& + operator=(NullEventImpl const&); }; //------------------------------------------------------------------------------ @@ -70,16 +74,17 @@ class NullGaugeImpl : public GaugeImpl public: explicit NullGaugeImpl() = default; - void set (value_type) override + void set(value_type) override { } - void increment (difference_type) override + void increment(difference_type) override { } private: - NullGaugeImpl& operator= (NullGaugeImpl const&); + NullGaugeImpl& + operator=(NullGaugeImpl const&); }; //------------------------------------------------------------------------------ @@ -89,12 +94,13 @@ class NullMeterImpl : public MeterImpl public: explicit NullMeterImpl() = default; - void increment (value_type) override + void increment(value_type) override { } private: - NullMeterImpl& operator= (NullMeterImpl const&); + NullMeterImpl& + operator=(NullMeterImpl const&); }; //------------------------------------------------------------------------------ @@ -107,40 +113,46 @@ public: ~NullCollectorImp() = default; - Hook make_hook (HookImpl::HandlerType const&) override + Hook + make_hook(HookImpl::HandlerType const&) override { - return Hook (std::make_shared ()); + return Hook(std::make_shared()); } - Counter make_counter (std::string const&) override + Counter + make_counter(std::string const&) override { - return Counter (std::make_shared ()); + return Counter(std::make_shared()); } - Event make_event (std::string const&) override + Event + make_event(std::string const&) override { - return Event (std::make_shared ()); + return Event(std::make_shared()); } - Gauge make_gauge (std::string const&) override + Gauge + make_gauge(std::string const&) override { - return Gauge (std::make_shared ()); + return Gauge(std::make_shared()); } - Meter make_meter (std::string const&) override + Meter + make_meter(std::string const&) override { - return Meter (std::make_shared ()); + return Meter(std::make_shared()); } }; -} +} // namespace detail //------------------------------------------------------------------------------ -std::shared_ptr NullCollector::New () +std::shared_ptr +NullCollector::New() { - return std::make_shared (); + return std::make_shared(); } -} -} +} // namespace insight +} // namespace beast diff --git a/src/ripple/beast/insight/impl/StatsDCollector.cpp b/src/ripple/beast/insight/impl/StatsDCollector.cpp index 71eda6479f..288ebfbe45 100644 --- a/src/ripple/beast/insight/impl/StatsDCollector.cpp +++ b/src/ripple/beast/insight/impl/StatsDCollector.cpp @@ -17,14 +17,14 @@ */ //============================================================================== -#include -#include +#include #include #include #include +#include #include #include -#include +#include #include #include #include @@ -49,60 +49,66 @@ class StatsDCollectorImp; //------------------------------------------------------------------------------ -class StatsDMetricBase : public List ::Node +class StatsDMetricBase : public List::Node { public: - virtual void do_process () = 0; + virtual void + do_process() = 0; virtual ~StatsDMetricBase() = default; StatsDMetricBase() = default; StatsDMetricBase(StatsDMetricBase const&) = delete; - StatsDMetricBase& operator=(StatsDMetricBase const&) = delete; + StatsDMetricBase& + operator=(StatsDMetricBase const&) = delete; }; //------------------------------------------------------------------------------ -class StatsDHookImpl - : public HookImpl - , public StatsDMetricBase +class StatsDHookImpl : public HookImpl, public StatsDMetricBase { public: - StatsDHookImpl ( + StatsDHookImpl( HandlerType const& handler, - std::shared_ptr const& impl); + std::shared_ptr const& impl); - ~StatsDHookImpl () override; + ~StatsDHookImpl() override; - void do_process () override; + void + do_process() override; private: - StatsDHookImpl& operator= (StatsDHookImpl const&); + StatsDHookImpl& + operator=(StatsDHookImpl const&); - std::shared_ptr m_impl; + std::shared_ptr m_impl; HandlerType m_handler; }; //------------------------------------------------------------------------------ -class StatsDCounterImpl - : public CounterImpl - , public StatsDMetricBase +class StatsDCounterImpl : public CounterImpl, public StatsDMetricBase { public: - StatsDCounterImpl (std::string const& name, - std::shared_ptr const& impl); + StatsDCounterImpl( + std::string const& name, + std::shared_ptr const& impl); - ~StatsDCounterImpl () override; + ~StatsDCounterImpl() override; - void increment (CounterImpl::value_type amount) override; + void + increment(CounterImpl::value_type amount) override; - void flush (); - void do_increment (CounterImpl::value_type amount); - void do_process () override; + void + flush(); + void + do_increment(CounterImpl::value_type amount); + void + do_process() override; private: - StatsDCounterImpl& operator= (StatsDCounterImpl const&); + StatsDCounterImpl& + operator=(StatsDCounterImpl const&); - std::shared_ptr m_impl; + std::shared_ptr m_impl; std::string m_name; CounterImpl::value_type m_value; bool m_dirty; @@ -110,51 +116,61 @@ private: //------------------------------------------------------------------------------ -class StatsDEventImpl - : public EventImpl +class StatsDEventImpl : public EventImpl { public: - StatsDEventImpl (std::string const& name, - std::shared_ptr const& impl); + StatsDEventImpl( + std::string const& name, + std::shared_ptr const& impl); - ~StatsDEventImpl () = default; + ~StatsDEventImpl() = default; - void notify (EventImpl::value_type const& value) override; + void + notify(EventImpl::value_type const& value) override; - void do_notify (EventImpl::value_type const& value); - void do_process (); + void + do_notify(EventImpl::value_type const& value); + void + do_process(); private: - StatsDEventImpl& operator= (StatsDEventImpl const&); + StatsDEventImpl& + operator=(StatsDEventImpl const&); - std::shared_ptr m_impl; + std::shared_ptr m_impl; std::string m_name; }; //------------------------------------------------------------------------------ -class StatsDGaugeImpl - : public GaugeImpl - , public StatsDMetricBase +class StatsDGaugeImpl : public GaugeImpl, public StatsDMetricBase { public: - StatsDGaugeImpl (std::string const& name, - std::shared_ptr const& impl); + StatsDGaugeImpl( + std::string const& name, + std::shared_ptr const& impl); - ~StatsDGaugeImpl () override; + ~StatsDGaugeImpl() override; - void set (GaugeImpl::value_type value) override; - void increment (GaugeImpl::difference_type amount) override; + void + set(GaugeImpl::value_type value) override; + void + increment(GaugeImpl::difference_type amount) override; - void flush (); - void do_set (GaugeImpl::value_type value); - void do_increment (GaugeImpl::difference_type amount); - void do_process () override; + void + flush(); + void + do_set(GaugeImpl::value_type value); + void + do_increment(GaugeImpl::difference_type amount); + void + do_process() override; private: - StatsDGaugeImpl& operator= (StatsDGaugeImpl const&); + StatsDGaugeImpl& + operator=(StatsDGaugeImpl const&); - std::shared_ptr m_impl; + std::shared_ptr m_impl; std::string m_name; GaugeImpl::value_type m_last_value; GaugeImpl::value_type m_value; @@ -163,26 +179,30 @@ private: //------------------------------------------------------------------------------ -class StatsDMeterImpl - : public MeterImpl - , public StatsDMetricBase +class StatsDMeterImpl : public MeterImpl, public StatsDMetricBase { public: - explicit StatsDMeterImpl (std::string const& name, - std::shared_ptr const& impl); + explicit StatsDMeterImpl( + std::string const& name, + std::shared_ptr const& impl); - ~StatsDMeterImpl () override; + ~StatsDMeterImpl() override; - void increment (MeterImpl::value_type amount) override; + void + increment(MeterImpl::value_type amount) override; - void flush (); - void do_increment (MeterImpl::value_type amount); - void do_process () override; + void + flush(); + void + do_increment(MeterImpl::value_type amount); + void + do_process() override; private: - StatsDMeterImpl& operator= (StatsDMeterImpl const&); + StatsDMeterImpl& + operator=(StatsDMeterImpl const&); - std::shared_ptr m_impl; + std::shared_ptr m_impl; std::string m_name; MeterImpl::value_type m_value; bool m_dirty; @@ -191,13 +211,12 @@ private: //------------------------------------------------------------------------------ class StatsDCollectorImp - : public StatsDCollector - , public std::enable_shared_from_this + : public StatsDCollector, + public std::enable_shared_from_this { private: - enum - { - //max_packet_size = 484 + enum { + // max_packet_size = 484 max_packet_size = 1472 }; @@ -205,120 +224,133 @@ private: IP::Endpoint m_address; std::string m_prefix; boost::asio::io_service m_io_service; - boost::optional m_work; + boost::optional m_work; boost::asio::io_service::strand m_strand; boost::asio::basic_waitable_timer m_timer; boost::asio::ip::udp::socket m_socket; - std::deque m_data; + std::deque m_data; std::recursive_mutex metricsLock_; - List metrics_; + List metrics_; // Must come last for order of init std::thread m_thread; - static boost::asio::ip::udp::endpoint to_endpoint ( - IP::Endpoint const &ep) + static boost::asio::ip::udp::endpoint + to_endpoint(IP::Endpoint const& ep) { - return boost::asio::ip::udp::endpoint (ep.address(), ep.port()); + return boost::asio::ip::udp::endpoint(ep.address(), ep.port()); } public: - StatsDCollectorImp ( + StatsDCollectorImp( IP::Endpoint const& address, std::string const& prefix, Journal journal) - : m_journal (journal) - , m_address (address) - , m_prefix (prefix) - , m_work (std::ref (m_io_service)) - , m_strand (m_io_service) - , m_timer (m_io_service) - , m_socket (m_io_service) - , m_thread (&StatsDCollectorImp::run, this) + : m_journal(journal) + , m_address(address) + , m_prefix(prefix) + , m_work(std::ref(m_io_service)) + , m_strand(m_io_service) + , m_timer(m_io_service) + , m_socket(m_io_service) + , m_thread(&StatsDCollectorImp::run, this) { } - ~StatsDCollectorImp () override + ~StatsDCollectorImp() override { boost::system::error_code ec; - m_timer.cancel (ec); + m_timer.cancel(ec); m_work = boost::none; - m_thread.join (); + m_thread.join(); } - Hook make_hook (HookImpl::HandlerType const& handler) override + Hook + make_hook(HookImpl::HandlerType const& handler) override { - return Hook (std::make_shared ( - handler, shared_from_this ())); + return Hook(std::make_shared( + handler, shared_from_this())); } - Counter make_counter (std::string const& name) override + Counter + make_counter(std::string const& name) override { - return Counter (std::make_shared ( - name, shared_from_this ())); + return Counter(std::make_shared( + name, shared_from_this())); } - Event make_event (std::string const& name) override + Event + make_event(std::string const& name) override { - return Event (std::make_shared ( - name, shared_from_this ())); + return Event(std::make_shared( + name, shared_from_this())); } - Gauge make_gauge (std::string const& name) override + Gauge + make_gauge(std::string const& name) override { - return Gauge (std::make_shared ( - name, shared_from_this ())); + return Gauge(std::make_shared( + name, shared_from_this())); } - Meter make_meter (std::string const& name) override + Meter + make_meter(std::string const& name) override { - return Meter (std::make_shared ( - name, shared_from_this ())); + return Meter(std::make_shared( + name, shared_from_this())); } //-------------------------------------------------------------------------- - void add (StatsDMetricBase& metric) + void + add(StatsDMetricBase& metric) { std::lock_guard _(metricsLock_); - metrics_.push_back (metric); + metrics_.push_back(metric); } - void remove (StatsDMetricBase& metric) + void + remove(StatsDMetricBase& metric) { std::lock_guard _(metricsLock_); - metrics_.erase (metrics_.iterator_to (metric)); + metrics_.erase(metrics_.iterator_to(metric)); } //-------------------------------------------------------------------------- - boost::asio::io_service& get_io_service () + boost::asio::io_service& + get_io_service() { return m_io_service; } - std::string const& prefix () const + std::string const& + prefix() const { return m_prefix; } - void do_post_buffer (std::string const& buffer) + void + do_post_buffer(std::string const& buffer) { - m_data.emplace_back (buffer); + m_data.emplace_back(buffer); } - void post_buffer (std::string&& buffer) + void + post_buffer(std::string&& buffer) { - m_io_service.dispatch (m_strand.wrap (std::bind ( - &StatsDCollectorImp::do_post_buffer, this, - std::move (buffer)))); + m_io_service.dispatch(m_strand.wrap(std::bind( + &StatsDCollectorImp::do_post_buffer, this, std::move(buffer)))); } // The keepAlive parameter makes sure the buffers sent to // boost::asio::async_send do not go away until the call is finished - void on_send (std::shared_ptr> /*keepAlive*/, - boost::system::error_code ec, std::size_t) + void + on_send( + std::shared_ptr> /*keepAlive*/, + boost::system::error_code ec, + std::size_t) { if (ec == boost::asio::error::operation_aborted) return; @@ -331,14 +363,16 @@ public: } } - void log (std::vector const& buffers) + void + log(std::vector const& buffers) { (void)buffers; #if BEAST_STATSDCOLLECTOR_TRACING_ENABLED for (auto const& buffer : buffers) { - std::string const s (boost::asio::buffer_cast (buffer), - boost::asio::buffer_size (buffer)); + std::string const s( + boost::asio::buffer_cast(buffer), + boost::asio::buffer_size(buffer)); std::cerr << s; } std::cerr << '\n'; @@ -346,61 +380,71 @@ public: } // Send what we have - void send_buffers () + void + send_buffers() { - if (m_data.empty ()) + if (m_data.empty()) return; // Break up the array of strings into blocks // that each fit into one UDP packet. // - std::vector buffers; - buffers.reserve (m_data.size ()); - std::size_t size (0); + std::vector buffers; + buffers.reserve(m_data.size()); + std::size_t size(0); auto keepAlive = - std::make_shared>(std::move (m_data)); - m_data.clear (); + std::make_shared>(std::move(m_data)); + m_data.clear(); for (auto const& s : *keepAlive) { - std::size_t const length (s.size ()); - assert (! s.empty ()); - if (! buffers.empty () && (size + length) > max_packet_size) + std::size_t const length(s.size()); + assert(!s.empty()); + if (!buffers.empty() && (size + length) > max_packet_size) { - log (buffers); - m_socket.async_send (buffers, std::bind ( - &StatsDCollectorImp::on_send, this, keepAlive, + log(buffers); + m_socket.async_send( + buffers, + std::bind( + &StatsDCollectorImp::on_send, + this, + keepAlive, std::placeholders::_1, - std::placeholders::_2)); - buffers.clear (); + std::placeholders::_2)); + buffers.clear(); size = 0; } - buffers.emplace_back (&s[0], length); + buffers.emplace_back(&s[0], length); size += length; } - if (! buffers.empty ()) + if (!buffers.empty()) { - log (buffers); - m_socket.async_send (buffers, std::bind ( - &StatsDCollectorImp::on_send, this, keepAlive, + log(buffers); + m_socket.async_send( + buffers, + std::bind( + &StatsDCollectorImp::on_send, + this, + keepAlive, std::placeholders::_1, - std::placeholders::_2)); + std::placeholders::_2)); } } - void set_timer () + void + set_timer() { using namespace std::chrono_literals; m_timer.expires_from_now(1s); - m_timer.async_wait (std::bind ( - &StatsDCollectorImp::on_timer, this, - std::placeholders::_1)); + m_timer.async_wait(std::bind( + &StatsDCollectorImp::on_timer, this, std::placeholders::_1)); } - void on_timer (boost::system::error_code ec) + void + on_timer(boost::system::error_code ec) { if (ec == boost::asio::error::operation_aborted) return; @@ -417,185 +461,182 @@ public: for (auto& m : metrics_) m.do_process(); - send_buffers (); + send_buffers(); - set_timer (); + set_timer(); } - void run () + void + run() { boost::system::error_code ec; - if (m_socket.connect (to_endpoint (m_address), ec)) + if (m_socket.connect(to_endpoint(m_address), ec)) { if (auto stream = m_journal.error()) stream << "Connect failed: " << ec.message(); return; } - set_timer (); + set_timer(); - m_io_service.run (); + m_io_service.run(); - m_socket.shutdown ( - boost::asio::ip::udp::socket::shutdown_send, ec); + m_socket.shutdown(boost::asio::ip::udp::socket::shutdown_send, ec); - m_socket.close (); + m_socket.close(); - m_io_service.poll (); + m_io_service.poll(); } }; //------------------------------------------------------------------------------ -StatsDHookImpl::StatsDHookImpl (HandlerType const& handler, - std::shared_ptr const& impl) - : m_impl (impl) - , m_handler (handler) +StatsDHookImpl::StatsDHookImpl( + HandlerType const& handler, + std::shared_ptr const& impl) + : m_impl(impl), m_handler(handler) { - m_impl->add (*this); + m_impl->add(*this); } -StatsDHookImpl::~StatsDHookImpl () +StatsDHookImpl::~StatsDHookImpl() { - m_impl->remove (*this); + m_impl->remove(*this); } -void StatsDHookImpl::do_process () +void +StatsDHookImpl::do_process() { - m_handler (); + m_handler(); } //------------------------------------------------------------------------------ -StatsDCounterImpl::StatsDCounterImpl (std::string const& name, - std::shared_ptr const& impl) - : m_impl (impl) - , m_name (name) - , m_value (0) - , m_dirty (false) +StatsDCounterImpl::StatsDCounterImpl( + std::string const& name, + std::shared_ptr const& impl) + : m_impl(impl), m_name(name), m_value(0), m_dirty(false) { - m_impl->add (*this); + m_impl->add(*this); } -StatsDCounterImpl::~StatsDCounterImpl () +StatsDCounterImpl::~StatsDCounterImpl() { - m_impl->remove (*this); + m_impl->remove(*this); } -void StatsDCounterImpl::increment (CounterImpl::value_type amount) +void +StatsDCounterImpl::increment(CounterImpl::value_type amount) { - m_impl->get_io_service().dispatch (std::bind ( + m_impl->get_io_service().dispatch(std::bind( &StatsDCounterImpl::do_increment, - std::static_pointer_cast ( - shared_from_this ()), amount)); + std::static_pointer_cast(shared_from_this()), + amount)); } -void StatsDCounterImpl::flush () +void +StatsDCounterImpl::flush() { if (m_dirty) { m_dirty = false; std::stringstream ss; - ss << - m_impl->prefix() << "." << - m_name << ":" << - m_value << "|c" << - "\n"; + ss << m_impl->prefix() << "." << m_name << ":" << m_value << "|c" + << "\n"; m_value = 0; - m_impl->post_buffer (ss.str ()); + m_impl->post_buffer(ss.str()); } } -void StatsDCounterImpl::do_increment (CounterImpl::value_type amount) +void +StatsDCounterImpl::do_increment(CounterImpl::value_type amount) { m_value += amount; m_dirty = true; } -void StatsDCounterImpl::do_process () +void +StatsDCounterImpl::do_process() { - flush (); + flush(); } //------------------------------------------------------------------------------ -StatsDEventImpl::StatsDEventImpl (std::string const& name, - std::shared_ptr const& impl) - : m_impl (impl) - , m_name (name) +StatsDEventImpl::StatsDEventImpl( + std::string const& name, + std::shared_ptr const& impl) + : m_impl(impl), m_name(name) { } -void StatsDEventImpl::notify (EventImpl::value_type const& value) +void +StatsDEventImpl::notify(EventImpl::value_type const& value) { - m_impl->get_io_service().dispatch (std::bind ( + m_impl->get_io_service().dispatch(std::bind( &StatsDEventImpl::do_notify, - std::static_pointer_cast ( - shared_from_this ()), value)); + std::static_pointer_cast(shared_from_this()), + value)); } -void StatsDEventImpl::do_notify (EventImpl::value_type const& value) +void +StatsDEventImpl::do_notify(EventImpl::value_type const& value) { std::stringstream ss; - ss << - m_impl->prefix() << "." << - m_name << ":" << - value.count() << "|ms" << - "\n"; - m_impl->post_buffer (ss.str ()); + ss << m_impl->prefix() << "." << m_name << ":" << value.count() << "|ms" + << "\n"; + m_impl->post_buffer(ss.str()); } //------------------------------------------------------------------------------ -StatsDGaugeImpl::StatsDGaugeImpl (std::string const& name, - std::shared_ptr const& impl) - : m_impl (impl) - , m_name (name) - , m_last_value (0) - , m_value (0) - , m_dirty (false) +StatsDGaugeImpl::StatsDGaugeImpl( + std::string const& name, + std::shared_ptr const& impl) + : m_impl(impl), m_name(name), m_last_value(0), m_value(0), m_dirty(false) { - m_impl->add (*this); + m_impl->add(*this); } -StatsDGaugeImpl::~StatsDGaugeImpl () +StatsDGaugeImpl::~StatsDGaugeImpl() { - m_impl->remove (*this); + m_impl->remove(*this); } -void StatsDGaugeImpl::set (GaugeImpl::value_type value) +void +StatsDGaugeImpl::set(GaugeImpl::value_type value) { - m_impl->get_io_service().dispatch (std::bind ( + m_impl->get_io_service().dispatch(std::bind( &StatsDGaugeImpl::do_set, - std::static_pointer_cast ( - shared_from_this ()), value)); + std::static_pointer_cast(shared_from_this()), + value)); } -void StatsDGaugeImpl::increment (GaugeImpl::difference_type amount) +void +StatsDGaugeImpl::increment(GaugeImpl::difference_type amount) { - m_impl->get_io_service().dispatch (std::bind ( + m_impl->get_io_service().dispatch(std::bind( &StatsDGaugeImpl::do_increment, - std::static_pointer_cast ( - shared_from_this ()), amount)); + std::static_pointer_cast(shared_from_this()), + amount)); } -void StatsDGaugeImpl::flush () +void +StatsDGaugeImpl::flush() { if (m_dirty) { m_dirty = false; std::stringstream ss; - ss << - m_impl->prefix() << "." << - m_name << ":" << - m_value << "|g" << - "\n"; - m_impl->post_buffer (ss.str ()); + ss << m_impl->prefix() << "." << m_name << ":" << m_value << "|g" + << "\n"; + m_impl->post_buffer(ss.str()); } } -void StatsDGaugeImpl::do_set (GaugeImpl::value_type value) +void +StatsDGaugeImpl::do_set(GaugeImpl::value_type value) { m_value = value; @@ -606,96 +647,100 @@ void StatsDGaugeImpl::do_set (GaugeImpl::value_type value) } } -void StatsDGaugeImpl::do_increment (GaugeImpl::difference_type amount) +void +StatsDGaugeImpl::do_increment(GaugeImpl::difference_type amount) { - GaugeImpl::value_type value (m_value); + GaugeImpl::value_type value(m_value); if (amount > 0) { - GaugeImpl::value_type const d ( - static_cast (amount)); + GaugeImpl::value_type const d( + static_cast(amount)); value += - (d >= std::numeric_limits ::max() - m_value) - ? std::numeric_limits ::max() - m_value + (d >= std::numeric_limits::max() - m_value) + ? std::numeric_limits::max() - m_value : d; } else if (amount < 0) { - GaugeImpl::value_type const d ( - static_cast (-amount)); + GaugeImpl::value_type const d( + static_cast(-amount)); value = (d >= value) ? 0 : value - d; } - do_set (value); + do_set(value); } -void StatsDGaugeImpl::do_process () +void +StatsDGaugeImpl::do_process() { - flush (); + flush(); } //------------------------------------------------------------------------------ -StatsDMeterImpl::StatsDMeterImpl (std::string const& name, - std::shared_ptr const& impl) - : m_impl (impl) - , m_name (name) - , m_value (0) - , m_dirty (false) +StatsDMeterImpl::StatsDMeterImpl( + std::string const& name, + std::shared_ptr const& impl) + : m_impl(impl), m_name(name), m_value(0), m_dirty(false) { - m_impl->add (*this); + m_impl->add(*this); } -StatsDMeterImpl::~StatsDMeterImpl () +StatsDMeterImpl::~StatsDMeterImpl() { - m_impl->remove (*this); + m_impl->remove(*this); } -void StatsDMeterImpl::increment (MeterImpl::value_type amount) +void +StatsDMeterImpl::increment(MeterImpl::value_type amount) { - m_impl->get_io_service().dispatch (std::bind ( + m_impl->get_io_service().dispatch(std::bind( &StatsDMeterImpl::do_increment, - std::static_pointer_cast ( - shared_from_this ()), amount)); + std::static_pointer_cast(shared_from_this()), + amount)); } -void StatsDMeterImpl::flush () +void +StatsDMeterImpl::flush() { if (m_dirty) { m_dirty = false; std::stringstream ss; - ss << - m_impl->prefix() << "." << - m_name << ":" << - m_value << "|m" << - "\n"; + ss << m_impl->prefix() << "." << m_name << ":" << m_value << "|m" + << "\n"; m_value = 0; - m_impl->post_buffer (ss.str ()); + m_impl->post_buffer(ss.str()); } } -void StatsDMeterImpl::do_increment (MeterImpl::value_type amount) +void +StatsDMeterImpl::do_increment(MeterImpl::value_type amount) { m_value += amount; m_dirty = true; } -void StatsDMeterImpl::do_process () +void +StatsDMeterImpl::do_process() { - flush (); + flush(); } -} +} // namespace detail //------------------------------------------------------------------------------ -std::shared_ptr StatsDCollector::New ( - IP::Endpoint const& address, std::string const& prefix, Journal journal) +std::shared_ptr +StatsDCollector::New( + IP::Endpoint const& address, + std::string const& prefix, + Journal journal) { - return std::make_shared ( + return std::make_shared( address, prefix, journal); } -} -} +} // namespace insight +} // namespace beast diff --git a/src/ripple/beast/net/IPAddress.h b/src/ripple/beast/net/IPAddress.h index ee473ba1a1..1dd71f2e86 100644 --- a/src/ripple/beast/net/IPAddress.h +++ b/src/ripple/beast/net/IPAddress.h @@ -20,17 +20,17 @@ #ifndef BEAST_NET_IPADDRESS_H_INCLUDED #define BEAST_NET_IPADDRESS_H_INCLUDED -#include -#include #include #include -#include +#include +#include #include +#include #include #include #include -#include #include +#include #include //------------------------------------------------------------------------------ @@ -41,58 +41,48 @@ namespace IP { using Address = boost::asio::ip::address; /** Returns the address represented as a string. */ -inline -std::string -to_string (Address const& addr) +inline std::string +to_string(Address const& addr) { - return addr.to_string (); + return addr.to_string(); } /** Returns `true` if this is a loopback address. */ -inline -bool -is_loopback (Address const& addr) +inline bool +is_loopback(Address const& addr) { return addr.is_loopback(); } /** Returns `true` if the address is unspecified. */ -inline -bool -is_unspecified (Address const& addr) +inline bool +is_unspecified(Address const& addr) { return addr.is_unspecified(); } /** Returns `true` if the address is a multicast address. */ -inline -bool -is_multicast (Address const& addr) +inline bool +is_multicast(Address const& addr) { return addr.is_multicast(); } /** Returns `true` if the address is a private unroutable address. */ -inline -bool -is_private (Address const& addr) +inline bool +is_private(Address const& addr) { - return (addr.is_v4 ()) - ? is_private (addr.to_v4 ()) - : is_private (addr.to_v6 ()); + return (addr.is_v4()) ? is_private(addr.to_v4()) : is_private(addr.to_v6()); } /** Returns `true` if the address is a public routable address. */ -inline -bool -is_public (Address const& addr) +inline bool +is_public(Address const& addr) { - return (addr.is_v4 ()) - ? is_public (addr.to_v4 ()) - : is_public (addr.to_v6 ()); + return (addr.is_v4()) ? is_public(addr.to_v4()) : is_public(addr.to_v6()); } -} +} // namespace IP //------------------------------------------------------------------------------ @@ -101,41 +91,41 @@ void hash_append(Hasher& h, beast::IP::Address const& addr) noexcept { using beast::hash_append; - if (addr.is_v4 ()) + if (addr.is_v4()) hash_append(h, addr.to_v4().to_bytes()); - else if (addr.is_v6 ()) + else if (addr.is_v6()) hash_append(h, addr.to_v6().to_bytes()); else - assert (false); -} + assert(false); } +} // namespace beast namespace std { template <> -struct hash +struct hash { explicit hash() = default; std::size_t - operator() (beast::IP::Address const& addr) const + operator()(beast::IP::Address const& addr) const { - return beast::uhash<>{} (addr); + return beast::uhash<>{}(addr); } }; -} +} // namespace std namespace boost { template <> -struct hash <::beast::IP::Address> +struct hash<::beast::IP::Address> { explicit hash() = default; std::size_t - operator() (::beast::IP::Address const& addr) const + operator()(::beast::IP::Address const& addr) const { - return ::beast::uhash<>{} (addr); + return ::beast::uhash<>{}(addr); } }; -} +} // namespace boost #endif diff --git a/src/ripple/beast/net/IPAddressConversion.h b/src/ripple/beast/net/IPAddressConversion.h index 2b4763f710..e37dd5b6c6 100644 --- a/src/ripple/beast/net/IPAddressConversion.h +++ b/src/ripple/beast/net/IPAddressConversion.h @@ -32,21 +32,25 @@ namespace IP { /** Convert to Endpoint. The port is set to zero. */ -Endpoint from_asio (boost::asio::ip::address const& address); +Endpoint +from_asio(boost::asio::ip::address const& address); /** Convert to Endpoint. */ -Endpoint from_asio (boost::asio::ip::tcp::endpoint const& endpoint); +Endpoint +from_asio(boost::asio::ip::tcp::endpoint const& endpoint); /** Convert to asio::ip::address. The port is ignored. */ -boost::asio::ip::address to_asio_address (Endpoint const& endpoint); +boost::asio::ip::address +to_asio_address(Endpoint const& endpoint); /** Convert to asio::ip::tcp::endpoint. */ -boost::asio::ip::tcp::endpoint to_asio_endpoint (Endpoint const& endpoint); +boost::asio::ip::tcp::endpoint +to_asio_endpoint(Endpoint const& endpoint); -} -} +} // namespace IP +} // namespace beast namespace beast { @@ -55,16 +59,28 @@ struct IPAddressConversion { explicit IPAddressConversion() = default; - static IP::Endpoint from_asio (boost::asio::ip::address const& address) - { return IP::from_asio (address); } - static IP::Endpoint from_asio (boost::asio::ip::tcp::endpoint const& endpoint) - { return IP::from_asio (endpoint); } - static boost::asio::ip::address to_asio_address (IP::Endpoint const& address) - { return IP::to_asio_address (address); } - static boost::asio::ip::tcp::endpoint to_asio_endpoint (IP::Endpoint const& address) - { return IP::to_asio_endpoint (address); } + static IP::Endpoint + from_asio(boost::asio::ip::address const& address) + { + return IP::from_asio(address); + } + static IP::Endpoint + from_asio(boost::asio::ip::tcp::endpoint const& endpoint) + { + return IP::from_asio(endpoint); + } + static boost::asio::ip::address + to_asio_address(IP::Endpoint const& address) + { + return IP::to_asio_address(address); + } + static boost::asio::ip::tcp::endpoint + to_asio_endpoint(IP::Endpoint const& address) + { + return IP::to_asio_endpoint(address); + } }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/net/IPAddressV4.h b/src/ripple/beast/net/IPAddressV4.h index 428d100369..2c6f6a8cce 100644 --- a/src/ripple/beast/net/IPAddressV4.h +++ b/src/ripple/beast/net/IPAddressV4.h @@ -21,12 +21,12 @@ #define BEAST_NET_IPADDRESSV4_H_INCLUDED #include +#include #include #include #include #include #include -#include namespace beast { namespace IP { @@ -34,17 +34,20 @@ namespace IP { using AddressV4 = boost::asio::ip::address_v4; /** Returns `true` if the address is a private unroutable address. */ -bool is_private (AddressV4 const& addr); +bool +is_private(AddressV4 const& addr); /** Returns `true` if the address is a public routable address. */ -bool is_public (AddressV4 const& addr); +bool +is_public(AddressV4 const& addr); /** Returns the address class for the given address. @note Class 'D' represents multicast addresses (224.*.*.*). */ -char get_class (AddressV4 const& address); +char +get_class(AddressV4 const& address); -} -} +} // namespace IP +} // namespace beast #endif diff --git a/src/ripple/beast/net/IPAddressV6.h b/src/ripple/beast/net/IPAddressV6.h index 9e541a5000..74eb6b96d8 100644 --- a/src/ripple/beast/net/IPAddressV6.h +++ b/src/ripple/beast/net/IPAddressV6.h @@ -20,13 +20,13 @@ #ifndef BEAST_NET_IPADDRESSV6_H_INCLUDED #define BEAST_NET_IPADDRESSV6_H_INCLUDED +#include #include #include #include #include #include #include -#include namespace beast { namespace IP { @@ -34,12 +34,14 @@ namespace IP { using AddressV6 = boost::asio::ip::address_v6; /** Returns `true` if the address is a private unroutable address. */ -bool is_private (AddressV6 const& addr); +bool +is_private(AddressV6 const& addr); /** Returns `true` if the address is a public routable address. */ -bool is_public (AddressV6 const& addr); +bool +is_public(AddressV6 const& addr); -} -} +} // namespace IP +} // namespace beast #endif diff --git a/src/ripple/beast/net/IPEndpoint.h b/src/ripple/beast/net/IPEndpoint.h index 74621395e9..7fa2fc86eb 100644 --- a/src/ripple/beast/net/IPEndpoint.h +++ b/src/ripple/beast/net/IPEndpoint.h @@ -20,9 +20,9 @@ #ifndef BEAST_NET_IPENDPOINT_H_INCLUDED #define BEAST_NET_IPENDPOINT_H_INCLUDED -#include #include #include +#include #include @@ -40,64 +40,101 @@ class Endpoint { public: /** Create an unspecified endpoint. */ - Endpoint (); + Endpoint(); /** Create an endpoint from the address and optional port. */ - explicit Endpoint (Address const& addr, Port port = 0); + explicit Endpoint(Address const& addr, Port port = 0); /** Create an Endpoint from a string. If the port is omitted, the endpoint will have a zero port. @return An optional endpoint; will be `boost::none` on failure */ - static boost::optional from_string_checked (std::string const& s); - static Endpoint from_string (std::string const& s); + static boost::optional + from_string_checked(std::string const& s); + static Endpoint + from_string(std::string const& s); /** Returns a string representing the endpoint. */ - std::string to_string () const; + std::string + to_string() const; /** Returns the port number on the endpoint. */ - Port port () const - { return m_port; } + Port + port() const + { + return m_port; + } /** Returns a new Endpoint with a different port. */ - Endpoint at_port (Port port) const - { return Endpoint (m_addr, port); } + Endpoint + at_port(Port port) const + { + return Endpoint(m_addr, port); + } /** Returns the address portion of this endpoint. */ - Address const& address () const - { return m_addr; } + Address const& + address() const + { + return m_addr; + } /** Convenience accessors for the address part. */ /** @{ */ - bool is_v4 () const - { return m_addr.is_v4(); } - bool is_v6 () const - { return m_addr.is_v6(); } - AddressV4 const to_v4 () const - { return m_addr.to_v4 (); } - AddressV6 const to_v6 () const - { return m_addr.to_v6 (); } + bool + is_v4() const + { + return m_addr.is_v4(); + } + bool + is_v6() const + { + return m_addr.is_v6(); + } + AddressV4 const + to_v4() const + { + return m_addr.to_v4(); + } + AddressV6 const + to_v6() const + { + return m_addr.to_v6(); + } /** @} */ /** Arithmetic comparison. */ /** @{ */ - friend bool operator== (Endpoint const& lhs, Endpoint const& rhs); - friend bool operator< (Endpoint const& lhs, Endpoint const& rhs); + friend bool + operator==(Endpoint const& lhs, Endpoint const& rhs); + friend bool + operator<(Endpoint const& lhs, Endpoint const& rhs); - friend bool operator!= (Endpoint const& lhs, Endpoint const& rhs) - { return ! (lhs == rhs); } - friend bool operator> (Endpoint const& lhs, Endpoint const& rhs) - { return rhs < lhs; } - friend bool operator<= (Endpoint const& lhs, Endpoint const& rhs) - { return ! (lhs > rhs); } - friend bool operator>= (Endpoint const& lhs, Endpoint const& rhs) - { return ! (rhs > lhs); } + friend bool + operator!=(Endpoint const& lhs, Endpoint const& rhs) + { + return !(lhs == rhs); + } + friend bool + operator>(Endpoint const& lhs, Endpoint const& rhs) + { + return rhs < lhs; + } + friend bool + operator<=(Endpoint const& lhs, Endpoint const& rhs) + { + return !(lhs > rhs); + } + friend bool + operator>=(Endpoint const& lhs, Endpoint const& rhs) + { + return !(rhs > lhs); + } /** @} */ template - friend - void - hash_append (Hasher& h, Endpoint const& endpoint) + friend void + hash_append(Hasher& h, Endpoint const& endpoint) { using ::beast::hash_append; hash_append(h, endpoint.m_addr, endpoint.m_port); @@ -113,69 +150,95 @@ private: // Properties /** Returns `true` if the endpoint is a loopback address. */ -inline bool is_loopback (Endpoint const& endpoint) - { return is_loopback (endpoint.address ()); } +inline bool +is_loopback(Endpoint const& endpoint) +{ + return is_loopback(endpoint.address()); +} /** Returns `true` if the endpoint is unspecified. */ -inline bool is_unspecified (Endpoint const& endpoint) - { return is_unspecified (endpoint.address ()); } +inline bool +is_unspecified(Endpoint const& endpoint) +{ + return is_unspecified(endpoint.address()); +} /** Returns `true` if the endpoint is a multicast address. */ -inline bool is_multicast (Endpoint const& endpoint) - { return is_multicast (endpoint.address ()); } +inline bool +is_multicast(Endpoint const& endpoint) +{ + return is_multicast(endpoint.address()); +} /** Returns `true` if the endpoint is a private unroutable address. */ -inline bool is_private (Endpoint const& endpoint) - { return is_private (endpoint.address ()); } +inline bool +is_private(Endpoint const& endpoint) +{ + return is_private(endpoint.address()); +} /** Returns `true` if the endpoint is a public routable address. */ -inline bool is_public (Endpoint const& endpoint) - { return is_public (endpoint.address ()); } +inline bool +is_public(Endpoint const& endpoint) +{ + return is_public(endpoint.address()); +} //------------------------------------------------------------------------------ /** Returns the endpoint represented as a string. */ -inline std::string to_string (Endpoint const& endpoint) - { return endpoint.to_string(); } +inline std::string +to_string(Endpoint const& endpoint) +{ + return endpoint.to_string(); +} /** Output stream conversion. */ template -OutputStream& operator<< (OutputStream& os, Endpoint const& endpoint) +OutputStream& +operator<<(OutputStream& os, Endpoint const& endpoint) { - os << to_string (endpoint); + os << to_string(endpoint); return os; } /** Input stream conversion. */ -std::istream& operator>> (std::istream& is, Endpoint& endpoint); +std::istream& +operator>>(std::istream& is, Endpoint& endpoint); -} -} +} // namespace IP +} // namespace beast //------------------------------------------------------------------------------ namespace std { /** std::hash support. */ template <> -struct hash <::beast::IP::Endpoint> +struct hash<::beast::IP::Endpoint> { explicit hash() = default; - std::size_t operator() (::beast::IP::Endpoint const& endpoint) const - { return ::beast::uhash<>{} (endpoint); } + std::size_t + operator()(::beast::IP::Endpoint const& endpoint) const + { + return ::beast::uhash<>{}(endpoint); + } }; -} +} // namespace std namespace boost { /** boost::hash support. */ template <> -struct hash <::beast::IP::Endpoint> +struct hash<::beast::IP::Endpoint> { explicit hash() = default; - std::size_t operator() (::beast::IP::Endpoint const& endpoint) const - { return ::beast::uhash<>{} (endpoint); } + std::size_t + operator()(::beast::IP::Endpoint const& endpoint) const + { + return ::beast::uhash<>{}(endpoint); + } }; -} +} // namespace boost #endif diff --git a/src/ripple/beast/net/impl/IPAddressConversion.cpp b/src/ripple/beast/net/impl/IPAddressConversion.cpp index 3e6193daed..6353c50dfb 100644 --- a/src/ripple/beast/net/impl/IPAddressConversion.cpp +++ b/src/ripple/beast/net/impl/IPAddressConversion.cpp @@ -22,25 +22,29 @@ namespace beast { namespace IP { -Endpoint from_asio (boost::asio::ip::address const& address) +Endpoint +from_asio(boost::asio::ip::address const& address) { - return Endpoint {address}; + return Endpoint{address}; } -Endpoint from_asio (boost::asio::ip::tcp::endpoint const& endpoint) +Endpoint +from_asio(boost::asio::ip::tcp::endpoint const& endpoint) { - return Endpoint {endpoint.address(), endpoint.port()}; + return Endpoint{endpoint.address(), endpoint.port()}; } -boost::asio::ip::address to_asio_address (Endpoint const& endpoint) +boost::asio::ip::address +to_asio_address(Endpoint const& endpoint) { return endpoint.address(); } -boost::asio::ip::tcp::endpoint to_asio_endpoint (Endpoint const& endpoint) +boost::asio::ip::tcp::endpoint +to_asio_endpoint(Endpoint const& endpoint) { - return boost::asio::ip::tcp::endpoint {endpoint.address(), endpoint.port()}; + return boost::asio::ip::tcp::endpoint{endpoint.address(), endpoint.port()}; } -} -} +} // namespace IP +} // namespace beast diff --git a/src/ripple/beast/net/impl/IPAddressV4.cpp b/src/ripple/beast/net/impl/IPAddressV4.cpp index b683e38265..7e428c4228 100644 --- a/src/ripple/beast/net/impl/IPAddressV4.cpp +++ b/src/ripple/beast/net/impl/IPAddressV4.cpp @@ -25,27 +25,30 @@ namespace beast { namespace IP { -bool is_private (AddressV4 const& addr) +bool +is_private(AddressV4 const& addr) { - return - ((addr.to_ulong() & 0xff000000) == 0x0a000000) || // Prefix /8, 10. #.#.# - ((addr.to_ulong() & 0xfff00000) == 0xac100000) || // Prefix /12 172. 16.#.# - 172.31.#.# - ((addr.to_ulong() & 0xffff0000) == 0xc0a80000) || // Prefix /16 192.168.#.# + return ((addr.to_ulong() & 0xff000000) == + 0x0a000000) || // Prefix /8, 10. #.#.# + ((addr.to_ulong() & 0xfff00000) == + 0xac100000) || // Prefix /12 172. 16.#.# - 172.31.#.# + ((addr.to_ulong() & 0xffff0000) == + 0xc0a80000) || // Prefix /16 192.168.#.# addr.is_loopback(); } -bool is_public (AddressV4 const& addr) +bool +is_public(AddressV4 const& addr) { - return - ! is_private (addr) && - ! addr.is_multicast(); + return !is_private(addr) && !addr.is_multicast(); } -char get_class (AddressV4 const& addr) +char +get_class(AddressV4 const& addr) { static char const* table = "AAAABBCD"; - return table [(addr.to_ulong() & 0xE0000000) >> 29]; + return table[(addr.to_ulong() & 0xE0000000) >> 29]; } -} -} +} // namespace IP +} // namespace beast diff --git a/src/ripple/beast/net/impl/IPAddressV6.cpp b/src/ripple/beast/net/impl/IPAddressV6.cpp index 503116b657..07dd2baaee 100644 --- a/src/ripple/beast/net/impl/IPAddressV6.cpp +++ b/src/ripple/beast/net/impl/IPAddressV6.cpp @@ -17,26 +17,26 @@ */ //============================================================================== -#include #include +#include namespace beast { namespace IP { -bool is_private (AddressV6 const& addr) +bool +is_private(AddressV6 const& addr) { - return ((addr.to_bytes()[0] & 0xfd) || // TODO fc00::/8 too ? - (addr.is_v4_mapped() && is_private(addr.to_v4())) ); + return ( + (addr.to_bytes()[0] & 0xfd) || // TODO fc00::/8 too ? + (addr.is_v4_mapped() && is_private(addr.to_v4()))); } -bool is_public (AddressV6 const& addr) +bool +is_public(AddressV6 const& addr) { // TODO is this correct? - return - ! is_private (addr) && - ! addr.is_multicast(); + return !is_private(addr) && !addr.is_multicast(); } - -} -} +} // namespace IP +} // namespace beast diff --git a/src/ripple/beast/net/impl/IPEndpoint.cpp b/src/ripple/beast/net/impl/IPEndpoint.cpp index 0ea7b11e88..5f2160f537 100644 --- a/src/ripple/beast/net/impl/IPEndpoint.cpp +++ b/src/ripple/beast/net/impl/IPEndpoint.cpp @@ -23,61 +23,62 @@ namespace beast { namespace IP { -Endpoint::Endpoint () - : m_port (0) +Endpoint::Endpoint() : m_port(0) { } -Endpoint::Endpoint (Address const& addr, Port port) - : m_addr (addr) - , m_port (port) +Endpoint::Endpoint(Address const& addr, Port port) : m_addr(addr), m_port(port) { } -boost::optional Endpoint::from_string_checked (std::string const& s) +boost::optional +Endpoint::from_string_checked(std::string const& s) { - std::stringstream is (boost::trim_copy(s)); + std::stringstream is(boost::trim_copy(s)); Endpoint endpoint; is >> endpoint; - if (! is.fail() && is.rdbuf()->in_avail() == 0) + if (!is.fail() && is.rdbuf()->in_avail() == 0) return endpoint; return {}; } -Endpoint Endpoint::from_string (std::string const& s) +Endpoint +Endpoint::from_string(std::string const& s) { if (boost::optional const result = from_string_checked(s)) return *result; return Endpoint{}; } -std::string Endpoint::to_string () const +std::string +Endpoint::to_string() const { std::string s; s.reserve( - (address().is_v6() ? INET6_ADDRSTRLEN-1 : 15) + + (address().is_v6() ? INET6_ADDRSTRLEN - 1 : 15) + (port() == 0 ? 0 : 6 + (address().is_v6() ? 2 : 0))); if (port() != 0 && address().is_v6()) s += '['; - s += address ().to_string(); + s += address().to_string(); if (port()) { if (address().is_v6()) s += ']'; - s += ":" + std::to_string (port()); + s += ":" + std::to_string(port()); } return s; } -bool operator== (Endpoint const& lhs, Endpoint const& rhs) +bool +operator==(Endpoint const& lhs, Endpoint const& rhs) { - return lhs.address() == rhs.address() && - lhs.port() == rhs.port(); + return lhs.address() == rhs.address() && lhs.port() == rhs.port(); } -bool operator< (Endpoint const& lhs, Endpoint const& rhs) +bool +operator<(Endpoint const& lhs, Endpoint const& rhs) { if (lhs.address() < rhs.address()) return true; @@ -88,19 +89,20 @@ bool operator< (Endpoint const& lhs, Endpoint const& rhs) //------------------------------------------------------------------------------ -std::istream& operator>> (std::istream& is, Endpoint& endpoint) +std::istream& +operator>>(std::istream& is, Endpoint& endpoint) { std::string addrStr; // valid addresses only need INET6_ADDRSTRLEN-1 chars, but allow the extra // char to check for invalid lengths addrStr.reserve(INET6_ADDRSTRLEN); - char i {0}; - char readTo {0}; + char i{0}; + char readTo{0}; is.get(i); - if (i == '[') // we are an IPv6 endpoint + if (i == '[') // we are an IPv6 endpoint readTo = ']'; else - addrStr+=i; + addrStr += i; while (is && is.rdbuf()->in_avail() > 0 && is.get(i)) { @@ -112,32 +114,30 @@ std::istream& operator>> (std::istream& is, Endpoint& endpoint) if (isspace(static_cast(i)) || (readTo && i == readTo)) break; - if ((i == '.') || - (i >= '0' && i <= ':') || - (i >= 'a' && i <= 'f') || + if ((i == '.') || (i >= '0' && i <= ':') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F')) { - addrStr+=i; + addrStr += i; // don't exceed a reasonable length... - if ( addrStr.size() == INET6_ADDRSTRLEN || + if (addrStr.size() == INET6_ADDRSTRLEN || (readTo && readTo == ':' && addrStr.size() > 15)) { - is.setstate (std::ios_base::failbit); + is.setstate(std::ios_base::failbit); return is; } - if (! readTo && (i == '.' || i == ':')) + if (!readTo && (i == '.' || i == ':')) { // if we see a dot first, must be IPv4 // otherwise must be non-bracketed IPv6 readTo = (i == '.') ? ':' : ' '; } } - else // invalid char + else // invalid char { is.unget(); - is.setstate (std::ios_base::failbit); + is.setstate(std::ios_base::failbit); return is; } } @@ -145,10 +145,10 @@ std::istream& operator>> (std::istream& is, Endpoint& endpoint) if (readTo == ']' && is.rdbuf()->in_avail() > 0) { is.get(i); - if (! (isspace(static_cast(i)) || i == ':')) + if (!(isspace(static_cast(i)) || i == ':')) { is.unget(); - is.setstate (std::ios_base::failbit); + is.setstate(std::ios_base::failbit); return is; } } @@ -157,7 +157,7 @@ std::istream& operator>> (std::istream& is, Endpoint& endpoint) auto addr = Address::from_string(addrStr, ec); if (ec) { - is.setstate (std::ios_base::failbit); + is.setstate(std::ios_base::failbit); return is; } @@ -167,13 +167,13 @@ std::istream& operator>> (std::istream& is, Endpoint& endpoint) is >> port; if (is.fail()) return is; - endpoint = Endpoint (addr, port); + endpoint = Endpoint(addr, port); } else - endpoint = Endpoint (addr); + endpoint = Endpoint(addr); return is; } -} -} +} // namespace IP +} // namespace beast diff --git a/src/ripple/beast/rfc2616.h b/src/ripple/beast/rfc2616.h index 4a970e1c33..bb176b944f 100644 --- a/src/ripple/beast/rfc2616.h +++ b/src/ripple/beast/rfc2616.h @@ -27,9 +27,9 @@ #include #include #include -#include #include -#include // for std::tie, remove ASAP +#include +#include // for std::tie, remove ASAP #include #include @@ -48,71 +48,82 @@ struct ci_equal_pred { explicit ci_equal_pred() = default; - bool operator()(char c1, char c2) + bool + operator()(char c1, char c2) { // VFALCO TODO Use a table lookup here return std::tolower(static_cast(c1)) == - std::tolower(static_cast(c2)); + std::tolower(static_cast(c2)); } }; -} // detail +} // namespace detail /** Returns `true` if `c` is linear white space. This excludes the CRLF sequence allowed for line continuations. */ -inline -bool +inline bool is_lws(char c) { return c == ' ' || c == '\t'; } /** Returns `true` if `c` is any whitespace character. */ -inline -bool +inline bool is_white(char c) { switch (c) { - case ' ': case '\f': case '\n': - case '\r': case '\t': case '\v': - return true; + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + return true; }; return false; } /** Returns `true` if `c` is a control character. */ -inline -bool +inline bool is_control(char c) { return c <= 31 || c >= 127; } /** Returns `true` if `c` is a separator. */ -inline -bool +inline bool is_separator(char c) { // VFALCO Could use a static table switch (c) { - case '(': case ')': case '<': case '>': case '@': - case ',': case ';': case ':': case '\\': case '"': - case '{': case '}': case ' ': case '\t': - return true; + case '(': + case ')': + case '<': + case '>': + case '@': + case ',': + case ';': + case ':': + case '\\': + case '"': + case '{': + case '}': + case ' ': + case '\t': + return true; }; return false; } /** Returns `true` if `c` is a character. */ -inline -bool +inline bool is_char(char c) { -#ifdef __CHAR_UNSIGNED__ /* -funsigned-char */ +#ifdef __CHAR_UNSIGNED__ /* -funsigned-char */ return c >= 0 && c <= 127; #else return c >= 0; @@ -121,75 +132,70 @@ is_char(char c) template FwdIter -trim_left (FwdIter first, FwdIter last) +trim_left(FwdIter first, FwdIter last) { - return std::find_if_not (first, last, - is_white); + return std::find_if_not(first, last, is_white); } template FwdIter -trim_right (FwdIter first, FwdIter last) +trim_right(FwdIter first, FwdIter last) { if (first == last) return last; do { --last; - if (! is_white (*last)) + if (!is_white(*last)) return ++last; - } - while (last != first); + } while (last != first); return first; } template void -trim_right_in_place (std::basic_string < - CharT, Traits, Allocator>& s) +trim_right_in_place(std::basic_string& s) { - s.resize (std::distance (s.begin(), - trim_right (s.begin(), s.end()))); + s.resize(std::distance(s.begin(), trim_right(s.begin(), s.end()))); } template -std::pair -trim (FwdIter first, FwdIter last) +std::pair +trim(FwdIter first, FwdIter last) { - first = trim_left (first, last); - last = trim_right (first, last); - return std::make_pair (first, last); + first = trim_left(first, last); + last = trim_right(first, last); + return std::make_pair(first, last); } template String -trim (String const& s) +trim(String const& s) { using std::begin; using std::end; auto first = begin(s); auto last = end(s); - std::tie (first, last) = trim (first, last); - return { first, last }; + std::tie(first, last) = trim(first, last); + return {first, last}; } template String -trim_right (String const& s) +trim_right(String const& s) { using std::begin; using std::end; - auto first (begin(s)); - auto last (end(s)); - last = trim_right (first, last); - return { first, last }; + auto first(begin(s)); + auto last(end(s)); + last = trim_right(first, last); + return {first, last}; } -inline -std::string -trim (std::string const& s) +inline std::string +trim(std::string const& s) { - return trim (s); + return trim(s); } /** Parse a character sequence of values separated by commas. @@ -200,11 +206,11 @@ trim (std::string const& s) Reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2 */ -template ::value_type>>, - class Char> + std::basic_string::value_type>>, + class Char> Result split(FwdIt first, FwdIt last, Char delim) { @@ -231,15 +237,15 @@ split(FwdIt first, FwdIt last, Char delim) // quoted-pair ++iter; if (iter != last) - e.append (1, *iter++); + e.append(1, *iter++); } else { // qdtext - e.append (1, *iter++); + e.append(1, *iter++); } } - if (! e.empty()) + if (!e.empty()) { result.emplace_back(std::move(e)); e.clear(); @@ -247,37 +253,37 @@ split(FwdIt first, FwdIt last, Char delim) } else if (*iter == delim) { - e = trim_right (e); - if (! e.empty()) + e = trim_right(e); + if (!e.empty()) { result.emplace_back(std::move(e)); e.clear(); } ++iter; } - else if (is_lws (*iter)) + else if (is_lws(*iter)) { ++iter; } else { - e.append (1, *iter++); + e.append(1, *iter++); } } - if (! e.empty()) + if (!e.empty()) { - e = trim_right (e); - if (! e.empty()) + e = trim_right(e); + if (!e.empty()) result.emplace_back(std::move(e)); } return result; } -template ::value_type>>> + std::basic_string::value_type>>> Result split_commas(FwdIt first, FwdIt last) { @@ -314,22 +320,19 @@ public: using pointer = value_type const*; using reference = value_type const&; using difference_type = std::ptrdiff_t; - using iterator_category = - std::forward_iterator_tag; + using iterator_category = std::forward_iterator_tag; - list_iterator(iter_type begin, iter_type end) - : it_(begin) - , end_(end) + list_iterator(iter_type begin, iter_type end) : it_(begin), end_(end) { - if(it_ != end_) + if (it_ != end_) increment(); } bool operator==(list_iterator const& other) const { - return other.it_ == it_ && other.end_ == end_ - && other.value_.size() == value_.size(); + return other.it_ == it_ && other.end_ == end_ && + other.value_.size() == value_.size(); } bool @@ -366,37 +369,37 @@ public: } private: - template + template void increment(); }; -template +template void list_iterator::increment() { value_.clear(); - while(it_ != end_) + while (it_ != end_) { - if(*it_ == '"') + if (*it_ == '"') { // quoted-string ++it_; - if(it_ == end_) + if (it_ == end_) return; - if(*it_ != '"') + if (*it_ != '"') { auto start = it_; - for(;;) + for (;;) { ++it_; - if(it_ == end_) + if (it_ == end_) { value_ = boost::string_ref( &*start, std::distance(start, it_)); return; } - if(*it_ == '"') + if (*it_ == '"') { value_ = boost::string_ref( &*start, std::distance(start, it_)); @@ -407,12 +410,12 @@ list_iterator::increment() } ++it_; } - else if(*it_ == ',') + else if (*it_ == ',') { it_++; continue; } - else if(is_lws(*it_)) + else if (is_lws(*it_)) { ++it_; continue; @@ -420,15 +423,13 @@ list_iterator::increment() else { auto start = it_; - for(;;) + for (;;) { ++it_; - if(it_ == end_ || - *it_ == ',' || - is_lws(*it_)) + if (it_ == end_ || *it_ == ',' || is_lws(*it_)) { - value_ = boost::string_ref( - &*start, std::distance(start, it_)); + value_ = + boost::string_ref(&*start, std::distance(start, it_)); return; } } @@ -440,51 +441,49 @@ list_iterator::increment() A case-insensitive comparison is used. */ -inline -bool +inline bool ci_equal(boost::string_ref s1, boost::string_ref s2) { - return boost::range::equal(s1, s2, - detail::ci_equal_pred{}); + return boost::range::equal(s1, s2, detail::ci_equal_pred{}); } /** Returns a range representing the list. */ -inline -boost::iterator_range +inline boost::iterator_range make_list(boost::string_ref const& field) { return boost::iterator_range{ list_iterator{field.begin(), field.end()}, - list_iterator{field.end(), field.end()}}; + list_iterator{field.end(), field.end()}}; } /** Returns true if the specified token exists in the list. A case-insensitive comparison is used. */ -template +template bool -token_in_list(boost::string_ref const& value, - boost::string_ref const& token) +token_in_list(boost::string_ref const& value, boost::string_ref const& token) { - for(auto const& item : make_list(value)) - if(ci_equal(item, token)) + for (auto const& item : make_list(value)) + if (ci_equal(item, token)) return true; return false; } -template +template bool is_keep_alive(boost::beast::http::message const& m) { - if(m.version() <= 10) + if (m.version() <= 10) return boost::beast::http::token_list{ - m[boost::beast::http::field::connection]}.exists("keep-alive"); - return ! boost::beast::http::token_list{ - m[boost::beast::http::field::connection]}.exists("close"); + m[boost::beast::http::field::connection]} + .exists("keep-alive"); + return !boost::beast::http::token_list{ + m[boost::beast::http::field::connection]} + .exists("close"); } -} // rfc2616 -} // beast +} // namespace rfc2616 +} // namespace beast #endif diff --git a/src/ripple/beast/type_name.h b/src/ripple/beast/type_name.h index 59222ee4fc..f131d950bf 100644 --- a/src/ripple/beast/type_name.h +++ b/src/ripple/beast/type_name.h @@ -20,22 +20,22 @@ #ifndef BEAST_TYPE_NAME_H_INCLUDED #define BEAST_TYPE_NAME_H_INCLUDED +#include #include #include -#include #ifndef _MSC_VER #include #endif +#include #include #include -#include #include namespace beast { #ifdef _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4127) // conditional expression is constant +#pragma warning(push) +#pragma warning(disable : 4127) // conditional expression is constant #endif template @@ -43,15 +43,13 @@ std::string type_name() { using TR = typename std::remove_reference::type; - std::unique_ptr own ( - #ifndef _MSC_VER - abi::__cxa_demangle (typeid(TR).name(), nullptr, - nullptr, nullptr), - #else - nullptr, - #endif - std::free - ); + std::unique_ptr own( +#ifndef _MSC_VER + abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr), +#else + nullptr, +#endif + std::free); std::string r = own != nullptr ? own.get() : typeid(TR).name(); if (std::is_const::value) r += " const"; @@ -65,9 +63,9 @@ type_name() } #ifdef _MSC_VER -#pragma warning (pop) +#pragma warning(pop) #endif -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/unit_test.h b/src/ripple/beast/unit_test.h index 7e7193ea03..3352abb6c5 100644 --- a/src/ripple/beast/unit_test.h +++ b/src/ripple/beast/unit_test.h @@ -34,7 +34,8 @@ #ifndef BEAST_EXPECT #define BEAST_EXPECT_S1(x) #x #define BEAST_EXPECT_S2(x) BEAST_EXPECT_S1(x) -//#define BEAST_EXPECT(cond) {expect(cond, __FILE__ ":" ## __LINE__);}while(false){} +//#define BEAST_EXPECT(cond) {expect(cond, __FILE__ ":" ## +//__LINE__);}while(false){} #define BEAST_EXPECT(cond) expect(cond, __FILE__ ":" BEAST_EXPECT_S2(__LINE__)) #endif diff --git a/src/ripple/beast/utility/Journal.h b/src/ripple/beast/utility/Journal.h index ec9e09bbfb..333a743a65 100644 --- a/src/ripple/beast/utility/Journal.h +++ b/src/ripple/beast/utility/Journal.h @@ -26,24 +26,22 @@ namespace beast { /** A namespace for easy access to logging severity values. */ -namespace severities -{ - /** Severity level / threshold of a Journal message. */ - enum Severity - { - kAll = 0, +namespace severities { +/** Severity level / threshold of a Journal message. */ +enum Severity { + kAll = 0, - kTrace = kAll, - kDebug, - kInfo, - kWarning, - kError, - kFatal, + kTrace = kAll, + kDebug, + kInfo, + kWarning, + kError, + kFatal, - kDisabled, - kNone = kDisabled - }; -} + kDisabled, + kNone = kDisabled +}; +} // namespace severities /** A generic endpoint for log messages. @@ -76,34 +74,43 @@ public: class Sink { protected: - Sink () = delete; + Sink() = delete; explicit Sink(Sink const& sink) = default; - Sink (Severity thresh, bool console); - Sink& operator= (Sink const& lhs) = delete; + Sink(Severity thresh, bool console); + Sink& + operator=(Sink const& lhs) = delete; public: - virtual ~Sink () = 0; + virtual ~Sink() = 0; /** Returns `true` if text at the passed severity produces output. */ - virtual bool active (Severity level) const; + virtual bool + active(Severity level) const; - /** Returns `true` if a message is also written to the Output Window (MSVC). */ - virtual bool console () const; + /** Returns `true` if a message is also written to the Output Window + * (MSVC). */ + virtual bool + console() const; - /** Set whether messages are also written to the Output Window (MSVC). */ - virtual void console (bool output); + /** Set whether messages are also written to the Output Window (MSVC). + */ + virtual void + console(bool output); /** Returns the minimum severity level this sink will report. */ - virtual Severity threshold() const; + virtual Severity + threshold() const; /** Set the minimum severity this sink will report. */ - virtual void threshold (Severity thresh); + virtual void + threshold(Severity thresh); /** Write text to the sink at the specified severity. A conforming implementation will not write the text if the passed level is below the current threshold(). */ - virtual void write (Severity level, std::string const& text) = 0; + virtual void + write(Severity level, std::string const& text) = 0; private: Severity thresh_; @@ -111,16 +118,17 @@ public: }; #ifndef __INTELLISENSE__ -static_assert(std::is_default_constructible::value == false, ""); -static_assert(std::is_copy_constructible::value == false, ""); -static_assert(std::is_move_constructible::value == false, ""); -static_assert(std::is_copy_assignable::value == false, ""); -static_assert(std::is_move_assignable::value == false, ""); -static_assert(std::is_nothrow_destructible::value == true, ""); + static_assert(std::is_default_constructible::value == false, ""); + static_assert(std::is_copy_constructible::value == false, ""); + static_assert(std::is_move_constructible::value == false, ""); + static_assert(std::is_copy_assignable::value == false, ""); + static_assert(std::is_move_assignable::value == false, ""); + static_assert(std::is_nothrow_destructible::value == true, ""); #endif /** Returns a Sink which does nothing. */ - static Sink& getNullSink (); + static Sink& + getNullSink(); //-------------------------------------------------------------------------- @@ -131,32 +139,35 @@ private: class ScopedStream { public: - ScopedStream (ScopedStream const& other) - : ScopedStream (other.m_sink, other.m_level) - { } + ScopedStream(ScopedStream const& other) + : ScopedStream(other.m_sink, other.m_level) + { + } - ScopedStream (Sink& sink, Severity level); + ScopedStream(Sink& sink, Severity level); template - ScopedStream (Stream const& stream, T const& t); + ScopedStream(Stream const& stream, T const& t); - ScopedStream ( - Stream const& stream, std::ostream& manip (std::ostream&)); + ScopedStream(Stream const& stream, std::ostream& manip(std::ostream&)); - ScopedStream& operator= (ScopedStream const&) = delete; + ScopedStream& + operator=(ScopedStream const&) = delete; - ~ScopedStream (); + ~ScopedStream(); - std::ostringstream& ostream () const + std::ostringstream& + ostream() const { return m_ostream; } - std::ostream& operator<< ( - std::ostream& manip (std::ostream&)) const; + std::ostream& + operator<<(std::ostream& manip(std::ostream&)) const; template - std::ostream& operator<< (T const& t) const; + std::ostream& + operator<<(T const& t) const; private: Sink& m_sink; @@ -165,12 +176,16 @@ private: }; #ifndef __INTELLISENSE__ -static_assert(std::is_default_constructible::value == false, ""); -static_assert(std::is_copy_constructible::value == true, ""); -static_assert(std::is_move_constructible::value == true, ""); -static_assert(std::is_copy_assignable::value == false, ""); -static_assert(std::is_move_assignable::value == false, ""); -static_assert(std::is_nothrow_destructible::value == true, ""); + static_assert( + std::is_default_constructible::value == false, + ""); + static_assert(std::is_copy_constructible::value == true, ""); + static_assert(std::is_move_constructible::value == true, ""); + static_assert(std::is_copy_assignable::value == false, ""); + static_assert(std::is_move_assignable::value == false, ""); + static_assert( + std::is_nothrow_destructible::value == true, + ""); #endif //-------------------------------------------------------------------------- @@ -180,50 +195,51 @@ public: { public: /** Create a stream which produces no output. */ - explicit Stream () - : m_sink (getNullSink()) - , m_level (severities::kDisabled) - { } + explicit Stream() + : m_sink(getNullSink()), m_level(severities::kDisabled) + { + } /** Create a stream that writes at the given level. Constructor is inlined so checking active() very inexpensive. */ - Stream (Sink& sink, Severity level) - : m_sink (sink) - , m_level (level) + Stream(Sink& sink, Severity level) : m_sink(sink), m_level(level) { - assert (m_level < severities::kDisabled); + assert(m_level < severities::kDisabled); } /** Construct or copy another Stream. */ - Stream (Stream const& other) - : Stream (other.m_sink, other.m_level) - { } + Stream(Stream const& other) : Stream(other.m_sink, other.m_level) + { + } - Stream& operator= (Stream const& other) = delete; + Stream& + operator=(Stream const& other) = delete; /** Returns the Sink that this Stream writes to. */ - Sink& sink() const + Sink& + sink() const { return m_sink; } /** Returns the Severity level of messages this Stream reports. */ - Severity level() const + Severity + level() const { return m_level; } /** Returns `true` if sink logs anything at this stream's level. */ /** @{ */ - bool active() const + bool + active() const { - return m_sink.active (m_level); + return m_sink.active(m_level); } - explicit - operator bool() const + explicit operator bool() const { return active(); } @@ -231,10 +247,12 @@ public: /** Output stream support. */ /** @{ */ - ScopedStream operator<< (std::ostream& manip (std::ostream&)) const; + ScopedStream + operator<<(std::ostream& manip(std::ostream&)) const; template - ScopedStream operator<< (T const& t) const; + ScopedStream + operator<<(T const& t) const; /** @} */ private: @@ -243,75 +261,84 @@ public: }; #ifndef __INTELLISENSE__ -static_assert(std::is_default_constructible::value == true, ""); -static_assert(std::is_copy_constructible::value == true, ""); -static_assert(std::is_move_constructible::value == true, ""); -static_assert(std::is_copy_assignable::value == false, ""); -static_assert(std::is_move_assignable::value == false, ""); -static_assert(std::is_nothrow_destructible::value == true, ""); + static_assert(std::is_default_constructible::value == true, ""); + static_assert(std::is_copy_constructible::value == true, ""); + static_assert(std::is_move_constructible::value == true, ""); + static_assert(std::is_copy_assignable::value == false, ""); + static_assert(std::is_move_assignable::value == false, ""); + static_assert(std::is_nothrow_destructible::value == true, ""); #endif //-------------------------------------------------------------------------- /** Journal has no default constructor. */ - Journal () = delete; + Journal() = delete; /** Create a journal that writes to the specified sink. */ - explicit Journal (Sink& sink) - : m_sink (&sink) - { } + explicit Journal(Sink& sink) : m_sink(&sink) + { + } /** Returns the Sink associated with this Journal. */ - Sink& sink() const + Sink& + sink() const { return *m_sink; } /** Returns a stream for this sink, with the specified severity level. */ - Stream stream (Severity level) const + Stream + stream(Severity level) const { - return Stream (*m_sink, level); + return Stream(*m_sink, level); } /** Returns `true` if any message would be logged at this severity level. For a message to be logged, the severity must be at or above the sink's severity threshold. */ - bool active (Severity level) const + bool + active(Severity level) const { - return m_sink->active (level); + return m_sink->active(level); } /** Severity stream access functions. */ /** @{ */ - Stream trace() const + Stream + trace() const { - return { *m_sink, severities::kTrace }; + return {*m_sink, severities::kTrace}; } - Stream debug() const + Stream + debug() const { - return { *m_sink, severities::kDebug }; + return {*m_sink, severities::kDebug}; } - Stream info() const + Stream + info() const { - return { *m_sink, severities::kInfo }; + return {*m_sink, severities::kInfo}; } - Stream warn() const + Stream + warn() const { - return { *m_sink, severities::kWarning }; + return {*m_sink, severities::kWarning}; } - Stream error() const + Stream + error() const { - return { *m_sink, severities::kError }; + return {*m_sink, severities::kError}; } - Stream fatal() const + Stream + fatal() const { - return { *m_sink, severities::kFatal }; + return {*m_sink, severities::kFatal}; } /** @} */ }; @@ -328,15 +355,15 @@ static_assert(std::is_nothrow_destructible::value == true, ""); //------------------------------------------------------------------------------ template -Journal::ScopedStream::ScopedStream (Journal::Stream const& stream, T const& t) - : ScopedStream (stream.sink(), stream.level()) +Journal::ScopedStream::ScopedStream(Journal::Stream const& stream, T const& t) + : ScopedStream(stream.sink(), stream.level()) { m_ostream << t; } template std::ostream& -Journal::ScopedStream::operator<< (T const& t) const +Journal::ScopedStream::operator<<(T const& t) const { m_ostream << t; return m_ostream; @@ -346,38 +373,38 @@ Journal::ScopedStream::operator<< (T const& t) const template Journal::ScopedStream -Journal::Stream::operator<< (T const& t) const +Journal::Stream::operator<<(T const& t) const { - return ScopedStream (*this, t); + return ScopedStream(*this, t); } namespace detail { -template> -class logstream_buf - : public std::basic_stringbuf +template > +class logstream_buf : public std::basic_stringbuf { beast::Journal::Stream strm_; - template - void write(T const*) = delete; + template + void + write(T const*) = delete; - void write(char const* s) + void + write(char const* s) { - if(strm_) + if (strm_) strm_ << s; } - void write(wchar_t const* s) + void + write(wchar_t const* s) { - if(strm_) + if (strm_) strm_ << s; } public: - explicit - logstream_buf(beast::Journal::Stream const& strm) - : strm_(strm) + explicit logstream_buf(beast::Journal::Stream const& strm) : strm_(strm) { } @@ -395,27 +422,22 @@ public: } }; -} // detail +} // namespace detail -template< - class CharT, - class Traits = std::char_traits -> -class basic_logstream - : public std::basic_ostream +template > +class basic_logstream : public std::basic_ostream { - typedef CharT char_type; - typedef Traits traits_type; + typedef CharT char_type; + typedef Traits traits_type; typedef typename traits_type::int_type int_type; typedef typename traits_type::pos_type pos_type; typedef typename traits_type::off_type off_type; detail::logstream_buf buf_; + public: - explicit - basic_logstream(beast::Journal::Stream const& strm) - : std::basic_ostream(&buf_) - , buf_(strm) + explicit basic_logstream(beast::Journal::Stream const& strm) + : std::basic_ostream(&buf_), buf_(strm) { } }; @@ -423,6 +445,6 @@ public: using logstream = basic_logstream; using logwstream = basic_logstream; -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/utility/PropertyStream.h b/src/ripple/beast/utility/PropertyStream.h index b11c1bffe3..bfedb39ecd 100644 --- a/src/ripple/beast/utility/PropertyStream.h +++ b/src/ripple/beast/utility/PropertyStream.h @@ -40,89 +40,133 @@ public: class Set; class Source; - PropertyStream () = default; - virtual ~PropertyStream () = default; + PropertyStream() = default; + virtual ~PropertyStream() = default; protected: - virtual void map_begin () = 0; - virtual void map_begin (std::string const& key) = 0; - virtual void map_end () = 0; + virtual void + map_begin() = 0; + virtual void + map_begin(std::string const& key) = 0; + virtual void + map_end() = 0; - virtual void add (std::string const& key, std::string const& value) = 0; + virtual void + add(std::string const& key, std::string const& value) = 0; - void add (std::string const& key, char const* value) + void + add(std::string const& key, char const* value) { - add (key, std::string (value)); + add(key, std::string(value)); } template - void lexical_add (std::string const &key, Value value) + void + lexical_add(std::string const& key, Value value) { std::stringstream ss; ss << value; - add (key, ss.str()); + add(key, ss.str()); } - virtual void add (std::string const& key, bool value); - virtual void add (std::string const& key, char value); - virtual void add (std::string const& key, signed char value); - virtual void add (std::string const& key, unsigned char value); - virtual void add (std::string const& key, wchar_t value); + virtual void + add(std::string const& key, bool value); + virtual void + add(std::string const& key, char value); + virtual void + add(std::string const& key, signed char value); + virtual void + add(std::string const& key, unsigned char value); + virtual void + add(std::string const& key, wchar_t value); #if 0 virtual void add (std::string const& key, char16_t value); virtual void add (std::string const& key, char32_t value); #endif - virtual void add (std::string const& key, short value); - virtual void add (std::string const& key, unsigned short value); - virtual void add (std::string const& key, int value); - virtual void add (std::string const& key, unsigned int value); - virtual void add (std::string const& key, long value); - virtual void add (std::string const& key, unsigned long value); - virtual void add (std::string const& key, long long value); - virtual void add (std::string const& key, unsigned long long value); - virtual void add (std::string const& key, float value); - virtual void add (std::string const& key, double value); - virtual void add (std::string const& key, long double value); + virtual void + add(std::string const& key, short value); + virtual void + add(std::string const& key, unsigned short value); + virtual void + add(std::string const& key, int value); + virtual void + add(std::string const& key, unsigned int value); + virtual void + add(std::string const& key, long value); + virtual void + add(std::string const& key, unsigned long value); + virtual void + add(std::string const& key, long long value); + virtual void + add(std::string const& key, unsigned long long value); + virtual void + add(std::string const& key, float value); + virtual void + add(std::string const& key, double value); + virtual void + add(std::string const& key, long double value); - virtual void array_begin () = 0; - virtual void array_begin (std::string const& key) = 0; - virtual void array_end () = 0; + virtual void + array_begin() = 0; + virtual void + array_begin(std::string const& key) = 0; + virtual void + array_end() = 0; - virtual void add (std::string const& value) = 0; + virtual void + add(std::string const& value) = 0; - void add (char const* value) + void + add(char const* value) { - add (std::string (value)); + add(std::string(value)); } template - void lexical_add (Value value) + void + lexical_add(Value value) { std::stringstream ss; ss << value; - add (ss.str()); + add(ss.str()); } - virtual void add (bool value); - virtual void add (char value); - virtual void add (signed char value); - virtual void add (unsigned char value); - virtual void add (wchar_t value); + virtual void + add(bool value); + virtual void + add(char value); + virtual void + add(signed char value); + virtual void + add(unsigned char value); + virtual void + add(wchar_t value); #if 0 virtual void add (char16_t value); virtual void add (char32_t value); #endif - virtual void add (short value); - virtual void add (unsigned short value); - virtual void add (int value); - virtual void add (unsigned int value); - virtual void add (long value); - virtual void add (unsigned long value); - virtual void add (long long value); - virtual void add (unsigned long long value); - virtual void add (float value); - virtual void add (double value); - virtual void add (long double value); + virtual void + add(short value); + virtual void + add(unsigned short value); + virtual void + add(int value); + virtual void + add(unsigned int value); + virtual void + add(long value); + virtual void + add(unsigned long value); + virtual void + add(long long value); + virtual void + add(unsigned long long value); + virtual void + add(float value); + virtual void + add(double value); + virtual void + add(long double value); private: class Item; @@ -135,13 +179,17 @@ private: // //------------------------------------------------------------------------------ -class PropertyStream::Item : public List ::Node +class PropertyStream::Item : public List::Node { public: - explicit Item (Source* source); - Source& source() const; - Source* operator-> () const; - Source& operator* () const; + explicit Item(Source* source); + Source& + source() const; + Source* + operator->() const; + Source& + operator*() const; + private: Source* m_source; }; @@ -160,17 +208,20 @@ private: std::ostringstream mutable m_ostream; public: - Proxy (Map const& map, std::string const& key); - Proxy (Proxy const& other); - ~Proxy (); + Proxy(Map const& map, std::string const& key); + Proxy(Proxy const& other); + ~Proxy(); template - Proxy& operator= (Value value); + Proxy& + operator=(Value value); - std::ostream& operator<< (std::ostream& manip (std::ostream&)) const; + std::ostream& + operator<<(std::ostream& manip(std::ostream&)) const; template - std::ostream& operator<< (T const& t) const + std::ostream& + operator<<(T const& t) const { return m_ostream << t; } @@ -188,52 +239,63 @@ private: PropertyStream& m_stream; public: - explicit Map (PropertyStream& stream); - explicit Map (Set& parent); - Map (std::string const& key, Map& parent); - Map (std::string const& key, PropertyStream& stream); - ~Map (); + explicit Map(PropertyStream& stream); + explicit Map(Set& parent); + Map(std::string const& key, Map& parent); + Map(std::string const& key, PropertyStream& stream); + ~Map(); Map(Map const&) = delete; - Map& operator= (Map const&) = delete; + Map& + operator=(Map const&) = delete; - PropertyStream& stream(); - PropertyStream const& stream() const; + PropertyStream& + stream(); + PropertyStream const& + stream() const; template - void add (std::string const& key, Value value) const + void + add(std::string const& key, Value value) const { - m_stream.add (key, value); + m_stream.add(key, value); } template - void add (Key key, Value value) const + void + add(Key key, Value value) const { std::stringstream ss; ss << key; - add (ss.str(), value); + add(ss.str(), value); } - Proxy operator[] (std::string const& key); + Proxy + operator[](std::string const& key); - Proxy operator[] (char const* key) - { return Proxy (*this, key); } + Proxy + operator[](char const* key) + { + return Proxy(*this, key); + } template - Proxy operator[] (Key key) const + Proxy + operator[](Key key) const { std::stringstream ss; ss << key; - return Proxy (*this, ss.str()); + return Proxy(*this, ss.str()); } }; //-------------------------------------------------------------------------- template -PropertyStream::Proxy& PropertyStream::Proxy::operator= (Value value) +PropertyStream::Proxy& +PropertyStream::Proxy::operator=(Value value) { - m_map->add (m_key, value); + m_map->add(m_key, value); return *this; } @@ -249,19 +311,25 @@ private: PropertyStream& m_stream; public: - Set (std::string const& key, Map& map); - Set (std::string const& key, PropertyStream& stream); - ~Set (); + Set(std::string const& key, Map& map); + Set(std::string const& key, PropertyStream& stream); + ~Set(); - Set (Set const&) = delete; - Set& operator= (Set const&) = delete; + Set(Set const&) = delete; + Set& + operator=(Set const&) = delete; - PropertyStream& stream(); - PropertyStream const& stream() const; + PropertyStream& + stream(); + PropertyStream const& + stream() const; template - void add (Value value) const - { m_stream.add (value); } + void + add(Value value) const + { + m_stream.add(value); + } }; //------------------------------------------------------------------------------ @@ -278,49 +346,58 @@ private: std::recursive_mutex lock_; Item item_; Source* parent_; - List children_; + List children_; public: - explicit Source (std::string const& name); - virtual ~Source (); + explicit Source(std::string const& name); + virtual ~Source(); - Source (Source const&) = delete; - Source& operator= (Source const&) = delete; + Source(Source const&) = delete; + Source& + operator=(Source const&) = delete; /** Returns the name of this source. */ - std::string const& name() const; + std::string const& + name() const; /** Add a child source. */ - void add (Source& source); + void + add(Source& source); /** Add a child source by pointer. The source pointer is returned so it can be used in ctor-initializers. */ template - Derived* add (Derived* child) + Derived* + add(Derived* child) { - add (*static_cast (child)); + add(*static_cast(child)); return child; } /** Remove a child source from this Source. */ - void remove (Source& child); + void + remove(Source& child); /** Remove all child sources from this Source. */ - void removeAll (); + void + removeAll(); /** Write only this Source to the stream. */ - void write_one (PropertyStream& stream); + void + write_one(PropertyStream& stream); /** write this source and all its children recursively to the stream. */ - void write (PropertyStream& stream); + void + write(PropertyStream& stream); /** Parse the path and write the corresponding Source and optional children. If the source is found, it is written. If the wildcard character '*' exists as the last character in the path, then all the children are written recursively. */ - void write (PropertyStream& stream, std::string const& path); + void + write(PropertyStream& stream, std::string const& path); /** Parse the dot-delimited Source path and return the result. The first value will be a pointer to the Source object corresponding @@ -334,27 +411,35 @@ public: "parent.child." start at the parent and print down to child "parent.grandchild" prints nothing- grandchild not direct discendent "parent.grandchild." starts at the parent and prints down to grandchild - "parent.grandchild.*" starts at parent, print through grandchild children + "parent.grandchild.*" starts at parent, print through grandchild + children */ - std::pair find (std::string path); + std::pair + find(std::string path); - Source* find_one_deep (std::string const& name); - PropertyStream::Source* find_path(std::string path); - PropertyStream::Source* find_one(std::string const& name); - - static bool peel_leading_slash (std::string* path); - static bool peel_trailing_slashstar (std::string* path); - static std::string peel_name(std::string* path); + Source* + find_one_deep(std::string const& name); + PropertyStream::Source* + find_path(std::string path); + PropertyStream::Source* + find_one(std::string const& name); + static bool + peel_leading_slash(std::string* path); + static bool + peel_trailing_slashstar(std::string* path); + static std::string + peel_name(std::string* path); //-------------------------------------------------------------------------- /** Subclass override. The default version does nothing. */ - virtual void onWrite (Map&); + virtual void + onWrite(Map&); }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/utility/WrappedSink.h b/src/ripple/beast/utility/WrappedSink.h index 9a10756743..ba714248a0 100644 --- a/src/ripple/beast/utility/WrappedSink.h +++ b/src/ripple/beast/utility/WrappedSink.h @@ -37,40 +37,42 @@ private: std::string prefix_; public: - explicit - WrappedSink (beast::Journal::Sink& sink, std::string const& prefix = "") - : Sink (sink) - , sink_(sink) - , prefix_(prefix) + explicit WrappedSink( + beast::Journal::Sink& sink, + std::string const& prefix = "") + : Sink(sink), sink_(sink), prefix_(prefix) { } - explicit - WrappedSink (beast::Journal const& journal, std::string const& prefix = "") - : WrappedSink (journal.sink(), prefix) + explicit WrappedSink( + beast::Journal const& journal, + std::string const& prefix = "") + : WrappedSink(journal.sink(), prefix) { } - void prefix (std::string const& s) + void + prefix(std::string const& s) { prefix_ = s; } bool - active (beast::severities::Severity level) const override + active(beast::severities::Severity level) const override { - return sink_.active (level); + return sink_.active(level); } bool - console () const override + console() const override { - return sink_.console (); + return sink_.console(); } - void console (bool output) override + void + console(bool output) override { - sink_.console (output); + sink_.console(output); } beast::severities::Severity @@ -79,18 +81,20 @@ public: return sink_.threshold(); } - void threshold (beast::severities::Severity thresh) override + void + threshold(beast::severities::Severity thresh) override { - sink_.threshold (thresh); + sink_.threshold(thresh); } - void write (beast::severities::Severity level, std::string const& text) override + void + write(beast::severities::Severity level, std::string const& text) override { using beast::Journal; - sink_.write (level, prefix_ + text); + sink_.write(level, prefix_ + text); } }; -} +} // namespace beast #endif diff --git a/src/ripple/beast/utility/Zero.h b/src/ripple/beast/utility/Zero.h index d231747499..efd4d6d667 100644 --- a/src/ripple/beast/utility/Zero.h +++ b/src/ripple/beast/utility/Zero.h @@ -50,7 +50,8 @@ static constexpr Zero zero{}; /** Default implementation of signum calls the method on the class. */ template -auto signum(T const& t) +auto +signum(T const& t) { return t.signum(); } @@ -61,48 +62,55 @@ namespace zero_helper { // For argument dependent lookup to function properly, calls to signum must // be made from a namespace that does not include overloads of the function.. template -auto call_signum(T const& t) +auto +call_signum(T const& t) { return signum(t); } -} // zero_helper -} // detail +} // namespace zero_helper +} // namespace detail // Handle operators where T is on the left side using signum. template -bool operator==(T const& t, Zero) +bool +operator==(T const& t, Zero) { return detail::zero_helper::call_signum(t) == 0; } template -bool operator!=(T const& t, Zero) +bool +operator!=(T const& t, Zero) { return detail::zero_helper::call_signum(t) != 0; } template -bool operator<(T const& t, Zero) +bool +operator<(T const& t, Zero) { return detail::zero_helper::call_signum(t) < 0; } template -bool operator>(T const& t, Zero) +bool +operator>(T const& t, Zero) { return detail::zero_helper::call_signum(t) > 0; } template -bool operator>=(T const& t, Zero) +bool +operator>=(T const& t, Zero) { return detail::zero_helper::call_signum(t) >= 0; } template -bool operator<=(T const& t, Zero) +bool +operator<=(T const& t, Zero) { return detail::zero_helper::call_signum(t) <= 0; } @@ -111,41 +119,47 @@ bool operator<=(T const& t, Zero) // reversing the operation, so that T is on the left side. template -bool operator==(Zero, T const& t) +bool +operator==(Zero, T const& t) { return t == zero; } template -bool operator!=(Zero, T const& t) +bool +operator!=(Zero, T const& t) { return t != zero; } template -bool operator<(Zero, T const& t) +bool +operator<(Zero, T const& t) { return t > zero; } template -bool operator>(Zero, T const& t) +bool +operator>(Zero, T const& t) { return t < zero; } template -bool operator>=(Zero, T const& t) +bool +operator>=(Zero, T const& t) { return t <= zero; } template -bool operator<=(Zero, T const& t) +bool +operator<=(Zero, T const& t) { return t >= zero; } -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/utility/hash_pair.h b/src/ripple/beast/utility/hash_pair.h index 74c3bd44e6..08042b3477 100644 --- a/src/ripple/beast/utility/hash_pair.h +++ b/src/ripple/beast/utility/hash_pair.h @@ -30,41 +30,43 @@ namespace std { /** Specialization of std::hash for any std::pair type. */ template -struct hash > - : private boost::base_from_member , 0> - , private boost::base_from_member , 1> +struct hash> + : private boost::base_from_member, 0>, + private boost::base_from_member, 1> { private: - using first_hash = boost::base_from_member , 0>; - using second_hash = boost::base_from_member , 1>; + using first_hash = boost::base_from_member, 0>; + using second_hash = boost::base_from_member, 1>; public: - hash () + hash() { } - hash (std::hash const& first_hash_, - std::hash const& second_hash_) - : first_hash (first_hash_) - , second_hash (second_hash_) + hash( + std::hash const& first_hash_, + std::hash const& second_hash_) + : first_hash(first_hash_), second_hash(second_hash_) { } - std::size_t operator() (std::pair const& value) + std::size_t + operator()(std::pair const& value) { - std::size_t result (first_hash::member (value.first)); - boost::hash_combine (result, second_hash::member (value.second)); + std::size_t result(first_hash::member(value.first)); + boost::hash_combine(result, second_hash::member(value.second)); return result; } - std::size_t operator() (std::pair const& value) const + std::size_t + operator()(std::pair const& value) const { - std::size_t result (first_hash::member (value.first)); - boost::hash_combine (result, second_hash::member (value.second)); + std::size_t result(first_hash::member(value.first)); + boost::hash_combine(result, second_hash::member(value.second)); return result; } }; -} +} // namespace std #endif diff --git a/src/ripple/beast/utility/maybe_const.h b/src/ripple/beast/utility/maybe_const.h index 9cddbf4178..c1c01490c9 100644 --- a/src/ripple/beast/utility/maybe_const.h +++ b/src/ripple/beast/utility/maybe_const.h @@ -29,15 +29,16 @@ template struct maybe_const { explicit maybe_const() = default; - using type = typename std::conditional ::type const, - typename std::remove_const ::type>::type; + using type = typename std::conditional< + IsConst, + typename std::remove_const::type const, + typename std::remove_const::type>::type; }; /** Alias for omitting `typename`. */ template -using maybe_const_t = typename maybe_const ::type; +using maybe_const_t = typename maybe_const::type; -} +} // namespace beast #endif diff --git a/src/ripple/beast/utility/rngfill.h b/src/ripple/beast/utility/rngfill.h index d2bd7b5fda..cb47679dd2 100644 --- a/src/ripple/beast/utility/rngfill.h +++ b/src/ripple/beast/utility/rngfill.h @@ -29,17 +29,14 @@ namespace beast { template void -rngfill (void* buffer, std::size_t bytes, - Generator& g) +rngfill(void* buffer, std::size_t bytes, Generator& g) { - using result_type = - typename Generator::result_type; + using result_type = typename Generator::result_type; while (bytes >= sizeof(result_type)) { auto const v = g(); std::memcpy(buffer, &v, sizeof(v)); - buffer = reinterpret_cast< - std::uint8_t*>(buffer) + sizeof(v); + buffer = reinterpret_cast(buffer) + sizeof(v); bytes -= sizeof(v); } if (bytes > 0) @@ -49,21 +46,20 @@ rngfill (void* buffer, std::size_t bytes, } } -template > +template < + class Generator, + std::size_t N, + class = std::enable_if_t> void -rngfill (std::array& a, Generator& g) +rngfill(std::array& a, Generator& g) { - using result_type = - typename Generator::result_type; + using result_type = typename Generator::result_type; auto i = N / sizeof(result_type); - result_type* p = - reinterpret_cast(a.data()); + result_type* p = reinterpret_cast(a.data()); while (i--) *p++ = g(); } -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/utility/src/beast_Journal.cpp b/src/ripple/beast/utility/src/beast_Journal.cpp index ec74e0d8e9..7c332bf6b5 100644 --- a/src/ripple/beast/utility/src/beast_Journal.cpp +++ b/src/ripple/beast/utility/src/beast_Journal.cpp @@ -28,43 +28,48 @@ namespace beast { class NullJournalSink : public Journal::Sink { public: - NullJournalSink () - : Sink (severities::kDisabled, false) - { } + NullJournalSink() : Sink(severities::kDisabled, false) + { + } ~NullJournalSink() override = default; - bool active (severities::Severity) const override + bool active(severities::Severity) const override { return false; } - bool console() const override + bool + console() const override { return false; } - void console (bool) override + void + console(bool) override { } - severities::Severity threshold() const override + severities::Severity + threshold() const override { return severities::kDisabled; } - void threshold (severities::Severity) override + void threshold(severities::Severity) override { } - void write (severities::Severity, std::string const&) override + void + write(severities::Severity, std::string const&) override { } }; //------------------------------------------------------------------------------ -Journal::Sink& Journal::getNullSink () +Journal::Sink& +Journal::getNullSink() { static NullJournalSink sink; return sink; @@ -72,82 +77,84 @@ Journal::Sink& Journal::getNullSink () //------------------------------------------------------------------------------ -Journal::Sink::Sink (Severity thresh, bool console) - : thresh_ (thresh) - , m_console (console) +Journal::Sink::Sink(Severity thresh, bool console) + : thresh_(thresh), m_console(console) { } Journal::Sink::~Sink() = default; -bool Journal::Sink::active (Severity level) const +bool +Journal::Sink::active(Severity level) const { return level >= thresh_; } -bool Journal::Sink::console () const +bool +Journal::Sink::console() const { return m_console; } -void Journal::Sink::console (bool output) +void +Journal::Sink::console(bool output) { m_console = output; } -severities::Severity Journal::Sink::threshold () const +severities::Severity +Journal::Sink::threshold() const { return thresh_; } -void Journal::Sink::threshold (Severity thresh) +void +Journal::Sink::threshold(Severity thresh) { thresh_ = thresh; } //------------------------------------------------------------------------------ -Journal::ScopedStream::ScopedStream (Sink& sink, Severity level) - : m_sink (sink) - , m_level (level) +Journal::ScopedStream::ScopedStream(Sink& sink, Severity level) + : m_sink(sink), m_level(level) { // Modifiers applied from all ctors - m_ostream - << std::boolalpha - << std::showbase; + m_ostream << std::boolalpha << std::showbase; } -Journal::ScopedStream::ScopedStream ( - Stream const& stream, std::ostream& manip (std::ostream&)) - : ScopedStream (stream.sink(), stream.level()) +Journal::ScopedStream::ScopedStream( + Stream const& stream, + std::ostream& manip(std::ostream&)) + : ScopedStream(stream.sink(), stream.level()) { m_ostream << manip; } -Journal::ScopedStream::~ScopedStream () +Journal::ScopedStream::~ScopedStream() { - std::string const& s (m_ostream.str()); - if (! s.empty ()) + std::string const& s(m_ostream.str()); + if (!s.empty()) { if (s == "\n") - m_sink.write (m_level, ""); + m_sink.write(m_level, ""); else - m_sink.write (m_level, s); + m_sink.write(m_level, s); } } -std::ostream& Journal::ScopedStream::operator<< (std::ostream& manip (std::ostream&)) const +std::ostream& +Journal::ScopedStream::operator<<(std::ostream& manip(std::ostream&)) const { return m_ostream << manip; } //------------------------------------------------------------------------------ -Journal::ScopedStream Journal::Stream::operator<< ( - std::ostream& manip (std::ostream&)) const +Journal::ScopedStream +Journal::Stream::operator<<(std::ostream& manip(std::ostream&)) const { - return ScopedStream (*this, manip); + return ScopedStream(*this, manip); } -} // beast - +} // namespace beast diff --git a/src/ripple/beast/utility/src/beast_PropertyStream.cpp b/src/ripple/beast/utility/src/beast_PropertyStream.cpp index 2193244853..70c5ab9a80 100644 --- a/src/ripple/beast/utility/src/beast_PropertyStream.cpp +++ b/src/ripple/beast/utility/src/beast_PropertyStream.cpp @@ -20,8 +20,8 @@ #include #include #include -#include #include +#include namespace beast { @@ -31,22 +31,24 @@ namespace beast { // //------------------------------------------------------------------------------ -PropertyStream::Item::Item (Source* source) - : m_source (source) +PropertyStream::Item::Item(Source* source) : m_source(source) { } -PropertyStream::Source& PropertyStream::Item::source() const +PropertyStream::Source& +PropertyStream::Item::source() const { return *m_source; } -PropertyStream::Source* PropertyStream::Item::operator-> () const +PropertyStream::Source* +PropertyStream::Item::operator->() const { return &source(); } -PropertyStream::Source& PropertyStream::Item::operator* () const +PropertyStream::Source& +PropertyStream::Item::operator*() const { return source(); } @@ -57,28 +59,25 @@ PropertyStream::Source& PropertyStream::Item::operator* () const // //------------------------------------------------------------------------------ -PropertyStream::Proxy::Proxy ( - Map const& map, std::string const& key) - : m_map (&map) - , m_key (key) +PropertyStream::Proxy::Proxy(Map const& map, std::string const& key) + : m_map(&map), m_key(key) { } -PropertyStream::Proxy::Proxy (Proxy const& other) - : m_map (other.m_map) - , m_key (other.m_key) +PropertyStream::Proxy::Proxy(Proxy const& other) + : m_map(other.m_map), m_key(other.m_key) { } -PropertyStream::Proxy::~Proxy () +PropertyStream::Proxy::~Proxy() { - std::string const s (m_ostream.str()); - if (! s.empty()) - m_map->add (m_key, s); + std::string const s(m_ostream.str()); + if (!s.empty()) + m_map->add(m_key, s); } -std::ostream& PropertyStream::Proxy::operator<< ( - std::ostream& manip (std::ostream&)) const +std::ostream& +PropertyStream::Proxy::operator<<(std::ostream& manip(std::ostream&)) const { return m_ostream << manip; } @@ -89,47 +88,48 @@ std::ostream& PropertyStream::Proxy::operator<< ( // //------------------------------------------------------------------------------ -PropertyStream::Map::Map (PropertyStream& stream) - : m_stream (stream) +PropertyStream::Map::Map(PropertyStream& stream) : m_stream(stream) { } -PropertyStream::Map::Map (Set& parent) - : m_stream (parent.stream()) +PropertyStream::Map::Map(Set& parent) : m_stream(parent.stream()) { - m_stream.map_begin (); + m_stream.map_begin(); } -PropertyStream::Map::Map (std::string const& key, Map& map) - : m_stream (map.stream()) +PropertyStream::Map::Map(std::string const& key, Map& map) + : m_stream(map.stream()) { - m_stream.map_begin (key); + m_stream.map_begin(key); } -PropertyStream::Map::Map (std::string const& key, PropertyStream& stream) - : m_stream (stream) +PropertyStream::Map::Map(std::string const& key, PropertyStream& stream) + : m_stream(stream) { - m_stream.map_begin (key); + m_stream.map_begin(key); } -PropertyStream::Map::~Map () +PropertyStream::Map::~Map() { - m_stream.map_end (); + m_stream.map_end(); } -PropertyStream& PropertyStream::Map::stream() +PropertyStream& +PropertyStream::Map::stream() { return m_stream; } -PropertyStream const& PropertyStream::Map::stream() const +PropertyStream const& +PropertyStream::Map::stream() const { return m_stream; } -PropertyStream::Proxy PropertyStream::Map::operator[] (std::string const& key) +PropertyStream::Proxy +PropertyStream::Map::operator[](std::string const& key) { - return Proxy (*this, key); + return Proxy(*this, key); } //------------------------------------------------------------------------------ @@ -138,29 +138,31 @@ PropertyStream::Proxy PropertyStream::Map::operator[] (std::string const& key) // //------------------------------------------------------------------------------ -PropertyStream::Set::Set (std::string const& key, Map& map) - : m_stream (map.stream()) +PropertyStream::Set::Set(std::string const& key, Map& map) + : m_stream(map.stream()) { - m_stream.array_begin (key); + m_stream.array_begin(key); } -PropertyStream::Set::Set (std::string const& key, PropertyStream& stream) - : m_stream (stream) +PropertyStream::Set::Set(std::string const& key, PropertyStream& stream) + : m_stream(stream) { - m_stream.array_begin (key); + m_stream.array_begin(key); } -PropertyStream::Set::~Set () +PropertyStream::Set::~Set() { - m_stream.array_end (); + m_stream.array_end(); } -PropertyStream& PropertyStream::Set::stream() +PropertyStream& +PropertyStream::Set::stream() { return m_stream; } -PropertyStream const& PropertyStream::Set::stream() const +PropertyStream const& +PropertyStream::Set::stream() const { return m_stream; } @@ -171,122 +173,128 @@ PropertyStream const& PropertyStream::Set::stream() const // //------------------------------------------------------------------------------ -PropertyStream::Source::Source (std::string const& name) - : m_name (name) - , item_ (this) - , parent_ (nullptr) +PropertyStream::Source::Source(std::string const& name) + : m_name(name), item_(this), parent_(nullptr) { } -PropertyStream::Source::~Source () +PropertyStream::Source::~Source() { std::lock_guard _(lock_); if (parent_ != nullptr) - parent_->remove (*this); - removeAll (); + parent_->remove(*this); + removeAll(); } -std::string const& PropertyStream::Source::name () const +std::string const& +PropertyStream::Source::name() const { return m_name; } -void PropertyStream::Source::add (Source& source) +void +PropertyStream::Source::add(Source& source) { std::lock(lock_, source.lock_); std::lock_guard lk1(lock_, std::adopt_lock); std::lock_guard lk2(source.lock_, std::adopt_lock); - assert (source.parent_ == nullptr); - children_.push_back (source.item_); + assert(source.parent_ == nullptr); + children_.push_back(source.item_); source.parent_ = this; } -void PropertyStream::Source::remove (Source& child) +void +PropertyStream::Source::remove(Source& child) { std::lock(lock_, child.lock_); std::lock_guard lk1(lock_, std::adopt_lock); std::lock_guard lk2(child.lock_, std::adopt_lock); - assert (child.parent_ == this); - children_.erase ( - children_.iterator_to ( - child.item_)); + assert(child.parent_ == this); + children_.erase(children_.iterator_to(child.item_)); child.parent_ = nullptr; } -void PropertyStream::Source::removeAll () +void +PropertyStream::Source::removeAll() { std::lock_guard _(lock_); - for (auto iter = children_.begin(); iter != children_.end(); ) + for (auto iter = children_.begin(); iter != children_.end();) { std::lock_guard _cl((*iter)->lock_); - remove (*(*iter)); + remove(*(*iter)); } } //------------------------------------------------------------------------------ -void PropertyStream::Source::write_one (PropertyStream& stream) +void +PropertyStream::Source::write_one(PropertyStream& stream) { - Map map (m_name, stream); - onWrite (map); + Map map(m_name, stream); + onWrite(map); } -void PropertyStream::Source::write (PropertyStream& stream) +void +PropertyStream::Source::write(PropertyStream& stream) { - Map map (m_name, stream); - onWrite (map); + Map map(m_name, stream); + onWrite(map); std::lock_guard _(lock_); for (auto& child : children_) - child.source().write (stream); + child.source().write(stream); } -void PropertyStream::Source::write (PropertyStream& stream, std::string const& path) +void +PropertyStream::Source::write(PropertyStream& stream, std::string const& path) { - std::pair result (find (path)); + std::pair result(find(path)); if (result.first == nullptr) return; if (result.second) - result.first->write (stream); + result.first->write(stream); else - result.first->write_one (stream); + result.first->write_one(stream); } -std::pair PropertyStream::Source::find (std::string path) +std::pair +PropertyStream::Source::find(std::string path) { - bool const deep (peel_trailing_slashstar (&path)); - bool const rooted (peel_leading_slash (&path)); - Source* source (this); - if (! path.empty()) + bool const deep(peel_trailing_slashstar(&path)); + bool const rooted(peel_leading_slash(&path)); + Source* source(this); + if (!path.empty()) { - if (! rooted) + if (!rooted) { - std::string const name (peel_name (&path)); - source = find_one_deep (name); + std::string const name(peel_name(&path)); + source = find_one_deep(name); if (source == nullptr) - return std::make_pair (nullptr, deep); + return std::make_pair(nullptr, deep); } - source = source->find_path (path); + source = source->find_path(path); } - return std::make_pair (source, deep); + return std::make_pair(source, deep); } -bool PropertyStream::Source::peel_leading_slash (std::string* path) +bool +PropertyStream::Source::peel_leading_slash(std::string* path) { - if (! path->empty() && path->front() == '/') + if (!path->empty() && path->front() == '/') { - *path = std::string (path->begin() + 1, path->end()); + *path = std::string(path->begin() + 1, path->end()); return true; } return false; } -bool PropertyStream::Source::peel_trailing_slashstar (std::string* path) +bool +PropertyStream::Source::peel_trailing_slashstar(std::string* path) { bool found(false); if (path->empty()) @@ -296,65 +304,68 @@ bool PropertyStream::Source::peel_trailing_slashstar (std::string* path) found = true; path->pop_back(); } - if(! path->empty() && path->back() == '/') + if (!path->empty() && path->back() == '/') path->pop_back(); return found; } -std::string PropertyStream::Source::peel_name (std::string* path) +std::string +PropertyStream::Source::peel_name(std::string* path) { if (path->empty()) return ""; std::string::const_iterator first = (*path).begin(); std::string::const_iterator last = (*path).end(); - std::string::const_iterator pos = std::find (first, last, '/'); - std::string s (first, pos); + std::string::const_iterator pos = std::find(first, last, '/'); + std::string s(first, pos); if (pos != last) - *path = std::string (pos+1, last); + *path = std::string(pos + 1, last); else - *path = std::string (); + *path = std::string(); return s; } // Recursive search through the whole tree until name is found -PropertyStream::Source* PropertyStream::Source::find_one_deep (std::string const& name) +PropertyStream::Source* +PropertyStream::Source::find_one_deep(std::string const& name) { - Source* found = find_one (name); + Source* found = find_one(name); if (found != nullptr) return found; std::lock_guard _(lock_); for (auto& s : children_) { - found = s.source().find_one_deep (name); + found = s.source().find_one_deep(name); if (found != nullptr) return found; } return nullptr; } -PropertyStream::Source* PropertyStream::Source::find_path (std::string path) +PropertyStream::Source* +PropertyStream::Source::find_path(std::string path) { if (path.empty()) return this; - Source* source (this); + Source* source(this); do { - std::string const name (peel_name (&path)); - if(name.empty ()) + std::string const name(peel_name(&path)); + if (name.empty()) break; source = source->find_one(name); - } - while (source != nullptr); + } while (source != nullptr); return source; } // This function only looks at immediate children // If no immediate children match, then return nullptr -PropertyStream::Source* PropertyStream::Source::find_one (std::string const& name) +PropertyStream::Source* +PropertyStream::Source::find_one(std::string const& name) { std::lock_guard _(lock_); for (auto& s : children_) @@ -365,7 +376,8 @@ PropertyStream::Source* PropertyStream::Source::find_one (std::string const& nam return nullptr; } -void PropertyStream::Source::onWrite (Map&) +void +PropertyStream::Source::onWrite(Map&) { } @@ -375,32 +387,37 @@ void PropertyStream::Source::onWrite (Map&) // //------------------------------------------------------------------------------ -void PropertyStream::add (std::string const& key, bool value) +void +PropertyStream::add(std::string const& key, bool value) { if (value) - add (key, "true"); + add(key, "true"); else - add (key, "false"); + add(key, "false"); } -void PropertyStream::add (std::string const& key, char value) +void +PropertyStream::add(std::string const& key, char value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, signed char value) +void +PropertyStream::add(std::string const& key, signed char value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, unsigned char value) +void +PropertyStream::add(std::string const& key, unsigned char value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, wchar_t value) +void +PropertyStream::add(std::string const& key, wchar_t value) { - lexical_add (key, value); + lexical_add(key, value); } #if 0 @@ -415,87 +432,103 @@ void PropertyStream::add (std::string const& key, char32_t value) } #endif -void PropertyStream::add (std::string const& key, short value) +void +PropertyStream::add(std::string const& key, short value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, unsigned short value) +void +PropertyStream::add(std::string const& key, unsigned short value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, int value) +void +PropertyStream::add(std::string const& key, int value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, unsigned int value) +void +PropertyStream::add(std::string const& key, unsigned int value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, long value) +void +PropertyStream::add(std::string const& key, long value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, unsigned long value) +void +PropertyStream::add(std::string const& key, unsigned long value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, long long value) +void +PropertyStream::add(std::string const& key, long long value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, unsigned long long value) +void +PropertyStream::add(std::string const& key, unsigned long long value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, float value) +void +PropertyStream::add(std::string const& key, float value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, double value) +void +PropertyStream::add(std::string const& key, double value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (std::string const& key, long double value) +void +PropertyStream::add(std::string const& key, long double value) { - lexical_add (key, value); + lexical_add(key, value); } -void PropertyStream::add (bool value) +void +PropertyStream::add(bool value) { if (value) - add ("true"); + add("true"); else - add ("false"); + add("false"); } -void PropertyStream::add (char value) +void +PropertyStream::add(char value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (signed char value) +void +PropertyStream::add(signed char value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (unsigned char value) +void +PropertyStream::add(unsigned char value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (wchar_t value) +void +PropertyStream::add(wchar_t value) { - lexical_add (value); + lexical_add(value); } #if 0 @@ -510,60 +543,70 @@ void PropertyStream::add (char32_t value) } #endif -void PropertyStream::add (short value) +void +PropertyStream::add(short value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (unsigned short value) +void +PropertyStream::add(unsigned short value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (int value) +void +PropertyStream::add(int value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (unsigned int value) +void +PropertyStream::add(unsigned int value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (long value) +void +PropertyStream::add(long value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (unsigned long value) +void +PropertyStream::add(unsigned long value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (long long value) +void +PropertyStream::add(long long value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (unsigned long long value) +void +PropertyStream::add(unsigned long long value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (float value) +void +PropertyStream::add(float value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (double value) +void +PropertyStream::add(double value) { - lexical_add (value); + lexical_add(value); } -void PropertyStream::add (long double value) +void +PropertyStream::add(long double value) { - lexical_add (value); -} - + lexical_add(value); } +} // namespace beast diff --git a/src/ripple/beast/utility/temp_dir.h b/src/ripple/beast/utility/temp_dir.h index 63cb4e4154..689b2d1efb 100644 --- a/src/ripple/beast/utility/temp_dir.h +++ b/src/ripple/beast/utility/temp_dir.h @@ -35,23 +35,21 @@ class temp_dir boost::filesystem::path path_; public: -#if ! GENERATING_DOCS +#if !GENERATING_DOCS temp_dir(const temp_dir&) = delete; - temp_dir& operator=(const temp_dir&) = delete; + temp_dir& + operator=(const temp_dir&) = delete; #endif /// Construct a temporary directory. temp_dir() { - auto const dir = - boost::filesystem::temp_directory_path(); + auto const dir = boost::filesystem::temp_directory_path(); do { - path_ = - dir / boost::filesystem::unique_path(); - } - while(boost::filesystem::exists(path_)); - boost::filesystem::create_directory (path_); + path_ = dir / boost::filesystem::unique_path(); + } while (boost::filesystem::exists(path_)); + boost::filesystem::create_directory(path_); } /// Destroy a temporary directory. @@ -81,6 +79,6 @@ public: } }; -} // beast +} // namespace beast #endif diff --git a/src/ripple/beast/xor_shift_engine.h b/src/ripple/beast/xor_shift_engine.h index 5b917c5feb..77012980c8 100644 --- a/src/ripple/beast/xor_shift_engine.h +++ b/src/ripple/beast/xor_shift_engine.h @@ -35,27 +35,23 @@ public: using result_type = std::uint64_t; xor_shift_engine(xor_shift_engine const&) = default; - xor_shift_engine& operator=(xor_shift_engine const&) = default; + xor_shift_engine& + operator=(xor_shift_engine const&) = default; - explicit - xor_shift_engine (result_type val = 1977u); + explicit xor_shift_engine(result_type val = 1977u); void - seed (result_type seed); + seed(result_type seed); result_type operator()(); - static - result_type constexpr - min() + static result_type constexpr min() { return std::numeric_limits::min(); } - static - result_type constexpr - max() + static result_type constexpr max() { return std::numeric_limits::max(); } @@ -63,32 +59,29 @@ public: private: result_type s_[2]; - static - result_type - murmurhash3 (result_type x); + static result_type + murmurhash3(result_type x); }; template -xor_shift_engine<_>::xor_shift_engine ( - result_type val) +xor_shift_engine<_>::xor_shift_engine(result_type val) { - seed (val); + seed(val); } template void -xor_shift_engine<_>::seed (result_type seed) +xor_shift_engine<_>::seed(result_type seed) { if (seed == 0) throw std::domain_error("invalid seed"); - s_[0] = murmurhash3 (seed); - s_[1] = murmurhash3 (s_[0]); + s_[0] = murmurhash3(seed); + s_[1] = murmurhash3(s_[0]); } template auto -xor_shift_engine<_>::operator()() -> - result_type +xor_shift_engine<_>::operator()() -> result_type { result_type s1 = s_[0]; result_type const s0 = s_[1]; @@ -99,8 +92,7 @@ xor_shift_engine<_>::operator()() -> template auto -xor_shift_engine<_>::murmurhash3 (result_type x) - -> result_type +xor_shift_engine<_>::murmurhash3(result_type x) -> result_type { x ^= x >> 33; x *= 0xff51afd7ed558ccdULL; @@ -109,7 +101,7 @@ xor_shift_engine<_>::murmurhash3 (result_type x) return x ^= x >> 33; } -} // detail +} // namespace detail /** XOR-shift Generator. @@ -121,6 +113,6 @@ xor_shift_engine<_>::murmurhash3 (result_type x) */ using xor_shift_engine = detail::xor_shift_engine<>; -} +} // namespace beast #endif diff --git a/src/ripple/conditions/Condition.h b/src/ripple/conditions/Condition.h index 0c92a5ea95..340b7c9612 100644 --- a/src/ripple/conditions/Condition.h +++ b/src/ripple/conditions/Condition.h @@ -33,9 +33,7 @@ namespace ripple { namespace cryptoconditions { -enum class Type - : std::uint8_t -{ +enum class Type : std::uint8_t { preimageSha256 = 0, prefixSha256 = 1, thresholdSha256 = 2, @@ -65,8 +63,7 @@ public: https://tools.ietf.org/html/draft-thomas-crypto-conditions-02#section-7.2 */ - static - std::unique_ptr + static std::unique_ptr deserialize(Slice s, std::error_code& ec); public: @@ -86,16 +83,12 @@ public: std::set subtypes; Condition(Type t, std::uint32_t c, Slice fp) - : type(t) - , fingerprint(fp) - , cost(c) + : type(t), fingerprint(fp), cost(c) { } Condition(Type t, std::uint32_t c, Buffer&& fp) - : type(t) - , fingerprint(std::move(fp)) - , cost(c) + : type(t), fingerprint(std::move(fp)), cost(c) { } @@ -107,26 +100,21 @@ public: Condition() = delete; }; -inline -bool -operator== (Condition const& lhs, Condition const& rhs) +inline bool +operator==(Condition const& lhs, Condition const& rhs) { - return - lhs.type == rhs.type && - lhs.cost == rhs.cost && - lhs.subtypes == rhs.subtypes && - lhs.fingerprint == rhs.fingerprint; + return lhs.type == rhs.type && lhs.cost == rhs.cost && + lhs.subtypes == rhs.subtypes && lhs.fingerprint == rhs.fingerprint; } -inline -bool -operator!= (Condition const& lhs, Condition const& rhs) +inline bool +operator!=(Condition const& lhs, Condition const& rhs) { return !(lhs == rhs); } -} +} // namespace cryptoconditions -} +} // namespace ripple #endif diff --git a/src/ripple/conditions/Fulfillment.h b/src/ripple/conditions/Fulfillment.h index c029641931..17869c0e39 100644 --- a/src/ripple/conditions/Fulfillment.h +++ b/src/ripple/conditions/Fulfillment.h @@ -51,35 +51,29 @@ public: https://tools.ietf.org/html/draft-thomas-crypto-conditions-02#section-7.3 */ - static - std::unique_ptr - deserialize( - Slice s, - std::error_code& ec); + static std::unique_ptr + deserialize(Slice s, std::error_code& ec); public: virtual ~Fulfillment() = default; /** Returns the fulfillment's fingerprint: - + The fingerprint is an octet string uniquely representing this fulfillment's condition with respect to other conditions of the same type. */ - virtual - Buffer + virtual Buffer fingerprint() const = 0; /** Returns the type of this condition. */ - virtual - Type - type () const = 0; + virtual Type + type() const = 0; /** Validates a fulfillment. */ - virtual - bool - validate (Slice data) const = 0; + virtual bool + validate(Slice data) const = 0; /** Calculates the cost associated with this fulfillment. * @@ -87,8 +81,7 @@ public: type and properties of the condition and the fulfillment that the condition is generated from. */ - virtual - std::uint32_t + virtual std::uint32_t cost() const = 0; /** Returns the condition associated with the given fulfillment. @@ -97,50 +90,40 @@ public: will, if compliant, produce the identical condition for the same fulfillment. */ - virtual - Condition + virtual Condition condition() const = 0; }; -inline -bool -operator== (Fulfillment const& lhs, Fulfillment const& rhs) +inline bool +operator==(Fulfillment const& lhs, Fulfillment const& rhs) { // FIXME: for compound conditions, need to also check subtypes - return - lhs.type() == rhs.type() && - lhs.cost() == rhs.cost() && - lhs.fingerprint() == rhs.fingerprint(); + return lhs.type() == rhs.type() && lhs.cost() == rhs.cost() && + lhs.fingerprint() == rhs.fingerprint(); } -inline -bool -operator!= (Fulfillment const& lhs, Fulfillment const& rhs) +inline bool +operator!=(Fulfillment const& lhs, Fulfillment const& rhs) { return !(lhs == rhs); } /** Determine whether the given fulfillment and condition match */ bool -match ( - Fulfillment const& f, - Condition const& c); +match(Fulfillment const& f, Condition const& c); /** Verify if the given message satisfies the fulfillment. @param f The fulfillment @param c The condition @param m The message - + @note the message is not relevant for some conditions and a fulfillment will successfully satisfy its condition for any given message. */ bool -validate ( - Fulfillment const& f, - Condition const& c, - Slice m); +validate(Fulfillment const& f, Condition const& c, Slice m); /** Verify a cryptoconditional trigger. @@ -156,11 +139,9 @@ validate ( @param c The condition */ bool -validate ( - Fulfillment const& f, - Condition const& c); +validate(Fulfillment const& f, Condition const& c); -} -} +} // namespace cryptoconditions +} // namespace ripple #endif diff --git a/src/ripple/conditions/impl/Condition.cpp b/src/ripple/conditions/impl/Condition.cpp index 9611507948..5e7e18a49d 100644 --- a/src/ripple/conditions/impl/Condition.cpp +++ b/src/ripple/conditions/impl/Condition.cpp @@ -22,10 +22,10 @@ #include #include #include -#include #include -#include +#include #include +#include namespace ripple { namespace cryptoconditions { @@ -111,7 +111,7 @@ loadSimpleSha256(Type type, Slice s, std::error_code& ec) if (!isPrimitive(p) || !isContextSpecific(p)) { ec = error::malformed_encoding; - return{}; + return {}; } if (p.tag != 1) @@ -133,22 +133,22 @@ loadSimpleSha256(Type type, Slice s, std::error_code& ec) switch (type) { - case Type::preimageSha256: - if (cost > PreimageSha256::maxPreimageLength) - { - ec = error::preimage_too_long; - return {}; - } - break; + case Type::preimageSha256: + if (cost > PreimageSha256::maxPreimageLength) + { + ec = error::preimage_too_long; + return {}; + } + break; - default: - break; + default: + break; } return std::make_unique(type, cost, std::move(b)); } -} +} // namespace detail std::unique_ptr Condition::deserialize(Slice s, std::error_code& ec) @@ -199,33 +199,32 @@ Condition::deserialize(Slice s, std::error_code& ec) switch (p.tag) { - case 0: // PreimageSha256 - c = detail::loadSimpleSha256( - Type::preimageSha256, - Slice(s.data(), p.length), ec); - if (!ec) - s += p.length; - break; + case 0: // PreimageSha256 + c = detail::loadSimpleSha256( + Type::preimageSha256, Slice(s.data(), p.length), ec); + if (!ec) + s += p.length; + break; - case 1: // PrefixSha256 - ec = error::unsupported_type; - return {}; + case 1: // PrefixSha256 + ec = error::unsupported_type; + return {}; - case 2: // ThresholdSha256 - ec = error::unsupported_type; - return {}; + case 2: // ThresholdSha256 + ec = error::unsupported_type; + return {}; - case 3: // RsaSha256 - ec = error::unsupported_type; - return {}; + case 3: // RsaSha256 + ec = error::unsupported_type; + return {}; - case 4: // Ed25519Sha256 - ec = error::unsupported_type; - return {}; + case 4: // Ed25519Sha256 + ec = error::unsupported_type; + return {}; - default: - ec = error::unknown_type; - return {}; + default: + ec = error::unknown_type; + return {}; } if (!s.empty()) @@ -237,5 +236,5 @@ Condition::deserialize(Slice s, std::error_code& ec) return c; } -} -} +} // namespace cryptoconditions +} // namespace ripple diff --git a/src/ripple/conditions/impl/Fulfillment.cpp b/src/ripple/conditions/impl/Fulfillment.cpp index 6fbd28b8d2..001a0569e3 100644 --- a/src/ripple/conditions/impl/Fulfillment.cpp +++ b/src/ripple/conditions/impl/Fulfillment.cpp @@ -22,8 +22,8 @@ #include #include #include -#include #include +#include #include #include @@ -31,9 +31,7 @@ namespace ripple { namespace cryptoconditions { bool -match ( - Fulfillment const& f, - Condition const& c) +match(Fulfillment const& f, Condition const& c) { // Fast check: the fulfillment's type must match the // conditions's type: @@ -46,26 +44,19 @@ match ( } bool -validate ( - Fulfillment const& f, - Condition const& c, - Slice m) +validate(Fulfillment const& f, Condition const& c, Slice m) { - return match (f, c) && f.validate (m); + return match(f, c) && f.validate(m); } bool -validate ( - Fulfillment const& f, - Condition const& c) +validate(Fulfillment const& f, Condition const& c) { - return validate (f, c, {}); + return validate(f, c, {}); } std::unique_ptr -Fulfillment::deserialize( - Slice s, - std::error_code& ec) +Fulfillment::deserialize(Slice s, std::error_code& ec) { // Per the RFC, in a fulfillment we choose a type based // on the tag of the item we contain: @@ -120,35 +111,35 @@ Fulfillment::deserialize( using TagType = decltype(p.tag); switch (p.tag) { - case safe_cast(Type::preimageSha256): - f = PreimageSha256::deserialize(Slice(s.data(), p.length), ec); - if (ec) + case safe_cast(Type::preimageSha256): + f = PreimageSha256::deserialize(Slice(s.data(), p.length), ec); + if (ec) + return {}; + s += p.length; + break; + + case safe_cast(Type::prefixSha256): + ec = error::unsupported_type; return {}; - s += p.length; - break; + break; - case safe_cast(Type::prefixSha256): - ec = error::unsupported_type; - return {}; - break; + case safe_cast(Type::thresholdSha256): + ec = error::unsupported_type; + return {}; + break; - case safe_cast(Type::thresholdSha256): - ec = error::unsupported_type; - return {}; - break; + case safe_cast(Type::rsaSha256): + ec = error::unsupported_type; + return {}; + break; - case safe_cast(Type::rsaSha256): - ec = error::unsupported_type; - return {}; - break; + case safe_cast(Type::ed25519Sha256): + ec = error::unsupported_type; + return {}; - case safe_cast(Type::ed25519Sha256): - ec = error::unsupported_type; - return {}; - - default: - ec = error::unknown_type; - return {}; + default: + ec = error::unknown_type; + return {}; } if (!s.empty()) @@ -160,5 +151,5 @@ Fulfillment::deserialize( return f; } -} -} +} // namespace cryptoconditions +} // namespace ripple diff --git a/src/ripple/conditions/impl/PreimageSha256.h b/src/ripple/conditions/impl/PreimageSha256.h index 704ad5a480..316e57ac17 100644 --- a/src/ripple/conditions/impl/PreimageSha256.h +++ b/src/ripple/conditions/impl/PreimageSha256.h @@ -31,8 +31,7 @@ namespace ripple { namespace cryptoconditions { -class PreimageSha256 final - : public Fulfillment +class PreimageSha256 final : public Fulfillment { public: /** The maximum allowed length of a preimage. @@ -52,11 +51,8 @@ public: @param ec indicates success or failure of the operation @return the preimage, if successful; empty pointer otherwise. */ - static - std::unique_ptr - deserialize( - Slice s, - std::error_code& ec) + static std::unique_ptr + deserialize(Slice s, std::error_code& ec) { // Per the RFC, a preimage fulfulliment is defined as // follows: @@ -106,13 +102,11 @@ private: Buffer payload_; public: - PreimageSha256(Buffer&& b) noexcept - : payload_(std::move(b)) + PreimageSha256(Buffer&& b) noexcept : payload_(std::move(b)) { } - PreimageSha256(Slice s) noexcept - : payload_(s) + PreimageSha256(Slice s) noexcept : payload_(s) { } @@ -128,7 +122,7 @@ public: sha256_hasher h; h(payload_.data(), payload_.size()); auto const d = static_cast(h); - return{ d.data(), d.size() }; + return {d.data(), d.size()}; } std::uint32_t @@ -140,11 +134,10 @@ public: Condition condition() const override { - return { type(), cost(), fingerprint() }; + return {type(), cost(), fingerprint()}; } - bool - validate(Slice) const override + bool validate(Slice) const override { // Perhaps counterintuitively, the message isn't // relevant. @@ -152,7 +145,7 @@ public: } }; -} -} +} // namespace cryptoconditions +} // namespace ripple #endif diff --git a/src/ripple/conditions/impl/error.cpp b/src/ripple/conditions/impl/error.cpp index 1ecc14df9f..f685a45899 100644 --- a/src/ripple/conditions/impl/error.cpp +++ b/src/ripple/conditions/impl/error.cpp @@ -19,16 +19,15 @@ #include #include -#include #include +#include #include namespace ripple { namespace cryptoconditions { namespace detail { -class cryptoconditions_error_category - : public std::error_category +class cryptoconditions_error_category : public std::error_category { public: explicit cryptoconditions_error_category() = default; @@ -44,103 +43,96 @@ public: { switch (safe_cast(ev)) { - case error::unsupported_type: - return "Specification: Requested type not supported."; + case error::unsupported_type: + return "Specification: Requested type not supported."; - case error::unsupported_subtype: - return "Specification: Requested subtype not supported."; + case error::unsupported_subtype: + return "Specification: Requested subtype not supported."; - case error::unknown_type: - return "Specification: Requested type not recognized."; + case error::unknown_type: + return "Specification: Requested type not recognized."; - case error::unknown_subtype: - return "Specification: Requested subtypes not recognized."; + case error::unknown_subtype: + return "Specification: Requested subtypes not recognized."; - case error::fingerprint_size: - return "Specification: Incorrect fingerprint size."; + case error::fingerprint_size: + return "Specification: Incorrect fingerprint size."; - case error::incorrect_encoding: - return "Specification: Incorrect encoding."; + case error::incorrect_encoding: + return "Specification: Incorrect encoding."; - case error::trailing_garbage: - return "Bad buffer: contains trailing garbage."; + case error::trailing_garbage: + return "Bad buffer: contains trailing garbage."; - case error::buffer_empty: - return "Bad buffer: no data."; + case error::buffer_empty: + return "Bad buffer: no data."; - case error::buffer_overfull: - return "Bad buffer: overfull."; + case error::buffer_overfull: + return "Bad buffer: overfull."; - case error::buffer_underfull: - return "Bad buffer: underfull."; + case error::buffer_underfull: + return "Bad buffer: underfull."; - case error::malformed_encoding: - return "Malformed DER encoding."; + case error::malformed_encoding: + return "Malformed DER encoding."; - case error::unexpected_tag: - return "Malformed DER encoding: Unexpected tag."; + case error::unexpected_tag: + return "Malformed DER encoding: Unexpected tag."; - case error::short_preamble: - return "Malformed DER encoding: Short preamble."; + case error::short_preamble: + return "Malformed DER encoding: Short preamble."; - case error::long_tag: - return "Implementation limit: Overlong tag."; + case error::long_tag: + return "Implementation limit: Overlong tag."; - case error::large_size: - return "Implementation limit: Large payload."; + case error::large_size: + return "Implementation limit: Large payload."; - case error::preimage_too_long: - return "Implementation limit: Specified preimage is too long."; + case error::preimage_too_long: + return "Implementation limit: Specified preimage is too long."; - case error::generic: - default: - return "generic error"; + case error::generic: + default: + return "generic error"; } } std::error_condition default_error_condition(int ev) const noexcept override { - return std::error_condition{ ev, *this }; + return std::error_condition{ev, *this}; } bool - equivalent( - int ev, - std::error_condition const& condition) const noexcept override + equivalent(int ev, std::error_condition const& condition) + const noexcept override { - return &condition.category() == this && - condition.value() == ev; + return &condition.category() == this && condition.value() == ev; } bool - equivalent( - std::error_code const& error, - int ev) const noexcept override + equivalent(std::error_code const& error, int ev) const noexcept override { - return &error.category() == this && - error.value() == ev; + return &error.category() == this && error.value() == ev; } }; -inline -std::error_category const& +inline std::error_category const& get_cryptoconditions_error_category() { static cryptoconditions_error_category const cat{}; return cat; } -} // detail +} // namespace detail std::error_code make_error_code(error ev) { - return std::error_code { + return std::error_code{ safe_cast::type>(ev), - detail::get_cryptoconditions_error_category() - }; + detail::get_cryptoconditions_error_category()}; } -} -} +} // namespace cryptoconditions +} // namespace ripple diff --git a/src/ripple/conditions/impl/error.h b/src/ripple/conditions/impl/error.h index de0a0fec29..8e8f8cc842 100644 --- a/src/ripple/conditions/impl/error.h +++ b/src/ripple/conditions/impl/error.h @@ -20,14 +20,13 @@ #ifndef RIPPLE_CONDITIONS_ERROR_H #define RIPPLE_CONDITIONS_ERROR_H -#include #include +#include namespace ripple { namespace cryptoconditions { -enum class error -{ +enum class error { generic = 1, unsupported_type, unsupported_subtype, @@ -50,13 +49,12 @@ enum class error std::error_code make_error_code(error ev); -} // cryptoconditions -} // ripple +} // namespace cryptoconditions +} // namespace ripple -namespace std -{ +namespace std { -template<> +template <> struct is_error_code_enum { explicit is_error_code_enum() = default; @@ -64,6 +62,6 @@ struct is_error_code_enum static bool const value = true; }; -} // std +} // namespace std #endif diff --git a/src/ripple/conditions/impl/utils.h b/src/ripple/conditions/impl/utils.h index 030667aa1e..75a23725e0 100644 --- a/src/ripple/conditions/impl/utils.h +++ b/src/ripple/conditions/impl/utils.h @@ -23,13 +23,13 @@ #include #include #include +#include #include +#include #include #include -#include -#include -#include #include +#include namespace ripple { namespace cryptoconditions { @@ -51,50 +51,43 @@ struct Preamble std::size_t length = 0; }; -inline -bool +inline bool isPrimitive(Preamble const& p) { return (p.type & 0x20) == 0; } -inline -bool +inline bool isConstructed(Preamble const& p) { return !isPrimitive(p); } -inline -bool +inline bool isUniversal(Preamble const& p) { return (p.type & 0xC0) == 0; } -inline -bool +inline bool isApplication(Preamble const& p) { return (p.type & 0xC0) == 0x40; } -inline -bool +inline bool isContextSpecific(Preamble const& p) { return (p.type & 0xC0) == 0x80; } -inline -bool +inline bool isPrivate(Preamble const& p) { return (p.type & 0xC0) == 0xC0; } -inline -Preamble +inline Preamble parsePreamble(Slice& s, std::error_code& ec) { Preamble p; @@ -111,7 +104,7 @@ parsePreamble(Slice& s, std::error_code& ec) s += 1; if (p.tag == 0x1F) - { // Long tag form, which we do not support: + { // Long tag form, which we do not support: ec = error::long_tag; return p; } @@ -120,7 +113,7 @@ parsePreamble(Slice& s, std::error_code& ec) s += 1; if (p.length & 0x80) - { // Long form length: + { // Long form length: std::size_t const cnt = p.length & 0x7F; if (cnt == 0) @@ -158,8 +151,7 @@ parsePreamble(Slice& s, std::error_code& ec) return p; } -inline -Buffer +inline Buffer parseOctetString(Slice& s, std::uint32_t count, std::error_code& ec) { if (count > s.size()) @@ -235,8 +227,8 @@ parseInteger(Slice& s, std::size_t count, std::error_code& ec) return v; } -} // der -} // cryptoconditions -} // ripple +} // namespace der +} // namespace cryptoconditions +} // namespace ripple #endif diff --git a/src/ripple/consensus/Consensus.h b/src/ripple/consensus/Consensus.h index c6c03fea80..5f88033ecf 100644 --- a/src/ripple/consensus/Consensus.h +++ b/src/ripple/consensus/Consensus.h @@ -23,18 +23,17 @@ #include #include #include -#include #include +#include #include -#include #include +#include #include #include #include namespace ripple { - /** Determines whether the current ledger should close at this time. This function should be called when a ledger is open and there is no close @@ -46,8 +45,8 @@ namespace ripple { @param proposersValidated proposers who have validated the last closed ledger @param prevRoundTime time for the previous ledger to reach consensus - @param timeSincePrevClose time since the previous ledger's (possibly rounded) - close time + @param timeSincePrevClose time since the previous ledger's (possibly + rounded) close time @param openTime duration this ledger has been open @param idleInterval the network's desired idle interval @param parms Consensus constant parameters @@ -63,7 +62,7 @@ shouldCloseLedger( std::chrono::milliseconds timeSincePrevClose, std::chrono::milliseconds openTime, std::chrono::milliseconds idleInterval, - ConsensusParms const & parms, + ConsensusParms const& parms, beast::Journal j); /** Determine whether the network reached consensus and whether we joined. @@ -88,7 +87,7 @@ checkConsensus( std::size_t currentFinished, std::chrono::milliseconds previousAgreeTime, std::chrono::milliseconds currentAgreeTime, - ConsensusParms const & parms, + ConsensusParms const& parms, bool proposing, beast::Journal j); @@ -318,6 +317,7 @@ class Consensus mode_ = mode; } }; + public: //! Clock type for measuring time within the consensus code using clock_type = beast::abstract_clock; @@ -350,7 +350,7 @@ public: NetClock::time_point const& now, typename Ledger_t::ID const& prevLedgerID, Ledger_t prevLedger, - hash_set const & nowUntrusted, + hash_set const& nowUntrusted, bool proposing); /** A peer has proposed a new position, adjust our tracking. @@ -456,7 +456,7 @@ private: playbackProposals(); /** Handle a replayed or a new peer proposal. - */ + */ bool peerProposalInternal( NetClock::time_point const& now, @@ -605,9 +605,7 @@ Consensus::Consensus( clock_type const& clock, Adaptor& adaptor, beast::Journal journal) - : adaptor_(adaptor) - , clock_(clock) - , j_{journal} + : adaptor_(adaptor), clock_(clock), j_{journal} { JLOG(j_.debug()) << "Creating consensus object"; } @@ -633,7 +631,7 @@ Consensus::startRound( prevCloseTime_ = rawCloseTimes_.self; } - for(NodeID_t const& n : nowUntrusted) + for (NodeID_t const& n : nowUntrusted) recentPeerPositions_.erase(n); ConsensusMode startMode = @@ -921,7 +919,8 @@ Consensus::getJson(bool full) const if (mode_.get() != ConsensusMode::wrongLedger) { ret["synched"] = true; - ret["ledger_seq"] = static_cast(previousLedger_.seq())+ 1; + ret["ledger_seq"] = + static_cast(previousLedger_.seq()) + 1; ret["close_granularity"] = static_cast(closeResolution_.count()); } else @@ -1146,12 +1145,13 @@ bool Consensus::shouldPause() const { auto const& parms = adaptor_.parms(); - std::uint32_t const ahead (previousLedger_.seq() - + std::uint32_t const ahead( + previousLedger_.seq() - std::min(adaptor_.getValidLedgerIndex(), previousLedger_.seq())); auto [quorum, trustedKeys] = adaptor_.getQuorumKeys(); std::size_t const totalValidators = trustedKeys.size(); - std::size_t laggards = adaptor_.laggards(previousLedger_.seq(), - trustedKeys); + std::size_t laggards = + adaptor_.laggards(previousLedger_.seq(), trustedKeys); std::size_t const offline = trustedKeys.size(); std::stringstream vars; @@ -1166,10 +1166,7 @@ Consensus::shouldPause() const << "offline: " << offline << ", " << "quorum: " << quorum << ")"; - if (!ahead || - !laggards || - !totalValidators || - !adaptor_.validator() || + if (!ahead || !laggards || !totalValidators || !adaptor_.validator() || !adaptor_.haveValidated() || result_->roundTime.read() > parms.ledgerMAX_CONSENSUS) { @@ -1265,7 +1262,7 @@ Consensus::phaseEstablish() assert(result_); using namespace std::chrono; - ConsensusParms const & parms = adaptor_.parms(); + ConsensusParms const& parms = adaptor_.parms(); result_->roundTime.tick(clock_.now()); result_->proposers = currPeerPositions_.size(); @@ -1362,7 +1359,7 @@ Consensus::updateOurPositions() { // We must have a position if we are updating it assert(result_); - ConsensusParms const & parms = adaptor_.parms(); + ConsensusParms const& parms = adaptor_.parms(); // Compute a cutoff time auto const peerCutoff = now_ - parms.proposeFRESHNESS; @@ -1405,7 +1402,7 @@ Consensus::updateOurPositions() // time can change our position on a dispute if (dispute.updateVote( convergePercent_, - mode_.get()== ConsensusMode::proposing, + mode_.get() == ConsensusMode::proposing, parms)) { if (!mutableSet) @@ -1473,8 +1470,8 @@ Consensus::updateOurPositions() JLOG(j_.debug()) << "CCTime: seq " << static_cast(previousLedger_.seq()) + 1 << ": " - << t.time_since_epoch().count() << " has " << v - << ", " << threshVote << " required"; + << t.time_since_epoch().count() << " has " << v << ", " + << threshVote << " required"; if (v >= threshVote) { @@ -1656,8 +1653,11 @@ Consensus::createDisputes(TxSet_t const& o) JLOG(j_.debug()) << "Transaction " << txID << " is disputed"; - typename Result::Dispute_t dtx{tx, result_->txns.exists(txID), - std::max(prevProposers_, currPeerPositions_.size()), j_}; + typename Result::Dispute_t dtx{ + tx, + result_->txns.exists(txID), + std::max(prevProposers_, currPeerPositions_.size()), + j_}; // Update all of the available peer's votes on the disputed transaction for (auto const& [nodeId, peerPos] : currPeerPositions_) diff --git a/src/ripple/consensus/ConsensusParms.h b/src/ripple/consensus/ConsensusParms.h index 94e8d3ff45..542b3644b4 100644 --- a/src/ripple/consensus/ConsensusParms.h +++ b/src/ripple/consensus/ConsensusParms.h @@ -43,7 +43,7 @@ struct ConsensusParms This is a safety to protect against very old validations and the time it takes to adjust the close time accuracy window. */ - std::chrono::seconds validationVALID_WALL = std::chrono::minutes {5}; + std::chrono::seconds validationVALID_WALL = std::chrono::minutes{5}; /** Duration a validation remains current after first observed. @@ -51,22 +51,20 @@ struct ConsensusParms first saw it. This provides faster recovery in very rare cases where the number of validations produced by the network is lower than normal */ - std::chrono::seconds validationVALID_LOCAL = std::chrono::minutes {3}; + std::chrono::seconds validationVALID_LOCAL = std::chrono::minutes{3}; /** Duration pre-close in which validations are acceptable. The number of seconds before a close time that we consider a validation acceptable. This protects against extreme clock errors */ - std::chrono::seconds validationVALID_EARLY = std::chrono::minutes {3}; - + std::chrono::seconds validationVALID_EARLY = std::chrono::minutes{3}; //! How long we consider a proposal fresh - std::chrono::seconds proposeFRESHNESS = std::chrono::seconds {20}; + std::chrono::seconds proposeFRESHNESS = std::chrono::seconds{20}; //! How often we force generating a new proposal to keep ours fresh - std::chrono::seconds proposeINTERVAL = std::chrono::seconds {12}; - + std::chrono::seconds proposeINTERVAL = std::chrono::seconds{12}; //------------------------------------------------------------------------- // Consensus durations are relative to the internal Consensus clock and use @@ -76,11 +74,11 @@ struct ConsensusParms std::size_t minCONSENSUS_PCT = 80; //! The duration a ledger may remain idle before closing - std::chrono::milliseconds ledgerIDLE_INTERVAL = std::chrono::seconds {15}; + std::chrono::milliseconds ledgerIDLE_INTERVAL = std::chrono::seconds{15}; //! The number of seconds we wait minimum to ensure participation std::chrono::milliseconds ledgerMIN_CONSENSUS = - std::chrono::milliseconds {1950}; + std::chrono::milliseconds{1950}; /** The maximum amount of time to spend pausing for laggards. * @@ -88,14 +86,13 @@ struct ConsensusParms * validators don't appear to be offline that are merely waiting for * laggards. */ - std::chrono::milliseconds ledgerMAX_CONSENSUS = - std::chrono::seconds {10}; + std::chrono::milliseconds ledgerMAX_CONSENSUS = std::chrono::seconds{10}; //! Minimum number of seconds to wait to ensure others have computed the LCL - std::chrono::milliseconds ledgerMIN_CLOSE = std::chrono::seconds {2}; + std::chrono::milliseconds ledgerMIN_CLOSE = std::chrono::seconds{2}; //! How often we check state or change positions - std::chrono::milliseconds ledgerGRANULARITY = std::chrono::seconds {1}; + std::chrono::milliseconds ledgerGRANULARITY = std::chrono::seconds{1}; /** The minimum amount of time to consider the previous round to have taken. @@ -107,7 +104,7 @@ struct ConsensusParms twice the interval between proposals (0.7s) divided by the interval between mid and late consensus ([85-50]/100). */ - std::chrono::milliseconds avMIN_CONSENSUS_TIME = std::chrono::seconds {5}; + std::chrono::milliseconds avMIN_CONSENSUS_TIME = std::chrono::seconds{5}; //------------------------------------------------------------------------------ // Avalanche tuning @@ -140,5 +137,5 @@ struct ConsensusParms std::size_t avCT_CONSENSUS_PCT = 75; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/consensus/ConsensusProposal.h b/src/ripple/consensus/ConsensusProposal.h index d8baff51fe..a3eccbb016 100644 --- a/src/ripple/consensus/ConsensusProposal.h +++ b/src/ripple/consensus/ConsensusProposal.h @@ -240,5 +240,5 @@ operator==( a.prevLedger() == b.prevLedger() && a.position() == b.position() && a.closeTime() == b.closeTime() && a.seenTime() == b.seenTime(); } -} +} // namespace ripple #endif diff --git a/src/ripple/consensus/DisputedTx.h b/src/ripple/consensus/DisputedTx.h index 971f2d1327..ae127197ee 100644 --- a/src/ripple/consensus/DisputedTx.h +++ b/src/ripple/consensus/DisputedTx.h @@ -20,7 +20,6 @@ #ifndef RIPPLE_APP_CONSENSUS_IMPL_DISPUTEDTX_H_INCLUDED #define RIPPLE_APP_CONSENSUS_IMPL_DISPUTEDTX_H_INCLUDED -#include #include #include #include @@ -28,6 +27,7 @@ #include #include #include +#include #include namespace ripple { @@ -51,6 +51,7 @@ class DisputedTx { using TxID_t = typename Tx_t::ID; using Map_t = boost::container::flat_map; + public: /** Constructor @@ -59,7 +60,11 @@ public: @param numPeers Anticipated number of peer votes @param j Journal for debugging */ - DisputedTx(Tx_t const& tx, bool ourVote, std::size_t numPeers, beast::Journal j) + DisputedTx( + Tx_t const& tx, + bool ourVote, + std::size_t numPeers, + beast::Journal j) : yays_(0), nays_(0), ourVote_(ourVote), tx_(tx), j_(j) { votes_.reserve(numPeers); @@ -270,6 +275,6 @@ DisputedTx::getJson() const return ret; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/consensus/LedgerTiming.h b/src/ripple/consensus/LedgerTiming.h index 71c53b996b..c2ab08df10 100644 --- a/src/ripple/consensus/LedgerTiming.h +++ b/src/ripple/consensus/LedgerTiming.h @@ -32,15 +32,13 @@ namespace ripple { Values should not be duplicated. @see getNextLedgerTimeResolution */ -std::chrono::seconds constexpr ledgerPossibleTimeResolutions[] = - { - std::chrono::seconds { 10}, - std::chrono::seconds { 20}, - std::chrono::seconds { 30}, - std::chrono::seconds { 60}, - std::chrono::seconds { 90}, - std::chrono::seconds {120} - }; +std::chrono::seconds constexpr ledgerPossibleTimeResolutions[] = { + std::chrono::seconds{10}, + std::chrono::seconds{20}, + std::chrono::seconds{30}, + std::chrono::seconds{60}, + std::chrono::seconds{90}, + std::chrono::seconds{120}}; //! Initial resolution of ledger close time. auto constexpr ledgerDefaultTimeResolution = ledgerPossibleTimeResolutions[2]; @@ -163,5 +161,5 @@ effCloseTime( roundCloseTime(closeTime, resolution), (priorCloseTime + 1s)); } -} +} // namespace ripple #endif diff --git a/src/ripple/consensus/LedgerTrie.h b/src/ripple/consensus/LedgerTrie.h index 548c7d51ed..641bcfdf67 100644 --- a/src/ripple/consensus/LedgerTrie.h +++ b/src/ripple/consensus/LedgerTrie.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_APP_CONSENSUS_LEDGERS_TRIE_H_INCLUDED #define RIPPLE_APP_CONSENSUS_LEDGERS_TRIE_H_INCLUDED -#include -#include #include +#include +#include #include #include #include @@ -568,8 +568,7 @@ public: else if (loc->children.size() == 1) { // This node can be combined with its child - std::unique_ptr child = - std::move(loc->children.front()); + std::unique_ptr child = std::move(loc->children.front()); child->span = merge(loc->span, child->span); child->parent = parent; parent->children.emplace_back(std::move(child)); @@ -610,7 +609,7 @@ public: Seq diffSeq; std::tie(loc, diffSeq) = find(ledger); // Check that ledger is a proper prefix of loc - if (! (diffSeq > ledger.seq() && ledger.seq() < loc->span.end())) + if (!(diffSeq > ledger.seq() && ledger.seq() < loc->span.end())) loc = nullptr; } return loc ? loc->branchSupport : 0; diff --git a/src/ripple/consensus/Validations.h b/src/ripple/consensus/Validations.h index d51e7fa622..17a9bb4c94 100644 --- a/src/ripple/consensus/Validations.h +++ b/src/ripple/consensus/Validations.h @@ -83,7 +83,7 @@ struct ValidationParms * ledgerMAX_CONSENSUS such that validators who are waiting for * laggards are not considered offline. */ - std::chrono::seconds validationFRESHNESS = std::chrono::seconds {20}; + std::chrono::seconds validationFRESHNESS = std::chrono::seconds{20}; }; /** Enforce validation increasing sequence requirement. @@ -326,7 +326,10 @@ class Validations private: // Remove support of a validated ledger void - removeTrie(std::lock_guard const&, NodeID const& nodeID, Validation const& val) + removeTrie( + std::lock_guard const&, + NodeID const& nodeID, + Validation const& val) { { auto it = @@ -369,7 +372,10 @@ private: // Update the trie to reflect a new validated ledger void - updateTrie(std::lock_guard const&, NodeID const& nodeID, Ledger ledger) + updateTrie( + std::lock_guard const&, + NodeID const& nodeID, + Ledger ledger) { auto const [it, inserted] = lastLedger_.emplace(nodeID, ledger); if (!inserted) @@ -449,7 +455,8 @@ private: withTrie(std::lock_guard const& lock, F&& f) { // Call current to flush any stale validations - current(lock, [](auto) {}, [](auto, auto) {}); + current( + lock, [](auto) {}, [](auto, auto) {}); checkAcquired(lock); return f(trie_); } @@ -510,7 +517,11 @@ private: */ template void - byLedger(std::lock_guard const&, ID const& ledgerID, Pre&& pre, F&& f) + byLedger( + std::lock_guard const&, + ID const& ledgerID, + Pre&& pre, + F&& f) { auto it = byLedger_.find(ledgerID); if (it != byLedger_.end()) @@ -976,7 +987,8 @@ public: { std::size_t laggards = 0; - current(std::lock_guard{mutex_}, + current( + std::lock_guard{mutex_}, [](std::size_t) {}, [&](NodeID const&, Validation const& v) { if (adaptor_.now() < diff --git a/src/ripple/core/ClosureCounter.h b/src/ripple/core/ClosureCounter.h index 48160e179b..d71c5bcb54 100644 --- a/src/ripple/core/ClosureCounter.h +++ b/src/ripple/core/ClosureCounter.h @@ -40,13 +40,14 @@ template class ClosureCounter { private: - std::mutex mutable mutex_ {}; - std::condition_variable allClosuresDoneCond_ {}; // guard with mutex_ - bool waitForClosures_ {false}; // guard with mutex_ - std::atomic closureCount_ {0}; + std::mutex mutable mutex_{}; + std::condition_variable allClosuresDoneCond_{}; // guard with mutex_ + bool waitForClosures_{false}; // guard with mutex_ + std::atomic closureCount_{0}; // Increment the count. - ClosureCounter& operator++() + ClosureCounter& + operator++() { ++closureCount_; return *this; @@ -54,13 +55,14 @@ private: // Decrement the count. If we're stopping and the count drops to zero // notify allClosuresDoneCond_. - ClosureCounter& operator--() + ClosureCounter& + operator--() { // Even though closureCount_ is atomic, we decrement its value under // a lock. This removes a small timing window that occurs if the // waiting thread is handling a spurious wakeup when closureCount_ // drops to zero. - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; // Update closureCount_. Notify if stopping and closureCount_ == 0. if ((--closureCount_ == 0) && waitForClosures_) @@ -78,38 +80,37 @@ private: ClosureCounter& counter_; std::remove_reference_t closure_; - static_assert ( - std::is_same()...)), Ret_t>::value, - "Closure arguments don't match ClosureCounter Ret_t or Args_t"); + static_assert( + std::is_same()...)), Ret_t>:: + value, + "Closure arguments don't match ClosureCounter Ret_t or Args_t"); public: Wrapper() = delete; - Wrapper (Wrapper const& rhs) - : counter_ (rhs.counter_) - , closure_ (rhs.closure_) + Wrapper(Wrapper const& rhs) + : counter_(rhs.counter_), closure_(rhs.closure_) { ++counter_; } - Wrapper (Wrapper&& rhs) noexcept( - std::is_nothrow_move_constructible::value) - : counter_ (rhs.counter_) - , closure_ (std::move (rhs.closure_)) + Wrapper(Wrapper&& rhs) noexcept( + std::is_nothrow_move_constructible::value) + : counter_(rhs.counter_), closure_(std::move(rhs.closure_)) { ++counter_; } - Wrapper (ClosureCounter& counter, Closure&& closure) - : counter_ (counter) - , closure_ (std::forward (closure)) + Wrapper(ClosureCounter& counter, Closure&& closure) + : counter_(counter), closure_(std::forward(closure)) { ++counter_; } - Wrapper& operator=(Wrapper const& rhs) = delete; - Wrapper& operator=(Wrapper&& rhs) = delete; + Wrapper& + operator=(Wrapper const& rhs) = delete; + Wrapper& + operator=(Wrapper&& rhs) = delete; ~Wrapper() { @@ -119,24 +120,26 @@ private: // Note that Args_t is not deduced, it is explicit. So Args_t&& // would be an rvalue reference, not a forwarding reference. We // want to forward exactly what the user declared. - Ret_t operator ()(Args_t... args) + Ret_t + operator()(Args_t... args) { - return closure_ (std::forward(args)...); + return closure_(std::forward(args)...); } }; public: ClosureCounter() = default; // Not copyable or movable. Outstanding counts would be hard to sort out. - ClosureCounter (ClosureCounter const&) = delete; + ClosureCounter(ClosureCounter const&) = delete; - ClosureCounter& operator=(ClosureCounter const&) = delete; + ClosureCounter& + operator=(ClosureCounter const&) = delete; /** Destructor verifies all in-flight closures are complete. */ ~ClosureCounter() { using namespace std::chrono_literals; - join ("ClosureCounter", 1s, debugLog()); + join("ClosureCounter", 1s, debugLog()); } /** Returns once all counted in-flight closures are destroyed. @@ -145,20 +148,19 @@ public: @param wait If join() exceeds this duration report to Journal. @param j Journal written to if wait is exceeded. */ - void join (char const* name, - std::chrono::milliseconds wait, beast::Journal j) + void + join(char const* name, std::chrono::milliseconds wait, beast::Journal j) { - std::unique_lock lock {mutex_}; + std::unique_lock lock{mutex_}; waitForClosures_ = true; if (closureCount_ > 0) { - if (! allClosuresDoneCond_.wait_for ( - lock, wait, [this] { return closureCount_ == 0; })) + if (!allClosuresDoneCond_.wait_for( + lock, wait, [this] { return closureCount_ == 0; })) { if (auto stream = j.error()) - stream << name - << " waiting for ClosureCounter::join()."; - allClosuresDoneCond_.wait ( + stream << name << " waiting for ClosureCounter::join()."; + allClosuresDoneCond_.wait( lock, [this] { return closureCount_ == 0; }); } } @@ -173,19 +175,20 @@ public: */ template boost::optional> - wrap (Closure&& closure) + wrap(Closure&& closure) { boost::optional> ret; - std::lock_guard lock {mutex_}; - if (! waitForClosures_) - ret.emplace (*this, std::forward (closure)); + std::lock_guard lock{mutex_}; + if (!waitForClosures_) + ret.emplace(*this, std::forward(closure)); return ret; } /** Current number of Closures outstanding. Only useful for testing. */ - int count() const + int + count() const { return closureCount_; } @@ -196,13 +199,14 @@ public: However if (joined() && (count() == 0)) there should be no more counted closures in flight. */ - bool joined() const + bool + joined() const { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; return waitForClosures_; } }; -} // ripple +} // namespace ripple -#endif // RIPPLE_CORE_CLOSURE_COUNTER_H_INCLUDED +#endif // RIPPLE_CORE_CLOSURE_COUNTER_H_INCLUDED diff --git a/src/ripple/core/Config.h b/src/ripple/core/Config.h index 5231f16946..0b7525efac 100644 --- a/src/ripple/core/Config.h +++ b/src/ripple/core/Config.h @@ -21,13 +21,13 @@ #define RIPPLE_CORE_CONFIG_H_INCLUDED #include -#include #include -#include // VFALCO Breaks levelization +#include #include -#include #include -#include // VFALCO FIX: This include should not be here +#include // VFALCO Breaks levelization +#include +#include // VFALCO FIX: This include should not be here #include #include #include @@ -44,8 +44,7 @@ class Rules; //------------------------------------------------------------------------------ -enum class SizedItem : std::size_t -{ +enum class SizedItem : std::size_t { sweepInterval = 0, treeCacheSize, treeCacheAge, @@ -73,20 +72,24 @@ public: static char const* const validatorsFileName; /** Returns the full path and filename of the debug log file. */ - boost::filesystem::path getDebugLogFile () const; + boost::filesystem::path + getDebugLogFile() const; private: boost::filesystem::path CONFIG_FILE; + public: boost::filesystem::path CONFIG_DIR; + private: boost::filesystem::path DEBUG_LOGFILE; - void load (); + void + load(); beast::Journal const j_; - bool QUIET = false; // Minimize logging verbosity. - bool SILENT = false; // No output to console after startup. + bool QUIET = false; // Minimize logging verbosity. + bool SILENT = false; // No output to console after startup. /** Operate in stand-alone mode. In stand alone mode: @@ -96,7 +99,7 @@ private: - If no ledger is loaded, the default ledger with the root account is created. */ - bool RUN_STANDALONE = false; + bool RUN_STANDALONE = false; /** Determines if the server will sign a tx, given an account's secret seed. @@ -112,70 +115,63 @@ public: bool validateShards = false; bool ELB_SUPPORT = false; - std::vector IPS; // Peer IPs from rippled.cfg. - std::vector IPS_FIXED; // Fixed Peer IPs from rippled.cfg. - std::vector SNTP_SERVERS; // SNTP servers from rippled.cfg. + std::vector IPS; // Peer IPs from rippled.cfg. + std::vector IPS_FIXED; // Fixed Peer IPs from rippled.cfg. + std::vector SNTP_SERVERS; // SNTP servers from rippled.cfg. - enum StartUpType - { - FRESH, - NORMAL, - LOAD, - LOAD_FILE, - REPLAY, - NETWORK - }; - StartUpType START_UP = NORMAL; + enum StartUpType { FRESH, NORMAL, LOAD, LOAD_FILE, REPLAY, NETWORK }; + StartUpType START_UP = NORMAL; - bool START_VALID = false; + bool START_VALID = false; - std::string START_LEDGER; + std::string START_LEDGER; // Network parameters // The number of fee units a reference transaction costs - static constexpr FeeUnit32 TRANSACTION_FEE_BASE{ 10 }; + static constexpr FeeUnit32 TRANSACTION_FEE_BASE{10}; // Note: The following parameters do not relate to the UNL or trust at all // Minimum number of nodes to consider the network present - std::size_t NETWORK_QUORUM = 1; + std::size_t NETWORK_QUORUM = 1; // Peer networking parameters // True to ask peers not to relay current IP. - bool PEER_PRIVATE = false; - std::size_t PEERS_MAX = 0; + bool PEER_PRIVATE = false; + std::size_t PEERS_MAX = 0; - std::chrono::seconds WEBSOCKET_PING_FREQ = std::chrono::minutes {5}; + std::chrono::seconds WEBSOCKET_PING_FREQ = std::chrono::minutes{5}; // Path searching - int PATH_SEARCH_OLD = 7; - int PATH_SEARCH = 7; - int PATH_SEARCH_FAST = 2; - int PATH_SEARCH_MAX = 10; + int PATH_SEARCH_OLD = 7; + int PATH_SEARCH = 7; + int PATH_SEARCH_FAST = 2; + int PATH_SEARCH_MAX = 10; // Validation - boost::optional VALIDATION_QUORUM; // validations to consider ledger authoritative + boost::optional + VALIDATION_QUORUM; // validations to consider ledger authoritative - XRPAmount FEE_DEFAULT{10}; - XRPAmount FEE_ACCOUNT_RESERVE{200 * DROPS_PER_XRP}; - XRPAmount FEE_OWNER_RESERVE{50 * DROPS_PER_XRP}; + XRPAmount FEE_DEFAULT{10}; + XRPAmount FEE_ACCOUNT_RESERVE{200 * DROPS_PER_XRP}; + XRPAmount FEE_OWNER_RESERVE{50 * DROPS_PER_XRP}; // Node storage configuration - std::uint32_t LEDGER_HISTORY = 256; - std::uint32_t FETCH_DEPTH = 1000000000; + std::uint32_t LEDGER_HISTORY = 256; + std::uint32_t FETCH_DEPTH = 1000000000; - std::size_t NODE_SIZE = 0; + std::size_t NODE_SIZE = 0; - bool SSL_VERIFY = true; - std::string SSL_VERIFY_FILE; - std::string SSL_VERIFY_DIR; + bool SSL_VERIFY = true; + std::string SSL_VERIFY_FILE; + std::string SSL_VERIFY_DIR; // Compression - bool COMPRESSION = false; + bool COMPRESSION = false; // Thread pool configuration - std::size_t WORKERS = 0; + std::size_t WORKERS = 0; // These override the command line client settings boost::optional rpc_ip; @@ -183,27 +179,50 @@ public: std::unordered_set> features; public: - Config() : j_ {beast::Journal::getNullSink()} {} + Config() : j_{beast::Journal::getNullSink()} + { + } /* Be very careful to make sure these bool params are in the right order. */ - void setup (std::string const& strConf, bool bQuiet, - bool bSilent, bool bStandalone); - void setupControl (bool bQuiet, - bool bSilent, bool bStandalone); + void + setup( + std::string const& strConf, + bool bQuiet, + bool bSilent, + bool bStandalone); + void + setupControl(bool bQuiet, bool bSilent, bool bStandalone); /** * Load the config from the contents of the string. * * @param fileContents String representing the config contents. */ - void loadFromString (std::string const& fileContents); + void + loadFromString(std::string const& fileContents); - bool quiet() const { return QUIET; } - bool silent() const { return SILENT; } - bool standalone() const { return RUN_STANDALONE; } + bool + quiet() const + { + return QUIET; + } + bool + silent() const + { + return SILENT; + } + bool + standalone() const + { + return RUN_STANDALONE; + } - bool canSign() const { return signingEnabled_; } + bool + canSign() const + { + return signingEnabled_; + } /** Retrieve the default value for the item at the specified node size @@ -223,10 +242,10 @@ public: defaults in the code for every case. */ int - getValueFor(SizedItem item, - boost::optional node = boost::none) const; + getValueFor(SizedItem item, boost::optional node = boost::none) + const; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/ConfigSections.h b/src/ripple/core/ConfigSections.h index 653b9c404e..cec6619f3b 100644 --- a/src/ripple/core/ConfigSections.h +++ b/src/ripple/core/ConfigSections.h @@ -29,52 +29,64 @@ struct ConfigSection { explicit ConfigSection() = default; - static std::string nodeDatabase () { return "node_db"; } - static std::string shardDatabase () { return "shard_db"; } - static std::string importNodeDatabase () { return "import_db"; } + static std::string + nodeDatabase() + { + return "node_db"; + } + static std::string + shardDatabase() + { + return "shard_db"; + } + static std::string + importNodeDatabase() + { + return "import_db"; + } }; // VFALCO TODO Rename and replace these macros with variables. -#define SECTION_AMENDMENTS "amendments" -#define SECTION_CLUSTER_NODES "cluster_nodes" -#define SECTION_COMPRESSION "compression" -#define SECTION_DEBUG_LOGFILE "debug_logfile" -#define SECTION_ELB_SUPPORT "elb_support" -#define SECTION_FEE_DEFAULT "fee_default" -#define SECTION_FEE_ACCOUNT_RESERVE "fee_account_reserve" -#define SECTION_FEE_OWNER_RESERVE "fee_owner_reserve" -#define SECTION_FETCH_DEPTH "fetch_depth" -#define SECTION_LEDGER_HISTORY "ledger_history" -#define SECTION_INSIGHT "insight" -#define SECTION_IPS "ips" -#define SECTION_IPS_FIXED "ips_fixed" -#define SECTION_NETWORK_QUORUM "network_quorum" -#define SECTION_NODE_SEED "node_seed" -#define SECTION_NODE_SIZE "node_size" -#define SECTION_PATH_SEARCH_OLD "path_search_old" -#define SECTION_PATH_SEARCH "path_search" -#define SECTION_PATH_SEARCH_FAST "path_search_fast" -#define SECTION_PATH_SEARCH_MAX "path_search_max" -#define SECTION_PEER_PRIVATE "peer_private" -#define SECTION_PEERS_MAX "peers_max" -#define SECTION_RPC_STARTUP "rpc_startup" -#define SECTION_SIGNING_SUPPORT "signing_support" -#define SECTION_SNTP "sntp_servers" -#define SECTION_SSL_VERIFY "ssl_verify" -#define SECTION_SSL_VERIFY_FILE "ssl_verify_file" -#define SECTION_SSL_VERIFY_DIR "ssl_verify_dir" -#define SECTION_VALIDATORS_FILE "validators_file" -#define SECTION_VALIDATION_SEED "validation_seed" -#define SECTION_WEBSOCKET_PING_FREQ "websocket_ping_frequency" -#define SECTION_VALIDATOR_KEYS "validator_keys" +#define SECTION_AMENDMENTS "amendments" +#define SECTION_CLUSTER_NODES "cluster_nodes" +#define SECTION_COMPRESSION "compression" +#define SECTION_DEBUG_LOGFILE "debug_logfile" +#define SECTION_ELB_SUPPORT "elb_support" +#define SECTION_FEE_DEFAULT "fee_default" +#define SECTION_FEE_ACCOUNT_RESERVE "fee_account_reserve" +#define SECTION_FEE_OWNER_RESERVE "fee_owner_reserve" +#define SECTION_FETCH_DEPTH "fetch_depth" +#define SECTION_LEDGER_HISTORY "ledger_history" +#define SECTION_INSIGHT "insight" +#define SECTION_IPS "ips" +#define SECTION_IPS_FIXED "ips_fixed" +#define SECTION_NETWORK_QUORUM "network_quorum" +#define SECTION_NODE_SEED "node_seed" +#define SECTION_NODE_SIZE "node_size" +#define SECTION_PATH_SEARCH_OLD "path_search_old" +#define SECTION_PATH_SEARCH "path_search" +#define SECTION_PATH_SEARCH_FAST "path_search_fast" +#define SECTION_PATH_SEARCH_MAX "path_search_max" +#define SECTION_PEER_PRIVATE "peer_private" +#define SECTION_PEERS_MAX "peers_max" +#define SECTION_RPC_STARTUP "rpc_startup" +#define SECTION_SIGNING_SUPPORT "signing_support" +#define SECTION_SNTP "sntp_servers" +#define SECTION_SSL_VERIFY "ssl_verify" +#define SECTION_SSL_VERIFY_FILE "ssl_verify_file" +#define SECTION_SSL_VERIFY_DIR "ssl_verify_dir" +#define SECTION_VALIDATORS_FILE "validators_file" +#define SECTION_VALIDATION_SEED "validation_seed" +#define SECTION_WEBSOCKET_PING_FREQ "websocket_ping_frequency" +#define SECTION_VALIDATOR_KEYS "validator_keys" #define SECTION_VALIDATOR_KEY_REVOCATION "validator_key_revocation" -#define SECTION_VALIDATOR_LIST_KEYS "validator_list_keys" -#define SECTION_VALIDATOR_LIST_SITES "validator_list_sites" -#define SECTION_VALIDATORS "validators" -#define SECTION_VALIDATOR_TOKEN "validator_token" -#define SECTION_VETO_AMENDMENTS "veto_amendments" -#define SECTION_WORKERS "workers" +#define SECTION_VALIDATOR_LIST_KEYS "validator_list_keys" +#define SECTION_VALIDATOR_LIST_SITES "validator_list_sites" +#define SECTION_VALIDATORS "validators" +#define SECTION_VALIDATOR_TOKEN "validator_token" +#define SECTION_VETO_AMENDMENTS "veto_amendments" +#define SECTION_WORKERS "workers" -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/Coro.ipp b/src/ripple/core/Coro.ipp index b1b1abe6cb..0234f90926 100644 --- a/src/ripple/core/Coro.ipp +++ b/src/ripple/core/Coro.ipp @@ -25,40 +25,40 @@ namespace ripple { template -JobQueue::Coro:: -Coro(Coro_create_t, JobQueue& jq, JobType type, - std::string const& name, F&& f) +JobQueue::Coro::Coro( + Coro_create_t, + JobQueue& jq, + JobType type, + std::string const& name, + F&& f) : jq_(jq) , type_(type) , name_(name) , running_(false) , coro_( - [this, fn = std::forward(f)] - (boost::coroutines::asymmetric_coroutine::push_type& do_yield) - { - yield_ = &do_yield; - yield(); - fn(shared_from_this()); + [this, fn = std::forward(f)]( + boost::coroutines::asymmetric_coroutine::push_type& + do_yield) { + yield_ = &do_yield; + yield(); + fn(shared_from_this()); #ifndef NDEBUG - finished_ = true; + finished_ = true; #endif - }, boost::coroutines::attributes (megabytes(1))) + }, + boost::coroutines::attributes(megabytes(1))) { } -inline -JobQueue::Coro:: -~Coro() +inline JobQueue::Coro::~Coro() { #ifndef NDEBUG assert(finished_); #endif } -inline -void -JobQueue::Coro:: -yield() const +inline void +JobQueue::Coro::yield() const { { std::lock_guard lock(jq_.m_mutex); @@ -67,10 +67,8 @@ yield() const (*yield_)(); } -inline -bool -JobQueue::Coro:: -post() +inline bool +JobQueue::Coro::post() { { std::lock_guard lk(mutex_run_); @@ -78,11 +76,8 @@ post() } // sp keeps 'this' alive - if (jq_.addJob(type_, name_, - [this, sp = shared_from_this()](Job&) - { - resume(); - })) + if (jq_.addJob( + type_, name_, [this, sp = shared_from_this()](Job&) { resume(); })) { return true; } @@ -94,10 +89,8 @@ post() return false; } -inline -void -JobQueue::Coro:: -resume() +inline void +JobQueue::Coro::resume() { { std::lock_guard lk(mutex_run_); @@ -110,7 +103,7 @@ resume() auto saved = detail::getLocalValues().release(); detail::getLocalValues().reset(&lvs_); std::lock_guard lock(mutex_); - assert (coro_); + assert(coro_); coro_(); detail::getLocalValues().release(); detail::getLocalValues().reset(saved); @@ -119,21 +112,17 @@ resume() cv_.notify_all(); } -inline -bool -JobQueue::Coro:: -runnable() const +inline bool +JobQueue::Coro::runnable() const { return static_cast(coro_); } -inline -void -JobQueue::Coro:: -expectEarlyExit() +inline void +JobQueue::Coro::expectEarlyExit() { #ifndef NDEBUG - if (! finished_) + if (!finished_) #endif { // expectEarlyExit() must only ever be called from outside the @@ -150,19 +139,13 @@ expectEarlyExit() } } -inline -void -JobQueue::Coro:: -join() +inline void +JobQueue::Coro::join() { std::unique_lock lk(mutex_run_); - cv_.wait(lk, - [this]() - { - return running_ == false; - }); + cv_.wait(lk, [this]() { return running_ == false; }); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/DatabaseCon.h b/src/ripple/core/DatabaseCon.h index 0090df52b0..d79ecef207 100644 --- a/src/ripple/core/DatabaseCon.h +++ b/src/ripple/core/DatabaseCon.h @@ -28,47 +28,52 @@ #include namespace soci { - class session; +class session; } namespace ripple { -template +template class LockedPointer { public: using mutex = TMutex; + private: T* it_; std::unique_lock lock_; public: - LockedPointer (T* it, mutex& m) : it_ (it), lock_ (m) + LockedPointer(T* it, mutex& m) : it_(it), lock_(m) { } - LockedPointer (LockedPointer&& rhs) noexcept - : it_ (rhs.it_), lock_ (std::move (rhs.lock_)) + LockedPointer(LockedPointer&& rhs) noexcept + : it_(rhs.it_), lock_(std::move(rhs.lock_)) { } - LockedPointer () = delete; - LockedPointer (LockedPointer const& rhs) = delete; - LockedPointer& operator=(LockedPointer const& rhs) = delete; + LockedPointer() = delete; + LockedPointer(LockedPointer const& rhs) = delete; + LockedPointer& + operator=(LockedPointer const& rhs) = delete; - T* get () + T* + get() { return it_; } - T& operator*() + T& + operator*() { return *it_; } - T* operator->() + T* + operator->() { return it_; } explicit operator bool() const { - return bool (it_); + return bool(it_); } }; @@ -86,7 +91,7 @@ public: boost::filesystem::path dataDir; }; - template + template DatabaseCon( Setup const& setup, std::string const& DBName, @@ -94,8 +99,7 @@ public: std::array const& initSQL) { // Use temporary files or regular DB files? - auto const useTempFiles = - setup.standAlone && + auto const useTempFiles = setup.standAlone && setup.startUp != Config::LOAD && setup.startUp != Config::LOAD_FILE && setup.startUp != Config::REPLAY; @@ -105,7 +109,7 @@ public: init(pPath, pragma, initSQL); } - template + template DatabaseCon( boost::filesystem::path const& dataDir, std::string const& DBName, @@ -115,23 +119,26 @@ public: init((dataDir / DBName), pragma, initSQL); } - soci::session& getSession() + soci::session& + getSession() { return session_; } - LockedSociSession checkoutDb () + LockedSociSession + checkoutDb() { - return LockedSociSession (&session_, lock_); + return LockedSociSession(&session_, lock_); } - void setupCheckpointing (JobQueue*, Logs&); + void + setupCheckpointing(JobQueue*, Logs&); private: - - template + template void - init(boost::filesystem::path const& pPath, + init( + boost::filesystem::path const& pPath, std::array const& pragma, std::array const& initSQL) { @@ -156,8 +163,8 @@ private: }; DatabaseCon::Setup -setup_DatabaseCon (Config const& c); +setup_DatabaseCon(Config const& c); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/Job.h b/src/ripple/core/Job.h index c06e463ec1..df53ae8bda 100644 --- a/src/ripple/core/Job.h +++ b/src/ripple/core/Job.h @@ -30,8 +30,7 @@ namespace ripple { // Note that this queue should only be used for CPU-bound jobs // It is primarily intended for signature checking -enum JobType -{ +enum JobType { // Special type indicating an invalid job - will go away soon. jtINVALID = -1, @@ -39,45 +38,45 @@ enum JobType // earlier jobs having lower priority than later jobs. If you wish to // insert a job at a specific priority, simply add it at the right location. - jtPACK, // Make a fetch pack for a peer - jtPUBOLDLEDGER, // An old ledger has been accepted - jtVALIDATION_ut, // A validation from an untrusted source - jtTRANSACTION_l, // A local transaction - jtLEDGER_REQ, // Peer request ledger/txnset data - jtPROPOSAL_ut, // A proposal from an untrusted source - jtLEDGER_DATA, // Received data for a ledger we're acquiring - jtCLIENT, // A websocket command from the client - jtRPC, // A websocket command from the client - jtUPDATE_PF, // Update pathfinding requests - jtTRANSACTION, // A transaction received from the network - jtBATCH, // Apply batched transactions - jtADVANCE, // Advance validated/acquired ledgers - jtPUBLEDGER, // Publish a fully-accepted ledger - jtTXN_DATA, // Fetch a proposed set - jtWAL, // Write-ahead logging - jtVALIDATION_t, // A validation from a trusted source - jtWRITE, // Write out hashed objects - jtACCEPT, // Accept a consensus ledger - jtPROPOSAL_t, // A proposal from a trusted source - jtSWEEP, // Sweep for stale structures - jtNETOP_CLUSTER, // NetworkOPs cluster peer report - jtNETOP_TIMER, // NetworkOPs net timer processing - jtADMIN, // An administrative operation + jtPACK, // Make a fetch pack for a peer + jtPUBOLDLEDGER, // An old ledger has been accepted + jtVALIDATION_ut, // A validation from an untrusted source + jtTRANSACTION_l, // A local transaction + jtLEDGER_REQ, // Peer request ledger/txnset data + jtPROPOSAL_ut, // A proposal from an untrusted source + jtLEDGER_DATA, // Received data for a ledger we're acquiring + jtCLIENT, // A websocket command from the client + jtRPC, // A websocket command from the client + jtUPDATE_PF, // Update pathfinding requests + jtTRANSACTION, // A transaction received from the network + jtBATCH, // Apply batched transactions + jtADVANCE, // Advance validated/acquired ledgers + jtPUBLEDGER, // Publish a fully-accepted ledger + jtTXN_DATA, // Fetch a proposed set + jtWAL, // Write-ahead logging + jtVALIDATION_t, // A validation from a trusted source + jtWRITE, // Write out hashed objects + jtACCEPT, // Accept a consensus ledger + jtPROPOSAL_t, // A proposal from a trusted source + jtSWEEP, // Sweep for stale structures + jtNETOP_CLUSTER, // NetworkOPs cluster peer report + jtNETOP_TIMER, // NetworkOPs net timer processing + jtADMIN, // An administrative operation // Special job types which are not dispatched by the job pool - jtPEER , - jtDISK , - jtTXN_PROC , - jtOB_SETUP , - jtPATH_FIND , - jtHO_READ , - jtHO_WRITE , - jtGENERIC , // Used just to measure time + jtPEER, + jtDISK, + jtTXN_PROC, + jtOB_SETUP, + jtPATH_FIND, + jtHO_READ, + jtHO_WRITE, + jtGENERIC, // Used just to measure time // Node store monitoring - jtNS_SYNC_READ , - jtNS_ASYNC_READ , - jtNS_WRITE , + jtNS_SYNC_READ, + jtNS_ASYNC_READ, + jtNS_WRITE, }; class Job @@ -96,56 +95,66 @@ public: // function? Having the invariant "all Job objects refer to // a job" would reduce the number of states. // - Job (); + Job(); - //Job (Job const& other); + // Job (Job const& other); - Job (JobType type, std::uint64_t index); + Job(JobType type, std::uint64_t index); /** A callback used to check for canceling a job. */ - using CancelCallback = std::function ; + using CancelCallback = std::function; // VFALCO TODO try to remove the dependency on LoadMonitor. - Job (JobType type, - std::string const& name, - std::uint64_t index, - LoadMonitor& lm, - std::function const& job, - CancelCallback cancelCallback); + Job(JobType type, + std::string const& name, + std::uint64_t index, + LoadMonitor& lm, + std::function const& job, + CancelCallback cancelCallback); - //Job& operator= (Job const& other); + // Job& operator= (Job const& other); - JobType getType () const; + JobType + getType() const; - CancelCallback getCancelCallback () const; + CancelCallback + getCancelCallback() const; /** Returns the time when the job was queued. */ - clock_type::time_point const& queue_time () const; + clock_type::time_point const& + queue_time() const; /** Returns `true` if the running job should make a best-effort cancel. */ - bool shouldCancel () const; + bool + shouldCancel() const; - void doJob (); + void + doJob(); - void rename (std::string const& n); + void + rename(std::string const& n); // These comparison operators make the jobs sort in priority order // in the job set - bool operator< (const Job& j) const; - bool operator> (const Job& j) const; - bool operator<= (const Job& j) const; - bool operator>= (const Job& j) const; + bool + operator<(const Job& j) const; + bool + operator>(const Job& j) const; + bool + operator<=(const Job& j) const; + bool + operator>=(const Job& j) const; private: CancelCallback m_cancelCallback; - JobType mType; - std::uint64_t mJobIndex; - std::function mJob; - std::shared_ptr m_loadEvent; - std::string mName; + JobType mType; + std::uint64_t mJobIndex; + std::function mJob; + std::shared_ptr m_loadEvent; + std::string mName; clock_type::time_point m_queue_time; }; -} +} // namespace ripple #endif diff --git a/src/ripple/core/JobQueue.h b/src/ripple/core/JobQueue.h index 0d80e63df1..6c14b117db 100644 --- a/src/ripple/core/JobQueue.h +++ b/src/ripple/core/JobQueue.h @@ -21,20 +21,19 @@ #define RIPPLE_CORE_JOBQUEUE_H_INCLUDED #include -#include #include +#include #include #include #include -#include // workaround for boost 1.72 bug -#include // workaround for boost 1.72 bug #include +#include // workaround for boost 1.72 bug +#include // workaround for boost 1.72 bug namespace ripple { -namespace perf -{ - class PerfLog; +namespace perf { +class PerfLog; } class Logs; @@ -53,9 +52,7 @@ struct Coro_create_t When the JobQueue stops, it waits for all jobs and coroutines to finish. */ -class JobQueue - : public Stoppable - , private Workers::Callback +class JobQueue : public Stoppable, private Workers::Callback { public: /** Coroutines must run to completion. */ @@ -72,19 +69,19 @@ public: std::condition_variable cv_; boost::coroutines::asymmetric_coroutine::pull_type coro_; boost::coroutines::asymmetric_coroutine::push_type* yield_; - #ifndef NDEBUG +#ifndef NDEBUG bool finished_ = false; - #endif +#endif public: // Private: Used in the implementation template - Coro(Coro_create_t, JobQueue&, JobType, - std::string const&, F&&); + Coro(Coro_create_t, JobQueue&, JobType, std::string const&, F&&); // Not copy-constructible or assignable Coro(Coro const&) = delete; - Coro& operator= (Coro const&) = delete; + Coro& + operator=(Coro const&) = delete; ~Coro(); @@ -94,25 +91,27 @@ public: The associated Job thread is released. Note: The associated Job function returns. - Undefined behavior if called consecutively without a corresponding post. + Undefined behavior if called consecutively without a corresponding + post. */ - void yield() const; + void + yield() const; /** Schedule coroutine execution. Effects: Returns immediately. A new job is scheduled to resume the execution of the coroutine. When the job runs, the coroutine's stack is restored and execution - continues at the beginning of coroutine function or the statement - after the previous call to yield. - Undefined behavior if called after the coroutine has completed - with a return (as opposed to a yield()). - Undefined behavior if post() or resume() called consecutively - without a corresponding yield. + continues at the beginning of coroutine function or the + statement after the previous call to yield. Undefined behavior if + called after the coroutine has completed with a return (as opposed to + a yield()). Undefined behavior if post() or resume() called + consecutively without a corresponding yield. @return true if the Coro's job is added to the JobQueue. */ - bool post(); + bool + post(); /** Resume coroutine execution. Effects: @@ -123,44 +122,53 @@ public: Undefined behavior if resume() or post() called consecutively without a corresponding yield. */ - void resume(); + void + resume(); /** Returns true if the Coro is still runnable (has not returned). */ - bool runnable() const; + bool + runnable() const; /** Once called, the Coro allows early exit without an assert. */ - void expectEarlyExit(); + void + expectEarlyExit(); /** Waits until coroutine returns from the user function. */ - void join(); + void + join(); }; - using JobFunction = std::function ; + using JobFunction = std::function; - JobQueue (beast::insight::Collector::ptr const& collector, - Stoppable& parent, beast::Journal journal, Logs& logs, + JobQueue( + beast::insight::Collector::ptr const& collector, + Stoppable& parent, + beast::Journal journal, + Logs& logs, perf::PerfLog& perfLog); - ~JobQueue (); + ~JobQueue(); /** Adds a job to the JobQueue. @param type The type of job. @param name Name of the job. - @param jobHandler Lambda with signature void (Job&). Called when the job is executed. + @param jobHandler Lambda with signature void (Job&). Called when the + job is executed. @return true if jobHandler added to queue. */ - template ()(std::declval())), void>::value>> - bool addJob (JobType type, - std::string const& name, JobHandler&& jobHandler) + template < + typename JobHandler, + typename = std::enable_if_t()(std::declval())), + void>::value>> + bool + addJob(JobType type, std::string const& name, JobHandler&& jobHandler) { - if (auto optionalCountedJob = - Stoppable::jobCounter().wrap (std::forward(jobHandler))) + if (auto optionalCountedJob = Stoppable::jobCounter().wrap( + std::forward(jobHandler))) { - return addRefCountedJob ( - type, name, std::move (*optionalCountedJob)); + return addRefCountedJob(type, name, std::move(*optionalCountedJob)); } return false; } @@ -169,43 +177,52 @@ public: @param t The type of job. @param name Name of the job. - @param f Has a signature of void(std::shared_ptr). Called when the job executes. + @param f Has a signature of void(std::shared_ptr). Called when the + job executes. @return shared_ptr to posted Coro. nullptr if post was not successful. */ template - std::shared_ptr postCoro (JobType t, std::string const& name, F&& f); + std::shared_ptr + postCoro(JobType t, std::string const& name, F&& f); /** Jobs waiting at this priority. - */ - int getJobCount (JobType t) const; + */ + int + getJobCount(JobType t) const; /** Jobs waiting plus running at this priority. - */ - int getJobCountTotal (JobType t) const; + */ + int + getJobCountTotal(JobType t) const; /** All waiting jobs at or greater than this priority. - */ - int getJobCountGE (JobType t) const; + */ + int + getJobCountGE(JobType t) const; /** Set the number of thread serving the job queue to precisely this number. - */ - void setThreadCount (int c, bool const standaloneMode); + */ + void + setThreadCount(int c, bool const standaloneMode); /** Return a scoped LoadEvent. - */ - std::unique_ptr - makeLoadEvent (JobType t, std::string const& name); + */ + std::unique_ptr + makeLoadEvent(JobType t, std::string const& name); /** Add multiple load events. - */ - void addLoadEvents (JobType t, int count, std::chrono::milliseconds elapsed); + */ + void + addLoadEvents(JobType t, int count, std::chrono::milliseconds elapsed); // Cannot be const because LoadMonitor has no const methods. - bool isOverloaded (); + bool + isOverloaded(); // Cannot be const because LoadMonitor has no const methods. - Json::Value getJson (int c = 0); + Json::Value + getJson(int c = 0); /** Block until no tasks running. */ void @@ -214,12 +231,12 @@ public: private: friend class Coro; - using JobDataMap = std::map ; + using JobDataMap = std::map; beast::Journal m_journal; mutable std::mutex m_mutex; std::uint64_t m_lastJob; - std::set m_jobSet; + std::set m_jobSet; JobDataMap m_jobData; JobTypeData m_invalidJobData; @@ -240,23 +257,31 @@ private: std::condition_variable cv_; - void collect(); - JobTypeData& getJobTypeData (JobType type); + void + collect(); + JobTypeData& + getJobTypeData(JobType type); - void onStop() override; + void + onStop() override; // Signals the service stopped if the stopped condition is met. - void checkStopped (std::lock_guard const& lock); + void + checkStopped(std::lock_guard const& lock); // Adds a reference counted job to the JobQueue. // // param type The type of job. // param name Name of the job. - // param func std::function with signature void (Job&). Called when the job is executed. + // param func std::function with signature void (Job&). Called when the + // job is executed. // // return true if func added to queue. - bool addRefCountedJob ( - JobType type, std::string const& name, JobFunction const& func); + bool + addRefCountedJob( + JobType type, + std::string const& name, + JobFunction const& func); // Signals an added Job for processing. // @@ -267,11 +292,13 @@ private: // // Post-conditions: // Count of waiting jobs of that type will be incremented. - // If JobQueue exists, and has at least one thread, Job will eventually run. + // If JobQueue exists, and has at least one thread, Job will eventually + // run. // // Invariants: // The calling thread owns the JobLock - void queueJob (Job const& job, std::lock_guard const& lock); + void + queueJob(Job const& job, std::lock_guard const& lock); // Returns the next Job we should run now. // @@ -290,7 +317,8 @@ private: // // Invariants: // The calling thread owns the JobLock - void getNextJob (Job& job); + void + getNextJob(Job& job); // Indicates that a running Job has completed its task. // @@ -300,11 +328,13 @@ private: // // Post-conditions: // The running count of that JobType is decremented - // A new task is signaled if there are more waiting Jobs than the limit, if any. + // A new task is signaled if there are more waiting Jobs than the limit, if + // any. // // Invariants: // - void finishJob (JobType type); + void + finishJob(JobType type); // Runs the next appropriate waiting Job. // @@ -316,14 +346,17 @@ private: // // Invariants: // - void processTask (int instance) override; + void + processTask(int instance) override; // Returns the limit of running jobs for the given job type. // For jobs with no limit, we return the largest int. Hopefully that // will be enough. - int getJobLimit (JobType type); + int + getJobLimit(JobType type); - void onChildrenStopped () override; + void + onChildrenStopped() override; }; /* @@ -336,9 +369,9 @@ private: postCoro() creates a Coro object. When the Coro ctor is called, and its coro_ member is initialized (a boost::coroutines::pull_type), execution automatically passes to the coroutine, which we don't want at this point, - since we are still in the handler thread context. It's important to note here - that construction of a boost pull_type automatically passes execution to the - coroutine. A pull_type object automatically generates a push_type that is + since we are still in the handler thread context. It's important to note + here that construction of a boost pull_type automatically passes execution to + the coroutine. A pull_type object automatically generates a push_type that is passed as a parameter (do_yield) in the signature of the function the pull_type was created with. This function is immediately called during coro_ construction and within it, Coro::yield_ is assigned the push_type @@ -372,11 +405,10 @@ private: 2- The coroutine is about to suspend, but before it can do so, it must arrange for some event to wake it up. 3- The coroutine arranges for some event to wake it up. - 4- Before the coroutine can suspend, that event occurs and the resumption - of the coroutine is scheduled on the job queue. - 5- Again, before the coroutine can suspend, the resumption of the coroutine - is dispatched. - 6- Again, before the coroutine can suspend, the resumption code runs the + 4- Before the coroutine can suspend, that event occurs and the + resumption of the coroutine is scheduled on the job queue. 5- Again, before + the coroutine can suspend, the resumption of the coroutine is dispatched. 6- + Again, before the coroutine can suspend, the resumption code runs the coroutine. The coroutine is now running in two threads. @@ -384,7 +416,7 @@ private: lock is released which only happens after the coroutine completes. */ -} // ripple +} // namespace ripple #include @@ -392,7 +424,7 @@ namespace ripple { template std::shared_ptr -JobQueue::postCoro (JobType t, std::string const& name, F&& f) +JobQueue::postCoro(JobType t, std::string const& name, F&& f) { /* First param is a detail type to make construction private. Last param is the function the coroutine runs. Signature of @@ -400,7 +432,7 @@ JobQueue::postCoro (JobType t, std::string const& name, F&& f) */ auto coro = std::make_shared( Coro_create_t{}, *this, t, name, std::forward(f)); - if (! coro->post()) + if (!coro->post()) { // The Coro was not successfully posted. Disable it so it's destructor // can run with no negative side effects. Then destroy it. @@ -410,6 +442,6 @@ JobQueue::postCoro (JobType t, std::string const& name, F&& f) return coro; } -} +} // namespace ripple #endif diff --git a/src/ripple/core/JobTypeData.h b/src/ripple/core/JobTypeData.h index 303bb2cdbd..3f0cdb6e93 100644 --- a/src/ripple/core/JobTypeData.h +++ b/src/ripple/core/JobTypeData.h @@ -21,11 +21,10 @@ #define RIPPLE_CORE_JOBTYPEDATA_H_INCLUDED #include -#include #include +#include -namespace ripple -{ +namespace ripple { struct JobTypeData { @@ -52,51 +51,57 @@ public: beast::insight::Event dequeue; beast::insight::Event execute; - JobTypeData (JobTypeInfo const& info_, - beast::insight::Collector::ptr const& collector, Logs& logs) noexcept - : m_load (logs.journal ("LoadMonitor")) - , m_collector (collector) - , info (info_) - , waiting (0) - , running (0) - , deferred (0) + JobTypeData( + JobTypeInfo const& info_, + beast::insight::Collector::ptr const& collector, + Logs& logs) noexcept + : m_load(logs.journal("LoadMonitor")) + , m_collector(collector) + , info(info_) + , waiting(0) + , running(0) + , deferred(0) { - m_load.setTargetLatency ( - info.getAverageLatency (), - info.getPeakLatency()); + m_load.setTargetLatency( + info.getAverageLatency(), info.getPeakLatency()); - if (!info.special ()) + if (!info.special()) { - dequeue = m_collector->make_event (info.name () + "_q"); - execute = m_collector->make_event (info.name ()); + dequeue = m_collector->make_event(info.name() + "_q"); + execute = m_collector->make_event(info.name()); } } /* Not copy-constructible or assignable */ - JobTypeData (JobTypeData const& other) = delete; - JobTypeData& operator= (JobTypeData const& other) = delete; + JobTypeData(JobTypeData const& other) = delete; + JobTypeData& + operator=(JobTypeData const& other) = delete; - std::string name () const + std::string + name() const { - return info.name (); + return info.name(); } - JobType type () const + JobType + type() const { - return info.type (); + return info.type(); } - LoadMonitor& load () + LoadMonitor& + load() { return m_load; } - LoadMonitor::Stats stats () + LoadMonitor::Stats + stats() { - return m_load.getStats (); + return m_load.getStats(); } }; -} +} // namespace ripple #endif diff --git a/src/ripple/core/JobTypeInfo.h b/src/ripple/core/JobTypeInfo.h index 389dcf0098..f4bb79b050 100644 --- a/src/ripple/core/JobTypeInfo.h +++ b/src/ripple/core/JobTypeInfo.h @@ -22,8 +22,7 @@ #include -namespace ripple -{ +namespace ripple { /** Holds all the 'static' information about a job, which does not change */ class JobTypeInfo @@ -44,52 +43,61 @@ private: public: // Not default constructible - JobTypeInfo () = delete; + JobTypeInfo() = delete; - JobTypeInfo (JobType type, std::string name, int limit, - bool special, std::chrono::milliseconds avgLatency, - std::chrono::milliseconds peakLatency) - : m_type (type) - , m_name (std::move(name)) - , m_limit (limit) - , m_special (special) - , m_avgLatency (avgLatency) - , m_peakLatency (peakLatency) + JobTypeInfo( + JobType type, + std::string name, + int limit, + bool special, + std::chrono::milliseconds avgLatency, + std::chrono::milliseconds peakLatency) + : m_type(type) + , m_name(std::move(name)) + , m_limit(limit) + , m_special(special) + , m_avgLatency(avgLatency) + , m_peakLatency(peakLatency) { - } - JobType type () const + JobType + type() const { return m_type; } - std::string const& name () const + std::string const& + name() const { return m_name; } - int limit () const + int + limit() const { return m_limit; } - bool special () const + bool + special() const { return m_special; } - std::chrono::milliseconds getAverageLatency () const + std::chrono::milliseconds + getAverageLatency() const { return m_avgLatency; } - std::chrono::milliseconds getPeakLatency () const + std::chrono::milliseconds + getPeakLatency() const { return m_peakLatency; } }; -} +} // namespace ripple #endif diff --git a/src/ripple/core/JobTypes.h b/src/ripple/core/JobTypes.h index 3abfeb1e99..881b3a2e1c 100644 --- a/src/ripple/core/JobTypes.h +++ b/src/ripple/core/JobTypes.h @@ -27,73 +27,88 @@ #include #include -namespace ripple -{ +namespace ripple { class JobTypes { public: - using Map = std::map ; + using Map = std::map; using const_iterator = Map::const_iterator; private: - JobTypes () - : m_unknown (jtINVALID, "invalid", 0, true, std::chrono::milliseconds{0}, - std::chrono::milliseconds{0}) + JobTypes() + : m_unknown( + jtINVALID, + "invalid", + 0, + true, + std::chrono::milliseconds{0}, + std::chrono::milliseconds{0}) { using namespace std::chrono_literals; - int maxLimit = std::numeric_limits ::max (); + int maxLimit = std::numeric_limits::max(); -add( jtPACK, "makeFetchPack", 1, false, 0ms, 0ms); -add( jtPUBOLDLEDGER, "publishAcqLedger", 2, false, 10000ms, 15000ms); -add( jtVALIDATION_ut, "untrustedValidation", maxLimit, false, 2000ms, 5000ms); -add( jtTRANSACTION_l, "localTransaction", maxLimit, false, 100ms, 500ms); -add( jtLEDGER_REQ, "ledgerRequest", 2, false, 0ms, 0ms); -add( jtPROPOSAL_ut, "untrustedProposal", maxLimit, false, 500ms, 1250ms); -add( jtLEDGER_DATA, "ledgerData", 2, false, 0ms, 0ms); -add( jtCLIENT, "clientCommand", maxLimit, false, 2000ms, 5000ms); -add( jtRPC, "RPC", maxLimit, false, 0ms, 0ms); -add( jtUPDATE_PF, "updatePaths", maxLimit, false, 0ms, 0ms); -add( jtTRANSACTION, "transaction", maxLimit, false, 250ms, 1000ms); -add( jtBATCH, "batch", maxLimit, false, 250ms, 1000ms); -add( jtADVANCE, "advanceLedger", maxLimit, false, 0ms, 0ms); -add( jtPUBLEDGER, "publishNewLedger", maxLimit, false, 3000ms, 4500ms); -add( jtTXN_DATA, "fetchTxnData", 1, false, 0ms, 0ms); -add( jtWAL, "writeAhead", maxLimit, false, 1000ms, 2500ms); -add( jtVALIDATION_t, "trustedValidation", maxLimit, false, 500ms, 1500ms); -add( jtWRITE, "writeObjects", maxLimit, false, 1750ms, 2500ms); -add( jtACCEPT, "acceptLedger", maxLimit, false, 0ms, 0ms); -add( jtPROPOSAL_t, "trustedProposal", maxLimit, false, 100ms, 500ms); -add( jtSWEEP, "sweep", maxLimit, false, 0ms, 0ms); -add( jtNETOP_CLUSTER, "clusterReport", 1, false, 9999ms, 9999ms); -add( jtNETOP_TIMER, "heartbeat", 1, false, 999ms, 999ms); -add( jtADMIN, "administration", maxLimit, false, 0ms, 0ms); - -add( jtPEER, "peerCommand", 0, true, 200ms, 2500ms); -add( jtDISK, "diskAccess", 0, true, 500ms, 1000ms); -add( jtTXN_PROC, "processTransaction", 0, true, 0ms, 0ms); -add( jtOB_SETUP, "orderBookSetup", 0, true, 0ms, 0ms); -add( jtPATH_FIND, "pathFind", 0, true, 0ms, 0ms); -add( jtHO_READ, "nodeRead", 0, true, 0ms, 0ms); -add( jtHO_WRITE, "nodeWrite", 0, true, 0ms, 0ms); -add( jtGENERIC, "generic", 0, true, 0ms, 0ms); -add( jtNS_SYNC_READ, "SyncReadNode", 0, true, 0ms, 0ms); -add( jtNS_ASYNC_READ, "AsyncReadNode", 0, true, 0ms, 0ms); -add( jtNS_WRITE, "WriteNode", 0, true, 0ms, 0ms); + add(jtPACK, "makeFetchPack", 1, false, 0ms, 0ms); + add(jtPUBOLDLEDGER, "publishAcqLedger", 2, false, 10000ms, 15000ms); + add(jtVALIDATION_ut, + "untrustedValidation", + maxLimit, + false, + 2000ms, + 5000ms); + add(jtTRANSACTION_l, "localTransaction", maxLimit, false, 100ms, 500ms); + add(jtLEDGER_REQ, "ledgerRequest", 2, false, 0ms, 0ms); + add(jtPROPOSAL_ut, "untrustedProposal", maxLimit, false, 500ms, 1250ms); + add(jtLEDGER_DATA, "ledgerData", 2, false, 0ms, 0ms); + add(jtCLIENT, "clientCommand", maxLimit, false, 2000ms, 5000ms); + add(jtRPC, "RPC", maxLimit, false, 0ms, 0ms); + add(jtUPDATE_PF, "updatePaths", maxLimit, false, 0ms, 0ms); + add(jtTRANSACTION, "transaction", maxLimit, false, 250ms, 1000ms); + add(jtBATCH, "batch", maxLimit, false, 250ms, 1000ms); + add(jtADVANCE, "advanceLedger", maxLimit, false, 0ms, 0ms); + add(jtPUBLEDGER, "publishNewLedger", maxLimit, false, 3000ms, 4500ms); + add(jtTXN_DATA, "fetchTxnData", 1, false, 0ms, 0ms); + add(jtWAL, "writeAhead", maxLimit, false, 1000ms, 2500ms); + add(jtVALIDATION_t, + "trustedValidation", + maxLimit, + false, + 500ms, + 1500ms); + add(jtWRITE, "writeObjects", maxLimit, false, 1750ms, 2500ms); + add(jtACCEPT, "acceptLedger", maxLimit, false, 0ms, 0ms); + add(jtPROPOSAL_t, "trustedProposal", maxLimit, false, 100ms, 500ms); + add(jtSWEEP, "sweep", maxLimit, false, 0ms, 0ms); + add(jtNETOP_CLUSTER, "clusterReport", 1, false, 9999ms, 9999ms); + add(jtNETOP_TIMER, "heartbeat", 1, false, 999ms, 999ms); + add(jtADMIN, "administration", maxLimit, false, 0ms, 0ms); + add(jtPEER, "peerCommand", 0, true, 200ms, 2500ms); + add(jtDISK, "diskAccess", 0, true, 500ms, 1000ms); + add(jtTXN_PROC, "processTransaction", 0, true, 0ms, 0ms); + add(jtOB_SETUP, "orderBookSetup", 0, true, 0ms, 0ms); + add(jtPATH_FIND, "pathFind", 0, true, 0ms, 0ms); + add(jtHO_READ, "nodeRead", 0, true, 0ms, 0ms); + add(jtHO_WRITE, "nodeWrite", 0, true, 0ms, 0ms); + add(jtGENERIC, "generic", 0, true, 0ms, 0ms); + add(jtNS_SYNC_READ, "SyncReadNode", 0, true, 0ms, 0ms); + add(jtNS_ASYNC_READ, "AsyncReadNode", 0, true, 0ms, 0ms); + add(jtNS_WRITE, "WriteNode", 0, true, 0ms, 0ms); } public: - static JobTypes const& instance() + static JobTypes const& + instance() { static JobTypes const types; return types; } - JobTypeInfo const& get (JobType jt) const + JobTypeInfo const& + get(JobType jt) const { - Map::const_iterator const iter (m_map.find (jt)); - assert (iter != m_map.end ()); + Map::const_iterator const iter(m_map.find(jt)); + assert(iter != m_map.end()); if (iter != m_map.end()) return iter->second; @@ -101,50 +116,60 @@ public: return m_unknown; } - JobTypeInfo const& getInvalid () const + JobTypeInfo const& + getInvalid() const { return m_unknown; } - Map::size_type size () const + Map::size_type + size() const { return m_map.size(); } - const_iterator begin () const + const_iterator + begin() const { - return m_map.cbegin (); + return m_map.cbegin(); } - const_iterator cbegin () const + const_iterator + cbegin() const { - return m_map.cbegin (); + return m_map.cbegin(); } - const_iterator end () const + const_iterator + end() const { - return m_map.cend (); + return m_map.cend(); } - const_iterator cend () const + const_iterator + cend() const { - return m_map.cend (); + return m_map.cend(); } private: - void add(JobType jt, std::string name, int limit, - bool special, std::chrono::milliseconds avgLatency, + void + add(JobType jt, + std::string name, + int limit, + bool special, + std::chrono::milliseconds avgLatency, std::chrono::milliseconds peakLatency) { - assert (m_map.find (jt) == m_map.end ()); + assert(m_map.find(jt) == m_map.end()); - auto const [_, inserted] = m_map.emplace ( + auto const [_, inserted] = m_map.emplace( std::piecewise_construct, - std::forward_as_tuple (jt), - std::forward_as_tuple (jt, name, limit, special, - avgLatency, peakLatency)); + std::forward_as_tuple(jt), + std::forward_as_tuple( + jt, name, limit, special, avgLatency, peakLatency)); - assert (inserted == true); + assert(inserted == true); (void)_; (void)inserted; } @@ -153,6 +178,6 @@ private: Map m_map; }; -} +} // namespace ripple #endif diff --git a/src/ripple/core/LoadEvent.h b/src/ripple/core/LoadEvent.h index b4b1bf4aed..458c8a0cd6 100644 --- a/src/ripple/core/LoadEvent.h +++ b/src/ripple/core/LoadEvent.h @@ -37,15 +37,13 @@ class LoadEvent { public: // VFALCO TODO remove the dependency on LoadMonitor. Is that possible? - LoadEvent (LoadMonitor& monitor, - std::string const& name, - bool shouldStart); + LoadEvent(LoadMonitor& monitor, std::string const& name, bool shouldStart); LoadEvent(LoadEvent const&) = delete; - ~LoadEvent (); + ~LoadEvent(); std::string const& - name () const; + name() const; // The time spent waiting. std::chrono::steady_clock::duration @@ -55,17 +53,20 @@ public: std::chrono::steady_clock::duration runTime() const; - void setName (std::string const& name); + void + setName(std::string const& name); // Start the measurement. If already started, then // restart, assigning the elapsed time to the "waiting" // state. - void start (); + void + start(); // Stop the measurement and report the results. The // time reported is measured from the last call to // start. - void stop (); + void + stop(); private: LoadMonitor& monitor_; @@ -84,6 +85,6 @@ private: std::chrono::steady_clock::duration timeRunning_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/LoadMonitor.h b/src/ripple/core/LoadMonitor.h index 1f3643d2d2..e903bf25e3 100644 --- a/src/ripple/core/LoadMonitor.h +++ b/src/ripple/core/LoadMonitor.h @@ -21,8 +21,8 @@ #define RIPPLE_CORE_LOADMONITOR_H_INCLUDED #include -#include #include +#include #include #include @@ -30,54 +30,61 @@ namespace ripple { // Monitors load levels and response times -// VFALCO TODO Rename this. Having both LoadManager and LoadMonitor is confusing. +// VFALCO TODO Rename this. Having both LoadManager and LoadMonitor is +// confusing. // class LoadMonitor { public: - explicit - LoadMonitor (beast::Journal j); + explicit LoadMonitor(beast::Journal j); - void addLoadSample (LoadEvent const& sample); + void + addLoadSample(LoadEvent const& sample); - void addSamples (int count, std::chrono::milliseconds latency); + void + addSamples(int count, std::chrono::milliseconds latency); - void setTargetLatency (std::chrono::milliseconds avg, - std::chrono::milliseconds pk); + void + setTargetLatency( + std::chrono::milliseconds avg, + std::chrono::milliseconds pk); - bool isOverTarget (std::chrono::milliseconds avg, - std::chrono::milliseconds peak); + bool + isOverTarget(std::chrono::milliseconds avg, std::chrono::milliseconds peak); // VFALCO TODO make this return the values in a struct. struct Stats { Stats(); - std::uint64_t count; + std::uint64_t count; std::chrono::milliseconds latencyAvg; std::chrono::milliseconds latencyPeak; - bool isOverloaded; + bool isOverloaded; }; - Stats getStats (); + Stats + getStats(); - bool isOver (); + bool + isOver(); private: - void update (); + void + update(); std::mutex mutex_; - std::uint64_t mCounts; - int mLatencyEvents; + std::uint64_t mCounts; + int mLatencyEvents; std::chrono::milliseconds mLatencyMSAvg; std::chrono::milliseconds mLatencyMSPeak; std::chrono::milliseconds mTargetLatencyAvg; std::chrono::milliseconds mTargetLatencyPk; - UptimeClock::time_point mLastUpdate; + UptimeClock::time_point mLastUpdate; beast::Journal const j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/SociDB.h b/src/ripple/core/SociDB.h index 3904dd4495..20c545da4e 100644 --- a/src/ripple/core/SociDB.h +++ b/src/ripple/core/SociDB.h @@ -36,30 +36,31 @@ #include #include #define SOCI_USE_BOOST +#include #include #include -#include #include namespace sqlite_api { - struct sqlite3; +struct sqlite3; } namespace ripple { template -T rangeCheckedCast (C c) +T +rangeCheckedCast(C c) { - if ((c > std::numeric_limits::max ()) || + if ((c > std::numeric_limits::max()) || (!std::numeric_limits::is_signed && c < 0) || (std::numeric_limits::is_signed && std::numeric_limits::is_signed && - c < std::numeric_limits::lowest ())) + c < std::numeric_limits::lowest())) { - JLOG (debugLog().error()) << "rangeCheckedCast domain error:" - << " value = " << c - << " min = " << std::numeric_limits::lowest () - << " max: " << std::numeric_limits::max (); + JLOG(debugLog().error()) + << "rangeCheckedCast domain error:" + << " value = " << c << " min = " << std::numeric_limits::lowest() + << " max: " << std::numeric_limits::max(); } return static_cast(c); @@ -79,10 +80,11 @@ class SociConfig SociConfig(std::pair init); public: - SociConfig(BasicConfig const& config, - std::string const& dbName); - std::string connectionString () const; - void open (soci::session& s) const; + SociConfig(BasicConfig const& config, std::string const& dbName); + std::string + connectionString() const; + void + open(soci::session& s) const; }; /** @@ -97,9 +99,8 @@ public: backends. Sometimes it is part of a filename (sqlite3), other times it is a database name (postgresql). */ -void open (soci::session& s, - BasicConfig const& config, - std::string const& dbName); +void +open(soci::session& s, BasicConfig const& config, std::string const& dbName); /** * Open a soci session. @@ -110,21 +111,29 @@ void open (soci::session& s, * see the soci::open documentation for how to use this. * */ -void open (soci::session& s, - std::string const& beName, - std::string const& connectionString); +void +open( + soci::session& s, + std::string const& beName, + std::string const& connectionString); -size_t getKBUsedAll (soci::session& s); -size_t getKBUsedDB (soci::session& s); +size_t +getKBUsedAll(soci::session& s); +size_t +getKBUsedDB(soci::session& s); -void convert (soci::blob& from, std::vector& to); -void convert (soci::blob& from, std::string& to); -void convert (std::vector const& from, soci::blob& to); -void convert (std::string const& from, soci::blob& to); +void +convert(soci::blob& from, std::vector& to); +void +convert(soci::blob& from, std::string& to); +void +convert(std::vector const& from, soci::blob& to); +void +convert(std::string const& from, soci::blob& to); class Checkpointer { - public: +public: virtual ~Checkpointer() = default; }; @@ -134,9 +143,10 @@ class Checkpointer The Checkpointer contains references to the session and job queue and so must outlive them both. */ -std::unique_ptr makeCheckpointer (soci::session&, JobQueue&, Logs&); +std::unique_ptr +makeCheckpointer(soci::session&, JobQueue&, Logs&); -} // ripple +} // namespace ripple #if defined(__clang__) #pragma clang diagnostic pop diff --git a/src/ripple/core/Stoppable.h b/src/ripple/core/Stoppable.h index 4d795147f8..c1366bef14 100644 --- a/src/ripple/core/Stoppable.h +++ b/src/ripple/core/Stoppable.h @@ -22,8 +22,8 @@ #include #include -#include #include +#include #include #include #include @@ -200,16 +200,20 @@ class RootStoppable; class Stoppable { protected: - Stoppable (std::string name, RootStoppable& root); + Stoppable(std::string name, RootStoppable& root); public: /** Create the Stoppable. */ - Stoppable (std::string name, Stoppable& parent); + Stoppable(std::string name, Stoppable& parent); /** Destroy the Stoppable. */ - virtual ~Stoppable (); + virtual ~Stoppable(); - RootStoppable& getRoot() {return m_root;} + RootStoppable& + getRoot() + { + return m_root; + } /** Set the parent of this Stoppable. @@ -217,45 +221,51 @@ public: The parent to be set cannot not be stopping. Both roots must match. */ - void setParent(Stoppable& parent); + void + setParent(Stoppable& parent); /** Returns `true` if the stoppable should stop. */ - bool isStopping () const; + bool + isStopping() const; /** Returns `true` if the requested stop has completed. */ - bool isStopped () const; + bool + isStopped() const; /** Returns `true` if all children have stopped. */ - bool areChildrenStopped () const; + bool + areChildrenStopped() const; /* JobQueue uses this method for Job counting. */ - inline JobCounter& jobCounter (); + inline JobCounter& + jobCounter(); /** Sleep or wake up on stop. @return `true` if we are stopping */ bool - alertable_sleep_until( - std::chrono::system_clock::time_point const& t); + alertable_sleep_until(std::chrono::system_clock::time_point const& t); protected: /** Called by derived classes to indicate that the stoppable has stopped. */ - void stopped (); + void + stopped(); private: /** Override called during preparation. Since all other Stoppable objects in the tree have already been - constructed, this provides an opportunity to perform initialization which - depends on calling into other Stoppable objects. - This call is made on the same thread that called prepare(). - The default implementation does nothing. - Guaranteed to only be called once. + constructed, this provides an opportunity to perform initialization + which depends on calling into other Stoppable objects. This call is made + on the same thread that called prepare(). The default implementation does + nothing. Guaranteed to only be called once. */ - virtual void onPrepare (); + virtual void + onPrepare(); /** Override called during start. */ - virtual void onStart (); + virtual void + onStart(); /** Override called when the stop notification is issued. @@ -278,7 +288,8 @@ private: Guaranteed only to be called once. Must be safe to call from any thread at any time. */ - virtual void onStop (); + virtual void + onStop(); /** Override called when all children have stopped. @@ -298,37 +309,42 @@ private: Guaranteed only to be called once. Must be safe to call from any thread at any time. */ - virtual void onChildrenStopped (); + virtual void + onChildrenStopped(); friend class RootStoppable; struct Child; - using Children = beast::LockFreeStack ; + using Children = beast::LockFreeStack; struct Child : Children::Node { - Child (Stoppable* stoppable_) : stoppable (stoppable_) + Child(Stoppable* stoppable_) : stoppable(stoppable_) { } Stoppable* stoppable; }; - void prepareRecursive (); - void startRecursive (); - void stopAsyncRecursive (beast::Journal j); - void stopRecursive (beast::Journal j); + void + prepareRecursive(); + void + startRecursive(); + void + stopAsyncRecursive(beast::Journal j); + void + stopRecursive(beast::Journal j); std::string m_name; RootStoppable& m_root; Child m_child; - std::atomic m_stopped {false}; - std::atomic m_childrenStopped {false}; + std::atomic m_stopped{false}; + std::atomic m_childrenStopped{false}; Children m_children; std::condition_variable m_cv; - std::mutex m_mut; - bool m_is_stopping = false; - bool hasParent_ {false}; + std::mutex m_mut; + bool m_is_stopping = false; + bool hasParent_{false}; }; //------------------------------------------------------------------------------ @@ -336,11 +352,12 @@ private: class RootStoppable : public Stoppable { public: - explicit RootStoppable (std::string name); + explicit RootStoppable(std::string name); - virtual ~RootStoppable (); + virtual ~RootStoppable(); - bool isStopping() const; + bool + isStopping() const; /** Prepare all contained Stoppable objects. This calls onPrepare for all Stoppable objects in the tree. @@ -348,7 +365,8 @@ public: Thread safety: May be called from any thread. */ - void prepare (); + void + prepare(); /** Start all contained Stoppable objects. The default implementation does nothing. @@ -356,7 +374,8 @@ public: Thread safety: May be called from any thread. */ - void start (); + void + start(); /** Notify a root stoppable and children to stop, and block until stopped. Has no effect if the stoppable was already notified. @@ -366,16 +385,19 @@ public: Thread safety: Safe to call from any thread not associated with a Stoppable. */ - void stop (beast::Journal j); + void + stop(beast::Journal j); /** Return true if start() was ever called. */ - bool started () const + bool + started() const { return m_started; } /* JobQueue uses this method for Job counting. */ - JobCounter& rootJobCounter () + JobCounter& + rootJobCounter() { return jobCounter_; } @@ -385,8 +407,7 @@ public: @return `true` if we are stopping */ bool - alertable_sleep_until( - std::chrono::system_clock::time_point const& t); + alertable_sleep_until(std::chrono::system_clock::time_point const& t); private: /* Notify a root stoppable and children to stop, without waiting. @@ -397,11 +418,12 @@ private: Thread safety: Safe to call from any thread at any time. */ - bool stopAsync(beast::Journal j); + bool + stopAsync(beast::Journal j); - std::atomic m_prepared {false}; - std::atomic m_started {false}; - std::atomic m_calledStop {false}; + std::atomic m_prepared{false}; + std::atomic m_started{false}; + std::atomic m_calledStop{false}; std::mutex m_; std::condition_variable c_; JobCounter jobCounter_; @@ -410,32 +432,30 @@ private: //------------------------------------------------------------------------------ -JobCounter& Stoppable::jobCounter () +JobCounter& +Stoppable::jobCounter() { return m_root.rootJobCounter(); } //------------------------------------------------------------------------------ -inline -bool +inline bool RootStoppable::alertable_sleep_until( std::chrono::system_clock::time_point const& t) { std::unique_lock lock(m_); if (m_calledStop) return true; - return c_.wait_until(lock, t, [this]{return m_calledStop.load();}); + return c_.wait_until(lock, t, [this] { return m_calledStop.load(); }); } -inline -bool -Stoppable::alertable_sleep_until( - std::chrono::system_clock::time_point const& t) +inline bool +Stoppable::alertable_sleep_until(std::chrono::system_clock::time_point const& t) { return m_root.alertable_sleep_until(t); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/TimeKeeper.h b/src/ripple/core/TimeKeeper.h index 88d1d6300f..ebc6c1f1ab 100644 --- a/src/ripple/core/TimeKeeper.h +++ b/src/ripple/core/TimeKeeper.h @@ -20,17 +20,16 @@ #ifndef RIPPLE_CORE_TIMEKEEPER_H_INCLUDED #define RIPPLE_CORE_TIMEKEEPER_H_INCLUDED +#include #include #include -#include #include #include namespace ripple { /** Manages various times used by the server. */ -class TimeKeeper - : public beast::abstract_clock +class TimeKeeper : public beast::abstract_clock { public: virtual ~TimeKeeper() = default; @@ -40,9 +39,8 @@ public: The internal thread synchronizes local network time using the provided list of SNTP servers. */ - virtual - void - run (std::vector const& servers) = 0; + virtual void + run(std::vector const& servers) = 0; /** Returns the estimate of wall time, in network time. @@ -56,8 +54,7 @@ public: Servers compute network time by adjusting a local wall clock using SNTP and then adjusting for the epoch. */ - virtual - time_point + virtual time_point now() const override = 0; /** Returns the close time, in network time. @@ -70,33 +67,28 @@ public: is correct, and tries to pull the close time towards its measure of network time. */ - virtual - time_point + virtual time_point closeTime() const = 0; /** Adjust the close time. This is called in response to received validations. */ - virtual - void - adjustCloseTime (std::chrono::duration amount) = 0; + virtual void + adjustCloseTime(std::chrono::duration amount) = 0; // This may return a negative value - virtual - std::chrono::duration + virtual std::chrono::duration nowOffset() const = 0; // This may return a negative value - virtual - std::chrono::duration + virtual std::chrono::duration closeOffset() const = 0; }; -extern -std::unique_ptr +extern std::unique_ptr make_TimeKeeper(beast::Journal j); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/impl/Config.cpp b/src/ripple/core/impl/Config.cpp index 3673280061..3fc805b21e 100644 --- a/src/ripple/core/impl/Config.cpp +++ b/src/ripple/core/impl/Config.cpp @@ -17,18 +17,18 @@ */ //============================================================================== -#include -#include -#include #include #include +#include +#include +#include +#include #include +#include #include #include -#include -#include -#include #include +#include #include #include #include @@ -39,103 +39,91 @@ namespace ripple { -inline constexpr -std::array>, 11> -sizedItems -{{ - // FIXME: We should document each of these items, explaining exactly what - // they control and whether there exists an explicit config option - // that can be used to override the default. - { SizedItem::sweepInterval, - {{ 10, 30, 60, 90, 120 }} }, - { SizedItem::treeCacheSize, - {{ 128000, 256000, 512000, 768000, 2048000 }} }, - { SizedItem::treeCacheAge, - {{ 30, 60, 90, 120, 900 }} }, - { SizedItem::ledgerSize, - {{ 32, 128, 256, 384, 768 }} }, - { SizedItem::ledgerAge, - {{ 30, 90, 180, 240, 900 }} }, - { SizedItem::ledgerFetch, - {{ 2, 3, 4, 5, 8 }} }, - { SizedItem::nodeCacheSize, - {{ 16384, 32768, 131072, 262144, 524288 }} }, - { SizedItem::nodeCacheAge, - {{ 60, 90, 120, 900, 1800 }} }, - { SizedItem::hashNodeDBCache, - {{ 4, 12, 24, 64, 128 }} }, - { SizedItem::txnDBCache, - {{ 4, 12, 24, 64, 128 }} }, - { SizedItem::lgrDBCache, - {{ 4, 8, 16, 32, 128 }} }, -}}; +inline constexpr std::array>, 11> + sizedItems{{ + // FIXME: We should document each of these items, explaining exactly + // what + // they control and whether there exists an explicit config + // option that can be used to override the default. + {SizedItem::sweepInterval, {{10, 30, 60, 90, 120}}}, + {SizedItem::treeCacheSize, {{128000, 256000, 512000, 768000, 2048000}}}, + {SizedItem::treeCacheAge, {{30, 60, 90, 120, 900}}}, + {SizedItem::ledgerSize, {{32, 128, 256, 384, 768}}}, + {SizedItem::ledgerAge, {{30, 90, 180, 240, 900}}}, + {SizedItem::ledgerFetch, {{2, 3, 4, 5, 8}}}, + {SizedItem::nodeCacheSize, {{16384, 32768, 131072, 262144, 524288}}}, + {SizedItem::nodeCacheAge, {{60, 90, 120, 900, 1800}}}, + {SizedItem::hashNodeDBCache, {{4, 12, 24, 64, 128}}}, + {SizedItem::txnDBCache, {{4, 12, 24, 64, 128}}}, + {SizedItem::lgrDBCache, {{4, 8, 16, 32, 128}}}, + }}; // Ensure that the order of entries in the table corresponds to the // order of entries in the enum: -static_assert([]() constexpr -> bool -{ - std::underlying_type_t idx = 0; +static_assert( + []() constexpr->bool { + std::underlying_type_t idx = 0; - for (auto const& i : sizedItems) - { - if (static_cast>(i.first) != idx) - return false; + for (auto const& i : sizedItems) + { + if (static_cast>(i.first) != idx) + return false; - ++idx; - } + ++idx; + } - return true; -}(), "Mismatch between sized item enum & array indices"); + return true; + }(), + "Mismatch between sized item enum & array indices"); // // TODO: Check permissions on config file before using it. // -#define SECTION_DEFAULT_NAME "" +#define SECTION_DEFAULT_NAME "" IniFileSections -parseIniFile (std::string const& strInput, const bool bTrim) +parseIniFile(std::string const& strInput, const bool bTrim) { - std::string strData (strInput); + std::string strData(strInput); std::vector vLines; IniFileSections secResult; // Convert DOS format to unix. - boost::algorithm::replace_all (strData, "\r\n", "\n"); + boost::algorithm::replace_all(strData, "\r\n", "\n"); // Convert MacOS format to unix. - boost::algorithm::replace_all (strData, "\r", "\n"); + boost::algorithm::replace_all(strData, "\r", "\n"); - boost::algorithm::split (vLines, strData, - boost::algorithm::is_any_of ("\n")); + boost::algorithm::split(vLines, strData, boost::algorithm::is_any_of("\n")); // Set the default Section name. - std::string strSection = SECTION_DEFAULT_NAME; + std::string strSection = SECTION_DEFAULT_NAME; // Initialize the default Section. - secResult[strSection] = IniFileSections::mapped_type (); + secResult[strSection] = IniFileSections::mapped_type(); // Parse each line. for (auto& strValue : vLines) { if (bTrim) - boost::algorithm::trim (strValue); + boost::algorithm::trim(strValue); - if (strValue.empty () || strValue[0] == '#') + if (strValue.empty() || strValue[0] == '#') { // Blank line or comment, do nothing. } - else if (strValue[0] == '[' && strValue[strValue.length () - 1] == ']') + else if (strValue[0] == '[' && strValue[strValue.length() - 1] == ']') { // New Section. - strSection = strValue.substr (1, strValue.length () - 2); + strSection = strValue.substr(1, strValue.length() - 2); secResult.emplace(strSection, IniFileSections::mapped_type{}); } else { // Another line for Section. - if (!strValue.empty ()) - secResult[strSection].push_back (strValue); + if (!strValue.empty()) + secResult[strSection].push_back(strValue); } } @@ -143,34 +131,38 @@ parseIniFile (std::string const& strInput, const bool bTrim) } IniFileSections::mapped_type* -getIniFileSection (IniFileSections& secSource, std::string const& strSection) +getIniFileSection(IniFileSections& secSource, std::string const& strSection) { IniFileSections::iterator it; IniFileSections::mapped_type* smtResult; - it = secSource.find (strSection); - if (it == secSource.end ()) - smtResult = nullptr; + it = secSource.find(strSection); + if (it == secSource.end()) + smtResult = nullptr; else - smtResult = & (it->second); + smtResult = &(it->second); return smtResult; } -bool getSingleSection (IniFileSections& secSource, - std::string const& strSection, std::string& strValue, beast::Journal j) +bool +getSingleSection( + IniFileSections& secSource, + std::string const& strSection, + std::string& strValue, + beast::Journal j) { IniFileSections::mapped_type* pmtEntries = - getIniFileSection (secSource, strSection); - bool bSingle = pmtEntries && 1 == pmtEntries->size (); + getIniFileSection(secSource, strSection); + bool bSingle = pmtEntries && 1 == pmtEntries->size(); if (bSingle) { - strValue = (*pmtEntries)[0]; + strValue = (*pmtEntries)[0]; } else if (pmtEntries) { - JLOG (j.warn()) << boost::str ( - boost::format ("Section [%s]: requires 1 line not %d lines.") % - strSection % pmtEntries->size ()); + JLOG(j.warn()) << boost::str( + boost::format("Section [%s]: requires 1 line not %d lines.") % + strSection % pmtEntries->size()); } return bSingle; @@ -186,13 +178,12 @@ char const* const Config::configFileName = "rippled.cfg"; char const* const Config::databaseDirName = "db"; char const* const Config::validatorsFileName = "validators.txt"; -static -std::string -getEnvVar (char const* name) +static std::string +getEnvVar(char const* name) { std::string value; - auto const v = getenv (name); + auto const v = getenv(name); if (v != nullptr) value = v; @@ -202,16 +193,20 @@ getEnvVar (char const* name) constexpr FeeUnit32 Config::TRANSACTION_FEE_BASE; -void Config::setupControl(bool bQuiet, - bool bSilent, bool bStandalone) +void +Config::setupControl(bool bQuiet, bool bSilent, bool bStandalone) { QUIET = bQuiet || bSilent; SILENT = bSilent; RUN_STANDALONE = bStandalone; } -void Config::setup (std::string const& strConf, bool bQuiet, - bool bSilent, bool bStandalone) +void +Config::setup( + std::string const& strConf, + bool bQuiet, + bool bSilent, + bool bStandalone) { boost::filesystem::path dataDir; std::string strDbPath, strConfFile; @@ -231,53 +226,54 @@ void Config::setup (std::string const& strConf, bool bQuiet, else strConfFile = configFileName; - if (!strConf.empty ()) + if (!strConf.empty()) { // --conf= : everything is relative that file. - CONFIG_FILE = strConfFile; - CONFIG_DIR = boost::filesystem::absolute (CONFIG_FILE); - CONFIG_DIR.remove_filename (); - dataDir = CONFIG_DIR / strDbPath; + CONFIG_FILE = strConfFile; + CONFIG_DIR = boost::filesystem::absolute(CONFIG_FILE); + CONFIG_DIR.remove_filename(); + dataDir = CONFIG_DIR / strDbPath; } else { - CONFIG_DIR = boost::filesystem::current_path (); - CONFIG_FILE = CONFIG_DIR / strConfFile; - dataDir = CONFIG_DIR / strDbPath; + CONFIG_DIR = boost::filesystem::current_path(); + CONFIG_FILE = CONFIG_DIR / strConfFile; + dataDir = CONFIG_DIR / strDbPath; // Construct XDG config and data home. // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html - std::string strHome = getEnvVar ("HOME"); - std::string strXdgConfigHome = getEnvVar ("XDG_CONFIG_HOME"); - std::string strXdgDataHome = getEnvVar ("XDG_DATA_HOME"); + std::string strHome = getEnvVar("HOME"); + std::string strXdgConfigHome = getEnvVar("XDG_CONFIG_HOME"); + std::string strXdgDataHome = getEnvVar("XDG_DATA_HOME"); - if (boost::filesystem::exists (CONFIG_FILE) - // Can we figure out XDG dirs? - || (strHome.empty () && (strXdgConfigHome.empty () || strXdgDataHome.empty ()))) + if (boost::filesystem::exists(CONFIG_FILE) + // Can we figure out XDG dirs? + || (strHome.empty() && + (strXdgConfigHome.empty() || strXdgDataHome.empty()))) { // Current working directory is fine, put dbs in a subdir. } else { - if (strXdgConfigHome.empty ()) + if (strXdgConfigHome.empty()) { // $XDG_CONFIG_HOME was not set, use default based on $HOME. - strXdgConfigHome = strHome + "/.config"; + strXdgConfigHome = strHome + "/.config"; } - if (strXdgDataHome.empty ()) + if (strXdgDataHome.empty()) { // $XDG_DATA_HOME was not set, use default based on $HOME. - strXdgDataHome = strHome + "/.local/share"; + strXdgDataHome = strHome + "/.local/share"; } - CONFIG_DIR = strXdgConfigHome + "/" + systemName (); + CONFIG_DIR = strXdgConfigHome + "/" + systemName(); CONFIG_FILE = CONFIG_DIR / strConfFile; - dataDir = strXdgDataHome + "/" + systemName (); + dataDir = strXdgDataHome + "/" + systemName(); - if (!boost::filesystem::exists (CONFIG_FILE)) + if (!boost::filesystem::exists(CONFIG_FILE)) { - CONFIG_DIR = "/etc/opt/" + systemName (); + CONFIG_DIR = "/etc/opt/" + systemName(); CONFIG_FILE = CONFIG_DIR / strConfFile; dataDir = "/var/opt/" + systemName(); } @@ -285,12 +281,12 @@ void Config::setup (std::string const& strConf, bool bQuiet, } // Update default values - load (); + load(); { // load() may have set a new value for the dataDir - std::string const dbPath (legacy ("database_path")); - if (!dbPath.empty ()) - dataDir = boost::filesystem::path (dbPath); + std::string const dbPath(legacy("database_path")); + if (!dbPath.empty()) + dataDir = boost::filesystem::path(dbPath); else if (RUN_STANDALONE) dataDir.clear(); } @@ -313,7 +309,8 @@ void Config::setup (std::string const& strConf, bool bQuiet, LEDGER_HISTORY = 0; } -void Config::load () +void +Config::load() { // NOTE: this writes to cerr because we want cout to be reserved // for the writing of the json response (so that stdout can be part of a @@ -326,48 +323,48 @@ void Config::load () if (ec) { - std::cerr << "Failed to read '" << CONFIG_FILE << "'." << - ec.value() << ": " << ec.message() << std::endl; + std::cerr << "Failed to read '" << CONFIG_FILE << "'." << ec.value() + << ": " << ec.message() << std::endl; return; } - loadFromString (fileContents); + loadFromString(fileContents); } -void Config::loadFromString (std::string const& fileContents) +void +Config::loadFromString(std::string const& fileContents) { - IniFileSections secConfig = parseIniFile (fileContents, true); + IniFileSections secConfig = parseIniFile(fileContents, true); - build (secConfig); + build(secConfig); - if (auto s = getIniFileSection (secConfig, SECTION_IPS)) + if (auto s = getIniFileSection(secConfig, SECTION_IPS)) IPS = *s; - if (auto s = getIniFileSection (secConfig, SECTION_IPS_FIXED)) + if (auto s = getIniFileSection(secConfig, SECTION_IPS_FIXED)) IPS_FIXED = *s; - if (auto s = getIniFileSection (secConfig, SECTION_SNTP)) + if (auto s = getIniFileSection(secConfig, SECTION_SNTP)) SNTP_SERVERS = *s; { std::string dbPath; - if (getSingleSection (secConfig, "database_path", dbPath, j_)) + if (getSingleSection(secConfig, "database_path", dbPath, j_)) { boost::filesystem::path p(dbPath); - legacy("database_path", - boost::filesystem::absolute (p).string ()); + legacy("database_path", boost::filesystem::absolute(p).string()); } } std::string strTemp; - if (getSingleSection (secConfig, SECTION_PEER_PRIVATE, strTemp, j_)) - PEER_PRIVATE = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_PEER_PRIVATE, strTemp, j_)) + PEER_PRIVATE = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_PEERS_MAX, strTemp, j_)) - PEERS_MAX = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_PEERS_MAX, strTemp, j_)) + PEERS_MAX = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_NODE_SIZE, strTemp, j_)) + if (getSingleSection(secConfig, SECTION_NODE_SIZE, strTemp, j_)) { if (boost::iequals(strTemp, "tiny")) NODE_SIZE = 0; @@ -380,85 +377,87 @@ void Config::loadFromString (std::string const& fileContents) else if (boost::iequals(strTemp, "huge")) NODE_SIZE = 4; else - NODE_SIZE = std::min(4, - beast::lexicalCastThrow(strTemp)); + NODE_SIZE = std::min( + 4, beast::lexicalCastThrow(strTemp)); } - if (getSingleSection (secConfig, SECTION_SIGNING_SUPPORT, strTemp, j_)) - signingEnabled_ = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_SIGNING_SUPPORT, strTemp, j_)) + signingEnabled_ = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_ELB_SUPPORT, strTemp, j_)) - ELB_SUPPORT = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_ELB_SUPPORT, strTemp, j_)) + ELB_SUPPORT = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_WEBSOCKET_PING_FREQ, strTemp, j_)) - WEBSOCKET_PING_FREQ = std::chrono::seconds{beast::lexicalCastThrow (strTemp)}; + if (getSingleSection(secConfig, SECTION_WEBSOCKET_PING_FREQ, strTemp, j_)) + WEBSOCKET_PING_FREQ = + std::chrono::seconds{beast::lexicalCastThrow(strTemp)}; - getSingleSection (secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_); - getSingleSection (secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_); + getSingleSection(secConfig, SECTION_SSL_VERIFY_FILE, SSL_VERIFY_FILE, j_); + getSingleSection(secConfig, SECTION_SSL_VERIFY_DIR, SSL_VERIFY_DIR, j_); - if (getSingleSection (secConfig, SECTION_SSL_VERIFY, strTemp, j_)) - SSL_VERIFY = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_SSL_VERIFY, strTemp, j_)) + SSL_VERIFY = beast::lexicalCastThrow(strTemp); if (exists(SECTION_VALIDATION_SEED) && exists(SECTION_VALIDATOR_TOKEN)) - Throw ( - "Cannot have both [" SECTION_VALIDATION_SEED "] " - "and [" SECTION_VALIDATOR_TOKEN "] config sections"); + Throw("Cannot have both [" SECTION_VALIDATION_SEED + "] " + "and [" SECTION_VALIDATOR_TOKEN + "] config sections"); - if (getSingleSection (secConfig, SECTION_NETWORK_QUORUM, strTemp, j_)) - NETWORK_QUORUM = beast::lexicalCastThrow(strTemp); + if (getSingleSection(secConfig, SECTION_NETWORK_QUORUM, strTemp, j_)) + NETWORK_QUORUM = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_FEE_ACCOUNT_RESERVE, strTemp, j_)) - FEE_ACCOUNT_RESERVE = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_FEE_ACCOUNT_RESERVE, strTemp, j_)) + FEE_ACCOUNT_RESERVE = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_FEE_OWNER_RESERVE, strTemp, j_)) - FEE_OWNER_RESERVE = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_FEE_OWNER_RESERVE, strTemp, j_)) + FEE_OWNER_RESERVE = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_FEE_DEFAULT, strTemp, j_)) - FEE_DEFAULT = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_FEE_DEFAULT, strTemp, j_)) + FEE_DEFAULT = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_LEDGER_HISTORY, strTemp, j_)) + if (getSingleSection(secConfig, SECTION_LEDGER_HISTORY, strTemp, j_)) { if (boost::iequals(strTemp, "full")) LEDGER_HISTORY = 1000000000u; else if (boost::iequals(strTemp, "none")) LEDGER_HISTORY = 0; else - LEDGER_HISTORY = beast::lexicalCastThrow (strTemp); + LEDGER_HISTORY = beast::lexicalCastThrow(strTemp); } - if (getSingleSection (secConfig, SECTION_FETCH_DEPTH, strTemp, j_)) + if (getSingleSection(secConfig, SECTION_FETCH_DEPTH, strTemp, j_)) { if (boost::iequals(strTemp, "none")) FETCH_DEPTH = 0; else if (boost::iequals(strTemp, "full")) FETCH_DEPTH = 1000000000u; else - FETCH_DEPTH = beast::lexicalCastThrow (strTemp); + FETCH_DEPTH = beast::lexicalCastThrow(strTemp); if (FETCH_DEPTH < 10) FETCH_DEPTH = 10; } - if (getSingleSection (secConfig, SECTION_PATH_SEARCH_OLD, strTemp, j_)) - PATH_SEARCH_OLD = beast::lexicalCastThrow (strTemp); - if (getSingleSection (secConfig, SECTION_PATH_SEARCH, strTemp, j_)) - PATH_SEARCH = beast::lexicalCastThrow (strTemp); - if (getSingleSection (secConfig, SECTION_PATH_SEARCH_FAST, strTemp, j_)) - PATH_SEARCH_FAST = beast::lexicalCastThrow (strTemp); - if (getSingleSection (secConfig, SECTION_PATH_SEARCH_MAX, strTemp, j_)) - PATH_SEARCH_MAX = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_PATH_SEARCH_OLD, strTemp, j_)) + PATH_SEARCH_OLD = beast::lexicalCastThrow(strTemp); + if (getSingleSection(secConfig, SECTION_PATH_SEARCH, strTemp, j_)) + PATH_SEARCH = beast::lexicalCastThrow(strTemp); + if (getSingleSection(secConfig, SECTION_PATH_SEARCH_FAST, strTemp, j_)) + PATH_SEARCH_FAST = beast::lexicalCastThrow(strTemp); + if (getSingleSection(secConfig, SECTION_PATH_SEARCH_MAX, strTemp, j_)) + PATH_SEARCH_MAX = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_DEBUG_LOGFILE, strTemp, j_)) - DEBUG_LOGFILE = strTemp; + if (getSingleSection(secConfig, SECTION_DEBUG_LOGFILE, strTemp, j_)) + DEBUG_LOGFILE = strTemp; - if (getSingleSection (secConfig, SECTION_WORKERS, strTemp, j_)) - WORKERS = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_WORKERS, strTemp, j_)) + WORKERS = beast::lexicalCastThrow(strTemp); - if (getSingleSection (secConfig, SECTION_COMPRESSION, strTemp, j_)) - COMPRESSION = beast::lexicalCastThrow (strTemp); + if (getSingleSection(secConfig, SECTION_COMPRESSION, strTemp, j_)) + COMPRESSION = beast::lexicalCastThrow(strTemp); // Do not load trusted validator configuration for standalone mode - if (! RUN_STANDALONE) + if (!RUN_STANDALONE) { // If a file was explicitly specified, then throw if the // path is malformed or if the file does not exist or is @@ -470,104 +469,108 @@ void Config::loadFromString (std::string const& fileContents) // if we can't find it. boost::filesystem::path validatorsFile; - if (getSingleSection (secConfig, SECTION_VALIDATORS_FILE, strTemp, j_)) + if (getSingleSection(secConfig, SECTION_VALIDATORS_FILE, strTemp, j_)) { validatorsFile = strTemp; - if (validatorsFile.empty ()) - Throw ( + if (validatorsFile.empty()) + Throw( "Invalid path specified in [" SECTION_VALIDATORS_FILE "]"); if (!validatorsFile.is_absolute() && !CONFIG_DIR.empty()) validatorsFile = CONFIG_DIR / validatorsFile; - if (!boost::filesystem::exists (validatorsFile)) - Throw ( - "The file specified in [" SECTION_VALIDATORS_FILE "] " - "does not exist: " + validatorsFile.string()); + if (!boost::filesystem::exists(validatorsFile)) + Throw( + "The file specified in [" SECTION_VALIDATORS_FILE + "] " + "does not exist: " + + validatorsFile.string()); - else if (!boost::filesystem::is_regular_file (validatorsFile) && - !boost::filesystem::is_symlink (validatorsFile)) - Throw ( - "Invalid file specified in [" SECTION_VALIDATORS_FILE "]: " + + else if ( + !boost::filesystem::is_regular_file(validatorsFile) && + !boost::filesystem::is_symlink(validatorsFile)) + Throw( + "Invalid file specified in [" SECTION_VALIDATORS_FILE + "]: " + validatorsFile.string()); } else if (!CONFIG_DIR.empty()) { validatorsFile = CONFIG_DIR / validatorsFileName; - if (!validatorsFile.empty ()) + if (!validatorsFile.empty()) { - if(!boost::filesystem::exists (validatorsFile)) + if (!boost::filesystem::exists(validatorsFile)) validatorsFile.clear(); - else if (!boost::filesystem::is_regular_file (validatorsFile) && - !boost::filesystem::is_symlink (validatorsFile)) + else if ( + !boost::filesystem::is_regular_file(validatorsFile) && + !boost::filesystem::is_symlink(validatorsFile)) validatorsFile.clear(); } } - if (!validatorsFile.empty () && - boost::filesystem::exists (validatorsFile) && - (boost::filesystem::is_regular_file (validatorsFile) || - boost::filesystem::is_symlink (validatorsFile))) + if (!validatorsFile.empty() && + boost::filesystem::exists(validatorsFile) && + (boost::filesystem::is_regular_file(validatorsFile) || + boost::filesystem::is_symlink(validatorsFile))) { boost::system::error_code ec; auto const data = getFileContents(ec, validatorsFile); if (ec) { - Throw("Failed to read '" + - validatorsFile.string() + "'." + + Throw( + "Failed to read '" + validatorsFile.string() + "'." + std::to_string(ec.value()) + ": " + ec.message()); } - auto iniFile = parseIniFile (data, true); + auto iniFile = parseIniFile(data, true); - auto entries = getIniFileSection ( - iniFile, - SECTION_VALIDATORS); + auto entries = getIniFileSection(iniFile, SECTION_VALIDATORS); if (entries) - section (SECTION_VALIDATORS).append (*entries); + section(SECTION_VALIDATORS).append(*entries); - auto valKeyEntries = getIniFileSection( - iniFile, - SECTION_VALIDATOR_KEYS); + auto valKeyEntries = + getIniFileSection(iniFile, SECTION_VALIDATOR_KEYS); if (valKeyEntries) - section (SECTION_VALIDATOR_KEYS).append (*valKeyEntries); + section(SECTION_VALIDATOR_KEYS).append(*valKeyEntries); - auto valSiteEntries = getIniFileSection( - iniFile, - SECTION_VALIDATOR_LIST_SITES); + auto valSiteEntries = + getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_SITES); if (valSiteEntries) - section (SECTION_VALIDATOR_LIST_SITES).append (*valSiteEntries); + section(SECTION_VALIDATOR_LIST_SITES).append(*valSiteEntries); - auto valListKeys = getIniFileSection( - iniFile, - SECTION_VALIDATOR_LIST_KEYS); + auto valListKeys = + getIniFileSection(iniFile, SECTION_VALIDATOR_LIST_KEYS); if (valListKeys) - section (SECTION_VALIDATOR_LIST_KEYS).append (*valListKeys); + section(SECTION_VALIDATOR_LIST_KEYS).append(*valListKeys); if (!entries && !valKeyEntries && !valListKeys) - Throw ( - "The file specified in [" SECTION_VALIDATORS_FILE "] " - "does not contain a [" SECTION_VALIDATORS "], " - "[" SECTION_VALIDATOR_KEYS "] or " - "[" SECTION_VALIDATOR_LIST_KEYS "]" + Throw( + "The file specified in [" SECTION_VALIDATORS_FILE + "] " + "does not contain a [" SECTION_VALIDATORS + "], " + "[" SECTION_VALIDATOR_KEYS + "] or " + "[" SECTION_VALIDATOR_LIST_KEYS + "]" " section: " + validatorsFile.string()); } // Consolidate [validator_keys] and [validators] - section (SECTION_VALIDATORS).append ( - section (SECTION_VALIDATOR_KEYS).lines ()); + section(SECTION_VALIDATORS) + .append(section(SECTION_VALIDATOR_KEYS).lines()); - if (! section (SECTION_VALIDATOR_LIST_SITES).lines().empty() && - section (SECTION_VALIDATOR_LIST_KEYS).lines().empty()) + if (!section(SECTION_VALIDATOR_LIST_SITES).lines().empty() && + section(SECTION_VALIDATOR_LIST_KEYS).lines().empty()) { - Throw ( + Throw( "[" + std::string(SECTION_VALIDATOR_LIST_KEYS) + "] config section is missing"); } @@ -575,7 +578,7 @@ void Config::loadFromString (std::string const& fileContents) { auto const part = section("features"); - for(auto const& s : part.values()) + for (auto const& s : part.values()) { if (auto const f = getRegisteredFeature(s)) features.insert(*f); @@ -604,34 +607,33 @@ void Config::loadFromString (std::string const& fileContents) } } -boost::filesystem::path Config::getDebugLogFile () const +boost::filesystem::path +Config::getDebugLogFile() const { auto log_file = DEBUG_LOGFILE; - if (!log_file.empty () && !log_file.is_absolute ()) + if (!log_file.empty() && !log_file.is_absolute()) { // Unless an absolute path for the log file is specified, the // path is relative to the config file directory. - log_file = boost::filesystem::absolute ( - log_file, CONFIG_DIR); + log_file = boost::filesystem::absolute(log_file, CONFIG_DIR); } - if (!log_file.empty ()) + if (!log_file.empty()) { - auto log_dir = log_file.parent_path (); + auto log_dir = log_file.parent_path(); - if (!boost::filesystem::is_directory (log_dir)) + if (!boost::filesystem::is_directory(log_dir)) { boost::system::error_code ec; - boost::filesystem::create_directories (log_dir, ec); + boost::filesystem::create_directories(log_dir, ec); // If we fail, we warn but continue so that the calling code can // decide how to handle this situation. if (ec) { - std::cerr << - "Unable to create log file path " << log_dir << - ": " << ec.message() << '\n'; + std::cerr << "Unable to create log file path " << log_dir + << ": " << ec.message() << '\n'; } } } @@ -648,4 +650,4 @@ Config::getValueFor(SizedItem item, boost::optional node) const return sizedItems.at(index).second.at(node.value_or(NODE_SIZE)); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/DatabaseCon.cpp b/src/ripple/core/impl/DatabaseCon.cpp index 54c8a4f835..3a4489b2f9 100644 --- a/src/ripple/core/impl/DatabaseCon.cpp +++ b/src/ripple/core/impl/DatabaseCon.cpp @@ -17,35 +17,36 @@ */ //============================================================================== +#include +#include #include #include -#include -#include #include namespace ripple { -DatabaseCon::Setup setup_DatabaseCon (Config const& c) +DatabaseCon::Setup +setup_DatabaseCon(Config const& c) { DatabaseCon::Setup setup; setup.startUp = c.START_UP; setup.standAlone = c.standalone(); - setup.dataDir = c.legacy ("database_path"); + setup.dataDir = c.legacy("database_path"); if (!setup.standAlone && setup.dataDir.empty()) { - Throw( - "database_path must be set."); + Throw("database_path must be set."); } return setup; } -void DatabaseCon::setupCheckpointing (JobQueue* q, Logs& l) +void +DatabaseCon::setupCheckpointing(JobQueue* q, Logs& l) { - if (! q) - Throw ("No JobQueue"); - checkpointer_ = makeCheckpointer (session_, *q, l); + if (!q) + Throw("No JobQueue"); + checkpointer_ = makeCheckpointer(session_, *q, l); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/Job.cpp b/src/ripple/core/impl/Job.cpp index a1b9dba79b..a9b82ccf33 100644 --- a/src/ripple/core/impl/Job.cpp +++ b/src/ripple/core/impl/Job.cpp @@ -17,82 +17,86 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { -Job::Job () - : mType (jtINVALID) - , mJobIndex (0) +Job::Job() : mType(jtINVALID), mJobIndex(0) { } -Job::Job (JobType type, std::uint64_t index) - : mType (type) - , mJobIndex (index) +Job::Job(JobType type, std::uint64_t index) : mType(type), mJobIndex(index) { } -Job::Job (JobType type, - std::string const& name, - std::uint64_t index, - LoadMonitor& lm, - std::function const& job, - CancelCallback cancelCallback) - : m_cancelCallback (cancelCallback) - , mType (type) - , mJobIndex (index) - , mJob (job) - , mName (name) - , m_queue_time (clock_type::now ()) +Job::Job( + JobType type, + std::string const& name, + std::uint64_t index, + LoadMonitor& lm, + std::function const& job, + CancelCallback cancelCallback) + : m_cancelCallback(cancelCallback) + , mType(type) + , mJobIndex(index) + , mJob(job) + , mName(name) + , m_queue_time(clock_type::now()) { - m_loadEvent = std::make_shared (std::ref (lm), name, false); + m_loadEvent = std::make_shared(std::ref(lm), name, false); } -JobType Job::getType () const +JobType +Job::getType() const { return mType; } -Job::CancelCallback Job::getCancelCallback () const +Job::CancelCallback +Job::getCancelCallback() const { - assert (m_cancelCallback); + assert(m_cancelCallback); return m_cancelCallback; } -Job::clock_type::time_point const& Job::queue_time () const +Job::clock_type::time_point const& +Job::queue_time() const { return m_queue_time; } -bool Job::shouldCancel () const +bool +Job::shouldCancel() const { if (m_cancelCallback) - return m_cancelCallback (); + return m_cancelCallback(); return false; } -void Job::doJob () +void +Job::doJob() { - beast::setCurrentThreadName ("doJob: " + mName); - m_loadEvent->start (); - m_loadEvent->setName (mName); + beast::setCurrentThreadName("doJob: " + mName); + m_loadEvent->start(); + m_loadEvent->setName(mName); - mJob (*this); + mJob(*this); // Destroy the lambda, otherwise we won't include // its duration in the time measurement mJob = nullptr; } -void Job::rename (std::string const& newName) +void +Job::rename(std::string const& newName) { mName = newName; } -bool Job::operator> (const Job& j) const +bool +Job::operator>(const Job& j) const { if (mType < j.mType) return true; @@ -103,7 +107,8 @@ bool Job::operator> (const Job& j) const return mJobIndex > j.mJobIndex; } -bool Job::operator>= (const Job& j) const +bool +Job::operator>=(const Job& j) const { if (mType < j.mType) return true; @@ -114,7 +119,8 @@ bool Job::operator>= (const Job& j) const return mJobIndex >= j.mJobIndex; } -bool Job::operator< (const Job& j) const +bool +Job::operator<(const Job& j) const { if (mType < j.mType) return false; @@ -125,7 +131,8 @@ bool Job::operator< (const Job& j) const return mJobIndex < j.mJobIndex; } -bool Job::operator<= (const Job& j) const +bool +Job::operator<=(const Job& j) const { if (mType < j.mType) return false; @@ -136,4 +143,4 @@ bool Job::operator<= (const Job& j) const return mJobIndex <= j.mJobIndex; } -} +} // namespace ripple diff --git a/src/ripple/core/impl/JobQueue.cpp b/src/ripple/core/impl/JobQueue.cpp index 3cf796f06e..cc4a6063b4 100644 --- a/src/ripple/core/impl/JobQueue.cpp +++ b/src/ripple/core/impl/JobQueue.cpp @@ -17,77 +17,83 @@ */ //============================================================================== -#include -#include #include +#include +#include namespace ripple { -JobQueue::JobQueue (beast::insight::Collector::ptr const& collector, - Stoppable& parent, beast::Journal journal, Logs& logs, +JobQueue::JobQueue( + beast::insight::Collector::ptr const& collector, + Stoppable& parent, + beast::Journal journal, + Logs& logs, perf::PerfLog& perfLog) - : Stoppable ("JobQueue", parent) - , m_journal (journal) - , m_lastJob (0) - , m_invalidJobData (JobTypes::instance().getInvalid (), collector, logs) - , m_processCount (0) - , m_workers (*this, &perfLog, "JobQueue", 0) - , m_cancelCallback (std::bind (&Stoppable::isStopping, this)) - , perfLog_ (perfLog) - , m_collector (collector) + : Stoppable("JobQueue", parent) + , m_journal(journal) + , m_lastJob(0) + , m_invalidJobData(JobTypes::instance().getInvalid(), collector, logs) + , m_processCount(0) + , m_workers(*this, &perfLog, "JobQueue", 0) + , m_cancelCallback(std::bind(&Stoppable::isStopping, this)) + , perfLog_(perfLog) + , m_collector(collector) { - hook = m_collector->make_hook (std::bind (&JobQueue::collect, this)); - job_count = m_collector->make_gauge ("job_count"); + hook = m_collector->make_hook(std::bind(&JobQueue::collect, this)); + job_count = m_collector->make_gauge("job_count"); { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); for (auto const& x : JobTypes::instance()) { JobTypeInfo const& jt = x.second; // And create dynamic information for all jobs - auto const result (m_jobData.emplace (std::piecewise_construct, - std::forward_as_tuple (jt.type ()), - std::forward_as_tuple (jt, m_collector, logs))); - assert (result.second == true); - (void) result.second; + auto const result(m_jobData.emplace( + std::piecewise_construct, + std::forward_as_tuple(jt.type()), + std::forward_as_tuple(jt, m_collector, logs))); + assert(result.second == true); + (void)result.second; } } } -JobQueue::~JobQueue () +JobQueue::~JobQueue() { // Must unhook before destroying - hook = beast::insight::Hook (); + hook = beast::insight::Hook(); } void -JobQueue::collect () +JobQueue::collect() { - std::lock_guard lock (m_mutex); - job_count = m_jobSet.size (); + std::lock_guard lock(m_mutex); + job_count = m_jobSet.size(); } bool -JobQueue::addRefCountedJob (JobType type, std::string const& name, +JobQueue::addRefCountedJob( + JobType type, + std::string const& name, JobFunction const& func) { - assert (type != jtINVALID); + assert(type != jtINVALID); - auto iter (m_jobData.find (type)); - assert (iter != m_jobData.end ()); - if (iter == m_jobData.end ()) + auto iter(m_jobData.find(type)); + assert(iter != m_jobData.end()); + if (iter == m_jobData.end()) return false; - JobTypeData& data (iter->second); + JobTypeData& data(iter->second); // FIXME: Workaround incorrect client shutdown ordering // do not add jobs to a queue with no threads - assert (type == jtCLIENT || m_workers.getNumberOfThreads () > 0); + assert(type == jtCLIENT || m_workers.getNumberOfThreads() > 0); { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); // If this goes off it means that a child didn't follow // the Stoppable API rules. A job may only be added if: @@ -100,50 +106,44 @@ JobQueue::addRefCountedJob (JobType type, std::string const& name, // OR // * Not all children are stopped // - assert (! isStopped() && ( - m_processCount>0 || - ! m_jobSet.empty () || - ! areChildrenStopped())); + assert( + !isStopped() && + (m_processCount > 0 || !m_jobSet.empty() || !areChildrenStopped())); - std::pair ::iterator, bool> result ( - m_jobSet.insert (Job (type, name, ++m_lastJob, - data.load (), func, m_cancelCallback))); - queueJob (*result.first, lock); + std::pair::iterator, bool> result(m_jobSet.insert( + Job(type, name, ++m_lastJob, data.load(), func, m_cancelCallback))); + queueJob(*result.first, lock); } return true; } int -JobQueue::getJobCount (JobType t) const +JobQueue::getJobCount(JobType t) const { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - JobDataMap::const_iterator c = m_jobData.find (t); + JobDataMap::const_iterator c = m_jobData.find(t); - return (c == m_jobData.end ()) - ? 0 - : c->second.waiting; + return (c == m_jobData.end()) ? 0 : c->second.waiting; } int -JobQueue::getJobCountTotal (JobType t) const +JobQueue::getJobCountTotal(JobType t) const { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); - JobDataMap::const_iterator c = m_jobData.find (t); + JobDataMap::const_iterator c = m_jobData.find(t); - return (c == m_jobData.end ()) - ? 0 - : (c->second.waiting + c->second.running); + return (c == m_jobData.end()) ? 0 : (c->second.waiting + c->second.running); } int -JobQueue::getJobCountGE (JobType t) const +JobQueue::getJobCountGE(JobType t) const { // return the number of jobs at this priority level or greater int ret = 0; - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); for (auto const& x : m_jobData) { @@ -155,7 +155,7 @@ JobQueue::getJobCountGE (JobType t) const } void -JobQueue::setThreadCount (int c, bool const standaloneMode) +JobQueue::setThreadCount(int c, bool const standaloneMode) { if (standaloneMode) { @@ -164,51 +164,50 @@ JobQueue::setThreadCount (int c, bool const standaloneMode) else if (c == 0) { c = static_cast(std::thread::hardware_concurrency()); - c = 2 + std::min(c, 4); // I/O will bottleneck - JLOG (m_journal.info()) << "Auto-tuning to " << c << - " validation/transaction/proposal threads."; + c = 2 + std::min(c, 4); // I/O will bottleneck + JLOG(m_journal.info()) << "Auto-tuning to " << c + << " validation/transaction/proposal threads."; } else { - JLOG (m_journal.info()) << "Configured " << c << - " validation/transaction/proposal threads."; + JLOG(m_journal.info()) << "Configured " << c + << " validation/transaction/proposal threads."; } - m_workers.setNumberOfThreads (c); + m_workers.setNumberOfThreads(c); } std::unique_ptr -JobQueue::makeLoadEvent (JobType t, std::string const& name) +JobQueue::makeLoadEvent(JobType t, std::string const& name) { - JobDataMap::iterator iter (m_jobData.find (t)); - assert (iter != m_jobData.end ()); + JobDataMap::iterator iter(m_jobData.find(t)); + assert(iter != m_jobData.end()); - if (iter == m_jobData.end ()) + if (iter == m_jobData.end()) return {}; - return std::make_unique (iter-> second.load (), name, true); + return std::make_unique(iter->second.load(), name, true); } void -JobQueue::addLoadEvents (JobType t, int count, - std::chrono::milliseconds elapsed) +JobQueue::addLoadEvents(JobType t, int count, std::chrono::milliseconds elapsed) { if (isStopped()) - LogicError ("JobQueue::addLoadEvents() called after JobQueue stopped"); + LogicError("JobQueue::addLoadEvents() called after JobQueue stopped"); - JobDataMap::iterator iter (m_jobData.find (t)); - assert (iter != m_jobData.end ()); - iter->second.load().addSamples (count, elapsed); + JobDataMap::iterator iter(m_jobData.find(t)); + assert(iter != m_jobData.end()); + iter->second.load().addSamples(count, elapsed); } bool -JobQueue::isOverloaded () +JobQueue::isOverloaded() { int count = 0; for (auto& x : m_jobData) { - if (x.second.load ().isOver ()) + if (x.second.load().isOver()) ++count; } @@ -216,37 +215,37 @@ JobQueue::isOverloaded () } Json::Value -JobQueue::getJson (int c) +JobQueue::getJson(int c) { using namespace std::chrono_literals; - Json::Value ret (Json::objectValue); + Json::Value ret(Json::objectValue); - ret["threads"] = m_workers.getNumberOfThreads (); + ret["threads"] = m_workers.getNumberOfThreads(); Json::Value priorities = Json::arrayValue; - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); for (auto& x : m_jobData) { - assert (x.first != jtINVALID); + assert(x.first != jtINVALID); if (x.first == jtGENERIC) continue; - JobTypeData& data (x.second); + JobTypeData& data(x.second); - LoadMonitor::Stats stats (data.stats ()); + LoadMonitor::Stats stats(data.stats()); - int waiting (data.waiting); - int running (data.running); + int waiting(data.waiting); + int running(data.running); if ((stats.count != 0) || (waiting != 0) || (stats.latencyPeak != 0ms) || (running != 0)) { - Json::Value& pri = priorities.append (Json::objectValue); + Json::Value& pri = priorities.append(Json::objectValue); - pri["job_type"] = data.name (); + pri["job_type"] = data.name(); if (stats.isOverloaded) pri["over_target"] = true; @@ -255,13 +254,13 @@ JobQueue::getJson (int c) pri["waiting"] = waiting; if (stats.count != 0) - pri["per_second"] = static_cast (stats.count); + pri["per_second"] = static_cast(stats.count); if (stats.latencyPeak != 0ms) - pri["peak_time"] = static_cast (stats.latencyPeak.count()); + pri["peak_time"] = static_cast(stats.latencyPeak.count()); if (stats.latencyAvg != 0ms) - pri["avg_time"] = static_cast (stats.latencyAvg.count()); + pri["avg_time"] = static_cast(stats.latencyAvg.count()); if (running != 0) pri["in_progress"] = running; @@ -277,22 +276,18 @@ void JobQueue::rendezvous() { std::unique_lock lock(m_mutex); - cv_.wait(lock, [&] - { - return m_processCount == 0 && - m_jobSet.empty(); - }); + cv_.wait(lock, [&] { return m_processCount == 0 && m_jobSet.empty(); }); } JobTypeData& -JobQueue::getJobTypeData (JobType type) +JobQueue::getJobTypeData(JobType type) { - JobDataMap::iterator c (m_jobData.find (type)); - assert (c != m_jobData.end ()); + JobDataMap::iterator c(m_jobData.find(type)); + assert(c != m_jobData.end()); // NIKB: This is ugly and I hate it. We must remove jtINVALID completely // and use something sane. - if (c == m_jobData.end ()) + if (c == m_jobData.end()) return m_invalidJobData; return c->second; @@ -306,7 +301,7 @@ JobQueue::onStop() } void -JobQueue::checkStopped (std::lock_guard const& lock) +JobQueue::checkStopped(std::lock_guard const& lock) { // We are stopped when all of the following are true: // @@ -316,29 +311,26 @@ JobQueue::checkStopped (std::lock_guard const& lock) // 4. There are no remaining Jobs in the job set // 5. There are no suspended coroutines // - if (isStopping() && - areChildrenStopped() && - (m_processCount == 0) && - m_jobSet.empty() && - nSuspend_ == 0) + if (isStopping() && areChildrenStopped() && (m_processCount == 0) && + m_jobSet.empty() && nSuspend_ == 0) { stopped(); } } void -JobQueue::queueJob (Job const& job, std::lock_guard const& lock) +JobQueue::queueJob(Job const& job, std::lock_guard const& lock) { - JobType const type (job.getType ()); - assert (type != jtINVALID); - assert (m_jobSet.find (job) != m_jobSet.end ()); + JobType const type(job.getType()); + assert(type != jtINVALID); + assert(m_jobSet.find(job) != m_jobSet.end()); perfLog_.jobQueue(type); - JobTypeData& data (getJobTypeData (type)); + JobTypeData& data(getJobTypeData(type)); - if (data.waiting + data.running < getJobLimit (type)) + if (data.waiting + data.running < getJobLimit(type)) { - m_workers.addTask (); + m_workers.addTask(); } else { @@ -350,88 +342,87 @@ JobQueue::queueJob (Job const& job, std::lock_guard const& lock) } void -JobQueue::getNextJob (Job& job) +JobQueue::getNextJob(Job& job) { - assert (! m_jobSet.empty ()); + assert(!m_jobSet.empty()); - std::set ::const_iterator iter; - for (iter = m_jobSet.begin (); iter != m_jobSet.end (); ++iter) + std::set::const_iterator iter; + for (iter = m_jobSet.begin(); iter != m_jobSet.end(); ++iter) { - JobTypeData& data (getJobTypeData (iter->getType ())); + JobTypeData& data(getJobTypeData(iter->getType())); - assert (data.running <= getJobLimit (data.type ())); + assert(data.running <= getJobLimit(data.type())); // Run this job if we're running below the limit. - if (data.running < getJobLimit (data.type ())) + if (data.running < getJobLimit(data.type())) { - assert (data.waiting > 0); + assert(data.waiting > 0); break; } } - assert (iter != m_jobSet.end ()); + assert(iter != m_jobSet.end()); - JobType const type = iter->getType (); - JobTypeData& data (getJobTypeData (type)); + JobType const type = iter->getType(); + JobTypeData& data(getJobTypeData(type)); - assert (type != jtINVALID); + assert(type != jtINVALID); job = *iter; - m_jobSet.erase (iter); + m_jobSet.erase(iter); --data.waiting; ++data.running; } void -JobQueue::finishJob (JobType type) +JobQueue::finishJob(JobType type) { assert(type != jtINVALID); - JobTypeData& data = getJobTypeData (type); + JobTypeData& data = getJobTypeData(type); // Queue a deferred task if possible if (data.deferred > 0) { - assert (data.running + data.waiting >= getJobLimit (type)); + assert(data.running + data.waiting >= getJobLimit(type)); --data.deferred; - m_workers.addTask (); + m_workers.addTask(); } --data.running; } void -JobQueue::processTask (int instance) +JobQueue::processTask(int instance) { JobType type; { using namespace std::chrono; - Job::clock_type::time_point const start_time ( - Job::clock_type::now()); + Job::clock_type::time_point const start_time(Job::clock_type::now()); { Job job; { - std::lock_guard lock (m_mutex); - getNextJob (job); + std::lock_guard lock(m_mutex); + getNextJob(job); ++m_processCount; } type = job.getType(); JobTypeData& data(getJobTypeData(type)); - JLOG(m_journal.trace()) << "Doing " << data.name () << "job"; + JLOG(m_journal.trace()) << "Doing " << data.name() << "job"; - //The amount of time that the job was in the queue - auto const q_time = date::ceil( - start_time - job.queue_time()); + // The amount of time that the job was in the queue + auto const q_time = + date::ceil(start_time - job.queue_time()); perfLog_.jobStart(type, q_time, start_time, instance); - - job.doJob (); - //The amount of time it took to execute the job - auto const x_time = date::ceil( - Job::clock_type::now() - start_time); + job.doJob(); + + // The amount of time it took to execute the job + auto const x_time = + date::ceil(Job::clock_type::now() - start_time); if (x_time >= 10ms || q_time >= 10ms) { @@ -440,18 +431,17 @@ JobQueue::processTask (int instance) } perfLog_.jobFinish(type, x_time, instance); } - } { - std::lock_guard lock (m_mutex); + std::lock_guard lock(m_mutex); // Job should be destroyed before calling checkStopped // otherwise destructors with side effects can access // parent objects that are already destroyed. - finishJob (type); - if(--m_processCount == 0 && m_jobSet.empty()) + finishJob(type); + if (--m_processCount == 0 && m_jobSet.empty()) cv_.notify_all(); - checkStopped (lock); + checkStopped(lock); } // Note that when Job::~Job is called, the last reference @@ -459,19 +449,19 @@ JobQueue::processTask (int instance) } int -JobQueue::getJobLimit (JobType type) +JobQueue::getJobLimit(JobType type) { - JobTypeInfo const& j (JobTypes::instance().get (type)); - assert (j.type () != jtINVALID); + JobTypeInfo const& j(JobTypes::instance().get(type)); + assert(j.type() != jtINVALID); - return j.limit (); + return j.limit(); } void -JobQueue::onChildrenStopped () +JobQueue::onChildrenStopped() { - std::lock_guard lock (m_mutex); - checkStopped (lock); + std::lock_guard lock(m_mutex); + checkStopped(lock); } -} +} // namespace ripple diff --git a/src/ripple/core/impl/LoadEvent.cpp b/src/ripple/core/impl/LoadEvent.cpp index 74b69e5269..38617e7f54 100644 --- a/src/ripple/core/impl/LoadEvent.cpp +++ b/src/ripple/core/impl/LoadEvent.cpp @@ -24,26 +24,27 @@ namespace ripple { -LoadEvent::LoadEvent ( - LoadMonitor& monitor, - std::string const& name, - bool shouldStart) - : monitor_ (monitor) - , running_ (shouldStart) - , name_ (name) - , mark_ { std::chrono::steady_clock::now() } - , timeWaiting_ {} - , timeRunning_ {} +LoadEvent::LoadEvent( + LoadMonitor& monitor, + std::string const& name, + bool shouldStart) + : monitor_(monitor) + , running_(shouldStart) + , name_(name) + , mark_{std::chrono::steady_clock::now()} + , timeWaiting_{} + , timeRunning_{} { } -LoadEvent::~LoadEvent () +LoadEvent::~LoadEvent() { if (running_) - stop (); + stop(); } -std::string const& LoadEvent::name () const +std::string const& +LoadEvent::name() const { return name_; } @@ -60,12 +61,14 @@ LoadEvent::runTime() const return timeRunning_; } -void LoadEvent::setName (std::string const& name) +void +LoadEvent::setName(std::string const& name) { name_ = name; } -void LoadEvent::start () +void +LoadEvent::start() { auto const now = std::chrono::steady_clock::now(); @@ -77,9 +80,10 @@ void LoadEvent::start () running_ = true; } -void LoadEvent::stop () +void +LoadEvent::stop() { - assert (running_); + assert(running_); auto const now = std::chrono::steady_clock::now(); @@ -87,7 +91,7 @@ void LoadEvent::stop () mark_ = now; running_ = false; - monitor_.addLoadSample (*this); + monitor_.addLoadSample(*this); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/LoadMonitor.cpp b/src/ripple/core/impl/LoadMonitor.cpp index f265fd04b8..d4173c8985 100644 --- a/src/ripple/core/impl/LoadMonitor.cpp +++ b/src/ripple/core/impl/LoadMonitor.cpp @@ -37,24 +37,21 @@ TODO //------------------------------------------------------------------------------ LoadMonitor::Stats::Stats() - : count (0) - , latencyAvg (0) - , latencyPeak (0) - , isOverloaded (false) + : count(0), latencyAvg(0), latencyPeak(0), isOverloaded(false) { } //------------------------------------------------------------------------------ -LoadMonitor::LoadMonitor (beast::Journal j) - : mCounts (0) - , mLatencyEvents (0) - , mLatencyMSAvg (0) - , mLatencyMSPeak (0) - , mTargetLatencyAvg (0) - , mTargetLatencyPk (0) - , mLastUpdate (UptimeClock::now()) - , j_ (j) +LoadMonitor::LoadMonitor(beast::Journal j) + : mCounts(0) + , mLatencyEvents(0) + , mLatencyMSAvg(0) + , mLatencyMSPeak(0) + , mTargetLatencyAvg(0) + , mTargetLatencyPk(0) + , mLastUpdate(UptimeClock::now()) + , j_(j) { } @@ -64,11 +61,12 @@ LoadMonitor::LoadMonitor (beast::Journal j) // It's not clear exactly which data needs to be protected. // // call with the mutex -void LoadMonitor::update () +void +LoadMonitor::update() { using namespace std::chrono_literals; auto now = UptimeClock::now(); - if (now == mLastUpdate) // current + if (now == mLastUpdate) // current return; // VFALCO TODO Why 8? @@ -99,11 +97,11 @@ void LoadMonitor::update () mLatencyEvents -= ((mLatencyEvents + 3) / 4); mLatencyMSAvg -= (mLatencyMSAvg / 4); mLatencyMSPeak -= (mLatencyMSPeak / 4); - } - while (mLastUpdate < now); + } while (mLastUpdate < now); } -void LoadMonitor::addLoadSample (LoadEvent const& s) +void +LoadMonitor::addLoadSample(LoadEvent const& s) { using namespace std::chrono; @@ -114,24 +112,26 @@ void LoadMonitor::addLoadSample (LoadEvent const& s) if (latency > 500ms) { auto mj = (latency > 1s) ? j_.warn() : j_.info(); - JLOG (mj) << "Job: " << s.name() << - " run: " << date::round(s.runTime()).count() << - "ms" << " wait: " << - date::round(s.waitTime()).count() << "ms"; + JLOG(mj) << "Job: " << s.name() + << " run: " << date::round(s.runTime()).count() + << "ms" + << " wait: " << date::round(s.waitTime()).count() + << "ms"; } - addSamples (1, latency); + addSamples(1, latency); } /* Add multiple samples @param count The number of samples to add @param latencyMS The total number of milliseconds */ -void LoadMonitor::addSamples (int count, std::chrono::milliseconds latency) +void +LoadMonitor::addSamples(int count, std::chrono::milliseconds latency) { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); - update (); + update(); mCounts += count; mLatencyEvents += count; mLatencyMSAvg += latency; @@ -143,41 +143,49 @@ void LoadMonitor::addSamples (int count, std::chrono::milliseconds latency) mLatencyMSPeak = latencyPeak; } -void LoadMonitor::setTargetLatency (std::chrono::milliseconds avg, - std::chrono::milliseconds pk) +void +LoadMonitor::setTargetLatency( + std::chrono::milliseconds avg, + std::chrono::milliseconds pk) { - mTargetLatencyAvg = avg; + mTargetLatencyAvg = avg; mTargetLatencyPk = pk; } -bool LoadMonitor::isOverTarget (std::chrono::milliseconds avg, - std::chrono::milliseconds peak) +bool +LoadMonitor::isOverTarget( + std::chrono::milliseconds avg, + std::chrono::milliseconds peak) { using namespace std::chrono_literals; return (mTargetLatencyPk > 0ms && (peak > mTargetLatencyPk)) || - (mTargetLatencyAvg > 0ms && (avg > mTargetLatencyAvg)); + (mTargetLatencyAvg > 0ms && (avg > mTargetLatencyAvg)); } -bool LoadMonitor::isOver () +bool +LoadMonitor::isOver() { - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); - update (); + update(); if (mLatencyEvents == 0) return 0; - return isOverTarget (mLatencyMSAvg / (mLatencyEvents * 4), mLatencyMSPeak / (mLatencyEvents * 4)); + return isOverTarget( + mLatencyMSAvg / (mLatencyEvents * 4), + mLatencyMSPeak / (mLatencyEvents * 4)); } -LoadMonitor::Stats LoadMonitor::getStats () +LoadMonitor::Stats +LoadMonitor::getStats() { using namespace std::chrono_literals; Stats stats; - std::lock_guard sl (mutex_); + std::lock_guard sl(mutex_); - update (); + update(); stats.count = mCounts / 4; @@ -192,9 +200,9 @@ LoadMonitor::Stats LoadMonitor::getStats () stats.latencyPeak = mLatencyMSPeak / (mLatencyEvents * 4); } - stats.isOverloaded = isOverTarget (stats.latencyAvg, stats.latencyPeak); + stats.isOverloaded = isOverTarget(stats.latencyAvg, stats.latencyPeak); return stats; } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/SNTPClock.cpp b/src/ripple/core/impl/SNTPClock.cpp index 7ea904d8ca..e2b17b14ab 100644 --- a/src/ripple/core/impl/SNTPClock.cpp +++ b/src/ripple/core/impl/SNTPClock.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include #include #include #include +#include #include #include #include @@ -32,18 +32,19 @@ namespace ripple { -static uint8_t SNTPQueryData[48] = -{ 0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +static uint8_t SNTPQueryData[48] = { + 0x1B, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; using namespace std::chrono_literals; // NTP query frequency - 4 minutes auto constexpr NTP_QUERY_FREQUENCY = 4min; // NTP minimum interval to query same servers - 3 minutes -auto constexpr NTP_MIN_QUERY = 3min; +auto constexpr NTP_MIN_QUERY = 3min; // NTP sample window (should be odd) -#define NTP_SAMPLE_WINDOW 9 +#define NTP_SAMPLE_WINDOW 9 // NTP timestamp constant auto constexpr NTP_UNIX_OFFSET = 0x83AA7E80s; @@ -52,25 +53,24 @@ auto constexpr NTP_UNIX_OFFSET = 0x83AA7E80s; auto constexpr NTP_TIMESTAMP_VALID = (NTP_QUERY_FREQUENCY + NTP_MIN_QUERY) * 2; // SNTP packet offsets -#define NTP_OFF_INFO 0 -#define NTP_OFF_ROOTDELAY 1 -#define NTP_OFF_ROOTDISP 2 -#define NTP_OFF_REFERENCEID 3 -#define NTP_OFF_REFTS_INT 4 -#define NTP_OFF_REFTS_FRAC 5 -#define NTP_OFF_ORGTS_INT 6 -#define NTP_OFF_ORGTS_FRAC 7 -#define NTP_OFF_RECVTS_INT 8 -#define NTP_OFF_RECVTS_FRAC 9 -#define NTP_OFF_XMITTS_INT 10 -#define NTP_OFF_XMITTS_FRAC 11 +#define NTP_OFF_INFO 0 +#define NTP_OFF_ROOTDELAY 1 +#define NTP_OFF_ROOTDISP 2 +#define NTP_OFF_REFERENCEID 3 +#define NTP_OFF_REFTS_INT 4 +#define NTP_OFF_REFTS_FRAC 5 +#define NTP_OFF_ORGTS_INT 6 +#define NTP_OFF_ORGTS_FRAC 7 +#define NTP_OFF_RECVTS_INT 8 +#define NTP_OFF_RECVTS_FRAC 9 +#define NTP_OFF_XMITTS_INT 10 +#define NTP_OFF_XMITTS_FRAC 11 -class SNTPClientImp - : public SNTPClock +class SNTPClientImp : public SNTPClock { private: template - using sys_time = std::chrono::time_point; + using sys_time = std::chrono::time_point; using sys_seconds = sys_time; @@ -80,9 +80,8 @@ private: sys_seconds sent; std::uint32_t nonce; - explicit Query (sys_seconds j = sys_seconds::max()) - : replied (false) - , sent (j) + explicit Query(sys_seconds j = sys_seconds::max()) + : replied(false), sent(j) { } }; @@ -91,10 +90,9 @@ private: std::mutex mutable mutex_; std::thread thread_; boost::asio::io_service io_service_; - boost::optional< - boost::asio::io_service::work> work_; + boost::optional work_; - std::map queries_; + std::map queries_; boost::asio::ip::udp::socket socket_; boost::asio::basic_waitable_timer timer_; boost::asio::ip::udp::resolver resolver_; @@ -108,20 +106,19 @@ private: public: using error_code = boost::system::error_code; - explicit - SNTPClientImp (beast::Journal j) - : j_ (j) + explicit SNTPClientImp(beast::Journal j) + : j_(j) , work_(io_service_) - , socket_ (io_service_) - , timer_ (io_service_) - , resolver_ (io_service_) - , offset_ (0) - , lastUpdate_ (sys_seconds::max()) - , buf_ (256) + , socket_(io_service_) + , timer_(io_service_) + , resolver_(io_service_) + , offset_(0) + , lastUpdate_(sys_seconds::max()) + , buf_(256) { } - ~SNTPClientImp () override + ~SNTPClientImp() override { if (thread_.joinable()) { @@ -136,37 +133,37 @@ public: //-------------------------------------------------------------------------- void - run (const std::vector& servers) override + run(const std::vector& servers) override { - std::vector::const_iterator it = servers.begin (); + std::vector::const_iterator it = servers.begin(); - if (it == servers.end ()) + if (it == servers.end()) { - JLOG(j_.info()) << - "SNTP: no server specified"; + JLOG(j_.info()) << "SNTP: no server specified"; return; } { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); for (auto const& item : servers) - servers_.emplace_back( - item, sys_seconds::max()); + servers_.emplace_back(item, sys_seconds::max()); } queryAll(); using namespace boost::asio; - socket_.open (ip::udp::v4 ()); - socket_.bind (ep_); - socket_.async_receive_from (buffer (buf_, 256), - ep_, std::bind( - &SNTPClientImp::onRead, this, - std::placeholders::_1, - std::placeholders::_2)); + socket_.open(ip::udp::v4()); + socket_.bind(ep_); + socket_.async_receive_from( + buffer(buf_, 256), + ep_, + std::bind( + &SNTPClientImp::onRead, + this, + std::placeholders::_1, + std::placeholders::_2)); timer_.expires_from_now(NTP_QUERY_FREQUENCY); - timer_.async_wait(std::bind( - &SNTPClientImp::onTimer, this, - std::placeholders::_1)); + timer_.async_wait( + std::bind(&SNTPClientImp::onTimer, this, std::placeholders::_1)); thread_ = std::thread(&SNTPClientImp::doRun, this); } @@ -174,12 +171,12 @@ public: time_point now() const override { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); using namespace std::chrono; auto const when = time_point_cast(clock_type::now()); if ((lastUpdate_ == sys_seconds::max()) || - ((lastUpdate_ + NTP_TIMESTAMP_VALID) < - time_point_cast(clock_type::now()))) + ((lastUpdate_ + NTP_TIMESTAMP_VALID) < + time_point_cast(clock_type::now()))) return when; return when + offset_; } @@ -187,40 +184,39 @@ public: duration offset() const override { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); return offset_; } //-------------------------------------------------------------------------- - void doRun () + void + doRun() { beast::setCurrentThreadName("rippled: SNTPClock"); io_service_.run(); } void - onTimer (error_code const& ec) + onTimer(error_code const& ec) { using namespace boost::asio; if (ec == error::operation_aborted) return; if (ec) { - JLOG(j_.error()) << - "SNTPClock::onTimer: " << ec.message(); + JLOG(j_.error()) << "SNTPClock::onTimer: " << ec.message(); return; } - doQuery (); + doQuery(); timer_.expires_from_now(NTP_QUERY_FREQUENCY); - timer_.async_wait(std::bind( - &SNTPClientImp::onTimer, this, - std::placeholders::_1)); + timer_.async_wait( + std::bind(&SNTPClientImp::onTimer, this, std::placeholders::_1)); } void - onRead (error_code const& ec, std::size_t bytes_xferd) + onRead(error_code const& ec, std::size_t bytes_xferd) { using namespace boost::asio; using namespace std::chrono; @@ -233,114 +229,120 @@ public: return; */ - if (! ec) + if (!ec) { - JLOG(j_.trace()) << - "SNTP: Packet from " << ep_; - std::lock_guard lock (mutex_); - auto const query = queries_.find (ep_); - if (query == queries_.end ()) + JLOG(j_.trace()) << "SNTP: Packet from " << ep_; + std::lock_guard lock(mutex_); + auto const query = queries_.find(ep_); + if (query == queries_.end()) { - JLOG(j_.debug()) << - "SNTP: Reply from " << ep_ << " found without matching query"; + JLOG(j_.debug()) << "SNTP: Reply from " << ep_ + << " found without matching query"; } else if (query->second.replied) { - JLOG(j_.debug()) << - "SNTP: Duplicate response from " << ep_; + JLOG(j_.debug()) << "SNTP: Duplicate response from " << ep_; } else { query->second.replied = true; - if (time_point_cast(clock_type::now()) > (query->second.sent + 1s)) + if (time_point_cast(clock_type::now()) > + (query->second.sent + 1s)) { - JLOG(j_.warn()) << - "SNTP: Late response from " << ep_; + JLOG(j_.warn()) << "SNTP: Late response from " << ep_; } else if (bytes_xferd < 48) { - JLOG(j_.warn()) << - "SNTP: Short reply from " << ep_ << - " (" << bytes_xferd << ") " << buf_.size (); + JLOG(j_.warn()) << "SNTP: Short reply from " << ep_ << " (" + << bytes_xferd << ") " << buf_.size(); } - else if (reinterpret_cast( - &buf_[0])[NTP_OFF_ORGTS_FRAC] != - query->second.nonce) + else if ( + reinterpret_cast( + &buf_[0])[NTP_OFF_ORGTS_FRAC] != query->second.nonce) { - JLOG(j_.warn()) << - "SNTP: Reply from " << ep_ << "had wrong nonce"; + JLOG(j_.warn()) + << "SNTP: Reply from " << ep_ << "had wrong nonce"; } else { - processReply (); + processReply(); } } } - socket_.async_receive_from(buffer(buf_, 256), - ep_, std::bind(&SNTPClientImp::onRead, this, + socket_.async_receive_from( + buffer(buf_, 256), + ep_, + std::bind( + &SNTPClientImp::onRead, + this, std::placeholders::_1, - std::placeholders::_2)); + std::placeholders::_2)); } //-------------------------------------------------------------------------- - void addServer (std::string const& server) + void + addServer(std::string const& server) { - std::lock_guard lock (mutex_); - servers_.push_back (std::make_pair (server, sys_seconds::max())); + std::lock_guard lock(mutex_); + servers_.push_back(std::make_pair(server, sys_seconds::max())); } - void queryAll () + void + queryAll() { - while (doQuery ()) + while (doQuery()) { } } - bool doQuery () + bool + doQuery() { - std::lock_guard lock (mutex_); - auto best = servers_.end (); + std::lock_guard lock(mutex_); + auto best = servers_.end(); - for (auto iter = servers_.begin (), end = best; - iter != end; ++iter) + for (auto iter = servers_.begin(), end = best; iter != end; ++iter) if ((best == end) || (iter->second == sys_seconds::max()) || - (iter->second < best->second)) + (iter->second < best->second)) best = iter; - if (best == servers_.end ()) + if (best == servers_.end()) { - JLOG(j_.trace()) << - "SNTP: No server to query"; + JLOG(j_.trace()) << "SNTP: No server to query"; return false; } using namespace std::chrono; auto now = time_point_cast(clock_type::now()); - if ((best->second != sys_seconds::max()) && ((best->second + NTP_MIN_QUERY) >= now)) + if ((best->second != sys_seconds::max()) && + ((best->second + NTP_MIN_QUERY) >= now)) { - JLOG(j_.trace()) << - "SNTP: All servers recently queried"; + JLOG(j_.trace()) << "SNTP: All servers recently queried"; return false; } best->second = now; boost::asio::ip::udp::resolver::query query( - boost::asio::ip::udp::v4 (), best->first, "ntp"); - resolver_.async_resolve (query, std::bind ( - &SNTPClientImp::resolveComplete, this, + boost::asio::ip::udp::v4(), best->first, "ntp"); + resolver_.async_resolve( + query, + std::bind( + &SNTPClientImp::resolveComplete, + this, std::placeholders::_1, - std::placeholders::_2)); - JLOG(j_.trace()) << - "SNTPClock: Resolve pending for " << best->first; + std::placeholders::_2)); + JLOG(j_.trace()) << "SNTPClock: Resolve pending for " << best->first; return true; } - void resolveComplete (error_code const& ec, + void + resolveComplete( + error_code const& ec, boost::asio::ip::udp::resolver::iterator it) { using namespace boost::asio; @@ -348,87 +350,97 @@ public: return; if (ec) { - JLOG(j_.trace()) << - "SNTPClock::resolveComplete: " << ec.message(); + JLOG(j_.trace()) << "SNTPClock::resolveComplete: " << ec.message(); return; } - assert (it != ip::udp::resolver::iterator()); + assert(it != ip::udp::resolver::iterator()); auto sel = it; int i = 1; while (++it != ip::udp::resolver::iterator()) { - if (rand_int (i++) == 0) + if (rand_int(i++) == 0) sel = it; } - if (sel != ip::udp::resolver::iterator ()) + if (sel != ip::udp::resolver::iterator()) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); Query& query = queries_[*sel]; using namespace std::chrono; auto now = time_point_cast(clock_type::now()); if ((query.sent == now) || ((query.sent + 1s) == now)) { - // This can happen if the same IP address is reached through multiple names - JLOG(j_.trace()) << - "SNTP: Redundant query suppressed"; + // This can happen if the same IP address is reached through + // multiple names + JLOG(j_.trace()) << "SNTP: Redundant query suppressed"; return; } query.replied = false; query.sent = now; query.nonce = rand_int(); - // The following line of code will overflow at 2036-02-07 06:28:16 UTC + // The following line of code will overflow at 2036-02-07 06:28:16 + // UTC // due to the 32 bit cast. - reinterpret_cast (SNTPQueryData)[NTP_OFF_XMITTS_INT] = - static_cast((time_point_cast(clock_type::now()) + - NTP_UNIX_OFFSET).time_since_epoch().count()); - reinterpret_cast (SNTPQueryData)[NTP_OFF_XMITTS_FRAC] = query.nonce; - socket_.async_send_to(buffer(SNTPQueryData, 48), - *sel, std::bind (&SNTPClientImp::onSend, this, + reinterpret_cast( + SNTPQueryData)[NTP_OFF_XMITTS_INT] = + static_cast( + (time_point_cast(clock_type::now()) + + NTP_UNIX_OFFSET) + .time_since_epoch() + .count()); + reinterpret_cast( + SNTPQueryData)[NTP_OFF_XMITTS_FRAC] = query.nonce; + socket_.async_send_to( + buffer(SNTPQueryData, 48), + *sel, + std::bind( + &SNTPClientImp::onSend, + this, std::placeholders::_1, - std::placeholders::_2)); + std::placeholders::_2)); } } - void onSend (error_code const& ec, std::size_t) + void + onSend(error_code const& ec, std::size_t) { if (ec == boost::asio::error::operation_aborted) return; if (ec) { - JLOG(j_.warn()) << - "SNTPClock::onSend: " << ec.message(); + JLOG(j_.warn()) << "SNTPClock::onSend: " << ec.message(); return; } } - void processReply () + void + processReply() { using namespace std::chrono; - assert (buf_.size () >= 48); - std::uint32_t* recvBuffer = reinterpret_cast (&buf_.front ()); + assert(buf_.size() >= 48); + std::uint32_t* recvBuffer = + reinterpret_cast(&buf_.front()); - unsigned info = ntohl (recvBuffer[NTP_OFF_INFO]); + unsigned info = ntohl(recvBuffer[NTP_OFF_INFO]); auto timev = seconds{ntohl(recvBuffer[NTP_OFF_RECVTS_INT])}; unsigned stratum = (info >> 16) & 0xff; if ((info >> 30) == 3) { - JLOG(j_.info()) << - "SNTP: Alarm condition " << ep_; + JLOG(j_.info()) << "SNTP: Alarm condition " << ep_; return; } if ((stratum == 0) || (stratum > 14)) { - JLOG(j_.info()) << - "SNTP: Unreasonable stratum (" << stratum << ") from " << ep_; + JLOG(j_.info()) << "SNTP: Unreasonable stratum (" << stratum + << ") from " << ep_; return; } @@ -438,18 +450,18 @@ public: timev -= NTP_UNIX_OFFSET; // add offset to list, replacing oldest one if appropriate - offsets_.push_back (timev); + offsets_.push_back(timev); - if (offsets_.size () >= NTP_SAMPLE_WINDOW) - offsets_.pop_front (); + if (offsets_.size() >= NTP_SAMPLE_WINDOW) + offsets_.pop_front(); lastUpdate_ = now; // select median time auto offsetList = offsets_; std::sort(offsetList.begin(), offsetList.end()); - auto j = offsetList.size (); - auto it = std::next(offsetList.begin (), j/2); + auto j = offsetList.size(); + auto it = std::next(offsetList.begin(), j / 2); offset_ = *it; if ((j % 2) == 0) @@ -462,8 +474,8 @@ public: if (timev != 0s || offset_ != 0s) { - JLOG(j_.trace()) << "SNTP: Offset is " << timev.count() << - ", new system offset is " << offset_.count(); + JLOG(j_.trace()) << "SNTP: Offset is " << timev.count() + << ", new system offset is " << offset_.count(); } } }; @@ -471,9 +483,9 @@ public: //------------------------------------------------------------------------------ std::unique_ptr -make_SNTPClock (beast::Journal j) +make_SNTPClock(beast::Journal j) { return std::make_unique(j); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/SNTPClock.h b/src/ripple/core/impl/SNTPClock.h index 35cd617566..b63cac53da 100644 --- a/src/ripple/core/impl/SNTPClock.h +++ b/src/ripple/core/impl/SNTPClock.h @@ -30,24 +30,18 @@ namespace ripple { /** A clock based on system_clock and adjusted for SNTP. */ -class SNTPClock - : public beast::abstract_clock< - std::chrono::system_clock> +class SNTPClock : public beast::abstract_clock { public: - virtual - void - run (std::vector const& servers) = 0; + virtual void + run(std::vector const& servers) = 0; - virtual - duration + virtual duration offset() const = 0; }; -extern -std::unique_ptr -make_SNTPClock (beast::Journal); +extern std::unique_ptr make_SNTPClock(beast::Journal); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/core/impl/SociDB.cpp b/src/ripple/core/impl/SociDB.cpp index 71a5fce86b..c21b185e7d 100644 --- a/src/ripple/core/impl/SociDB.cpp +++ b/src/ripple/core/impl/SociDB.cpp @@ -22,15 +22,14 @@ #pragma clang diagnostic ignored "-Wdeprecated" #endif - -#include #include +#include +#include #include #include -#include +#include #include #include -#include namespace ripple { @@ -39,143 +38,152 @@ static auto checkpointPageCount = 1000; namespace detail { std::pair -getSociSqliteInit (std::string const& name, - std::string const& dir, - std::string const& ext) +getSociSqliteInit( + std::string const& name, + std::string const& dir, + std::string const& ext) { - if (name.empty ()) + if (name.empty()) { - Throw ( - "Sqlite databases must specify a dir and a name. Name: " + - name + " Dir: " + dir); + Throw( + "Sqlite databases must specify a dir and a name. Name: " + name + + " Dir: " + dir); } - boost::filesystem::path file (dir); - if (is_directory (file)) + boost::filesystem::path file(dir); + if (is_directory(file)) file /= name + ext; - return std::make_pair (file.string (), std::ref (soci::sqlite3)); + return std::make_pair(file.string(), std::ref(soci::sqlite3)); } std::pair -getSociInit (BasicConfig const& config, - std::string const& dbName) +getSociInit(BasicConfig const& config, std::string const& dbName) { - auto const& section = config.section ("sqdb"); + auto const& section = config.section("sqdb"); auto const backendName = get(section, "backend", "sqlite"); if (backendName != "sqlite") - Throw ("Unsupported soci backend: " + backendName); + Throw("Unsupported soci backend: " + backendName); - auto const path = config.legacy ("database_path"); - auto const ext = dbName == "validators" || dbName == "peerfinder" - ? ".sqlite" : ".db"; + auto const path = config.legacy("database_path"); + auto const ext = + dbName == "validators" || dbName == "peerfinder" ? ".sqlite" : ".db"; return detail::getSociSqliteInit(dbName, path, ext); } -} // detail +} // namespace detail -SociConfig::SociConfig ( +SociConfig::SociConfig( std::pair init) - : connectionString_ (std::move (init.first)), - backendFactory_ (init.second) + : connectionString_(std::move(init.first)), backendFactory_(init.second) { } -SociConfig::SociConfig (BasicConfig const& config, std::string const& dbName) - : SociConfig (detail::getSociInit (config, dbName)) +SociConfig::SociConfig(BasicConfig const& config, std::string const& dbName) + : SociConfig(detail::getSociInit(config, dbName)) { } -std::string SociConfig::connectionString () const +std::string +SociConfig::connectionString() const { return connectionString_; } -void SociConfig::open (soci::session& s) const +void +SociConfig::open(soci::session& s) const { - s.open (backendFactory_, connectionString ()); + s.open(backendFactory_, connectionString()); } -void open (soci::session& s, - BasicConfig const& config, - std::string const& dbName) +void +open(soci::session& s, BasicConfig const& config, std::string const& dbName) { - SociConfig (config, dbName).open(s); + SociConfig(config, dbName).open(s); } -void open (soci::session& s, - std::string const& beName, - std::string const& connectionString) +void +open( + soci::session& s, + std::string const& beName, + std::string const& connectionString) { if (beName == "sqlite") s.open(soci::sqlite3, connectionString); else - Throw ("Unsupported soci backend: " + beName); + Throw("Unsupported soci backend: " + beName); } -static -sqlite_api::sqlite3* getConnection (soci::session& s) +static sqlite_api::sqlite3* +getConnection(soci::session& s) { sqlite_api::sqlite3* result = nullptr; - auto be = s.get_backend (); - if (auto b = dynamic_cast (be)) + auto be = s.get_backend(); + if (auto b = dynamic_cast(be)) result = b->conn_; - if (! result) - Throw ("Didn't get a database connection."); + if (!result) + Throw("Didn't get a database connection."); return result; } -size_t getKBUsedAll (soci::session& s) +size_t +getKBUsedAll(soci::session& s) { - if (! getConnection (s)) - Throw ("No connection found."); - return static_cast (sqlite_api::sqlite3_memory_used () / kilobytes(1)); + if (!getConnection(s)) + Throw("No connection found."); + return static_cast( + sqlite_api::sqlite3_memory_used() / kilobytes(1)); } -size_t getKBUsedDB (soci::session& s) +size_t +getKBUsedDB(soci::session& s) { // This function will have to be customized when other backends are added - if (auto conn = getConnection (s)) + if (auto conn = getConnection(s)) { int cur = 0, hiw = 0; - sqlite_api::sqlite3_db_status ( + sqlite_api::sqlite3_db_status( conn, SQLITE_DBSTATUS_CACHE_USED, &cur, &hiw, 0); return cur / kilobytes(1); } - Throw (""); - return 0; // Silence compiler warning. + Throw(""); + return 0; // Silence compiler warning. } -void convert (soci::blob& from, std::vector& to) +void +convert(soci::blob& from, std::vector& to) { - to.resize (from.get_len ()); - if (to.empty ()) + to.resize(from.get_len()); + if (to.empty()) return; - from.read (0, reinterpret_cast(&to[0]), from.get_len ()); + from.read(0, reinterpret_cast(&to[0]), from.get_len()); } -void convert (soci::blob& from, std::string& to) +void +convert(soci::blob& from, std::string& to) { std::vector tmp; - convert (from, tmp); - to.assign (tmp.begin (), tmp.end()); + convert(from, tmp); + to.assign(tmp.begin(), tmp.end()); } -void convert (std::vector const& from, soci::blob& to) +void +convert(std::vector const& from, soci::blob& to) { - if (!from.empty ()) - to.write (0, reinterpret_cast(&from[0]), from.size ()); + if (!from.empty()) + to.write(0, reinterpret_cast(&from[0]), from.size()); else - to.trim (0); + to.trim(0); } -void convert (std::string const& from, soci::blob& to) +void +convert(std::string const& from, soci::blob& to) { - if (!from.empty ()) - to.write (0, from.data (), from.size ()); + if (!from.empty()) + to.write(0, from.data(), from.size()); else - to.trim (0); + to.trim(0); } namespace { @@ -191,13 +199,13 @@ namespace { class WALCheckpointer : public Checkpointer { public: - WALCheckpointer (sqlite_api::sqlite3& conn, JobQueue& q, Logs& logs) - : conn_ (conn), jobQueue_ (q), j_ (logs.journal ("WALCheckpointer")) + WALCheckpointer(sqlite_api::sqlite3& conn, JobQueue& q, Logs& logs) + : conn_(conn), jobQueue_(q), j_(logs.journal("WALCheckpointer")) { - sqlite_api::sqlite3_wal_hook (&conn_, &sqliteWALHook, this); + sqlite_api::sqlite3_wal_hook(&conn_, &sqliteWALHook, this); } - ~WALCheckpointer () override = default; + ~WALCheckpointer() override = default; private: sqlite_api::sqlite3& conn_; @@ -207,74 +215,76 @@ private: bool running_ = false; beast::Journal const j_; - static - int sqliteWALHook ( - void* cp, sqlite_api::sqlite3*, const char* dbName, int walSize) + static int + sqliteWALHook( + void* cp, + sqlite_api::sqlite3*, + const char* dbName, + int walSize) { if (walSize >= checkpointPageCount) { - if (auto checkpointer = reinterpret_cast (cp)) + if (auto checkpointer = reinterpret_cast(cp)) checkpointer->scheduleCheckpoint(); else - Throw ("Didn't get a WALCheckpointer"); + Throw("Didn't get a WALCheckpointer"); } return SQLITE_OK; } - void scheduleCheckpoint () + void + scheduleCheckpoint() { { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); if (running_) return; running_ = true; } // If the Job is not added to the JobQueue then we're not running_. - if (! jobQueue_.addJob ( - jtWAL, "WAL", [this] (Job&) { checkpoint(); })) + if (!jobQueue_.addJob(jtWAL, "WAL", [this](Job&) { checkpoint(); })) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); running_ = false; } } - void checkpoint () + void + checkpoint() { int log = 0, ckpt = 0; - int ret = sqlite3_wal_checkpoint_v2 ( + int ret = sqlite3_wal_checkpoint_v2( &conn_, nullptr, SQLITE_CHECKPOINT_PASSIVE, &log, &ckpt); - auto fname = sqlite3_db_filename (&conn_, "main"); + auto fname = sqlite3_db_filename(&conn_, "main"); if (ret != SQLITE_OK) { auto jm = (ret == SQLITE_LOCKED) ? j_.trace() : j_.warn(); - JLOG (jm) - << "WAL(" << fname << "): error " << ret; + JLOG(jm) << "WAL(" << fname << "): error " << ret; } else { - JLOG (j_.trace()) - << "WAL(" << fname << "): frames=" - << log << ", written=" << ckpt; + JLOG(j_.trace()) << "WAL(" << fname << "): frames=" << log + << ", written=" << ckpt; } - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); running_ = false; } }; -} // namespace +} // namespace -std::unique_ptr makeCheckpointer ( - soci::session& session, JobQueue& queue, Logs& logs) +std::unique_ptr +makeCheckpointer(soci::session& session, JobQueue& queue, Logs& logs) { - if (auto conn = getConnection (session)) - return std::make_unique (*conn, queue, logs); + if (auto conn = getConnection(session)) + return std::make_unique(*conn, queue, logs); return {}; } -} +} // namespace ripple #if defined(__clang__) #pragma clang diagnostic pop diff --git a/src/ripple/core/impl/Stoppable.cpp b/src/ripple/core/impl/Stoppable.cpp index 3816873767..f76317351c 100644 --- a/src/ripple/core/impl/Stoppable.cpp +++ b/src/ripple/core/impl/Stoppable.cpp @@ -24,26 +24,23 @@ namespace ripple { -Stoppable::Stoppable (std::string name, RootStoppable& root) - : m_name (std::move (name)) - , m_root (root) - , m_child (this) +Stoppable::Stoppable(std::string name, RootStoppable& root) + : m_name(std::move(name)), m_root(root), m_child(this) { } -Stoppable::Stoppable (std::string name, Stoppable& parent) - : m_name (std::move (name)) - , m_root (parent.m_root) - , m_child (this) +Stoppable::Stoppable(std::string name, Stoppable& parent) + : m_name(std::move(name)), m_root(parent.m_root), m_child(this) { setParent(parent); } -Stoppable::~Stoppable () +Stoppable::~Stoppable() { } -void Stoppable::setParent (Stoppable& parent) +void +Stoppable::setParent(Stoppable& parent) { assert(!hasParent_); assert(!parent.isStopping()); @@ -53,142 +50,163 @@ void Stoppable::setParent (Stoppable& parent) hasParent_ = true; } -bool Stoppable::isStopping() const +bool +Stoppable::isStopping() const { return m_root.isStopping(); } -bool Stoppable::isStopped () const +bool +Stoppable::isStopped() const { return m_stopped; } -bool Stoppable::areChildrenStopped () const +bool +Stoppable::areChildrenStopped() const { return m_childrenStopped; } -void Stoppable::stopped () +void +Stoppable::stopped() { std::lock_guard lk{m_mut}; m_is_stopping = true; m_cv.notify_all(); } -void Stoppable::onPrepare () +void +Stoppable::onPrepare() { } -void Stoppable::onStart () +void +Stoppable::onStart() { } -void Stoppable::onStop () +void +Stoppable::onStop() { stopped(); } -void Stoppable::onChildrenStopped () +void +Stoppable::onChildrenStopped() { } //------------------------------------------------------------------------------ -void Stoppable::prepareRecursive () +void +Stoppable::prepareRecursive() { - for (Children::const_iterator iter (m_children.cbegin ()); - iter != m_children.cend(); ++iter) - iter->stoppable->prepareRecursive (); - onPrepare (); + for (Children::const_iterator iter(m_children.cbegin()); + iter != m_children.cend(); + ++iter) + iter->stoppable->prepareRecursive(); + onPrepare(); } -void Stoppable::startRecursive () +void +Stoppable::startRecursive() { - onStart (); - for (Children::const_iterator iter (m_children.cbegin ()); - iter != m_children.cend(); ++iter) - iter->stoppable->startRecursive (); + onStart(); + for (Children::const_iterator iter(m_children.cbegin()); + iter != m_children.cend(); + ++iter) + iter->stoppable->startRecursive(); } -void Stoppable::stopAsyncRecursive (beast::Journal j) +void +Stoppable::stopAsyncRecursive(beast::Journal j) { - onStop (); + onStop(); - for (Children::const_iterator iter (m_children.cbegin ()); - iter != m_children.cend(); ++iter) + for (Children::const_iterator iter(m_children.cbegin()); + iter != m_children.cend(); + ++iter) iter->stoppable->stopAsyncRecursive(j); } -void Stoppable::stopRecursive (beast::Journal j) +void +Stoppable::stopRecursive(beast::Journal j) { // Block on each child from the bottom of the tree up. // - for (Children::const_iterator iter (m_children.cbegin ()); - iter != m_children.cend(); ++iter) - iter->stoppable->stopRecursive (j); + for (Children::const_iterator iter(m_children.cbegin()); + iter != m_children.cend(); + ++iter) + iter->stoppable->stopRecursive(j); // if we get here then all children have stopped // m_childrenStopped = true; - onChildrenStopped (); + onChildrenStopped(); // Now block on this Stoppable until m_is_stopping is set by stopped(). // using namespace std::chrono_literals; std::unique_lock lk{m_mut}; - if (!m_cv.wait_for(lk, 1s, [this]{return m_is_stopping;})) + if (!m_cv.wait_for(lk, 1s, [this] { return m_is_stopping; })) { if (auto stream = j.error()) stream << "Waiting for '" << m_name << "' to stop"; - m_cv.wait(lk, [this]{return m_is_stopping;}); + m_cv.wait(lk, [this] { return m_is_stopping; }); } m_stopped = true; } //------------------------------------------------------------------------------ -RootStoppable::RootStoppable (std::string name) - : Stoppable (std::move (name), *this) +RootStoppable::RootStoppable(std::string name) + : Stoppable(std::move(name), *this) { } -RootStoppable::~RootStoppable () +RootStoppable::~RootStoppable() { using namespace std::chrono_literals; jobCounter_.join(m_name.c_str(), 1s, debugLog()); } -bool RootStoppable::isStopping() const +bool +RootStoppable::isStopping() const { return m_calledStop; } -void RootStoppable::prepare () +void +RootStoppable::prepare() { - if (m_prepared.exchange (true) == false) - prepareRecursive (); + if (m_prepared.exchange(true) == false) + prepareRecursive(); } -void RootStoppable::start () +void +RootStoppable::start() { // Courtesy call to prepare. - if (m_prepared.exchange (true) == false) - prepareRecursive (); + if (m_prepared.exchange(true) == false) + prepareRecursive(); - if (m_started.exchange (true) == false) - startRecursive (); + if (m_started.exchange(true) == false) + startRecursive(); } -void RootStoppable::stop (beast::Journal j) +void +RootStoppable::stop(beast::Journal j) { // Must have a prior call to start() - assert (m_started); + assert(m_started); - if (stopAsync (j)) - stopRecursive (j); + if (stopAsync(j)) + stopRecursive(j); } -bool RootStoppable::stopAsync (beast::Journal j) +bool +RootStoppable::stopAsync(beast::Journal j) { bool alreadyCalled; { @@ -196,8 +214,8 @@ bool RootStoppable::stopAsync (beast::Journal j) // lock. This removes a small timing window that occurs if the // waiting thread is handling a spurious wakeup while m_calledStop // changes state. - std::unique_lock lock (m_); - alreadyCalled = m_calledStop.exchange (true); + std::unique_lock lock(m_); + alreadyCalled = m_calledStop.exchange(true); } if (alreadyCalled) { @@ -208,11 +226,11 @@ bool RootStoppable::stopAsync (beast::Journal j) // Wait until all in-flight JobQueue Jobs are completed. using namespace std::chrono_literals; - jobCounter_.join (m_name.c_str(), 1s, j); + jobCounter_.join(m_name.c_str(), 1s, j); c_.notify_all(); stopAsyncRecursive(j); return true; } -} +} // namespace ripple diff --git a/src/ripple/core/impl/TimeKeeper.cpp b/src/ripple/core/impl/TimeKeeper.cpp index 2e79c6e6a8..d1b07f53f4 100644 --- a/src/ripple/core/impl/TimeKeeper.cpp +++ b/src/ripple/core/impl/TimeKeeper.cpp @@ -34,28 +34,21 @@ private: std::unique_ptr clock_; // Adjust system_clock::time_point for NetClock epoch - static - time_point - adjust (std::chrono::system_clock::time_point when) + static time_point + adjust(std::chrono::system_clock::time_point when) { - return time_point( - std::chrono::duration_cast( - when.time_since_epoch() - - days(10957))); + return time_point(std::chrono::duration_cast( + when.time_since_epoch() - days(10957))); } public: - explicit - TimeKeeperImpl (beast::Journal j) - : j_ (j) - , closeOffset_ {} - , clock_ (make_SNTPClock(j)) + explicit TimeKeeperImpl(beast::Journal j) + : j_(j), closeOffset_{}, clock_(make_SNTPClock(j)) { } void - run (std::vector< - std::string> const& servers) override + run(std::vector const& servers) override { clock_->run(servers); } @@ -75,8 +68,7 @@ public: } void - adjustCloseTime( - std::chrono::duration amount) override + adjustCloseTime(std::chrono::duration amount) override { using namespace std::chrono; auto const s = amount.count(); @@ -91,17 +83,15 @@ public: closeOffset_ = (closeOffset_ * 3) / 4; if (closeOffset_.count() != 0) { - if (std::abs (closeOffset_.count()) < 60) + if (std::abs(closeOffset_.count()) < 60) { - JLOG(j_.info()) << - "TimeKeeper: Close time offset now " << - closeOffset_.count(); + JLOG(j_.info()) << "TimeKeeper: Close time offset now " + << closeOffset_.count(); } else { - JLOG(j_.warn()) << - "TimeKeeper: Large close time offset = " << - closeOffset_.count(); + JLOG(j_.warn()) << "TimeKeeper: Large close time offset = " + << closeOffset_.count(); } } } @@ -126,9 +116,9 @@ public: //------------------------------------------------------------------------------ std::unique_ptr -make_TimeKeeper (beast::Journal j) +make_TimeKeeper(beast::Journal j) { return std::make_unique(j); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/Workers.cpp b/src/ripple/core/impl/Workers.cpp index f04f94e4b8..c08a449ce7 100644 --- a/src/ripple/core/impl/Workers.cpp +++ b/src/ripple/core/impl/Workers.cpp @@ -17,39 +17,40 @@ */ //============================================================================== -#include #include #include +#include #include namespace ripple { -Workers::Workers ( +Workers::Workers( Callback& callback, perf::PerfLog* perfLog, std::string const& threadNames, int numberOfThreads) - : m_callback (callback) - , perfLog_ (perfLog) - , m_threadNames (threadNames) - , m_allPaused (true) - , m_semaphore (0) - , m_numberOfThreads (0) - , m_activeCount (0) - , m_pauseCount (0) - , m_runningTaskCount (0) + : m_callback(callback) + , perfLog_(perfLog) + , m_threadNames(threadNames) + , m_allPaused(true) + , m_semaphore(0) + , m_numberOfThreads(0) + , m_activeCount(0) + , m_pauseCount(0) + , m_runningTaskCount(0) { - setNumberOfThreads (numberOfThreads); + setNumberOfThreads(numberOfThreads); } -Workers::~Workers () +Workers::~Workers() { - pauseAllThreadsAndWait (); + pauseAllThreadsAndWait(); - deleteWorkers (m_everyone); + deleteWorkers(m_everyone); } -int Workers::getNumberOfThreads () const noexcept +int +Workers::getNumberOfThreads() const noexcept { return m_numberOfThreads; } @@ -58,9 +59,10 @@ int Workers::getNumberOfThreads () const noexcept // increase the number of threads, it could result in // more paused threads being created than expected. // -void Workers::setNumberOfThreads (int numberOfThreads) +void +Workers::setNumberOfThreads(int numberOfThreads) { - static int instance {0}; + static int instance{0}; if (m_numberOfThreads != numberOfThreads) { if (perfLog_) @@ -74,19 +76,19 @@ void Workers::setNumberOfThreads (int numberOfThreads) for (int i = 0; i < amount; ++i) { // See if we can reuse a paused worker - Worker* worker = m_paused.pop_front (); + Worker* worker = m_paused.pop_front(); if (worker != nullptr) { // If we got here then the worker thread is at [1] // This will unblock their call to wait() // - worker->notify (); + worker->notify(); } else { - worker = new Worker (*this, m_threadNames, instance++); - m_everyone.push_front (worker); + worker = new Worker(*this, m_threadNames, instance++); + m_everyone.push_front(worker); } } } @@ -100,7 +102,7 @@ void Workers::setNumberOfThreads (int numberOfThreads) ++m_pauseCount; // Pausing a thread counts as one "internal task" - m_semaphore.notify (); + m_semaphore.notify(); } } @@ -108,32 +110,36 @@ void Workers::setNumberOfThreads (int numberOfThreads) } } -void Workers::pauseAllThreadsAndWait () +void +Workers::pauseAllThreadsAndWait() { - setNumberOfThreads (0); + setNumberOfThreads(0); std::unique_lock lk{m_mut}; - m_cv.wait(lk, [this]{return m_allPaused;}); + m_cv.wait(lk, [this] { return m_allPaused; }); lk.unlock(); - assert (numberOfCurrentlyRunningTasks () == 0); + assert(numberOfCurrentlyRunningTasks() == 0); } -void Workers::addTask () +void +Workers::addTask() { - m_semaphore.notify (); + m_semaphore.notify(); } -int Workers::numberOfCurrentlyRunningTasks () const noexcept +int +Workers::numberOfCurrentlyRunningTasks() const noexcept { - return m_runningTaskCount.load (); + return m_runningTaskCount.load(); } -void Workers::deleteWorkers (beast::LockFreeStack & stack) +void +Workers::deleteWorkers(beast::LockFreeStack& stack) { for (;;) { - Worker* const worker = stack.pop_front (); + Worker* const worker = stack.pop_front(); if (worker != nullptr) { @@ -149,21 +155,23 @@ void Workers::deleteWorkers (beast::LockFreeStack & stack) //------------------------------------------------------------------------------ -Workers::Worker::Worker (Workers& workers, std::string const& threadName, +Workers::Worker::Worker( + Workers& workers, + std::string const& threadName, int const instance) - : m_workers {workers} - , threadName_ {threadName} - , instance_ {instance} - , wakeCount_ {0} - , shouldExit_ {false} + : m_workers{workers} + , threadName_{threadName} + , instance_{instance} + , wakeCount_{0} + , shouldExit_{false} { - thread_ = std::thread {&Workers::Worker::run, this}; + thread_ = std::thread{&Workers::Worker::run, this}; } -Workers::Worker::~Worker () +Workers::Worker::~Worker() { { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; ++wakeCount_; shouldExit_ = true; } @@ -172,14 +180,16 @@ Workers::Worker::~Worker () thread_.join(); } -void Workers::Worker::notify () +void +Workers::Worker::notify() { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; ++wakeCount_; wakeup_.notify_one(); } -void Workers::Worker::run () +void +Workers::Worker::run() { bool shouldExit = true; do @@ -196,16 +206,16 @@ void Workers::Worker::run () for (;;) { // Put the name back in case the callback changed it - beast::setCurrentThreadName (threadName_); + beast::setCurrentThreadName(threadName_); // Acquire a task or "internal task." // - m_workers.m_semaphore.wait (); + m_workers.m_semaphore.wait(); // See if there's a pause request. This // counts as an "internal task." // - int pauseCount = m_workers.m_pauseCount.load (); + int pauseCount = m_workers.m_pauseCount.load(); if (pauseCount > 0) { @@ -228,7 +238,7 @@ void Workers::Worker::run () // unblocked in order to process a task. // ++m_workers.m_runningTaskCount; - m_workers.m_callback.processTask (instance_); + m_workers.m_callback.processTask(instance_); --m_workers.m_runningTaskCount; } @@ -236,7 +246,7 @@ void Workers::Worker::run () // guarantee that it will eventually block on its // event object. // - m_workers.m_paused.push_front (this); + m_workers.m_paused.push_front(this); // Decrement the count of active workers, and if we // are the last one then signal the "all paused" event. @@ -249,7 +259,7 @@ void Workers::Worker::run () } // Set inactive thread name. - beast::setCurrentThreadName ("(" + threadName_ + ")"); + beast::setCurrentThreadName("(" + threadName_ + ")"); // [1] We will be here when the paused list is popped // @@ -258,13 +268,13 @@ void Workers::Worker::run () // // wakeup_ will get signaled by either Worker::notify() or ~Worker. { - std::unique_lock lock {mutex_}; - wakeup_.wait (lock, [this] {return this->wakeCount_ > 0;}); + std::unique_lock lock{mutex_}; + wakeup_.wait(lock, [this] { return this->wakeCount_ > 0; }); shouldExit = shouldExit_; --wakeCount_; } - } while (! shouldExit); + } while (!shouldExit); } -} // ripple +} // namespace ripple diff --git a/src/ripple/core/impl/Workers.h b/src/ripple/core/impl/Workers.h index 3a811ce899..f54093fba8 100644 --- a/src/ripple/core/impl/Workers.h +++ b/src/ripple/core/impl/Workers.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_CORE_WORKERS_H_INCLUDED #define RIPPLE_CORE_WORKERS_H_INCLUDED -#include #include +#include #include #include #include @@ -30,23 +30,23 @@ namespace ripple { -namespace perf -{ - class PerfLog; +namespace perf { +class PerfLog; } /** A group of threads that process tasks. -*/ + */ class Workers { public: /** Called to perform tasks as needed. */ struct Callback { - virtual ~Callback () = default; + virtual ~Callback() = default; Callback() = default; Callback(Callback const&) = delete; - Callback& operator=(Callback const&) = delete; + Callback& + operator=(Callback const&) = delete; /** Perform a task. @@ -58,7 +58,8 @@ public: @see Workers::addTask */ - virtual void processTask (int instance) = 0; + virtual void + processTask(int instance) = 0; }; /** Create the object. @@ -68,28 +69,31 @@ public: @param threadNames The name given to each created worker thread. */ - explicit Workers (Callback& callback, - perf::PerfLog* perfLog, - std::string const& threadNames = "Worker", - int numberOfThreads = - static_cast(std::thread::hardware_concurrency())); + explicit Workers( + Callback& callback, + perf::PerfLog* perfLog, + std::string const& threadNames = "Worker", + int numberOfThreads = + static_cast(std::thread::hardware_concurrency())); - ~Workers (); + ~Workers(); /** Retrieve the desired number of threads. This just returns the number of active threads that were requested. If - there was a recent call to setNumberOfThreads, the actual number of active - threads may be temporarily different from what was last requested. + there was a recent call to setNumberOfThreads, the actual number of + active threads may be temporarily different from what was last requested. @note This function is not thread-safe. */ - int getNumberOfThreads () const noexcept; + int + getNumberOfThreads() const noexcept; /** Set the desired number of threads. @note This function is not thread-safe. */ - void setNumberOfThreads (int numberOfThreads); + void + setNumberOfThreads(int numberOfThreads); /** Pause all threads and wait until they are paused. @@ -99,7 +103,8 @@ public: @note This function is not thread-safe. */ - void pauseAllThreadsAndWait (); + void + pauseAllThreadsAndWait(); /** Add a task to be performed. @@ -109,13 +114,15 @@ public: @note This function is thread-safe. */ - void addTask (); + void + addTask(); /** Get the number of currently executing calls of Callback::processTask. While this function is thread-safe, the value may not stay accurate for very long. It's mainly for diagnostic purposes. */ - int numberOfCurrentlyRunningTasks () const noexcept; + int + numberOfCurrentlyRunningTasks() const noexcept; //-------------------------------------------------------------------------- @@ -133,21 +140,23 @@ private: Idle: Active, but blocked on waiting for a task. Paused: Blocked waiting to exit or become active. */ - class Worker - : public beast::LockFreeStack ::Node - , public beast::LockFreeStack ::Node + class Worker : public beast::LockFreeStack::Node, + public beast::LockFreeStack::Node { public: - Worker (Workers& workers, + Worker( + Workers& workers, std::string const& threadName, int const instance); - ~Worker (); + ~Worker(); - void notify (); + void + notify(); private: - void run (); + void + run(); private: Workers& m_workers; @@ -157,29 +166,32 @@ private: std::thread thread_; std::mutex mutex_; std::condition_variable wakeup_; - int wakeCount_; // how many times to un-pause + int wakeCount_; // how many times to un-pause bool shouldExit_; }; private: - static void deleteWorkers (beast::LockFreeStack & stack); + static void + deleteWorkers(beast::LockFreeStack& stack); private: Callback& m_callback; perf::PerfLog* perfLog_; - std::string m_threadNames; // The name to give each thread - std::condition_variable m_cv; // signaled when all threads paused - std::mutex m_mut; - bool m_allPaused; - semaphore m_semaphore; // each pending task is 1 resource - int m_numberOfThreads; // how many we want active now - std::atomic m_activeCount; // to know when all are paused - std::atomic m_pauseCount; // how many threads need to pause now - std::atomic m_runningTaskCount; // how many calls to processTask() active - beast::LockFreeStack m_everyone; // holds all created workers - beast::LockFreeStack m_paused; // holds just paused workers + std::string m_threadNames; // The name to give each thread + std::condition_variable m_cv; // signaled when all threads paused + std::mutex m_mut; + bool m_allPaused; + semaphore m_semaphore; // each pending task is 1 resource + int m_numberOfThreads; // how many we want active now + std::atomic m_activeCount; // to know when all are paused + std::atomic m_pauseCount; // how many threads need to pause now + std::atomic + m_runningTaskCount; // how many calls to processTask() active + beast::LockFreeStack m_everyone; // holds all created workers + beast::LockFreeStack + m_paused; // holds just paused workers }; -} // beast +} // namespace ripple #endif diff --git a/src/ripple/core/impl/semaphore.h b/src/ripple/core/impl/semaphore.h index 392a0d4871..3b64265bb1 100644 --- a/src/ripple/core/impl/semaphore.h +++ b/src/ripple/core/impl/semaphore.h @@ -39,32 +39,34 @@ public: /** Create the semaphore, with an optional initial count. If unspecified, the initial count is zero. */ - explicit basic_semaphore (size_type count = 0) - : m_count (count) + explicit basic_semaphore(size_type count = 0) : m_count(count) { } /** Increment the count and unblock one waiting thread. */ - void notify () + void + notify() { std::lock_guard lock{m_mutex}; ++m_count; - m_cond.notify_one (); + m_cond.notify_one(); } /** Block until notify is called. */ - void wait () + void + wait() { std::unique_lock lock{m_mutex}; while (m_count == 0) - m_cond.wait (lock); + m_cond.wait(lock); --m_count; } /** Perform a non-blocking wait. @return `true` If the wait would be satisfied. */ - bool try_wait () + bool + try_wait() { std::lock_guard lock{m_mutex}; if (m_count == 0) @@ -74,9 +76,8 @@ public: } }; -using semaphore = basic_semaphore ; +using semaphore = basic_semaphore; -} // ripple +} // namespace ripple #endif - diff --git a/src/ripple/crypto/GenerateDeterministicKey.h b/src/ripple/crypto/GenerateDeterministicKey.h index d23a8482a5..765126ed05 100644 --- a/src/ripple/crypto/GenerateDeterministicKey.h +++ b/src/ripple/crypto/GenerateDeterministicKey.h @@ -25,30 +25,23 @@ #ifndef RIPPLE_CRYPTO_GENERATEDETERMINISTICKEY_H_INCLUDED #define RIPPLE_CRYPTO_GENERATEDETERMINISTICKEY_H_INCLUDED -#include #include +#include namespace ripple { Blob -generateRootDeterministicPublicKey ( - uint128 const& seed); +generateRootDeterministicPublicKey(uint128 const& seed); uint256 -generateRootDeterministicPrivateKey ( - uint128 const& seed); +generateRootDeterministicPrivateKey(uint128 const& seed); Blob -generatePublicDeterministicKey ( - Blob const& generator, - int n); +generatePublicDeterministicKey(Blob const& generator, int n); uint256 -generatePrivateDeterministicKey ( - Blob const& family, - uint128 const& seed, - int n); +generatePrivateDeterministicKey(Blob const& family, uint128 const& seed, int n); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/crypto/RFC1751.h b/src/ripple/crypto/RFC1751.h index 3a7ea4b853..fa70830b79 100644 --- a/src/ripple/crypto/RFC1751.h +++ b/src/ripple/crypto/RFC1751.h @@ -28,9 +28,11 @@ namespace ripple { class RFC1751 { public: - static int getKeyFromEnglish (std::string& strKey, std::string const& strHuman); + static int + getKeyFromEnglish(std::string& strKey, std::string const& strHuman); - static void getEnglishFromKey (std::string& strHuman, std::string const& strKey); + static void + getEnglishFromKey(std::string& strHuman, std::string const& strKey); /** Chooses a single dictionary word from the data. @@ -39,19 +41,26 @@ public: it to turn the pubkey_node into an easily remembered and identified 4 character string. */ - static std::string getWordFromBlob (void const* blob, size_t bytes); + static std::string + getWordFromBlob(void const* blob, size_t bytes); private: - static unsigned long extract (char const* s, int start, int length); - static void btoe (std::string& strHuman, std::string const& strData); - static void insert (char* s, int x, int start, int length); - static void standard (std::string& strWord); - static int wsrch (std::string const& strWord, int iMin, int iMax); - static int etob (std::string& strData, std::vector vsHuman); + static unsigned long + extract(char const* s, int start, int length); + static void + btoe(std::string& strHuman, std::string const& strData); + static void + insert(char* s, int x, int start, int length); + static void + standard(std::string& strWord); + static int + wsrch(std::string const& strWord, int iMin, int iMax); + static int + etob(std::string& strData, std::vector vsHuman); - static char const* s_dictionary []; + static char const* s_dictionary[]; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/crypto/csprng.h b/src/ripple/crypto/csprng.h index a06b8a2e7d..3950632496 100644 --- a/src/ripple/crypto/csprng.h +++ b/src/ripple/crypto/csprng.h @@ -40,26 +40,25 @@ private: std::mutex mutex_; void - mix ( - void* buffer, - std::size_t count, - double bitsPerByte); + mix(void* buffer, std::size_t count, double bitsPerByte); public: using result_type = std::uint64_t; - csprng_engine(csprng_engine const&) = delete; - csprng_engine& operator=(csprng_engine const&) = delete; + csprng_engine(csprng_engine const&) = delete; + csprng_engine& + operator=(csprng_engine const&) = delete; csprng_engine(csprng_engine&&) = delete; - csprng_engine& operator=(csprng_engine&&) = delete; + csprng_engine& + operator=(csprng_engine&&) = delete; - csprng_engine (); - ~csprng_engine (); + csprng_engine(); + ~csprng_engine(); /** Mix entropy into the pool */ void - mix_entropy (void* buffer = nullptr, std::size_t count = 0); + mix_entropy(void* buffer = nullptr, std::size_t count = 0); /** Generate a random integer */ result_type @@ -67,19 +66,17 @@ public: /** Fill a buffer with the requested amount of random data */ void - operator()(void *ptr, std::size_t count); + operator()(void* ptr, std::size_t count); /* The smallest possible value that can be returned */ - static constexpr - result_type + static constexpr result_type min() { return std::numeric_limits::min(); } /* The largest possible value that can be returned */ - static constexpr - result_type + static constexpr result_type max() { return std::numeric_limits::max(); @@ -94,8 +91,9 @@ public: This meets the requirements of UniformRandomNumberEngine */ -csprng_engine& crypto_prng(); +csprng_engine& +crypto_prng(); -} +} // namespace ripple #endif diff --git a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp index aa7f31ff05..7719e2b65d 100644 --- a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp +++ b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp @@ -24,9 +24,9 @@ #include #include #include -#include #include #include +#include namespace ripple { @@ -37,21 +37,24 @@ struct secp256k1_data EC_GROUP const* group; bignum order; - secp256k1_data () + secp256k1_data() { - group = EC_GROUP_new_by_curve_name (NID_secp256k1); + group = EC_GROUP_new_by_curve_name(NID_secp256k1); if (!group) - LogicError ("The OpenSSL library on this system lacks elliptic curve support."); + LogicError( + "The OpenSSL library on this system lacks elliptic curve " + "support."); bn_ctx ctx; - order = get_order (group, ctx); + order = get_order(group, ctx); } }; -static secp256k1_data const& secp256k1curve() +static secp256k1_data const& +secp256k1curve() { - static secp256k1_data const curve {}; + static secp256k1_data const curve{}; return curve; } @@ -59,30 +62,32 @@ static secp256k1_data const& secp256k1curve() using namespace openssl; -static Blob serialize_ec_point (ec_point const& point) +static Blob +serialize_ec_point(ec_point const& point) { - Blob result (33); + Blob result(33); - serialize_ec_point (point, &result[0]); + serialize_ec_point(point, &result[0]); return result; } template void -copy_uint32 (FwdIt out, std::uint32_t v) +copy_uint32(FwdIt out, std::uint32_t v) { - *out++ = v >> 24; + *out++ = v >> 24; *out++ = (v >> 16) & 0xff; - *out++ = (v >> 8) & 0xff; - *out = v & 0xff; + *out++ = (v >> 8) & 0xff; + *out = v & 0xff; } // Functions to add support for deterministic EC keys // --> seed // <-- private root generator + public root generator -static bignum generateRootDeterministicKey (uint128 const& seed) +static bignum +generateRootDeterministicKey(uint128 const& seed) { // find non-zero private key less than the curve's order bignum privKey; @@ -94,52 +99,55 @@ static bignum generateRootDeterministicKey (uint128 const& seed) // |<--------------------------------->|<------>| std::array buf; std::copy(seed.begin(), seed.end(), buf.begin()); - copy_uint32 (buf.begin() + 16, seq++); + copy_uint32(buf.begin() + 16, seq++); auto root = sha512Half(buf); beast::secure_erase(buf.data(), buf.size()); - privKey.assign (root.data(), root.size()); + privKey.assign(root.data(), root.size()); beast::secure_erase(root.data(), root.size()); - } - while (privKey.is_zero() || privKey >= secp256k1curve().order); + } while (privKey.is_zero() || privKey >= secp256k1curve().order); beast::secure_erase(&seq, sizeof(seq)); return privKey; } // --> seed // <-- private root generator + public root generator -Blob generateRootDeterministicPublicKey (uint128 const& seed) +Blob +generateRootDeterministicPublicKey(uint128 const& seed) { bn_ctx ctx; - bignum privKey = generateRootDeterministicKey (seed); + bignum privKey = generateRootDeterministicKey(seed); // compute the corresponding public key point - ec_point pubKey = multiply (secp256k1curve().group, privKey, ctx); + ec_point pubKey = multiply(secp256k1curve().group, privKey, ctx); privKey.clear(); // security erase - return serialize_ec_point (pubKey); + return serialize_ec_point(pubKey); } -uint256 generateRootDeterministicPrivateKey (uint128 const& seed) +uint256 +generateRootDeterministicPrivateKey(uint128 const& seed) { - bignum key = generateRootDeterministicKey (seed); + bignum key = generateRootDeterministicKey(seed); - return uint256_from_bignum_clear (key); + return uint256_from_bignum_clear(key); } // Take ripple address. // --> root public generator (consumes) // <-- root public generator in EC format -static ec_point generateRootPubKey (bignum&& pubGenerator) +static ec_point +generateRootPubKey(bignum&& pubGenerator) { - ec_point pubPoint = bn2point (secp256k1curve().group, pubGenerator.get()); + ec_point pubPoint = bn2point(secp256k1curve().group, pubGenerator.get()); return pubPoint; } // --> public generator -static bignum makeHash (Blob const& pubGen, int seq, bignum const& order) +static bignum +makeHash(Blob const& pubGen, int seq, bignum const& order) { int subSeq = 0; @@ -151,57 +159,60 @@ static bignum makeHash (Blob const& pubGen, int seq, bignum const& order) // buf: 0 pubGen 33 seq 37 subSeq 41 // |<--------------------------->|<------>|<-------->| std::array buf; - std::copy (pubGen.begin(), pubGen.end(), buf.begin()); - copy_uint32 (buf.begin() + 33, seq); - copy_uint32 (buf.begin() + 37, subSeq++); + std::copy(pubGen.begin(), pubGen.end(), buf.begin()); + copy_uint32(buf.begin() + 33, seq); + copy_uint32(buf.begin() + 37, subSeq++); auto root = sha512Half_s(buf); beast::secure_erase(buf.data(), buf.size()); - result.assign (root.data(), root.size()); + result.assign(root.data(), root.size()); beast::secure_erase(root.data(), root.size()); - } - while (result.is_zero() || result >= order); + } while (result.is_zero() || result >= order); return result; } // --> public generator -Blob generatePublicDeterministicKey (Blob const& pubGen, int seq) +Blob +generatePublicDeterministicKey(Blob const& pubGen, int seq) { // publicKey(n) = rootPublicKey EC_POINT_+ Hash(pubHash|seq)*point - ec_point rootPubKey = generateRootPubKey (bignum (pubGen)); + ec_point rootPubKey = generateRootPubKey(bignum(pubGen)); bn_ctx ctx; // Calculate the private additional key. - bignum hash = makeHash (pubGen, seq, secp256k1curve().order); + bignum hash = makeHash(pubGen, seq, secp256k1curve().order); // Calculate the corresponding public key. - ec_point newPoint = multiply (secp256k1curve().group, hash, ctx); + ec_point newPoint = multiply(secp256k1curve().group, hash, ctx); // Add the master public key and set. - add_to (secp256k1curve().group, rootPubKey, newPoint, ctx); + add_to(secp256k1curve().group, rootPubKey, newPoint, ctx); - return serialize_ec_point (newPoint); + return serialize_ec_point(newPoint); } // --> root private key -uint256 generatePrivateDeterministicKey ( - Blob const& pubGen, uint128 const& seed, int seq) +uint256 +generatePrivateDeterministicKey( + Blob const& pubGen, + uint128 const& seed, + int seq) { // privateKey(n) = (rootPrivateKey + Hash(pubHash|seq)) % order - bignum rootPrivKey = generateRootDeterministicKey (seed); + bignum rootPrivKey = generateRootDeterministicKey(seed); bn_ctx ctx; // calculate the private additional key - bignum privKey = makeHash (pubGen, seq, secp256k1curve().order); + bignum privKey = makeHash(pubGen, seq, secp256k1curve().order); // calculate the final private key - add_to (rootPrivKey, privKey, secp256k1curve().order, ctx); + add_to(rootPrivKey, privKey, secp256k1curve().order, ctx); rootPrivKey.clear(); // security erase - return uint256_from_bignum_clear (privKey); + return uint256_from_bignum_clear(privKey); } -} // ripple +} // namespace ripple diff --git a/src/ripple/crypto/impl/RFC1751.cpp b/src/ripple/crypto/impl/RFC1751.cpp index f0166258ca..177d438581 100644 --- a/src/ripple/crypto/impl/RFC1751.cpp +++ b/src/ripple/crypto/impl/RFC1751.cpp @@ -30,283 +30,289 @@ namespace ripple { // RFC 1751 code converted to C++/Boost. // -char const* RFC1751::s_dictionary [2048] = -{ - "A", "ABE", "ACE", "ACT", "AD", "ADA", "ADD", - "AGO", "AID", "AIM", "AIR", "ALL", "ALP", "AM", "AMY", "AN", "ANA", - "AND", "ANN", "ANT", "ANY", "APE", "APS", "APT", "ARC", "ARE", "ARK", - "ARM", "ART", "AS", "ASH", "ASK", "AT", "ATE", "AUG", "AUK", "AVE", - "AWE", "AWK", "AWL", "AWN", "AX", "AYE", "BAD", "BAG", "BAH", "BAM", - "BAN", "BAR", "BAT", "BAY", "BE", "BED", "BEE", "BEG", "BEN", "BET", - "BEY", "BIB", "BID", "BIG", "BIN", "BIT", "BOB", "BOG", "BON", "BOO", - "BOP", "BOW", "BOY", "BUB", "BUD", "BUG", "BUM", "BUN", "BUS", "BUT", - "BUY", "BY", "BYE", "CAB", "CAL", "CAM", "CAN", "CAP", "CAR", "CAT", - "CAW", "COD", "COG", "COL", "CON", "COO", "COP", "COT", "COW", "COY", - "CRY", "CUB", "CUE", "CUP", "CUR", "CUT", "DAB", "DAD", "DAM", "DAN", - "DAR", "DAY", "DEE", "DEL", "DEN", "DES", "DEW", "DID", "DIE", "DIG", - "DIN", "DIP", "DO", "DOE", "DOG", "DON", "DOT", "DOW", "DRY", "DUB", - "DUD", "DUE", "DUG", "DUN", "EAR", "EAT", "ED", "EEL", "EGG", "EGO", - "ELI", "ELK", "ELM", "ELY", "EM", "END", "EST", "ETC", "EVA", "EVE", - "EWE", "EYE", "FAD", "FAN", "FAR", "FAT", "FAY", "FED", "FEE", "FEW", - "FIB", "FIG", "FIN", "FIR", "FIT", "FLO", "FLY", "FOE", "FOG", "FOR", - "FRY", "FUM", "FUN", "FUR", "GAB", "GAD", "GAG", "GAL", "GAM", "GAP", - "GAS", "GAY", "GEE", "GEL", "GEM", "GET", "GIG", "GIL", "GIN", "GO", - "GOT", "GUM", "GUN", "GUS", "GUT", "GUY", "GYM", "GYP", "HA", "HAD", - "HAL", "HAM", "HAN", "HAP", "HAS", "HAT", "HAW", "HAY", "HE", "HEM", - "HEN", "HER", "HEW", "HEY", "HI", "HID", "HIM", "HIP", "HIS", "HIT", - "HO", "HOB", "HOC", "HOE", "HOG", "HOP", "HOT", "HOW", "HUB", "HUE", - "HUG", "HUH", "HUM", "HUT", "I", "ICY", "IDA", "IF", "IKE", "ILL", - "INK", "INN", "IO", "ION", "IQ", "IRA", "IRE", "IRK", "IS", "IT", "ITS", - "IVY", "JAB", "JAG", "JAM", "JAN", "JAR", "JAW", "JAY", "JET", "JIG", - "JIM", "JO", "JOB", "JOE", "JOG", "JOT", "JOY", "JUG", "JUT", "KAY", - "KEG", "KEN", "KEY", "KID", "KIM", "KIN", "KIT", "LA", "LAB", "LAC", - "LAD", "LAG", "LAM", "LAP", "LAW", "LAY", "LEA", "LED", "LEE", "LEG", - "LEN", "LEO", "LET", "LEW", "LID", "LIE", "LIN", "LIP", "LIT", "LO", - "LOB", "LOG", "LOP", "LOS", "LOT", "LOU", "LOW", "LOY", "LUG", "LYE", - "MA", "MAC", "MAD", "MAE", "MAN", "MAO", "MAP", "MAT", "MAW", "MAY", - "ME", "MEG", "MEL", "MEN", "MET", "MEW", "MID", "MIN", "MIT", "MOB", - "MOD", "MOE", "MOO", "MOP", "MOS", "MOT", "MOW", "MUD", "MUG", "MUM", - "MY", "NAB", "NAG", "NAN", "NAP", "NAT", "NAY", "NE", "NED", "NEE", - "NET", "NEW", "NIB", "NIL", "NIP", "NIT", "NO", "NOB", "NOD", "NON", - "NOR", "NOT", "NOV", "NOW", "NU", "NUN", "NUT", "O", "OAF", "OAK", - "OAR", "OAT", "ODD", "ODE", "OF", "OFF", "OFT", "OH", "OIL", "OK", - "OLD", "ON", "ONE", "OR", "ORB", "ORE", "ORR", "OS", "OTT", "OUR", - "OUT", "OVA", "OW", "OWE", "OWL", "OWN", "OX", "PA", "PAD", "PAL", - "PAM", "PAN", "PAP", "PAR", "PAT", "PAW", "PAY", "PEA", "PEG", "PEN", - "PEP", "PER", "PET", "PEW", "PHI", "PI", "PIE", "PIN", "PIT", "PLY", - "PO", "POD", "POE", "POP", "POT", "POW", "PRO", "PRY", "PUB", "PUG", - "PUN", "PUP", "PUT", "QUO", "RAG", "RAM", "RAN", "RAP", "RAT", "RAW", - "RAY", "REB", "RED", "REP", "RET", "RIB", "RID", "RIG", "RIM", "RIO", - "RIP", "ROB", "ROD", "ROE", "RON", "ROT", "ROW", "ROY", "RUB", "RUE", - "RUG", "RUM", "RUN", "RYE", "SAC", "SAD", "SAG", "SAL", "SAM", "SAN", - "SAP", "SAT", "SAW", "SAY", "SEA", "SEC", "SEE", "SEN", "SET", "SEW", - "SHE", "SHY", "SIN", "SIP", "SIR", "SIS", "SIT", "SKI", "SKY", "SLY", - "SO", "SOB", "SOD", "SON", "SOP", "SOW", "SOY", "SPA", "SPY", "SUB", - "SUD", "SUE", "SUM", "SUN", "SUP", "TAB", "TAD", "TAG", "TAN", "TAP", - "TAR", "TEA", "TED", "TEE", "TEN", "THE", "THY", "TIC", "TIE", "TIM", - "TIN", "TIP", "TO", "TOE", "TOG", "TOM", "TON", "TOO", "TOP", "TOW", - "TOY", "TRY", "TUB", "TUG", "TUM", "TUN", "TWO", "UN", "UP", "US", - "USE", "VAN", "VAT", "VET", "VIE", "WAD", "WAG", "WAR", "WAS", "WAY", - "WE", "WEB", "WED", "WEE", "WET", "WHO", "WHY", "WIN", "WIT", "WOK", - "WON", "WOO", "WOW", "WRY", "WU", "YAM", "YAP", "YAW", "YE", "YEA", - "YES", "YET", "YOU", "ABED", "ABEL", "ABET", "ABLE", "ABUT", "ACHE", - "ACID", "ACME", "ACRE", "ACTA", "ACTS", "ADAM", "ADDS", "ADEN", "AFAR", - "AFRO", "AGEE", "AHEM", "AHOY", "AIDA", "AIDE", "AIDS", "AIRY", "AJAR", - "AKIN", "ALAN", "ALEC", "ALGA", "ALIA", "ALLY", "ALMA", "ALOE", "ALSO", - "ALTO", "ALUM", "ALVA", "AMEN", "AMES", "AMID", "AMMO", "AMOK", "AMOS", - "AMRA", "ANDY", "ANEW", "ANNA", "ANNE", "ANTE", "ANTI", "AQUA", "ARAB", - "ARCH", "AREA", "ARGO", "ARID", "ARMY", "ARTS", "ARTY", "ASIA", "ASKS", - "ATOM", "AUNT", "AURA", "AUTO", "AVER", "AVID", "AVIS", "AVON", "AVOW", - "AWAY", "AWRY", "BABE", "BABY", "BACH", "BACK", "BADE", "BAIL", "BAIT", - "BAKE", "BALD", "BALE", "BALI", "BALK", "BALL", "BALM", "BAND", "BANE", - "BANG", "BANK", "BARB", "BARD", "BARE", "BARK", "BARN", "BARR", "BASE", - "BASH", "BASK", "BASS", "BATE", "BATH", "BAWD", "BAWL", "BEAD", "BEAK", - "BEAM", "BEAN", "BEAR", "BEAT", "BEAU", "BECK", "BEEF", "BEEN", "BEER", - "BEET", "BELA", "BELL", "BELT", "BEND", "BENT", "BERG", "BERN", "BERT", - "BESS", "BEST", "BETA", "BETH", "BHOY", "BIAS", "BIDE", "BIEN", "BILE", - "BILK", "BILL", "BIND", "BING", "BIRD", "BITE", "BITS", "BLAB", "BLAT", - "BLED", "BLEW", "BLOB", "BLOC", "BLOT", "BLOW", "BLUE", "BLUM", "BLUR", - "BOAR", "BOAT", "BOCA", "BOCK", "BODE", "BODY", "BOGY", "BOHR", "BOIL", - "BOLD", "BOLO", "BOLT", "BOMB", "BONA", "BOND", "BONE", "BONG", "BONN", - "BONY", "BOOK", "BOOM", "BOON", "BOOT", "BORE", "BORG", "BORN", "BOSE", - "BOSS", "BOTH", "BOUT", "BOWL", "BOYD", "BRAD", "BRAE", "BRAG", "BRAN", - "BRAY", "BRED", "BREW", "BRIG", "BRIM", "BROW", "BUCK", "BUDD", "BUFF", - "BULB", "BULK", "BULL", "BUNK", "BUNT", "BUOY", "BURG", "BURL", "BURN", - "BURR", "BURT", "BURY", "BUSH", "BUSS", "BUST", "BUSY", "BYTE", "CADY", - "CAFE", "CAGE", "CAIN", "CAKE", "CALF", "CALL", "CALM", "CAME", "CANE", - "CANT", "CARD", "CARE", "CARL", "CARR", "CART", "CASE", "CASH", "CASK", - "CAST", "CAVE", "CEIL", "CELL", "CENT", "CERN", "CHAD", "CHAR", "CHAT", - "CHAW", "CHEF", "CHEN", "CHEW", "CHIC", "CHIN", "CHOU", "CHOW", "CHUB", - "CHUG", "CHUM", "CITE", "CITY", "CLAD", "CLAM", "CLAN", "CLAW", "CLAY", - "CLOD", "CLOG", "CLOT", "CLUB", "CLUE", "COAL", "COAT", "COCA", "COCK", - "COCO", "CODA", "CODE", "CODY", "COED", "COIL", "COIN", "COKE", "COLA", - "COLD", "COLT", "COMA", "COMB", "COME", "COOK", "COOL", "COON", "COOT", - "CORD", "CORE", "CORK", "CORN", "COST", "COVE", "COWL", "CRAB", "CRAG", - "CRAM", "CRAY", "CREW", "CRIB", "CROW", "CRUD", "CUBA", "CUBE", "CUFF", - "CULL", "CULT", "CUNY", "CURB", "CURD", "CURE", "CURL", "CURT", "CUTS", - "DADE", "DALE", "DAME", "DANA", "DANE", "DANG", "DANK", "DARE", "DARK", - "DARN", "DART", "DASH", "DATA", "DATE", "DAVE", "DAVY", "DAWN", "DAYS", - "DEAD", "DEAF", "DEAL", "DEAN", "DEAR", "DEBT", "DECK", "DEED", "DEEM", - "DEER", "DEFT", "DEFY", "DELL", "DENT", "DENY", "DESK", "DIAL", "DICE", - "DIED", "DIET", "DIME", "DINE", "DING", "DINT", "DIRE", "DIRT", "DISC", - "DISH", "DISK", "DIVE", "DOCK", "DOES", "DOLE", "DOLL", "DOLT", "DOME", - "DONE", "DOOM", "DOOR", "DORA", "DOSE", "DOTE", "DOUG", "DOUR", "DOVE", - "DOWN", "DRAB", "DRAG", "DRAM", "DRAW", "DREW", "DRUB", "DRUG", "DRUM", - "DUAL", "DUCK", "DUCT", "DUEL", "DUET", "DUKE", "DULL", "DUMB", "DUNE", - "DUNK", "DUSK", "DUST", "DUTY", "EACH", "EARL", "EARN", "EASE", "EAST", - "EASY", "EBEN", "ECHO", "EDDY", "EDEN", "EDGE", "EDGY", "EDIT", "EDNA", - "EGAN", "ELAN", "ELBA", "ELLA", "ELSE", "EMIL", "EMIT", "EMMA", "ENDS", - "ERIC", "EROS", "EVEN", "EVER", "EVIL", "EYED", "FACE", "FACT", "FADE", - "FAIL", "FAIN", "FAIR", "FAKE", "FALL", "FAME", "FANG", "FARM", "FAST", - "FATE", "FAWN", "FEAR", "FEAT", "FEED", "FEEL", "FEET", "FELL", "FELT", - "FEND", "FERN", "FEST", "FEUD", "FIEF", "FIGS", "FILE", "FILL", "FILM", - "FIND", "FINE", "FINK", "FIRE", "FIRM", "FISH", "FISK", "FIST", "FITS", - "FIVE", "FLAG", "FLAK", "FLAM", "FLAT", "FLAW", "FLEA", "FLED", "FLEW", - "FLIT", "FLOC", "FLOG", "FLOW", "FLUB", "FLUE", "FOAL", "FOAM", "FOGY", - "FOIL", "FOLD", "FOLK", "FOND", "FONT", "FOOD", "FOOL", "FOOT", "FORD", - "FORE", "FORK", "FORM", "FORT", "FOSS", "FOUL", "FOUR", "FOWL", "FRAU", - "FRAY", "FRED", "FREE", "FRET", "FREY", "FROG", "FROM", "FUEL", "FULL", - "FUME", "FUND", "FUNK", "FURY", "FUSE", "FUSS", "GAFF", "GAGE", "GAIL", - "GAIN", "GAIT", "GALA", "GALE", "GALL", "GALT", "GAME", "GANG", "GARB", - "GARY", "GASH", "GATE", "GAUL", "GAUR", "GAVE", "GAWK", "GEAR", "GELD", - "GENE", "GENT", "GERM", "GETS", "GIBE", "GIFT", "GILD", "GILL", "GILT", - "GINA", "GIRD", "GIRL", "GIST", "GIVE", "GLAD", "GLEE", "GLEN", "GLIB", - "GLOB", "GLOM", "GLOW", "GLUE", "GLUM", "GLUT", "GOAD", "GOAL", "GOAT", - "GOER", "GOES", "GOLD", "GOLF", "GONE", "GONG", "GOOD", "GOOF", "GORE", - "GORY", "GOSH", "GOUT", "GOWN", "GRAB", "GRAD", "GRAY", "GREG", "GREW", - "GREY", "GRID", "GRIM", "GRIN", "GRIT", "GROW", "GRUB", "GULF", "GULL", - "GUNK", "GURU", "GUSH", "GUST", "GWEN", "GWYN", "HAAG", "HAAS", "HACK", - "HAIL", "HAIR", "HALE", "HALF", "HALL", "HALO", "HALT", "HAND", "HANG", - "HANK", "HANS", "HARD", "HARK", "HARM", "HART", "HASH", "HAST", "HATE", - "HATH", "HAUL", "HAVE", "HAWK", "HAYS", "HEAD", "HEAL", "HEAR", "HEAT", - "HEBE", "HECK", "HEED", "HEEL", "HEFT", "HELD", "HELL", "HELM", "HERB", - "HERD", "HERE", "HERO", "HERS", "HESS", "HEWN", "HICK", "HIDE", "HIGH", - "HIKE", "HILL", "HILT", "HIND", "HINT", "HIRE", "HISS", "HIVE", "HOBO", - "HOCK", "HOFF", "HOLD", "HOLE", "HOLM", "HOLT", "HOME", "HONE", "HONK", - "HOOD", "HOOF", "HOOK", "HOOT", "HORN", "HOSE", "HOST", "HOUR", "HOVE", - "HOWE", "HOWL", "HOYT", "HUCK", "HUED", "HUFF", "HUGE", "HUGH", "HUGO", - "HULK", "HULL", "HUNK", "HUNT", "HURD", "HURL", "HURT", "HUSH", "HYDE", - "HYMN", "IBIS", "ICON", "IDEA", "IDLE", "IFFY", "INCA", "INCH", "INTO", - "IONS", "IOTA", "IOWA", "IRIS", "IRMA", "IRON", "ISLE", "ITCH", "ITEM", - "IVAN", "JACK", "JADE", "JAIL", "JAKE", "JANE", "JAVA", "JEAN", "JEFF", - "JERK", "JESS", "JEST", "JIBE", "JILL", "JILT", "JIVE", "JOAN", "JOBS", - "JOCK", "JOEL", "JOEY", "JOHN", "JOIN", "JOKE", "JOLT", "JOVE", "JUDD", - "JUDE", "JUDO", "JUDY", "JUJU", "JUKE", "JULY", "JUNE", "JUNK", "JUNO", - "JURY", "JUST", "JUTE", "KAHN", "KALE", "KANE", "KANT", "KARL", "KATE", - "KEEL", "KEEN", "KENO", "KENT", "KERN", "KERR", "KEYS", "KICK", "KILL", - "KIND", "KING", "KIRK", "KISS", "KITE", "KLAN", "KNEE", "KNEW", "KNIT", - "KNOB", "KNOT", "KNOW", "KOCH", "KONG", "KUDO", "KURD", "KURT", "KYLE", - "LACE", "LACK", "LACY", "LADY", "LAID", "LAIN", "LAIR", "LAKE", "LAMB", - "LAME", "LAND", "LANE", "LANG", "LARD", "LARK", "LASS", "LAST", "LATE", - "LAUD", "LAVA", "LAWN", "LAWS", "LAYS", "LEAD", "LEAF", "LEAK", "LEAN", - "LEAR", "LEEK", "LEER", "LEFT", "LEND", "LENS", "LENT", "LEON", "LESK", - "LESS", "LEST", "LETS", "LIAR", "LICE", "LICK", "LIED", "LIEN", "LIES", - "LIEU", "LIFE", "LIFT", "LIKE", "LILA", "LILT", "LILY", "LIMA", "LIMB", - "LIME", "LIND", "LINE", "LINK", "LINT", "LION", "LISA", "LIST", "LIVE", - "LOAD", "LOAF", "LOAM", "LOAN", "LOCK", "LOFT", "LOGE", "LOIS", "LOLA", - "LONE", "LONG", "LOOK", "LOON", "LOOT", "LORD", "LORE", "LOSE", "LOSS", - "LOST", "LOUD", "LOVE", "LOWE", "LUCK", "LUCY", "LUGE", "LUKE", "LULU", - "LUND", "LUNG", "LURA", "LURE", "LURK", "LUSH", "LUST", "LYLE", "LYNN", - "LYON", "LYRA", "MACE", "MADE", "MAGI", "MAID", "MAIL", "MAIN", "MAKE", - "MALE", "MALI", "MALL", "MALT", "MANA", "MANN", "MANY", "MARC", "MARE", - "MARK", "MARS", "MART", "MARY", "MASH", "MASK", "MASS", "MAST", "MATE", - "MATH", "MAUL", "MAYO", "MEAD", "MEAL", "MEAN", "MEAT", "MEEK", "MEET", - "MELD", "MELT", "MEMO", "MEND", "MENU", "MERT", "MESH", "MESS", "MICE", - "MIKE", "MILD", "MILE", "MILK", "MILL", "MILT", "MIMI", "MIND", "MINE", - "MINI", "MINK", "MINT", "MIRE", "MISS", "MIST", "MITE", "MITT", "MOAN", - "MOAT", "MOCK", "MODE", "MOLD", "MOLE", "MOLL", "MOLT", "MONA", "MONK", - "MONT", "MOOD", "MOON", "MOOR", "MOOT", "MORE", "MORN", "MORT", "MOSS", - "MOST", "MOTH", "MOVE", "MUCH", "MUCK", "MUDD", "MUFF", "MULE", "MULL", - "MURK", "MUSH", "MUST", "MUTE", "MUTT", "MYRA", "MYTH", "NAGY", "NAIL", - "NAIR", "NAME", "NARY", "NASH", "NAVE", "NAVY", "NEAL", "NEAR", "NEAT", - "NECK", "NEED", "NEIL", "NELL", "NEON", "NERO", "NESS", "NEST", "NEWS", - "NEWT", "NIBS", "NICE", "NICK", "NILE", "NINA", "NINE", "NOAH", "NODE", - "NOEL", "NOLL", "NONE", "NOOK", "NOON", "NORM", "NOSE", "NOTE", "NOUN", - "NOVA", "NUDE", "NULL", "NUMB", "OATH", "OBEY", "OBOE", "ODIN", "OHIO", - "OILY", "OINT", "OKAY", "OLAF", "OLDY", "OLGA", "OLIN", "OMAN", "OMEN", - "OMIT", "ONCE", "ONES", "ONLY", "ONTO", "ONUS", "ORAL", "ORGY", "OSLO", - "OTIS", "OTTO", "OUCH", "OUST", "OUTS", "OVAL", "OVEN", "OVER", "OWLY", - "OWNS", "QUAD", "QUIT", "QUOD", "RACE", "RACK", "RACY", "RAFT", "RAGE", - "RAID", "RAIL", "RAIN", "RAKE", "RANK", "RANT", "RARE", "RASH", "RATE", - "RAVE", "RAYS", "READ", "REAL", "REAM", "REAR", "RECK", "REED", "REEF", - "REEK", "REEL", "REID", "REIN", "RENA", "REND", "RENT", "REST", "RICE", - "RICH", "RICK", "RIDE", "RIFT", "RILL", "RIME", "RING", "RINK", "RISE", - "RISK", "RITE", "ROAD", "ROAM", "ROAR", "ROBE", "ROCK", "RODE", "ROIL", - "ROLL", "ROME", "ROOD", "ROOF", "ROOK", "ROOM", "ROOT", "ROSA", "ROSE", - "ROSS", "ROSY", "ROTH", "ROUT", "ROVE", "ROWE", "ROWS", "RUBE", "RUBY", - "RUDE", "RUDY", "RUIN", "RULE", "RUNG", "RUNS", "RUNT", "RUSE", "RUSH", - "RUSK", "RUSS", "RUST", "RUTH", "SACK", "SAFE", "SAGE", "SAID", "SAIL", - "SALE", "SALK", "SALT", "SAME", "SAND", "SANE", "SANG", "SANK", "SARA", - "SAUL", "SAVE", "SAYS", "SCAN", "SCAR", "SCAT", "SCOT", "SEAL", "SEAM", - "SEAR", "SEAT", "SEED", "SEEK", "SEEM", "SEEN", "SEES", "SELF", "SELL", - "SEND", "SENT", "SETS", "SEWN", "SHAG", "SHAM", "SHAW", "SHAY", "SHED", - "SHIM", "SHIN", "SHOD", "SHOE", "SHOT", "SHOW", "SHUN", "SHUT", "SICK", - "SIDE", "SIFT", "SIGH", "SIGN", "SILK", "SILL", "SILO", "SILT", "SINE", - "SING", "SINK", "SIRE", "SITE", "SITS", "SITU", "SKAT", "SKEW", "SKID", - "SKIM", "SKIN", "SKIT", "SLAB", "SLAM", "SLAT", "SLAY", "SLED", "SLEW", - "SLID", "SLIM", "SLIT", "SLOB", "SLOG", "SLOT", "SLOW", "SLUG", "SLUM", - "SLUR", "SMOG", "SMUG", "SNAG", "SNOB", "SNOW", "SNUB", "SNUG", "SOAK", - "SOAR", "SOCK", "SODA", "SOFA", "SOFT", "SOIL", "SOLD", "SOME", "SONG", - "SOON", "SOOT", "SORE", "SORT", "SOUL", "SOUR", "SOWN", "STAB", "STAG", - "STAN", "STAR", "STAY", "STEM", "STEW", "STIR", "STOW", "STUB", "STUN", - "SUCH", "SUDS", "SUIT", "SULK", "SUMS", "SUNG", "SUNK", "SURE", "SURF", - "SWAB", "SWAG", "SWAM", "SWAN", "SWAT", "SWAY", "SWIM", "SWUM", "TACK", - "TACT", "TAIL", "TAKE", "TALE", "TALK", "TALL", "TANK", "TASK", "TATE", - "TAUT", "TEAL", "TEAM", "TEAR", "TECH", "TEEM", "TEEN", "TEET", "TELL", - "TEND", "TENT", "TERM", "TERN", "TESS", "TEST", "THAN", "THAT", "THEE", - "THEM", "THEN", "THEY", "THIN", "THIS", "THUD", "THUG", "TICK", "TIDE", - "TIDY", "TIED", "TIER", "TILE", "TILL", "TILT", "TIME", "TINA", "TINE", - "TINT", "TINY", "TIRE", "TOAD", "TOGO", "TOIL", "TOLD", "TOLL", "TONE", - "TONG", "TONY", "TOOK", "TOOL", "TOOT", "TORE", "TORN", "TOTE", "TOUR", - "TOUT", "TOWN", "TRAG", "TRAM", "TRAY", "TREE", "TREK", "TRIG", "TRIM", - "TRIO", "TROD", "TROT", "TROY", "TRUE", "TUBA", "TUBE", "TUCK", "TUFT", - "TUNA", "TUNE", "TUNG", "TURF", "TURN", "TUSK", "TWIG", "TWIN", "TWIT", - "ULAN", "UNIT", "URGE", "USED", "USER", "USES", "UTAH", "VAIL", "VAIN", - "VALE", "VARY", "VASE", "VAST", "VEAL", "VEDA", "VEIL", "VEIN", "VEND", - "VENT", "VERB", "VERY", "VETO", "VICE", "VIEW", "VINE", "VISE", "VOID", - "VOLT", "VOTE", "WACK", "WADE", "WAGE", "WAIL", "WAIT", "WAKE", "WALE", - "WALK", "WALL", "WALT", "WAND", "WANE", "WANG", "WANT", "WARD", "WARM", - "WARN", "WART", "WASH", "WAST", "WATS", "WATT", "WAVE", "WAVY", "WAYS", - "WEAK", "WEAL", "WEAN", "WEAR", "WEED", "WEEK", "WEIR", "WELD", "WELL", - "WELT", "WENT", "WERE", "WERT", "WEST", "WHAM", "WHAT", "WHEE", "WHEN", - "WHET", "WHOA", "WHOM", "WICK", "WIFE", "WILD", "WILL", "WIND", "WINE", - "WING", "WINK", "WINO", "WIRE", "WISE", "WISH", "WITH", "WOLF", "WONT", - "WOOD", "WOOL", "WORD", "WORE", "WORK", "WORM", "WORN", "WOVE", "WRIT", - "WYNN", "YALE", "YANG", "YANK", "YARD", "YARN", "YAWL", "YAWN", "YEAH", - "YEAR", "YELL", "YOGA", "YOKE" -}; +char const* RFC1751::s_dictionary[2048] = { + "A", "ABE", "ACE", "ACT", "AD", "ADA", "ADD", "AGO", "AID", + "AIM", "AIR", "ALL", "ALP", "AM", "AMY", "AN", "ANA", "AND", + "ANN", "ANT", "ANY", "APE", "APS", "APT", "ARC", "ARE", "ARK", + "ARM", "ART", "AS", "ASH", "ASK", "AT", "ATE", "AUG", "AUK", + "AVE", "AWE", "AWK", "AWL", "AWN", "AX", "AYE", "BAD", "BAG", + "BAH", "BAM", "BAN", "BAR", "BAT", "BAY", "BE", "BED", "BEE", + "BEG", "BEN", "BET", "BEY", "BIB", "BID", "BIG", "BIN", "BIT", + "BOB", "BOG", "BON", "BOO", "BOP", "BOW", "BOY", "BUB", "BUD", + "BUG", "BUM", "BUN", "BUS", "BUT", "BUY", "BY", "BYE", "CAB", + "CAL", "CAM", "CAN", "CAP", "CAR", "CAT", "CAW", "COD", "COG", + "COL", "CON", "COO", "COP", "COT", "COW", "COY", "CRY", "CUB", + "CUE", "CUP", "CUR", "CUT", "DAB", "DAD", "DAM", "DAN", "DAR", + "DAY", "DEE", "DEL", "DEN", "DES", "DEW", "DID", "DIE", "DIG", + "DIN", "DIP", "DO", "DOE", "DOG", "DON", "DOT", "DOW", "DRY", + "DUB", "DUD", "DUE", "DUG", "DUN", "EAR", "EAT", "ED", "EEL", + "EGG", "EGO", "ELI", "ELK", "ELM", "ELY", "EM", "END", "EST", + "ETC", "EVA", "EVE", "EWE", "EYE", "FAD", "FAN", "FAR", "FAT", + "FAY", "FED", "FEE", "FEW", "FIB", "FIG", "FIN", "FIR", "FIT", + "FLO", "FLY", "FOE", "FOG", "FOR", "FRY", "FUM", "FUN", "FUR", + "GAB", "GAD", "GAG", "GAL", "GAM", "GAP", "GAS", "GAY", "GEE", + "GEL", "GEM", "GET", "GIG", "GIL", "GIN", "GO", "GOT", "GUM", + "GUN", "GUS", "GUT", "GUY", "GYM", "GYP", "HA", "HAD", "HAL", + "HAM", "HAN", "HAP", "HAS", "HAT", "HAW", "HAY", "HE", "HEM", + "HEN", "HER", "HEW", "HEY", "HI", "HID", "HIM", "HIP", "HIS", + "HIT", "HO", "HOB", "HOC", "HOE", "HOG", "HOP", "HOT", "HOW", + "HUB", "HUE", "HUG", "HUH", "HUM", "HUT", "I", "ICY", "IDA", + "IF", "IKE", "ILL", "INK", "INN", "IO", "ION", "IQ", "IRA", + "IRE", "IRK", "IS", "IT", "ITS", "IVY", "JAB", "JAG", "JAM", + "JAN", "JAR", "JAW", "JAY", "JET", "JIG", "JIM", "JO", "JOB", + "JOE", "JOG", "JOT", "JOY", "JUG", "JUT", "KAY", "KEG", "KEN", + "KEY", "KID", "KIM", "KIN", "KIT", "LA", "LAB", "LAC", "LAD", + "LAG", "LAM", "LAP", "LAW", "LAY", "LEA", "LED", "LEE", "LEG", + "LEN", "LEO", "LET", "LEW", "LID", "LIE", "LIN", "LIP", "LIT", + "LO", "LOB", "LOG", "LOP", "LOS", "LOT", "LOU", "LOW", "LOY", + "LUG", "LYE", "MA", "MAC", "MAD", "MAE", "MAN", "MAO", "MAP", + "MAT", "MAW", "MAY", "ME", "MEG", "MEL", "MEN", "MET", "MEW", + "MID", "MIN", "MIT", "MOB", "MOD", "MOE", "MOO", "MOP", "MOS", + "MOT", "MOW", "MUD", "MUG", "MUM", "MY", "NAB", "NAG", "NAN", + "NAP", "NAT", "NAY", "NE", "NED", "NEE", "NET", "NEW", "NIB", + "NIL", "NIP", "NIT", "NO", "NOB", "NOD", "NON", "NOR", "NOT", + "NOV", "NOW", "NU", "NUN", "NUT", "O", "OAF", "OAK", "OAR", + "OAT", "ODD", "ODE", "OF", "OFF", "OFT", "OH", "OIL", "OK", + "OLD", "ON", "ONE", "OR", "ORB", "ORE", "ORR", "OS", "OTT", + "OUR", "OUT", "OVA", "OW", "OWE", "OWL", "OWN", "OX", "PA", + "PAD", "PAL", "PAM", "PAN", "PAP", "PAR", "PAT", "PAW", "PAY", + "PEA", "PEG", "PEN", "PEP", "PER", "PET", "PEW", "PHI", "PI", + "PIE", "PIN", "PIT", "PLY", "PO", "POD", "POE", "POP", "POT", + "POW", "PRO", "PRY", "PUB", "PUG", "PUN", "PUP", "PUT", "QUO", + "RAG", "RAM", "RAN", "RAP", "RAT", "RAW", "RAY", "REB", "RED", + "REP", "RET", "RIB", "RID", "RIG", "RIM", "RIO", "RIP", "ROB", + "ROD", "ROE", "RON", "ROT", "ROW", "ROY", "RUB", "RUE", "RUG", + "RUM", "RUN", "RYE", "SAC", "SAD", "SAG", "SAL", "SAM", "SAN", + "SAP", "SAT", "SAW", "SAY", "SEA", "SEC", "SEE", "SEN", "SET", + "SEW", "SHE", "SHY", "SIN", "SIP", "SIR", "SIS", "SIT", "SKI", + "SKY", "SLY", "SO", "SOB", "SOD", "SON", "SOP", "SOW", "SOY", + "SPA", "SPY", "SUB", "SUD", "SUE", "SUM", "SUN", "SUP", "TAB", + "TAD", "TAG", "TAN", "TAP", "TAR", "TEA", "TED", "TEE", "TEN", + "THE", "THY", "TIC", "TIE", "TIM", "TIN", "TIP", "TO", "TOE", + "TOG", "TOM", "TON", "TOO", "TOP", "TOW", "TOY", "TRY", "TUB", + "TUG", "TUM", "TUN", "TWO", "UN", "UP", "US", "USE", "VAN", + "VAT", "VET", "VIE", "WAD", "WAG", "WAR", "WAS", "WAY", "WE", + "WEB", "WED", "WEE", "WET", "WHO", "WHY", "WIN", "WIT", "WOK", + "WON", "WOO", "WOW", "WRY", "WU", "YAM", "YAP", "YAW", "YE", + "YEA", "YES", "YET", "YOU", "ABED", "ABEL", "ABET", "ABLE", "ABUT", + "ACHE", "ACID", "ACME", "ACRE", "ACTA", "ACTS", "ADAM", "ADDS", "ADEN", + "AFAR", "AFRO", "AGEE", "AHEM", "AHOY", "AIDA", "AIDE", "AIDS", "AIRY", + "AJAR", "AKIN", "ALAN", "ALEC", "ALGA", "ALIA", "ALLY", "ALMA", "ALOE", + "ALSO", "ALTO", "ALUM", "ALVA", "AMEN", "AMES", "AMID", "AMMO", "AMOK", + "AMOS", "AMRA", "ANDY", "ANEW", "ANNA", "ANNE", "ANTE", "ANTI", "AQUA", + "ARAB", "ARCH", "AREA", "ARGO", "ARID", "ARMY", "ARTS", "ARTY", "ASIA", + "ASKS", "ATOM", "AUNT", "AURA", "AUTO", "AVER", "AVID", "AVIS", "AVON", + "AVOW", "AWAY", "AWRY", "BABE", "BABY", "BACH", "BACK", "BADE", "BAIL", + "BAIT", "BAKE", "BALD", "BALE", "BALI", "BALK", "BALL", "BALM", "BAND", + "BANE", "BANG", "BANK", "BARB", "BARD", "BARE", "BARK", "BARN", "BARR", + "BASE", "BASH", "BASK", "BASS", "BATE", "BATH", "BAWD", "BAWL", "BEAD", + "BEAK", "BEAM", "BEAN", "BEAR", "BEAT", "BEAU", "BECK", "BEEF", "BEEN", + "BEER", "BEET", "BELA", "BELL", "BELT", "BEND", "BENT", "BERG", "BERN", + "BERT", "BESS", "BEST", "BETA", "BETH", "BHOY", "BIAS", "BIDE", "BIEN", + "BILE", "BILK", "BILL", "BIND", "BING", "BIRD", "BITE", "BITS", "BLAB", + "BLAT", "BLED", "BLEW", "BLOB", "BLOC", "BLOT", "BLOW", "BLUE", "BLUM", + "BLUR", "BOAR", "BOAT", "BOCA", "BOCK", "BODE", "BODY", "BOGY", "BOHR", + "BOIL", "BOLD", "BOLO", "BOLT", "BOMB", "BONA", "BOND", "BONE", "BONG", + "BONN", "BONY", "BOOK", "BOOM", "BOON", "BOOT", "BORE", "BORG", "BORN", + "BOSE", "BOSS", "BOTH", "BOUT", "BOWL", "BOYD", "BRAD", "BRAE", "BRAG", + "BRAN", "BRAY", "BRED", "BREW", "BRIG", "BRIM", "BROW", "BUCK", "BUDD", + "BUFF", "BULB", "BULK", "BULL", "BUNK", "BUNT", "BUOY", "BURG", "BURL", + "BURN", "BURR", "BURT", "BURY", "BUSH", "BUSS", "BUST", "BUSY", "BYTE", + "CADY", "CAFE", "CAGE", "CAIN", "CAKE", "CALF", "CALL", "CALM", "CAME", + "CANE", "CANT", "CARD", "CARE", "CARL", "CARR", "CART", "CASE", "CASH", + "CASK", "CAST", "CAVE", "CEIL", "CELL", "CENT", "CERN", "CHAD", "CHAR", + "CHAT", "CHAW", "CHEF", "CHEN", "CHEW", "CHIC", "CHIN", "CHOU", "CHOW", + "CHUB", "CHUG", "CHUM", "CITE", "CITY", "CLAD", "CLAM", "CLAN", "CLAW", + "CLAY", "CLOD", "CLOG", "CLOT", "CLUB", "CLUE", "COAL", "COAT", "COCA", + "COCK", "COCO", "CODA", "CODE", "CODY", "COED", "COIL", "COIN", "COKE", + "COLA", "COLD", "COLT", "COMA", "COMB", "COME", "COOK", "COOL", "COON", + "COOT", "CORD", "CORE", "CORK", "CORN", "COST", "COVE", "COWL", "CRAB", + "CRAG", "CRAM", "CRAY", "CREW", "CRIB", "CROW", "CRUD", "CUBA", "CUBE", + "CUFF", "CULL", "CULT", "CUNY", "CURB", "CURD", "CURE", "CURL", "CURT", + "CUTS", "DADE", "DALE", "DAME", "DANA", "DANE", "DANG", "DANK", "DARE", + "DARK", "DARN", "DART", "DASH", "DATA", "DATE", "DAVE", "DAVY", "DAWN", + "DAYS", "DEAD", "DEAF", "DEAL", "DEAN", "DEAR", "DEBT", "DECK", "DEED", + "DEEM", "DEER", "DEFT", "DEFY", "DELL", "DENT", "DENY", "DESK", "DIAL", + "DICE", "DIED", "DIET", "DIME", "DINE", "DING", "DINT", "DIRE", "DIRT", + "DISC", "DISH", "DISK", "DIVE", "DOCK", "DOES", "DOLE", "DOLL", "DOLT", + "DOME", "DONE", "DOOM", "DOOR", "DORA", "DOSE", "DOTE", "DOUG", "DOUR", + "DOVE", "DOWN", "DRAB", "DRAG", "DRAM", "DRAW", "DREW", "DRUB", "DRUG", + "DRUM", "DUAL", "DUCK", "DUCT", "DUEL", "DUET", "DUKE", "DULL", "DUMB", + "DUNE", "DUNK", "DUSK", "DUST", "DUTY", "EACH", "EARL", "EARN", "EASE", + "EAST", "EASY", "EBEN", "ECHO", "EDDY", "EDEN", "EDGE", "EDGY", "EDIT", + "EDNA", "EGAN", "ELAN", "ELBA", "ELLA", "ELSE", "EMIL", "EMIT", "EMMA", + "ENDS", "ERIC", "EROS", "EVEN", "EVER", "EVIL", "EYED", "FACE", "FACT", + "FADE", "FAIL", "FAIN", "FAIR", "FAKE", "FALL", "FAME", "FANG", "FARM", + "FAST", "FATE", "FAWN", "FEAR", "FEAT", "FEED", "FEEL", "FEET", "FELL", + "FELT", "FEND", "FERN", "FEST", "FEUD", "FIEF", "FIGS", "FILE", "FILL", + "FILM", "FIND", "FINE", "FINK", "FIRE", "FIRM", "FISH", "FISK", "FIST", + "FITS", "FIVE", "FLAG", "FLAK", "FLAM", "FLAT", "FLAW", "FLEA", "FLED", + "FLEW", "FLIT", "FLOC", "FLOG", "FLOW", "FLUB", "FLUE", "FOAL", "FOAM", + "FOGY", "FOIL", "FOLD", "FOLK", "FOND", "FONT", "FOOD", "FOOL", "FOOT", + "FORD", "FORE", "FORK", "FORM", "FORT", "FOSS", "FOUL", "FOUR", "FOWL", + "FRAU", "FRAY", "FRED", "FREE", "FRET", "FREY", "FROG", "FROM", "FUEL", + "FULL", "FUME", "FUND", "FUNK", "FURY", "FUSE", "FUSS", "GAFF", "GAGE", + "GAIL", "GAIN", "GAIT", "GALA", "GALE", "GALL", "GALT", "GAME", "GANG", + "GARB", "GARY", "GASH", "GATE", "GAUL", "GAUR", "GAVE", "GAWK", "GEAR", + "GELD", "GENE", "GENT", "GERM", "GETS", "GIBE", "GIFT", "GILD", "GILL", + "GILT", "GINA", "GIRD", "GIRL", "GIST", "GIVE", "GLAD", "GLEE", "GLEN", + "GLIB", "GLOB", "GLOM", "GLOW", "GLUE", "GLUM", "GLUT", "GOAD", "GOAL", + "GOAT", "GOER", "GOES", "GOLD", "GOLF", "GONE", "GONG", "GOOD", "GOOF", + "GORE", "GORY", "GOSH", "GOUT", "GOWN", "GRAB", "GRAD", "GRAY", "GREG", + "GREW", "GREY", "GRID", "GRIM", "GRIN", "GRIT", "GROW", "GRUB", "GULF", + "GULL", "GUNK", "GURU", "GUSH", "GUST", "GWEN", "GWYN", "HAAG", "HAAS", + "HACK", "HAIL", "HAIR", "HALE", "HALF", "HALL", "HALO", "HALT", "HAND", + "HANG", "HANK", "HANS", "HARD", "HARK", "HARM", "HART", "HASH", "HAST", + "HATE", "HATH", "HAUL", "HAVE", "HAWK", "HAYS", "HEAD", "HEAL", "HEAR", + "HEAT", "HEBE", "HECK", "HEED", "HEEL", "HEFT", "HELD", "HELL", "HELM", + "HERB", "HERD", "HERE", "HERO", "HERS", "HESS", "HEWN", "HICK", "HIDE", + "HIGH", "HIKE", "HILL", "HILT", "HIND", "HINT", "HIRE", "HISS", "HIVE", + "HOBO", "HOCK", "HOFF", "HOLD", "HOLE", "HOLM", "HOLT", "HOME", "HONE", + "HONK", "HOOD", "HOOF", "HOOK", "HOOT", "HORN", "HOSE", "HOST", "HOUR", + "HOVE", "HOWE", "HOWL", "HOYT", "HUCK", "HUED", "HUFF", "HUGE", "HUGH", + "HUGO", "HULK", "HULL", "HUNK", "HUNT", "HURD", "HURL", "HURT", "HUSH", + "HYDE", "HYMN", "IBIS", "ICON", "IDEA", "IDLE", "IFFY", "INCA", "INCH", + "INTO", "IONS", "IOTA", "IOWA", "IRIS", "IRMA", "IRON", "ISLE", "ITCH", + "ITEM", "IVAN", "JACK", "JADE", "JAIL", "JAKE", "JANE", "JAVA", "JEAN", + "JEFF", "JERK", "JESS", "JEST", "JIBE", "JILL", "JILT", "JIVE", "JOAN", + "JOBS", "JOCK", "JOEL", "JOEY", "JOHN", "JOIN", "JOKE", "JOLT", "JOVE", + "JUDD", "JUDE", "JUDO", "JUDY", "JUJU", "JUKE", "JULY", "JUNE", "JUNK", + "JUNO", "JURY", "JUST", "JUTE", "KAHN", "KALE", "KANE", "KANT", "KARL", + "KATE", "KEEL", "KEEN", "KENO", "KENT", "KERN", "KERR", "KEYS", "KICK", + "KILL", "KIND", "KING", "KIRK", "KISS", "KITE", "KLAN", "KNEE", "KNEW", + "KNIT", "KNOB", "KNOT", "KNOW", "KOCH", "KONG", "KUDO", "KURD", "KURT", + "KYLE", "LACE", "LACK", "LACY", "LADY", "LAID", "LAIN", "LAIR", "LAKE", + "LAMB", "LAME", "LAND", "LANE", "LANG", "LARD", "LARK", "LASS", "LAST", + "LATE", "LAUD", "LAVA", "LAWN", "LAWS", "LAYS", "LEAD", "LEAF", "LEAK", + "LEAN", "LEAR", "LEEK", "LEER", "LEFT", "LEND", "LENS", "LENT", "LEON", + "LESK", "LESS", "LEST", "LETS", "LIAR", "LICE", "LICK", "LIED", "LIEN", + "LIES", "LIEU", "LIFE", "LIFT", "LIKE", "LILA", "LILT", "LILY", "LIMA", + "LIMB", "LIME", "LIND", "LINE", "LINK", "LINT", "LION", "LISA", "LIST", + "LIVE", "LOAD", "LOAF", "LOAM", "LOAN", "LOCK", "LOFT", "LOGE", "LOIS", + "LOLA", "LONE", "LONG", "LOOK", "LOON", "LOOT", "LORD", "LORE", "LOSE", + "LOSS", "LOST", "LOUD", "LOVE", "LOWE", "LUCK", "LUCY", "LUGE", "LUKE", + "LULU", "LUND", "LUNG", "LURA", "LURE", "LURK", "LUSH", "LUST", "LYLE", + "LYNN", "LYON", "LYRA", "MACE", "MADE", "MAGI", "MAID", "MAIL", "MAIN", + "MAKE", "MALE", "MALI", "MALL", "MALT", "MANA", "MANN", "MANY", "MARC", + "MARE", "MARK", "MARS", "MART", "MARY", "MASH", "MASK", "MASS", "MAST", + "MATE", "MATH", "MAUL", "MAYO", "MEAD", "MEAL", "MEAN", "MEAT", "MEEK", + "MEET", "MELD", "MELT", "MEMO", "MEND", "MENU", "MERT", "MESH", "MESS", + "MICE", "MIKE", "MILD", "MILE", "MILK", "MILL", "MILT", "MIMI", "MIND", + "MINE", "MINI", "MINK", "MINT", "MIRE", "MISS", "MIST", "MITE", "MITT", + "MOAN", "MOAT", "MOCK", "MODE", "MOLD", "MOLE", "MOLL", "MOLT", "MONA", + "MONK", "MONT", "MOOD", "MOON", "MOOR", "MOOT", "MORE", "MORN", "MORT", + "MOSS", "MOST", "MOTH", "MOVE", "MUCH", "MUCK", "MUDD", "MUFF", "MULE", + "MULL", "MURK", "MUSH", "MUST", "MUTE", "MUTT", "MYRA", "MYTH", "NAGY", + "NAIL", "NAIR", "NAME", "NARY", "NASH", "NAVE", "NAVY", "NEAL", "NEAR", + "NEAT", "NECK", "NEED", "NEIL", "NELL", "NEON", "NERO", "NESS", "NEST", + "NEWS", "NEWT", "NIBS", "NICE", "NICK", "NILE", "NINA", "NINE", "NOAH", + "NODE", "NOEL", "NOLL", "NONE", "NOOK", "NOON", "NORM", "NOSE", "NOTE", + "NOUN", "NOVA", "NUDE", "NULL", "NUMB", "OATH", "OBEY", "OBOE", "ODIN", + "OHIO", "OILY", "OINT", "OKAY", "OLAF", "OLDY", "OLGA", "OLIN", "OMAN", + "OMEN", "OMIT", "ONCE", "ONES", "ONLY", "ONTO", "ONUS", "ORAL", "ORGY", + "OSLO", "OTIS", "OTTO", "OUCH", "OUST", "OUTS", "OVAL", "OVEN", "OVER", + "OWLY", "OWNS", "QUAD", "QUIT", "QUOD", "RACE", "RACK", "RACY", "RAFT", + "RAGE", "RAID", "RAIL", "RAIN", "RAKE", "RANK", "RANT", "RARE", "RASH", + "RATE", "RAVE", "RAYS", "READ", "REAL", "REAM", "REAR", "RECK", "REED", + "REEF", "REEK", "REEL", "REID", "REIN", "RENA", "REND", "RENT", "REST", + "RICE", "RICH", "RICK", "RIDE", "RIFT", "RILL", "RIME", "RING", "RINK", + "RISE", "RISK", "RITE", "ROAD", "ROAM", "ROAR", "ROBE", "ROCK", "RODE", + "ROIL", "ROLL", "ROME", "ROOD", "ROOF", "ROOK", "ROOM", "ROOT", "ROSA", + "ROSE", "ROSS", "ROSY", "ROTH", "ROUT", "ROVE", "ROWE", "ROWS", "RUBE", + "RUBY", "RUDE", "RUDY", "RUIN", "RULE", "RUNG", "RUNS", "RUNT", "RUSE", + "RUSH", "RUSK", "RUSS", "RUST", "RUTH", "SACK", "SAFE", "SAGE", "SAID", + "SAIL", "SALE", "SALK", "SALT", "SAME", "SAND", "SANE", "SANG", "SANK", + "SARA", "SAUL", "SAVE", "SAYS", "SCAN", "SCAR", "SCAT", "SCOT", "SEAL", + "SEAM", "SEAR", "SEAT", "SEED", "SEEK", "SEEM", "SEEN", "SEES", "SELF", + "SELL", "SEND", "SENT", "SETS", "SEWN", "SHAG", "SHAM", "SHAW", "SHAY", + "SHED", "SHIM", "SHIN", "SHOD", "SHOE", "SHOT", "SHOW", "SHUN", "SHUT", + "SICK", "SIDE", "SIFT", "SIGH", "SIGN", "SILK", "SILL", "SILO", "SILT", + "SINE", "SING", "SINK", "SIRE", "SITE", "SITS", "SITU", "SKAT", "SKEW", + "SKID", "SKIM", "SKIN", "SKIT", "SLAB", "SLAM", "SLAT", "SLAY", "SLED", + "SLEW", "SLID", "SLIM", "SLIT", "SLOB", "SLOG", "SLOT", "SLOW", "SLUG", + "SLUM", "SLUR", "SMOG", "SMUG", "SNAG", "SNOB", "SNOW", "SNUB", "SNUG", + "SOAK", "SOAR", "SOCK", "SODA", "SOFA", "SOFT", "SOIL", "SOLD", "SOME", + "SONG", "SOON", "SOOT", "SORE", "SORT", "SOUL", "SOUR", "SOWN", "STAB", + "STAG", "STAN", "STAR", "STAY", "STEM", "STEW", "STIR", "STOW", "STUB", + "STUN", "SUCH", "SUDS", "SUIT", "SULK", "SUMS", "SUNG", "SUNK", "SURE", + "SURF", "SWAB", "SWAG", "SWAM", "SWAN", "SWAT", "SWAY", "SWIM", "SWUM", + "TACK", "TACT", "TAIL", "TAKE", "TALE", "TALK", "TALL", "TANK", "TASK", + "TATE", "TAUT", "TEAL", "TEAM", "TEAR", "TECH", "TEEM", "TEEN", "TEET", + "TELL", "TEND", "TENT", "TERM", "TERN", "TESS", "TEST", "THAN", "THAT", + "THEE", "THEM", "THEN", "THEY", "THIN", "THIS", "THUD", "THUG", "TICK", + "TIDE", "TIDY", "TIED", "TIER", "TILE", "TILL", "TILT", "TIME", "TINA", + "TINE", "TINT", "TINY", "TIRE", "TOAD", "TOGO", "TOIL", "TOLD", "TOLL", + "TONE", "TONG", "TONY", "TOOK", "TOOL", "TOOT", "TORE", "TORN", "TOTE", + "TOUR", "TOUT", "TOWN", "TRAG", "TRAM", "TRAY", "TREE", "TREK", "TRIG", + "TRIM", "TRIO", "TROD", "TROT", "TROY", "TRUE", "TUBA", "TUBE", "TUCK", + "TUFT", "TUNA", "TUNE", "TUNG", "TURF", "TURN", "TUSK", "TWIG", "TWIN", + "TWIT", "ULAN", "UNIT", "URGE", "USED", "USER", "USES", "UTAH", "VAIL", + "VAIN", "VALE", "VARY", "VASE", "VAST", "VEAL", "VEDA", "VEIL", "VEIN", + "VEND", "VENT", "VERB", "VERY", "VETO", "VICE", "VIEW", "VINE", "VISE", + "VOID", "VOLT", "VOTE", "WACK", "WADE", "WAGE", "WAIL", "WAIT", "WAKE", + "WALE", "WALK", "WALL", "WALT", "WAND", "WANE", "WANG", "WANT", "WARD", + "WARM", "WARN", "WART", "WASH", "WAST", "WATS", "WATT", "WAVE", "WAVY", + "WAYS", "WEAK", "WEAL", "WEAN", "WEAR", "WEED", "WEEK", "WEIR", "WELD", + "WELL", "WELT", "WENT", "WERE", "WERT", "WEST", "WHAM", "WHAT", "WHEE", + "WHEN", "WHET", "WHOA", "WHOM", "WICK", "WIFE", "WILD", "WILL", "WIND", + "WINE", "WING", "WINK", "WINO", "WIRE", "WISE", "WISH", "WITH", "WOLF", + "WONT", "WOOD", "WOOL", "WORD", "WORE", "WORK", "WORM", "WORN", "WOVE", + "WRIT", "WYNN", "YALE", "YANG", "YANK", "YARD", "YARN", "YAWL", "YAWN", + "YEAH", "YEAR", "YELL", "YOGA", "YOKE"}; /* Extract 'length' bits from the char array 's' starting with bit 'start' */ -unsigned long RFC1751::extract (char const* s, int start, int length) +unsigned long +RFC1751::extract(char const* s, int start, int length) { unsigned char cl; unsigned char cc; unsigned char cr; unsigned long x; - assert (length <= 11); - assert (start >= 0); - assert (length >= 0); - assert (start + length <= 66); + assert(length <= 11); + assert(start >= 0); + assert(length >= 0); + assert(start + length <= 66); int const shiftR = 24 - (length + (start % 8)); - cl = s[start / 8]; // get components + cl = s[start / 8]; // get components cc = (shiftR < 16) ? s[start / 8 + 1] : 0; cr = (shiftR < 8) ? s[start / 8 + 2] : 0; - x = ((long) (cl << 8 | cc) << 8 | cr) ; // Put bits together - x = x >> shiftR; // Right justify number - x = ( x & (0xffff >> (16 - length) ) ); // Trim extra bits. + x = ((long)(cl << 8 | cc) << 8 | cr); // Put bits together + x = x >> shiftR; // Right justify number + x = (x & (0xffff >> (16 - length))); // Trim extra bits. return x; } // Encode 8 bytes in 'c' as a string of English words. // Returns a pointer to a static buffer -void RFC1751::btoe (std::string& strHuman, std::string const& strData) +void +RFC1751::btoe(std::string& strHuman, std::string const& strData) { - char caBuffer[9]; /* add in room for the parity 2 bits*/ - int p, i; + char caBuffer[9]; /* add in room for the parity 2 bits*/ + int p, i; - memcpy (caBuffer, strData.c_str (), 8); + memcpy(caBuffer, strData.c_str(), 8); // compute parity: merely add groups of two bits. for (p = 0, i = 0; i < 64; i += 2) - p += extract (caBuffer, i, 2); + p += extract(caBuffer, i, 2); - caBuffer[8] = char (p) << 6; + caBuffer[8] = char(p) << 6; - strHuman = std::string () - + s_dictionary[extract (caBuffer, 0, 11)] + " " - + s_dictionary[extract (caBuffer, 11, 11)] + " " - + s_dictionary[extract (caBuffer, 22, 11)] + " " - + s_dictionary[extract (caBuffer, 33, 11)] + " " - + s_dictionary[extract (caBuffer, 44, 11)] + " " - + s_dictionary[extract (caBuffer, 55, 11)]; + strHuman = std::string() + s_dictionary[extract(caBuffer, 0, 11)] + " " + + s_dictionary[extract(caBuffer, 11, 11)] + " " + + s_dictionary[extract(caBuffer, 22, 11)] + " " + + s_dictionary[extract(caBuffer, 33, 11)] + " " + + s_dictionary[extract(caBuffer, 44, 11)] + " " + + s_dictionary[extract(caBuffer, 55, 11)]; } -void RFC1751::insert (char* s, int x, int start, int length) +void +RFC1751::insert(char* s, int x, int start, int length) { unsigned char cl; unsigned char cc; @@ -314,13 +320,13 @@ void RFC1751::insert (char* s, int x, int start, int length) unsigned long y; int shift; - assert (length <= 11); - assert (start >= 0); - assert (length >= 0); - assert (start + length <= 66); + assert(length <= 11); + assert(start >= 0); + assert(length >= 0); + assert(start + length <= 66); - shift = ((8 - (( start + length) % 8)) % 8); - y = (long) x << shift; + shift = ((8 - ((start + length) % 8)) % 8); + y = (long)x << shift; cl = (y >> 16) & 0xff; cc = (y >> 8) & 0xff; cr = y & 0xff; @@ -342,12 +348,13 @@ void RFC1751::insert (char* s, int x, int start, int length) } } -void RFC1751::standard (std::string& strWord) +void +RFC1751::standard(std::string& strWord) { for (auto& letter : strWord) { - if (islower (static_cast(letter))) - letter = toupper (static_cast(letter)); + if (islower(static_cast(letter))) + letter = toupper(static_cast(letter)); else if (letter == '1') letter = 'L'; else if (letter == '0') @@ -358,27 +365,28 @@ void RFC1751::standard (std::string& strWord) } // Binary search of dictionary. -int RFC1751::wsrch (std::string const& strWord, int iMin, int iMax) +int +RFC1751::wsrch(std::string const& strWord, int iMin, int iMax) { int iResult = -1; while (iResult < 0 && iMin != iMax) { // Have a range to search. - int iMid = iMin + (iMax - iMin) / 2; - int iDir = strWord.compare (s_dictionary[iMid]); + int iMid = iMin + (iMax - iMin) / 2; + int iDir = strWord.compare(s_dictionary[iMid]); if (!iDir) { - iResult = iMid; // Found it. + iResult = iMid; // Found it. } else if (iDir < 0) { - iMax = iMid; // key < middle, middle is new max. + iMax = iMid; // key < middle, middle is new max. } else { - iMin = iMid + 1; // key > middle, new min is past the middle. + iMin = iMid + 1; // key > middle, new min is past the middle. } } @@ -391,42 +399,41 @@ int RFC1751::wsrch (std::string const& strWord, int iMin, int iMax) // 0 word not in data base // -1 badly formed in put ie > 4 char word // -2 words OK but parity is wrong -int RFC1751::etob (std::string& strData, std::vector vsHuman) +int +RFC1751::etob(std::string& strData, std::vector vsHuman) { - if (6 != vsHuman.size ()) + if (6 != vsHuman.size()) return -1; int i, p = 0; - char b[9] = { 0 }; + char b[9] = {0}; for (auto& strWord : vsHuman) { - int l = strWord.length (); + int l = strWord.length(); if (l > 4 || l < 1) return -1; - standard (strWord); + standard(strWord); - auto v = wsrch (strWord, - l < 4 ? 0 : 571, - l < 4 ? 570 : 2048); + auto v = wsrch(strWord, l < 4 ? 0 : 571, l < 4 ? 570 : 2048); - if (v < 0 ) + if (v < 0) return 0; - insert (b, v, p, 11); + insert(b, v, p, 11); p += 11; } /* now check the parity of what we got */ for (p = 0, i = 0; i < 64; i += 2) - p += extract (b, i, 2); + p += extract(b, i, 2); - if ( (p & 3) != extract (b, 64, 2) ) + if ((p & 3) != extract(b, 64, 2)) return -2; - strData.assign (b, 8); + strData.assign(b, 8); return 1; } @@ -439,47 +446,52 @@ int RFC1751::etob (std::string& strData, std::vector vsHuman) -1 if badly formed string -2 if words are okay but parity is wrong. */ -int RFC1751::getKeyFromEnglish (std::string& strKey, std::string const& strHuman) +int +RFC1751::getKeyFromEnglish(std::string& strKey, std::string const& strHuman) { std::vector vWords; std::string strFirst, strSecond; int rc = 0; - std::string strTrimmed (strHuman); + std::string strTrimmed(strHuman); - boost::algorithm::trim (strTrimmed); + boost::algorithm::trim(strTrimmed); - boost::algorithm::split (vWords, strTrimmed, - boost::algorithm::is_space (), boost::algorithm::token_compress_on); + boost::algorithm::split( + vWords, + strTrimmed, + boost::algorithm::is_space(), + boost::algorithm::token_compress_on); - rc = 12 == vWords.size () ? 1 : -1; + rc = 12 == vWords.size() ? 1 : -1; if (1 == rc) - rc = etob (strFirst, vWords | boost::adaptors::copied (0, 6)); + rc = etob(strFirst, vWords | boost::adaptors::copied(0, 6)); if (1 == rc) - rc = etob (strSecond, vWords | boost::adaptors::copied (6, 12)); + rc = etob(strSecond, vWords | boost::adaptors::copied(6, 12)); if (1 == rc) - strKey = strFirst + strSecond; + strKey = strFirst + strSecond; return rc; } /** Convert to human from a 128 bit key in big-endian format -*/ -void RFC1751::getEnglishFromKey (std::string& strHuman, std::string const& strKey) + */ +void +RFC1751::getEnglishFromKey(std::string& strHuman, std::string const& strKey) { std::string strFirst, strSecond; - btoe (strFirst, strKey.substr (0, 8)); - btoe (strSecond, strKey.substr (8, 8)); + btoe(strFirst, strKey.substr(0, 8)); + btoe(strSecond, strKey.substr(8, 8)); - strHuman = strFirst + " " + strSecond; + strHuman = strFirst + " " + strSecond; } std::string -RFC1751::getWordFromBlob (void const* blob, size_t bytes) +RFC1751::getWordFromBlob(void const* blob, size_t bytes) { // This is a simple implementation of the Jenkins one-at-a-time hash // algorithm: @@ -498,7 +510,8 @@ RFC1751::getWordFromBlob (void const* blob, size_t bytes) hash ^= (hash >> 11); hash += (hash << 15); - return s_dictionary [hash % (sizeof (s_dictionary) / sizeof (s_dictionary [0]))]; + return s_dictionary + [hash % (sizeof(s_dictionary) / sizeof(s_dictionary[0]))]; } -} // ripple +} // namespace ripple diff --git a/src/ripple/crypto/impl/csprng.cpp b/src/ripple/crypto/impl/csprng.cpp index 0f1f310c21..a166fe2880 100644 --- a/src/ripple/crypto/impl/csprng.cpp +++ b/src/ripple/crypto/impl/csprng.cpp @@ -17,41 +17,40 @@ */ //============================================================================== -#include #include +#include #include -#include #include #include +#include #include #include namespace ripple { void -csprng_engine::mix ( - void* data, std::size_t size, double bitsPerByte) +csprng_engine::mix(void* data, std::size_t size, double bitsPerByte) { - assert (data != nullptr); - assert (size != 0); - assert (bitsPerByte != 0); + assert(data != nullptr); + assert(size != 0); + assert(bitsPerByte != 0); - std::lock_guard lock (mutex_); - RAND_add (data, size, (size * bitsPerByte) / 8.0); + std::lock_guard lock(mutex_); + RAND_add(data, size, (size * bitsPerByte) / 8.0); } -csprng_engine::csprng_engine () +csprng_engine::csprng_engine() { - mix_entropy (); + mix_entropy(); } -csprng_engine::~csprng_engine () +csprng_engine::~csprng_engine() { - RAND_cleanup (); + RAND_cleanup(); } void -csprng_engine::mix_entropy (void* buffer, std::size_t count) +csprng_engine::mix_entropy(void* buffer, std::size_t count) { std::array entropy; @@ -66,8 +65,7 @@ csprng_engine::mix_entropy (void* buffer, std::size_t count) } // Assume 2 bits per byte for the system entropy: - mix ( - entropy.data(), + mix(entropy.data(), entropy.size() * sizeof(std::random_device::result_type), 2.0); @@ -75,7 +73,7 @@ csprng_engine::mix_entropy (void* buffer, std::size_t count) // how much entropy the buffer the user gives us contains // and assume only 0.5 bits of entropy per byte: if (buffer != nullptr && count != 0) - mix (buffer, count, 0.5); + mix(buffer, count, 0.5); } csprng_engine::result_type @@ -83,35 +81,34 @@ csprng_engine::operator()() { result_type ret; - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - auto const result = RAND_bytes ( - reinterpret_cast(&ret), - sizeof(ret)); + auto const result = + RAND_bytes(reinterpret_cast(&ret), sizeof(ret)); if (result == 0) - Throw ("Insufficient entropy"); + Throw("Insufficient entropy"); return ret; } void -csprng_engine::operator()(void *ptr, std::size_t count) +csprng_engine::operator()(void* ptr, std::size_t count) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); - auto const result = RAND_bytes ( - reinterpret_cast(ptr), - count); + auto const result = + RAND_bytes(reinterpret_cast(ptr), count); if (result != 1) - Throw ("Insufficient entropy"); + Throw("Insufficient entropy"); } -csprng_engine& crypto_prng() +csprng_engine& +crypto_prng() { static csprng_engine engine; return engine; } -} +} // namespace ripple diff --git a/src/ripple/crypto/impl/ec_key.cpp b/src/ripple/crypto/impl/ec_key.cpp index 6602a61149..74ea1f54d4 100644 --- a/src/ripple/crypto/impl/ec_key.cpp +++ b/src/ripple/crypto/impl/ec_key.cpp @@ -26,15 +26,16 @@ #include #include -namespace ripple { +namespace ripple { namespace openssl { -static inline EC_KEY* get_EC_KEY (const ec_key& that) +static inline EC_KEY* +get_EC_KEY(const ec_key& that) { - return (EC_KEY*) that.get(); + return (EC_KEY*)that.get(); } -ec_key::ec_key (const ec_key& that) +ec_key::ec_key(const ec_key& that) { if (that.ptr == nullptr) { @@ -42,22 +43,23 @@ ec_key::ec_key (const ec_key& that) return; } - ptr = (pointer_t) EC_KEY_dup (get_EC_KEY (that)); + ptr = (pointer_t)EC_KEY_dup(get_EC_KEY(that)); if (ptr == nullptr) - Throw ("ec_key::ec_key() : EC_KEY_dup failed"); + Throw("ec_key::ec_key() : EC_KEY_dup failed"); - EC_KEY_set_conv_form (get_EC_KEY (*this), POINT_CONVERSION_COMPRESSED); + EC_KEY_set_conv_form(get_EC_KEY(*this), POINT_CONVERSION_COMPRESSED); } -void ec_key::destroy() +void +ec_key::destroy() { if (ptr != nullptr) { - EC_KEY_free (get_EC_KEY (*this)); + EC_KEY_free(get_EC_KEY(*this)); ptr = nullptr; } } -} // openssl -} // ripple +} // namespace openssl +} // namespace ripple diff --git a/src/ripple/crypto/impl/ec_key.h b/src/ripple/crypto/impl/ec_key.h index cad4ead7ad..c5f06e29e4 100644 --- a/src/ripple/crypto/impl/ec_key.h +++ b/src/ripple/crypto/impl/ec_key.h @@ -31,11 +31,11 @@ class ec_key public: using pointer_t = struct opaque_EC_KEY*; - ec_key () : ptr(nullptr) + ec_key() : ptr(nullptr) { } - ec_key (pointer_t raw) : ptr(raw) + ec_key(pointer_t raw) : ptr(raw) { } @@ -44,16 +44,22 @@ public: destroy(); } - bool valid() const + bool + valid() const { return ptr != nullptr; } - pointer_t get() const { return ptr; } + pointer_t + get() const + { + return ptr; + } - ec_key (const ec_key&); + ec_key(const ec_key&); - pointer_t release() + pointer_t + release() { pointer_t released = ptr; @@ -65,12 +71,14 @@ public: private: pointer_t ptr; - void destroy(); + void + destroy(); - ec_key& operator= (const ec_key&) = delete; + ec_key& + operator=(const ec_key&) = delete; }; -} // openssl -} // ripple +} // namespace openssl +} // namespace ripple #endif diff --git a/src/ripple/crypto/impl/openssl.cpp b/src/ripple/crypto/impl/openssl.cpp index 8792af58bc..dac26a47ab 100644 --- a/src/ripple/crypto/impl/openssl.cpp +++ b/src/ripple/crypto/impl/openssl.cpp @@ -21,115 +21,120 @@ #include #include -namespace ripple { +namespace ripple { namespace openssl { bignum::bignum() { ptr = BN_new(); if (ptr == nullptr) - Throw ("BN_new() failed"); + Throw("BN_new() failed"); } -void bignum::assign (uint8_t const* data, size_t size) +void +bignum::assign(uint8_t const* data, size_t size) { // This reuses and assigns ptr - BIGNUM* bn = BN_bin2bn (data, size, ptr); + BIGNUM* bn = BN_bin2bn(data, size, ptr); if (bn == nullptr) - Throw ("BN_bin2bn() failed"); + Throw("BN_bin2bn() failed"); } -void bignum::assign_new (uint8_t const* data, size_t size) +void +bignum::assign_new(uint8_t const* data, size_t size) { // ptr must not be allocated - ptr = BN_bin2bn (data, size, nullptr); + ptr = BN_bin2bn(data, size, nullptr); if (ptr == nullptr) - Throw ("BN_bin2bn() failed"); + Throw("BN_bin2bn() failed"); } bn_ctx::bn_ctx() { ptr = BN_CTX_new(); if (ptr == nullptr) - Throw ("BN_CTX_new() failed"); + Throw("BN_CTX_new() failed"); } -bignum get_order (EC_GROUP const* group, bn_ctx& ctx) +bignum +get_order(EC_GROUP const* group, bn_ctx& ctx) { bignum result; - if (! EC_GROUP_get_order (group, result.get(), ctx.get())) - Throw ("EC_GROUP_get_order() failed"); + if (!EC_GROUP_get_order(group, result.get(), ctx.get())) + Throw("EC_GROUP_get_order() failed"); return result; } -ec_point::ec_point (EC_GROUP const* group) +ec_point::ec_point(EC_GROUP const* group) { - ptr = EC_POINT_new (group); + ptr = EC_POINT_new(group); if (ptr == nullptr) - Throw ("EC_POINT_new() failed"); + Throw("EC_POINT_new() failed"); } -void add_to (EC_GROUP const* group, - ec_point const& a, - ec_point& b, - bn_ctx& ctx) +void +add_to(EC_GROUP const* group, ec_point const& a, ec_point& b, bn_ctx& ctx) { - if (!EC_POINT_add (group, b.get(), a.get(), b.get(), ctx.get())) - Throw ("EC_POINT_add() failed"); + if (!EC_POINT_add(group, b.get(), a.get(), b.get(), ctx.get())) + Throw("EC_POINT_add() failed"); } -ec_point multiply (EC_GROUP const* group, - bignum const& n, - bn_ctx& ctx) +ec_point +multiply(EC_GROUP const* group, bignum const& n, bn_ctx& ctx) { - ec_point result (group); - if (! EC_POINT_mul (group, result.get(), n.get(), nullptr, nullptr, ctx.get())) - Throw ("EC_POINT_mul() failed"); + ec_point result(group); + if (!EC_POINT_mul( + group, result.get(), n.get(), nullptr, nullptr, ctx.get())) + Throw("EC_POINT_mul() failed"); return result; } -ec_point bn2point (EC_GROUP const* group, BIGNUM const* number) +ec_point +bn2point(EC_GROUP const* group, BIGNUM const* number) { - EC_POINT* result = EC_POINT_bn2point (group, number, nullptr, nullptr); + EC_POINT* result = EC_POINT_bn2point(group, number, nullptr, nullptr); if (result == nullptr) - Throw ("EC_POINT_bn2point() failed"); + Throw("EC_POINT_bn2point() failed"); - return ec_point::acquire (result); + return ec_point::acquire(result); } -static ec_key ec_key_new_secp256k1_compressed() +static ec_key +ec_key_new_secp256k1_compressed() { - EC_KEY* key = EC_KEY_new_by_curve_name (NID_secp256k1); + EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1); - if (key == nullptr) Throw ("EC_KEY_new_by_curve_name() failed"); + if (key == nullptr) + Throw("EC_KEY_new_by_curve_name() failed"); - EC_KEY_set_conv_form (key, POINT_CONVERSION_COMPRESSED); + EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED); - return ec_key((ec_key::pointer_t) key); + return ec_key((ec_key::pointer_t)key); } -void serialize_ec_point (ec_point const& point, std::uint8_t* ptr) +void +serialize_ec_point(ec_point const& point, std::uint8_t* ptr) { ec_key key = ec_key_new_secp256k1_compressed(); - if (EC_KEY_set_public_key((EC_KEY*) key.get(), point.get()) <= 0) - Throw ("EC_KEY_set_public_key() failed"); + if (EC_KEY_set_public_key((EC_KEY*)key.get(), point.get()) <= 0) + Throw("EC_KEY_set_public_key() failed"); - int const size = i2o_ECPublicKey ((EC_KEY*) key.get(), &ptr); + int const size = i2o_ECPublicKey((EC_KEY*)key.get(), &ptr); - assert (size <= 33); - (void) size; + assert(size <= 33); + (void)size; } -} // openssl -} // ripple +} // namespace openssl +} // namespace ripple #include #ifdef _MSC_VER FILE _iob[] = {*stdin, *stdout, *stderr}; -extern "C" FILE * __cdecl __iob_func(void) +extern "C" FILE* __cdecl __iob_func(void) { return _iob; } diff --git a/src/ripple/crypto/impl/openssl.h b/src/ripple/crypto/impl/openssl.h index 68cffed420..58892b2583 100644 --- a/src/ripple/crypto/impl/openssl.h +++ b/src/ripple/crypto/impl/openssl.h @@ -25,7 +25,7 @@ #include #include -namespace ripple { +namespace ripple { namespace openssl { class bignum @@ -34,73 +34,96 @@ private: BIGNUM* ptr; // non-copyable - bignum (bignum const&) = delete; - bignum& operator=(bignum const&) = delete; + bignum(bignum const&) = delete; + bignum& + operator=(bignum const&) = delete; - void assign_new (uint8_t const* data, size_t size); + void + assign_new(uint8_t const* data, size_t size); public: bignum(); ~bignum() { - if ( ptr != nullptr) + if (ptr != nullptr) { - BN_free (ptr); + BN_free(ptr); } } - bignum (uint8_t const* data, size_t size) + bignum(uint8_t const* data, size_t size) { - assign_new (data, size); + assign_new(data, size); } template - explicit bignum (T const& thing) + explicit bignum(T const& thing) { - assign_new (thing.data(), thing.size()); + assign_new(thing.data(), thing.size()); } - bignum(bignum&& that) noexcept : ptr( that.ptr ) + bignum(bignum&& that) noexcept : ptr(that.ptr) { that.ptr = nullptr; } - bignum& operator= (bignum&& that) noexcept + bignum& + operator=(bignum&& that) noexcept { using std::swap; - swap( ptr, that.ptr ); + swap(ptr, that.ptr); return *this; } - BIGNUM * get() { return ptr; } - BIGNUM const* get() const { return ptr; } + BIGNUM* + get() + { + return ptr; + } + BIGNUM const* + get() const + { + return ptr; + } - bool is_zero() const { return BN_is_zero (ptr); } + bool + is_zero() const + { + return BN_is_zero(ptr); + } - void clear() { BN_clear (ptr); } + void + clear() + { + BN_clear(ptr); + } - void assign (uint8_t const* data, size_t size); + void + assign(uint8_t const* data, size_t size); }; -inline bool operator< (bignum const& a, bignum const& b) +inline bool +operator<(bignum const& a, bignum const& b) { - return BN_cmp (a.get(), b.get()) < 0; + return BN_cmp(a.get(), b.get()) < 0; } -inline bool operator>= (bignum const& a, bignum const& b) +inline bool +operator>=(bignum const& a, bignum const& b) { return !(a < b); } -inline uint256 uint256_from_bignum_clear (bignum& number) +inline uint256 +uint256_from_bignum_clear(bignum& number) { uint256 result; result.zero(); - BN_bn2bin (number.get(), result.end() - BN_num_bytes (number.get())); + BN_bn2bin(number.get(), result.end() - BN_num_bytes(number.get())); number.clear(); @@ -113,29 +136,37 @@ private: BN_CTX* ptr; // non-copyable - bn_ctx (bn_ctx const&); - bn_ctx& operator=(bn_ctx const&); + bn_ctx(bn_ctx const&); + bn_ctx& + operator=(bn_ctx const&); public: bn_ctx(); ~bn_ctx() { - BN_CTX_free (ptr); + BN_CTX_free(ptr); } - BN_CTX * get() { return ptr; } - BN_CTX const* get() const { return ptr; } + BN_CTX* + get() + { + return ptr; + } + BN_CTX const* + get() const + { + return ptr; + } }; -bignum get_order (EC_GROUP const* group, bn_ctx& ctx); +bignum +get_order(EC_GROUP const* group, bn_ctx& ctx); -inline void add_to (bignum const& a, - bignum& b, - bignum const& modulus, - bn_ctx& ctx) +inline void +add_to(bignum const& a, bignum& b, bignum const& modulus, bn_ctx& ctx) { - BN_mod_add (b.get(), a.get(), b.get(), modulus.get(), ctx.get()); + BN_mod_add(b.get(), a.get(), b.get(), modulus.get(), ctx.get()); } class ec_point @@ -146,48 +177,60 @@ public: private: pointer_t ptr; - ec_point (pointer_t raw) : ptr(raw) + ec_point(pointer_t raw) : ptr(raw) { } public: - static ec_point acquire (pointer_t raw) + static ec_point + acquire(pointer_t raw) { - return ec_point (raw); + return ec_point(raw); } - ec_point (EC_GROUP const* group); + ec_point(EC_GROUP const* group); - ~ec_point() { EC_POINT_free (ptr); } + ~ec_point() + { + EC_POINT_free(ptr); + } - ec_point (ec_point const&) = delete; - ec_point& operator=(ec_point const&) = delete; + ec_point(ec_point const&) = delete; + ec_point& + operator=(ec_point const&) = delete; ec_point(ec_point&& that) noexcept { - ptr = that.ptr; + ptr = that.ptr; that.ptr = nullptr; } - EC_POINT * get() { return ptr; } - EC_POINT const* get() const { return ptr; } + EC_POINT* + get() + { + return ptr; + } + EC_POINT const* + get() const + { + return ptr; + } }; -void add_to (EC_GROUP const* group, - ec_point const& a, - ec_point& b, - bn_ctx& ctx); +void +add_to(EC_GROUP const* group, ec_point const& a, ec_point& b, bn_ctx& ctx); -ec_point multiply (EC_GROUP const* group, - bignum const& n, - bn_ctx& ctx); +ec_point +multiply(EC_GROUP const* group, bignum const& n, bn_ctx& ctx); -ec_point bn2point (EC_GROUP const* group, BIGNUM const* number); +ec_point +bn2point(EC_GROUP const* group, BIGNUM const* number); // output buffer must hold 33 bytes -void serialize_ec_point (ec_point const& point, std::uint8_t* ptr); +void +serialize_ec_point(ec_point const& point, std::uint8_t* ptr); -} // openssl -} // ripple +} // namespace openssl +} // namespace ripple #endif diff --git a/src/ripple/json/JsonPropertyStream.h b/src/ripple/json/JsonPropertyStream.h index 7e4658e9c3..0f25e59eb5 100644 --- a/src/ripple/json/JsonPropertyStream.h +++ b/src/ripple/json/JsonPropertyStream.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_JSON_JSONPROPERTYSTREAM_H_INCLUDED #define RIPPLE_JSON_JSONPROPERTYSTREAM_H_INCLUDED -#include #include +#include namespace ripple { @@ -30,39 +30,61 @@ class JsonPropertyStream : public beast::PropertyStream { public: Json::Value m_top; - std::vector m_stack; + std::vector m_stack; public: - JsonPropertyStream (); - Json::Value const& top() const; + JsonPropertyStream(); + Json::Value const& + top() const; protected: + void + map_begin() override; + void + map_begin(std::string const& key) override; + void + map_end() override; + void + add(std::string const& key, short value) override; + void + add(std::string const& key, unsigned short value) override; + void + add(std::string const& key, int value) override; + void + add(std::string const& key, unsigned int value) override; + void + add(std::string const& key, long value) override; + void + add(std::string const& key, float v) override; + void + add(std::string const& key, double v) override; + void + add(std::string const& key, std::string const& v) override; + void + array_begin() override; + void + array_begin(std::string const& key) override; + void + array_end() override; - void map_begin () override; - void map_begin (std::string const& key) override; - void map_end () override; - void add (std::string const& key, short value) override; - void add (std::string const& key, unsigned short value) override; - void add (std::string const& key, int value) override; - void add (std::string const& key, unsigned int value) override; - void add (std::string const& key, long value) override; - void add (std::string const& key, float v) override; - void add (std::string const& key, double v) override; - void add (std::string const& key, std::string const& v) override; - void array_begin () override; - void array_begin (std::string const& key) override; - void array_end () override; - - void add (short value) override; - void add (unsigned short value) override; - void add (int value) override; - void add (unsigned int value) override; - void add (long value) override; - void add (float v) override; - void add (double v) override; - void add (std::string const& v) override; + void + add(short value) override; + void + add(unsigned short value) override; + void + add(int value) override; + void + add(unsigned int value) override; + void + add(long value) override; + void + add(float v) override; + void + add(double v) override; + void + add(std::string const& v) override; }; -} +} // namespace ripple #endif diff --git a/src/ripple/json/Object.h b/src/ripple/json/Object.h index f847940a85..0450103333 100644 --- a/src/ripple/json/Object.h +++ b/src/ripple/json/Object.h @@ -39,8 +39,8 @@ namespace Json { 1. Only one collection can be open for change at any one time. - This condition is enforced automatically and a std::logic_error thrown if it - is violated. + This condition is enforced automatically and a std::logic_error thrown if + it is violated. 2. A tag may only be used once in an Object. @@ -151,8 +151,9 @@ namespace Json { class Collection { public: - Collection (Collection&& c) noexcept; - Collection& operator= (Collection&& c) noexcept; + Collection(Collection&& c) noexcept; + Collection& + operator=(Collection&& c) noexcept; Collection() = delete; ~Collection(); @@ -160,8 +161,9 @@ public: protected: // A null parent means "no parent at all". // Writers cannot be null. - Collection (Collection* parent, Writer*); - void checkWritable (std::string const& label); + Collection(Collection* parent, Writer*); + void + checkWritable(std::string const& label); Collection* parent_; Writer* writer_; @@ -193,40 +195,48 @@ public: An operator[] is provided to allow writing `object["key"] = scalar;`. */ template - void set (std::string const& key, Scalar const&); + void + set(std::string const& key, Scalar const&); - void set (std::string const& key, Json::Value const&); + void + set(std::string const& key, Json::Value const&); // Detail class and method used to implement operator[]. class Proxy; - Proxy operator[] (std::string const& key); - Proxy operator[] (Json::StaticString const& key); + Proxy + operator[](std::string const& key); + Proxy + operator[](Json::StaticString const& key); /** Make a new Object at a key and return it. This Object is disabled until that sub-object is destroyed. Throws an exception if this Object was already disabled. */ - Object setObject (std::string const& key); + Object + setObject(std::string const& key); /** Make a new Array at a key and return it. This Object is disabled until that sub-array is destroyed. Throws an exception if this Object was already disabled. */ - Array setArray (std::string const& key); + Array + setArray(std::string const& key); protected: friend class Array; - Object (Collection* parent, Writer* w) : Collection (parent, w) {} + Object(Collection* parent, Writer* w) : Collection(parent, w) + { + } }; class Object::Root : public Object { - public: +public: /** Each Object::Root must be constructed with its own unique Writer. */ - Root (Writer&); + Root(Writer&); }; //------------------------------------------------------------------------------ @@ -241,31 +251,37 @@ public: its sub-collections is enabled). */ template - void append (Scalar const&); + void + append(Scalar const&); /** Appends a Json::Value to an array. Throws an exception if this Array was disabled. */ - void append (Json::Value const&); + void + append(Json::Value const&); /** Append a new Object and return it. This Array is disabled until that sub-object is destroyed. Throws an exception if this Array was disabled. */ - Object appendObject (); + Object + appendObject(); /** Append a new Array and return it. This Array is disabled until that sub-array is destroyed. Throws an exception if this Array was already disabled. */ - Array appendArray (); + Array + appendArray(); - protected: +protected: friend class Object; - Array (Collection* parent, Writer* w) : Collection (parent, w) {} + Array(Collection* parent, Writer* w) : Collection(parent, w) + { + } }; //------------------------------------------------------------------------------ @@ -274,68 +290,76 @@ public: // interoperate. /** Add a new subarray at a named key in a Json object. */ -Json::Value& setArray (Json::Value&, Json::StaticString const& key); +Json::Value& +setArray(Json::Value&, Json::StaticString const& key); /** Add a new subarray at a named key in a Json object. */ -Array setArray (Object&, Json::StaticString const& key); - +Array +setArray(Object&, Json::StaticString const& key); /** Add a new subobject at a named key in a Json object. */ -Json::Value& addObject (Json::Value&, Json::StaticString const& key); +Json::Value& +addObject(Json::Value&, Json::StaticString const& key); /** Add a new subobject at a named key in a Json object. */ -Object addObject (Object&, Json::StaticString const& key); - +Object +addObject(Object&, Json::StaticString const& key); /** Append a new subarray to a Json array. */ -Json::Value& appendArray (Json::Value&); +Json::Value& +appendArray(Json::Value&); /** Append a new subarray to a Json array. */ -Array appendArray (Array&); - +Array +appendArray(Array&); /** Append a new subobject to a Json object. */ -Json::Value& appendObject (Json::Value&); +Json::Value& +appendObject(Json::Value&); /** Append a new subobject to a Json object. */ -Object appendObject (Array&); - +Object +appendObject(Array&); /** Copy all the keys and values from one object into another. */ -void copyFrom (Json::Value& to, Json::Value const& from); +void +copyFrom(Json::Value& to, Json::Value const& from); /** Copy all the keys and values from one object into another. */ -void copyFrom (Object& to, Json::Value const& from); - +void +copyFrom(Object& to, Json::Value const& from); /** An Object that contains its own Writer. */ class WriterObject { public: - WriterObject (Output const& output) - : writer_ (std::make_unique (output)), - object_ (std::make_unique (*writer_)) + WriterObject(Output const& output) + : writer_(std::make_unique(output)) + , object_(std::make_unique(*writer_)) { } - WriterObject (WriterObject&& other) = default; + WriterObject(WriterObject&& other) = default; - Object* operator->() + Object* + operator->() { return object_.get(); } - Object& operator*() + Object& + operator*() { return *object_; } private: - std::unique_ptr writer_; + std::unique_ptr writer_; std::unique_ptr object_; }; -WriterObject stringWriterObject (std::string&); +WriterObject +stringWriterObject(std::string&); //------------------------------------------------------------------------------ // Implementation details. @@ -348,12 +372,13 @@ private: std::string const key_; public: - Proxy (Object& object, std::string const& key); + Proxy(Object& object, std::string const& key); template - void operator= (T const& t) + void + operator=(T const& t) { - object_.set (key_, t); + object_.set(key_, t); // Note: This function shouldn't return *this, because it's a trap. // // In Json::Value, foo[jss::key] returns a reference to a @@ -368,69 +393,71 @@ public: //------------------------------------------------------------------------------ template -void Array::append (Scalar const& value) +void +Array::append(Scalar const& value) { - checkWritable ("append"); + checkWritable("append"); if (writer_) - writer_->append (value); + writer_->append(value); } template -void Object::set (std::string const& key, Scalar const& value) +void +Object::set(std::string const& key, Scalar const& value) { - checkWritable ("set"); + checkWritable("set"); if (writer_) - writer_->set (key, value); + writer_->set(key, value); } -inline -Json::Value& setArray (Json::Value& json, Json::StaticString const& key) +inline Json::Value& +setArray(Json::Value& json, Json::StaticString const& key) { return (json[key] = Json::arrayValue); } -inline -Array setArray (Object& json, Json::StaticString const& key) +inline Array +setArray(Object& json, Json::StaticString const& key) { - return json.setArray (std::string (key)); + return json.setArray(std::string(key)); } -inline -Json::Value& addObject (Json::Value& json, Json::StaticString const& key) +inline Json::Value& +addObject(Json::Value& json, Json::StaticString const& key) { return (json[key] = Json::objectValue); } -inline -Object addObject (Object& object, Json::StaticString const& key) +inline Object +addObject(Object& object, Json::StaticString const& key) { - return object.setObject (std::string (key)); + return object.setObject(std::string(key)); } -inline -Json::Value& appendArray (Json::Value& json) +inline Json::Value& +appendArray(Json::Value& json) { - return json.append (Json::arrayValue); + return json.append(Json::arrayValue); } -inline -Array appendArray (Array& json) +inline Array +appendArray(Array& json) { - return json.appendArray (); + return json.appendArray(); } -inline -Json::Value& appendObject (Json::Value& json) +inline Json::Value& +appendObject(Json::Value& json) { - return json.append (Json::objectValue); + return json.append(Json::objectValue); } -inline -Object appendObject (Array& json) +inline Object +appendObject(Array& json) { - return json.appendObject (); + return json.appendObject(); } -} // Json +} // namespace Json #endif diff --git a/src/ripple/json/Output.h b/src/ripple/json/Output.h index cc685d03be..74aaa65269 100644 --- a/src/ripple/json/Output.h +++ b/src/ripple/json/Output.h @@ -27,12 +27,14 @@ namespace Json { class Value; -using Output = std::function ; +using Output = std::function; -inline -Output stringOutput (std::string& s) +inline Output +stringOutput(std::string& s) { - return [&](boost::beast::string_view const& b) { s.append (b.data(), b.size()); }; + return [&](boost::beast::string_view const& b) { + s.append(b.data(), b.size()); + }; } /** Writes a minimal representation of a Json value to an Output in O(n) time. @@ -40,15 +42,17 @@ Output stringOutput (std::string& s) Data is streamed right to the output, so only a marginal amount of memory is used. This can be very important for a very large Json::Value. */ -void outputJson (Json::Value const&, Output const&); +void +outputJson(Json::Value const&, Output const&); /** Return the minimal string representation of a Json::Value in O(n) time. This requires a memory allocation for the full size of the output. If possible, use outputJson(). */ -std::string jsonAsString (Json::Value const&); +std::string +jsonAsString(Json::Value const&); -} // Json +} // namespace Json #endif diff --git a/src/ripple/json/Writer.h b/src/ripple/json/Writer.h index 962f7f7a8c..5801caf851 100644 --- a/src/ripple/json/Writer.h +++ b/src/ripple/json/Writer.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_JSON_WRITER_H_INCLUDED #define RIPPLE_JSON_WRITER_H_INCLUDED -#include #include +#include #include #include #include @@ -126,29 +126,33 @@ namespace Json { class Writer { public: - enum CollectionType {array, object}; + enum CollectionType { array, object }; - explicit Writer (Output const& output); + explicit Writer(Output const& output); Writer(Writer&&) noexcept; - Writer& operator=(Writer&&) noexcept; + Writer& + operator=(Writer&&) noexcept; ~Writer(); /** Start a new collection at the root level. */ - void startRoot (CollectionType); + void startRoot(CollectionType); /** Start a new collection inside an array. */ - void startAppend (CollectionType); + void startAppend(CollectionType); /** Start a new collection inside an object. */ - void startSet (CollectionType, std::string const& key); + void + startSet(CollectionType, std::string const& key); /** Finish the collection most recently started. */ - void finish (); + void + finish(); /** Finish all objects and arrays. After finishArray() has been called, no * more operations can be performed. */ - void finishAll (); + void + finishAll(); /** Append a value to an array. * @@ -156,15 +160,17 @@ public: * literal, nullptr or Json::Value */ template - void append (Scalar t) + void + append(Scalar t) { rawAppend(); - output (t); + output(t); } /** Add a comma before this next item if not the first item in an array. Useful if you are writing the actual array yourself. */ - void rawAppend(); + void + rawAppend(); /** Add a key, value assignment to an object. * @@ -178,65 +184,77 @@ public: * the tag you use has already been used in this object. */ template - void set (std::string const& tag, Type t) + void + set(std::string const& tag, Type t) { - rawSet (tag); - output (t); + rawSet(tag); + output(t); } /** Emit just "tag": as part of an object. Useful if you are writing the actual value data yourself. */ - void rawSet (std::string const& key); + void + rawSet(std::string const& key); // You won't need to call anything below here until you are writing single // items (numbers, strings, bools, null) to a JSON stream. /*** Output a string. */ - void output (std::string const&); + void + output(std::string const&); /*** Output a literal constant or C string. */ - void output (char const*); + void + output(char const*); /*** Output a Json::Value. */ - void output (Json::Value const&); + void + output(Json::Value const&); /** Output a null. */ - void output (std::nullptr_t); + void output(std::nullptr_t); /** Output a float. */ - void output (float); + void + output(float); /** Output a double. */ - void output (double); + void + output(double); /** Output a bool. */ - void output (bool); + void + output(bool); /** Output numbers or booleans. */ template - void output (Type t) + void + output(Type t) { - implOutput (std::to_string (t)); + implOutput(std::to_string(t)); } - void output (Json::StaticString const& t) + void + output(Json::StaticString const& t) { - output (t.c_str()); + output(t.c_str()); } private: class Impl; - std::unique_ptr impl_; + std::unique_ptr impl_; - void implOutput (std::string const&); + void + implOutput(std::string const&); }; -inline void check (bool condition, std::string const& message) +inline void +check(bool condition, std::string const& message) { - if (! condition) - ripple::Throw (message); + if (!condition) + ripple::Throw(message); } -} // Json +} // namespace Json #endif diff --git a/src/ripple/json/impl/JsonPropertyStream.cpp b/src/ripple/json/impl/JsonPropertyStream.cpp index 1080e2c313..3083deeeee 100644 --- a/src/ripple/json/impl/JsonPropertyStream.cpp +++ b/src/ripple/json/impl/JsonPropertyStream.cpp @@ -17,144 +17,165 @@ */ //============================================================================== -#include #include +#include namespace ripple { -JsonPropertyStream::JsonPropertyStream () - : m_top (Json::objectValue) +JsonPropertyStream::JsonPropertyStream() : m_top(Json::objectValue) { - m_stack.reserve (64); - m_stack.push_back (&m_top); + m_stack.reserve(64); + m_stack.push_back(&m_top); } -Json::Value const& JsonPropertyStream::top() const +Json::Value const& +JsonPropertyStream::top() const { return m_top; } -void JsonPropertyStream::map_begin () +void +JsonPropertyStream::map_begin() { // top is array - Json::Value& top (*m_stack.back()); - Json::Value& map (top.append (Json::objectValue)); - m_stack.push_back (&map); + Json::Value& top(*m_stack.back()); + Json::Value& map(top.append(Json::objectValue)); + m_stack.push_back(&map); } -void JsonPropertyStream::map_begin (std::string const& key) +void +JsonPropertyStream::map_begin(std::string const& key) { // top is a map - Json::Value& top (*m_stack.back()); - Json::Value& map (top [key] = Json::objectValue); - m_stack.push_back (&map); + Json::Value& top(*m_stack.back()); + Json::Value& map(top[key] = Json::objectValue); + m_stack.push_back(&map); } -void JsonPropertyStream::map_end () +void +JsonPropertyStream::map_end() { - m_stack.pop_back (); + m_stack.pop_back(); } -void JsonPropertyStream::add (std::string const& key, short v) +void +JsonPropertyStream::add(std::string const& key, short v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, unsigned short v) +void +JsonPropertyStream::add(std::string const& key, unsigned short v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, int v) +void +JsonPropertyStream::add(std::string const& key, int v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, unsigned int v) +void +JsonPropertyStream::add(std::string const& key, unsigned int v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, long v) +void +JsonPropertyStream::add(std::string const& key, long v) { (*m_stack.back())[key] = int(v); } -void JsonPropertyStream::add (std::string const& key, float v) +void +JsonPropertyStream::add(std::string const& key, float v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, double v) +void +JsonPropertyStream::add(std::string const& key, double v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::add (std::string const& key, std::string const& v) +void +JsonPropertyStream::add(std::string const& key, std::string const& v) { (*m_stack.back())[key] = v; } -void JsonPropertyStream::array_begin () +void +JsonPropertyStream::array_begin() { // top is array - Json::Value& top (*m_stack.back()); - Json::Value& vec (top.append (Json::arrayValue)); - m_stack.push_back (&vec); + Json::Value& top(*m_stack.back()); + Json::Value& vec(top.append(Json::arrayValue)); + m_stack.push_back(&vec); } -void JsonPropertyStream::array_begin (std::string const& key) +void +JsonPropertyStream::array_begin(std::string const& key) { // top is a map - Json::Value& top (*m_stack.back()); - Json::Value& vec (top [key] = Json::arrayValue); - m_stack.push_back (&vec); + Json::Value& top(*m_stack.back()); + Json::Value& vec(top[key] = Json::arrayValue); + m_stack.push_back(&vec); } -void JsonPropertyStream::array_end () +void +JsonPropertyStream::array_end() { - m_stack.pop_back (); + m_stack.pop_back(); } -void JsonPropertyStream::add (short v) +void +JsonPropertyStream::add(short v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (unsigned short v) +void +JsonPropertyStream::add(unsigned short v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (int v) +void +JsonPropertyStream::add(int v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (unsigned int v) +void +JsonPropertyStream::add(unsigned int v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (long v) +void +JsonPropertyStream::add(long v) { - m_stack.back()->append (int (v)); + m_stack.back()->append(int(v)); } -void JsonPropertyStream::add (float v) +void +JsonPropertyStream::add(float v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (double v) +void +JsonPropertyStream::add(double v) { - m_stack.back()->append (v); + m_stack.back()->append(v); } -void JsonPropertyStream::add (std::string const& v) +void +JsonPropertyStream::add(std::string const& v) { - m_stack.back()->append (v); -} - + m_stack.back()->append(v); } +} // namespace ripple diff --git a/src/ripple/json/impl/Object.cpp b/src/ripple/json/impl/Object.cpp index 2ae699713e..a855fc31a2 100644 --- a/src/ripple/json/impl/Object.cpp +++ b/src/ripple/json/impl/Object.cpp @@ -23,26 +23,27 @@ namespace Json { -Collection::Collection (Collection* parent, Writer* writer) - : parent_ (parent), writer_ (writer), enabled_ (true) +Collection::Collection(Collection* parent, Writer* writer) + : parent_(parent), writer_(writer), enabled_(true) { - checkWritable ("Collection::Collection()"); + checkWritable("Collection::Collection()"); if (parent_) { - check (parent_->enabled_, "Parent not enabled in constructor"); + check(parent_->enabled_, "Parent not enabled in constructor"); parent_->enabled_ = false; } } -Collection::~Collection () +Collection::~Collection() { if (writer_) - writer_->finish (); + writer_->finish(); if (parent_) parent_->enabled_ = true; } -Collection& Collection::operator= (Collection&& that) noexcept +Collection& +Collection::operator=(Collection&& that) noexcept { parent_ = that.parent_; writer_ = that.writer_; @@ -55,137 +56,154 @@ Collection& Collection::operator= (Collection&& that) noexcept return *this; } -Collection::Collection (Collection&& that) noexcept +Collection::Collection(Collection&& that) noexcept { - *this = std::move (that); + *this = std::move(that); } -void Collection::checkWritable (std::string const& label) +void +Collection::checkWritable(std::string const& label) { - if (! enabled_) - ripple::Throw (label + ": not enabled"); - if (! writer_) - ripple::Throw (label + ": not writable"); + if (!enabled_) + ripple::Throw(label + ": not enabled"); + if (!writer_) + ripple::Throw(label + ": not writable"); } //------------------------------------------------------------------------------ -Object::Root::Root (Writer& w) : Object (nullptr, &w) +Object::Root::Root(Writer& w) : Object(nullptr, &w) { - writer_->startRoot (Writer::object); + writer_->startRoot(Writer::object); } -Object Object::setObject (std::string const& key) +Object +Object::setObject(std::string const& key) { - checkWritable ("Object::setObject"); + checkWritable("Object::setObject"); if (writer_) - writer_->startSet (Writer::object, key); - return Object (this, writer_); + writer_->startSet(Writer::object, key); + return Object(this, writer_); } -Array Object::setArray (std::string const& key) { - checkWritable ("Object::setArray"); +Array +Object::setArray(std::string const& key) +{ + checkWritable("Object::setArray"); if (writer_) - writer_->startSet (Writer::array, key); - return Array (this, writer_); + writer_->startSet(Writer::array, key); + return Array(this, writer_); } //------------------------------------------------------------------------------ -Object Array::appendObject () +Object +Array::appendObject() { - checkWritable ("Array::appendObject"); + checkWritable("Array::appendObject"); if (writer_) - writer_->startAppend (Writer::object); - return Object (this, writer_); + writer_->startAppend(Writer::object); + return Object(this, writer_); } -Array Array::appendArray () +Array +Array::appendArray() { - checkWritable ("Array::makeArray"); + checkWritable("Array::makeArray"); if (writer_) - writer_->startAppend (Writer::array); - return Array (this, writer_); + writer_->startAppend(Writer::array); + return Array(this, writer_); } //------------------------------------------------------------------------------ -Object::Proxy::Proxy (Object& object, std::string const& key) - : object_ (object) - , key_ (key) +Object::Proxy::Proxy(Object& object, std::string const& key) + : object_(object), key_(key) { } -Object::Proxy Object::operator[] (std::string const& key) +Object::Proxy +Object::operator[](std::string const& key) { - return Proxy (*this, key); + return Proxy(*this, key); } -Object::Proxy Object::operator[] (Json::StaticString const& key) +Object::Proxy +Object::operator[](Json::StaticString const& key) { - return Proxy (*this, std::string (key)); + return Proxy(*this, std::string(key)); } //------------------------------------------------------------------------------ -void Array::append (Json::Value const& v) +void +Array::append(Json::Value const& v) { auto t = v.type(); switch (t) { - case Json::nullValue: return append (nullptr); - case Json::intValue: return append (v.asInt()); - case Json::uintValue: return append (v.asUInt()); - case Json::realValue: return append (v.asDouble()); - case Json::stringValue: return append (v.asString()); - case Json::booleanValue: return append (v.asBool()); + case Json::nullValue: + return append(nullptr); + case Json::intValue: + return append(v.asInt()); + case Json::uintValue: + return append(v.asUInt()); + case Json::realValue: + return append(v.asDouble()); + case Json::stringValue: + return append(v.asString()); + case Json::booleanValue: + return append(v.asBool()); - case Json::objectValue: - { - auto object = appendObject (); - copyFrom (object, v); - return; - } + case Json::objectValue: { + auto object = appendObject(); + copyFrom(object, v); + return; + } - case Json::arrayValue: - { - auto array = appendArray (); - for (auto& item: v) - array.append (item); - return; + case Json::arrayValue: { + auto array = appendArray(); + for (auto& item : v) + array.append(item); + return; + } } - } - assert (false); // Can't get here. + assert(false); // Can't get here. } -void Object::set (std::string const& k, Json::Value const& v) +void +Object::set(std::string const& k, Json::Value const& v) { auto t = v.type(); switch (t) { - case Json::nullValue: return set (k, nullptr); - case Json::intValue: return set (k, v.asInt()); - case Json::uintValue: return set (k, v.asUInt()); - case Json::realValue: return set (k, v.asDouble()); - case Json::stringValue: return set (k, v.asString()); - case Json::booleanValue: return set (k, v.asBool()); + case Json::nullValue: + return set(k, nullptr); + case Json::intValue: + return set(k, v.asInt()); + case Json::uintValue: + return set(k, v.asUInt()); + case Json::realValue: + return set(k, v.asDouble()); + case Json::stringValue: + return set(k, v.asString()); + case Json::booleanValue: + return set(k, v.asBool()); - case Json::objectValue: - { - auto object = setObject (k); - copyFrom (object, v); - return; - } + case Json::objectValue: { + auto object = setObject(k); + copyFrom(object, v); + return; + } - case Json::arrayValue: - { - auto array = setArray (k); - for (auto& item: v) - array.append (item); - return; + case Json::arrayValue: { + auto array = setArray(k); + for (auto& item : v) + array.append(item); + return; + } } - } - assert (false); // Can't get here. + assert(false); // Can't get here. } //------------------------------------------------------------------------------ @@ -193,32 +211,36 @@ void Object::set (std::string const& k, Json::Value const& v) namespace { template -void doCopyFrom (Object& to, Json::Value const& from) +void +doCopyFrom(Object& to, Json::Value const& from) { - assert (from.isObjectOrNull()); + assert(from.isObjectOrNull()); auto members = from.getMemberNames(); - for (auto& m: members) + for (auto& m : members) to[m] = from[m]; } -} +} // namespace -void copyFrom (Json::Value& to, Json::Value const& from) +void +copyFrom(Json::Value& to, Json::Value const& from) { if (!to) // Short circuit this very common case. to = from; else - doCopyFrom (to, from); + doCopyFrom(to, from); } -void copyFrom (Object& to, Json::Value const& from) +void +copyFrom(Object& to, Json::Value const& from) { - doCopyFrom (to, from); + doCopyFrom(to, from); } -WriterObject stringWriterObject (std::string& s) +WriterObject +stringWriterObject(std::string& s) { - return WriterObject (stringOutput (s)); + return WriterObject(stringOutput(s)); } -} // Json +} // namespace Json diff --git a/src/ripple/json/impl/Output.cpp b/src/ripple/json/impl/Output.cpp index 03bf32ed21..b32e9ad5a6 100644 --- a/src/ripple/json/impl/Output.cpp +++ b/src/ripple/json/impl/Output.cpp @@ -24,87 +24,82 @@ namespace Json { namespace { -void outputJson (Json::Value const& value, Writer& writer) +void +outputJson(Json::Value const& value, Writer& writer) { switch (value.type()) { - case Json::nullValue: - { - writer.output (nullptr); - break; - } - - case Json::intValue: - { - writer.output (value.asInt()); - break; - } - - case Json::uintValue: - { - writer.output (value.asUInt()); - break; - } - - case Json::realValue: - { - writer.output (value.asDouble()); - break; - } - - case Json::stringValue: - { - writer.output (value.asString()); - break; - } - - case Json::booleanValue: - { - writer.output (value.asBool()); - break; - } - - case Json::arrayValue: - { - writer.startRoot (Writer::array); - for (auto const& i: value) - { - writer.rawAppend(); - outputJson (i, writer); + case Json::nullValue: { + writer.output(nullptr); + break; } - writer.finish(); - break; - } - case Json::objectValue: - { - writer.startRoot (Writer::object); - auto members = value.getMemberNames (); - for (auto const& tag: members) - { - writer.rawSet (tag); - outputJson (value[tag], writer); + case Json::intValue: { + writer.output(value.asInt()); + break; } - writer.finish(); - break; - } - } // switch + + case Json::uintValue: { + writer.output(value.asUInt()); + break; + } + + case Json::realValue: { + writer.output(value.asDouble()); + break; + } + + case Json::stringValue: { + writer.output(value.asString()); + break; + } + + case Json::booleanValue: { + writer.output(value.asBool()); + break; + } + + case Json::arrayValue: { + writer.startRoot(Writer::array); + for (auto const& i : value) + { + writer.rawAppend(); + outputJson(i, writer); + } + writer.finish(); + break; + } + + case Json::objectValue: { + writer.startRoot(Writer::object); + auto members = value.getMemberNames(); + for (auto const& tag : members) + { + writer.rawSet(tag); + outputJson(value[tag], writer); + } + writer.finish(); + break; + } + } // switch } -} // namespace +} // namespace -void outputJson (Json::Value const& value, Output const& out) +void +outputJson(Json::Value const& value, Output const& out) { - Writer writer (out); - outputJson (value, writer); + Writer writer(out); + outputJson(value, writer); } -std::string jsonAsString (Json::Value const& value) +std::string +jsonAsString(Json::Value const& value) { std::string s; - Writer writer (stringOutput (s)); - outputJson (value, writer); + Writer writer(stringOutput(s)); + outputJson(value, writer); return s; } -} // Json +} // namespace Json diff --git a/src/ripple/json/impl/Writer.cpp b/src/ripple/json/impl/Writer.cpp index 27aac6f800..98c8debe96 100644 --- a/src/ripple/json/impl/Writer.cpp +++ b/src/ripple/json/impl/Writer.cpp @@ -19,23 +19,22 @@ #include #include -#include #include +#include namespace Json { namespace { -std::map jsonSpecialCharacterEscape = { - {'"', "\\\""}, +std::map jsonSpecialCharacterEscape = { + {'"', "\\\""}, {'\\', "\\\\"}, - {'/', "\\/"}, + {'/', "\\/"}, {'\b', "\\b"}, {'\f', "\\f"}, {'\n', "\\n"}, {'\r', "\\r"}, - {'\t', "\\t"} -}; + {'\t', "\\t"}}; static size_t const jsonEscapeLength = 2; @@ -52,13 +51,14 @@ const std::string none; static auto const integralFloatsBecomeInts = false; -size_t lengthWithoutTrailingZeros (std::string const& s) +size_t +lengthWithoutTrailingZeros(std::string const& s) { - auto dotPos = s.find ('.'); + auto dotPos = s.find('.'); if (dotPos == std::string::npos) return s.size(); - auto lastNonZero = s.find_last_not_of ('0'); + auto lastNonZero = s.find_last_not_of('0'); auto hasDecimals = dotPos != lastNonZero; if (hasDecimals) @@ -70,110 +70,127 @@ size_t lengthWithoutTrailingZeros (std::string const& s) return lastNonZero + 2; } -} // namespace +} // namespace class Writer::Impl { public: - explicit - Impl (Output const& output) : output_(output) {} + explicit Impl(Output const& output) : output_(output) + { + } ~Impl() = default; Impl(Impl&&) = delete; - Impl& operator=(Impl&&) = delete; + Impl& + operator=(Impl&&) = delete; - bool empty() const { return stack_.empty (); } + bool + empty() const + { + return stack_.empty(); + } - void start (CollectionType ct) + void + start(CollectionType ct) { char ch = (ct == array) ? openBracket : openBrace; - output ({&ch, 1}); - stack_.push (Collection()); + output({&ch, 1}); + stack_.push(Collection()); stack_.top().type = ct; } - void output (boost::beast::string_view const& bytes) + void + output(boost::beast::string_view const& bytes) { - markStarted (); - output_ (bytes); + markStarted(); + output_(bytes); } - void stringOutput (boost::beast::string_view const& bytes) + void + stringOutput(boost::beast::string_view const& bytes) { - markStarted (); + markStarted(); std::size_t position = 0, writtenUntil = 0; - output_ ({"e, 1}); + output_({"e, 1}); auto data = bytes.data(); for (; position < bytes.size(); ++position) { - auto i = jsonSpecialCharacterEscape.find (data[position]); - if (i != jsonSpecialCharacterEscape.end ()) + auto i = jsonSpecialCharacterEscape.find(data[position]); + if (i != jsonSpecialCharacterEscape.end()) { if (writtenUntil < position) { - output_ ({data + writtenUntil, position - writtenUntil}); + output_({data + writtenUntil, position - writtenUntil}); } - output_ ({i->second, jsonEscapeLength}); + output_({i->second, jsonEscapeLength}); writtenUntil = position + 1; }; } if (writtenUntil < position) - output_ ({data + writtenUntil, position - writtenUntil}); - output_ ({"e, 1}); + output_({data + writtenUntil, position - writtenUntil}); + output_({"e, 1}); } - void markStarted () + void + markStarted() { - check (!isFinished(), "isFinished() in output."); + check(!isFinished(), "isFinished() in output."); isStarted_ = true; } - void nextCollectionEntry (CollectionType type, std::string const& message) + void + nextCollectionEntry(CollectionType type, std::string const& message) { - check (!empty() , "empty () in " + message); + check(!empty(), "empty () in " + message); - auto t = stack_.top ().type; + auto t = stack_.top().type; if (t != type) { - check (false, "Not an " + - ((type == array ? "array: " : "object: ") + message)); + check( + false, + "Not an " + + ((type == array ? "array: " : "object: ") + message)); } - if (stack_.top ().isFirst) - stack_.top ().isFirst = false; + if (stack_.top().isFirst) + stack_.top().isFirst = false; else - output_ ({&comma, 1}); + output_({&comma, 1}); } - void writeObjectTag (std::string const& tag) + void + writeObjectTag(std::string const& tag) { #ifndef NDEBUG // Make sure we haven't already seen this tag. - auto& tags = stack_.top ().tags; - check (tags.find (tag) == tags.end (), "Already seen tag " + tag); - tags.insert (tag); + auto& tags = stack_.top().tags; + check(tags.find(tag) == tags.end(), "Already seen tag " + tag); + tags.insert(tag); #endif - stringOutput (tag); - output_ ({&colon, 1}); + stringOutput(tag); + output_({&colon, 1}); } - bool isFinished() const + bool + isFinished() const { return isStarted_ && empty(); } - void finish () + void + finish() { - check (!empty(), "Empty stack in finish()"); + check(!empty(), "Empty stack in finish()"); auto isArray = stack_.top().type == array; auto ch = isArray ? closeBracket : closeBrace; - output_ ({&ch, 1}); + output_({&ch, 1}); stack_.pop(); } - void finishAll () + void + finishAll() { if (isStarted_) { @@ -182,7 +199,11 @@ public: } } - Output const& getOutput() const { return output_; } + Output const& + getOutput() const + { + return output_; + } private: // JSON collections are either arrrays, or objects. @@ -199,11 +220,11 @@ private: #ifndef NDEBUG /** What tags have we already seen in this collection? */ - std::set tags; + std::set tags; #endif }; - using Stack = std::stack >; + using Stack = std::stack>; Output output_; Stack stack_; @@ -211,112 +232,126 @@ private: bool isStarted_ = false; }; -Writer::Writer (Output const &output) - : impl_(std::make_unique (output)) +Writer::Writer(Output const& output) : impl_(std::make_unique(output)) { } Writer::~Writer() { if (impl_) - impl_->finishAll (); + impl_->finishAll(); } Writer::Writer(Writer&& w) noexcept { - impl_ = std::move (w.impl_); + impl_ = std::move(w.impl_); } -Writer& Writer::operator=(Writer&& w) noexcept +Writer& +Writer::operator=(Writer&& w) noexcept { - impl_ = std::move (w.impl_); + impl_ = std::move(w.impl_); return *this; } -void Writer::output (char const* s) +void +Writer::output(char const* s) { - impl_->stringOutput (s); + impl_->stringOutput(s); } -void Writer::output (std::string const& s) +void +Writer::output(std::string const& s) { - impl_->stringOutput (s); + impl_->stringOutput(s); } -void Writer::output (Json::Value const& value) +void +Writer::output(Json::Value const& value) { impl_->markStarted(); - outputJson (value, impl_->getOutput()); + outputJson(value, impl_->getOutput()); } -void Writer::output (float f) +void +Writer::output(float f) { - auto s = ripple::to_string (f); - impl_->output ({s.data (), lengthWithoutTrailingZeros (s)}); + auto s = ripple::to_string(f); + impl_->output({s.data(), lengthWithoutTrailingZeros(s)}); } -void Writer::output (double f) +void +Writer::output(double f) { - auto s = ripple::to_string (f); - impl_->output ({s.data (), lengthWithoutTrailingZeros (s)}); + auto s = ripple::to_string(f); + impl_->output({s.data(), lengthWithoutTrailingZeros(s)}); } -void Writer::output (std::nullptr_t) +void Writer::output(std::nullptr_t) { - impl_->output ("null"); + impl_->output("null"); } -void Writer::output (bool b) +void +Writer::output(bool b) { - impl_->output (b ? "true" : "false"); + impl_->output(b ? "true" : "false"); } -void Writer::implOutput (std::string const& s) +void +Writer::implOutput(std::string const& s) { - impl_->output (s); + impl_->output(s); } -void Writer::finishAll () +void +Writer::finishAll() { if (impl_) - impl_->finishAll (); + impl_->finishAll(); } -void Writer::rawAppend() +void +Writer::rawAppend() { - impl_->nextCollectionEntry (array, "append"); + impl_->nextCollectionEntry(array, "append"); } -void Writer::rawSet (std::string const& tag) +void +Writer::rawSet(std::string const& tag) { - check (!tag.empty(), "Tag can't be empty"); + check(!tag.empty(), "Tag can't be empty"); - impl_->nextCollectionEntry (object, "set"); - impl_->writeObjectTag (tag); + impl_->nextCollectionEntry(object, "set"); + impl_->writeObjectTag(tag); } -void Writer::startRoot (CollectionType type) +void +Writer::startRoot(CollectionType type) { - impl_->start (type); + impl_->start(type); } -void Writer::startAppend (CollectionType type) +void +Writer::startAppend(CollectionType type) { - impl_->nextCollectionEntry (array, "startAppend"); - impl_->start (type); + impl_->nextCollectionEntry(array, "startAppend"); + impl_->start(type); } -void Writer::startSet (CollectionType type, std::string const& key) +void +Writer::startSet(CollectionType type, std::string const& key) { - impl_->nextCollectionEntry (object, "startSet"); - impl_->writeObjectTag (key); - impl_->start (type); + impl_->nextCollectionEntry(object, "startSet"); + impl_->writeObjectTag(key); + impl_->start(type); } -void Writer::finish () +void +Writer::finish() { if (impl_) - impl_->finish (); + impl_->finish(); } -} // Json +} // namespace Json diff --git a/src/ripple/json/impl/json_assert.h b/src/ripple/json/impl/json_assert.h index c4af5c0443..b7297e016c 100644 --- a/src/ripple/json/impl/json_assert.h +++ b/src/ripple/json/impl/json_assert.h @@ -22,8 +22,11 @@ #include -#define JSON_ASSERT_UNREACHABLE assert( false ) -#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw -#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) ripple::Throw ( message ); +#define JSON_ASSERT_UNREACHABLE assert(false) +#define JSON_ASSERT(condition) \ + assert(condition); // @todo <= change this into an exception throw +#define JSON_ASSERT_MESSAGE(condition, message) \ + if (!(condition)) \ + ripple::Throw(message); #endif diff --git a/src/ripple/json/impl/json_reader.cpp b/src/ripple/json/impl/json_reader.cpp index ef1b2b8a73..91dd77cfa3 100644 --- a/src/ripple/json/impl/json_reader.cpp +++ b/src/ripple/json/impl/json_reader.cpp @@ -20,19 +20,17 @@ #include #include #include -#include #include +#include -namespace Json -{ +namespace Json { // Implementation of class Reader // //////////////////////////////// constexpr unsigned Reader::nest_limit; -static -std::string -codePointToUTF8 (unsigned int cp) +static std::string +codePointToUTF8(unsigned int cp) { std::string result; @@ -40,92 +38,89 @@ codePointToUTF8 (unsigned int cp) if (cp <= 0x7f) { - result.resize (1); - result[0] = static_cast (cp); + result.resize(1); + result[0] = static_cast(cp); } else if (cp <= 0x7FF) { - result.resize (2); - result[1] = static_cast (0x80 | (0x3f & cp)); - result[0] = static_cast (0xC0 | (0x1f & (cp >> 6))); + result.resize(2); + result[1] = static_cast(0x80 | (0x3f & cp)); + result[0] = static_cast(0xC0 | (0x1f & (cp >> 6))); } else if (cp <= 0xFFFF) { - result.resize (3); - result[2] = static_cast (0x80 | (0x3f & cp)); - result[1] = 0x80 | static_cast ((0x3f & (cp >> 6))); - result[0] = 0xE0 | static_cast ((0xf & (cp >> 12))); + result.resize(3); + result[2] = static_cast(0x80 | (0x3f & cp)); + result[1] = 0x80 | static_cast((0x3f & (cp >> 6))); + result[0] = 0xE0 | static_cast((0xf & (cp >> 12))); } else if (cp <= 0x10FFFF) { - result.resize (4); - result[3] = static_cast (0x80 | (0x3f & cp)); - result[2] = static_cast (0x80 | (0x3f & (cp >> 6))); - result[1] = static_cast (0x80 | (0x3f & (cp >> 12))); - result[0] = static_cast (0xF0 | (0x7 & (cp >> 18))); + result.resize(4); + result[3] = static_cast(0x80 | (0x3f & cp)); + result[2] = static_cast(0x80 | (0x3f & (cp >> 6))); + result[1] = static_cast(0x80 | (0x3f & (cp >> 12))); + result[0] = static_cast(0xF0 | (0x7 & (cp >> 18))); } return result; } - // Class Reader // ////////////////////////////////////////////////////////////////// bool -Reader::parse ( std::string const& document, - Value& root) +Reader::parse(std::string const& document, Value& root) { document_ = document; - const char* begin = document_.c_str (); - const char* end = begin + document_.length (); - return parse ( begin, end, root ); + const char* begin = document_.c_str(); + const char* end = begin + document_.length(); + return parse(begin, end, root); } - bool -Reader::parse ( std::istream& sin, - Value& root) +Reader::parse(std::istream& sin, Value& root) { - //std::istream_iterator begin(sin); - //std::istream_iterator end; + // std::istream_iterator begin(sin); + // std::istream_iterator end; // Those would allow streamed input from a file, if parse() were a // template function. // Since std::string is reference-counted, this at least does not // create an extra copy. std::string doc; - std::getline (sin, doc, (char)EOF); - return parse ( doc, root ); + std::getline(sin, doc, (char)EOF); + return parse(doc, root); } bool -Reader::parse ( const char* beginDoc, const char* endDoc, - Value& root) +Reader::parse(const char* beginDoc, const char* endDoc, Value& root) { begin_ = beginDoc; end_ = endDoc; current_ = begin_; lastValueEnd_ = 0; lastValue_ = 0; - errors_.clear (); + errors_.clear(); - while ( !nodes_.empty () ) - nodes_.pop (); + while (!nodes_.empty()) + nodes_.pop(); - nodes_.push ( &root ); + nodes_.push(&root); bool successful = readValue(0); Token token; - skipCommentTokens ( token ); + skipCommentTokens(token); - if ( !root.isNull() && !root.isArray() && !root.isObject() ) + if (!root.isNull() && !root.isArray() && !root.isObject()) { - // Set error location to start of doc, ideally should be first token found in doc + // Set error location to start of doc, ideally should be first token + // found in doc token.type_ = tokenError; token.start_ = beginDoc; token.end_ = endDoc; - addError ( "A valid JSON document must be either an array or an object value.", - token ); + addError( + "A valid JSON document must be either an array or an object value.", + token); return false; } @@ -136,236 +131,228 @@ bool Reader::readValue(unsigned depth) { Token token; - skipCommentTokens ( token ); + skipCommentTokens(token); if (depth > nest_limit) return addError("Syntax error: maximum nesting depth exceeded", token); bool successful = true; - switch ( token.type_ ) + switch (token.type_) { - case tokenObjectBegin: - successful = readObject(token, depth); - break; + case tokenObjectBegin: + successful = readObject(token, depth); + break; - case tokenArrayBegin: - successful = readArray(token, depth); - break; + case tokenArrayBegin: + successful = readArray(token, depth); + break; - case tokenInteger: - successful = decodeNumber ( token ); - break; + case tokenInteger: + successful = decodeNumber(token); + break; - case tokenDouble: - successful = decodeDouble ( token ); - break; + case tokenDouble: + successful = decodeDouble(token); + break; - case tokenString: - successful = decodeString ( token ); - break; + case tokenString: + successful = decodeString(token); + break; - case tokenTrue: - currentValue () = true; - break; + case tokenTrue: + currentValue() = true; + break; - case tokenFalse: - currentValue () = false; - break; + case tokenFalse: + currentValue() = false; + break; - case tokenNull: - currentValue () = Value (); - break; + case tokenNull: + currentValue() = Value(); + break; - default: - return addError ( "Syntax error: value, object or array expected.", token ); + default: + return addError( + "Syntax error: value, object or array expected.", token); } return successful; } - void -Reader::skipCommentTokens ( Token& token ) +Reader::skipCommentTokens(Token& token) { do { - readToken ( token ); - } - while ( token.type_ == tokenComment ); + readToken(token); + } while (token.type_ == tokenComment); } - bool -Reader::expectToken ( TokenType type, Token& token, const char* message ) +Reader::expectToken(TokenType type, Token& token, const char* message) { - readToken ( token ); + readToken(token); - if ( token.type_ != type ) - return addError ( message, token ); + if (token.type_ != type) + return addError(message, token); return true; } - bool -Reader::readToken ( Token& token ) +Reader::readToken(Token& token) { - skipSpaces (); + skipSpaces(); token.start_ = current_; - Char c = getNextChar (); + Char c = getNextChar(); bool ok = true; - switch ( c ) + switch (c) { - case '{': - token.type_ = tokenObjectBegin; - break; + case '{': + token.type_ = tokenObjectBegin; + break; - case '}': - token.type_ = tokenObjectEnd; - break; + case '}': + token.type_ = tokenObjectEnd; + break; - case '[': - token.type_ = tokenArrayBegin; - break; + case '[': + token.type_ = tokenArrayBegin; + break; - case ']': - token.type_ = tokenArrayEnd; - break; + case ']': + token.type_ = tokenArrayEnd; + break; - case '"': - token.type_ = tokenString; - ok = readString (); - break; + case '"': + token.type_ = tokenString; + ok = readString(); + break; - case '/': - token.type_ = tokenComment; - ok = readComment (); - break; + case '/': + token.type_ = tokenComment; + ok = readComment(); + break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - case '-': - token.type_ = readNumber (); - break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '-': + token.type_ = readNumber(); + break; - case 't': - token.type_ = tokenTrue; - ok = match ( "rue", 3 ); - break; + case 't': + token.type_ = tokenTrue; + ok = match("rue", 3); + break; - case 'f': - token.type_ = tokenFalse; - ok = match ( "alse", 4 ); - break; + case 'f': + token.type_ = tokenFalse; + ok = match("alse", 4); + break; - case 'n': - token.type_ = tokenNull; - ok = match ( "ull", 3 ); - break; + case 'n': + token.type_ = tokenNull; + ok = match("ull", 3); + break; - case ',': - token.type_ = tokenArraySeparator; - break; + case ',': + token.type_ = tokenArraySeparator; + break; - case ':': - token.type_ = tokenMemberSeparator; - break; + case ':': + token.type_ = tokenMemberSeparator; + break; - case 0: - token.type_ = tokenEndOfStream; - break; + case 0: + token.type_ = tokenEndOfStream; + break; - default: - ok = false; - break; + default: + ok = false; + break; } - if ( !ok ) + if (!ok) token.type_ = tokenError; token.end_ = current_; return true; } - void -Reader::skipSpaces () +Reader::skipSpaces() { - while ( current_ != end_ ) + while (current_ != end_) { Char c = *current_; - if ( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) + if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++current_; else break; } } - bool -Reader::match ( Location pattern, - int patternLength ) +Reader::match(Location pattern, int patternLength) { - if ( end_ - current_ < patternLength ) + if (end_ - current_ < patternLength) return false; int index = patternLength; - while ( index-- ) - if ( current_[index] != pattern[index] ) + while (index--) + if (current_[index] != pattern[index]) return false; current_ += patternLength; return true; } - bool -Reader::readComment () +Reader::readComment() { - Char c = getNextChar (); + Char c = getNextChar(); - if ( c == '*' ) - return readCStyleComment (); + if (c == '*') + return readCStyleComment(); - if ( c == '/' ) - return readCppStyleComment (); + if (c == '/') + return readCppStyleComment(); return false; } bool -Reader::readCStyleComment () +Reader::readCStyleComment() { - while ( current_ != end_ ) + while (current_ != end_) { - Char c = getNextChar (); + Char c = getNextChar(); - if ( c == '*' && *current_ == '/' ) + if (c == '*' && *current_ == '/') break; } - return getNextChar () == '/'; + return getNextChar() == '/'; } - bool -Reader::readCppStyleComment () +Reader::readCppStyleComment() { - while ( current_ != end_ ) + while (current_ != end_) { - Char c = getNextChar (); + Char c = getNextChar(); - if ( c == '\r' || c == '\n' ) + if (c == '\r' || c == '\n') break; } @@ -373,25 +360,27 @@ Reader::readCppStyleComment () } Reader::TokenType -Reader::readNumber () +Reader::readNumber() { - static char const extended_tokens[] = { '.', 'e', 'E', '+', '-' }; + static char const extended_tokens[] = {'.', 'e', 'E', '+', '-'}; TokenType type = tokenInteger; - if ( current_ != end_ ) + if (current_ != end_) { if (*current_ == '-') ++current_; - while ( current_ != end_ ) + while (current_ != end_) { - if (!std::isdigit (static_cast(*current_))) + if (!std::isdigit(static_cast(*current_))) { - auto ret = std::find (std::begin (extended_tokens), - std::end (extended_tokens), *current_); + auto ret = std::find( + std::begin(extended_tokens), + std::end(extended_tokens), + *current_); - if (ret == std::end (extended_tokens)) + if (ret == std::end(extended_tokens)) break; type = tokenDouble; @@ -405,146 +394,143 @@ Reader::readNumber () } bool -Reader::readString () +Reader::readString() { Char c = 0; - while ( current_ != end_ ) + while (current_ != end_) { - c = getNextChar (); + c = getNextChar(); - if ( c == '\\' ) - getNextChar (); - else if ( c == '"' ) + if (c == '\\') + getNextChar(); + else if (c == '"') break; } return c == '"'; } - bool Reader::readObject(Token& tokenStart, unsigned depth) { Token tokenName; std::string name; - currentValue () = Value ( objectValue ); + currentValue() = Value(objectValue); - while ( readToken ( tokenName ) ) + while (readToken(tokenName)) { bool initialTokenOk = true; - while ( tokenName.type_ == tokenComment && initialTokenOk ) - initialTokenOk = readToken ( tokenName ); + while (tokenName.type_ == tokenComment && initialTokenOk) + initialTokenOk = readToken(tokenName); - if ( !initialTokenOk ) + if (!initialTokenOk) break; - if ( tokenName.type_ == tokenObjectEnd && name.empty () ) // empty object + if (tokenName.type_ == tokenObjectEnd && name.empty()) // empty object return true; - if ( tokenName.type_ != tokenString ) + if (tokenName.type_ != tokenString) break; name = ""; - if ( !decodeString ( tokenName, name ) ) - return recoverFromError ( tokenObjectEnd ); + if (!decodeString(tokenName, name)) + return recoverFromError(tokenObjectEnd); Token colon; - if ( !readToken ( colon ) || colon.type_ != tokenMemberSeparator ) + if (!readToken(colon) || colon.type_ != tokenMemberSeparator) { - return addErrorAndRecover ( "Missing ':' after object member name", - colon, - tokenObjectEnd ); + return addErrorAndRecover( + "Missing ':' after object member name", colon, tokenObjectEnd); } // Reject duplicate names - if (currentValue ().isMember (name)) - return addError ( "Key '" + name + "' appears twice.", tokenName ); + if (currentValue().isMember(name)) + return addError("Key '" + name + "' appears twice.", tokenName); - Value& value = currentValue ()[ name ]; - nodes_.push ( &value ); - bool ok = readValue(depth+1); - nodes_.pop (); + Value& value = currentValue()[name]; + nodes_.push(&value); + bool ok = readValue(depth + 1); + nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError ( tokenObjectEnd ); + if (!ok) // error already set + return recoverFromError(tokenObjectEnd); Token comma; - if ( !readToken ( comma ) - || ( comma.type_ != tokenObjectEnd && - comma.type_ != tokenArraySeparator && - comma.type_ != tokenComment ) ) + if (!readToken(comma) || + (comma.type_ != tokenObjectEnd && + comma.type_ != tokenArraySeparator && comma.type_ != tokenComment)) { - return addErrorAndRecover ( "Missing ',' or '}' in object declaration", - comma, - tokenObjectEnd ); + return addErrorAndRecover( + "Missing ',' or '}' in object declaration", + comma, + tokenObjectEnd); } bool finalizeTokenOk = true; - while ( comma.type_ == tokenComment && - finalizeTokenOk ) - finalizeTokenOk = readToken ( comma ); + while (comma.type_ == tokenComment && finalizeTokenOk) + finalizeTokenOk = readToken(comma); - if ( comma.type_ == tokenObjectEnd ) + if (comma.type_ == tokenObjectEnd) return true; } - return addErrorAndRecover ( "Missing '}' or object member name", - tokenName, - tokenObjectEnd ); + return addErrorAndRecover( + "Missing '}' or object member name", tokenName, tokenObjectEnd); } - bool Reader::readArray(Token& tokenStart, unsigned depth) { - currentValue () = Value ( arrayValue ); - skipSpaces (); + currentValue() = Value(arrayValue); + skipSpaces(); - if ( *current_ == ']' ) // empty array + if (*current_ == ']') // empty array { Token endArray; - readToken ( endArray ); + readToken(endArray); return true; } int index = 0; - while ( true ) + while (true) { - Value& value = currentValue ()[ index++ ]; - nodes_.push ( &value ); - bool ok = readValue(depth+1); - nodes_.pop (); + Value& value = currentValue()[index++]; + nodes_.push(&value); + bool ok = readValue(depth + 1); + nodes_.pop(); - if ( !ok ) // error already set - return recoverFromError ( tokenArrayEnd ); + if (!ok) // error already set + return recoverFromError(tokenArrayEnd); Token token; // Accept Comment after last item in the array. - ok = readToken ( token ); + ok = readToken(token); - while ( token.type_ == tokenComment && ok ) + while (token.type_ == tokenComment && ok) { - ok = readToken ( token ); + ok = readToken(token); } - bool badTokenType = ( token.type_ != tokenArraySeparator && - token.type_ != tokenArrayEnd ); + bool badTokenType = + (token.type_ != tokenArraySeparator && + token.type_ != tokenArrayEnd); - if ( !ok || badTokenType ) + if (!ok || badTokenType) { - return addErrorAndRecover ( "Missing ',' or ']' in array declaration", - token, - tokenArrayEnd ); + return addErrorAndRecover( + "Missing ',' or ']' in array declaration", + token, + tokenArrayEnd); } - if ( token.type_ == tokenArrayEnd ) + if (token.type_ == tokenArrayEnd) break; } @@ -552,35 +538,40 @@ Reader::readArray(Token& tokenStart, unsigned depth) } bool -Reader::decodeNumber ( Token& token ) +Reader::decodeNumber(Token& token) { Location current = token.start_; bool isNegative = *current == '-'; - if ( isNegative ) + if (isNegative) ++current; if (current == token.end_) { - return addError ( "'" + std::string ( token.start_, token.end_ ) + - "' is not a valid number.", token ); + return addError( + "'" + std::string(token.start_, token.end_) + + "' is not a valid number.", + token); } // The existing Json integers are 32-bit so using a 64-bit value here avoids // overflows in the conversion code below. std::int64_t value = 0; - static_assert(sizeof(value) > sizeof(Value::maxUInt), + static_assert( + sizeof(value) > sizeof(Value::maxUInt), "The JSON integer overflow logic will need to be reworked."); while (current < token.end_ && (value <= Value::maxUInt)) { Char c = *current++; - if ( c < '0' || c > '9' ) + if (c < '0' || c > '9') { - return addError ( "'" + std::string ( token.start_, token.end_ ) + - "' is not a number.", token ); + return addError( + "'" + std::string(token.start_, token.end_) + + "' is not a number.", + token); } value = (value * 10) + (c - '0'); @@ -589,157 +580,164 @@ Reader::decodeNumber ( Token& token ) // More tokens left -> input is larger than largest possible return value if (current != token.end_) { - return addError ( "'" + std::string ( token.start_, token.end_ ) + - "' exceeds the allowable range.", token ); + return addError( + "'" + std::string(token.start_, token.end_) + + "' exceeds the allowable range.", + token); } - if ( isNegative ) + if (isNegative) { value = -value; if (value < Value::minInt || value > Value::maxInt) { - return addError ( "'" + std::string ( token.start_, token.end_ ) + - "' exceeds the allowable range.", token ); + return addError( + "'" + std::string(token.start_, token.end_) + + "' exceeds the allowable range.", + token); } - currentValue () = static_cast( value ); + currentValue() = static_cast(value); } else { if (value > Value::maxUInt) { - return addError ( "'" + std::string ( token.start_, token.end_ ) + - "' exceeds the allowable range.", token ); + return addError( + "'" + std::string(token.start_, token.end_) + + "' exceeds the allowable range.", + token); } // If it's representable as a signed integer, construct it as one. - if ( value <= Value::maxInt ) - currentValue () = static_cast( value ); + if (value <= Value::maxInt) + currentValue() = static_cast(value); else - currentValue () = static_cast( value ); + currentValue() = static_cast(value); } return true; } bool -Reader::decodeDouble( Token &token ) +Reader::decodeDouble(Token& token) { double value = 0; const int bufferSize = 32; int count; int length = int(token.end_ - token.start_); // Sanity check to avoid buffer overflow exploits. - if (length < 0) { - return addError( "Unable to parse token length", token ); + if (length < 0) + { + return addError("Unable to parse token length", token); } // Avoid using a string constant for the format control string given to - // sscanf, as this can cause hard to debug crashes on OS X. See here for more - // info: + // sscanf, as this can cause hard to debug crashes on OS X. See here for + // more info: // // http://developer.apple.com/library/mac/#DOCUMENTATION/DeveloperTools/gcc-4.0.1/gcc/Incompatibilities.html char format[] = "%lf"; - if ( length <= bufferSize ) + if (length <= bufferSize) { - Char buffer[bufferSize+1]; - memcpy( buffer, token.start_, length ); + Char buffer[bufferSize + 1]; + memcpy(buffer, token.start_, length); buffer[length] = 0; - count = sscanf( buffer, format, &value ); + count = sscanf(buffer, format, &value); } else { - std::string buffer( token.start_, token.end_ ); - count = sscanf( buffer.c_str(), format, &value ); + std::string buffer(token.start_, token.end_); + count = sscanf(buffer.c_str(), format, &value); } - if ( count != 1 ) - return addError( "'" + std::string( token.start_, token.end_ ) + "' is not a number.", token ); + if (count != 1) + return addError( + "'" + std::string(token.start_, token.end_) + "' is not a number.", + token); currentValue() = value; return true; } - - bool -Reader::decodeString ( Token& token ) +Reader::decodeString(Token& token) { std::string decoded; - if ( !decodeString ( token, decoded ) ) + if (!decodeString(token, decoded)) return false; - currentValue () = decoded; + currentValue() = decoded; return true; } - bool -Reader::decodeString ( Token& token, std::string& decoded ) +Reader::decodeString(Token& token, std::string& decoded) { - decoded.reserve ( token.end_ - token.start_ - 2 ); - Location current = token.start_ + 1; // skip '"' - Location end = token.end_ - 1; // do not include '"' + decoded.reserve(token.end_ - token.start_ - 2); + Location current = token.start_ + 1; // skip '"' + Location end = token.end_ - 1; // do not include '"' - while ( current != end ) + while (current != end) { Char c = *current++; - if ( c == '"' ) + if (c == '"') break; - else if ( c == '\\' ) + else if (c == '\\') { - if ( current == end ) - return addError ( "Empty escape sequence in string", token, current ); + if (current == end) + return addError( + "Empty escape sequence in string", token, current); Char escape = *current++; - switch ( escape ) + switch (escape) { - case '"': - decoded += '"'; + case '"': + decoded += '"'; + break; + + case '/': + decoded += '/'; + break; + + case '\\': + decoded += '\\'; + break; + + case 'b': + decoded += '\b'; + break; + + case 'f': + decoded += '\f'; + break; + + case 'n': + decoded += '\n'; + break; + + case 'r': + decoded += '\r'; + break; + + case 't': + decoded += '\t'; + break; + + case 'u': { + unsigned int unicode; + + if (!decodeUnicodeCodePoint(token, current, end, unicode)) + return false; + + decoded += codePointToUTF8(unicode); + } break; - case '/': - decoded += '/'; - break; - - case '\\': - decoded += '\\'; - break; - - case 'b': - decoded += '\b'; - break; - - case 'f': - decoded += '\f'; - break; - - case 'n': - decoded += '\n'; - break; - - case 'r': - decoded += '\r'; - break; - - case 't': - decoded += '\t'; - break; - - case 'u': - { - unsigned int unicode; - - if ( !decodeUnicodeCodePoint ( token, current, end, unicode ) ) - return false; - - decoded += codePointToUTF8 (unicode); - } - break; - - default: - return addError ( "Bad escape sequence in string", token, current ); + default: + return addError( + "Bad escape sequence in string", token, current); } } else @@ -752,152 +750,161 @@ Reader::decodeString ( Token& token, std::string& decoded ) } bool -Reader::decodeUnicodeCodePoint ( Token& token, - Location& current, - Location end, - unsigned int& unicode ) +Reader::decodeUnicodeCodePoint( + Token& token, + Location& current, + Location end, + unsigned int& unicode) { - - if ( !decodeUnicodeEscapeSequence ( token, current, end, unicode ) ) + if (!decodeUnicodeEscapeSequence(token, current, end, unicode)) return false; if (unicode >= 0xD800 && unicode <= 0xDBFF) { // surrogate pairs if (end - current < 6) - return addError ( "additional six characters expected to parse unicode surrogate pair.", token, current ); + return addError( + "additional six characters expected to parse unicode surrogate " + "pair.", + token, + current); unsigned int surrogatePair; - if (* (current++) == '\\' && * (current++) == 'u') + if (*(current++) == '\\' && *(current++) == 'u') { - if (decodeUnicodeEscapeSequence ( token, current, end, surrogatePair )) + if (decodeUnicodeEscapeSequence(token, current, end, surrogatePair)) { - unicode = 0x10000 + ((unicode & 0x3FF) << 10) + (surrogatePair & 0x3FF); + unicode = 0x10000 + ((unicode & 0x3FF) << 10) + + (surrogatePair & 0x3FF); } else return false; } else - return addError ( "expecting another \\u token to begin the second half of a unicode surrogate pair", token, current ); + return addError( + "expecting another \\u token to begin the second half of a " + "unicode surrogate pair", + token, + current); } return true; } bool -Reader::decodeUnicodeEscapeSequence ( Token& token, - Location& current, - Location end, - unsigned int& unicode ) +Reader::decodeUnicodeEscapeSequence( + Token& token, + Location& current, + Location end, + unsigned int& unicode) { - if ( end - current < 4 ) - return addError ( "Bad unicode escape sequence in string: four digits expected.", token, current ); + if (end - current < 4) + return addError( + "Bad unicode escape sequence in string: four digits expected.", + token, + current); unicode = 0; - for ( int index = 0; index < 4; ++index ) + for (int index = 0; index < 4; ++index) { Char c = *current++; unicode *= 16; - if ( c >= '0' && c <= '9' ) + if (c >= '0' && c <= '9') unicode += c - '0'; - else if ( c >= 'a' && c <= 'f' ) + else if (c >= 'a' && c <= 'f') unicode += c - 'a' + 10; - else if ( c >= 'A' && c <= 'F' ) + else if (c >= 'A' && c <= 'F') unicode += c - 'A' + 10; else - return addError ( "Bad unicode escape sequence in string: hexadecimal digit expected.", token, current ); + return addError( + "Bad unicode escape sequence in string: hexadecimal digit " + "expected.", + token, + current); } return true; } - bool -Reader::addError ( std::string const& message, - Token& token, - Location extra ) +Reader::addError(std::string const& message, Token& token, Location extra) { ErrorInfo info; info.token_ = token; info.message_ = message; info.extra_ = extra; - errors_.push_back ( info ); + errors_.push_back(info); return false; } - bool -Reader::recoverFromError ( TokenType skipUntilToken ) +Reader::recoverFromError(TokenType skipUntilToken) { - int errorCount = int (errors_.size ()); + int errorCount = int(errors_.size()); Token skip; - while ( true ) + while (true) { - if ( !readToken (skip) ) - errors_.resize ( errorCount ); // discard errors caused by recovery + if (!readToken(skip)) + errors_.resize(errorCount); // discard errors caused by recovery - if ( skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream ) + if (skip.type_ == skipUntilToken || skip.type_ == tokenEndOfStream) break; } - errors_.resize ( errorCount ); + errors_.resize(errorCount); return false; } - bool -Reader::addErrorAndRecover ( std::string const& message, - Token& token, - TokenType skipUntilToken ) +Reader::addErrorAndRecover( + std::string const& message, + Token& token, + TokenType skipUntilToken) { - addError ( message, token ); - return recoverFromError ( skipUntilToken ); + addError(message, token); + return recoverFromError(skipUntilToken); } - Value& -Reader::currentValue () +Reader::currentValue() { - return * (nodes_.top ()); + return *(nodes_.top()); } - Reader::Char -Reader::getNextChar () +Reader::getNextChar() { - if ( current_ == end_ ) + if (current_ == end_) return 0; return *current_++; } - void -Reader::getLocationLineAndColumn ( Location location, - int& line, - int& column ) const +Reader::getLocationLineAndColumn(Location location, int& line, int& column) + const { Location current = begin_; Location lastLineStart = current; line = 0; - while ( current < location && current != end_ ) + while (current < location && current != end_) { Char c = *current++; - if ( c == '\r' ) + if (c == '\r') { - if ( *current == '\n' ) + if (*current == '\n') ++current; lastLineStart = current; ++line; } - else if ( c == '\n' ) + else if (c == '\n') { lastLineStart = current; ++line; @@ -905,54 +912,53 @@ Reader::getLocationLineAndColumn ( Location location, } // column & line start at 1 - column = int (location - lastLineStart) + 1; + column = int(location - lastLineStart) + 1; ++line; } - std::string -Reader::getLocationLineAndColumn ( Location location ) const +Reader::getLocationLineAndColumn(Location location) const { int line, column; - getLocationLineAndColumn ( location, line, column ); + getLocationLineAndColumn(location, line, column); char buffer[18 + 16 + 16 + 1]; - sprintf ( buffer, "Line %d, Column %d", line, column ); + sprintf(buffer, "Line %d, Column %d", line, column); return buffer; } - std::string -Reader::getFormatedErrorMessages () const +Reader::getFormatedErrorMessages() const { std::string formattedMessage; - for ( Errors::const_iterator itError = errors_.begin (); - itError != errors_.end (); - ++itError ) + for (Errors::const_iterator itError = errors_.begin(); + itError != errors_.end(); + ++itError) { const ErrorInfo& error = *itError; - formattedMessage += "* " + getLocationLineAndColumn ( error.token_.start_ ) + "\n"; + formattedMessage += + "* " + getLocationLineAndColumn(error.token_.start_) + "\n"; formattedMessage += " " + error.message_ + "\n"; - if ( error.extra_ ) - formattedMessage += "See " + getLocationLineAndColumn ( error.extra_ ) + " for detail.\n"; + if (error.extra_) + formattedMessage += "See " + + getLocationLineAndColumn(error.extra_) + " for detail.\n"; } return formattedMessage; } - -std::istream& operator>> ( std::istream& sin, Value& root ) +std::istream& +operator>>(std::istream& sin, Value& root) { Json::Reader reader; - bool ok = reader.parse (sin, root); + bool ok = reader.parse(sin, root); - //JSON_ASSERT( ok ); - if (! ok) - ripple::Throw (reader.getFormatedErrorMessages ()); + // JSON_ASSERT( ok ); + if (!ok) + ripple::Throw(reader.getFormatedErrorMessages()); return sin; } - -} // namespace Json +} // namespace Json diff --git a/src/ripple/json/impl/json_value.cpp b/src/ripple/json/impl/json_value.cpp index 677ee8e343..c5659347d4 100644 --- a/src/ripple/json/impl/json_value.cpp +++ b/src/ripple/json/impl/json_value.cpp @@ -18,58 +18,63 @@ //============================================================================== #include -#include -#include -#include #include +#include +#include +#include namespace Json { const Value Value::null; -const Int Value::minInt = Int ( ~ (UInt (-1) / 2) ); -const Int Value::maxInt = Int ( UInt (-1) / 2 ); -const UInt Value::maxUInt = UInt (-1); +const Int Value::minInt = Int(~(UInt(-1) / 2)); +const Int Value::maxInt = Int(UInt(-1) / 2); +const UInt Value::maxUInt = UInt(-1); class DefaultValueAllocator : public ValueAllocator { public: virtual ~DefaultValueAllocator() = default; - char* makeMemberName ( const char* memberName ) override + char* + makeMemberName(const char* memberName) override { - return duplicateStringValue ( memberName ); + return duplicateStringValue(memberName); } - void releaseMemberName ( char* memberName ) override + void + releaseMemberName(char* memberName) override { - releaseStringValue ( memberName ); + releaseStringValue(memberName); } - char* duplicateStringValue ( const char* value, - unsigned int length = unknown ) override + char* + duplicateStringValue(const char* value, unsigned int length = unknown) + override { //@todo investigate this old optimization - //if ( !value || value[0] == 0 ) + // if ( !value || value[0] == 0 ) // return 0; - if ( length == unknown ) - length = value ? (unsigned int)strlen ( value ) : 0; + if (length == unknown) + length = value ? (unsigned int)strlen(value) : 0; - char* newString = static_cast ( malloc ( length + 1 ) ); - if ( value ) - memcpy ( newString, value, length ); + char* newString = static_cast(malloc(length + 1)); + if (value) + memcpy(newString, value, length); newString[length] = 0; return newString; } - void releaseStringValue ( char* value ) override + void + releaseStringValue(char* value) override { - if ( value ) - free ( value ); + if (value) + free(value); } }; -static ValueAllocator*& valueAllocator () +static ValueAllocator*& +valueAllocator() { static ValueAllocator* valueAllocator = new DefaultValueAllocator; return valueAllocator; @@ -77,9 +82,10 @@ static ValueAllocator*& valueAllocator () static struct DummyValueAllocatorInitializer { - DummyValueAllocatorInitializer () + DummyValueAllocatorInitializer() { - valueAllocator (); // ensure valueAllocator() statics are initialized before main(). + valueAllocator(); // ensure valueAllocator() statics are initialized + // before main(). } } dummyValueAllocatorInitializer; @@ -94,68 +100,67 @@ static struct DummyValueAllocatorInitializer // Notes: index_ indicates if the string was allocated when // a string is stored. -Value::CZString::CZString ( int index ) - : cstr_ ( 0 ) - , index_ ( index ) +Value::CZString::CZString(int index) : cstr_(0), index_(index) { } -Value::CZString::CZString ( const char* cstr, DuplicationPolicy allocate ) - : cstr_ ( allocate == duplicate ? valueAllocator ()->makeMemberName (cstr) - : cstr ) - , index_ ( allocate ) +Value::CZString::CZString(const char* cstr, DuplicationPolicy allocate) + : cstr_( + allocate == duplicate ? valueAllocator()->makeMemberName(cstr) : cstr) + , index_(allocate) { } -Value::CZString::CZString ( const CZString& other ) - : cstr_ ( other.index_ != noDuplication&& other.cstr_ != 0 - ? valueAllocator ()->makeMemberName ( other.cstr_ ) - : other.cstr_ ) - , index_ ( other.cstr_ ? (other.index_ == noDuplication ? noDuplication : duplicate) - : other.index_ ) +Value::CZString::CZString(const CZString& other) + : cstr_( + other.index_ != noDuplication && other.cstr_ != 0 + ? valueAllocator()->makeMemberName(other.cstr_) + : other.cstr_) + , index_( + other.cstr_ + ? (other.index_ == noDuplication ? noDuplication : duplicate) + : other.index_) { } -Value::CZString::~CZString () +Value::CZString::~CZString() { - if ( cstr_ && index_ == duplicate ) - valueAllocator ()->releaseMemberName ( const_cast ( cstr_ ) ); + if (cstr_ && index_ == duplicate) + valueAllocator()->releaseMemberName(const_cast(cstr_)); } bool -Value::CZString::operator< ( const CZString& other ) const +Value::CZString::operator<(const CZString& other) const { - if ( cstr_ ) - return strcmp ( cstr_, other.cstr_ ) < 0; + if (cstr_) + return strcmp(cstr_, other.cstr_) < 0; return index_ < other.index_; } bool -Value::CZString::operator== ( const CZString& other ) const +Value::CZString::operator==(const CZString& other) const { - if ( cstr_ ) - return strcmp ( cstr_, other.cstr_ ) == 0; + if (cstr_) + return strcmp(cstr_, other.cstr_) == 0; return index_ == other.index_; } - int -Value::CZString::index () const +Value::CZString::index() const { return index_; } - const char* -Value::CZString::c_str () const +Value::CZString::c_str() const { return cstr_; } bool -Value::CZString::isStaticString () const +Value::CZString::isStaticString() const { return index_ == noDuplication; } @@ -172,151 +177,135 @@ Value::CZString::isStaticString () const * memset( this, 0, sizeof(Value) ) * This optimization is used in ValueInternalMap fast allocator. */ -Value::Value ( ValueType type ) - : type_ ( type ) - , allocated_ ( 0 ) +Value::Value(ValueType type) : type_(type), allocated_(0) { - switch ( type ) + switch (type) { - case nullValue: - break; + case nullValue: + break; - case intValue: - case uintValue: - value_.int_ = 0; - break; + case intValue: + case uintValue: + value_.int_ = 0; + break; - case realValue: - value_.real_ = 0.0; - break; + case realValue: + value_.real_ = 0.0; + break; - case stringValue: - value_.string_ = 0; - break; + case stringValue: + value_.string_ = 0; + break; - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues (); - break; + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues(); + break; - case booleanValue: - value_.bool_ = false; - break; + case booleanValue: + value_.bool_ = false; + break; - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } } - -Value::Value ( Int value ) - : type_ ( intValue ) +Value::Value(Int value) : type_(intValue) { value_.int_ = value; } - -Value::Value ( UInt value ) - : type_ ( uintValue ) +Value::Value(UInt value) : type_(uintValue) { value_.uint_ = value; } -Value::Value ( double value ) - : type_ ( realValue ) +Value::Value(double value) : type_(realValue) { value_.real_ = value; } -Value::Value ( const char* value ) - : type_ ( stringValue ) - , allocated_ ( true ) +Value::Value(const char* value) : type_(stringValue), allocated_(true) { - value_.string_ = valueAllocator ()->duplicateStringValue ( value ); + value_.string_ = valueAllocator()->duplicateStringValue(value); } -Value::Value ( std::string const& value ) - : type_ ( stringValue ) - , allocated_ ( true ) +Value::Value(std::string const& value) : type_(stringValue), allocated_(true) { - value_.string_ = valueAllocator ()->duplicateStringValue ( value.c_str (), - (unsigned int)value.length () ); + value_.string_ = valueAllocator()->duplicateStringValue( + value.c_str(), (unsigned int)value.length()); } -Value::Value ( const StaticString& value ) - : type_ ( stringValue ) - , allocated_ ( false ) +Value::Value(const StaticString& value) : type_(stringValue), allocated_(false) { - value_.string_ = const_cast ( value.c_str () ); + value_.string_ = const_cast(value.c_str()); } -Value::Value ( bool value ) - : type_ ( booleanValue ) +Value::Value(bool value) : type_(booleanValue) { value_.bool_ = value; } - -Value::Value ( const Value& other ) - : type_ ( other.type_ ) +Value::Value(const Value& other) : type_(other.type_) { - switch ( type_ ) + switch (type_) { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - value_ = other.value_; - break; + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + value_ = other.value_; + break; - case stringValue: - if ( other.value_.string_ ) - { - value_.string_ = valueAllocator ()->duplicateStringValue ( other.value_.string_ ); - allocated_ = true; - } - else - value_.string_ = 0; + case stringValue: + if (other.value_.string_) + { + value_.string_ = valueAllocator()->duplicateStringValue( + other.value_.string_); + allocated_ = true; + } + else + value_.string_ = 0; - break; + break; - case arrayValue: - case objectValue: - value_.map_ = new ObjectValues ( *other.value_.map_ ); - break; + case arrayValue: + case objectValue: + value_.map_ = new ObjectValues(*other.value_.map_); + break; - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } } - -Value::~Value () +Value::~Value() { - switch ( type_ ) + switch (type_) { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - break; + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + break; - case stringValue: - if ( allocated_ ) - valueAllocator ()->releaseStringValue ( value_.string_ ); + case stringValue: + if (allocated_) + valueAllocator()->releaseStringValue(value_.string_); - break; + break; - case arrayValue: - case objectValue: - if (value_.map_) - delete value_.map_; - break; + case arrayValue: + case objectValue: + if (value_.map_) + delete value_.map_; + break; - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } } @@ -328,10 +317,8 @@ Value::operator=(Value const& other) return *this; } -Value::Value ( Value&& other ) noexcept - : value_ ( other.value_ ) - , type_ ( other.type_ ) - , allocated_ ( other.allocated_ ) +Value::Value(Value&& other) noexcept + : value_(other.value_), type_(other.type_), allocated_(other.allocated_) { other.type_ = nullValue; other.allocated_ = 0; @@ -346,9 +333,9 @@ Value::operator=(Value&& other) } void -Value::swap ( Value& other ) noexcept +Value::swap(Value& other) noexcept { - std::swap ( value_, other.value_ ); + std::swap(value_, other.value_); ValueType temp = type_; type_ = other.type_; @@ -360,13 +347,13 @@ Value::swap ( Value& other ) noexcept } ValueType -Value::type () const +Value::type() const { return type_; } -static -int integerCmp (Int i, UInt ui) +static int +integerCmp(Int i, UInt ui) { // All negative numbers are less than all unsigned numbers. if (i < 0) @@ -376,682 +363,654 @@ int integerCmp (Int i, UInt ui) return (i < ui) ? -1 : (i == ui) ? 0 : 1; } -bool operator < (const Value& x, const Value& y) +bool +operator<(const Value& x, const Value& y) { if (auto signum = x.type_ - y.type_) { if (x.type_ == intValue && y.type_ == uintValue) - signum = integerCmp (x.value_.int_, y.value_.uint_); + signum = integerCmp(x.value_.int_, y.value_.uint_); else if (x.type_ == uintValue && y.type_ == intValue) - signum = - integerCmp (y.value_.int_, x.value_.uint_); + signum = -integerCmp(y.value_.int_, x.value_.uint_); return signum < 0; } switch (x.type_) { - case nullValue: - return false; + case nullValue: + return false; - case intValue: - return x.value_.int_ < y.value_.int_; + case intValue: + return x.value_.int_ < y.value_.int_; - case uintValue: - return x.value_.uint_ < y.value_.uint_; + case uintValue: + return x.value_.uint_ < y.value_.uint_; - case realValue: - return x.value_.real_ < y.value_.real_; + case realValue: + return x.value_.real_ < y.value_.real_; - case booleanValue: - return x.value_.bool_ < y.value_.bool_; + case booleanValue: + return x.value_.bool_ < y.value_.bool_; - case stringValue: - return (x.value_.string_ == 0 && y.value_.string_) - || (y.value_.string_ && x.value_.string_ && - strcmp (x.value_.string_, y.value_.string_) < 0); + case stringValue: + return (x.value_.string_ == 0 && y.value_.string_) || + (y.value_.string_ && x.value_.string_ && + strcmp(x.value_.string_, y.value_.string_) < 0); - case arrayValue: - case objectValue: - { - if (int signum = int (x.value_.map_->size ()) - y.value_.map_->size ()) - return signum < 0; + case arrayValue: + case objectValue: { + if (int signum = int(x.value_.map_->size()) - y.value_.map_->size()) + return signum < 0; - return *x.value_.map_ < *y.value_.map_; - } + return *x.value_.map_ < *y.value_.map_; + } - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } return 0; // unreachable } -bool operator== (const Value& x, const Value& y) +bool +operator==(const Value& x, const Value& y) { if (x.type_ != y.type_) { if (x.type_ == intValue && y.type_ == uintValue) - return ! integerCmp (x.value_.int_, y.value_.uint_); + return !integerCmp(x.value_.int_, y.value_.uint_); if (x.type_ == uintValue && y.type_ == intValue) - return ! integerCmp (y.value_.int_, x.value_.uint_); + return !integerCmp(y.value_.int_, x.value_.uint_); return false; } switch (x.type_) { - case nullValue: - return true; + case nullValue: + return true; - case intValue: - return x.value_.int_ == y.value_.int_; + case intValue: + return x.value_.int_ == y.value_.int_; - case uintValue: - return x.value_.uint_ == y.value_.uint_; + case uintValue: + return x.value_.uint_ == y.value_.uint_; - case realValue: - return x.value_.real_ == y.value_.real_; + case realValue: + return x.value_.real_ == y.value_.real_; - case booleanValue: - return x.value_.bool_ == y.value_.bool_; + case booleanValue: + return x.value_.bool_ == y.value_.bool_; - case stringValue: - return x.value_.string_ == y.value_.string_ - || (y.value_.string_ && x.value_.string_ && - ! strcmp (x.value_.string_, y.value_.string_)); + case stringValue: + return x.value_.string_ == y.value_.string_ || + (y.value_.string_ && x.value_.string_ && + !strcmp(x.value_.string_, y.value_.string_)); - case arrayValue: - case objectValue: - return x.value_.map_->size () == y.value_.map_->size () - && *x.value_.map_ == *y.value_.map_; + case arrayValue: + case objectValue: + return x.value_.map_->size() == y.value_.map_->size() && + *x.value_.map_ == *y.value_.map_; - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } return 0; // unreachable } const char* -Value::asCString () const +Value::asCString() const { - JSON_ASSERT ( type_ == stringValue ); + JSON_ASSERT(type_ == stringValue); return value_.string_; } - std::string -Value::asString () const +Value::asString() const { - switch ( type_ ) + switch (type_) { - case nullValue: - return ""; + case nullValue: + return ""; - case stringValue: - return value_.string_ ? value_.string_ : ""; + case stringValue: + return value_.string_ ? value_.string_ : ""; - case booleanValue: - return value_.bool_ ? "true" : "false"; + case booleanValue: + return value_.bool_ ? "true" : "false"; - case intValue: - return beast::lexicalCastThrow (value_.int_); + case intValue: + return beast::lexicalCastThrow(value_.int_); - case uintValue: - return beast::lexicalCastThrow (value_.uint_); + case uintValue: + return beast::lexicalCastThrow(value_.uint_); - case realValue: - return beast::lexicalCastThrow (value_.real_); + case realValue: + return beast::lexicalCastThrow(value_.real_); - case arrayValue: - case objectValue: - JSON_ASSERT_MESSAGE ( false, "Type is not convertible to string" ); + case arrayValue: + case objectValue: + JSON_ASSERT_MESSAGE(false, "Type is not convertible to string"); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return ""; // unreachable + return ""; // unreachable } Value::Int -Value::asInt () const +Value::asInt() const { - switch ( type_ ) + switch (type_) { - case nullValue: - return 0; + case nullValue: + return 0; - case intValue: - return value_.int_; + case intValue: + return value_.int_; - case uintValue: - JSON_ASSERT_MESSAGE ( value_.uint_ < (unsigned)maxInt, "integer out of signed integer range" ); - return value_.uint_; + case uintValue: + JSON_ASSERT_MESSAGE( + value_.uint_ < (unsigned)maxInt, + "integer out of signed integer range"); + return value_.uint_; - case realValue: - JSON_ASSERT_MESSAGE ( value_.real_ >= minInt && value_.real_ <= maxInt, "Real out of signed integer range" ); - return Int ( value_.real_ ); + case realValue: + JSON_ASSERT_MESSAGE( + value_.real_ >= minInt && value_.real_ <= maxInt, + "Real out of signed integer range"); + return Int(value_.real_); - case booleanValue: - return value_.bool_ ? 1 : 0; + case booleanValue: + return value_.bool_ ? 1 : 0; - case stringValue: - { - char const* const str {value_.string_ ? value_.string_ : ""}; - return beast::lexicalCastThrow (str); + case stringValue: { + char const* const str{value_.string_ ? value_.string_ : ""}; + return beast::lexicalCastThrow(str); } - case arrayValue: - case objectValue: - JSON_ASSERT_MESSAGE ( false, "Type is not convertible to int" ); + case arrayValue: + case objectValue: + JSON_ASSERT_MESSAGE(false, "Type is not convertible to int"); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return 0; // unreachable; + return 0; // unreachable; } Value::UInt -Value::asUInt () const +Value::asUInt() const { - switch ( type_ ) + switch (type_) { - case nullValue: - return 0; + case nullValue: + return 0; - case intValue: - JSON_ASSERT_MESSAGE ( value_.int_ >= 0, "Negative integer can not be converted to unsigned integer" ); - return value_.int_; + case intValue: + JSON_ASSERT_MESSAGE( + value_.int_ >= 0, + "Negative integer can not be converted to unsigned integer"); + return value_.int_; - case uintValue: - return value_.uint_; + case uintValue: + return value_.uint_; - case realValue: - JSON_ASSERT_MESSAGE ( value_.real_ >= 0 && value_.real_ <= maxUInt, "Real out of unsigned integer range" ); - return UInt ( value_.real_ ); + case realValue: + JSON_ASSERT_MESSAGE( + value_.real_ >= 0 && value_.real_ <= maxUInt, + "Real out of unsigned integer range"); + return UInt(value_.real_); - case booleanValue: - return value_.bool_ ? 1 : 0; + case booleanValue: + return value_.bool_ ? 1 : 0; - case stringValue: - { - char const* const str {value_.string_ ? value_.string_ : ""}; - return beast::lexicalCastThrow (str); + case stringValue: { + char const* const str{value_.string_ ? value_.string_ : ""}; + return beast::lexicalCastThrow(str); } - case arrayValue: - case objectValue: - JSON_ASSERT_MESSAGE ( false, "Type is not convertible to uint" ); + case arrayValue: + case objectValue: + JSON_ASSERT_MESSAGE(false, "Type is not convertible to uint"); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return 0; // unreachable; + return 0; // unreachable; } double -Value::asDouble () const +Value::asDouble() const { - switch ( type_ ) + switch (type_) { - case nullValue: - return 0.0; + case nullValue: + return 0.0; - case intValue: - return value_.int_; + case intValue: + return value_.int_; - case uintValue: - return value_.uint_; + case uintValue: + return value_.uint_; - case realValue: - return value_.real_; + case realValue: + return value_.real_; - case booleanValue: - return value_.bool_ ? 1.0 : 0.0; + case booleanValue: + return value_.bool_ ? 1.0 : 0.0; - case stringValue: - case arrayValue: - case objectValue: - JSON_ASSERT_MESSAGE ( false, "Type is not convertible to double" ); + case stringValue: + case arrayValue: + case objectValue: + JSON_ASSERT_MESSAGE(false, "Type is not convertible to double"); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return 0; // unreachable; + return 0; // unreachable; } bool -Value::asBool () const +Value::asBool() const { - switch ( type_ ) + switch (type_) { - case nullValue: - return false; + case nullValue: + return false; - case intValue: - case uintValue: - return value_.int_ != 0; + case intValue: + case uintValue: + return value_.int_ != 0; - case realValue: - return value_.real_ != 0.0; + case realValue: + return value_.real_ != 0.0; - case booleanValue: - return value_.bool_; + case booleanValue: + return value_.bool_; - case stringValue: - return value_.string_ && value_.string_[0] != 0; + case stringValue: + return value_.string_ && value_.string_[0] != 0; - case arrayValue: - case objectValue: - return value_.map_->size () != 0; + case arrayValue: + case objectValue: + return value_.map_->size() != 0; - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return false; // unreachable; + return false; // unreachable; } - bool -Value::isConvertibleTo ( ValueType other ) const +Value::isConvertibleTo(ValueType other) const { - switch ( type_ ) + switch (type_) { - case nullValue: - return true; + case nullValue: + return true; - case intValue: - return ( other == nullValue && value_.int_ == 0 ) - || other == intValue - || ( other == uintValue && value_.int_ >= 0 ) - || other == realValue - || other == stringValue - || other == booleanValue; + case intValue: + return (other == nullValue && value_.int_ == 0) || + other == intValue || (other == uintValue && value_.int_ >= 0) || + other == realValue || other == stringValue || + other == booleanValue; - case uintValue: - return ( other == nullValue && value_.uint_ == 0 ) - || ( other == intValue && value_.uint_ <= (unsigned)maxInt ) - || other == uintValue - || other == realValue - || other == stringValue - || other == booleanValue; + case uintValue: + return (other == nullValue && value_.uint_ == 0) || + (other == intValue && value_.uint_ <= (unsigned)maxInt) || + other == uintValue || other == realValue || + other == stringValue || other == booleanValue; - case realValue: - return ( other == nullValue && value_.real_ == 0.0 ) - || ( other == intValue && value_.real_ >= minInt && value_.real_ <= maxInt ) - || ( other == uintValue && value_.real_ >= 0 && value_.real_ <= maxUInt ) - || other == realValue - || other == stringValue - || other == booleanValue; + case realValue: + return (other == nullValue && value_.real_ == 0.0) || + (other == intValue && value_.real_ >= minInt && + value_.real_ <= maxInt) || + (other == uintValue && value_.real_ >= 0 && + value_.real_ <= maxUInt) || + other == realValue || other == stringValue || + other == booleanValue; - case booleanValue: - return ( other == nullValue && value_.bool_ == false ) - || other == intValue - || other == uintValue - || other == realValue - || other == stringValue - || other == booleanValue; + case booleanValue: + return (other == nullValue && value_.bool_ == false) || + other == intValue || other == uintValue || other == realValue || + other == stringValue || other == booleanValue; - case stringValue: - return other == stringValue - || ( other == nullValue && (!value_.string_ || value_.string_[0] == 0) ); + case stringValue: + return other == stringValue || + (other == nullValue && + (!value_.string_ || value_.string_[0] == 0)); - case arrayValue: - return other == arrayValue - || ( other == nullValue && value_.map_->size () == 0 ); + case arrayValue: + return other == arrayValue || + (other == nullValue && value_.map_->size() == 0); - case objectValue: - return other == objectValue - || ( other == nullValue && value_.map_->size () == 0 ); + case objectValue: + return other == objectValue || + (other == nullValue && value_.map_->size() == 0); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return false; // unreachable; + return false; // unreachable; } - /// Number of values in array or object Value::UInt -Value::size () const +Value::size() const { - switch ( type_ ) + switch (type_) { - case nullValue: - case intValue: - case uintValue: - case realValue: - case booleanValue: - case stringValue: - return 0; + case nullValue: + case intValue: + case uintValue: + case realValue: + case booleanValue: + case stringValue: + return 0; - case arrayValue: // size of the array is highest index + 1 - if ( !value_.map_->empty () ) - { - ObjectValues::const_iterator itLast = value_.map_->end (); - --itLast; - return (*itLast).first.index () + 1; - } + case arrayValue: // size of the array is highest index + 1 + if (!value_.map_->empty()) + { + ObjectValues::const_iterator itLast = value_.map_->end(); + --itLast; + return (*itLast).first.index() + 1; + } - return 0; + return 0; - case objectValue: - return Int ( value_.map_->size () ); + case objectValue: + return Int(value_.map_->size()); - default: - JSON_ASSERT_UNREACHABLE; + default: + JSON_ASSERT_UNREACHABLE; } - return 0; // unreachable; + return 0; // unreachable; } - -Value::operator bool () const +Value::operator bool() const { - if (isNull ()) + if (isNull()) return false; - if (isString ()) + if (isString()) { auto s = asCString(); return s && s[0]; } - return ! (isArray() || isObject()) || size (); + return !(isArray() || isObject()) || size(); } void -Value::clear () +Value::clear() { - JSON_ASSERT ( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); + JSON_ASSERT( + type_ == nullValue || type_ == arrayValue || type_ == objectValue); - switch ( type_ ) + switch (type_) { - case arrayValue: - case objectValue: - value_.map_->clear (); - break; + case arrayValue: + case objectValue: + value_.map_->clear(); + break; - default: - break; + default: + break; } } Value& -Value::operator[] ( UInt index ) +Value::operator[](UInt index) { - JSON_ASSERT ( type_ == nullValue || type_ == arrayValue ); + JSON_ASSERT(type_ == nullValue || type_ == arrayValue); - if ( type_ == nullValue ) - *this = Value ( arrayValue ); + if (type_ == nullValue) + *this = Value(arrayValue); - CZString key ( index ); - ObjectValues::iterator it = value_.map_->lower_bound ( key ); + CZString key(index); + ObjectValues::iterator it = value_.map_->lower_bound(key); - if ( it != value_.map_->end () && (*it).first == key ) + if (it != value_.map_->end() && (*it).first == key) return (*it).second; - ObjectValues::value_type defaultValue ( key, null ); - it = value_.map_->insert ( it, defaultValue ); + ObjectValues::value_type defaultValue(key, null); + it = value_.map_->insert(it, defaultValue); return (*it).second; } - const Value& -Value::operator[] ( UInt index ) const +Value::operator[](UInt index) const { - JSON_ASSERT ( type_ == nullValue || type_ == arrayValue ); + JSON_ASSERT(type_ == nullValue || type_ == arrayValue); - if ( type_ == nullValue ) + if (type_ == nullValue) return null; - CZString key ( index ); - ObjectValues::const_iterator it = value_.map_->find ( key ); + CZString key(index); + ObjectValues::const_iterator it = value_.map_->find(key); - if ( it == value_.map_->end () ) + if (it == value_.map_->end()) return null; return (*it).second; } - Value& -Value::operator[] ( const char* key ) +Value::operator[](const char* key) { - return resolveReference ( key, false ); + return resolveReference(key, false); } - Value& -Value::resolveReference ( const char* key, - bool isStatic ) +Value::resolveReference(const char* key, bool isStatic) { - JSON_ASSERT ( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT(type_ == nullValue || type_ == objectValue); - if ( type_ == nullValue ) - *this = Value ( objectValue ); + if (type_ == nullValue) + *this = Value(objectValue); - CZString actualKey ( key, isStatic ? CZString::noDuplication - : CZString::duplicateOnCopy ); - ObjectValues::iterator it = value_.map_->lower_bound ( actualKey ); + CZString actualKey( + key, isStatic ? CZString::noDuplication : CZString::duplicateOnCopy); + ObjectValues::iterator it = value_.map_->lower_bound(actualKey); - if ( it != value_.map_->end () && (*it).first == actualKey ) + if (it != value_.map_->end() && (*it).first == actualKey) return (*it).second; - ObjectValues::value_type defaultValue ( actualKey, null ); - it = value_.map_->insert ( it, defaultValue ); + ObjectValues::value_type defaultValue(actualKey, null); + it = value_.map_->insert(it, defaultValue); Value& value = (*it).second; return value; } - Value -Value::get ( UInt index, - const Value& defaultValue ) const +Value::get(UInt index, const Value& defaultValue) const { - const Value* value = & ((*this)[index]); + const Value* value = &((*this)[index]); return value == &null ? defaultValue : *value; } - bool -Value::isValidIndex ( UInt index ) const +Value::isValidIndex(UInt index) const { - return index < size (); + return index < size(); } - - const Value& -Value::operator[] ( const char* key ) const +Value::operator[](const char* key) const { - JSON_ASSERT ( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT(type_ == nullValue || type_ == objectValue); - if ( type_ == nullValue ) + if (type_ == nullValue) return null; - CZString actualKey ( key, CZString::noDuplication ); - ObjectValues::const_iterator it = value_.map_->find ( actualKey ); + CZString actualKey(key, CZString::noDuplication); + ObjectValues::const_iterator it = value_.map_->find(actualKey); - if ( it == value_.map_->end () ) + if (it == value_.map_->end()) return null; return (*it).second; } - Value& -Value::operator[] ( std::string const& key ) +Value::operator[](std::string const& key) { - return (*this)[ key.c_str () ]; + return (*this)[key.c_str()]; } - const Value& -Value::operator[] ( std::string const& key ) const +Value::operator[](std::string const& key) const { - return (*this)[ key.c_str () ]; + return (*this)[key.c_str()]; } Value& -Value::operator[] ( const StaticString& key ) +Value::operator[](const StaticString& key) { - return resolveReference ( key, true ); + return resolveReference(key, true); } Value& -Value::append ( const Value& value ) +Value::append(const Value& value) { - return (*this)[size ()] = value; + return (*this)[size()] = value; } Value& -Value::append ( Value&& value ) +Value::append(Value&& value) { - return (*this)[size ()] = std::move(value); + return (*this)[size()] = std::move(value); } - Value -Value::get ( const char* key, - const Value& defaultValue ) const +Value::get(const char* key, const Value& defaultValue) const { - const Value* value = & ((*this)[key]); + const Value* value = &((*this)[key]); return value == &null ? defaultValue : *value; } - Value -Value::get ( std::string const& key, - const Value& defaultValue ) const +Value::get(std::string const& key, const Value& defaultValue) const { - return get ( key.c_str (), defaultValue ); + return get(key.c_str(), defaultValue); } Value -Value::removeMember ( const char* key ) +Value::removeMember(const char* key) { - JSON_ASSERT ( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT(type_ == nullValue || type_ == objectValue); - if ( type_ == nullValue ) + if (type_ == nullValue) return null; - CZString actualKey ( key, CZString::noDuplication ); - ObjectValues::iterator it = value_.map_->find ( actualKey ); + CZString actualKey(key, CZString::noDuplication); + ObjectValues::iterator it = value_.map_->find(actualKey); - if ( it == value_.map_->end () ) + if (it == value_.map_->end()) return null; - Value old (it->second); - value_.map_->erase (it); + Value old(it->second); + value_.map_->erase(it); return old; } Value -Value::removeMember ( std::string const& key ) +Value::removeMember(std::string const& key) { - return removeMember ( key.c_str () ); + return removeMember(key.c_str()); } bool -Value::isMember ( const char* key ) const +Value::isMember(const char* key) const { if (type_ != objectValue) return false; - const Value* value = & ((*this)[key]); + const Value* value = &((*this)[key]); return value != &null; } - bool -Value::isMember ( std::string const& key ) const +Value::isMember(std::string const& key) const { - return isMember ( key.c_str () ); + return isMember(key.c_str()); } - Value::Members -Value::getMemberNames () const +Value::getMemberNames() const { - JSON_ASSERT ( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT(type_ == nullValue || type_ == objectValue); - if ( type_ == nullValue ) - return Value::Members (); + if (type_ == nullValue) + return Value::Members(); Members members; - members.reserve ( value_.map_->size () ); - ObjectValues::const_iterator it = value_.map_->begin (); - ObjectValues::const_iterator itEnd = value_.map_->end (); + members.reserve(value_.map_->size()); + ObjectValues::const_iterator it = value_.map_->begin(); + ObjectValues::const_iterator itEnd = value_.map_->end(); - for ( ; it != itEnd; ++it ) - members.push_back ( std::string ( (*it).first.c_str () ) ); + for (; it != itEnd; ++it) + members.push_back(std::string((*it).first.c_str())); return members; } bool -Value::isNull () const +Value::isNull() const { return type_ == nullValue; } - bool -Value::isBool () const +Value::isBool() const { return type_ == booleanValue; } - bool -Value::isInt () const +Value::isInt() const { return type_ == intValue; } - bool -Value::isUInt () const +Value::isUInt() const { return type_ == uintValue; } - bool -Value::isIntegral () const +Value::isIntegral() const { - return type_ == intValue - || type_ == uintValue - || type_ == booleanValue; + return type_ == intValue || type_ == uintValue || type_ == booleanValue; } - bool -Value::isDouble () const +Value::isDouble() const { return type_ == realValue; } - bool -Value::isNumeric () const +Value::isNumeric() const { - return isIntegral () || isDouble (); + return isIntegral() || isDouble(); } - bool -Value::isString () const +Value::isString() const { return type_ == stringValue; } - bool Value::isArray() const { @@ -1059,12 +1018,11 @@ Value::isArray() const } bool -Value::isArrayOrNull () const +Value::isArrayOrNull() const { - return type_ == nullValue || type_ == arrayValue; + return type_ == nullValue || type_ == arrayValue; } - bool Value::isObject() const { @@ -1072,88 +1030,86 @@ Value::isObject() const } bool -Value::isObjectOrNull () const +Value::isObjectOrNull() const { - return type_ == nullValue || type_ == objectValue; + return type_ == nullValue || type_ == objectValue; } std::string -Value::toStyledString () const +Value::toStyledString() const { StyledWriter writer; - return writer.write ( *this ); -} - - -Value::const_iterator -Value::begin () const -{ - switch ( type_ ) - { - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator ( value_.map_->begin () ); - - break; - default: - break; - } - - return const_iterator (); + return writer.write(*this); } Value::const_iterator -Value::end () const +Value::begin() const { - switch ( type_ ) + switch (type_) { - case arrayValue: - case objectValue: - if ( value_.map_ ) - return const_iterator ( value_.map_->end () ); + case arrayValue: + case objectValue: + if (value_.map_) + return const_iterator(value_.map_->begin()); - break; - default: - break; + break; + default: + break; } - return const_iterator (); + return const_iterator(); } - -Value::iterator -Value::begin () +Value::const_iterator +Value::end() const { - switch ( type_ ) + switch (type_) { - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator ( value_.map_->begin () ); - break; - default: - break; + case arrayValue: + case objectValue: + if (value_.map_) + return const_iterator(value_.map_->end()); + + break; + default: + break; } - return iterator (); + return const_iterator(); } Value::iterator -Value::end () +Value::begin() { - switch ( type_ ) + switch (type_) { - case arrayValue: - case objectValue: - if ( value_.map_ ) - return iterator ( value_.map_->end () ); - break; - default: - break; + case arrayValue: + case objectValue: + if (value_.map_) + return iterator(value_.map_->begin()); + break; + default: + break; } - return iterator (); + return iterator(); } -} // namespace Json +Value::iterator +Value::end() +{ + switch (type_) + { + case arrayValue: + case objectValue: + if (value_.map_) + return iterator(value_.map_->end()); + break; + default: + break; + } + + return iterator(); +} + +} // namespace Json diff --git a/src/ripple/json/impl/json_valueiterator.cpp b/src/ripple/json/impl/json_valueiterator.cpp index 3098a8fc31..355bfad23c 100644 --- a/src/ripple/json/impl/json_valueiterator.cpp +++ b/src/ripple/json/impl/json_valueiterator.cpp @@ -31,60 +31,55 @@ namespace Json { // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// -ValueIteratorBase::ValueIteratorBase () - : current_ () - , isNull_ ( true ) +ValueIteratorBase::ValueIteratorBase() : current_(), isNull_(true) { } -ValueIteratorBase::ValueIteratorBase ( const Value::ObjectValues::iterator& current ) - : current_ ( current ) - , isNull_ ( false ) +ValueIteratorBase::ValueIteratorBase( + const Value::ObjectValues::iterator& current) + : current_(current), isNull_(false) { } Value& -ValueIteratorBase::deref () const +ValueIteratorBase::deref() const { return current_->second; } - void -ValueIteratorBase::increment () +ValueIteratorBase::increment() { ++current_; } - void -ValueIteratorBase::decrement () +ValueIteratorBase::decrement() { --current_; } - ValueIteratorBase::difference_type -ValueIteratorBase::computeDistance ( const SelfType& other ) const +ValueIteratorBase::computeDistance(const SelfType& other) const { // Iterator for null value are initialized using the default // constructor, which initialize current_ to the default // std::map::iterator. As begin() and end() are two instance // of the default std::map::iterator, they can not be compared. // To allow this, we handle this comparison specifically. - if ( isNull_ && other.isNull_ ) + if (isNull_ && other.isNull_) { return 0; } - - // Usage of std::distance is not portable (does not compile with Sun Studio 12 RogueWave STL, - // which is the one used by default). - // Using a portable hand-made version for non random iterator instead: + // Usage of std::distance is not portable (does not compile with Sun Studio + // 12 RogueWave STL, which is the one used by default). Using a portable + // hand-made version for non random iterator instead: // return difference_type( std::distance( current_, other.current_ ) ); difference_type myDistance = 0; - for ( Value::ObjectValues::iterator it = current_; it != other.current_; ++it ) + for (Value::ObjectValues::iterator it = current_; it != other.current_; + ++it) { ++myDistance; } @@ -92,11 +87,10 @@ ValueIteratorBase::computeDistance ( const SelfType& other ) const return myDistance; } - bool -ValueIteratorBase::isEqual ( const SelfType& other ) const +ValueIteratorBase::isEqual(const SelfType& other) const { - if ( isNull_ ) + if (isNull_) { return other.isNull_; } @@ -104,51 +98,46 @@ ValueIteratorBase::isEqual ( const SelfType& other ) const return current_ == other.current_; } - void -ValueIteratorBase::copy ( const SelfType& other ) +ValueIteratorBase::copy(const SelfType& other) { current_ = other.current_; } - Value -ValueIteratorBase::key () const +ValueIteratorBase::key() const { const Value::CZString czstring = (*current_).first; - if ( czstring.c_str () ) + if (czstring.c_str()) { - if ( czstring.isStaticString () ) - return Value ( StaticString ( czstring.c_str () ) ); + if (czstring.isStaticString()) + return Value(StaticString(czstring.c_str())); - return Value ( czstring.c_str () ); + return Value(czstring.c_str()); } - return Value ( czstring.index () ); + return Value(czstring.index()); } - UInt -ValueIteratorBase::index () const +ValueIteratorBase::index() const { const Value::CZString czstring = (*current_).first; - if ( !czstring.c_str () ) - return czstring.index (); + if (!czstring.c_str()) + return czstring.index(); - return Value::UInt ( -1 ); + return Value::UInt(-1); } - const char* -ValueIteratorBase::memberName () const +ValueIteratorBase::memberName() const { - const char* name = (*current_).first.c_str (); + const char* name = (*current_).first.c_str(); return name ? name : ""; } - // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// @@ -157,20 +146,19 @@ ValueIteratorBase::memberName () const // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// - -ValueConstIterator::ValueConstIterator ( const Value::ObjectValues::iterator& current ) - : ValueIteratorBase ( current ) +ValueConstIterator::ValueConstIterator( + const Value::ObjectValues::iterator& current) + : ValueIteratorBase(current) { } ValueConstIterator& -ValueConstIterator::operator = ( const ValueIteratorBase& other ) +ValueConstIterator::operator=(const ValueIteratorBase& other) { - copy ( other ); + copy(other); return *this; } - // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// @@ -179,27 +167,26 @@ ValueConstIterator::operator = ( const ValueIteratorBase& other ) // ////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////// - -ValueIterator::ValueIterator ( const Value::ObjectValues::iterator& current ) - : ValueIteratorBase ( current ) +ValueIterator::ValueIterator(const Value::ObjectValues::iterator& current) + : ValueIteratorBase(current) { } -ValueIterator::ValueIterator ( const ValueConstIterator& other ) - : ValueIteratorBase ( other ) +ValueIterator::ValueIterator(const ValueConstIterator& other) + : ValueIteratorBase(other) { } -ValueIterator::ValueIterator ( const ValueIterator& other ) - : ValueIteratorBase ( other ) +ValueIterator::ValueIterator(const ValueIterator& other) + : ValueIteratorBase(other) { } ValueIterator& -ValueIterator::operator = ( const SelfType& other ) +ValueIterator::operator=(const SelfType& other) { - copy ( other ); + copy(other); return *this; } -} // Json +} // namespace Json diff --git a/src/ripple/json/impl/json_writer.cpp b/src/ripple/json/impl/json_writer.cpp index 2fe0eb8def..4d696ab55b 100644 --- a/src/ripple/json/impl/json_writer.cpp +++ b/src/ripple/json/impl/json_writer.cpp @@ -24,26 +24,27 @@ #include #include -namespace Json -{ +namespace Json { -static bool isControlCharacter (char ch) +static bool +isControlCharacter(char ch) { return ch > 0 && ch <= 0x1F; } -static bool containsControlCharacter ( const char* str ) +static bool +containsControlCharacter(const char* str) { - while ( *str ) + while (*str) { - if ( isControlCharacter ( * (str++) ) ) + if (isControlCharacter(*(str++))) return true; } return false; } -static void uintToString ( unsigned int value, - char*& current ) +static void +uintToString(unsigned int value, char*& current) { *--current = 0; @@ -51,47 +52,50 @@ static void uintToString ( unsigned int value, { *--current = (value % 10) + '0'; value /= 10; - } - while ( value != 0 ); + } while (value != 0); } -std::string valueToString ( Int value ) +std::string +valueToString(Int value) { char buffer[32]; - char* current = buffer + sizeof (buffer); + char* current = buffer + sizeof(buffer); bool isNegative = value < 0; - if ( isNegative ) + if (isNegative) value = -value; - uintToString ( UInt (value), current ); + uintToString(UInt(value), current); - if ( isNegative ) + if (isNegative) *--current = '-'; - assert ( current >= buffer ); + assert(current >= buffer); return current; } - -std::string valueToString ( UInt value ) +std::string +valueToString(UInt value) { char buffer[32]; - char* current = buffer + sizeof (buffer); - uintToString ( value, current ); - assert ( current >= buffer ); + char* current = buffer + sizeof(buffer); + uintToString(value, current); + assert(current >= buffer); return current; } -std::string valueToString( double value ) +std::string +valueToString(double value) { - // Allocate a buffer that is more than large enough to store the 16 digits of - // precision requested below. + // Allocate a buffer that is more than large enough to store the 16 digits + // of precision requested below. char buffer[32]; // Print into the buffer. We need not request the alternative representation // that always has a decimal point because JSON doesn't distingish the // concepts of reals and integers. -#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 to avoid warning. +#if defined(_MSC_VER) && \ + defined(__STDC_SECURE_LIB__) // Use secure version with visual studio 2005 + // to avoid warning. sprintf_s(buffer, sizeof(buffer), "%.16g", value); #else snprintf(buffer, sizeof(buffer), "%.16g", value); @@ -99,78 +103,82 @@ std::string valueToString( double value ) return buffer; } -std::string valueToString ( bool value ) +std::string +valueToString(bool value) { return value ? "true" : "false"; } -std::string valueToQuotedString ( const char* value ) +std::string +valueToQuotedString(const char* value) { // Not sure how to handle unicode... - if (strpbrk (value, "\"\\\b\f\n\r\t") == nullptr && !containsControlCharacter ( value )) - return std::string ("\"") + value + "\""; + if (strpbrk(value, "\"\\\b\f\n\r\t") == nullptr && + !containsControlCharacter(value)) + return std::string("\"") + value + "\""; // We have to walk value and escape any special characters. // Appending to std::string is not efficient, but this should be rare. // (Note: forward slashes are *not* rare, but I am not escaping them.) - unsigned maxsize = strlen (value) * 2 + 3; // allescaped+quotes+NULL + unsigned maxsize = strlen(value) * 2 + 3; // allescaped+quotes+NULL std::string result; - result.reserve (maxsize); // to avoid lots of mallocs + result.reserve(maxsize); // to avoid lots of mallocs result += "\""; for (const char* c = value; *c != 0; ++c) { switch (*c) { - case '\"': - result += "\\\""; - break; + case '\"': + result += "\\\""; + break; - case '\\': - result += "\\\\"; - break; + case '\\': + result += "\\\\"; + break; - case '\b': - result += "\\b"; - break; + case '\b': + result += "\\b"; + break; - case '\f': - result += "\\f"; - break; + case '\f': + result += "\\f"; + break; - case '\n': - result += "\\n"; - break; + case '\n': + result += "\\n"; + break; - case '\r': - result += "\\r"; - break; + case '\r': + result += "\\r"; + break; - case '\t': - result += "\\t"; - break; + case '\t': + result += "\\t"; + break; - //case '/': - // Even though \/ is considered a legal escape in JSON, a bare - // slash is also legal, so I see no reason to escape it. - // (I hope I am not misunderstanding something. - // blep notes: actually escaping \/ may be useful in javascript to avoid (*c); - result += oss.str (); - } - else - { - result += *c; - } + // case '/': + // Even though \/ is considered a legal escape in JSON, a bare + // slash is also legal, so I see no reason to escape it. + // (I hope I am not misunderstanding something. + // blep notes: actually escaping \/ may be useful in javascript + // to avoid (*c); + result += oss.str(); + } + else + { + result += *c; + } - break; + break; } } @@ -181,225 +189,214 @@ std::string valueToQuotedString ( const char* value ) // Class FastWriter // ////////////////////////////////////////////////////////////////// - std::string -FastWriter::write ( const Value& root ) +FastWriter::write(const Value& root) { document_ = ""; - writeValue ( root ); - return std::move (document_); + writeValue(root); + return std::move(document_); } - void -FastWriter::writeValue ( const Value& value ) +FastWriter::writeValue(const Value& value) { - switch ( value.type () ) + switch (value.type()) { - case nullValue: - document_ += "null"; - break; + case nullValue: + document_ += "null"; + break; - case intValue: - document_ += valueToString ( value.asInt () ); - break; + case intValue: + document_ += valueToString(value.asInt()); + break; - case uintValue: - document_ += valueToString ( value.asUInt () ); - break; + case uintValue: + document_ += valueToString(value.asUInt()); + break; - case realValue: - document_ += valueToString ( value.asDouble () ); - break; + case realValue: + document_ += valueToString(value.asDouble()); + break; - case stringValue: - document_ += valueToQuotedString ( value.asCString () ); - break; + case stringValue: + document_ += valueToQuotedString(value.asCString()); + break; - case booleanValue: - document_ += valueToString ( value.asBool () ); - break; + case booleanValue: + document_ += valueToString(value.asBool()); + break; - case arrayValue: - { - document_ += "["; - int size = value.size (); + case arrayValue: { + document_ += "["; + int size = value.size(); - for ( int index = 0; index < size; ++index ) - { - if ( index > 0 ) - document_ += ","; + for (int index = 0; index < size; ++index) + { + if (index > 0) + document_ += ","; - writeValue ( value[index] ); + writeValue(value[index]); + } + + document_ += "]"; } + break; - document_ += "]"; - } - break; + case objectValue: { + Value::Members members(value.getMemberNames()); + document_ += "{"; - case objectValue: - { - Value::Members members ( value.getMemberNames () ); - document_ += "{"; + for (Value::Members::iterator it = members.begin(); + it != members.end(); + ++it) + { + std::string const& name = *it; - for ( Value::Members::iterator it = members.begin (); - it != members.end (); - ++it ) - { - std::string const& name = *it; + if (it != members.begin()) + document_ += ","; - if ( it != members.begin () ) - document_ += ","; + document_ += valueToQuotedString(name.c_str()); + document_ += ":"; + writeValue(value[name]); + } - document_ += valueToQuotedString ( name.c_str () ); - document_ += ":"; - writeValue ( value[name] ); + document_ += "}"; } - - document_ += "}"; - } - break; + break; } } - // Class StyledWriter // ////////////////////////////////////////////////////////////////// -StyledWriter::StyledWriter () - : rightMargin_ ( 74 ) - , indentSize_ ( 3 ) +StyledWriter::StyledWriter() : rightMargin_(74), indentSize_(3) { } - std::string -StyledWriter::write ( const Value& root ) +StyledWriter::write(const Value& root) { document_ = ""; addChildValues_ = false; indentString_ = ""; - writeValue ( root ); + writeValue(root); document_ += "\n"; return document_; } - void -StyledWriter::writeValue ( const Value& value ) +StyledWriter::writeValue(const Value& value) { - switch ( value.type () ) + switch (value.type()) { - case nullValue: - pushValue ( "null" ); - break; + case nullValue: + pushValue("null"); + break; - case intValue: - pushValue ( valueToString ( value.asInt () ) ); - break; + case intValue: + pushValue(valueToString(value.asInt())); + break; - case uintValue: - pushValue ( valueToString ( value.asUInt () ) ); - break; + case uintValue: + pushValue(valueToString(value.asUInt())); + break; - case realValue: - pushValue ( valueToString ( value.asDouble () ) ); - break; + case realValue: + pushValue(valueToString(value.asDouble())); + break; - case stringValue: - pushValue ( valueToQuotedString ( value.asCString () ) ); - break; + case stringValue: + pushValue(valueToQuotedString(value.asCString())); + break; - case booleanValue: - pushValue ( valueToString ( value.asBool () ) ); - break; + case booleanValue: + pushValue(valueToString(value.asBool())); + break; - case arrayValue: - writeArrayValue ( value); - break; + case arrayValue: + writeArrayValue(value); + break; - case objectValue: - { - Value::Members members ( value.getMemberNames () ); + case objectValue: { + Value::Members members(value.getMemberNames()); - if ( members.empty () ) - pushValue ( "{}" ); - else - { - writeWithIndent ( "{" ); - indent (); - Value::Members::iterator it = members.begin (); - - while ( true ) + if (members.empty()) + pushValue("{}"); + else { - std::string const& name = *it; - const Value& childValue = value[name]; - writeWithIndent ( valueToQuotedString ( name.c_str () ) ); - document_ += " : "; - writeValue ( childValue ); + writeWithIndent("{"); + indent(); + Value::Members::iterator it = members.begin(); - if ( ++it == members.end () ) - break; + while (true) + { + std::string const& name = *it; + const Value& childValue = value[name]; + writeWithIndent(valueToQuotedString(name.c_str())); + document_ += " : "; + writeValue(childValue); - document_ += ","; + if (++it == members.end()) + break; + + document_ += ","; + } + + unindent(); + writeWithIndent("}"); } - - unindent (); - writeWithIndent ( "}" ); } - } - break; + break; } } - void -StyledWriter::writeArrayValue ( const Value& value ) +StyledWriter::writeArrayValue(const Value& value) { - unsigned size = value.size (); + unsigned size = value.size(); - if ( size == 0 ) - pushValue ( "[]" ); + if (size == 0) + pushValue("[]"); else { - bool isArrayMultiLine = isMultineArray ( value ); + bool isArrayMultiLine = isMultineArray(value); - if ( isArrayMultiLine ) + if (isArrayMultiLine) { - writeWithIndent ( "[" ); - indent (); - bool hasChildValue = !childValues_.empty (); + writeWithIndent("["); + indent(); + bool hasChildValue = !childValues_.empty(); unsigned index = 0; - while ( true ) + while (true) { const Value& childValue = value[index]; - if ( hasChildValue ) - writeWithIndent ( childValues_[index] ); + if (hasChildValue) + writeWithIndent(childValues_[index]); else { - writeIndent (); - writeValue ( childValue ); + writeIndent(); + writeValue(childValue); } - if ( ++index == size ) + if (++index == size) break; document_ += ","; } - unindent (); - writeWithIndent ( "]" ); + unindent(); + writeWithIndent("]"); } - else // output on a single line + else // output on a single line { - assert ( childValues_.size () == size ); + assert(childValues_.size() == size); document_ += "[ "; - for ( unsigned index = 0; index < size; ++index ) + for (unsigned index = 0; index < size; ++index) { - if ( index > 0 ) + if (index > 0) document_ += ", "; document_ += childValues_[index]; @@ -410,230 +407,218 @@ StyledWriter::writeArrayValue ( const Value& value ) } } - bool -StyledWriter::isMultineArray ( const Value& value ) +StyledWriter::isMultineArray(const Value& value) { - int size = value.size (); - bool isMultiLine = size * 3 >= rightMargin_ ; - childValues_.clear (); + int size = value.size(); + bool isMultiLine = size * 3 >= rightMargin_; + childValues_.clear(); - for ( int index = 0; index < size && !isMultiLine; ++index ) + for (int index = 0; index < size && !isMultiLine; ++index) { const Value& childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size () > 0 ); + isMultiLine = isMultiLine || + ((childValue.isArray() || childValue.isObject()) && + childValue.size() > 0); } - if ( !isMultiLine ) // check if line length > max line length + if (!isMultiLine) // check if line length > max line length { - childValues_.reserve ( size ); + childValues_.reserve(size); addChildValues_ = true; - int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' + int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' - for ( int index = 0; index < size; ++index ) + for (int index = 0; index < size; ++index) { - writeValue ( value[index] ); - lineLength += int ( childValues_[index].length () ); + writeValue(value[index]); + lineLength += int(childValues_[index].length()); } addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; + isMultiLine = isMultiLine || lineLength >= rightMargin_; } return isMultiLine; } - void -StyledWriter::pushValue ( std::string const& value ) +StyledWriter::pushValue(std::string const& value) { - if ( addChildValues_ ) - childValues_.push_back ( value ); + if (addChildValues_) + childValues_.push_back(value); else document_ += value; } - void -StyledWriter::writeIndent () +StyledWriter::writeIndent() { - if ( !document_.empty () ) + if (!document_.empty()) { - char last = document_[document_.length () - 1]; + char last = document_[document_.length() - 1]; - if ( last == ' ' ) // already indented + if (last == ' ') // already indented return; - if ( last != '\n' ) // Comments may add new-line + if (last != '\n') // Comments may add new-line document_ += '\n'; } document_ += indentString_; } - void -StyledWriter::writeWithIndent ( std::string const& value ) +StyledWriter::writeWithIndent(std::string const& value) { - writeIndent (); + writeIndent(); document_ += value; } - void -StyledWriter::indent () +StyledWriter::indent() { - indentString_ += std::string ( indentSize_, ' ' ); + indentString_ += std::string(indentSize_, ' '); } - void -StyledWriter::unindent () +StyledWriter::unindent() { - assert ( int (indentString_.size ()) >= indentSize_ ); - indentString_.resize ( indentString_.size () - indentSize_ ); + assert(int(indentString_.size()) >= indentSize_); + indentString_.resize(indentString_.size() - indentSize_); } // Class StyledStreamWriter // ////////////////////////////////////////////////////////////////// -StyledStreamWriter::StyledStreamWriter ( std::string indentation ) - : document_ (nullptr) - , rightMargin_ ( 74 ) - , indentation_ ( indentation ) +StyledStreamWriter::StyledStreamWriter(std::string indentation) + : document_(nullptr), rightMargin_(74), indentation_(indentation) { } - void -StyledStreamWriter::write ( std::ostream& out, const Value& root ) +StyledStreamWriter::write(std::ostream& out, const Value& root) { document_ = &out; addChildValues_ = false; indentString_ = ""; - writeValue ( root ); + writeValue(root); *document_ << "\n"; - document_ = nullptr; // Forget the stream, for safety. + document_ = nullptr; // Forget the stream, for safety. } - void -StyledStreamWriter::writeValue ( const Value& value ) +StyledStreamWriter::writeValue(const Value& value) { - switch ( value.type () ) + switch (value.type()) { - case nullValue: - pushValue ( "null" ); - break; + case nullValue: + pushValue("null"); + break; - case intValue: - pushValue ( valueToString ( value.asInt () ) ); - break; + case intValue: + pushValue(valueToString(value.asInt())); + break; - case uintValue: - pushValue ( valueToString ( value.asUInt () ) ); - break; + case uintValue: + pushValue(valueToString(value.asUInt())); + break; - case realValue: - pushValue ( valueToString ( value.asDouble () ) ); - break; + case realValue: + pushValue(valueToString(value.asDouble())); + break; - case stringValue: - pushValue ( valueToQuotedString ( value.asCString () ) ); - break; + case stringValue: + pushValue(valueToQuotedString(value.asCString())); + break; - case booleanValue: - pushValue ( valueToString ( value.asBool () ) ); - break; + case booleanValue: + pushValue(valueToString(value.asBool())); + break; - case arrayValue: - writeArrayValue ( value); - break; + case arrayValue: + writeArrayValue(value); + break; - case objectValue: - { - Value::Members members ( value.getMemberNames () ); + case objectValue: { + Value::Members members(value.getMemberNames()); - if ( members.empty () ) - pushValue ( "{}" ); - else - { - writeWithIndent ( "{" ); - indent (); - Value::Members::iterator it = members.begin (); - - while ( true ) + if (members.empty()) + pushValue("{}"); + else { - std::string const& name = *it; - const Value& childValue = value[name]; - writeWithIndent ( valueToQuotedString ( name.c_str () ) ); - *document_ << " : "; - writeValue ( childValue ); + writeWithIndent("{"); + indent(); + Value::Members::iterator it = members.begin(); - if ( ++it == members.end () ) - break; + while (true) + { + std::string const& name = *it; + const Value& childValue = value[name]; + writeWithIndent(valueToQuotedString(name.c_str())); + *document_ << " : "; + writeValue(childValue); - *document_ << ","; + if (++it == members.end()) + break; + + *document_ << ","; + } + + unindent(); + writeWithIndent("}"); } - - unindent (); - writeWithIndent ( "}" ); } - } - break; + break; } } - void -StyledStreamWriter::writeArrayValue ( const Value& value ) +StyledStreamWriter::writeArrayValue(const Value& value) { - unsigned size = value.size (); + unsigned size = value.size(); - if ( size == 0 ) - pushValue ( "[]" ); + if (size == 0) + pushValue("[]"); else { - bool isArrayMultiLine = isMultineArray ( value ); + bool isArrayMultiLine = isMultineArray(value); - if ( isArrayMultiLine ) + if (isArrayMultiLine) { - writeWithIndent ( "[" ); - indent (); - bool hasChildValue = !childValues_.empty (); + writeWithIndent("["); + indent(); + bool hasChildValue = !childValues_.empty(); unsigned index = 0; - while ( true ) + while (true) { const Value& childValue = value[index]; - if ( hasChildValue ) - writeWithIndent ( childValues_[index] ); + if (hasChildValue) + writeWithIndent(childValues_[index]); else { - writeIndent (); - writeValue ( childValue ); + writeIndent(); + writeValue(childValue); } - if ( ++index == size ) + if (++index == size) break; *document_ << ","; } - unindent (); - writeWithIndent ( "]" ); + unindent(); + writeWithIndent("]"); } - else // output on a single line + else // output on a single line { - assert ( childValues_.size () == size ); + assert(childValues_.size() == size); *document_ << "[ "; - for ( unsigned index = 0; index < size; ++index ) + for (unsigned index = 0; index < size; ++index) { - if ( index > 0 ) + if (index > 0) *document_ << ", "; *document_ << childValues_[index]; @@ -644,54 +629,51 @@ StyledStreamWriter::writeArrayValue ( const Value& value ) } } - bool -StyledStreamWriter::isMultineArray ( const Value& value ) +StyledStreamWriter::isMultineArray(const Value& value) { - int size = value.size (); - bool isMultiLine = size * 3 >= rightMargin_ ; - childValues_.clear (); + int size = value.size(); + bool isMultiLine = size * 3 >= rightMargin_; + childValues_.clear(); - for ( int index = 0; index < size && !isMultiLine; ++index ) + for (int index = 0; index < size && !isMultiLine; ++index) { const Value& childValue = value[index]; - isMultiLine = isMultiLine || - ( (childValue.isArray() || childValue.isObject()) && - childValue.size () > 0 ); + isMultiLine = isMultiLine || + ((childValue.isArray() || childValue.isObject()) && + childValue.size() > 0); } - if ( !isMultiLine ) // check if line length > max line length + if (!isMultiLine) // check if line length > max line length { - childValues_.reserve ( size ); + childValues_.reserve(size); addChildValues_ = true; - int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' + int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]' - for ( int index = 0; index < size; ++index ) + for (int index = 0; index < size; ++index) { - writeValue ( value[index] ); - lineLength += int ( childValues_[index].length () ); + writeValue(value[index]); + lineLength += int(childValues_[index].length()); } addChildValues_ = false; - isMultiLine = isMultiLine || lineLength >= rightMargin_; + isMultiLine = isMultiLine || lineLength >= rightMargin_; } return isMultiLine; } - void -StyledStreamWriter::pushValue ( std::string const& value ) +StyledStreamWriter::pushValue(std::string const& value) { - if ( addChildValues_ ) - childValues_.push_back ( value ); + if (addChildValues_) + childValues_.push_back(value); else *document_ << value; } - void -StyledStreamWriter::writeIndent () +StyledStreamWriter::writeIndent() { /* Some comments in this method would have been nice. ;-) @@ -708,35 +690,32 @@ StyledStreamWriter::writeIndent () *document_ << '\n' << indentString_; } - void -StyledStreamWriter::writeWithIndent ( std::string const& value ) +StyledStreamWriter::writeWithIndent(std::string const& value) { - writeIndent (); + writeIndent(); *document_ << value; } - void -StyledStreamWriter::indent () +StyledStreamWriter::indent() { indentString_ += indentation_; } - void -StyledStreamWriter::unindent () +StyledStreamWriter::unindent() { - assert ( indentString_.size () >= indentation_.size () ); - indentString_.resize ( indentString_.size () - indentation_.size () ); + assert(indentString_.size() >= indentation_.size()); + indentString_.resize(indentString_.size() - indentation_.size()); } - -std::ostream& operator<< ( std::ostream& sout, const Value& root ) +std::ostream& +operator<<(std::ostream& sout, const Value& root) { Json::StyledStreamWriter writer; - writer.write (sout, root); + writer.write(sout, root); return sout; } -} // namespace Json +} // namespace Json diff --git a/src/ripple/json/impl/to_string.cpp b/src/ripple/json/impl/to_string.cpp index 7dccfe4a02..b965417721 100644 --- a/src/ripple/json/impl/to_string.cpp +++ b/src/ripple/json/impl/to_string.cpp @@ -20,17 +20,18 @@ #include #include -namespace Json -{ +namespace Json { -std::string to_string (Value const& value) +std::string +to_string(Value const& value) { - return FastWriter ().write (value); + return FastWriter().write(value); } -std::string pretty (Value const& value) +std::string +pretty(Value const& value) { - return StyledWriter().write (value); + return StyledWriter().write(value); } -} // namespace Json +} // namespace Json diff --git a/src/ripple/json/json_errors.h b/src/ripple/json/json_errors.h index 558d99e3a7..4bed420409 100644 --- a/src/ripple/json/json_errors.h +++ b/src/ripple/json/json_errors.h @@ -22,15 +22,13 @@ #include -namespace Json -{ +namespace Json { struct error : std::runtime_error { using std::runtime_error::runtime_error; }; -} // namespace Json +} // namespace Json - -#endif // JSON_FORWARDS_H_INCLUDED +#endif // JSON_FORWARDS_H_INCLUDED diff --git a/src/ripple/json/json_forwards.h b/src/ripple/json/json_forwards.h index 6e8014ad8f..e77ea58169 100644 --- a/src/ripple/json/json_forwards.h +++ b/src/ripple/json/json_forwards.h @@ -20,8 +20,7 @@ #ifndef RIPPLE_JSON_JSON_FORWARDS_H_INCLUDED #define RIPPLE_JSON_JSON_FORWARDS_H_INCLUDED -namespace Json -{ +namespace Json { // value.h using Int = int; @@ -32,7 +31,6 @@ class ValueIteratorBase; class ValueIterator; class ValueConstIterator; -} // namespace Json +} // namespace Json - -#endif // JSON_FORWARDS_H_INCLUDED +#endif // JSON_FORWARDS_H_INCLUDED diff --git a/src/ripple/json/json_reader.h b/src/ripple/json/json_reader.h index 0ec4cdbe02..caa657eb76 100644 --- a/src/ripple/json/json_reader.h +++ b/src/ripple/json/json_reader.h @@ -20,17 +20,17 @@ #ifndef RIPPLE_JSON_JSON_READER_H_INCLUDED #define RIPPLE_JSON_JSON_READER_H_INCLUDED -# define CPPTL_JSON_READER_H_INCLUDED +#define CPPTL_JSON_READER_H_INCLUDED #include #include #include #include -namespace Json -{ +namespace Json { -/** \brief Unserialize a JSON document into a Value. +/** \brief Unserialize a JSON document into a + * Value. * */ class Reader @@ -42,50 +42,55 @@ public: /** \brief Constructs a Reader allowing all features * for parsing. */ - Reader () = default; + Reader() = default; - /** \brief Read a Value from a JSON document. - * \param document UTF-8 encoded string containing the document to read. - * \param root [out] Contains the root value of the document if it was + /** \brief Read a Value from a JSON + * document. \param document UTF-8 encoded string containing the document to + * read. \param root [out] Contains the root value of the document if it was * successfully parsed. - * \return \c true if the document was successfully parsed, \c false if an error occurred. + * \return \c true if the document was successfully parsed, \c false if an + * error occurred. */ - bool parse ( std::string const& document, Value& root); + bool + parse(std::string const& document, Value& root); - /** \brief Read a Value from a JSON document. - * \param document UTF-8 encoded string containing the document to read. - * \param root [out] Contains the root value of the document if it was + /** \brief Read a Value from a JSON + * document. \param document UTF-8 encoded string containing the document to + * read. \param root [out] Contains the root value of the document if it was * successfully parsed. - * \return \c true if the document was successfully parsed, \c false if an error occurred. + * \return \c true if the document was successfully parsed, \c false if an + * error occurred. */ - bool parse ( const char* beginDoc, const char* endDoc, Value& root); + bool + parse(const char* beginDoc, const char* endDoc, Value& root); /// \brief Parse from input stream. /// \see Json::operator>>(std::istream&, Json::Value&). - bool parse ( std::istream& is, Value& root); + bool + parse(std::istream& is, Value& root); - /** \brief Read a Value from a JSON buffer sequence. - * \param root [out] Contains the root value of the document if it was - * successfully parsed. - * \param UTF-8 encoded buffer sequence. - * \return \c true if the buffer was successfully parsed, \c false if an error occurred. + /** \brief Read a Value from a JSON buffer + * sequence. \param root [out] Contains the root value of the document if it + * was successfully parsed. \param UTF-8 encoded buffer sequence. \return \c + * true if the buffer was successfully parsed, \c false if an error + * occurred. */ - template + template bool parse(Value& root, BufferSequence const& bs); - /** \brief Returns a user friendly string that list errors in the parsed document. - * \return Formatted error message with the list of errors with their location in - * the parsed document. An empty string is returned if no error occurred - * during parsing. + /** \brief Returns a user friendly string that list errors in the parsed + * document. \return Formatted error message with the list of errors with + * their location in the parsed document. An empty string is returned if no + * error occurred during parsing. */ - std::string getFormatedErrorMessages () const; + std::string + getFormatedErrorMessages() const; - static constexpr unsigned nest_limit {25}; + static constexpr unsigned nest_limit{25}; private: - enum TokenType - { + enum TokenType { tokenEndOfStream = 0, tokenObjectBegin, tokenObjectEnd, @@ -125,46 +130,71 @@ private: using Errors = std::deque; - bool expectToken ( TokenType type, Token& token, const char* message ); - bool readToken ( Token& token ); - void skipSpaces (); - bool match ( Location pattern, - int patternLength ); - bool readComment (); - bool readCStyleComment (); - bool readCppStyleComment (); - bool readString (); - Reader::TokenType readNumber (); - bool readValue(unsigned depth); - bool readObject(Token& token, unsigned depth); - bool readArray (Token& token, unsigned depth); - bool decodeNumber ( Token& token ); - bool decodeString ( Token& token ); - bool decodeString ( Token& token, std::string& decoded ); - bool decodeDouble ( Token& token ); - bool decodeUnicodeCodePoint ( Token& token, - Location& current, - Location end, - unsigned int& unicode ); - bool decodeUnicodeEscapeSequence ( Token& token, - Location& current, - Location end, - unsigned int& unicode ); - bool addError ( std::string const& message, - Token& token, - Location extra = 0 ); - bool recoverFromError ( TokenType skipUntilToken ); - bool addErrorAndRecover ( std::string const& message, - Token& token, - TokenType skipUntilToken ); - void skipUntilSpace (); - Value& currentValue (); - Char getNextChar (); - void getLocationLineAndColumn ( Location location, - int& line, - int& column ) const; - std::string getLocationLineAndColumn ( Location location ) const; - void skipCommentTokens ( Token& token ); + bool + expectToken(TokenType type, Token& token, const char* message); + bool + readToken(Token& token); + void + skipSpaces(); + bool + match(Location pattern, int patternLength); + bool + readComment(); + bool + readCStyleComment(); + bool + readCppStyleComment(); + bool + readString(); + Reader::TokenType + readNumber(); + bool + readValue(unsigned depth); + bool + readObject(Token& token, unsigned depth); + bool + readArray(Token& token, unsigned depth); + bool + decodeNumber(Token& token); + bool + decodeString(Token& token); + bool + decodeString(Token& token, std::string& decoded); + bool + decodeDouble(Token& token); + bool + decodeUnicodeCodePoint( + Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool + decodeUnicodeEscapeSequence( + Token& token, + Location& current, + Location end, + unsigned int& unicode); + bool + addError(std::string const& message, Token& token, Location extra = 0); + bool + recoverFromError(TokenType skipUntilToken); + bool + addErrorAndRecover( + std::string const& message, + Token& token, + TokenType skipUntilToken); + void + skipUntilSpace(); + Value& + currentValue(); + Char + getNextChar(); + void + getLocationLineAndColumn(Location location, int& line, int& column) const; + std::string + getLocationLineAndColumn(Location location) const; + void + skipCommentTokens(Token& token); using Nodes = std::stack; Nodes nodes_; @@ -177,13 +207,13 @@ private: Value* lastValue_; }; -template +template bool Reader::parse(Value& root, BufferSequence const& bs) { using namespace boost::asio; std::string s; - s.reserve (buffer_size(bs)); + s.reserve(buffer_size(bs)); for (auto const& b : bs) s.append(buffer_cast(b), buffer_size(b)); return parse(s, root); @@ -213,8 +243,9 @@ Reader::parse(Value& root, BufferSequence const& bs) \throw std::exception on parse error. \see Json::operator<<() */ -std::istream& operator>> ( std::istream&, Value& ); +std::istream& +operator>>(std::istream&, Value&); -} // namespace Json +} // namespace Json -#endif // CPPTL_JSON_READER_H_INCLUDED +#endif // CPPTL_JSON_READER_H_INCLUDED diff --git a/src/ripple/json/json_value.h b/src/ripple/json/json_value.h index 1b111d69e9..503a053a9c 100644 --- a/src/ripple/json/json_value.h +++ b/src/ripple/json/json_value.h @@ -28,21 +28,19 @@ /** \brief JSON (JavaScript Object Notation). */ -namespace Json -{ +namespace Json { /** \brief Type of the value held by a Value object. */ -enum ValueType -{ - nullValue = 0, ///< 'null' value - intValue, ///< signed integer value - uintValue, ///< unsigned integer value - realValue, ///< double value - stringValue, ///< UTF-8 string value - booleanValue, ///< bool value - arrayValue, ///< array value (ordered list) - objectValue ///< object value (collection of name/value pairs). +enum ValueType { + nullValue = 0, ///< 'null' value + intValue, ///< signed integer value + uintValue, ///< unsigned integer value + realValue, ///< double value + stringValue, ///< UTF-8 string value + booleanValue, ///< bool value + arrayValue, ///< array value (ordered list) + objectValue ///< object value (collection of name/value pairs). }; /** \brief Lightweight wrapper to tag static string. @@ -62,17 +60,17 @@ enum ValueType class StaticString { public: - constexpr explicit StaticString ( const char* czstring ) - : str_ ( czstring ) + constexpr explicit StaticString(const char* czstring) : str_(czstring) { } - constexpr operator const char* () const + constexpr operator const char*() const { return str_; } - constexpr const char* c_str () const + constexpr const char* + c_str() const { return str_; } @@ -81,34 +79,40 @@ private: const char* str_; }; -inline bool operator== (StaticString x, StaticString y) -{ - return strcmp (x.c_str(), y.c_str()) == 0; -} - -inline bool operator!= (StaticString x, StaticString y) -{ - return ! (x == y); -} - -inline bool operator== (std::string const& x, StaticString y) +inline bool +operator==(StaticString x, StaticString y) { return strcmp(x.c_str(), y.c_str()) == 0; } -inline bool operator!= (std::string const& x, StaticString y) +inline bool +operator!=(StaticString x, StaticString y) { - return ! (x == y); + return !(x == y); } -inline bool operator== (StaticString x, std::string const& y) +inline bool +operator==(std::string const& x, StaticString y) +{ + return strcmp(x.c_str(), y.c_str()) == 0; +} + +inline bool +operator!=(std::string const& x, StaticString y) +{ + return !(x == y); +} + +inline bool +operator==(StaticString x, std::string const& y) { return y == x; } -inline bool operator!= (StaticString x, std::string const& y) +inline bool +operator!=(StaticString x, std::string const& y) { - return ! (y == x); + return !(y == x); } /** \brief Represents a JSON value. @@ -126,14 +130,14 @@ inline bool operator!= (StaticString x, std::string const& y) * The type of the held value is represented by a #ValueType and * can be obtained using type(). * - * values of an #objectValue or #arrayValue can be accessed using operator[]() methods. - * Non const methods will automatically create the a #nullValue element + * values of an #objectValue or #arrayValue can be accessed using operator[]() + * methods. Non const methods will automatically create the a #nullValue element * if it does not exist. * The sequence of an #arrayValue will be automatically resize and initialized * with #nullValue. resize() can be used to enlarge or truncate an #arrayValue. * - * The get() methods can be used to obtanis default value in the case the required element - * does not exist. + * The get() methods can be used to obtanis default value in the case the + * required element does not exist. * * It is possible to iterate over the list of a #objectValue values using * the getMemberNames() method. @@ -159,22 +163,28 @@ private: class CZString { public: - enum DuplicationPolicy - { + enum DuplicationPolicy { noDuplication = 0, duplicate, duplicateOnCopy }; - CZString ( int index ); - CZString ( const char* cstr, DuplicationPolicy allocate ); - CZString ( const CZString& other ); - ~CZString (); - CZString& operator = ( const CZString& other ) = delete; - bool operator< ( const CZString& other ) const; - bool operator== ( const CZString& other ) const; - int index () const; - const char* c_str () const; - bool isStaticString () const; + CZString(int index); + CZString(const char* cstr, DuplicationPolicy allocate); + CZString(const CZString& other); + ~CZString(); + CZString& + operator=(const CZString& other) = delete; + bool + operator<(const CZString& other) const; + bool + operator==(const CZString& other) const; + int + index() const; + const char* + c_str() const; + bool + isStaticString() const; + private: const char* cstr_; int index_; @@ -199,109 +209,149 @@ public: Json::Value obj_value(Json::objectValue); // {} \endcode */ - Value ( ValueType type = nullValue ); - Value ( Int value ); - Value ( UInt value ); - Value ( double value ); - Value ( const char* value ); + Value(ValueType type = nullValue); + Value(Int value); + Value(UInt value); + Value(double value); + Value(const char* value); /** \brief Constructs a value from a static string. * Like other value string constructor but do not duplicate the string for - * internal storage. The given string must remain alive after the call to this + * internal storage. The given string must remain alive after the call to + this * constructor. * Example of usage: * \code * Json::Value aValue( StaticString("some text") ); * \endcode */ - Value ( const StaticString& value ); - Value ( std::string const& value ); - Value ( bool value ); - Value ( const Value& other ); - ~Value (); + Value(const StaticString& value); + Value(std::string const& value); + Value(bool value); + Value(const Value& other); + ~Value(); - Value& operator= ( Value const& other ); - Value& operator= ( Value&& other ); + Value& + operator=(Value const& other); + Value& + operator=(Value&& other); - Value ( Value&& other ) noexcept; + Value(Value&& other) noexcept; /// Swap values. - void swap ( Value& other ) noexcept; + void + swap(Value& other) noexcept; - ValueType type () const; + ValueType + type() const; - const char* asCString () const; + const char* + asCString() const; /** Returns the unquoted string value. */ - std::string asString () const; - Int asInt () const; - UInt asUInt () const; - double asDouble () const; - bool asBool () const; + std::string + asString() const; + Int + asInt() const; + UInt + asUInt() const; + double + asDouble() const; + bool + asBool() const; // TODO: What is the "empty()" method this docstring mentions? /** isNull() tests to see if this field is null. Don't use this method to test for emptiness: use empty(). */ - bool isNull () const; - bool isBool () const; - bool isInt () const; - bool isUInt () const; - bool isIntegral () const; - bool isDouble () const; - bool isNumeric () const; - bool isString () const; - bool isArray() const; - bool isArrayOrNull () const; - bool isObject() const; - bool isObjectOrNull () const; + bool + isNull() const; + bool + isBool() const; + bool + isInt() const; + bool + isUInt() const; + bool + isIntegral() const; + bool + isDouble() const; + bool + isNumeric() const; + bool + isString() const; + bool + isArray() const; + bool + isArrayOrNull() const; + bool + isObject() const; + bool + isObjectOrNull() const; - bool isConvertibleTo ( ValueType other ) const; + bool + isConvertibleTo(ValueType other) const; /// Number of values in array or object - UInt size () const; + UInt + size() const; /** Returns false if this is an empty array, empty object, empty string, or null. */ - explicit - operator bool() const; + explicit operator bool() const; /// Remove all object members and array elements. /// \pre type() is arrayValue, objectValue, or nullValue /// \post type() is unchanged - void clear (); + void + clear(); /// Access an array element (zero based index ). - /// If the array contains less than index element, then null value are inserted - /// in the array so that its size is index+1. - /// (You may need to say 'value[0u]' to get your compiler to distinguish + /// If the array contains less than index element, then null value are + /// inserted in the array so that its size is index+1. (You may need to say + /// 'value[0u]' to get your compiler to distinguish /// this from the operator[] which takes a string.) - Value& operator[] ( UInt index ); + Value& + operator[](UInt index); /// Access an array element (zero based index ) /// (You may need to say 'value[0u]' to get your compiler to distinguish /// this from the operator[] which takes a string.) - const Value& operator[] ( UInt index ) const; - /// If the array contains at least index+1 elements, returns the element value, - /// otherwise returns defaultValue. - Value get ( UInt index, - const Value& defaultValue ) const; + const Value& + operator[](UInt index) const; + /// If the array contains at least index+1 elements, returns the element + /// value, otherwise returns defaultValue. + Value + get(UInt index, const Value& defaultValue) const; /// Return true if index < size(). - bool isValidIndex ( UInt index ) const; + bool + isValidIndex(UInt index) const; /// \brief Append value to array at the end. /// /// Equivalent to jsonvalue[jsonvalue.size()] = value; - Value& append ( const Value& value ); - Value& append ( Value&& value ); + Value& + append(const Value& value); + Value& + append(Value&& value); - /// Access an object value by name, create a null member if it does not exist. - Value& operator[] ( const char* key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value& operator[] ( const char* key ) const; - /// Access an object value by name, create a null member if it does not exist. - Value& operator[] ( std::string const& key ); - /// Access an object value by name, returns null if there is no member with that name. - const Value& operator[] ( std::string const& key ) const; - /** \brief Access an object value by name, create a null member if it does not exist. + /// Access an object value by name, create a null member if it does not + /// exist. + Value& + operator[](const char* key); + /// Access an object value by name, returns null if there is no member with + /// that name. + const Value& + operator[](const char* key) const; + /// Access an object value by name, create a null member if it does not + /// exist. + Value& + operator[](std::string const& key); + /// Access an object value by name, returns null if there is no member with + /// that name. + const Value& + operator[](std::string const& key) const; + /** \brief Access an object value by name, create a null member if it does + not exist. - * If the object as no entry for that name, then the member name used to store + * If the object as no entry for that name, then the member name used to + store * the new entry is not duplicated. * Example of use: * \code @@ -310,14 +360,15 @@ public: * object[code] = 1234; * \endcode */ - Value& operator[] ( const StaticString& key ); + Value& + operator[](const StaticString& key); /// Return the member named key if it exist, defaultValue otherwise. - Value get ( const char* key, - const Value& defaultValue ) const; + Value + get(const char* key, const Value& defaultValue) const; /// Return the member named key if it exist, defaultValue otherwise. - Value get ( std::string const& key, - const Value& defaultValue ) const; + Value + get(std::string const& key, const Value& defaultValue) const; /// \brief Remove and return the named member. /// @@ -325,36 +376,48 @@ public: /// \return the removed Value, or null. /// \pre type() is objectValue or nullValue /// \post type() is unchanged - Value removeMember ( const char* key ); + Value + removeMember(const char* key); /// Same as removeMember(const char*) - Value removeMember ( std::string const& key ); + Value + removeMember(std::string const& key); /// Return true if the object has a member named key. - bool isMember ( const char* key ) const; + bool + isMember(const char* key) const; /// Return true if the object has a member named key. - bool isMember ( std::string const& key ) const; + bool + isMember(std::string const& key) const; /// \brief Return a list of the member names. /// /// If null, return an empty list. /// \pre type() is objectValue or nullValue /// \post if type() was nullValue, it remains nullValue - Members getMemberNames () const; + Members + getMemberNames() const; - std::string toStyledString () const; + std::string + toStyledString() const; - const_iterator begin () const; - const_iterator end () const; + const_iterator + begin() const; + const_iterator + end() const; - iterator begin (); - iterator end (); + iterator + begin(); + iterator + end(); - friend bool operator== (const Value&, const Value&); - friend bool operator< (const Value&, const Value&); + friend bool + operator==(const Value&, const Value&); + friend bool + operator<(const Value&, const Value&); private: - Value& resolveReference ( const char* key, - bool isStatic ); + Value& + resolveReference(const char* key, bool isStatic); private: union ValueHolder @@ -364,59 +427,65 @@ private: double real_; bool bool_; char* string_; - ObjectValues* map_ {nullptr}; + ObjectValues* map_{nullptr}; } value_; ValueType type_ : 8; - int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. + int allocated_ : 1; // Notes: if declared as bool, bitfield is useless. }; -bool operator== (const Value&, const Value&); +bool +operator==(const Value&, const Value&); -inline -bool operator!= (const Value& x, const Value& y) +inline bool +operator!=(const Value& x, const Value& y) { - return ! (x == y); + return !(x == y); } -bool operator< (const Value&, const Value&); +bool +operator<(const Value&, const Value&); -inline -bool operator<= (const Value& x, const Value& y) +inline bool +operator<=(const Value& x, const Value& y) { - return ! (y < x); + return !(y < x); } -inline -bool operator> (const Value& x, const Value& y) +inline bool +operator>(const Value& x, const Value& y) { return y < x; } -inline -bool operator>= (const Value& x, const Value& y) +inline bool +operator>=(const Value& x, const Value& y) { - return ! (x < y); + return !(x < y); } -/** \brief Experimental do not use: Allocator to customize member name and string value memory management done by Value. +/** \brief Experimental do not use: Allocator to customize member name and + * string value memory management done by Value. * - * - makeMemberName() and releaseMemberName() are called to respectively duplicate and - * free an Json::objectValue member name. + * - makeMemberName() and releaseMemberName() are called to respectively + * duplicate and free an Json::objectValue member name. * - duplicateStringValue() and releaseStringValue() are called similarly to * duplicate and free a Json::stringValue value. */ class ValueAllocator { public: - enum { unknown = (unsigned) - 1 }; + enum { unknown = (unsigned)-1 }; - virtual ~ValueAllocator () = default; + virtual ~ValueAllocator() = default; - virtual char* makeMemberName ( const char* memberName ) = 0; - virtual void releaseMemberName ( char* memberName ) = 0; - virtual char* duplicateStringValue ( const char* value, - unsigned int length = unknown ) = 0; - virtual void releaseStringValue ( char* value ) = 0; + virtual char* + makeMemberName(const char* memberName) = 0; + virtual void + releaseMemberName(char* memberName) = 0; + virtual char* + duplicateStringValue(const char* value, unsigned int length = unknown) = 0; + virtual void + releaseStringValue(char* value) = 0; }; /** \brief base class for Value iterators. @@ -429,41 +498,54 @@ public: using difference_type = int; using SelfType = ValueIteratorBase; - ValueIteratorBase (); + ValueIteratorBase(); - explicit ValueIteratorBase ( const Value::ObjectValues::iterator& current ); + explicit ValueIteratorBase(const Value::ObjectValues::iterator& current); - bool operator == ( const SelfType& other ) const + bool + operator==(const SelfType& other) const { - return isEqual ( other ); + return isEqual(other); } - bool operator != ( const SelfType& other ) const + bool + operator!=(const SelfType& other) const { - return !isEqual ( other ); + return !isEqual(other); } - /// Return either the index or the member name of the referenced value as a Value. - Value key () const; + /// Return either the index or the member name of the referenced value as a + /// Value. + Value + key() const; /// Return the index of the referenced Value. -1 if it is not an arrayValue. - UInt index () const; + UInt + index() const; - /// Return the member name of the referenced Value. "" if it is not an objectValue. - const char* memberName () const; + /// Return the member name of the referenced Value. "" if it is not an + /// objectValue. + const char* + memberName() const; protected: - Value& deref () const; + Value& + deref() const; - void increment (); + void + increment(); - void decrement (); + void + decrement(); - difference_type computeDistance ( const SelfType& other ) const; + difference_type + computeDistance(const SelfType& other) const; - bool isEqual ( const SelfType& other ) const; + bool + isEqual(const SelfType& other) const; - void copy ( const SelfType& other ); + void + copy(const SelfType& other); private: Value::ObjectValues::iterator current_; @@ -477,6 +559,7 @@ private: class ValueConstIterator : public ValueIteratorBase { friend class Value; + public: using size_t = unsigned int; using difference_type = int; @@ -484,52 +567,60 @@ public: using pointer = const Value*; using SelfType = ValueConstIterator; - ValueConstIterator () = default; + ValueConstIterator() = default; + private: /*! \internal Use by Value to create an iterator. */ - explicit ValueConstIterator ( const Value::ObjectValues::iterator& current ); -public: - SelfType& operator = ( const ValueIteratorBase& other ); + explicit ValueConstIterator(const Value::ObjectValues::iterator& current); - SelfType operator++ ( int ) +public: + SelfType& + operator=(const ValueIteratorBase& other); + + SelfType + operator++(int) { - SelfType temp ( *this ); + SelfType temp(*this); ++*this; return temp; } - SelfType operator-- ( int ) + SelfType + operator--(int) { - SelfType temp ( *this ); + SelfType temp(*this); --*this; return temp; } - SelfType& operator-- () + SelfType& + operator--() { - decrement (); + decrement(); return *this; } - SelfType& operator++ () + SelfType& + operator++() { - increment (); + increment(); return *this; } - reference operator * () const + reference + operator*() const { - return deref (); + return deref(); } }; - /** \brief Iterator for object and array value. */ class ValueIterator : public ValueIteratorBase { friend class Value; + public: using size_t = unsigned int; using difference_type = int; @@ -537,50 +628,56 @@ public: using pointer = Value*; using SelfType = ValueIterator; - ValueIterator () = default; - ValueIterator ( const ValueConstIterator& other ); - ValueIterator ( const ValueIterator& other ); + ValueIterator() = default; + ValueIterator(const ValueConstIterator& other); + ValueIterator(const ValueIterator& other); + private: /*! \internal Use by Value to create an iterator. */ - explicit ValueIterator ( const Value::ObjectValues::iterator& current ); + explicit ValueIterator(const Value::ObjectValues::iterator& current); + public: + SelfType& + operator=(const SelfType& other); - SelfType& operator = ( const SelfType& other ); - - SelfType operator++ ( int ) + SelfType + operator++(int) { - SelfType temp ( *this ); + SelfType temp(*this); ++*this; return temp; } - SelfType operator-- ( int ) + SelfType + operator--(int) { - SelfType temp ( *this ); + SelfType temp(*this); --*this; return temp; } - SelfType& operator-- () + SelfType& + operator--() { - decrement (); + decrement(); return *this; } - SelfType& operator++ () + SelfType& + operator++() { - increment (); + increment(); return *this; } - reference operator * () const + reference + operator*() const { - return deref (); + return deref(); } }; -} // namespace Json +} // namespace Json - -#endif // CPPTL_JSON_H_INCLUDED +#endif // CPPTL_JSON_H_INCLUDED diff --git a/src/ripple/json/json_writer.h b/src/ripple/json/json_writer.h index c32a0fcb42..d1ddcd4dec 100644 --- a/src/ripple/json/json_writer.h +++ b/src/ripple/json/json_writer.h @@ -25,8 +25,7 @@ #include #include -namespace Json -{ +namespace Json { class Value; @@ -35,70 +34,91 @@ class Value; class WriterBase { public: - virtual ~WriterBase () {} - virtual std::string write ( const Value& root ) = 0; + virtual ~WriterBase() + { + } + virtual std::string + write(const Value& root) = 0; }; -/** \brief Outputs a Value in JSON format without formatting (not human friendly). +/** \brief Outputs a Value in JSON format + * without formatting (not human friendly). * - * The JSON document is written in a single line. It is not intended for 'human' consumption, - * but may be useful to support feature such as RPC where bandwith is limited. - * \sa Reader, Value + * The JSON document is written in a single line. It is not intended for 'human' + * consumption, but may be useful to support feature such as RPC where bandwith + * is limited. \sa Reader, Value */ class FastWriter : public WriterBase { public: - FastWriter () = default; - virtual ~FastWriter () {} + FastWriter() = default; + virtual ~FastWriter() + { + } -public: // overridden from Writer - std::string write ( const Value& root ) override; +public: // overridden from Writer + std::string + write(const Value& root) override; private: - void writeValue ( const Value& value ); + void + writeValue(const Value& value); std::string document_; }; -/** \brief Writes a Value in JSON format in a human friendly way. +/** \brief Writes a Value in JSON format in a + * human friendly way. * * The rules for line break and indent are as follow: * - Object value: * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line - * and then unindent and line break and print '}'. + * - if not empty the print '{', line break & indent, print one value per + * line and then unindent and line break and print '}'. * - Array value: * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. + * - if the array contains no object value, empty array or some other value + * types, and all the values fit on one lines, then print the array on a single + * line. * - otherwise, it the values do not fit on one line, or the array contains * object or non empty array, then print one value per line. * * \sa Reader, Value */ -class StyledWriter: public WriterBase +class StyledWriter : public WriterBase { public: - StyledWriter (); - virtual ~StyledWriter () {} + StyledWriter(); + virtual ~StyledWriter() + { + } -public: // overridden from Writer - /** \brief Serialize a Value in JSON format. - * \param root Value to serialize. - * \return String containing the JSON document that represents the root value. +public: // overridden from Writer + /** \brief Serialize a Value in JSON + * format. \param root Value to serialize. \return String containing the + * JSON document that represents the root value. */ - std::string write ( const Value& root ) override; + std::string + write(const Value& root) override; private: - void writeValue ( const Value& value ); - void writeArrayValue ( const Value& value ); - bool isMultineArray ( const Value& value ); - void pushValue ( std::string const& value ); - void writeIndent (); - void writeWithIndent ( std::string const& value ); - void indent (); - void unindent (); + void + writeValue(const Value& value); + void + writeArrayValue(const Value& value); + bool + isMultineArray(const Value& value); + void + pushValue(std::string const& value); + void + writeIndent(); + void + writeWithIndent(std::string const& value); + void + indent(); + void + unindent(); using ChildValues = std::vector; @@ -110,18 +130,21 @@ private: bool addChildValues_; }; -/** \brief Writes a Value in JSON format in a human friendly way, - to a stream rather than to a string. +/** \brief Writes a Value in JSON format in a + human friendly way, to a stream rather than to a string. * * The rules for line break and indent are as follow: * - Object value: * - if empty then print {} without indent and line break - * - if not empty the print '{', line break & indent, print one value per line + * - if not empty the print '{', line break & indent, print one value per + line * and then unindent and line break and print '}'. * - Array value: * - if empty then print [] without indent and line break - * - if the array contains no object value, empty array or some other value types, - * and all the values fit on one lines, then print the array on a single line. + * - if the array contains no object value, empty array or some other value + types, + * and all the values fit on one lines, then print the array on a single + line. * - otherwise, it the values do not fit on one line, or the array contains * object or non empty array, then print one value per line. * @@ -131,26 +154,38 @@ private: class StyledStreamWriter { public: - StyledStreamWriter ( std::string indentation = "\t" ); - ~StyledStreamWriter () {} + StyledStreamWriter(std::string indentation = "\t"); + ~StyledStreamWriter() + { + } public: - /** \brief Serialize a Value in JSON format. - * \param out Stream to write to. (Can be ostringstream, e.g.) + /** \brief Serialize a Value in JSON + * format. \param out Stream to write to. (Can be ostringstream, e.g.) * \param root Value to serialize. - * \note There is no point in deriving from Writer, since write() should not return a value. + * \note There is no point in deriving from Writer, since write() should not + * return a value. */ - void write ( std::ostream& out, const Value& root ); + void + write(std::ostream& out, const Value& root); private: - void writeValue ( const Value& value ); - void writeArrayValue ( const Value& value ); - bool isMultineArray ( const Value& value ); - void pushValue ( std::string const& value ); - void writeIndent (); - void writeWithIndent ( std::string const& value ); - void indent (); - void unindent (); + void + writeValue(const Value& value); + void + writeArrayValue(const Value& value); + bool + isMultineArray(const Value& value); + void + pushValue(std::string const& value); + void + writeIndent(); + void + writeWithIndent(std::string const& value); + void + indent(); + void + unindent(); using ChildValues = std::vector; @@ -162,15 +197,21 @@ private: bool addChildValues_; }; -std::string valueToString ( Int value ); -std::string valueToString ( UInt value ); -std::string valueToString ( double value ); -std::string valueToString ( bool value ); -std::string valueToQuotedString ( const char* value ); +std::string +valueToString(Int value); +std::string +valueToString(UInt value); +std::string +valueToString(double value); +std::string +valueToString(bool value); +std::string +valueToQuotedString(const char* value); /// \brief Output using the StyledStreamWriter. /// \see Json::operator>>() -std::ostream& operator<< ( std::ostream&, const Value& root ); +std::ostream& +operator<<(std::ostream&, const Value& root); //------------------------------------------------------------------------------ @@ -214,8 +255,7 @@ write_value(Write const& write, Value const& value) write_string(write, valueToString(value.asBool())); break; - case arrayValue: - { + case arrayValue: { write("[", 1); int const size = value.size(); for (int index = 0; index < size; ++index) @@ -228,8 +268,7 @@ write_value(Write const& write, Value const& value) break; } - case objectValue: - { + case objectValue: { Value::Members const members = value.getMemberNames(); write("{", 1); for (auto it = members.begin(); it != members.end(); ++it) @@ -305,4 +344,4 @@ public: } // namespace Json -#endif // JSON_WRITER_H_INCLUDED +#endif // JSON_WRITER_H_INCLUDED diff --git a/src/ripple/json/to_string.h b/src/ripple/json/to_string.h index cf093e2125..5f692a415e 100644 --- a/src/ripple/json/to_string.h +++ b/src/ripple/json/to_string.h @@ -20,22 +20,25 @@ #ifndef RIPPLE_JSON_TO_STRING_H_INCLUDED #define RIPPLE_JSON_TO_STRING_H_INCLUDED -#include #include +#include namespace Json { class Value; /** Writes a Json::Value to an std::string. */ -std::string to_string (Value const&); +std::string +to_string(Value const&); /** Writes a Json::Value to an std::string. */ -std::string pretty (Value const&); +std::string +pretty(Value const&); /** Output using the StyledStreamWriter. @see Json::operator>>(). */ -std::ostream& operator<< (std::ostream&, const Value& root); +std::ostream& +operator<<(std::ostream&, const Value& root); -} // Json +} // namespace Json -#endif // JSON_TO_STRING_H_INCLUDED +#endif // JSON_TO_STRING_H_INCLUDED diff --git a/src/ripple/ledger/ApplyView.h b/src/ripple/ledger/ApplyView.h index bd5be0ac6c..b6020c3227 100644 --- a/src/ripple/ledger/ApplyView.h +++ b/src/ripple/ledger/ApplyView.h @@ -27,82 +27,72 @@ namespace ripple { -enum ApplyFlags - : std::uint32_t -{ - tapNONE = 0x00, +enum ApplyFlags : std::uint32_t { + tapNONE = 0x00, // This is a local transaction with the // fail_hard flag set. - tapFAIL_HARD = 0x10, + tapFAIL_HARD = 0x10, // This is not the transaction's last pass // Transaction can be retried, soft failures allowed - tapRETRY = 0x20, + tapRETRY = 0x20, // Transaction must pay more than both the open ledger // fee and all transactions in the queue to get into the // open ledger - tapPREFER_QUEUE = 0x40, + tapPREFER_QUEUE = 0x40, // Transaction came from a privileged source - tapUNLIMITED = 0x400, + tapUNLIMITED = 0x400, }; -constexpr -ApplyFlags -operator|(ApplyFlags const& lhs, - ApplyFlags const& rhs) +constexpr ApplyFlags +operator|(ApplyFlags const& lhs, ApplyFlags const& rhs) { return safe_cast( safe_cast>(lhs) | - safe_cast>(rhs)); + safe_cast>(rhs)); } -static_assert((tapPREFER_QUEUE | tapRETRY) == safe_cast(0x60u), +static_assert( + (tapPREFER_QUEUE | tapRETRY) == safe_cast(0x60u), "ApplyFlags operator |"); -static_assert((tapRETRY | tapPREFER_QUEUE) == safe_cast(0x60u), +static_assert( + (tapRETRY | tapPREFER_QUEUE) == safe_cast(0x60u), "ApplyFlags operator |"); -constexpr -ApplyFlags -operator&(ApplyFlags const& lhs, - ApplyFlags const& rhs) +constexpr ApplyFlags +operator&(ApplyFlags const& lhs, ApplyFlags const& rhs) { return safe_cast( safe_cast>(lhs) & - safe_cast>(rhs)); + safe_cast>(rhs)); } -static_assert((tapPREFER_QUEUE & tapRETRY) == tapNONE, - "ApplyFlags operator &"); -static_assert((tapRETRY & tapPREFER_QUEUE) == tapNONE, - "ApplyFlags operator &"); +static_assert((tapPREFER_QUEUE & tapRETRY) == tapNONE, "ApplyFlags operator &"); +static_assert((tapRETRY & tapPREFER_QUEUE) == tapNONE, "ApplyFlags operator &"); -constexpr -ApplyFlags +constexpr ApplyFlags operator~(ApplyFlags const& flags) { return safe_cast( ~safe_cast>(flags)); } -static_assert(~tapRETRY == safe_cast(0xFFFFFFDFu), +static_assert( + ~tapRETRY == safe_cast(0xFFFFFFDFu), "ApplyFlags operator ~"); -inline -ApplyFlags -operator|=(ApplyFlags & lhs, - ApplyFlags const& rhs) +inline ApplyFlags +operator|=(ApplyFlags& lhs, ApplyFlags const& rhs) { lhs = lhs | rhs; return lhs; } -inline -ApplyFlags -operator&=(ApplyFlags& lhs, - ApplyFlags const& rhs) +inline ApplyFlags +operator&=(ApplyFlags& lhs, ApplyFlags const& rhs) { lhs = lhs & rhs; return lhs; @@ -147,20 +137,19 @@ operator&=(ApplyFlags& lhs, The invariant is that insert, update, and erase may not be called with any SLE which belongs to different view. */ -class ApplyView - : public ReadView +class ApplyView : public ReadView { private: /** Add an entry to a directory using the specified insert strategy */ boost::optional - dirAdd ( + dirAdd( bool preserveOrder, Keylet const& directory, uint256 const& key, std::function const&)> const& describe); public: - ApplyView () = default; + ApplyView() = default; /** Returns the tx apply flags. @@ -170,8 +159,7 @@ public: while transactions applied to the consensus ledger produce hard failures (and claim a fee). */ - virtual - ApplyFlags + virtual ApplyFlags flags() const = 0; /** Prepare to modify the SLE associated with key. @@ -188,9 +176,8 @@ public: @return `nullptr` if the key is not present */ - virtual - std::shared_ptr - peek (Keylet const& k) = 0; + virtual std::shared_ptr + peek(Keylet const& k) = 0; /** Remove a peeked SLE. @@ -203,9 +190,8 @@ public: The key is no longer associated with the SLE. */ - virtual - void - erase (std::shared_ptr const& sle) = 0; + virtual void + erase(std::shared_ptr const& sle) = 0; /** Insert a new state SLE @@ -225,9 +211,8 @@ public: @note The key is taken from the SLE */ - virtual - void - insert (std::shared_ptr const& sle) = 0; + virtual void + insert(std::shared_ptr const& sle) = 0; /** Indicate changes to a peeked SLE @@ -245,16 +230,16 @@ public: @note The key is taken from the SLE */ /** @{ */ - virtual - void - update (std::shared_ptr const& sle) = 0; + virtual void + update(std::shared_ptr const& sle) = 0; //-------------------------------------------------------------------------- // Called when a credit is made to an account // This is required to support PaymentSandbox virtual void - creditHook (AccountID const& from, + creditHook( + AccountID const& from, AccountID const& to, STAmount const& amount, STAmount const& preCreditBalance) @@ -263,10 +248,13 @@ public: // Called when the owner count changes // This is required to support PaymentSandbox - virtual - void adjustOwnerCountHook (AccountID const& account, - std::uint32_t cur, std::uint32_t next) - {} + virtual void + adjustOwnerCountHook( + AccountID const& account, + std::uint32_t cur, + std::uint32_t next) + { + } /** Append an entry to a directory @@ -287,21 +275,21 @@ public: */ /** @{ */ boost::optional - dirAppend ( + dirAppend( Keylet const& directory, uint256 const& key, std::function const&)> const& describe) { - return dirAdd (true, directory, key, describe); + return dirAdd(true, directory, key, describe); } boost::optional - dirAppend ( + dirAppend( Keylet const& directory, Keylet const& key, std::function const&)> const& describe) { - return dirAppend (directory, key.key, describe); + return dirAppend(directory, key.key, describe); } /** @} */ @@ -324,21 +312,21 @@ public: */ /** @{ */ boost::optional - dirInsert ( + dirInsert( Keylet const& directory, uint256 const& key, std::function const&)> const& describe) { - return dirAdd (false, directory, key, describe); + return dirAdd(false, directory, key, describe); } boost::optional - dirInsert ( + dirInsert( Keylet const& directory, Keylet const& key, std::function const&)> const& describe) { - return dirInsert (directory, key.key, describe); + return dirInsert(directory, key.key, describe); } /** @} */ @@ -359,20 +347,20 @@ public: */ /** @{ */ bool - dirRemove ( + dirRemove( Keylet const& directory, std::uint64_t page, uint256 const& key, bool keepRoot); bool - dirRemove ( + dirRemove( Keylet const& directory, std::uint64_t page, Keylet const& key, bool keepRoot) { - return dirRemove (directory, page, key.key, keepRoot); + return dirRemove(directory, page, key.key, keepRoot); } /** @} */ @@ -389,6 +377,6 @@ public: emptyDirDelete(Keylet const& directory); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/ApplyViewImpl.h b/src/ripple/ledger/ApplyViewImpl.h index 3c5531c6ff..fe26ca25fd 100644 --- a/src/ripple/ledger/ApplyViewImpl.h +++ b/src/ripple/ledger/ApplyViewImpl.h @@ -34,18 +34,18 @@ namespace ripple { @note Presented as ApplyView to clients. */ -class ApplyViewImpl final - : public detail::ApplyViewBase +class ApplyViewImpl final : public detail::ApplyViewBase { public: ApplyViewImpl() = delete; - ApplyViewImpl (ApplyViewImpl const&) = delete; - ApplyViewImpl& operator= (ApplyViewImpl&&) = delete; - ApplyViewImpl& operator= (ApplyViewImpl const&) = delete; + ApplyViewImpl(ApplyViewImpl const&) = delete; + ApplyViewImpl& + operator=(ApplyViewImpl&&) = delete; + ApplyViewImpl& + operator=(ApplyViewImpl const&) = delete; - ApplyViewImpl (ApplyViewImpl&&) = default; - ApplyViewImpl( - ReadView const* base, ApplyFlags flags); + ApplyViewImpl(ApplyViewImpl&&) = default; + ApplyViewImpl(ReadView const* base, ApplyFlags flags); /** Apply the transaction. @@ -54,9 +54,7 @@ public: destructor. */ void - apply (OpenView& to, - STTx const& tx, TER ter, - beast::Journal j); + apply(OpenView& to, STTx const& tx, TER ter, beast::Journal j); /** Set the amount of currency delivered. @@ -66,30 +64,31 @@ public: excluded from the resulting metadata. */ void - deliver (STAmount const& amount) + deliver(STAmount const& amount) { deliver_ = amount; } /** Get the number of modified entries - */ + */ std::size_t - size (); + size(); /** Visit modified entries - */ + */ void - visit ( + visit( OpenView& target, - std::function const& before, - std::shared_ptr const& after)> const& func); + std::shared_ptr const& before, + std::shared_ptr const& after)> const& func); + private: boost::optional deliver_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/BookDirs.h b/src/ripple/ledger/BookDirs.h index ddf3eebdba..7b384dc0cf 100644 --- a/src/ripple/ledger/BookDirs.h +++ b/src/ripple/ledger/BookDirs.h @@ -51,16 +51,11 @@ public: class BookDirs::const_iterator { public: - using value_type = - BookDirs::value_type; - using pointer = - value_type const*; - using reference = - value_type const&; - using difference_type = - std::ptrdiff_t; - using iterator_category = - std::forward_iterator_tag; + using value_type = BookDirs::value_type; + using pointer = value_type const*; + using reference = value_type const&; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; const_iterator() = default; @@ -70,7 +65,7 @@ public: bool operator!=(const_iterator const& other) const { - return ! (*this == other); + return !(*this == other); } reference @@ -91,12 +86,11 @@ public: private: friend class BookDirs; - const_iterator(ReadView const& view, - uint256 const& root, uint256 const& dir_key) - : view_(&view) - , root_(root) - , key_(dir_key) - , cur_key_(dir_key) + const_iterator( + ReadView const& view, + uint256 const& root, + uint256 const& dir_key) + : view_(&view), root_(root), key_(dir_key), cur_key_(dir_key) { } @@ -113,6 +107,6 @@ private: static beast::Journal j_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/CachedSLEs.h b/src/ripple/ledger/CachedSLEs.h index 2c8261b94f..190bc37098 100644 --- a/src/ripple/ledger/CachedSLEs.h +++ b/src/ripple/ledger/CachedSLEs.h @@ -21,8 +21,8 @@ #define RIPPLE_LEDGER_CACHEDSLES_H_INCLUDED #include -#include #include +#include #include #include @@ -34,18 +34,17 @@ class CachedSLEs public: using digest_type = uint256; - using value_type = - std::shared_ptr; + using value_type = std::shared_ptr; - CachedSLEs (CachedSLEs const&) = delete; - CachedSLEs& operator= (CachedSLEs const&) = delete; + CachedSLEs(CachedSLEs const&) = delete; + CachedSLEs& + operator=(CachedSLEs const&) = delete; template - CachedSLEs (std::chrono::duration< - Rep, Period> const& timeToLive, - Stopwatch& clock) - : timeToLive_ (timeToLive) - , map_ (clock) + CachedSLEs( + std::chrono::duration const& timeToLive, + Stopwatch& clock) + : timeToLive_(timeToLive), map_(clock) { } @@ -65,13 +64,11 @@ public: */ template value_type - fetch (digest_type const& digest, - Handler const& h) + fetch(digest_type const& digest, Handler const& h) { { std::lock_guard lock(mutex_); - auto iter = - map_.find(digest); + auto iter = map_.find(digest); if (iter != map_.end()) { ++hit_; @@ -80,7 +77,7 @@ public: } } auto sle = h(); - if (! sle) + if (!sle) return nullptr; std::lock_guard lock(mutex_); ++miss_; @@ -99,11 +96,14 @@ private: std::size_t miss_ = 0; std::mutex mutable mutex_; Stopwatch::duration timeToLive_; - beast::aged_unordered_map > map_; + beast::aged_unordered_map< + digest_type, + value_type, + Stopwatch::clock_type, + hardened_hash> + map_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/CachedView.h b/src/ripple/ledger/CachedView.h index 4ac82eab4b..57221c02ff 100644 --- a/src/ripple/ledger/CachedView.h +++ b/src/ripple/ledger/CachedView.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_LEDGER_CACHEDVIEW_H_INCLUDED #define RIPPLE_LEDGER_CACHEDVIEW_H_INCLUDED +#include #include #include -#include #include #include #include @@ -32,26 +32,25 @@ namespace ripple { namespace detail { -class CachedViewImpl - : public DigestAwareReadView +class CachedViewImpl : public DigestAwareReadView { private: DigestAwareReadView const& base_; CachedSLEs& cache_; std::mutex mutable mutex_; - std::unordered_map, - hardened_hash<>> mutable map_; + hardened_hash<>> mutable map_; public: CachedViewImpl() = delete; - CachedViewImpl (CachedViewImpl const&) = delete; - CachedViewImpl& operator= (CachedViewImpl const&) = delete; + CachedViewImpl(CachedViewImpl const&) = delete; + CachedViewImpl& + operator=(CachedViewImpl const&) = delete; - CachedViewImpl (DigestAwareReadView const* base, - CachedSLEs& cache) - : base_ (*base) - , cache_ (cache) + CachedViewImpl(DigestAwareReadView const* base, CachedSLEs& cache) + : base_(*base), cache_(cache) { } @@ -60,10 +59,10 @@ public: // bool - exists (Keylet const& k) const override; + exists(Keylet const& k) const override; std::shared_ptr - read (Keylet const& k) const override; + read(Keylet const& k) const override; bool open() const override @@ -90,8 +89,9 @@ public: } boost::optional - succ (key_type const& key, boost::optional< - key_type> const& last = boost::none) const override + succ( + key_type const& key, + boost::optional const& last = boost::none) const override { return base_.succ(key, last); } @@ -133,7 +133,7 @@ public: } tx_type - txRead (key_type const& key) const override + txRead(key_type const& key) const override { return base_.txRead(key); } @@ -143,26 +143,23 @@ public: // boost::optional - digest (key_type const& key) const override + digest(key_type const& key) const override { return base_.digest(key); } - }; -} // detail +} // namespace detail /** Wraps a DigestAwareReadView to provide caching. @tparam Base A subclass of DigestAwareReadView */ template -class CachedView - : public detail::CachedViewImpl +class CachedView : public detail::CachedViewImpl { private: - static_assert(std::is_base_of< - DigestAwareReadView, Base>::value, ""); + static_assert(std::is_base_of::value, ""); std::shared_ptr sp_; @@ -170,13 +167,12 @@ public: using base_type = Base; CachedView() = delete; - CachedView (CachedView const&) = delete; - CachedView& operator= (CachedView const&) = delete; + CachedView(CachedView const&) = delete; + CachedView& + operator=(CachedView const&) = delete; - CachedView (std::shared_ptr< - Base const> const& base, CachedSLEs& cache) - : CachedViewImpl (base.get(), cache) - , sp_ (base) + CachedView(std::shared_ptr const& base, CachedSLEs& cache) + : CachedViewImpl(base.get(), cache), sp_(base) { } @@ -191,6 +187,6 @@ public: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/CashDiff.h b/src/ripple/ledger/CashDiff.h index 9ed5521395..3b1dea364e 100644 --- a/src/ripple/ledger/CashDiff.h +++ b/src/ripple/ledger/CashDiff.h @@ -22,7 +22,7 @@ #include #include -#include // std::unique_ptr +#include // std::unique_ptr namespace ripple { @@ -36,22 +36,21 @@ class ApplyStateTable; // Used by CashDiff to specify filters applied while processing differences. // Entries are bit flags that can be ANDed and ORed. -enum class CashFilter : std::uint8_t -{ +enum class CashFilter : std::uint8_t { none = 0x0, treatZeroOfferAsDeletion = 0x1 }; -inline CashFilter operator| (CashFilter lhs, CashFilter rhs) +inline CashFilter +operator|(CashFilter lhs, CashFilter rhs) { using ul_t = std::underlying_type::type; - return static_cast( - safe_cast(lhs) | safe_cast(rhs)); + return static_cast(safe_cast(lhs) | safe_cast(rhs)); } -inline CashFilter operator& (CashFilter lhs, CashFilter rhs) +inline CashFilter +operator&(CashFilter lhs, CashFilter rhs) { using ul_t = std::underlying_type::type; - return static_cast( - safe_cast(lhs) & safe_cast(rhs)); + return static_cast(safe_cast(lhs) & safe_cast(rhs)); } //------------------------------------------------------------------------------ @@ -62,27 +61,35 @@ class CashDiff { public: CashDiff() = delete; - CashDiff (CashDiff const&) = delete; - CashDiff (CashDiff&& other) noexcept; - CashDiff& operator= (CashDiff const&) = delete; + CashDiff(CashDiff const&) = delete; + CashDiff(CashDiff&& other) noexcept; + CashDiff& + operator=(CashDiff const&) = delete; ~CashDiff(); - CashDiff (ReadView const& view, - CashFilter lhsFilter, detail::ApplyStateTable const& lhs, - CashFilter rhsFilter, detail::ApplyStateTable const& rhs); + CashDiff( + ReadView const& view, + CashFilter lhsFilter, + detail::ApplyStateTable const& lhs, + CashFilter rhsFilter, + detail::ApplyStateTable const& rhs); // Returns the number of cases where lhs and rhs had the same entries // (but not necessarily the same amounts) - std::size_t commonCount () const; + std::size_t + commonCount() const; // Returns the number of entries that were present in rhs but not in lhs. - std::size_t rhsOnlyCount () const; + std::size_t + rhsOnlyCount() const; // Returns the number of entries that were present in lhs but not in rhs. - std::size_t lhsOnlyCount () const; + std::size_t + lhsOnlyCount() const; // Returns true is there are any differences to report. - bool hasDiff() const; + bool + hasDiff() const; // Checks for the XRP round-to-zero case. Returns zero if not detected. // Otherwise returns -1 if seen on lhs, +1 if seen on rhs. @@ -97,29 +104,46 @@ public: // value of the received IOU. // // This check should be made before calling rmDust(). - int xrpRoundToZero() const; + int + xrpRoundToZero() const; // Remove dust-sized differences. Returns true is dust was removed. - bool rmDust(); + bool + rmDust(); // Remove offer deletion differences from a given side. Returns true // if any deleted offers were removed from the differences. - bool rmLhsDeletedOffers(); - bool rmRhsDeletedOffers(); + bool + rmLhsDeletedOffers(); + bool + rmRhsDeletedOffers(); struct OfferAmounts { static std::size_t constexpr count_ = 2; - static std::size_t constexpr count() { return count_; } - STAmount amounts[count_]; - STAmount const& takerPays() const { return amounts[0]; } - STAmount const& takerGets() const { return amounts[1]; } - STAmount const& operator[] (std::size_t i) const + static std::size_t constexpr count() { - assert (i < count()); + return count_; + } + STAmount amounts[count_]; + STAmount const& + takerPays() const + { + return amounts[0]; + } + STAmount const& + takerGets() const + { + return amounts[1]; + } + STAmount const& + operator[](std::size_t i) const + { + assert(i < count()); return amounts[i]; } - friend bool operator< (OfferAmounts const& lhs, OfferAmounts const& rhs) + friend bool + operator<(OfferAmounts const& lhs, OfferAmounts const& rhs) { if (lhs[0] < rhs[0]) return true; @@ -140,8 +164,9 @@ private: // If v1 < v2, smallness is computed as v1 / (v2 - v1). // The e10 argument says at least how big that ratio must be. Default is 10^6. // If both v1 and v2 are XRP, consider any diff of 2 drops or less to be dust. -bool diffIsDust (STAmount const& v1, STAmount const& v2, std::uint8_t e10 = 6); +bool +diffIsDust(STAmount const& v1, STAmount const& v2, std::uint8_t e10 = 6); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/Directory.h b/src/ripple/ledger/Directory.h index a0bb0135f9..5946931fc0 100644 --- a/src/ripple/ledger/Directory.h +++ b/src/ripple/ledger/Directory.h @@ -49,16 +49,11 @@ public: class Dir::const_iterator { public: - using value_type = - Dir::value_type; - using pointer = - value_type const*; - using reference = - value_type const&; - using difference_type = - std::ptrdiff_t; - using iterator_category = - std::forward_iterator_tag; + using value_type = Dir::value_type; + using pointer = value_type const*; + using reference = value_type const&; + using difference_type = std::ptrdiff_t; + using iterator_category = std::forward_iterator_tag; bool operator==(const_iterator const& other) const; @@ -66,7 +61,7 @@ public: bool operator!=(const_iterator const& other) const { - return ! (*this == other); + return !(*this == other); } reference @@ -99,11 +94,8 @@ public: private: friend class Dir; - const_iterator(ReadView const& view, - Keylet const& root, Keylet const& page) - : view_(&view) - , root_(root) - , page_(page) + const_iterator(ReadView const& view, Keylet const& root, Keylet const& page) + : view_(&view), root_(root), page_(page) { } @@ -117,6 +109,6 @@ private: std::vector::const_iterator it_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/OpenView.h b/src/ripple/ledger/OpenView.h index d25bf77060..fe43aeb57e 100644 --- a/src/ripple/ledger/OpenView.h +++ b/src/ripple/ledger/OpenView.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_LEDGER_OPENVIEW_H_INCLUDED #define RIPPLE_LEDGER_OPENVIEW_H_INCLUDED +#include +#include #include #include #include -#include -#include #include #include @@ -49,20 +49,25 @@ extern open_ledger_t const open_ledger; @note Presented as ReadView to clients. */ -class OpenView final - : public ReadView - , public TxsRawView +class OpenView final : public ReadView, public TxsRawView { private: class txs_iter_impl; // List of tx, key order - using txs_map = std::map, - std::shared_ptr>, - std::less, qalloc_type, - std::shared_ptr>>, false>>; + using txs_map = std::map< + key_type, + std::pair< + std::shared_ptr, + std::shared_ptr>, + std::less, + qalloc_type< + std::pair< + key_type const, + std::pair< + std::shared_ptr, + std::shared_ptr>>, + false>>; Rules rules_; txs_map txs_; @@ -74,10 +79,12 @@ private: public: OpenView() = delete; - OpenView& operator= (OpenView&&) = delete; - OpenView& operator= (OpenView const&) = delete; + OpenView& + operator=(OpenView&&) = delete; + OpenView& + operator=(OpenView const&) = delete; - OpenView (OpenView&&) = default; + OpenView(OpenView&&) = default; /** Construct a shallow copy. @@ -91,7 +98,7 @@ public: Since the SLEs are immutable, calls on the RawView interface cannot break invariants. */ - OpenView (OpenView const&) = default; + OpenView(OpenView const&) = default; /** Construct an open ledger view. @@ -114,13 +121,17 @@ public: all newly inserted tx. */ /** @{ */ - OpenView (open_ledger_t, - ReadView const* base, Rules const& rules, - std::shared_ptr hold = nullptr); + OpenView( + open_ledger_t, + ReadView const* base, + Rules const& rules, + std::shared_ptr hold = nullptr); - OpenView (open_ledger_t, Rules const& rules, - std::shared_ptr const& base) - : OpenView (open_ledger, &*base, rules, base) + OpenView( + open_ledger_t, + Rules const& rules, + std::shared_ptr const& base) + : OpenView(open_ledger, &*base, rules, base) { } /** @} */ @@ -136,8 +147,7 @@ public: The tx list starts empty and will contain all newly inserted tx. */ - OpenView (ReadView const* base, - std::shared_ptr hold = nullptr); + OpenView(ReadView const* base, std::shared_ptr hold = nullptr); /** Returns true if this reflects an open ledger. */ bool @@ -156,7 +166,7 @@ public: /** Apply changes. */ void - apply (TxsRawView& to) const; + apply(TxsRawView& to) const; // ReadView @@ -170,14 +180,15 @@ public: rules() const override; bool - exists (Keylet const& k) const override; + exists(Keylet const& k) const override; boost::optional - succ (key_type const& key, boost::optional< - key_type> const& last = boost::none) const override; + succ( + key_type const& key, + boost::optional const& last = boost::none) const override; std::shared_ptr - read (Keylet const& k) const override; + read(Keylet const& k) const override; std::unique_ptr slesBegin() const override; @@ -195,39 +206,34 @@ public: txsEnd() const override; bool - txExists (key_type const& key) const override; + txExists(key_type const& key) const override; tx_type - txRead (key_type const& key) const override; + txRead(key_type const& key) const override; // RawView void - rawErase (std::shared_ptr< - SLE> const& sle) override; + rawErase(std::shared_ptr const& sle) override; void - rawInsert (std::shared_ptr< - SLE> const& sle) override; + rawInsert(std::shared_ptr const& sle) override; void - rawReplace (std::shared_ptr< - SLE> const& sle) override; + rawReplace(std::shared_ptr const& sle) override; void - rawDestroyXRP( - XRPAmount const& fee) override; + rawDestroyXRP(XRPAmount const& fee) override; // TxsRawView void - rawTxInsert (key_type const& key, - std::shared_ptr - const& txn, std::shared_ptr< - Serializer const> - const& metaData) override; + rawTxInsert( + key_type const& key, + std::shared_ptr const& txn, + std::shared_ptr const& metaData) override; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/PaymentSandbox.h b/src/ripple/ledger/PaymentSandbox.h index 0c0bbb0e37..30f1f5f617 100644 --- a/src/ripple/ledger/PaymentSandbox.h +++ b/src/ripple/ledger/PaymentSandbox.h @@ -38,8 +38,10 @@ class DeferredCredits public: struct Adjustment { - Adjustment (STAmount const& d, STAmount const& c, STAmount const& b) - : debits (d), credits (c), origBalance (b) {} + Adjustment(STAmount const& d, STAmount const& c, STAmount const& b) + : debits(d), credits(c), origBalance(b) + { + } STAmount debits; STAmount credits; STAmount origBalance; @@ -47,31 +49,34 @@ public: // Get the adjustments for the balance between main and other. // Returns the debits, credits and the original balance - boost::optional adjustments ( + boost::optional + adjustments( AccountID const& main, AccountID const& other, Currency const& currency) const; - void credit (AccountID const& sender, + void + credit( + AccountID const& sender, AccountID const& receiver, STAmount const& amount, STAmount const& preCreditSenderBalance); - void ownerCount (AccountID const& id, - std::uint32_t cur, - std::uint32_t next); + void + ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next); // Get the adjusted owner count. Since DeferredCredits is meant to be used // in payments, and payments only decrease owner counts, return the max // remembered owner count. boost::optional - ownerCount (AccountID const& id) const; + ownerCount(AccountID const& id) const; + + void + apply(DeferredCredits& to); - void apply (DeferredCredits& to); private: // lowAccount, highAccount - using Key = std::tuple< - AccountID, AccountID, Currency>; + using Key = std::tuple; struct Value { explicit Value() = default; @@ -81,17 +86,14 @@ private: STAmount lowAcctOrigBalance; }; - static - Key - makeKey (AccountID const& a1, - AccountID const& a2, - Currency const& c); + static Key + makeKey(AccountID const& a1, AccountID const& a2, Currency const& c); std::map credits_; std::map ownerCounts_; }; -} // detail +} // namespace detail //------------------------------------------------------------------------------ @@ -107,24 +109,24 @@ private: @note Presented as ApplyView to clients */ -class PaymentSandbox final - : public detail::ApplyViewBase +class PaymentSandbox final : public detail::ApplyViewBase { public: PaymentSandbox() = delete; - PaymentSandbox (PaymentSandbox const&) = delete; - PaymentSandbox& operator= (PaymentSandbox&&) = delete; - PaymentSandbox& operator= (PaymentSandbox const&) = delete; + PaymentSandbox(PaymentSandbox const&) = delete; + PaymentSandbox& + operator=(PaymentSandbox&&) = delete; + PaymentSandbox& + operator=(PaymentSandbox const&) = delete; - PaymentSandbox (PaymentSandbox&&) = default; + PaymentSandbox(PaymentSandbox&&) = default; - PaymentSandbox (ReadView const* base, ApplyFlags flags) - : ApplyViewBase (base, flags) + PaymentSandbox(ReadView const* base, ApplyFlags flags) + : ApplyViewBase(base, flags) { } - PaymentSandbox (ApplyView const* base) - : ApplyViewBase (base, base->flags()) + PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags()) { } @@ -142,40 +144,39 @@ public: // or a PaymentSandbox-derived class, we MUST go through // one of these constructors or invariants will be broken. /** @{ */ - explicit - PaymentSandbox (PaymentSandbox const* base) - : ApplyViewBase(base, base->flags()) - , ps_ (base) + explicit PaymentSandbox(PaymentSandbox const* base) + : ApplyViewBase(base, base->flags()), ps_(base) { } - explicit - PaymentSandbox (PaymentSandbox* base) - : ApplyViewBase(base, base->flags()) - , ps_ (base) + explicit PaymentSandbox(PaymentSandbox* base) + : ApplyViewBase(base, base->flags()), ps_(base) { } /** @} */ STAmount - balanceHook (AccountID const& account, + balanceHook( + AccountID const& account, AccountID const& issuer, - STAmount const& amount) const override; + STAmount const& amount) const override; void - creditHook (AccountID const& from, + creditHook( + AccountID const& from, AccountID const& to, - STAmount const& amount, - STAmount const& preCreditBalance) override; + STAmount const& amount, + STAmount const& preCreditBalance) override; void - adjustOwnerCountHook (AccountID const& account, + adjustOwnerCountHook( + AccountID const& account, std::uint32_t cur, - std::uint32_t next) override; + std::uint32_t next) override; std::uint32_t - ownerCountHook (AccountID const& account, - std::uint32_t count) const override; + ownerCountHook(AccountID const& account, std::uint32_t count) + const override; /** Apply changes to base view. @@ -184,10 +185,11 @@ public: behavior will result. */ /** @{ */ - void apply (RawView& to); + void + apply(RawView& to); void - apply (PaymentSandbox& to); + apply(PaymentSandbox& to); /** @} */ // Return a map of balance changes on trust lines. The low account is the @@ -195,15 +197,16 @@ public: // the total changes in currency regardless of issuer. This is useful to get // the total change in XRP balances. std::map, STAmount> - balanceChanges (ReadView const& view) const; + balanceChanges(ReadView const& view) const; - XRPAmount xrpDestroyed () const; + XRPAmount + xrpDestroyed() const; private: detail::DeferredCredits tab_; PaymentSandbox const* ps_ = nullptr; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/RawView.h b/src/ripple/ledger/RawView.h index e85ba7b121..1d71772aad 100644 --- a/src/ripple/ledger/RawView.h +++ b/src/ripple/ledger/RawView.h @@ -21,8 +21,8 @@ #define RIPPLE_LEDGER_RAWVIEW_H_INCLUDED #include -#include #include +#include #include #include #include @@ -40,16 +40,16 @@ public: virtual ~RawView() = default; RawView() = default; RawView(RawView const&) = default; - RawView& operator=(RawView const&) = delete; + RawView& + operator=(RawView const&) = delete; /** Delete an existing state item. The SLE is provided so the implementation can calculate metadata. */ - virtual - void - rawErase (std::shared_ptr const& sle) = 0; + virtual void + rawErase(std::shared_ptr const& sle) = 0; /** Unconditionally insert a state item. @@ -62,9 +62,8 @@ public: @note The key is taken from the SLE */ - virtual - void - rawInsert (std::shared_ptr const& sle) = 0; + virtual void + rawInsert(std::shared_ptr const& sle) = 0; /** Unconditionally replace a state item. @@ -78,17 +77,15 @@ public: @note The key is taken from the SLE */ - virtual - void - rawReplace (std::shared_ptr const& sle) = 0; + virtual void + rawReplace(std::shared_ptr const& sle) = 0; /** Destroy XRP. This is used to pay for transaction fees. */ - virtual - void - rawDestroyXRP (XRPAmount const& fee) = 0; + virtual void + rawDestroyXRP(XRPAmount const& fee) = 0; }; //------------------------------------------------------------------------------ @@ -106,14 +103,13 @@ public: Closed ledgers must have metadata, while open ledgers omit metadata. */ - virtual - void - rawTxInsert (ReadView::key_type const& key, - std::shared_ptr - const& txn, std::shared_ptr< - Serializer const> const& metaData) = 0; + virtual void + rawTxInsert( + ReadView::key_type const& key, + std::shared_ptr const& txn, + std::shared_ptr const& metaData) = 0; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/ReadView.h b/src/ripple/ledger/ReadView.h index 15c5c79e01..1e8052cc3f 100644 --- a/src/ripple/ledger/ReadView.h +++ b/src/ripple/ledger/ReadView.h @@ -20,17 +20,17 @@ #ifndef RIPPLE_LEDGER_READVIEW_H_INCLUDED #define RIPPLE_LEDGER_READVIEW_H_INCLUDED -#include -#include #include #include #include +#include +#include +#include +#include #include #include #include #include -#include -#include #include #include #include @@ -46,14 +46,15 @@ namespace ripple { */ struct Fees { - XRPAmount base{ 0 }; // Reference tx cost (drops) - FeeUnit32 units{ 0 }; // Reference fee units - XRPAmount reserve{ 0 }; // Reserve base (drops) - XRPAmount increment{ 0 }; // Reserve increment (drops) + XRPAmount base{0}; // Reference tx cost (drops) + FeeUnit32 units{0}; // Reference fee units + XRPAmount reserve{0}; // Reserve base (drops) + XRPAmount increment{0}; // Reserve increment (drops) explicit Fees() = default; - Fees (Fees const&) = default; - Fees& operator= (Fees const&) = default; + Fees(Fees const&) = default; + Fees& + operator=(Fees const&) = default; /** Returns the account reserve given the owner count, in drops. @@ -61,7 +62,7 @@ struct Fees the reserve increment times the number of increments. */ XRPAmount - accountReserve (std::size_t ownerCount) const + accountReserve(std::size_t ownerCount) const { return reserve + ownerCount * increment; } @@ -131,8 +132,9 @@ private: std::shared_ptr impl_; public: - Rules (Rules const&) = default; - Rules& operator= (Rules const&) = default; + Rules(Rules const&) = default; + Rules& + operator=(Rules const&) = default; Rules() = delete; @@ -150,15 +152,15 @@ public: */ explicit Rules( DigestAwareReadView const& ledger, - std::unordered_set> const& presets); + std::unordered_set> const& presets); /** Returns `true` if a feature is enabled. */ bool - enabled (uint256 const& id) const; + enabled(uint256 const& id) const; /** Returns `true` if these rules don't match the ledger. */ bool - changed (DigestAwareReadView const& ledger) const; + changed(DigestAwareReadView const& ledger) const; /** Returns `true` if two rule sets are identical. @@ -166,12 +168,12 @@ public: rules should be constructed, call changed() first instead. */ bool - operator== (Rules const&) const; + operator==(Rules const&) const; bool - operator!= (Rules const& other) const + operator!=(Rules const& other) const { - return ! (*this == other); + return !(*this == other); } }; @@ -187,63 +189,59 @@ class ReadView { public: using tx_type = - std::pair, - std::shared_ptr>; + std::pair, std::shared_ptr>; using key_type = uint256; - using mapped_type = - std::shared_ptr; + using mapped_type = std::shared_ptr; - struct sles_type : detail::ReadViewFwdRange< - std::shared_ptr> + struct sles_type : detail::ReadViewFwdRange> { - explicit sles_type (ReadView const& view); - iterator begin() const; - iterator const& end() const; - iterator upper_bound(key_type const& key) const; + explicit sles_type(ReadView const& view); + iterator + begin() const; + iterator const& + end() const; + iterator + upper_bound(key_type const& key) const; }; - struct txs_type - : detail::ReadViewFwdRange + struct txs_type : detail::ReadViewFwdRange { - explicit txs_type (ReadView const& view); - bool empty() const; - iterator begin() const; - iterator const& end() const; + explicit txs_type(ReadView const& view); + bool + empty() const; + iterator + begin() const; + iterator const& + end() const; }; virtual ~ReadView() = default; - ReadView& operator= (ReadView&& other) = delete; - ReadView& operator= (ReadView const& other) = delete; + ReadView& + operator=(ReadView&& other) = delete; + ReadView& + operator=(ReadView const& other) = delete; - ReadView () - : sles(*this) - , txs(*this) + ReadView() : sles(*this), txs(*this) { } - ReadView (ReadView const& other) - : sles(*this) - , txs(*this) + ReadView(ReadView const& other) : sles(*this), txs(*this) { } - ReadView (ReadView&& other) - : sles(*this) - , txs(*this) + ReadView(ReadView&& other) : sles(*this), txs(*this) { } /** Returns information about the ledger. */ - virtual - LedgerInfo const& + virtual LedgerInfo const& info() const = 0; /** Returns true if this reflects an open ledger. */ - virtual - bool + virtual bool open() const = 0; /** Returns the close time of the previous ledger. */ @@ -261,13 +259,11 @@ public: } /** Returns the fees for the base ledger. */ - virtual - Fees const& + virtual Fees const& fees() const = 0; /** Returns the tx processing rules. */ - virtual - Rules const& + virtual Rules const& rules() const = 0; /** Determine if a state item exists. @@ -277,9 +273,8 @@ public: @return `true` if a SLE is associated with the specified key. */ - virtual - bool - exists (Keylet const& k) const = 0; + virtual bool + exists(Keylet const& k) const = 0; /** Return the key of the next state item. @@ -291,10 +286,10 @@ public: the key returned would be outside the open interval (key, last). */ - virtual - boost::optional - succ (key_type const& key, boost::optional< - key_type> const& last = boost::none) const = 0; + virtual boost::optional + succ( + key_type const& key, + boost::optional const& last = boost::none) const = 0; /** Return the state item associated with a key. @@ -309,20 +304,19 @@ public: @return `nullptr` if the key is not present or if the type does not match. */ - virtual - std::shared_ptr - read (Keylet const& k) const = 0; + virtual std::shared_ptr + read(Keylet const& k) const = 0; // Accounts in a payment are not allowed to use assets acquired during that // payment. The PaymentSandbox tracks the debits, credits, and owner count - // changes that accounts make during a payment. `balanceHook` adjusts balances - // so newly acquired assets are not counted toward the balance. + // changes that accounts make during a payment. `balanceHook` adjusts + // balances so newly acquired assets are not counted toward the balance. // This is required to support PaymentSandbox. - virtual - STAmount - balanceHook (AccountID const& account, + virtual STAmount + balanceHook( + AccountID const& account, AccountID const& issuer, - STAmount const& amount) const + STAmount const& amount) const { return amount; } @@ -332,37 +326,30 @@ public: // changes that accounts make during a payment. `ownerCountHook` adjusts the // ownerCount so it returns the max value of the ownerCount so far. // This is required to support PaymentSandbox. - virtual - std::uint32_t - ownerCountHook (AccountID const& account, - std::uint32_t count) const + virtual std::uint32_t + ownerCountHook(AccountID const& account, std::uint32_t count) const { return count; } // used by the implementation - virtual - std::unique_ptr + virtual std::unique_ptr slesBegin() const = 0; // used by the implementation - virtual - std::unique_ptr + virtual std::unique_ptr slesEnd() const = 0; // used by the implementation - virtual - std::unique_ptr + virtual std::unique_ptr slesUpperBound(key_type const& key) const = 0; // used by the implementation - virtual - std::unique_ptr + virtual std::unique_ptr txsBegin() const = 0; // used by the implementation - virtual - std::unique_ptr + virtual std::unique_ptr txsEnd() const = 0; /** Returns `true` if a tx exists in the tx map. @@ -370,9 +357,8 @@ public: A tx exists in the map if it is part of the base ledger, or if it is a newly inserted tx. */ - virtual - bool - txExists (key_type const& key) const = 0; + virtual bool + txExists(key_type const& key) const = 0; /** Read a transaction from the tx map. @@ -382,9 +368,8 @@ public: @return A pair of nullptr if the key is not found in the tx map. */ - virtual - tx_type - txRead (key_type const& key) const = 0; + virtual tx_type + txRead(key_type const& key) const = 0; // // Memberspaces @@ -404,39 +389,37 @@ public: //------------------------------------------------------------------------------ /** ReadView that associates keys with digests. */ -class DigestAwareReadView - : public ReadView +class DigestAwareReadView : public ReadView { public: using digest_type = uint256; - DigestAwareReadView () = default; - DigestAwareReadView (const DigestAwareReadView&) = default; + DigestAwareReadView() = default; + DigestAwareReadView(const DigestAwareReadView&) = default; /** Return the digest associated with the key. @return boost::none if the item does not exist. */ - virtual - boost::optional - digest (key_type const& key) const = 0; + virtual boost::optional + digest(key_type const& key) const = 0; }; //------------------------------------------------------------------------------ // ledger close flags -static -std::uint32_t const sLCF_NoConsensusTime = 0x01; +static std::uint32_t const sLCF_NoConsensusTime = 0x01; -inline -bool getCloseAgree (LedgerInfo const& info) +inline bool +getCloseAgree(LedgerInfo const& info) { return (info.closeFlags & sLCF_NoConsensusTime) == 0; } -void addRaw (LedgerInfo const&, Serializer&); +void +addRaw(LedgerInfo const&, Serializer&); -} // ripple +} // namespace ripple #include diff --git a/src/ripple/ledger/Sandbox.h b/src/ripple/ledger/Sandbox.h index 36f6c9e6bf..d8610a69ef 100644 --- a/src/ripple/ledger/Sandbox.h +++ b/src/ripple/ledger/Sandbox.h @@ -31,34 +31,33 @@ namespace ripple { @note Presented as ApplyView to clients. */ -class Sandbox - : public detail::ApplyViewBase +class Sandbox : public detail::ApplyViewBase { public: Sandbox() = delete; - Sandbox (Sandbox const&) = delete; - Sandbox& operator= (Sandbox&&) = delete; - Sandbox& operator= (Sandbox const&) = delete; + Sandbox(Sandbox const&) = delete; + Sandbox& + operator=(Sandbox&&) = delete; + Sandbox& + operator=(Sandbox const&) = delete; - Sandbox (Sandbox&&) = default; + Sandbox(Sandbox&&) = default; - Sandbox (ReadView const* base, ApplyFlags flags) - : ApplyViewBase (base, flags) + Sandbox(ReadView const* base, ApplyFlags flags) : ApplyViewBase(base, flags) { } - Sandbox (ApplyView const* base) - : Sandbox(base, base->flags()) + Sandbox(ApplyView const* base) : Sandbox(base, base->flags()) { } void - apply (RawView& to) + apply(RawView& to) { items_.apply(to); } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/TxMeta.h b/src/ripple/ledger/TxMeta.h index d5ea4ad0a6..426a967077 100644 --- a/src/ripple/ledger/TxMeta.h +++ b/src/ripple/ledger/TxMeta.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_APP_TX_TRANSACTIONMETA_H_INCLUDED #define RIPPLE_APP_TX_TRANSACTIONMETA_H_INCLUDED -#include -#include -#include #include +#include +#include +#include #include #include @@ -41,108 +41,130 @@ private: { explicit CtorHelper() = default; }; - template - TxMeta (uint256 const& txID, std::uint32_t ledger, T const& data, CtorHelper); + template + TxMeta( + uint256 const& txID, + std::uint32_t ledger, + T const& data, + CtorHelper); + public: - TxMeta () - : mLedger (0) - , mIndex (static_cast (-1)) - , mResult (255) + TxMeta() : mLedger(0), mIndex(static_cast(-1)), mResult(255) { } - TxMeta (uint256 const& txID, std::uint32_t ledger, std::uint32_t index) - : mTransactionID (txID) - , mLedger (ledger) - , mIndex (static_cast (-1)) - , mResult (255) + TxMeta(uint256 const& txID, std::uint32_t ledger, std::uint32_t index) + : mTransactionID(txID) + , mLedger(ledger) + , mIndex(static_cast(-1)) + , mResult(255) { } - TxMeta (uint256 const& txID, std::uint32_t ledger, Blob const&); - TxMeta (uint256 const& txID, std::uint32_t ledger, std::string const&); - TxMeta (uint256 const& txID, std::uint32_t ledger, STObject const&); + TxMeta(uint256 const& txID, std::uint32_t ledger, Blob const&); + TxMeta(uint256 const& txID, std::uint32_t ledger, std::string const&); + TxMeta(uint256 const& txID, std::uint32_t ledger, STObject const&); - void init (uint256 const& transactionID, std::uint32_t ledger); - void clear () + void + init(uint256 const& transactionID, std::uint32_t ledger); + void + clear() { - mNodes.clear (); + mNodes.clear(); } - void swap (TxMeta&) noexcept; + void + swap(TxMeta&) noexcept; - uint256 const& getTxID () + uint256 const& + getTxID() { return mTransactionID; } - std::uint32_t getLgrSeq () + std::uint32_t + getLgrSeq() { return mLedger; } - int getResult () const + int + getResult() const { return mResult; } - TER getResultTER () const + TER + getResultTER() const { - return TER::fromInt (mResult); + return TER::fromInt(mResult); } - std::uint32_t getIndex () const + std::uint32_t + getIndex() const { return mIndex; } - bool isNodeAffected (uint256 const& ) const; - void setAffectedNode (uint256 const& , SField const& type, - std::uint16_t nodeType); - STObject& getAffectedNode (SLE::ref node, SField const& type); // create if needed - STObject& getAffectedNode (uint256 const& ); - const STObject& peekAffectedNode (uint256 const& ) const; + bool + isNodeAffected(uint256 const&) const; + void + setAffectedNode(uint256 const&, SField const& type, std::uint16_t nodeType); + STObject& + getAffectedNode(SLE::ref node, SField const& type); // create if needed + STObject& + getAffectedNode(uint256 const&); + const STObject& + peekAffectedNode(uint256 const&) const; /** Return a list of accounts affected by this transaction */ boost::container::flat_set getAffectedAccounts(beast::Journal j) const; - Json::Value getJson (JsonOptions p) const + Json::Value + getJson(JsonOptions p) const { - return getAsObject ().getJson (p); + return getAsObject().getJson(p); } - void addRaw (Serializer&, TER, std::uint32_t index); + void + addRaw(Serializer&, TER, std::uint32_t index); - STObject getAsObject () const; - STArray& getNodes () + STObject + getAsObject() const; + STArray& + getNodes() { return (mNodes); } - void setDeliveredAmount (STAmount const& delivered) + void + setDeliveredAmount(STAmount const& delivered) { - mDelivered.reset (delivered); + mDelivered.reset(delivered); } - STAmount getDeliveredAmount () const + STAmount + getDeliveredAmount() const { - assert (hasDeliveredAmount ()); + assert(hasDeliveredAmount()); return *mDelivered; } - bool hasDeliveredAmount () const + bool + hasDeliveredAmount() const { - return static_cast (mDelivered); + return static_cast(mDelivered); } - static bool thread (STObject& node, uint256 const& prevTxID, std::uint32_t prevLgrID); + static bool + thread(STObject& node, uint256 const& prevTxID, std::uint32_t prevLgrID); private: - uint256 mTransactionID; + uint256 mTransactionID; std::uint32_t mLedger; std::uint32_t mIndex; - int mResult; + int mResult; - boost::optional mDelivered; + boost::optional mDelivered; STArray mNodes; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/View.h b/src/ripple/ledger/View.h index 638d4991e7..d6a23d1e40 100644 --- a/src/ripple/ledger/View.h +++ b/src/ripple/ledger/View.h @@ -20,19 +20,19 @@ #ifndef RIPPLE_LEDGER_VIEW_H_INCLUDED #define RIPPLE_LEDGER_VIEW_H_INCLUDED +#include +#include #include #include #include #include #include #include -#include #include #include #include +#include #include -#include -#include #include #include #include @@ -50,33 +50,37 @@ namespace ripple { //------------------------------------------------------------------------------ /** Controls the treatment of frozen account balances */ -enum FreezeHandling -{ - fhIGNORE_FREEZE, - fhZERO_IF_FROZEN -}; +enum FreezeHandling { fhIGNORE_FREEZE, fhZERO_IF_FROZEN }; [[nodiscard]] bool -isGlobalFrozen (ReadView const& view, +isGlobalFrozen(ReadView const& view, AccountID const& issuer); + +[[nodiscard]] bool +isFrozen( + ReadView const& view, + AccountID const& account, + Currency const& currency, AccountID const& issuer); -[[nodiscard]] bool -isFrozen (ReadView const& view, AccountID const& account, - Currency const& currency, AccountID const& issuer); - // Returns the amount an account can spend without going into debt. // // <-- saAmount: amount of currency held by account. May be negative. [[nodiscard]] STAmount -accountHolds (ReadView const& view, - AccountID const& account, Currency const& currency, - AccountID const& issuer, FreezeHandling zeroIfFrozen, - beast::Journal j); +accountHolds( + ReadView const& view, + AccountID const& account, + Currency const& currency, + AccountID const& issuer, + FreezeHandling zeroIfFrozen, + beast::Journal j); [[nodiscard]] STAmount -accountFunds (ReadView const& view, AccountID const& id, - STAmount const& saDefault, FreezeHandling freezeHandling, - beast::Journal j); +accountFunds( + ReadView const& view, + AccountID const& id, + STAmount const& saDefault, + FreezeHandling freezeHandling, + beast::Journal j); // Return the account's liquid (not reserved) XRP. Generally prefer // calling accountHolds() over this interface. However this interface @@ -85,13 +89,18 @@ accountFunds (ReadView const& view, AccountID const& id, // // @param ownerCountAdj positive to add to count, negative to reduce count. [[nodiscard]] XRPAmount -xrpLiquid (ReadView const& view, AccountID const& id, - std::int32_t ownerCountAdj, beast::Journal j); +xrpLiquid( + ReadView const& view, + AccountID const& id, + std::int32_t ownerCountAdj, + beast::Journal j); /** Iterate all items in an account's owner directory. */ void -forEachItem (ReadView const& view, AccountID const& id, - std::function const&)> f); +forEachItem( + ReadView const& view, + AccountID const& id, + std::function const&)> f); /** Iterate all items after an item in an owner directory. @param after The key of the item to start after @@ -100,52 +109,55 @@ forEachItem (ReadView const& view, AccountID const& id, @return `false` if the iteration failed */ bool -forEachItemAfter (ReadView const& view, AccountID const& id, - uint256 const& after, std::uint64_t const hint, - unsigned int limit, std::function< - bool (std::shared_ptr const&)> f); +forEachItemAfter( + ReadView const& view, + AccountID const& id, + uint256 const& after, + std::uint64_t const hint, + unsigned int limit, + std::function const&)> f); [[nodiscard]] Rate -transferRate (ReadView const& view, - AccountID const& issuer); +transferRate(ReadView const& view, AccountID const& issuer); /** Returns `true` if the directory is empty @param key The key of the directory */ [[nodiscard]] bool -dirIsEmpty (ReadView const& view, - Keylet const& k); +dirIsEmpty(ReadView const& view, Keylet const& k); // Return the first entry and advance uDirEntry. // <-- true, if had a next entry. // VFALCO Fix these clumsy routines with an iterator bool -cdirFirst (ReadView const& view, - uint256 const& uRootIndex, // --> Root of directory. - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-- next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +cdirFirst( + ReadView const& view, + uint256 const& uRootIndex, // --> Root of directory. + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-- next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j); // Return the current entry and advance uDirEntry. // <-- true, if had a next entry. // VFALCO Fix these clumsy routines with an iterator bool -cdirNext (ReadView const& view, - uint256 const& uRootIndex, // --> Root of directory - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-> next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +cdirNext( + ReadView const& view, + uint256 const& uRootIndex, // --> Root of directory + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-> next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j); // Return the list of enabled amendments -[[nodiscard]] std::set -getEnabledAmendments (ReadView const& view); +[[nodiscard]] std::set +getEnabledAmendments(ReadView const& view); // Return a map of amendments that have achieved majority -using majorityAmendments_t = std::map ; +using majorityAmendments_t = std::map; [[nodiscard]] majorityAmendments_t -getMajorityAmendments (ReadView const& view); +getMajorityAmendments(ReadView const& view); /** Return the hash of a ledger by sequence. The hash is retrieved by looking up the "skip list" @@ -157,8 +169,7 @@ getMajorityAmendments (ReadView const& view); given sequence number or boost::none. */ [[nodiscard]] boost::optional -hashOfSeq (ReadView const& ledger, LedgerIndex seq, - beast::Journal journal); +hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal); /** Find a ledger index from which we could easily get the requested ledger @@ -172,9 +183,8 @@ hashOfSeq (ReadView const& ledger, LedgerIndex seq, because every such ledger is permanently enshrined in a LedgerHashes page which we can easily retrieve via the skip list. */ -inline -LedgerIndex -getCandidateLedger (LedgerIndex requested) +inline LedgerIndex +getCandidateLedger(LedgerIndex requested) { return (requested + 255) & (~255); } @@ -185,12 +195,19 @@ getCandidateLedger (LedgerIndex requested) use the second form if you have not acquired the valid ledger yet */ [[nodiscard]] bool -areCompatible (ReadView const& validLedger, ReadView const& testLedger, - beast::Journal::Stream& s, const char* reason); +areCompatible( + ReadView const& validLedger, + ReadView const& testLedger, + beast::Journal::Stream& s, + const char* reason); [[nodiscard]] bool -areCompatible (uint256 const& validHash, LedgerIndex validIndex, - ReadView const& testLedger, beast::Journal::Stream& s, const char* reason); +areCompatible( + uint256 const& validHash, + LedgerIndex validIndex, + ReadView const& testLedger, + beast::Journal::Stream& s, + const char* reason); //------------------------------------------------------------------------------ // @@ -200,42 +217,47 @@ areCompatible (uint256 const& validHash, LedgerIndex validIndex, /** Adjust the owner count up or down. */ void -adjustOwnerCount (ApplyView& view, +adjustOwnerCount( + ApplyView& view, std::shared_ptr const& sle, - std::int32_t amount, beast::Journal j); + std::int32_t amount, + beast::Journal j); // Return the first entry and advance uDirEntry. // <-- true, if had a next entry. // VFALCO Fix these clumsy routines with an iterator bool -dirFirst (ApplyView& view, - uint256 const& uRootIndex, // --> Root of directory. - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-- next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +dirFirst( + ApplyView& view, + uint256 const& uRootIndex, // --> Root of directory. + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-- next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j); // Return the current entry and advance uDirEntry. // <-- true, if had a next entry. // VFALCO Fix these clumsy routines with an iterator bool -dirNext (ApplyView& view, - uint256 const& uRootIndex, // --> Root of directory - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-> next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +dirNext( + ApplyView& view, + uint256 const& uRootIndex, // --> Root of directory + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-> next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j); -[[nodiscard]] std::function +[[nodiscard]] std::function describeOwnerDir(AccountID const& account); // deprecated boost::optional -dirAdd (ApplyView& view, - Keylet const& uRootIndex, - uint256 const& uLedgerIndex, - bool strictOrder, - std::function fDescriber, +dirAdd( + ApplyView& view, + Keylet const& uRootIndex, + uint256 const& uLedgerIndex, + bool strictOrder, + std::function fDescriber, beast::Journal j); // VFALCO NOTE Both STAmount parameters should just @@ -246,29 +268,31 @@ dirAdd (ApplyView& view, This can set an initial balance. */ [[nodiscard]] TER -trustCreate (ApplyView& view, - const bool bSrcHigh, - AccountID const& uSrcAccountID, - AccountID const& uDstAccountID, - uint256 const& uIndex, // --> ripple state entry - SLE::ref sleAccount, // --> the account being set. - const bool bAuth, // --> authorize account. - const bool bNoRipple, // --> others cannot ripple through - const bool bFreeze, // --> funds cannot leave - STAmount const& saBalance, // --> balance of account being set. - // Issuer should be noAccount() - STAmount const& saLimit, // --> limit for account being set. - // Issuer should be the account being set. +trustCreate( + ApplyView& view, + const bool bSrcHigh, + AccountID const& uSrcAccountID, + AccountID const& uDstAccountID, + uint256 const& uIndex, // --> ripple state entry + SLE::ref sleAccount, // --> the account being set. + const bool bAuth, // --> authorize account. + const bool bNoRipple, // --> others cannot ripple through + const bool bFreeze, // --> funds cannot leave + STAmount const& saBalance, // --> balance of account being set. + // Issuer should be noAccount() + STAmount const& saLimit, // --> limit for account being set. + // Issuer should be the account being set. std::uint32_t uSrcQualityIn, std::uint32_t uSrcQualityOut, beast::Journal j); [[nodiscard]] TER -trustDelete (ApplyView& view, +trustDelete( + ApplyView& view, std::shared_ptr const& sleRippleState, - AccountID const& uLowAccountID, - AccountID const& uHighAccountID, - beast::Journal j); + AccountID const& uLowAccountID, + AccountID const& uHighAccountID, + beast::Journal j); /** Delete an offer. @@ -278,9 +302,7 @@ trustDelete (ApplyView& view, */ // [[nodiscard]] // nodiscard commented out so Flow, BookTip and others compile. TER -offerDelete (ApplyView& view, - std::shared_ptr const& sle, - beast::Journal j); +offerDelete(ApplyView& view, std::shared_ptr const& sle, beast::Journal j); //------------------------------------------------------------------------------ @@ -294,40 +316,46 @@ offerDelete (ApplyView& view, // --> bCheckIssuer : normally require issuer to be involved. // [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles. TER -rippleCredit (ApplyView& view, - AccountID const& uSenderID, AccountID const& uReceiverID, - const STAmount & saAmount, bool bCheckIssuer, +rippleCredit( + ApplyView& view, + AccountID const& uSenderID, + AccountID const& uReceiverID, + const STAmount& saAmount, + bool bCheckIssuer, beast::Journal j); -[[nodiscard]] -TER -accountSend (ApplyView& view, +[[nodiscard]] TER +accountSend( + ApplyView& view, AccountID const& from, - AccountID const& to, - const STAmount & saAmount, - beast::Journal j); + AccountID const& to, + const STAmount& saAmount, + beast::Journal j); [[nodiscard]] TER -issueIOU (ApplyView& view, +issueIOU( + ApplyView& view, AccountID const& account, - STAmount const& amount, - Issue const& issue, - beast::Journal j); + STAmount const& amount, + Issue const& issue, + beast::Journal j); [[nodiscard]] TER -redeemIOU (ApplyView& view, +redeemIOU( + ApplyView& view, AccountID const& account, - STAmount const& amount, - Issue const& issue, - beast::Journal j); + STAmount const& amount, + Issue const& issue, + beast::Journal j); [[nodiscard]] TER -transferXRP (ApplyView& view, +transferXRP( + ApplyView& view, AccountID const& from, - AccountID const& to, - STAmount const& amount, - beast::Journal j); + AccountID const& to, + STAmount const& amount, + beast::Journal j); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/ledger/detail/ApplyStateTable.h b/src/ripple/ledger/detail/ApplyStateTable.h index 1bbfbe30e1..c63fd4c2d0 100644 --- a/src/ripple/ledger/detail/ApplyStateTable.h +++ b/src/ripple/ledger/detail/ApplyStateTable.h @@ -20,13 +20,13 @@ #ifndef RIPPLE_LEDGER_APPLYSTATETABLE_H_INCLUDED #define RIPPLE_LEDGER_APPLYSTATETABLE_H_INCLUDED +#include +#include #include #include #include #include -#include #include -#include #include namespace ripple { @@ -39,121 +39,122 @@ public: using key_type = ReadView::key_type; private: - enum class Action - { + enum class Action { cache, erase, insert, modify, }; - using items_t = std::map>>; + using items_t = std::map>>; items_t items_; XRPAmount dropsDestroyed_{0}; public: ApplyStateTable() = default; - ApplyStateTable (ApplyStateTable&&) = default; + ApplyStateTable(ApplyStateTable&&) = default; - ApplyStateTable (ApplyStateTable const&) = delete; - ApplyStateTable& operator= (ApplyStateTable&&) = delete; - ApplyStateTable& operator= (ApplyStateTable const&) = delete; + ApplyStateTable(ApplyStateTable const&) = delete; + ApplyStateTable& + operator=(ApplyStateTable&&) = delete; + ApplyStateTable& + operator=(ApplyStateTable const&) = delete; void - apply (RawView& to) const; + apply(RawView& to) const; void - apply (OpenView& to, STTx const& tx, - TER ter, boost::optional< - STAmount> const& deliver, - beast::Journal j); + apply( + OpenView& to, + STTx const& tx, + TER ter, + boost::optional const& deliver, + beast::Journal j); bool - exists (ReadView const& base, - Keylet const& k) const; + exists(ReadView const& base, Keylet const& k) const; boost::optional - succ (ReadView const& base, - key_type const& key, boost::optional< - key_type> const& last) const; + succ( + ReadView const& base, + key_type const& key, + boost::optional const& last) const; std::shared_ptr - read (ReadView const& base, - Keylet const& k) const; + read(ReadView const& base, Keylet const& k) const; std::shared_ptr - peek (ReadView const& base, - Keylet const& k); + peek(ReadView const& base, Keylet const& k); std::size_t - size () const; + size() const; void - visit (ReadView const& base, - std::function const& before, - std::shared_ptr const& after)> const& func) const; + std::shared_ptr const& before, + std::shared_ptr const& after)> const& func) const; void - erase (ReadView const& base, - std::shared_ptr const& sle); + erase(ReadView const& base, std::shared_ptr const& sle); void - rawErase (ReadView const& base, - std::shared_ptr const& sle); + rawErase(ReadView const& base, std::shared_ptr const& sle); void - insert(ReadView const& base, - std::shared_ptr const& sle); + insert(ReadView const& base, std::shared_ptr const& sle); void - update(ReadView const& base, - std::shared_ptr const& sle); + update(ReadView const& base, std::shared_ptr const& sle); void - replace(ReadView const& base, - std::shared_ptr const& sle); + replace(ReadView const& base, std::shared_ptr const& sle); void - destroyXRP (XRPAmount const& fee); + destroyXRP(XRPAmount const& fee); // For debugging - XRPAmount const& dropsDestroyed () const + XRPAmount const& + dropsDestroyed() const { return dropsDestroyed_; } private: - using Mods = hash_map>; + using Mods = hash_map>; - static - void - threadItem (TxMeta& meta, - std::shared_ptr const& to); + static void + threadItem(TxMeta& meta, std::shared_ptr const& to); std::shared_ptr - getForMod (ReadView const& base, - key_type const& key, Mods& mods, - beast::Journal j); + getForMod( + ReadView const& base, + key_type const& key, + Mods& mods, + beast::Journal j); void - threadTx (ReadView const& base, TxMeta& meta, - AccountID const& to, Mods& mods, - beast::Journal j); + threadTx( + ReadView const& base, + TxMeta& meta, + AccountID const& to, + Mods& mods, + beast::Journal j); void - threadOwners (ReadView const& base, - TxMeta& meta, std::shared_ptr< - SLE const> const& sle, Mods& mods, - beast::Journal j); + threadOwners( + ReadView const& base, + TxMeta& meta, + std::shared_ptr const& sle, + Mods& mods, + beast::Journal j); }; -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/ledger/detail/ApplyViewBase.h b/src/ripple/ledger/detail/ApplyViewBase.h index 91bf1e02dc..6b7c310e9c 100644 --- a/src/ripple/ledger/detail/ApplyViewBase.h +++ b/src/ripple/ledger/detail/ApplyViewBase.h @@ -20,31 +20,29 @@ #ifndef RIPPLE_LEDGER_APPLYVIEWBASE_H_INCLUDED #define RIPPLE_LEDGER_APPLYVIEWBASE_H_INCLUDED +#include #include #include #include #include #include -#include namespace ripple { namespace detail { -class ApplyViewBase - : public ApplyView - , public RawView +class ApplyViewBase : public ApplyView, public RawView { public: ApplyViewBase() = delete; - ApplyViewBase (ApplyViewBase const&) = delete; - ApplyViewBase& operator= (ApplyViewBase&&) = delete; - ApplyViewBase& operator= (ApplyViewBase const&) = delete; + ApplyViewBase(ApplyViewBase const&) = delete; + ApplyViewBase& + operator=(ApplyViewBase&&) = delete; + ApplyViewBase& + operator=(ApplyViewBase const&) = delete; + ApplyViewBase(ApplyViewBase&&) = default; - ApplyViewBase (ApplyViewBase&&) = default; - - ApplyViewBase( - ReadView const* base, ApplyFlags flags); + ApplyViewBase(ReadView const* base, ApplyFlags flags); // ReadView bool @@ -60,14 +58,15 @@ public: rules() const override; bool - exists (Keylet const& k) const override; + exists(Keylet const& k) const override; boost::optional - succ (key_type const& key, boost::optional< - key_type> const& last = boost::none) const override; + succ( + key_type const& key, + boost::optional const& last = boost::none) const override; std::shared_ptr - read (Keylet const& k) const override; + read(Keylet const& k) const override; std::unique_ptr slesBegin() const override; @@ -85,10 +84,10 @@ public: txsEnd() const override; bool - txExists (key_type const& key) const override; + txExists(key_type const& key) const override; tx_type - txRead (key_type const& key) const override; + txRead(key_type const& key) const override; // ApplyView @@ -96,42 +95,37 @@ public: flags() const override; std::shared_ptr - peek (Keylet const& k) override; + peek(Keylet const& k) override; void - erase (std::shared_ptr< - SLE> const& sle) override; + erase(std::shared_ptr const& sle) override; void - insert (std::shared_ptr< - SLE> const& sle) override; + insert(std::shared_ptr const& sle) override; void - update (std::shared_ptr< - SLE> const& sle) override; + update(std::shared_ptr const& sle) override; // RawView void - rawErase (std::shared_ptr< - SLE> const& sle) override; + rawErase(std::shared_ptr const& sle) override; void - rawInsert (std::shared_ptr< - SLE> const& sle) override; + rawInsert(std::shared_ptr const& sle) override; void - rawReplace (std::shared_ptr< - SLE> const& sle) override; + rawReplace(std::shared_ptr const& sle) override; void - rawDestroyXRP ( - XRPAmount const& feeDrops) override; + rawDestroyXRP(XRPAmount const& feeDrops) override; - friend - CashDiff cashFlowDiff ( - CashFilter lhsFilter, ApplyViewBase const& lhs, - CashFilter rhsFilter, ApplyViewBase const& rhs); + friend CashDiff + cashFlowDiff( + CashFilter lhsFilter, + ApplyViewBase const& lhs, + CashFilter rhsFilter, + ApplyViewBase const& rhs); protected: ApplyFlags flags_; @@ -139,7 +133,7 @@ protected: detail::ApplyStateTable items_; }; -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/ledger/detail/RawStateTable.h b/src/ripple/ledger/detail/RawStateTable.h index fcd2d2836c..cf2e70b002 100644 --- a/src/ripple/ledger/detail/RawStateTable.h +++ b/src/ripple/ledger/detail/RawStateTable.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_LEDGER_RAWSTATETABLE_H_INCLUDED #define RIPPLE_LEDGER_RAWSTATETABLE_H_INCLUDED +#include #include #include -#include #include #include @@ -36,52 +36,52 @@ public: using key_type = ReadView::key_type; RawStateTable() = default; - RawStateTable (RawStateTable const&) = default; - RawStateTable (RawStateTable&&) = default; + RawStateTable(RawStateTable const&) = default; + RawStateTable(RawStateTable&&) = default; - RawStateTable& operator= (RawStateTable&&) = delete; - RawStateTable& operator= (RawStateTable const&) = delete; + RawStateTable& + operator=(RawStateTable&&) = delete; + RawStateTable& + operator=(RawStateTable const&) = delete; void - apply (RawView& to) const; + apply(RawView& to) const; bool - exists (ReadView const& base, - Keylet const& k) const; + exists(ReadView const& base, Keylet const& k) const; boost::optional - succ (ReadView const& base, - key_type const& key, boost::optional< - key_type> const& last) const; + succ( + ReadView const& base, + key_type const& key, + boost::optional const& last) const; void - erase (std::shared_ptr const& sle); + erase(std::shared_ptr const& sle); void - insert (std::shared_ptr const& sle); + insert(std::shared_ptr const& sle); void - replace (std::shared_ptr const& sle); + replace(std::shared_ptr const& sle); std::shared_ptr - read (ReadView const& base, - Keylet const& k) const; + read(ReadView const& base, Keylet const& k) const; void - destroyXRP (XRPAmount const& fee); + destroyXRP(XRPAmount const& fee); std::unique_ptr - slesBegin (ReadView const& base) const; + slesBegin(ReadView const& base) const; std::unique_ptr - slesEnd (ReadView const& base) const; + slesEnd(ReadView const& base) const; std::unique_ptr - slesUpperBound (ReadView const& base, uint256 const& key) const; + slesUpperBound(ReadView const& base, uint256 const& key) const; private: - enum class Action - { + enum class Action { erase, insert, replace, @@ -89,16 +89,19 @@ private: class sles_iter_impl; - using items_t = std::map>, - std::less, qalloc_type>>, false>>; + std::less, + qalloc_type< + std::pair>>, + false>>; items_t items_; XRPAmount dropsDestroyed_{0}; }; -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/ledger/detail/ReadViewFwdRange.h b/src/ripple/ledger/detail/ReadViewFwdRange.h index debf112d75..3e7408568e 100644 --- a/src/ripple/ledger/detail/ReadViewFwdRange.h +++ b/src/ripple/ledger/detail/ReadViewFwdRange.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_LEDGER_READVIEWFWDRANGE_H_INCLUDED #define RIPPLE_LEDGER_READVIEWFWDRANGE_H_INCLUDED +#include #include #include #include -#include namespace ripple { @@ -43,38 +43,33 @@ public: ReadViewFwdIter() = default; ReadViewFwdIter(ReadViewFwdIter const&) = default; - ReadViewFwdIter& operator=(ReadViewFwdIter const&) = default; + ReadViewFwdIter& + operator=(ReadViewFwdIter const&) = default; - virtual - ~ReadViewFwdIter() = default; + virtual ~ReadViewFwdIter() = default; - virtual - std::unique_ptr + virtual std::unique_ptr copy() const = 0; - virtual - bool - equal (ReadViewFwdIter const& impl) const = 0; + virtual bool + equal(ReadViewFwdIter const& impl) const = 0; - virtual - void + virtual void increment() = 0; - virtual - value_type + virtual value_type dereference() const = 0; }; // A range using type-erased ForwardIterator // -template +template class ReadViewFwdRange { public: - using iter_base = - ReadViewFwdIter; + using iter_base = ReadViewFwdIter; - static_assert( + static_assert( std::is_nothrow_move_constructible{}, "ReadViewFwdRange move and move assign constructors should be " "noexcept"); @@ -88,33 +83,31 @@ public: using reference = value_type const&; - using difference_type = - std::ptrdiff_t; + using difference_type = std::ptrdiff_t; - using iterator_category = - std::forward_iterator_tag; + using iterator_category = std::forward_iterator_tag; iterator() = default; - iterator (iterator const& other); - iterator (iterator&& other) noexcept; + iterator(iterator const& other); + iterator(iterator&& other) noexcept; // Used by the implementation - explicit - iterator (ReadView const* view, + explicit iterator( + ReadView const* view, std::unique_ptr impl); iterator& - operator= (iterator const& other); + operator=(iterator const& other); iterator& - operator= (iterator&& other) noexcept; + operator=(iterator&& other) noexcept; bool - operator== (iterator const& other) const; + operator==(iterator const& other) const; bool - operator!= (iterator const& other) const; + operator!=(iterator const& other) const; // Can throw reference @@ -138,22 +131,21 @@ public: static_assert(std::is_nothrow_move_constructible{}, ""); static_assert(std::is_nothrow_move_assignable{}, ""); - + using const_iterator = iterator; using value_type = ValueType; ReadViewFwdRange() = delete; - ReadViewFwdRange (ReadViewFwdRange const&) = default; - ReadViewFwdRange& operator= (ReadViewFwdRange const&) = default; + ReadViewFwdRange(ReadViewFwdRange const&) = default; + ReadViewFwdRange& + operator=(ReadViewFwdRange const&) = default; // VFALCO Otherwise causes errors on clang -//private: -// friend class ReadView; + // private: + // friend class ReadView; - explicit - ReadViewFwdRange (ReadView const& view) - : view_ (&view) + explicit ReadViewFwdRange(ReadView const& view) : view_(&view) { } @@ -162,7 +154,7 @@ protected: boost::optional mutable end_; }; -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/ledger/detail/ReadViewFwdRange.ipp b/src/ripple/ledger/detail/ReadViewFwdRange.ipp index 7e39d87ace..7fc1bb3da8 100644 --- a/src/ripple/ledger/detail/ReadViewFwdRange.ipp +++ b/src/ripple/ledger/detail/ReadViewFwdRange.ipp @@ -23,54 +23,47 @@ namespace ripple { namespace detail { -template -ReadViewFwdRange::iterator::iterator( - iterator const& other) - : view_ (other.view_) - , impl_ (other.impl_ ? - other.impl_->copy() : nullptr) - , cache_ (other.cache_) +template +ReadViewFwdRange::iterator::iterator(iterator const& other) + : view_(other.view_) + , impl_(other.impl_ ? other.impl_->copy() : nullptr) + , cache_(other.cache_) { } -template -ReadViewFwdRange::iterator::iterator( - iterator&& other) noexcept - : view_ (other.view_) - , impl_ (std::move(other.impl_)) - , cache_ (std::move(other.cache_)) +template +ReadViewFwdRange::iterator::iterator(iterator&& other) noexcept + : view_(other.view_) + , impl_(std::move(other.impl_)) + , cache_(std::move(other.cache_)) { } -template +template ReadViewFwdRange::iterator::iterator( ReadView const* view, - std::unique_ptr impl) - : view_ (view) - , impl_ (std::move(impl)) + std::unique_ptr impl) + : view_(view), impl_(std::move(impl)) { } -template +template auto -ReadViewFwdRange::iterator::operator=( - iterator const& other) -> - iterator& +ReadViewFwdRange::iterator::operator=(iterator const& other) + -> iterator& { if (this == &other) return *this; view_ = other.view_; - impl_ = other.impl_ ? - other.impl_->copy() : nullptr; + impl_ = other.impl_ ? other.impl_->copy() : nullptr; cache_ = other.cache_; return *this; } -template +template auto -ReadViewFwdRange::iterator::operator=( - iterator&& other) noexcept -> - iterator& +ReadViewFwdRange::iterator::operator=(iterator&& other) noexcept + -> iterator& { view_ = other.view_; impl_ = std::move(other.impl_); @@ -78,64 +71,57 @@ ReadViewFwdRange::iterator::operator=( return *this; } -template +template bool -ReadViewFwdRange::iterator::operator==( - iterator const& other) const +ReadViewFwdRange::iterator::operator==(iterator const& other) const { assert(view_ == other.view_); return impl_->equal(*other.impl_); } -template +template bool -ReadViewFwdRange::iterator::operator!=( - iterator const& other) const +ReadViewFwdRange::iterator::operator!=(iterator const& other) const { - return ! (*this == other); + return !(*this == other); } -template +template auto -ReadViewFwdRange::iterator::operator*() const -> - reference +ReadViewFwdRange::iterator::operator*() const -> reference { - if (! cache_) + if (!cache_) cache_ = impl_->dereference(); return *cache_; } -template +template auto -ReadViewFwdRange::iterator::operator->() const -> - pointer +ReadViewFwdRange::iterator::operator->() const -> pointer { return &**this; } -template +template auto -ReadViewFwdRange::iterator::operator++() -> - iterator& +ReadViewFwdRange::iterator::operator++() -> iterator& { impl_->increment(); cache_ = boost::none; return *this; } -template +template auto -ReadViewFwdRange::iterator::operator++(int) -> - iterator +ReadViewFwdRange::iterator::operator++(int) -> iterator { - iterator prev(view_, - impl_->copy()); + iterator prev(view_, impl_->copy()); prev.cache_ = std::move(cache_); ++(*this); return prev; } -} // detail -} // ripple +} // namespace detail +} // namespace ripple #endif diff --git a/src/ripple/ledger/impl/ApplyStateTable.cpp b/src/ripple/ledger/impl/ApplyStateTable.cpp index 8e1afe577d..ce94ce9518 100644 --- a/src/ripple/ledger/impl/ApplyStateTable.cpp +++ b/src/ripple/ledger/impl/ApplyStateTable.cpp @@ -17,9 +17,9 @@ */ //============================================================================== -#include #include #include +#include #include #include #include @@ -28,98 +28,104 @@ namespace ripple { namespace detail { void -ApplyStateTable::apply (RawView& to) const +ApplyStateTable::apply(RawView& to) const { to.rawDestroyXRP(dropsDestroyed_); for (auto const& item : items_) { - auto const& sle = - item.second.second; - switch(item.second.first) + auto const& sle = item.second.second; + switch (item.second.first) { - case Action::cache: - break; - case Action::erase: - to.rawErase(sle); - break; - case Action::insert: - to.rawInsert(sle); - break; - case Action::modify: - to.rawReplace(sle); - break; + case Action::cache: + break; + case Action::erase: + to.rawErase(sle); + break; + case Action::insert: + to.rawInsert(sle); + break; + case Action::modify: + to.rawReplace(sle); + break; }; } } std::size_t -ApplyStateTable::size () const +ApplyStateTable::size() const { std::size_t ret = 0; for (auto& item : items_) { switch (item.second.first) { - case Action::erase: - case Action::insert: - case Action::modify: - ++ret; - default: - break; + case Action::erase: + case Action::insert: + case Action::modify: + ++ret; + default: + break; } } return ret; } void -ApplyStateTable::visit (ReadView const& to, - std::function const& before, - std::shared_ptr const& after)> const& func) const + std::shared_ptr const& before, + std::shared_ptr const& after)> const& func) const { for (auto& item : items_) { switch (item.second.first) { - case Action::erase: - func (item.first, true, - to.read (keylet::unchecked (item.first)), item.second.second); - break; + case Action::erase: + func( + item.first, + true, + to.read(keylet::unchecked(item.first)), + item.second.second); + break; - case Action::insert: - func (item.first, false, - nullptr, item.second.second); - break; + case Action::insert: + func(item.first, false, nullptr, item.second.second); + break; - case Action::modify: - func (item.first, false, - to.read (keylet::unchecked (item.first)), item.second.second); - break; + case Action::modify: + func( + item.first, + false, + to.read(keylet::unchecked(item.first)), + item.second.second); + break; - default: - break; + default: + break; } } } void -ApplyStateTable::apply (OpenView& to, - STTx const& tx, TER ter, - boost::optional const& deliver, - beast::Journal j) +ApplyStateTable::apply( + OpenView& to, + STTx const& tx, + TER ter, + boost::optional const& deliver, + beast::Journal j) { // Build metadata and insert - auto const sTx = - std::make_shared(); + auto const sTx = std::make_shared(); tx.add(*sTx); std::shared_ptr sMeta; if (!to.open()) { TxMeta meta; // VFALCO Shouldn't TxMeta ctor do this? - meta.init (tx.getTransactionID(), to.seq()); + meta.init(tx.getTransactionID(), to.seq()); if (deliver) meta.setDeliveredAmount(*deliver); Mods newMod; @@ -128,138 +134,140 @@ ApplyStateTable::apply (OpenView& to, SField const* type; switch (item.second.first) { - default: - case Action::cache: - continue; - case Action::erase: - type = &sfDeletedNode; - break; - case Action::insert: - type = &sfCreatedNode; - break; - case Action::modify: - type = &sfModifiedNode; - break; + default: + case Action::cache: + continue; + case Action::erase: + type = &sfDeletedNode; + break; + case Action::insert: + type = &sfCreatedNode; + break; + case Action::modify: + type = &sfModifiedNode; + break; } - auto const origNode = - to.read(keylet::unchecked(item.first)); + auto const origNode = to.read(keylet::unchecked(item.first)); auto curNode = item.second.second; if ((type == &sfModifiedNode) && (*curNode == *origNode)) continue; std::uint16_t nodeType = curNode - ? curNode->getFieldU16 (sfLedgerEntryType) - : origNode->getFieldU16 (sfLedgerEntryType); - meta.setAffectedNode (item.first, *type, nodeType); + ? curNode->getFieldU16(sfLedgerEntryType) + : origNode->getFieldU16(sfLedgerEntryType); + meta.setAffectedNode(item.first, *type, nodeType); if (type == &sfDeletedNode) { - assert (origNode && curNode); - threadOwners (to, meta, origNode, newMod, j); + assert(origNode && curNode); + threadOwners(to, meta, origNode, newMod, j); - STObject prevs (sfPreviousFields); + STObject prevs(sfPreviousFields); for (auto const& obj : *origNode) { // go through the original node for // modified fields saved on modification - if (obj.getFName().shouldMeta( - SField::sMD_ChangeOrig) && - ! curNode->hasMatchingEntry (obj)) - prevs.emplace_back (obj); + if (obj.getFName().shouldMeta(SField::sMD_ChangeOrig) && + !curNode->hasMatchingEntry(obj)) + prevs.emplace_back(obj); } - if (!prevs.empty ()) - meta.getAffectedNode(item.first).emplace_back(std::move(prevs)); + if (!prevs.empty()) + meta.getAffectedNode(item.first) + .emplace_back(std::move(prevs)); - STObject finals (sfFinalFields); + STObject finals(sfFinalFields); for (auto const& obj : *curNode) { // go through the final node for final fields if (obj.getFName().shouldMeta( SField::sMD_Always | SField::sMD_DeleteFinal)) - finals.emplace_back (obj); + finals.emplace_back(obj); } - if (!finals.empty ()) - meta.getAffectedNode (item.first).emplace_back (std::move(finals)); + if (!finals.empty()) + meta.getAffectedNode(item.first) + .emplace_back(std::move(finals)); } else if (type == &sfModifiedNode) { - assert (curNode && origNode); + assert(curNode && origNode); - if (curNode->isThreadedType ()) // thread transaction to node item modified - threadItem (meta, curNode); + if (curNode->isThreadedType()) // thread transaction to node + // item modified + threadItem(meta, curNode); - STObject prevs (sfPreviousFields); + STObject prevs(sfPreviousFields); for (auto const& obj : *origNode) { // search the original node for values saved on modify - if (obj.getFName ().shouldMeta (SField::sMD_ChangeOrig) && !curNode->hasMatchingEntry (obj)) - prevs.emplace_back (obj); + if (obj.getFName().shouldMeta(SField::sMD_ChangeOrig) && + !curNode->hasMatchingEntry(obj)) + prevs.emplace_back(obj); } - if (!prevs.empty ()) - meta.getAffectedNode (item.first).emplace_back (std::move(prevs)); + if (!prevs.empty()) + meta.getAffectedNode(item.first) + .emplace_back(std::move(prevs)); - STObject finals (sfFinalFields); + STObject finals(sfFinalFields); for (auto const& obj : *curNode) { // search the final node for values saved always - if (obj.getFName ().shouldMeta (SField::sMD_Always | SField::sMD_ChangeNew)) - finals.emplace_back (obj); + if (obj.getFName().shouldMeta( + SField::sMD_Always | SField::sMD_ChangeNew)) + finals.emplace_back(obj); } - if (!finals.empty ()) - meta.getAffectedNode (item.first).emplace_back (std::move(finals)); + if (!finals.empty()) + meta.getAffectedNode(item.first) + .emplace_back(std::move(finals)); } - else if (type == &sfCreatedNode) // if created, thread to owner(s) + else if (type == &sfCreatedNode) // if created, thread to owner(s) { - assert (curNode && !origNode); - threadOwners (to, meta, curNode, newMod, j); + assert(curNode && !origNode); + threadOwners(to, meta, curNode, newMod, j); - if (curNode->isThreadedType ()) // always thread to self - threadItem (meta, curNode); + if (curNode->isThreadedType()) // always thread to self + threadItem(meta, curNode); - STObject news (sfNewFields); + STObject news(sfNewFields); for (auto const& obj : *curNode) { // save non-default values - if (!obj.isDefault () && - obj.getFName().shouldMeta( - SField::sMD_Create | SField::sMD_Always)) - news.emplace_back (obj); + if (!obj.isDefault() && + obj.getFName().shouldMeta( + SField::sMD_Create | SField::sMD_Always)) + news.emplace_back(obj); } - if (!news.empty ()) - meta.getAffectedNode (item.first).emplace_back (std::move(news)); + if (!news.empty()) + meta.getAffectedNode(item.first) + .emplace_back(std::move(news)); } else { - assert (false); + assert(false); } } // add any new modified nodes to the modification set for (auto& mod : newMod) - to.rawReplace (mod.second); + to.rawReplace(mod.second); sMeta = std::make_shared(); - meta.addRaw (*sMeta, ter, to.txCount()); + meta.addRaw(*sMeta, ter, to.txCount()); // VFALCO For diagnostics do we want to show // metadata even when the base view is open? - JLOG(j.trace()) << - "metadata " << meta.getJson (JsonOptions::none); + JLOG(j.trace()) << "metadata " << meta.getJson(JsonOptions::none); } - to.rawTxInsert( - tx.getTransactionID(), - sTx, sMeta); + to.rawTxInsert(tx.getTransactionID(), sTx, sMeta); apply(to); } //--- bool -ApplyStateTable::exists (ReadView const& base, - Keylet const& k) const +ApplyStateTable::exists(ReadView const& base, Keylet const& k) const { auto const iter = items_.find(k.key); if (iter == items_.end()) @@ -268,23 +276,23 @@ ApplyStateTable::exists (ReadView const& base, auto const& sle = item.second; switch (item.first) { - case Action::erase: - return false; - case Action::cache: - case Action::insert: - case Action::modify: - break; + case Action::erase: + return false; + case Action::cache: + case Action::insert: + case Action::modify: + break; } - if (! k.check(*sle)) + if (!k.check(*sle)) return false; return true; } auto -ApplyStateTable::succ (ReadView const& base, - key_type const& key, boost::optional< - key_type> const& last) const -> - boost::optional +ApplyStateTable::succ( + ReadView const& base, + key_type const& key, + boost::optional const& last) const -> boost::optional { boost::optional next = key; items_t::const_iterator iter; @@ -293,20 +301,17 @@ ApplyStateTable::succ (ReadView const& base, do { next = base.succ(*next, last); - if (! next) + if (!next) break; iter = items_.find(*next); - } - while (iter != items_.end() && - iter->second.first == Action::erase); + } while (iter != items_.end() && iter->second.first == Action::erase); // Find non-deleted successor in our list - for (iter = items_.upper_bound(key); - iter != items_.end (); ++iter) + for (iter = items_.upper_bound(key); iter != items_.end(); ++iter) { if (iter->second.first != Action::erase) { // Found both, return the lower key - if (! next || next > iter->first) + if (!next || next > iter->first) next = iter->first; break; } @@ -319,8 +324,7 @@ ApplyStateTable::succ (ReadView const& base, } std::shared_ptr -ApplyStateTable::read (ReadView const& base, - Keylet const& k) const +ApplyStateTable::read(ReadView const& base, Keylet const& k) const { auto const iter = items_.find(k.key); if (iter == items_.end()) @@ -329,177 +333,166 @@ ApplyStateTable::read (ReadView const& base, auto const& sle = item.second; switch (item.first) { - case Action::erase: - return nullptr; - case Action::cache: - case Action::insert: - case Action::modify: - break; + case Action::erase: + return nullptr; + case Action::cache: + case Action::insert: + case Action::modify: + break; }; - if (! k.check(*sle)) + if (!k.check(*sle)) return nullptr; return sle; } std::shared_ptr -ApplyStateTable::peek (ReadView const& base, - Keylet const& k) +ApplyStateTable::peek(ReadView const& base, Keylet const& k) { auto iter = items_.lower_bound(k.key); - if (iter == items_.end() || - iter->first != k.key) + if (iter == items_.end() || iter->first != k.key) { auto const sle = base.read(k); - if (! sle) + if (!sle) return nullptr; // Make our own copy using namespace std; - iter = items_.emplace_hint (iter, + iter = items_.emplace_hint( + iter, piecewise_construct, - forward_as_tuple(sle->key()), - forward_as_tuple(Action::cache, - make_shared(*sle))); + forward_as_tuple(sle->key()), + forward_as_tuple(Action::cache, make_shared(*sle))); return iter->second.second; } auto const& item = iter->second; auto const& sle = item.second; switch (item.first) { - case Action::erase: - return nullptr; - case Action::cache: - case Action::insert: - case Action::modify: - break; + case Action::erase: + return nullptr; + case Action::cache: + case Action::insert: + case Action::modify: + break; }; - if (! k.check(*sle)) + if (!k.check(*sle)) return nullptr; return sle; } void -ApplyStateTable::erase( - ReadView const& base, - std::shared_ptr const& sle) +ApplyStateTable::erase(ReadView const& base, std::shared_ptr const& sle) { - auto const iter = - items_.find(sle->key()); + auto const iter = items_.find(sle->key()); if (iter == items_.end()) LogicError("ApplyStateTable::erase: missing key"); auto& item = iter->second; if (item.second != sle) LogicError("ApplyStateTable::erase: unknown SLE"); - switch(item.first) + switch (item.first) { - case Action::erase: - LogicError("ApplyStateTable::erase: double erase"); - break; - case Action::insert: - items_.erase(iter); - break; - case Action::cache: - case Action::modify: - item.first = Action::erase; - break; + case Action::erase: + LogicError("ApplyStateTable::erase: double erase"); + break; + case Action::insert: + items_.erase(iter); + break; + case Action::cache: + case Action::modify: + item.first = Action::erase; + break; } } void -ApplyStateTable::rawErase (ReadView const& base, - std::shared_ptr const& sle) +ApplyStateTable::rawErase(ReadView const& base, std::shared_ptr const& sle) { using namespace std; auto const result = items_.emplace( piecewise_construct, - forward_as_tuple(sle->key()), - forward_as_tuple(Action::erase, sle)); + forward_as_tuple(sle->key()), + forward_as_tuple(Action::erase, sle)); if (result.second) return; auto& item = result.first->second; - switch(item.first) + switch (item.first) { - case Action::erase: - LogicError("ApplyStateTable::rawErase: double erase"); - break; - case Action::insert: - items_.erase(result.first); - break; - case Action::cache: - case Action::modify: - item.first = Action::erase; - item.second = sle; - break; + case Action::erase: + LogicError("ApplyStateTable::rawErase: double erase"); + break; + case Action::insert: + items_.erase(result.first); + break; + case Action::cache: + case Action::modify: + item.first = Action::erase; + item.second = sle; + break; } } void -ApplyStateTable::insert (ReadView const& base, - std::shared_ptr const& sle) +ApplyStateTable::insert(ReadView const& base, std::shared_ptr const& sle) { - auto const iter = - items_.lower_bound(sle->key()); - if (iter == items_.end() || - iter->first != sle->key()) + auto const iter = items_.lower_bound(sle->key()); + if (iter == items_.end() || iter->first != sle->key()) { using namespace std; - items_.emplace_hint(iter, + items_.emplace_hint( + iter, piecewise_construct, - forward_as_tuple(sle->key()), - forward_as_tuple(Action::insert, sle)); + forward_as_tuple(sle->key()), + forward_as_tuple(Action::insert, sle)); return; } auto& item = iter->second; - switch(item.first) + switch (item.first) { - case Action::cache: - LogicError("ApplyStateTable::insert: already cached"); - case Action::insert: - LogicError("ApplyStateTable::insert: already inserted"); - case Action::modify: - LogicError("ApplyStateTable::insert: already modified"); - case Action::erase: - break; + case Action::cache: + LogicError("ApplyStateTable::insert: already cached"); + case Action::insert: + LogicError("ApplyStateTable::insert: already inserted"); + case Action::modify: + LogicError("ApplyStateTable::insert: already modified"); + case Action::erase: + break; } item.first = Action::modify; item.second = sle; } void -ApplyStateTable::replace (ReadView const& base, - std::shared_ptr const& sle) +ApplyStateTable::replace(ReadView const& base, std::shared_ptr const& sle) { - auto const iter = - items_.lower_bound(sle->key()); - if (iter == items_.end() || - iter->first != sle->key()) + auto const iter = items_.lower_bound(sle->key()); + if (iter == items_.end() || iter->first != sle->key()) { using namespace std; - items_.emplace_hint(iter, piecewise_construct, + items_.emplace_hint( + iter, + piecewise_construct, forward_as_tuple(sle->key()), - forward_as_tuple(Action::modify, sle)); + forward_as_tuple(Action::modify, sle)); return; } auto& item = iter->second; switch (item.first) { - case Action::erase: - LogicError("ApplyStateTable::replace: already erased"); - case Action::cache: - item.first = Action::modify; - break; - case Action::insert: - case Action::modify: - break; + case Action::erase: + LogicError("ApplyStateTable::replace: already erased"); + case Action::cache: + item.first = Action::modify; + break; + case Action::insert: + case Action::modify: + break; } item.second = sle; } void -ApplyStateTable::update (ReadView const& base, - std::shared_ptr const& sle) +ApplyStateTable::update(ReadView const& base, std::shared_ptr const& sle) { - auto const iter = - items_.find(sle->key()); + auto const iter = items_.find(sle->key()); if (iter == items_.end()) LogicError("ApplyStateTable::update: missing key"); auto& item = iter->second; @@ -507,15 +500,15 @@ ApplyStateTable::update (ReadView const& base, LogicError("ApplyStateTable::update: unknown SLE"); switch (item.first) { - case Action::erase: - LogicError("ApplyStateTable::update: erased"); - break; - case Action::cache: - item.first = Action::modify; - break; - case Action::insert: - case Action::modify: - break; + case Action::erase: + LogicError("ApplyStateTable::update: erased"); + break; + case Action::cache: + item.first = Action::modify; + break; + case Action::insert: + case Action::modify: + break; }; } @@ -529,35 +522,36 @@ ApplyStateTable::destroyXRP(XRPAmount const& fee) // Insert this transaction to the SLE's threading list void -ApplyStateTable::threadItem (TxMeta& meta, - std::shared_ptr const& sle) +ApplyStateTable::threadItem(TxMeta& meta, std::shared_ptr const& sle) { key_type prevTxID; LedgerIndex prevLgrID; - if (! sle->thread(meta.getTxID(), - meta.getLgrSeq(), prevTxID, prevLgrID)) + if (!sle->thread(meta.getTxID(), meta.getLgrSeq(), prevTxID, prevLgrID)) return; if (prevTxID.isZero()) return; - TxMeta::thread(meta.getAffectedNode( - sle, sfModifiedNode), prevTxID, prevLgrID); + TxMeta::thread( + meta.getAffectedNode(sle, sfModifiedNode), prevTxID, prevLgrID); } std::shared_ptr -ApplyStateTable::getForMod (ReadView const& base, - key_type const& key, Mods& mods, beast::Journal j) +ApplyStateTable::getForMod( + ReadView const& base, + key_type const& key, + Mods& mods, + beast::Journal j) { { - auto miter = mods.find (key); - if (miter != mods.end ()) + auto miter = mods.find(key); + if (miter != mods.end()) { - assert (miter->second); + assert(miter->second); return miter->second; } } { - auto iter = items_.find (key); - if (iter != items_.end ()) + auto iter = items_.find(key); + if (iter != items_.end()) { auto const& item = iter->second; if (item.first == Action::erase) @@ -575,8 +569,8 @@ ApplyStateTable::getForMod (ReadView const& base, // metadata; fall through and track it in the mods table. } } - auto c = base.read (keylet::unchecked (key)); - if (! c) + auto c = base.read(keylet::unchecked(key)); + if (!c) { // The Destination of an Escrow or a PayChannel may have been // deleted. In that case the account we're threading to will @@ -584,19 +578,21 @@ ApplyStateTable::getForMod (ReadView const& base, JLOG(j.warn()) << "ApplyStateTable::getForMod: key not found"; return nullptr; } - auto sle = std::make_shared (*c); + auto sle = std::make_shared(*c); mods.emplace(key, sle); return sle; } void -ApplyStateTable::threadTx (ReadView const& base, - TxMeta& meta, AccountID const& to, - Mods& mods, beast::Journal j) +ApplyStateTable::threadTx( + ReadView const& base, + TxMeta& meta, + AccountID const& to, + Mods& mods, + beast::Journal j) { - auto const sle = getForMod(base, - keylet::account(to).key, mods, j); - if (! sle) + auto const sle = getForMod(base, keylet::account(to).key, mods, j); + if (!sle) { // The Destination of an Escrow or PayChannel may have been deleted. // In that case the account we are threading to will not be found. @@ -604,45 +600,46 @@ ApplyStateTable::threadTx (ReadView const& base, JLOG(j.warn()) << "Threading to non-existent account: " << toBase58(to); return; } - threadItem (meta, sle); + threadItem(meta, sle); } void -ApplyStateTable::threadOwners (ReadView const& base, - TxMeta& meta, std::shared_ptr< - SLE const> const& sle, Mods& mods, - beast::Journal j) +ApplyStateTable::threadOwners( + ReadView const& base, + TxMeta& meta, + std::shared_ptr const& sle, + Mods& mods, + beast::Journal j) { - LedgerEntryType const ledgerType {sle->getType()}; - switch(ledgerType) + LedgerEntryType const ledgerType{sle->getType()}; + switch (ledgerType) { - case ltACCOUNT_ROOT: - { - // Nothing to do - break; - } - case ltRIPPLE_STATE: - { - threadTx (base, meta, (*sle)[sfLowLimit].getIssuer(), mods, j); - threadTx (base, meta, (*sle)[sfHighLimit].getIssuer(), mods, j); - break; - } - default: - { - // If sfAccount is present, thread to that account - if (auto const optSleAcct {(*sle)[~sfAccount]}) - threadTx (base, meta, *optSleAcct, mods, j); - - // Don't thread a check's sfDestination unless the amendment is enabled - if (ledgerType == ltCHECK && !base.rules().enabled(fixCheckThreading)) + case ltACCOUNT_ROOT: { + // Nothing to do break; + } + case ltRIPPLE_STATE: { + threadTx(base, meta, (*sle)[sfLowLimit].getIssuer(), mods, j); + threadTx(base, meta, (*sle)[sfHighLimit].getIssuer(), mods, j); + break; + } + default: { + // If sfAccount is present, thread to that account + if (auto const optSleAcct{(*sle)[~sfAccount]}) + threadTx(base, meta, *optSleAcct, mods, j); - // If sfDestination is present, thread to that account - if (auto const optSleDest {(*sle)[~sfDestination]}) - threadTx (base, meta, *optSleDest, mods, j); - } + // Don't thread a check's sfDestination unless the amendment is + // enabled + if (ledgerType == ltCHECK && + !base.rules().enabled(fixCheckThreading)) + break; + + // If sfDestination is present, thread to that account + if (auto const optSleDest{(*sle)[~sfDestination]}) + threadTx(base, meta, *optSleDest, mods, j); + } } } -} // detail -} // ripple +} // namespace detail +} // namespace ripple diff --git a/src/ripple/ledger/impl/ApplyView.cpp b/src/ripple/ledger/impl/ApplyView.cpp index 467add5e46..cccec9421a 100644 --- a/src/ripple/ledger/impl/ApplyView.cpp +++ b/src/ripple/ledger/impl/ApplyView.cpp @@ -17,15 +17,15 @@ */ //============================================================================== -#include #include +#include #include #include namespace ripple { boost::optional -ApplyView::dirAdd ( +ApplyView::dirAdd( bool preserveOrder, Keylet const& directory, uint256 const& key, @@ -33,18 +33,18 @@ ApplyView::dirAdd ( { auto root = peek(directory); - if (! root) + if (!root) { // No root, make it. root = std::make_shared(directory); - root->setFieldH256 (sfRootIndex, directory.key); - describe (root); + root->setFieldH256(sfRootIndex, directory.key); + describe(root); STVector256 v; - v.push_back (key); - root->setFieldV256 (sfIndexes, v); + v.push_back(key); + root->setFieldV256(sfIndexes, v); - insert (root); + insert(root); return std::uint64_t{0}; } @@ -54,20 +54,20 @@ ApplyView::dirAdd ( if (page) { - node = peek (keylet::page(directory, page)); + node = peek(keylet::page(directory, page)); if (!node) - LogicError ("Directory chain: root back-pointer broken."); + LogicError("Directory chain: root back-pointer broken."); } auto indexes = node->getFieldV256(sfIndexes); // If there's space, we use it: - if (indexes.size () < dirNodeMaxEntries) + if (indexes.size() < dirNodeMaxEntries) { if (preserveOrder) { if (std::find(indexes.begin(), indexes.end(), key) != indexes.end()) - LogicError ("dirInsert: double insertion"); + LogicError("dirInsert: double insertion"); indexes.push_back(key); } @@ -76,17 +76,17 @@ ApplyView::dirAdd ( // We can't be sure if this page is already sorted because // it may be a legacy page we haven't yet touched. Take // the time to sort it. - std::sort (indexes.begin(), indexes.end()); + std::sort(indexes.begin(), indexes.end()); auto pos = std::lower_bound(indexes.begin(), indexes.end(), key); if (pos != indexes.end() && key == *pos) - LogicError ("dirInsert: double insertion"); + LogicError("dirInsert: double insertion"); - indexes.insert (pos, key); + indexes.insert(pos, key); } - node->setFieldV256 (sfIndexes, indexes); + node->setFieldV256(sfIndexes, indexes); update(node); return page; } @@ -97,26 +97,26 @@ ApplyView::dirAdd ( // We are about to create a new node; we'll link it to // the chain first: - node->setFieldU64 (sfIndexNext, page); + node->setFieldU64(sfIndexNext, page); update(node); - root->setFieldU64 (sfIndexPrevious, page); + root->setFieldU64(sfIndexPrevious, page); update(root); // Insert the new key: indexes.clear(); - indexes.push_back (key); + indexes.push_back(key); node = std::make_shared(keylet::page(directory, page)); - node->setFieldH256 (sfRootIndex, directory.key); - node->setFieldV256 (sfIndexes, indexes); + node->setFieldH256(sfRootIndex, directory.key); + node->setFieldV256(sfIndexes, indexes); // Save some space by not specifying the value 0 since // it's the default. if (page != 1) - node->setFieldU64 (sfIndexPrevious, page - 1); - describe (node); - insert (node); + node->setFieldU64(sfIndexPrevious, page - 1); + describe(node); + insert(node); return page; } @@ -133,7 +133,7 @@ ApplyView::emptyDirDelete(Keylet const& directory) if (directory.type != ltDIR_NODE || node->getFieldH256(sfRootIndex) != directory.key) { - assert (!"emptyDirDelete() called with wrong node type"); + assert(!"emptyDirDelete() called with wrong node type"); return false; } @@ -146,10 +146,10 @@ ApplyView::emptyDirDelete(Keylet const& directory) auto nextPage = node->getFieldU64(sfIndexNext); if (nextPage == rootPage && prevPage != rootPage) - LogicError ("Directory chain: fwd link broken"); + LogicError("Directory chain: fwd link broken"); if (prevPage == rootPage && nextPage != rootPage) - LogicError ("Directory chain: rev link broken"); + LogicError("Directory chain: rev link broken"); // Older versions of the code would, in some cases, allow the last // page to be empty. Remove such pages: @@ -158,15 +158,15 @@ ApplyView::emptyDirDelete(Keylet const& directory) auto last = peek(keylet::page(directory, nextPage)); if (!last) - LogicError ("Directory chain: fwd link broken."); + LogicError("Directory chain: fwd link broken."); - if (!last->getFieldV256 (sfIndexes).empty()) + if (!last->getFieldV256(sfIndexes).empty()) return false; // Update the first page's linked list and // mark it as updated. - node->setFieldU64 (sfIndexNext, rootPage); - node->setFieldU64 (sfIndexPrevious, rootPage); + node->setFieldU64(sfIndexNext, rootPage); + node->setFieldU64(sfIndexPrevious, rootPage); update(node); // And erase the empty last page: @@ -186,7 +186,7 @@ ApplyView::emptyDirDelete(Keylet const& directory) } bool -ApplyView::dirRemove ( +ApplyView::dirRemove( Keylet const& directory, std::uint64_t page, uint256 const& key, @@ -204,7 +204,7 @@ ApplyView::dirRemove ( auto it = std::find(entries.begin(), entries.end(), key); - if (entries.end () == it) + if (entries.end() == it) return false; // We always preserve the relative order when we remove. @@ -230,10 +230,10 @@ ApplyView::dirRemove ( if (page == rootPage) { if (nextPage == page && prevPage != page) - LogicError ("Directory chain: fwd link broken"); + LogicError("Directory chain: fwd link broken"); if (prevPage == page && nextPage != page) - LogicError ("Directory chain: rev link broken"); + LogicError("Directory chain: rev link broken"); // Older versions of the code would, in some cases, // allow the last page to be empty. Remove such @@ -242,14 +242,14 @@ ApplyView::dirRemove ( { auto last = peek(keylet::page(directory, nextPage)); if (!last) - LogicError ("Directory chain: fwd link broken."); + LogicError("Directory chain: fwd link broken."); - if (last->getFieldV256 (sfIndexes).empty()) + if (last->getFieldV256(sfIndexes).empty()) { // Update the first page's linked list and // mark it as updated. - node->setFieldU64 (sfIndexNext, page); - node->setFieldU64 (sfIndexPrevious, page); + node->setFieldU64(sfIndexNext, page); + node->setFieldU64(sfIndexPrevious, page); update(node); // And erase the empty last page: @@ -274,10 +274,10 @@ ApplyView::dirRemove ( // This can never happen for nodes other than the root: if (nextPage == page) - LogicError ("Directory chain: fwd link broken"); + LogicError("Directory chain: fwd link broken"); if (prevPage == page) - LogicError ("Directory chain: rev link broken"); + LogicError("Directory chain: rev link broken"); // This node isn't the root, so it can either be in the // middle of the list, or at the end. Unlink it first @@ -285,14 +285,14 @@ ApplyView::dirRemove ( // root: auto prev = peek(keylet::page(directory, prevPage)); if (!prev) - LogicError ("Directory chain: fwd link broken."); + LogicError("Directory chain: fwd link broken."); // Fix previous to point to its new next. prev->setFieldU64(sfIndexNext, nextPage); - update (prev); + update(prev); auto next = peek(keylet::page(directory, nextPage)); if (!next) - LogicError ("Directory chain: rev link broken."); + LogicError("Directory chain: rev link broken."); // Fix next to point to its new previous. next->setFieldU64(sfIndexPrevious, prevPage); update(next); @@ -302,9 +302,8 @@ ApplyView::dirRemove ( // Check whether the next page is the last page and, if // so, whether it's empty. If it is, delete it. - if (nextPage != rootPage && - next->getFieldU64 (sfIndexNext) == rootPage && - next->getFieldV256 (sfIndexes).empty()) + if (nextPage != rootPage && next->getFieldU64(sfIndexNext) == rootPage && + next->getFieldV256(sfIndexes).empty()) { // Since next doesn't point to the root, it // can't be pointing to prev. @@ -312,14 +311,14 @@ ApplyView::dirRemove ( // The previous page is now the last page: prev->setFieldU64(sfIndexNext, rootPage); - update (prev); + update(prev); // And the root points to the the last page: auto root = peek(keylet::page(directory, rootPage)); if (!root) - LogicError ("Directory chain: root link broken."); + LogicError("Directory chain: root link broken."); root->setFieldU64(sfIndexPrevious, prevPage); - update (root); + update(root); nextPage = rootPage; } @@ -328,11 +327,11 @@ ApplyView::dirRemove ( // it's left empty. If so, delete it as well. if (!keepRoot && nextPage == rootPage && prevPage == rootPage) { - if (prev->getFieldV256 (sfIndexes).empty()) + if (prev->getFieldV256(sfIndexes).empty()) erase(prev); } return true; } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/ApplyViewBase.cpp b/src/ripple/ledger/impl/ApplyViewBase.cpp index f5c119ecac..139a8c90a2 100644 --- a/src/ripple/ledger/impl/ApplyViewBase.cpp +++ b/src/ripple/ledger/impl/ApplyViewBase.cpp @@ -17,17 +17,15 @@ */ //============================================================================== -#include #include #include +#include namespace ripple { namespace detail { -ApplyViewBase::ApplyViewBase( - ReadView const* base, ApplyFlags flags) - : flags_ (flags) - , base_ (base) +ApplyViewBase::ApplyViewBase(ReadView const* base, ApplyFlags flags) + : flags_(flags), base_(base) { } @@ -58,70 +56,63 @@ ApplyViewBase::rules() const } bool -ApplyViewBase::exists (Keylet const& k) const +ApplyViewBase::exists(Keylet const& k) const { return items_.exists(*base_, k); } auto -ApplyViewBase::succ (key_type const& key, - boost::optional const& last) const -> - boost::optional +ApplyViewBase::succ(key_type const& key, boost::optional const& last) + const -> boost::optional { return items_.succ(*base_, key, last); } std::shared_ptr -ApplyViewBase::read (Keylet const& k) const +ApplyViewBase::read(Keylet const& k) const { return items_.read(*base_, k); } auto -ApplyViewBase::slesBegin() const -> - std::unique_ptr +ApplyViewBase::slesBegin() const -> std::unique_ptr { return base_->slesBegin(); } auto -ApplyViewBase::slesEnd() const -> - std::unique_ptr +ApplyViewBase::slesEnd() const -> std::unique_ptr { return base_->slesEnd(); } auto -ApplyViewBase::slesUpperBound(uint256 const& key) const -> - std::unique_ptr +ApplyViewBase::slesUpperBound(uint256 const& key) const + -> std::unique_ptr { return base_->slesUpperBound(key); } auto -ApplyViewBase::txsBegin() const -> - std::unique_ptr +ApplyViewBase::txsBegin() const -> std::unique_ptr { return base_->txsBegin(); } auto -ApplyViewBase::txsEnd() const -> - std::unique_ptr +ApplyViewBase::txsEnd() const -> std::unique_ptr { return base_->txsEnd(); } bool -ApplyViewBase::txExists (key_type const& key) const +ApplyViewBase::txExists(key_type const& key) const { return base_->txExists(key); } auto -ApplyViewBase::txRead( - key_type const& key) const -> - tx_type +ApplyViewBase::txRead(key_type const& key) const -> tx_type { return base_->txRead(key); } @@ -135,28 +126,25 @@ ApplyViewBase::flags() const } std::shared_ptr -ApplyViewBase::peek (Keylet const& k) +ApplyViewBase::peek(Keylet const& k) { return items_.peek(*base_, k); } void -ApplyViewBase::erase( - std::shared_ptr const& sle) +ApplyViewBase::erase(std::shared_ptr const& sle) { items_.erase(*base_, sle); } void -ApplyViewBase::insert( - std::shared_ptr const& sle) +ApplyViewBase::insert(std::shared_ptr const& sle) { items_.insert(*base_, sle); } void -ApplyViewBase::update( - std::shared_ptr const& sle) +ApplyViewBase::update(std::shared_ptr const& sle) { items_.update(*base_, sle); } @@ -164,43 +152,41 @@ ApplyViewBase::update( //--- void -ApplyViewBase::rawErase( - std::shared_ptr const& sle) +ApplyViewBase::rawErase(std::shared_ptr const& sle) { items_.rawErase(*base_, sle); } void -ApplyViewBase::rawInsert( - std::shared_ptr const& sle) +ApplyViewBase::rawInsert(std::shared_ptr const& sle) { items_.insert(*base_, sle); } void -ApplyViewBase::rawReplace( - std::shared_ptr const& sle) +ApplyViewBase::rawReplace(std::shared_ptr const& sle) { items_.replace(*base_, sle); } void -ApplyViewBase::rawDestroyXRP( - XRPAmount const& fee) +ApplyViewBase::rawDestroyXRP(XRPAmount const& fee) { items_.destroyXRP(fee); } //--- -CashDiff cashFlowDiff ( - CashFilter lhsFilter, ApplyViewBase const& lhs, - CashFilter rhsFilter, ApplyViewBase const& rhs) +CashDiff +cashFlowDiff( + CashFilter lhsFilter, + ApplyViewBase const& lhs, + CashFilter rhsFilter, + ApplyViewBase const& rhs) { - assert (lhs.base_ == rhs.base_); - return CashDiff { - *lhs.base_, lhsFilter, lhs.items_, rhsFilter, rhs.items_ }; + assert(lhs.base_ == rhs.base_); + return CashDiff{*lhs.base_, lhsFilter, lhs.items_, rhsFilter, rhs.items_}; } -} // detail -} // ripple +} // namespace detail +} // namespace ripple diff --git a/src/ripple/ledger/impl/ApplyViewImpl.cpp b/src/ripple/ledger/impl/ApplyViewImpl.cpp index 3ff7b4237d..155ff4df99 100644 --- a/src/ripple/ledger/impl/ApplyViewImpl.cpp +++ b/src/ripple/ledger/impl/ApplyViewImpl.cpp @@ -17,42 +17,39 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { -ApplyViewImpl::ApplyViewImpl( - ReadView const* base, ApplyFlags flags) - : ApplyViewBase (base, flags) +ApplyViewImpl::ApplyViewImpl(ReadView const* base, ApplyFlags flags) + : ApplyViewBase(base, flags) { } void -ApplyViewImpl::apply (OpenView& to, - STTx const& tx, TER ter, - beast::Journal j) +ApplyViewImpl::apply(OpenView& to, STTx const& tx, TER ter, beast::Journal j) { items_.apply(to, tx, ter, deliver_, j); } std::size_t -ApplyViewImpl::size () +ApplyViewImpl::size() { - return items_.size (); + return items_.size(); } void -ApplyViewImpl::visit ( +ApplyViewImpl::visit( OpenView& to, - std::function const& before, - std::shared_ptr const& after)> const& func) + std::shared_ptr const& before, + std::shared_ptr const& after)> const& func) { - items_.visit (to, func); + items_.visit(to, func); } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/BookDirs.cpp b/src/ripple/ledger/impl/BookDirs.cpp index 0ae552748b..ddda64dd1d 100644 --- a/src/ripple/ledger/impl/BookDirs.cpp +++ b/src/ripple/ledger/impl/BookDirs.cpp @@ -1,4 +1,5 @@ -//------------ ------------------------------------------------------------------ +//------------ +//------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2015 Ripple Labs Inc. @@ -32,8 +33,13 @@ BookDirs::BookDirs(ReadView const& view, Book const& book) assert(root_ != beast::zero); if (key_ != beast::zero) { - if (! cdirFirst(*view_, key_, sle_, entry_, index_, - beast::Journal {beast::Journal::getNullSink()})) + if (!cdirFirst( + *view_, + key_, + sle_, + entry_, + index_, + beast::Journal{beast::Journal::getNullSink()})) { assert(false); } @@ -41,8 +47,7 @@ BookDirs::BookDirs(ReadView const& view, Book const& book) } auto -BookDirs::begin() const -> - BookDirs::const_iterator +BookDirs::begin() const -> BookDirs::const_iterator { auto it = BookDirs::const_iterator(*view_, root_, key_); if (key_ != beast::zero) @@ -56,34 +61,31 @@ BookDirs::begin() const -> } auto -BookDirs::end() const -> - BookDirs::const_iterator +BookDirs::end() const -> BookDirs::const_iterator { return BookDirs::const_iterator(*view_, root_, key_); } - beast::Journal BookDirs::const_iterator::j_ = beast::Journal{beast::Journal::getNullSink()}; bool -BookDirs::const_iterator::operator== - (BookDirs::const_iterator const& other) const +BookDirs::const_iterator::operator==( + BookDirs::const_iterator const& other) const { if (view_ == nullptr || other.view_ == nullptr) return false; assert(view_ == other.view_ && root_ == other.root_); - return entry_ == other.entry_ && - cur_key_ == other.cur_key_ && - index_ == other.index_; + return entry_ == other.entry_ && cur_key_ == other.cur_key_ && + index_ == other.index_; } BookDirs::const_iterator::reference BookDirs::const_iterator::operator*() const { assert(index_ != beast::zero); - if (! cache_) + if (!cache_) cache_ = view_->read(keylet::offer(index_)); return *cache_; } @@ -94,18 +96,17 @@ BookDirs::const_iterator::operator++() using beast::zero; assert(index_ != zero); - if (! cdirNext(*view_, cur_key_, sle_, entry_, index_, j_)) + if (!cdirNext(*view_, cur_key_, sle_, entry_, index_, j_)) { if (index_ != 0 || - (cur_key_ = view_->succ(++cur_key_, - next_quality_).value_or(zero)) == zero) + (cur_key_ = + view_->succ(++cur_key_, next_quality_).value_or(zero)) == zero) { cur_key_ = key_; entry_ = 0; index_ = zero; } - else if (! cdirFirst(*view_, cur_key_, - sle_, entry_, index_, j_)) + else if (!cdirFirst(*view_, cur_key_, sle_, entry_, index_, j_)) { assert(false); } @@ -124,4 +125,4 @@ BookDirs::const_iterator::operator++(int) return tmp; } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/CachedSLEs.cpp b/src/ripple/ledger/impl/CachedSLEs.cpp index 2789d8c68e..3d00aeaf13 100644 --- a/src/ripple/ledger/impl/CachedSLEs.cpp +++ b/src/ripple/ledger/impl/CachedSLEs.cpp @@ -25,21 +25,19 @@ namespace ripple { void CachedSLEs::expire() { - std::vector< - std::shared_ptr> trash; + std::vector> trash; { - auto const expireTime = - map_.clock().now() - timeToLive_; + auto const expireTime = map_.clock().now() - timeToLive_; std::lock_guard lock(mutex_); for (auto iter = map_.chronological.begin(); - iter != map_.chronological.end(); ++iter) + iter != map_.chronological.end(); + ++iter) { if (iter.when() > expireTime) break; if (iter->second.unique()) { - trash.emplace_back( - std::move(iter->second)); + trash.emplace_back(std::move(iter->second)); iter = map_.erase(iter); } } @@ -56,4 +54,4 @@ CachedSLEs::rate() const return double(hit_) / tot; } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/CachedView.cpp b/src/ripple/ledger/impl/CachedView.cpp index 3d5c721375..3ad8a812b0 100644 --- a/src/ripple/ledger/impl/CachedView.cpp +++ b/src/ripple/ledger/impl/CachedView.cpp @@ -17,15 +17,15 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { namespace detail { bool -CachedViewImpl::exists (Keylet const& k) const +CachedViewImpl::exists(Keylet const& k) const { return read(k) != nullptr; } @@ -55,9 +55,9 @@ CachedViewImpl::read(Keylet const& k) const { if (!inserted) { - // On entry, this function did not find this key in map_. Now something - // (another thread?) has inserted the sle into the map and it has - // the wrong type. + // On entry, this function did not find this key in map_. Now + // something (another thread?) has inserted the sle into the map and + // it has the wrong type. LogicError("CachedView::read: wrong type"); } return nullptr; @@ -65,5 +65,5 @@ CachedViewImpl::read(Keylet const& k) const return iter->second; } -} // detail -} // ripple +} // namespace detail +} // namespace ripple diff --git a/src/ripple/ledger/impl/CashDiff.cpp b/src/ripple/ledger/impl/CashDiff.cpp index eb651c12e7..ed0fed00de 100644 --- a/src/ripple/ledger/impl/CashDiff.cpp +++ b/src/ripple/ledger/impl/CashDiff.cpp @@ -22,7 +22,7 @@ #include #include #include -#include // std::abs() +#include // std::abs() namespace ripple { namespace detail { @@ -33,43 +33,43 @@ struct CashSummary explicit CashSummary() = default; // Sorted vectors. All of the vectors fill in for std::maps. - std::vector> xrpChanges; + std::vector> xrpChanges; - std::vector, STAmount>> trustChanges; + std::vector, STAmount>> + trustChanges; - std::vector, bool>> trustDeletions; + std::vector, bool>> + trustDeletions; - std::vector, - CashDiff::OfferAmounts>> offerChanges; + std::vector< + std::pair, CashDiff::OfferAmounts>> + offerChanges; // Note that the OfferAmounts hold the amount *prior* to deletion. - std::vector, - CashDiff::OfferAmounts>> offerDeletions; + std::vector< + std::pair, CashDiff::OfferAmounts>> + offerDeletions; - bool hasDiff () const + bool + hasDiff() const { - return !xrpChanges.empty() - || !trustChanges.empty() - || !trustDeletions.empty() - || !offerChanges.empty() - || !offerDeletions.empty(); + return !xrpChanges.empty() || !trustChanges.empty() || + !trustDeletions.empty() || !offerChanges.empty() || + !offerDeletions.empty(); } - void reserve (size_t newCap) + void + reserve(size_t newCap) { - xrpChanges.reserve (newCap); - trustChanges.reserve (newCap); - trustDeletions.reserve (newCap); - offerChanges.reserve (newCap); - offerDeletions.reserve (newCap); + xrpChanges.reserve(newCap); + trustChanges.reserve(newCap); + trustDeletions.reserve(newCap); + offerChanges.reserve(newCap); + offerDeletions.reserve(newCap); } - void shrink_to_fit() + void + shrink_to_fit() { xrpChanges.shrink_to_fit(); trustChanges.shrink_to_fit(); @@ -78,13 +78,14 @@ struct CashSummary offerDeletions.shrink_to_fit(); } - void sort() + void + sort() { - std::sort (xrpChanges.begin(), xrpChanges.end()); - std::sort (trustChanges.begin(), trustChanges.end()); - std::sort (trustDeletions.begin(), trustDeletions.end()); - std::sort (offerChanges.begin(), offerChanges.end()); - std::sort (offerDeletions.begin(), offerDeletions.end()); + std::sort(xrpChanges.begin(), xrpChanges.end()); + std::sort(trustChanges.begin(), trustChanges.end()); + std::sort(trustDeletions.begin(), trustDeletions.end()); + std::sort(offerChanges.begin(), offerChanges.end()); + std::sort(offerDeletions.begin(), offerDeletions.end()); } }; @@ -105,7 +106,10 @@ struct CashSummary // // The returned bool indicates whether the passed in data was handled. // This allows the caller to avoid further handling. -static bool treatZeroOfferAsDeletion (CashSummary& result, bool isDelete, +static bool +treatZeroOfferAsDeletion( + CashSummary& result, + bool isDelete, std::shared_ptr const& before, std::shared_ptr const& after) { @@ -118,8 +122,8 @@ static bool treatZeroOfferAsDeletion (CashSummary& result, bool isDelete, if (isDelete) { - if (prev.getType() == ltOFFER && - prev[sfTakerPays] == zero && prev[sfTakerGets] == zero) + if (prev.getType() == ltOFFER && prev[sfTakerPays] == zero && + prev[sfTakerGets] == zero) { // Offer was previously treated as deleted when it was zeroed. return true; @@ -132,18 +136,17 @@ static bool treatZeroOfferAsDeletion (CashSummary& result, bool isDelete, return false; auto const& cur = *after; - if (cur.getType() == ltOFFER && - cur[sfTakerPays] == zero && cur[sfTakerGets] == zero) + if (cur.getType() == ltOFFER && cur[sfTakerPays] == zero && + cur[sfTakerGets] == zero) { // Either ignore or treat as delete. auto const oldTakerPays = prev[sfTakerPays]; auto const oldTakerGets = prev[sfTakerGets]; if (oldTakerPays != zero && oldTakerGets != zero) { - result.offerDeletions.push_back ( - std::make_pair ( - std::make_tuple (prev[sfAccount], prev[sfSequence]), - CashDiff::OfferAmounts {{oldTakerPays, oldTakerGets}})); + result.offerDeletions.push_back(std::make_pair( + std::make_tuple(prev[sfAccount], prev[sfSequence]), + CashDiff::OfferAmounts{{oldTakerPays, oldTakerGets}})); return true; } } @@ -151,7 +154,10 @@ static bool treatZeroOfferAsDeletion (CashSummary& result, bool isDelete, return false; } -static bool getBasicCashFlow (CashSummary& result, bool isDelete, +static bool +getBasicCashFlow( + CashSummary& result, + bool isDelete, std::shared_ptr const& before, std::shared_ptr const& after) { @@ -161,33 +167,31 @@ static bool getBasicCashFlow (CashSummary& result, bool isDelete, return false; auto const& prev = *before; - switch(prev.getType()) + switch (prev.getType()) { - case ltACCOUNT_ROOT: - result.xrpChanges.push_back ( - std::make_pair (prev[sfAccount], XRPAmount {0})); - return true; + case ltACCOUNT_ROOT: + result.xrpChanges.push_back( + std::make_pair(prev[sfAccount], XRPAmount{0})); + return true; - case ltRIPPLE_STATE: - result.trustDeletions.push_back( - std::make_pair ( - std::make_tuple ( + case ltRIPPLE_STATE: + result.trustDeletions.push_back(std::make_pair( + std::make_tuple( prev[sfLowLimit].getIssuer(), prev[sfHighLimit].getIssuer(), - prev[sfBalance].getCurrency()), false)); - return true; + prev[sfBalance].getCurrency()), + false)); + return true; - case ltOFFER: - result.offerDeletions.push_back ( - std::make_pair ( - std::make_tuple (prev[sfAccount], prev[sfSequence]), - CashDiff::OfferAmounts {{ - prev[sfTakerPays], - prev[sfTakerGets]}})); - return true; + case ltOFFER: + result.offerDeletions.push_back(std::make_pair( + std::make_tuple(prev[sfAccount], prev[sfSequence]), + CashDiff::OfferAmounts{ + {prev[sfTakerPays], prev[sfTakerGets]}})); + return true; - default: - return false; + default: + return false; } } else @@ -195,50 +199,45 @@ static bool getBasicCashFlow (CashSummary& result, bool isDelete, // insert or modify if (!after) { - assert (after); + assert(after); return false; } auto const& cur = *after; - switch(cur.getType()) + switch (cur.getType()) { - case ltACCOUNT_ROOT: - { - auto const curXrp = cur[sfBalance].xrp(); - if (!before || (*before)[sfBalance].xrp() != curXrp) - result.xrpChanges.push_back ( - std::make_pair (cur[sfAccount], curXrp)); - return true; - } - case ltRIPPLE_STATE: - { - auto const curBalance = cur[sfBalance]; - if (!before || (*before)[sfBalance] != curBalance) - result.trustChanges.push_back ( - std::make_pair ( - std::make_tuple ( + case ltACCOUNT_ROOT: { + auto const curXrp = cur[sfBalance].xrp(); + if (!before || (*before)[sfBalance].xrp() != curXrp) + result.xrpChanges.push_back( + std::make_pair(cur[sfAccount], curXrp)); + return true; + } + case ltRIPPLE_STATE: { + auto const curBalance = cur[sfBalance]; + if (!before || (*before)[sfBalance] != curBalance) + result.trustChanges.push_back(std::make_pair( + std::make_tuple( cur[sfLowLimit].getIssuer(), cur[sfHighLimit].getIssuer(), curBalance.getCurrency()), curBalance)); - return true; - } - case ltOFFER: - { - auto const curTakerPays = cur[sfTakerPays]; - auto const curTakerGets = cur[sfTakerGets]; - if (!before || (*before)[sfTakerGets] != curTakerGets || - (*before)[sfTakerPays] != curTakerPays) - { - result.offerChanges.push_back ( - std::make_pair ( - std::make_tuple (cur[sfAccount], cur[sfSequence]), - CashDiff::OfferAmounts {{curTakerPays, curTakerGets}})); + return true; } - return true; - } - default: - break; + case ltOFFER: { + auto const curTakerPays = cur[sfTakerPays]; + auto const curTakerGets = cur[sfTakerGets]; + if (!before || (*before)[sfTakerGets] != curTakerGets || + (*before)[sfTakerPays] != curTakerPays) + { + result.offerChanges.push_back(std::make_pair( + std::make_tuple(cur[sfAccount], cur[sfSequence]), + CashDiff::OfferAmounts{{curTakerPays, curTakerGets}})); + } + return true; + } + default: + break; } } return false; @@ -246,38 +245,41 @@ static bool getBasicCashFlow (CashSummary& result, bool isDelete, // Extract the final cash state from an ApplyStateTable. static CashSummary -getCashFlow (ReadView const& view, CashFilter f, ApplyStateTable const& table) +getCashFlow(ReadView const& view, CashFilter f, ApplyStateTable const& table) { CashSummary result; - result.reserve (table.size()); + result.reserve(table.size()); // Make a container of filters based on the passed in filter flags. - using FuncType = decltype (&getBasicCashFlow); + using FuncType = decltype(&getBasicCashFlow); boost::container::static_vector filters; if ((f & CashFilter::treatZeroOfferAsDeletion) != CashFilter::none) - filters.push_back (treatZeroOfferAsDeletion); + filters.push_back(treatZeroOfferAsDeletion); - filters.push_back (&getBasicCashFlow); + filters.push_back(&getBasicCashFlow); - auto each = [&result, &filters](uint256 const& key, bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) { - auto discarded = - std::find_if (filters.begin(), filters.end(), - [&result, isDelete, &before, &after] (FuncType func) { - return func (result, isDelete, before, after); - }); - (void) discarded; + auto each = [&result, &filters]( + uint256 const& key, + bool isDelete, + std::shared_ptr const& before, + std::shared_ptr const& after) { + auto discarded = std::find_if( + filters.begin(), + filters.end(), + [&result, isDelete, &before, &after](FuncType func) { + return func(result, isDelete, before, after); + }); + (void)discarded; }; - table.visit (view, each); + table.visit(view, each); result.sort(); result.shrink_to_fit(); return result; } -} // detail +} // namespace detail //------------------------------------------------------------------------------ @@ -303,50 +305,63 @@ private: public: // Constructor. - Impl (ReadView const& view, - CashFilter lhsFilter, detail::ApplyStateTable const& lhs, - CashFilter rhsFilter, detail::ApplyStateTable const& rhs) - : view_ (view) + Impl( + ReadView const& view, + CashFilter lhsFilter, + detail::ApplyStateTable const& lhs, + CashFilter rhsFilter, + detail::ApplyStateTable const& rhs) + : view_(view) { - findDiffs (lhsFilter, lhs, rhsFilter, rhs); + findDiffs(lhsFilter, lhs, rhsFilter, rhs); } - std::size_t commonCount() const + std::size_t + commonCount() const { return commonKeys_; } - std::size_t lhsOnlyCount() const + std::size_t + lhsOnlyCount() const { return lhsKeys_; } - std::size_t rhsOnlyCount () const + std::size_t + rhsOnlyCount() const { return rhsKeys_; } - bool hasDiff () const + bool + hasDiff() const { - return dropsGone_ != boost::none - || lhsDiffs_.hasDiff() - || rhsDiffs_.hasDiff(); + return dropsGone_ != boost::none || lhsDiffs_.hasDiff() || + rhsDiffs_.hasDiff(); } - int xrpRoundToZero () const; + int + xrpRoundToZero() const; // Filter out differences that are small enough to be in the floating // point noise. - bool rmDust (); + bool + rmDust(); // Remove offer deletion differences from a given side - bool rmLhsDeletedOffers(); - bool rmRhsDeletedOffers(); + bool + rmLhsDeletedOffers(); + bool + rmRhsDeletedOffers(); private: - void findDiffs ( - CashFilter lhsFilter, detail::ApplyStateTable const& lhs, - CashFilter rhsFilter, detail::ApplyStateTable const& rhs); + void + findDiffs( + CashFilter lhsFilter, + detail::ApplyStateTable const& lhs, + CashFilter rhsFilter, + detail::ApplyStateTable const& rhs); }; // Template function to count difference types in individual CashDiff vectors. @@ -357,11 +372,12 @@ private: // [1] count of keys present in lhs only. // [2] count of keys present in rhs only. template -static std::array countKeys ( +static std::array +countKeys( std::vector> const& lhs, std::vector> const& rhs) { - std::array ret {}; // Zero initialize; + std::array ret{}; // Zero initialize; auto lhsItr = lhs.cbegin(); auto rhsItr = rhs.cbegin(); @@ -411,36 +427,40 @@ static std::array countKeys ( // [1] count of keys present in lhs only. // [2] count of keys present in rhs only. static std::array -countKeys (detail::CashSummary const& lhs, detail::CashSummary const& rhs) +countKeys(detail::CashSummary const& lhs, detail::CashSummary const& rhs) { - std::array ret {}; // Zero initialize; + std::array ret{}; // Zero initialize; // Lambda to add in new results. - auto addIn = [&ret] (std::array const& a) - { - std::transform (a.cbegin(), a.cend(), - ret.cbegin(), ret.begin(), std::plus()); + auto addIn = [&ret](std::array const& a) { + std::transform( + a.cbegin(), + a.cend(), + ret.cbegin(), + ret.begin(), + std::plus()); }; - addIn (countKeys(lhs.xrpChanges, rhs.xrpChanges)); - addIn (countKeys(lhs.trustChanges, rhs.trustChanges)); - addIn (countKeys(lhs.trustDeletions, rhs.trustDeletions)); - addIn (countKeys(lhs.offerChanges, rhs.offerChanges)); - addIn (countKeys(lhs.offerDeletions, rhs.offerDeletions)); + addIn(countKeys(lhs.xrpChanges, rhs.xrpChanges)); + addIn(countKeys(lhs.trustChanges, rhs.trustChanges)); + addIn(countKeys(lhs.trustDeletions, rhs.trustDeletions)); + addIn(countKeys(lhs.offerChanges, rhs.offerChanges)); + addIn(countKeys(lhs.offerDeletions, rhs.offerDeletions)); return ret; } -int CashDiff::Impl::xrpRoundToZero () const +int +CashDiff::Impl::xrpRoundToZero() const { // The case has one OfferChange that is present on both lhs_ and rhs_. // That OfferChange should have XRP for TakerGets. There should be a 1 // drop difference between the TakerGets of lhsDiffs_ and rhsDiffs_. if (lhsDiffs_.offerChanges.size() != 1 || rhsDiffs_.offerChanges.size() != 1) - return 0; + return 0; - if (! lhsDiffs_.offerChanges[0].second.takerGets().native() || - ! rhsDiffs_.offerChanges[0].second.takerGets().native()) - return 0; + if (!lhsDiffs_.offerChanges[0].second.takerGets().native() || + !rhsDiffs_.offerChanges[0].second.takerGets().native()) + return 0; bool const lhsBigger = lhsDiffs_.offerChanges[0].second.takerGets().mantissa() > @@ -449,21 +469,22 @@ int CashDiff::Impl::xrpRoundToZero () const detail::CashSummary const& bigger = lhsBigger ? lhsDiffs_ : rhsDiffs_; detail::CashSummary const& smaller = lhsBigger ? rhsDiffs_ : lhsDiffs_; if (bigger.offerChanges[0].second.takerGets().mantissa() - - smaller.offerChanges[0].second.takerGets().mantissa() != 1) - return 0; + smaller.offerChanges[0].second.takerGets().mantissa() != + 1) + return 0; // The side with the smaller XRP balance in the OfferChange should have // two XRP differences. The other side should have no XRP differences. if (smaller.xrpChanges.size() != 2) return 0; - if (! bigger.xrpChanges.empty()) + if (!bigger.xrpChanges.empty()) return 0; // There should be no other differences. - if (!smaller.trustChanges.empty() || !bigger.trustChanges.empty() || + if (!smaller.trustChanges.empty() || !bigger.trustChanges.empty() || !smaller.trustDeletions.empty() || !bigger.trustDeletions.empty() || !smaller.offerDeletions.empty() || !bigger.offerDeletions.empty()) - return 0; + return 0; // Return which side exhibited the problem. return lhsBigger ? -1 : 1; @@ -471,12 +492,12 @@ int CashDiff::Impl::xrpRoundToZero () const // Function that compares two CashDiff::OfferAmounts and returns true if // the difference is dust-sized. -static bool diffIsDust ( - CashDiff::OfferAmounts const& lhs, CashDiff::OfferAmounts const& rhs) +static bool +diffIsDust(CashDiff::OfferAmounts const& lhs, CashDiff::OfferAmounts const& rhs) { for (auto i = 0; i < lhs.count(); ++i) { - if (!diffIsDust (lhs[i], rhs[i])) + if (!diffIsDust(lhs[i], rhs[i])) return false; } return true; @@ -485,14 +506,14 @@ static bool diffIsDust ( // Template function to remove dust from individual CashDiff vectors. template static bool -rmVecDust ( +rmVecDust( std::vector>& lhs, std::vector>& rhs, L&& justDust) { - static_assert ( - std::is_same::value, + static_assert( + std::is_same:: + value, "Invalid lambda passed to rmVecDust"); bool dustWasRemoved = false; @@ -500,9 +521,11 @@ rmVecDust ( while (lhsItr != lhs.end()) { using value_t = std::pair; - auto const found = std::equal_range (rhs.begin(), rhs.end(), *lhsItr, - [] (value_t const& a, value_t const& b) - { + auto const found = std::equal_range( + rhs.begin(), + rhs.end(), + *lhsItr, + [](value_t const& a, value_t const& b) { return a.first < b.first; }); @@ -510,12 +533,12 @@ rmVecDust ( { // The same entry changed for both lhs and rhs. Check whether // the differences are small enough to be removed. - if (justDust (lhsItr->second, found.first->second)) + if (justDust(lhsItr->second, found.first->second)) { dustWasRemoved = true; - rhs.erase (found.first); + rhs.erase(found.first); // Dodge an invalid iterator by using erase's return value. - lhsItr = lhs.erase (lhsItr); + lhsItr = lhs.erase(lhsItr); continue; } } @@ -524,7 +547,8 @@ rmVecDust ( return dustWasRemoved; } -bool CashDiff::Impl::rmDust () +bool +CashDiff::Impl::rmDust() { bool removedDust = false; @@ -534,38 +558,40 @@ bool CashDiff::Impl::rmDust () // entries that are _almost_ the same between lhs and rhs. // xrpChanges. We call a difference of 2 drops or less dust. - removedDust |= rmVecDust (lhsDiffs_.xrpChanges, rhsDiffs_.xrpChanges, - [](XRPAmount const& lhs, XRPAmount const& rhs) - { - return diffIsDust (lhs, rhs); + removedDust |= rmVecDust( + lhsDiffs_.xrpChanges, + rhsDiffs_.xrpChanges, + [](XRPAmount const& lhs, XRPAmount const& rhs) { + return diffIsDust(lhs, rhs); }); // trustChanges. - removedDust |= rmVecDust (lhsDiffs_.trustChanges, rhsDiffs_.trustChanges, - [](STAmount const& lhs, STAmount const& rhs) - { - return diffIsDust (lhs, rhs); + removedDust |= rmVecDust( + lhsDiffs_.trustChanges, + rhsDiffs_.trustChanges, + [](STAmount const& lhs, STAmount const& rhs) { + return diffIsDust(lhs, rhs); }); // offerChanges. - removedDust |= rmVecDust (lhsDiffs_.offerChanges, rhsDiffs_.offerChanges, - [](CashDiff::OfferAmounts const& lhs, CashDiff::OfferAmounts const& rhs) - { - return diffIsDust (lhs, rhs); - }); + removedDust |= rmVecDust( + lhsDiffs_.offerChanges, + rhsDiffs_.offerChanges, + [](CashDiff::OfferAmounts const& lhs, + CashDiff::OfferAmounts const& rhs) { return diffIsDust(lhs, rhs); }); // offerDeletions. - removedDust |= rmVecDust ( - lhsDiffs_.offerDeletions, rhsDiffs_.offerDeletions, - [](CashDiff::OfferAmounts const& lhs, CashDiff::OfferAmounts const& rhs) - { - return diffIsDust (lhs, rhs); - }); + removedDust |= rmVecDust( + lhsDiffs_.offerDeletions, + rhsDiffs_.offerDeletions, + [](CashDiff::OfferAmounts const& lhs, + CashDiff::OfferAmounts const& rhs) { return diffIsDust(lhs, rhs); }); return removedDust; } -bool CashDiff::Impl::rmLhsDeletedOffers() +bool +CashDiff::Impl::rmLhsDeletedOffers() { bool const ret = !lhsDiffs_.offerDeletions.empty(); if (ret) @@ -573,7 +599,8 @@ bool CashDiff::Impl::rmLhsDeletedOffers() return ret; } -bool CashDiff::Impl::rmRhsDeletedOffers() +bool +CashDiff::Impl::rmRhsDeletedOffers() { bool const ret = !rhsDiffs_.offerDeletions.empty(); if (ret) @@ -583,17 +610,24 @@ bool CashDiff::Impl::rmRhsDeletedOffers() // Deposits differences between two sorted vectors into a destination. template -static void setDiff ( - std::vector const& a, std::vector const& b, std::vector& dest) +static void +setDiff(std::vector const& a, std::vector const& b, std::vector& dest) { dest.clear(); - std::set_difference (a.cbegin(), a.cend(), - b.cbegin(), b.cend(), std::inserter (dest, dest.end())); + std::set_difference( + a.cbegin(), + a.cend(), + b.cbegin(), + b.cend(), + std::inserter(dest, dest.end())); } -void CashDiff::Impl::findDiffs ( - CashFilter lhsFilter, detail::ApplyStateTable const& lhs, - CashFilter rhsFilter, detail::ApplyStateTable const& rhs) +void +CashDiff::Impl::findDiffs( + CashFilter lhsFilter, + detail::ApplyStateTable const& lhs, + CashFilter rhsFilter, + detail::ApplyStateTable const& rhs) { // If dropsDestroyed_ is different, note that. if (lhs.dropsDestroyed() != rhs.dropsDestroyed()) @@ -602,90 +636,116 @@ void CashDiff::Impl::findDiffs ( } // Extract cash flow changes from the state tables - auto lhsDiffs = getCashFlow (view_, lhsFilter, lhs); - auto rhsDiffs = getCashFlow (view_, rhsFilter, rhs); + auto lhsDiffs = getCashFlow(view_, lhsFilter, lhs); + auto rhsDiffs = getCashFlow(view_, rhsFilter, rhs); // Get statistics on keys. - auto const counts = countKeys (lhsDiffs, rhsDiffs); + auto const counts = countKeys(lhsDiffs, rhsDiffs); commonKeys_ = counts[0]; - lhsKeys_ = counts[1]; - rhsKeys_ = counts[2]; + lhsKeys_ = counts[1]; + rhsKeys_ = counts[2]; // Save only the differences between the results. // xrpChanges: - setDiff (lhsDiffs.xrpChanges, rhsDiffs.xrpChanges, lhsDiffs_.xrpChanges); - setDiff (rhsDiffs.xrpChanges, lhsDiffs.xrpChanges, rhsDiffs_.xrpChanges); + setDiff(lhsDiffs.xrpChanges, rhsDiffs.xrpChanges, lhsDiffs_.xrpChanges); + setDiff(rhsDiffs.xrpChanges, lhsDiffs.xrpChanges, rhsDiffs_.xrpChanges); // trustChanges: - setDiff (lhsDiffs.trustChanges, rhsDiffs.trustChanges, lhsDiffs_.trustChanges); - setDiff (rhsDiffs.trustChanges, lhsDiffs.trustChanges, rhsDiffs_.trustChanges); + setDiff( + lhsDiffs.trustChanges, rhsDiffs.trustChanges, lhsDiffs_.trustChanges); + setDiff( + rhsDiffs.trustChanges, lhsDiffs.trustChanges, rhsDiffs_.trustChanges); // trustDeletions: - setDiff (lhsDiffs.trustDeletions, rhsDiffs.trustDeletions, lhsDiffs_.trustDeletions); - setDiff (rhsDiffs.trustDeletions, lhsDiffs.trustDeletions, rhsDiffs_.trustDeletions); + setDiff( + lhsDiffs.trustDeletions, + rhsDiffs.trustDeletions, + lhsDiffs_.trustDeletions); + setDiff( + rhsDiffs.trustDeletions, + lhsDiffs.trustDeletions, + rhsDiffs_.trustDeletions); // offerChanges: - setDiff (lhsDiffs.offerChanges, rhsDiffs.offerChanges, lhsDiffs_.offerChanges); - setDiff (rhsDiffs.offerChanges, lhsDiffs.offerChanges, rhsDiffs_.offerChanges); + setDiff( + lhsDiffs.offerChanges, rhsDiffs.offerChanges, lhsDiffs_.offerChanges); + setDiff( + rhsDiffs.offerChanges, lhsDiffs.offerChanges, rhsDiffs_.offerChanges); // offerDeletions: - setDiff (lhsDiffs.offerDeletions, rhsDiffs.offerDeletions, lhsDiffs_.offerDeletions); - setDiff (rhsDiffs.offerDeletions, lhsDiffs.offerDeletions, rhsDiffs_.offerDeletions); + setDiff( + lhsDiffs.offerDeletions, + rhsDiffs.offerDeletions, + lhsDiffs_.offerDeletions); + setDiff( + rhsDiffs.offerDeletions, + lhsDiffs.offerDeletions, + rhsDiffs_.offerDeletions); } //------------------------------------------------------------------------------ // Locates differences between two ApplyStateTables. -CashDiff::CashDiff (CashDiff&& other) noexcept -: impl_ (std::move (other.impl_)) +CashDiff::CashDiff(CashDiff&& other) noexcept : impl_(std::move(other.impl_)) { } CashDiff::~CashDiff() = default; -CashDiff::CashDiff (ReadView const& view, - CashFilter lhsFilter, detail::ApplyStateTable const& lhs, - CashFilter rhsFilter, detail::ApplyStateTable const& rhs) -: impl_ (new Impl (view, lhsFilter, lhs, rhsFilter, rhs)) +CashDiff::CashDiff( + ReadView const& view, + CashFilter lhsFilter, + detail::ApplyStateTable const& lhs, + CashFilter rhsFilter, + detail::ApplyStateTable const& rhs) + : impl_(new Impl(view, lhsFilter, lhs, rhsFilter, rhs)) { } -std::size_t CashDiff::commonCount () const +std::size_t +CashDiff::commonCount() const { return impl_->commonCount(); } -std::size_t CashDiff::rhsOnlyCount () const +std::size_t +CashDiff::rhsOnlyCount() const { return impl_->rhsOnlyCount(); } -std::size_t CashDiff::lhsOnlyCount () const +std::size_t +CashDiff::lhsOnlyCount() const { return impl_->lhsOnlyCount(); } -bool CashDiff::hasDiff() const +bool +CashDiff::hasDiff() const { return impl_->hasDiff(); } -int CashDiff::xrpRoundToZero() const +int +CashDiff::xrpRoundToZero() const { return impl_->xrpRoundToZero(); } -bool CashDiff::rmDust() +bool +CashDiff::rmDust() { return impl_->rmDust(); } -bool CashDiff::rmLhsDeletedOffers() +bool +CashDiff::rmLhsDeletedOffers() { return impl_->rmLhsDeletedOffers(); } -bool CashDiff::rmRhsDeletedOffers() +bool +CashDiff::rmRhsDeletedOffers() { return impl_->rmRhsDeletedOffers(); } @@ -694,11 +754,13 @@ bool CashDiff::rmRhsDeletedOffers() // Function that compares two STAmounts and returns true if the difference // is dust-sized. -bool diffIsDust (STAmount const& v1, STAmount const& v2, std::uint8_t e10) +bool +diffIsDust(STAmount const& v1, STAmount const& v2, std::uint8_t e10) { // If one value is positive and the other negative then there's something // odd afoot. - if (v1 != beast::zero && v2 != beast::zero && (v1.negative() != v2.negative())) + if (v1 != beast::zero && v2 != beast::zero && + (v1.negative() != v2.negative())) return false; // v1 and v2 must be the same Issue for their difference to make sense. @@ -725,38 +787,39 @@ bool diffIsDust (STAmount const& v1, STAmount const& v2, std::uint8_t e10) if (l - s <= 2) return true; - static_assert (sizeof (1ULL) == sizeof (std::uint64_t), ""); + static_assert(sizeof(1ULL) == sizeof(std::uint64_t), ""); std::uint64_t const ratio = s / (l - s); - static constexpr std::uint64_t e10Lookup[] - { - 1ULL, - 10ULL, - 100ULL, - 1'000ULL, - 10'000ULL, - 100'000ULL, - 1'000'000ULL, - 10'000'000ULL, - 100'000'000ULL, - 1'000'000'000ULL, - 10'000'000'000ULL, - 100'000'000'000ULL, - 1'000'000'000'000ULL, - 10'000'000'000'000ULL, - 100'000'000'000'000ULL, - 1'000'000'000'000'000ULL, - 10'000'000'000'000'000ULL, - 100'000'000'000'000'000ULL, - 1'000'000'000'000'000'000ULL, + static constexpr std::uint64_t e10Lookup[]{ + 1ULL, + 10ULL, + 100ULL, + 1'000ULL, + 10'000ULL, + 100'000ULL, + 1'000'000ULL, + 10'000'000ULL, + 100'000'000ULL, + 1'000'000'000ULL, + 10'000'000'000ULL, + 100'000'000'000ULL, + 1'000'000'000'000ULL, + 10'000'000'000'000ULL, + 100'000'000'000'000ULL, + 1'000'000'000'000'000ULL, + 10'000'000'000'000'000ULL, + 100'000'000'000'000'000ULL, + 1'000'000'000'000'000'000ULL, 10'000'000'000'000'000'000ULL, }; static std::size_t constexpr maxIndex = - sizeof (e10Lookup) / sizeof e10Lookup[0]; + sizeof(e10Lookup) / sizeof e10Lookup[0]; // Make sure the table is big enough. - static_assert ( + static_assert( std::numeric_limits::max() / - e10Lookup[maxIndex - 1] < 10, "Table too small"); + e10Lookup[maxIndex - 1] < + 10, + "Table too small"); if (e10 >= maxIndex) return false; @@ -772,11 +835,11 @@ bool diffIsDust (STAmount const& v1, STAmount const& v2, std::uint8_t e10) if (diff == beast::zero) return true; - STAmount const ratio = divide (small, diff, v1.issue()); - STAmount const one (v1.issue(), 1); + STAmount const ratio = divide(small, diff, v1.issue()); + STAmount const one(v1.issue(), 1); int const ratioExp = ratio.exponent() - one.exponent(); return ratioExp >= e10; }; -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/Directory.cpp b/src/ripple/ledger/impl/Directory.cpp index 2d770e3eca..6faf418744 100644 --- a/src/ripple/ledger/impl/Directory.cpp +++ b/src/ripple/ledger/impl/Directory.cpp @@ -1,4 +1,5 @@ -//------------ ------------------------------------------------------------------ +//------------ +//------------------------------------------------------------------ /* This file is part of rippled: https://github.com/ripple/rippled Copyright (c) 2012, 2015 Ripple Labs Inc. @@ -23,25 +24,21 @@ namespace ripple { using const_iterator = Dir::const_iterator; -Dir::Dir(ReadView const& view, - Keylet const& key) - : view_(&view) - , root_(key) - , sle_(view_->read(root_)) +Dir::Dir(ReadView const& view, Keylet const& key) + : view_(&view), root_(key), sle_(view_->read(root_)) { if (sle_ != nullptr) indexes_ = &sle_->getFieldV256(sfIndexes); } auto -Dir::begin() const -> - const_iterator +Dir::begin() const -> const_iterator { auto it = const_iterator(*view_, root_, root_); if (sle_ != nullptr) { it.sle_ = sle_; - if (! indexes_->empty()) + if (!indexes_->empty()) { it.indexes_ = indexes_; it.it_ = std::begin(*indexes_); @@ -53,8 +50,7 @@ Dir::begin() const -> } auto -Dir::end() const -> - const_iterator +Dir::end() const -> const_iterator { return const_iterator(*view_, root_, root_); } @@ -73,7 +69,7 @@ const_iterator::reference const_iterator::operator*() const { assert(index_ != beast::zero); - if (! cache_) + if (!cache_) cache_ = view_->read(keylet::child(index_)); return *cache_; } @@ -88,8 +84,7 @@ const_iterator::operator++() } else { - auto const next = - sle_->getFieldU64(sfIndexNext); + auto const next = sle_->getFieldU64(sfIndexNext); if (next == 0) { page_.key = root_.key; @@ -126,4 +121,4 @@ const_iterator::operator++(int) return tmp; } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/OpenView.cpp b/src/ripple/ledger/impl/OpenView.cpp index 38facd9e40..431b766edb 100644 --- a/src/ripple/ledger/impl/OpenView.cpp +++ b/src/ripple/ledger/impl/OpenView.cpp @@ -17,42 +17,35 @@ */ //============================================================================== -#include #include +#include namespace ripple { -open_ledger_t const open_ledger {}; +open_ledger_t const open_ledger{}; -class OpenView::txs_iter_impl - : public txs_type::iter_base +class OpenView::txs_iter_impl : public txs_type::iter_base { private: bool metadata_; txs_map::const_iterator iter_; public: - explicit - txs_iter_impl (bool metadata, - txs_map::const_iterator iter) - : metadata_(metadata) - , iter_(iter) + explicit txs_iter_impl(bool metadata, txs_map::const_iterator iter) + : metadata_(metadata), iter_(iter) { } std::unique_ptr copy() const override { - return std::make_unique< - txs_iter_impl>( - metadata_, iter_); + return std::make_unique(metadata_, iter_); } bool - equal (base_type const& impl) const override + equal(base_type const& impl) const override { - auto const& other = dynamic_cast< - txs_iter_impl const&>(impl); + auto const& other = dynamic_cast(impl); return iter_ == other.iter_; } @@ -67,17 +60,13 @@ public: { value_type result; { - SerialIter sit( - iter_->second.first->slice()); - result.first = std::make_shared< - STTx const>(sit); + SerialIter sit(iter_->second.first->slice()); + result.first = std::make_shared(sit); } if (metadata_) { - SerialIter sit( - iter_->second.second->slice()); - result.second = std::make_shared< - STObject const>(sit, sfMetadata); + SerialIter sit(iter_->second.second->slice()); + result.second = std::make_shared(sit, sfMetadata); } return result; } @@ -85,13 +74,12 @@ public: //------------------------------------------------------------------------------ -OpenView::OpenView (open_ledger_t, - ReadView const* base, Rules const& rules, - std::shared_ptr hold) - : rules_ (rules) - , info_ (base->info()) - , base_ (base) - , hold_ (std::move(hold)) +OpenView::OpenView( + open_ledger_t, + ReadView const* base, + Rules const& rules, + std::shared_ptr hold) + : rules_(rules), info_(base->info()), base_(base), hold_(std::move(hold)) { info_.validated = false; info_.accepted = false; @@ -100,13 +88,12 @@ OpenView::OpenView (open_ledger_t, info_.parentHash = base_->info().hash; } -OpenView::OpenView (ReadView const* base, - std::shared_ptr hold) - : rules_ (base->rules()) - , info_ (base->info()) - , base_ (base) - , hold_ (std::move(hold)) - , open_ (base->open()) +OpenView::OpenView(ReadView const* base, std::shared_ptr hold) + : rules_(base->rules()) + , info_(base->info()) + , base_(base) + , hold_(std::move(hold)) + , open_(base->open()) { } @@ -117,13 +104,11 @@ OpenView::txCount() const } void -OpenView::apply (TxsRawView& to) const +OpenView::apply(TxsRawView& to) const { items_.apply(to); for (auto const& item : txs_) - to.rawTxInsert (item.first, - item.second.first, - item.second.second); + to.rawTxInsert(item.first, item.second.first, item.second.second); } //--- @@ -147,114 +132,100 @@ OpenView::rules() const } bool -OpenView::exists (Keylet const& k) const +OpenView::exists(Keylet const& k) const { return items_.exists(*base_, k); } auto -OpenView::succ (key_type const& key, - boost::optional const& last) const -> - boost::optional +OpenView::succ(key_type const& key, boost::optional const& last) const + -> boost::optional { return items_.succ(*base_, key, last); } std::shared_ptr -OpenView::read (Keylet const& k) const +OpenView::read(Keylet const& k) const { return items_.read(*base_, k); } auto -OpenView::slesBegin() const -> - std::unique_ptr +OpenView::slesBegin() const -> std::unique_ptr { return items_.slesBegin(*base_); } auto -OpenView::slesEnd() const -> - std::unique_ptr +OpenView::slesEnd() const -> std::unique_ptr { return items_.slesEnd(*base_); } auto -OpenView::slesUpperBound(uint256 const& key) const -> - std::unique_ptr +OpenView::slesUpperBound(uint256 const& key) const + -> std::unique_ptr { return items_.slesUpperBound(*base_, key); } auto -OpenView::txsBegin() const -> - std::unique_ptr +OpenView::txsBegin() const -> std::unique_ptr { - return std::make_unique( - !open(), txs_.cbegin()); + return std::make_unique(!open(), txs_.cbegin()); } auto -OpenView::txsEnd() const -> - std::unique_ptr +OpenView::txsEnd() const -> std::unique_ptr { - return std::make_unique( - !open(), txs_.cend()); + return std::make_unique(!open(), txs_.cend()); } bool -OpenView::txExists (key_type const& key) const +OpenView::txExists(key_type const& key) const { return txs_.find(key) != txs_.end(); } auto -OpenView::txRead (key_type const& key) const -> - tx_type +OpenView::txRead(key_type const& key) const -> tx_type { auto const iter = txs_.find(key); if (iter == txs_.end()) return base_->txRead(key); auto const& item = iter->second; - auto stx = std::make_shared(SerialIter{ item.first->slice() }); + auto stx = std::make_shared(SerialIter{item.first->slice()}); decltype(tx_type::second) sto; if (item.second) sto = std::make_shared( - SerialIter{ item.second->slice() }, - sfMetadata); + SerialIter{item.second->slice()}, sfMetadata); else sto = nullptr; - return { std::move(stx), std::move(sto) }; + return {std::move(stx), std::move(sto)}; } //--- void -OpenView::rawErase( - std::shared_ptr const& sle) +OpenView::rawErase(std::shared_ptr const& sle) { items_.erase(sle); } void -OpenView::rawInsert( - std::shared_ptr const& sle) +OpenView::rawInsert(std::shared_ptr const& sle) { items_.insert(sle); } void -OpenView::rawReplace( - std::shared_ptr const& sle) +OpenView::rawReplace(std::shared_ptr const& sle) { items_.replace(sle); } void -OpenView::rawDestroyXRP( - XRPAmount const& fee) +OpenView::rawDestroyXRP(XRPAmount const& fee) { items_.destroyXRP(fee); // VFALCO Deduct from info_.totalDrops ? @@ -264,17 +235,14 @@ OpenView::rawDestroyXRP( //--- void -OpenView::rawTxInsert (key_type const& key, - std::shared_ptr - const& txn, std::shared_ptr< - Serializer const> - const& metaData) +OpenView::rawTxInsert( + key_type const& key, + std::shared_ptr const& txn, + std::shared_ptr const& metaData) { - auto const result = txs_.emplace (key, - std::make_pair(txn, metaData)); - if (! result.second) - LogicError("rawTxInsert: duplicate TX id" + - to_string(key)); + auto const result = txs_.emplace(key, std::make_pair(txn, metaData)); + if (!result.second) + LogicError("rawTxInsert: duplicate TX id" + to_string(key)); } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/PaymentSandbox.cpp b/src/ripple/ledger/impl/PaymentSandbox.cpp index 6339cd75fe..1cf071d6c3 100644 --- a/src/ripple/ledger/impl/PaymentSandbox.cpp +++ b/src/ripple/ledger/impl/PaymentSandbox.cpp @@ -31,7 +31,9 @@ namespace ripple { namespace detail { -auto DeferredCredits::makeKey (AccountID const& a1, +auto +DeferredCredits::makeKey( + AccountID const& a1, AccountID const& a2, Currency const& c) -> Key { @@ -42,29 +44,30 @@ auto DeferredCredits::makeKey (AccountID const& a1, } void -DeferredCredits::credit (AccountID const& sender, +DeferredCredits::credit( + AccountID const& sender, AccountID const& receiver, STAmount const& amount, STAmount const& preCreditSenderBalance) { - assert (sender != receiver); - assert (!amount.negative()); + assert(sender != receiver); + assert(!amount.negative()); - auto const k = makeKey (sender, receiver, amount.getCurrency ()); - auto i = credits_.find (k); - if (i == credits_.end ()) + auto const k = makeKey(sender, receiver, amount.getCurrency()); + auto i = credits_.find(k); + if (i == credits_.end()) { Value v; if (sender < receiver) { v.highAcctCredits = amount; - v.lowAcctCredits = amount.zeroed (); + v.lowAcctCredits = amount.zeroed(); v.lowAcctOrigBalance = preCreditSenderBalance; } else { - v.highAcctCredits = amount.zeroed (); + v.highAcctCredits = amount.zeroed(); v.lowAcctCredits = amount; v.lowAcctOrigBalance = -preCreditSenderBalance; } @@ -83,61 +86,65 @@ DeferredCredits::credit (AccountID const& sender, } void -DeferredCredits::ownerCount (AccountID const& id, +DeferredCredits::ownerCount( + AccountID const& id, std::uint32_t cur, std::uint32_t next) { - auto const v = std::max (cur, next); - auto r = ownerCounts_.emplace (std::make_pair (id, v)); + auto const v = std::max(cur, next); + auto r = ownerCounts_.emplace(std::make_pair(id, v)); if (!r.second) { auto& mapVal = r.first->second; - mapVal = std::max (v, mapVal); + mapVal = std::max(v, mapVal); } } boost::optional -DeferredCredits::ownerCount (AccountID const& id) const +DeferredCredits::ownerCount(AccountID const& id) const { - auto i = ownerCounts_.find (id); - if (i != ownerCounts_.end ()) + auto i = ownerCounts_.find(id); + if (i != ownerCounts_.end()) return i->second; return boost::none; } // Get the adjustments for the balance between main and other. auto -DeferredCredits::adjustments (AccountID const& main, +DeferredCredits::adjustments( + AccountID const& main, AccountID const& other, Currency const& currency) const -> boost::optional { boost::optional result; - Key const k = makeKey (main, other, currency); - auto i = credits_.find (k); - if (i == credits_.end ()) + Key const k = makeKey(main, other, currency); + auto i = credits_.find(k); + if (i == credits_.end()) return result; auto const& v = i->second; if (main < other) { - result.emplace (v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance); + result.emplace( + v.highAcctCredits, v.lowAcctCredits, v.lowAcctOrigBalance); return result; } else { - result.emplace (v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance); + result.emplace( + v.lowAcctCredits, v.highAcctCredits, -v.lowAcctOrigBalance); return result; } } -void DeferredCredits::apply( - DeferredCredits& to) +void +DeferredCredits::apply(DeferredCredits& to) { for (auto const& i : credits_) { - auto r = to.credits_.emplace (i); + auto r = to.credits_.emplace(i); if (!r.second) { auto& toVal = r.first->second; @@ -150,22 +157,23 @@ void DeferredCredits::apply( for (auto const& i : ownerCounts_) { - auto r = to.ownerCounts_.emplace (i); + auto r = to.ownerCounts_.emplace(i); if (!r.second) { auto& toVal = r.first->second; auto const& fromVal = i.second; - toVal = std::max (toVal, fromVal); + toVal = std::max(toVal, fromVal); } } } -} // detail +} // namespace detail STAmount -PaymentSandbox::balanceHook (AccountID const& account, +PaymentSandbox::balanceHook( + AccountID const& account, AccountID const& issuer, - STAmount const& amount) const + STAmount const& amount) const { /* There are two algorithms here. The pre-switchover algorithm takes the @@ -178,14 +186,14 @@ PaymentSandbox::balanceHook (AccountID const& account, magnitudes, (B+C)-C may not equal B. */ - auto const currency = amount.getCurrency (); + auto const currency = amount.getCurrency(); - auto delta = amount.zeroed (); + auto delta = amount.zeroed(); auto lastBal = amount; auto minBal = amount; for (auto curSB = this; curSB; curSB = curSB->ps_) { - if (auto adj = curSB->tab_.adjustments (account, issuer, currency)) + if (auto adj = curSB->tab_.adjustments(account, issuer, currency)) { delta += adj->debits; lastBal = adj->origBalance; @@ -212,44 +220,46 @@ PaymentSandbox::balanceHook (AccountID const& account, } std::uint32_t -PaymentSandbox::ownerCountHook (AccountID const& account, - std::uint32_t count) const +PaymentSandbox::ownerCountHook(AccountID const& account, std::uint32_t count) + const { std::uint32_t result = count; for (auto curSB = this; curSB; curSB = curSB->ps_) { - if (auto adj = curSB->tab_.ownerCount (account)) - result = std::max (result, *adj); + if (auto adj = curSB->tab_.ownerCount(account)) + result = std::max(result, *adj); } return result; } void -PaymentSandbox::creditHook (AccountID const& from, +PaymentSandbox::creditHook( + AccountID const& from, AccountID const& to, - STAmount const& amount, - STAmount const& preCreditBalance) + STAmount const& amount, + STAmount const& preCreditBalance) { - tab_.credit (from, to, amount, preCreditBalance); + tab_.credit(from, to, amount, preCreditBalance); } void -PaymentSandbox::adjustOwnerCountHook (AccountID const& account, +PaymentSandbox::adjustOwnerCountHook( + AccountID const& account, std::uint32_t cur, - std::uint32_t next) + std::uint32_t next) { - tab_.ownerCount (account, cur, next); + tab_.ownerCount(account, cur, next); } void -PaymentSandbox::apply (RawView& to) +PaymentSandbox::apply(RawView& to) { - assert(! ps_); + assert(!ps_); items_.apply(to); } void -PaymentSandbox::apply (PaymentSandbox& to) +PaymentSandbox::apply(PaymentSandbox& to) { assert(ps_ == &to); items_.apply(to); @@ -257,20 +267,22 @@ PaymentSandbox::apply (PaymentSandbox& to) } std::map, STAmount> -PaymentSandbox::balanceChanges (ReadView const& view) const +PaymentSandbox::balanceChanges(ReadView const& view) const { using key_t = std::tuple; // Map of delta trust lines. As a special case, when both ends of the trust // line are the same currency, then it's delta currency for that issuer. To - // get the change in XRP balance, Account == root, issuer == root, currency == XRP + // get the change in XRP balance, Account == root, issuer == root, currency + // == XRP std::map result; // populate a dictionary with low/high/currency/delta. This can be // compared with the other versions payment code. - auto each = [&result](uint256 const& key, bool isDelete, - std::shared_ptr const& before, - std::shared_ptr const& after) { - + auto each = [&result]( + uint256 const& key, + bool isDelete, + std::shared_ptr const& before, + std::shared_ptr const& after) { STAmount oldBalance; STAmount newBalance; AccountID lowID; @@ -282,8 +294,8 @@ PaymentSandbox::balanceChanges (ReadView const& view) const if (!before) return; - auto const bt = before->getType (); - switch(bt) + auto const bt = before->getType(); + switch (bt) { case ltACCOUNT_ROOT: lowID = xrpAccount(); @@ -307,8 +319,8 @@ PaymentSandbox::balanceChanges (ReadView const& view) const else if (!before) { // insert - auto const at = after->getType (); - switch(at) + auto const at = after->getType(); + switch (at) { case ltACCOUNT_ROOT: lowID = xrpAccount(); @@ -332,9 +344,9 @@ PaymentSandbox::balanceChanges (ReadView const& view) const else { // modify - auto const at = after->getType (); - assert (at == before->getType ()); - switch(at) + auto const at = after->getType(); + assert(at == before->getType()); + switch (at) { case ltACCOUNT_ROOT: lowID = xrpAccount(); @@ -372,13 +384,14 @@ PaymentSandbox::balanceChanges (ReadView const& view) const r.first->second += delta; } }; - items_.visit (view, each); + items_.visit(view, each); return result; } -XRPAmount PaymentSandbox::xrpDestroyed () const +XRPAmount +PaymentSandbox::xrpDestroyed() const { - return items_.dropsDestroyed (); + return items_.dropsDestroyed(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/RawStateTable.cpp b/src/ripple/ledger/impl/RawStateTable.cpp index 5694121d12..0de38ebe80 100644 --- a/src/ripple/ledger/impl/RawStateTable.cpp +++ b/src/ripple/ledger/impl/RawStateTable.cpp @@ -17,14 +17,13 @@ */ //============================================================================== -#include #include +#include namespace ripple { namespace detail { -class RawStateTable::sles_iter_impl - : public ReadView::sles_type::iter_base +class RawStateTable::sles_iter_impl : public ReadView::sles_type::iter_base { private: std::shared_ptr sle0_; @@ -35,23 +34,21 @@ private: items_t::const_iterator end1_; public: - sles_iter_impl (sles_iter_impl const&) = default; + sles_iter_impl(sles_iter_impl const&) = default; - sles_iter_impl (items_t::const_iterator iter1, + sles_iter_impl( + items_t::const_iterator iter1, items_t::const_iterator end1, - ReadView::sles_type::iterator iter0, - ReadView::sles_type::iterator end0) - : iter0_ (iter0) - , end0_ (end0) - , iter1_ (iter1) - , end1_ (end1) + ReadView::sles_type::iterator iter0, + ReadView::sles_type::iterator end0) + : iter0_(iter0), end0_(end0), iter1_(iter1), end1_(end1) { if (iter0_ != end0_) sle0_ = *iter0_; if (iter1_ != end1) { sle1_ = iter1_->second.second; - skip (); + skip(); } } @@ -62,20 +59,17 @@ public: } bool - equal (base_type const& impl) const override + equal(base_type const& impl) const override { - auto const& other = dynamic_cast< - sles_iter_impl const&>(impl); - assert(end1_ == other.end1_ && - end0_ == other.end0_); - return iter1_ == other.iter1_ && - iter0_ == other.iter0_; + auto const& other = dynamic_cast(impl); + assert(end1_ == other.end1_ && end0_ == other.end0_); + return iter1_ == other.iter1_ && iter0_ == other.iter0_; } void increment() override { - assert (sle1_ || sle0_); + assert(sle1_ || sle0_); if (sle1_ && !sle0_) { @@ -89,12 +83,12 @@ public: return; } - if (sle1_->key () == sle0_->key()) + if (sle1_->key() == sle0_->key()) { inc1(); inc0(); } - else if (sle1_->key () < sle0_->key()) + else if (sle1_->key() < sle0_->key()) { inc1(); } @@ -108,16 +102,18 @@ public: value_type dereference() const override { - if (! sle1_) + if (!sle1_) return sle0_; - else if (! sle0_) + else if (!sle0_) return sle1_; if (sle1_->key() <= sle0_->key()) return sle1_; return sle0_; } + private: - void inc0() + void + inc0() { ++iter0_; if (iter0_ == end0_) @@ -125,8 +121,9 @@ private: else sle0_ = *iter0_; } - - void inc1() + + void + inc1() { ++iter1_; if (iter1_ == end1_) @@ -134,16 +131,16 @@ private: else sle1_ = iter1_->second.second; } - - void skip() + + void + skip() { - while (iter1_ != end1_ && - iter1_->second.first == Action::erase && + while (iter1_ != end1_ && iter1_->second.first == Action::erase && sle0_->key() == sle1_->key()) { inc1(); inc0(); - if (! sle0_) + if (!sle0_) return; } } @@ -154,30 +151,29 @@ private: // Base invariants are checked by the base during apply() void -RawStateTable::apply (RawView& to) const +RawStateTable::apply(RawView& to) const { to.rawDestroyXRP(dropsDestroyed_); for (auto const& elem : items_) { auto const& item = elem.second; - switch(item.first) + switch (item.first) { - case Action::erase: - to.rawErase(item.second); - break; - case Action::insert: - to.rawInsert(item.second); - break; - case Action::replace: - to.rawReplace(item.second); - break; + case Action::erase: + to.rawErase(item.second); + break; + case Action::insert: + to.rawInsert(item.second); + break; + case Action::replace: + to.rawReplace(item.second); + break; } } } bool -RawStateTable::exists (ReadView const& base, - Keylet const& k) const +RawStateTable::exists(ReadView const& base, Keylet const& k) const { assert(k.key.isNonZero()); auto const iter = items_.find(k.key); @@ -186,7 +182,7 @@ RawStateTable::exists (ReadView const& base, auto const& item = iter->second; if (item.first == Action::erase) return false; - if (! k.check(*item.second)) + if (!k.check(*item.second)) return false; return true; } @@ -196,10 +192,10 @@ RawStateTable::exists (ReadView const& base, the lower of the two. */ auto -RawStateTable::succ (ReadView const& base, - key_type const& key, boost::optional< - key_type> const& last) const -> - boost::optional +RawStateTable::succ( + ReadView const& base, + key_type const& key, + boost::optional const& last) const -> boost::optional { boost::optional next = key; items_t::const_iterator iter; @@ -208,20 +204,17 @@ RawStateTable::succ (ReadView const& base, do { next = base.succ(*next, last); - if (! next) + if (!next) break; iter = items_.find(*next); - } - while (iter != items_.end() && - iter->second.first == Action::erase); + } while (iter != items_.end() && iter->second.first == Action::erase); // Find non-deleted successor in our list - for (iter = items_.upper_bound(key); - iter != items_.end (); ++iter) + for (iter = items_.upper_bound(key); iter != items_.end(); ++iter) { if (iter->second.first != Action::erase) { // Found both, return the lower key - if (! next || next > iter->first) + if (!next || next > iter->first) next = iter->first; break; } @@ -234,99 +227,90 @@ RawStateTable::succ (ReadView const& base, } void -RawStateTable::erase( - std::shared_ptr const& sle) +RawStateTable::erase(std::shared_ptr const& sle) { // The base invariant is checked during apply auto const result = items_.emplace( std::piecewise_construct, - std::forward_as_tuple(sle->key()), - std::forward_as_tuple( - Action::erase, sle)); + std::forward_as_tuple(sle->key()), + std::forward_as_tuple(Action::erase, sle)); if (result.second) return; auto& item = result.first->second; - switch(item.first) + switch (item.first) { - case Action::erase: - LogicError("RawStateTable::erase: already erased"); - break; - case Action::insert: - items_.erase(result.first); - break; - case Action::replace: - item.first = Action::erase; - item.second = sle; - break; + case Action::erase: + LogicError("RawStateTable::erase: already erased"); + break; + case Action::insert: + items_.erase(result.first); + break; + case Action::replace: + item.first = Action::erase; + item.second = sle; + break; } } void -RawStateTable::insert( - std::shared_ptr const& sle) +RawStateTable::insert(std::shared_ptr const& sle) { auto const result = items_.emplace( std::piecewise_construct, - std::forward_as_tuple(sle->key()), - std::forward_as_tuple( - Action::insert, sle)); + std::forward_as_tuple(sle->key()), + std::forward_as_tuple(Action::insert, sle)); if (result.second) return; auto& item = result.first->second; - switch(item.first) + switch (item.first) { - case Action::erase: - item.first = Action::replace; - item.second = sle; - break; - case Action::insert: - LogicError("RawStateTable::insert: already inserted"); - break; - case Action::replace: - LogicError("RawStateTable::insert: already exists"); - break; + case Action::erase: + item.first = Action::replace; + item.second = sle; + break; + case Action::insert: + LogicError("RawStateTable::insert: already inserted"); + break; + case Action::replace: + LogicError("RawStateTable::insert: already exists"); + break; } } void -RawStateTable::replace( - std::shared_ptr const& sle) +RawStateTable::replace(std::shared_ptr const& sle) { auto const result = items_.emplace( std::piecewise_construct, - std::forward_as_tuple(sle->key()), - std::forward_as_tuple( - Action::replace, sle)); + std::forward_as_tuple(sle->key()), + std::forward_as_tuple(Action::replace, sle)); if (result.second) return; auto& item = result.first->second; - switch(item.first) + switch (item.first) { - case Action::erase: - LogicError("RawStateTable::replace: was erased"); - break; - case Action::insert: - case Action::replace: - item.second = sle; - break; + case Action::erase: + LogicError("RawStateTable::replace: was erased"); + break; + case Action::insert: + case Action::replace: + item.second = sle; + break; } } std::shared_ptr -RawStateTable::read (ReadView const& base, - Keylet const& k) const +RawStateTable::read(ReadView const& base, Keylet const& k) const { - auto const iter = - items_.find(k.key); + auto const iter = items_.find(k.key); if (iter == items_.end()) return base.read(k); auto const& item = iter->second; if (item.first == Action::erase) return nullptr; // Convert to SLE const - std::shared_ptr< - SLE const> sle = item.second; - if (! k.check(*sle)) + std::shared_ptr sle = item.second; + if (!k.check(*sle)) return nullptr; return sle; } @@ -338,28 +322,28 @@ RawStateTable::destroyXRP(XRPAmount const& fee) } std::unique_ptr -RawStateTable::slesBegin (ReadView const& base) const +RawStateTable::slesBegin(ReadView const& base) const { return std::make_unique( - items_.begin(), items_.end(), - base.sles.begin(), base.sles.end()); + items_.begin(), items_.end(), base.sles.begin(), base.sles.end()); } std::unique_ptr -RawStateTable::slesEnd (ReadView const& base) const +RawStateTable::slesEnd(ReadView const& base) const { return std::make_unique( - items_.end(), items_.end(), - base.sles.end(), base.sles.end()); + items_.end(), items_.end(), base.sles.end(), base.sles.end()); } std::unique_ptr -RawStateTable::slesUpperBound (ReadView const& base, uint256 const& key) const +RawStateTable::slesUpperBound(ReadView const& base, uint256 const& key) const { return std::make_unique( - items_.upper_bound(key), items_.end(), - base.sles.upper_bound(key), base.sles.end()); + items_.upper_bound(key), + items_.end(), + base.sles.upper_bound(key), + base.sles.end()); } -} // detail -} // ripple +} // namespace detail +} // namespace ripple diff --git a/src/ripple/ledger/impl/ReadView.cpp b/src/ripple/ledger/impl/ReadView.cpp index 99ca350770..69349e1dfc 100644 --- a/src/ripple/ledger/impl/ReadView.cpp +++ b/src/ripple/ledger/impl/ReadView.cpp @@ -25,41 +25,38 @@ namespace ripple { class Rules::Impl { private: - std::unordered_set> set_; + std::unordered_set> set_; boost::optional digest_; std::unordered_set> const& presets_; public: - explicit Impl( - std::unordered_set> const& presets) + explicit Impl(std::unordered_set> const& presets) : presets_(presets) { } explicit Impl( DigestAwareReadView const& ledger, - std::unordered_set> const& presets) + std::unordered_set> const& presets) : presets_(presets) { auto const k = keylet::amendments(); digest_ = ledger.digest(k.key); - if (! digest_) + if (!digest_) return; auto const sle = ledger.read(k); - if (! sle) + if (!sle) { // LogicError() ? return; } - for (auto const& item : - sle->getFieldV256(sfAmendments)) + for (auto const& item : sle->getFieldV256(sfAmendments)) set_.insert(item); } bool - enabled (uint256 const& feature) const + enabled(uint256 const& feature) const { if (presets_.count(feature) > 0) return true; @@ -67,23 +64,22 @@ public: } bool - changed (DigestAwareReadView const& ledger) const + changed(DigestAwareReadView const& ledger) const { - auto const digest = - ledger.digest(keylet::amendments().key); - if (! digest && ! digest_) + auto const digest = ledger.digest(keylet::amendments().key); + if (!digest && !digest_) return false; - if (! digest || ! digest_) + if (!digest || !digest_) return true; return *digest != *digest_; } bool - operator== (Impl const& other) const + operator==(Impl const& other) const { - if (! digest_ && ! other.digest_) + if (!digest_ && !other.digest_) return true; - if (! digest_ || ! other.digest_) + if (!digest_ || !other.digest_) return false; return *digest_ == *other.digest_; } @@ -93,7 +89,7 @@ public: Rules::Rules( DigestAwareReadView const& ledger, - std::unordered_set> const& presets) + std::unordered_set> const& presets) : impl_(std::make_shared(ledger, presets)) { } @@ -104,21 +100,21 @@ Rules::Rules(std::unordered_set> const& presets) } bool -Rules::enabled (uint256 const& id) const +Rules::enabled(uint256 const& id) const { - assert (impl_); + assert(impl_); return impl_->enabled(id); } bool -Rules::changed (DigestAwareReadView const& ledger) const +Rules::changed(DigestAwareReadView const& ledger) const { - assert (impl_); + assert(impl_); return impl_->changed(ledger); } bool -Rules::operator== (Rules const& other) const +Rules::operator==(Rules const& other) const { assert(impl_ && other.impl_); if (impl_.get() == other.impl_.get()) @@ -128,38 +124,31 @@ Rules::operator== (Rules const& other) const //------------------------------------------------------------------------------ -ReadView::sles_type::sles_type( - ReadView const& view) - : ReadViewFwdRange(view) +ReadView::sles_type::sles_type(ReadView const& view) : ReadViewFwdRange(view) { } auto -ReadView::sles_type::begin() const -> - iterator +ReadView::sles_type::begin() const -> iterator { return iterator(view_, view_->slesBegin()); } auto -ReadView::sles_type::end() const -> - iterator const& +ReadView::sles_type::end() const -> iterator const& { - if (! end_) + if (!end_) end_ = iterator(view_, view_->slesEnd()); return *end_; } auto -ReadView::sles_type::upper_bound(key_type const& key) const -> - iterator +ReadView::sles_type::upper_bound(key_type const& key) const -> iterator { return iterator(view_, view_->slesUpperBound(key)); } -ReadView::txs_type::txs_type( - ReadView const& view) - : ReadViewFwdRange(view) +ReadView::txs_type::txs_type(ReadView const& view) : ReadViewFwdRange(view) { } @@ -170,19 +159,17 @@ ReadView::txs_type::empty() const } auto -ReadView::txs_type::begin() const -> - iterator +ReadView::txs_type::begin() const -> iterator { return iterator(view_, view_->txsBegin()); } auto -ReadView::txs_type::end() const -> - iterator const& +ReadView::txs_type::end() const -> iterator const& { - if (! end_) + if (!end_) end_ = iterator(view_, view_->txsEnd()); return *end_; } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/TxMeta.cpp b/src/ripple/ledger/impl/TxMeta.cpp index 482c5d93b0..d00fc9d60a 100644 --- a/src/ripple/ledger/impl/TxMeta.cpp +++ b/src/ripple/ledger/impl/TxMeta.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include -#include #include +#include #include +#include #include #include @@ -28,131 +28,141 @@ namespace ripple { // VFALCO TODO rename class to TransactionMeta -template -TxMeta::TxMeta (uint256 const& txid, - std::uint32_t ledger, T const& data, CtorHelper) - : mTransactionID (txid) - , mLedger (ledger) - , mNodes (sfAffectedNodes, 32) +template +TxMeta::TxMeta( + uint256 const& txid, + std::uint32_t ledger, + T const& data, + CtorHelper) + : mTransactionID(txid), mLedger(ledger), mNodes(sfAffectedNodes, 32) { - SerialIter sit (makeSlice(data)); + SerialIter sit(makeSlice(data)); STObject obj(sit, sfMetadata); - mResult = obj.getFieldU8 (sfTransactionResult); - mIndex = obj.getFieldU32 (sfTransactionIndex); - mNodes = * dynamic_cast (&obj.getField (sfAffectedNodes)); + mResult = obj.getFieldU8(sfTransactionResult); + mIndex = obj.getFieldU32(sfTransactionIndex); + mNodes = *dynamic_cast(&obj.getField(sfAffectedNodes)); - if (obj.isFieldPresent (sfDeliveredAmount)) - setDeliveredAmount (obj.getFieldAmount (sfDeliveredAmount)); + if (obj.isFieldPresent(sfDeliveredAmount)) + setDeliveredAmount(obj.getFieldAmount(sfDeliveredAmount)); } -TxMeta::TxMeta (uint256 const& txid, std::uint32_t ledger, STObject const& obj) - : mTransactionID (txid) - , mLedger (ledger) - , mNodes (obj.getFieldArray (sfAffectedNodes)) +TxMeta::TxMeta(uint256 const& txid, std::uint32_t ledger, STObject const& obj) + : mTransactionID(txid) + , mLedger(ledger) + , mNodes(obj.getFieldArray(sfAffectedNodes)) { - mResult = obj.getFieldU8 (sfTransactionResult); - mIndex = obj.getFieldU32 (sfTransactionIndex); + mResult = obj.getFieldU8(sfTransactionResult); + mIndex = obj.getFieldU32(sfTransactionIndex); - auto affectedNodes = dynamic_cast - (obj.peekAtPField (sfAffectedNodes)); - assert (affectedNodes); + auto affectedNodes = + dynamic_cast(obj.peekAtPField(sfAffectedNodes)); + assert(affectedNodes); if (affectedNodes) mNodes = *affectedNodes; - if (obj.isFieldPresent (sfDeliveredAmount)) - setDeliveredAmount (obj.getFieldAmount (sfDeliveredAmount)); + if (obj.isFieldPresent(sfDeliveredAmount)) + setDeliveredAmount(obj.getFieldAmount(sfDeliveredAmount)); } -TxMeta::TxMeta (uint256 const& txid, - std::uint32_t ledger, - Blob const& vec) - : TxMeta (txid, ledger, vec, CtorHelper ()) +TxMeta::TxMeta(uint256 const& txid, std::uint32_t ledger, Blob const& vec) + : TxMeta(txid, ledger, vec, CtorHelper()) { } -TxMeta::TxMeta (uint256 const& txid, +TxMeta::TxMeta( + uint256 const& txid, std::uint32_t ledger, std::string const& data) - : TxMeta (txid, ledger, data, CtorHelper ()) + : TxMeta(txid, ledger, data, CtorHelper()) { } -bool TxMeta::isNodeAffected (uint256 const& node) const +bool +TxMeta::isNodeAffected(uint256 const& node) const { for (auto const& n : mNodes) { - if (n.getFieldH256 (sfLedgerIndex) == node) + if (n.getFieldH256(sfLedgerIndex) == node) return true; } return false; } -void TxMeta::setAffectedNode (uint256 const& node, SField const& type, - std::uint16_t nodeType) +void +TxMeta::setAffectedNode( + uint256 const& node, + SField const& type, + std::uint16_t nodeType) { // make sure the node exists and force its type for (auto& n : mNodes) { - if (n.getFieldH256 (sfLedgerIndex) == node) + if (n.getFieldH256(sfLedgerIndex) == node) { - n.setFName (type); - n.setFieldU16 (sfLedgerEntryType, nodeType); + n.setFName(type); + n.setFieldU16(sfLedgerEntryType, nodeType); return; } } - mNodes.push_back (STObject (type)); - STObject& obj = mNodes.back (); + mNodes.push_back(STObject(type)); + STObject& obj = mNodes.back(); - assert (obj.getFName () == type); - obj.setFieldH256 (sfLedgerIndex, node); - obj.setFieldU16 (sfLedgerEntryType, nodeType); + assert(obj.getFName() == type); + obj.setFieldH256(sfLedgerIndex, node); + obj.setFieldU16(sfLedgerEntryType, nodeType); } boost::container::flat_set TxMeta::getAffectedAccounts(beast::Journal j) const { boost::container::flat_set list; - list.reserve (10); + list.reserve(10); // This code should match the behavior of the JS method: // Meta#getAffectedAccounts for (auto const& it : mNodes) { - int index = it.getFieldIndex ((it.getFName () == sfCreatedNode) ? sfNewFields : sfFinalFields); + int index = it.getFieldIndex( + (it.getFName() == sfCreatedNode) ? sfNewFields : sfFinalFields); if (index != -1) { - const STObject* inner = dynamic_cast (&it.peekAtIndex (index)); + const STObject* inner = + dynamic_cast(&it.peekAtIndex(index)); assert(inner); if (inner) { for (auto const& field : *inner) { - if (auto sa = dynamic_cast (&field)) + if (auto sa = dynamic_cast(&field)) { - assert (! sa->isDefault()); - if (! sa->isDefault()) + assert(!sa->isDefault()); + if (!sa->isDefault()) list.insert(sa->value()); } - else if ((field.getFName () == sfLowLimit) || (field.getFName () == sfHighLimit) || - (field.getFName () == sfTakerPays) || (field.getFName () == sfTakerGets)) + else if ( + (field.getFName() == sfLowLimit) || + (field.getFName() == sfHighLimit) || + (field.getFName() == sfTakerPays) || + (field.getFName() == sfTakerGets)) { - const STAmount* lim = dynamic_cast (&field); + const STAmount* lim = + dynamic_cast(&field); if (lim != nullptr) { - auto issuer = lim->getIssuer (); + auto issuer = lim->getIssuer(); - if (issuer.isNonZero ()) + if (issuer.isNonZero()) list.insert(issuer); } else { - JLOG (j.fatal()) << "limit is not amount " - << field.getJson (JsonOptions::none); + JLOG(j.fatal()) << "limit is not amount " + << field.getJson(JsonOptions::none); } } } @@ -163,103 +173,112 @@ TxMeta::getAffectedAccounts(beast::Journal j) const return list; } -STObject& TxMeta::getAffectedNode (SLE::ref node, SField const& type) +STObject& +TxMeta::getAffectedNode(SLE::ref node, SField const& type) { uint256 index = node->key(); for (auto& n : mNodes) { - if (n.getFieldH256 (sfLedgerIndex) == index) + if (n.getFieldH256(sfLedgerIndex) == index) return n; } - mNodes.push_back (STObject (type)); - STObject& obj = mNodes.back (); + mNodes.push_back(STObject(type)); + STObject& obj = mNodes.back(); - assert (obj.getFName () == type); - obj.setFieldH256 (sfLedgerIndex, index); - obj.setFieldU16 (sfLedgerEntryType, node->getFieldU16 (sfLedgerEntryType)); + assert(obj.getFName() == type); + obj.setFieldH256(sfLedgerIndex, index); + obj.setFieldU16(sfLedgerEntryType, node->getFieldU16(sfLedgerEntryType)); return obj; } -STObject& TxMeta::getAffectedNode (uint256 const& node) +STObject& +TxMeta::getAffectedNode(uint256 const& node) { for (auto& n : mNodes) { - if (n.getFieldH256 (sfLedgerIndex) == node) + if (n.getFieldH256(sfLedgerIndex) == node) return n; } - assert (false); - Throw ("Affected node not found"); - return *(mNodes.begin()); // Silence compiler warning. + assert(false); + Throw("Affected node not found"); + return *(mNodes.begin()); // Silence compiler warning. } -const STObject& TxMeta::peekAffectedNode (uint256 const& node) const +const STObject& +TxMeta::peekAffectedNode(uint256 const& node) const { for (auto const& n : mNodes) { - if (n.getFieldH256 (sfLedgerIndex) == node) + if (n.getFieldH256(sfLedgerIndex) == node) return n; } - Throw ("Affected node not found"); - return *(mNodes.begin()); // Silence compiler warning. + Throw("Affected node not found"); + return *(mNodes.begin()); // Silence compiler warning. } -void TxMeta::init (uint256 const& id, std::uint32_t ledger) +void +TxMeta::init(uint256 const& id, std::uint32_t ledger) { mTransactionID = id; mLedger = ledger; - mNodes = STArray (sfAffectedNodes, 32); - mDelivered = boost::optional (); + mNodes = STArray(sfAffectedNodes, 32); + mDelivered = boost::optional(); } -void TxMeta::swap (TxMeta& s) noexcept +void +TxMeta::swap(TxMeta& s) noexcept { - assert ((mTransactionID == s.mTransactionID) && (mLedger == s.mLedger)); - mNodes.swap (s.mNodes); + assert((mTransactionID == s.mTransactionID) && (mLedger == s.mLedger)); + mNodes.swap(s.mNodes); } -bool TxMeta::thread (STObject& node, uint256 const& prevTxID, std::uint32_t prevLgrID) +bool +TxMeta::thread(STObject& node, uint256 const& prevTxID, std::uint32_t prevLgrID) { - if (node.getFieldIndex (sfPreviousTxnID) == -1) + if (node.getFieldIndex(sfPreviousTxnID) == -1) { - assert (node.getFieldIndex (sfPreviousTxnLgrSeq) == -1); - node.setFieldH256 (sfPreviousTxnID, prevTxID); - node.setFieldU32 (sfPreviousTxnLgrSeq, prevLgrID); + assert(node.getFieldIndex(sfPreviousTxnLgrSeq) == -1); + node.setFieldH256(sfPreviousTxnID, prevTxID); + node.setFieldU32(sfPreviousTxnLgrSeq, prevLgrID); return true; } - assert (node.getFieldH256 (sfPreviousTxnID) == prevTxID); - assert (node.getFieldU32 (sfPreviousTxnLgrSeq) == prevLgrID); + assert(node.getFieldH256(sfPreviousTxnID) == prevTxID); + assert(node.getFieldU32(sfPreviousTxnLgrSeq) == prevLgrID); return false; } -static bool compare (const STObject& o1, const STObject& o2) +static bool +compare(const STObject& o1, const STObject& o2) { - return o1.getFieldH256 (sfLedgerIndex) < o2.getFieldH256 (sfLedgerIndex); + return o1.getFieldH256(sfLedgerIndex) < o2.getFieldH256(sfLedgerIndex); } -STObject TxMeta::getAsObject () const +STObject +TxMeta::getAsObject() const { - STObject metaData (sfTransactionMetaData); - assert (mResult != 255); - metaData.setFieldU8 (sfTransactionResult, mResult); - metaData.setFieldU32 (sfTransactionIndex, mIndex); - metaData.emplace_back (mNodes); - if (hasDeliveredAmount ()) - metaData.setFieldAmount (sfDeliveredAmount, getDeliveredAmount ()); + STObject metaData(sfTransactionMetaData); + assert(mResult != 255); + metaData.setFieldU8(sfTransactionResult, mResult); + metaData.setFieldU32(sfTransactionIndex, mIndex); + metaData.emplace_back(mNodes); + if (hasDeliveredAmount()) + metaData.setFieldAmount(sfDeliveredAmount, getDeliveredAmount()); return metaData; } -void TxMeta::addRaw (Serializer& s, TER result, std::uint32_t index) +void +TxMeta::addRaw(Serializer& s, TER result, std::uint32_t index) { - mResult = TERtoInt (result); + mResult = TERtoInt(result); mIndex = index; - assert ((mResult == 0) || ((mResult > 100) && (mResult <= 255))); + assert((mResult == 0) || ((mResult > 100) && (mResult <= 255))); - mNodes.sort (compare); + mNodes.sort(compare); - getAsObject ().add (s); + getAsObject().add(s); } -} // ripple +} // namespace ripple diff --git a/src/ripple/ledger/impl/View.cpp b/src/ripple/ledger/impl/View.cpp index 6ddb557873..6264ca8a72 100644 --- a/src/ripple/ledger/impl/View.cpp +++ b/src/ripple/ledger/impl/View.cpp @@ -17,17 +17,17 @@ */ //============================================================================== +#include +#include #include +#include #include #include #include -#include -#include -#include #include -#include #include #include +#include #include #include @@ -39,121 +39,130 @@ namespace ripple { // //------------------------------------------------------------------------------ -void addRaw (LedgerInfo const& info, Serializer& s) +void +addRaw(LedgerInfo const& info, Serializer& s) { - s.add32 (info.seq); - s.add64 (info.drops.drops ()); - s.add256 (info.parentHash); - s.add256 (info.txHash); - s.add256 (info.accountHash); - s.add32 (info.parentCloseTime.time_since_epoch().count()); - s.add32 (info.closeTime.time_since_epoch().count()); - s.add8 (info.closeTimeResolution.count()); - s.add8 (info.closeFlags); + s.add32(info.seq); + s.add64(info.drops.drops()); + s.add256(info.parentHash); + s.add256(info.txHash); + s.add256(info.accountHash); + s.add32(info.parentCloseTime.time_since_epoch().count()); + s.add32(info.closeTime.time_since_epoch().count()); + s.add8(info.closeTimeResolution.count()); + s.add8(info.closeFlags); } bool -isGlobalFrozen (ReadView const& view, AccountID const& issuer) +isGlobalFrozen(ReadView const& view, AccountID const& issuer) { - if (isXRP (issuer)) + if (isXRP(issuer)) return false; if (auto const sle = view.read(keylet::account(issuer))) - return sle->isFlag (lsfGlobalFreeze); + return sle->isFlag(lsfGlobalFreeze); return false; } // Can the specified account spend the specified currency issued by // the specified issuer or does the freeze flag prohibit it? bool -isFrozen (ReadView const& view, AccountID const& account, - Currency const& currency, AccountID const& issuer) +isFrozen( + ReadView const& view, + AccountID const& account, + Currency const& currency, + AccountID const& issuer) { - if (isXRP (currency)) + if (isXRP(currency)) return false; - auto sle = - view.read(keylet::account(issuer)); - if (sle && sle->isFlag (lsfGlobalFreeze)) + auto sle = view.read(keylet::account(issuer)); + if (sle && sle->isFlag(lsfGlobalFreeze)) return true; if (issuer != account) { // Check if the issuer froze the line - sle = view.read(keylet::line( - account, issuer, currency)); - if (sle && sle->isFlag( - (issuer > account) ? - lsfHighFreeze : lsfLowFreeze)) + sle = view.read(keylet::line(account, issuer, currency)); + if (sle && + sle->isFlag((issuer > account) ? lsfHighFreeze : lsfLowFreeze)) return true; } return false; } STAmount -accountHolds (ReadView const& view, - AccountID const& account, Currency const& currency, - AccountID const& issuer, FreezeHandling zeroIfFrozen, - beast::Journal j) +accountHolds( + ReadView const& view, + AccountID const& account, + Currency const& currency, + AccountID const& issuer, + FreezeHandling zeroIfFrozen, + beast::Journal j) { STAmount amount; if (isXRP(currency)) { - return {xrpLiquid (view, account, 0, j)}; + return {xrpLiquid(view, account, 0, j)}; } // IOU: Return balance on trust line modulo freeze - auto const sle = view.read(keylet::line( - account, issuer, currency)); - if (! sle) + auto const sle = view.read(keylet::line(account, issuer, currency)); + if (!sle) { - amount.clear ({currency, issuer}); + amount.clear({currency, issuer}); } - else if ((zeroIfFrozen == fhZERO_IF_FROZEN) && + else if ( + (zeroIfFrozen == fhZERO_IF_FROZEN) && isFrozen(view, account, currency, issuer)) { - amount.clear (Issue (currency, issuer)); + amount.clear(Issue(currency, issuer)); } else { - amount = sle->getFieldAmount (sfBalance); + amount = sle->getFieldAmount(sfBalance); if (account > issuer) { // Put balance in account terms. amount.negate(); } - amount.setIssuer (issuer); + amount.setIssuer(issuer); } - JLOG (j.trace()) << "accountHolds:" << - " account=" << to_string (account) << - " amount=" << amount.getFullText (); + JLOG(j.trace()) << "accountHolds:" + << " account=" << to_string(account) + << " amount=" << amount.getFullText(); - return view.balanceHook( - account, issuer, amount); + return view.balanceHook(account, issuer, amount); } STAmount -accountFunds (ReadView const& view, AccountID const& id, - STAmount const& saDefault, FreezeHandling freezeHandling, - beast::Journal j) +accountFunds( + ReadView const& view, + AccountID const& id, + STAmount const& saDefault, + FreezeHandling freezeHandling, + beast::Journal j) { STAmount saFunds; - if (!saDefault.native () && - saDefault.getIssuer () == id) + if (!saDefault.native() && saDefault.getIssuer() == id) { saFunds = saDefault; - JLOG (j.trace()) << "accountFunds:" << - " account=" << to_string (id) << - " saDefault=" << saDefault.getFullText () << - " SELF-FUNDED"; + JLOG(j.trace()) << "accountFunds:" + << " account=" << to_string(id) + << " saDefault=" << saDefault.getFullText() + << " SELF-FUNDED"; } else { - saFunds = accountHolds(view, id, - saDefault.getCurrency(), saDefault.getIssuer(), - freezeHandling, j); - JLOG (j.trace()) << "accountFunds:" << - " account=" << to_string (id) << - " saDefault=" << saDefault.getFullText () << - " saFunds=" << saFunds.getFullText (); + saFunds = accountHolds( + view, + id, + saDefault.getCurrency(), + saDefault.getIssuer(), + freezeHandling, + j); + JLOG(j.trace()) << "accountFunds:" + << " account=" << to_string(id) + << " saDefault=" << saDefault.getFullText() + << " saFunds=" << saFunds.getFullText(); } return saFunds; } @@ -164,13 +173,14 @@ accountFunds (ReadView const& view, AccountID const& id, // If id != boost.none, then do error reporting. // // Returns adjusted owner count. -static -std::uint32_t -confineOwnerCount (std::uint32_t current, std::int32_t adjustment, +static std::uint32_t +confineOwnerCount( + std::uint32_t current, + std::int32_t adjustment, boost::optional const& id = boost::none, - beast::Journal j = beast::Journal {beast::Journal::getNullSink()}) + beast::Journal j = beast::Journal{beast::Journal::getNullSink()}) { - std::uint32_t adjusted {current + adjustment}; + std::uint32_t adjusted{current + adjustment}; if (adjustment > 0) { // Overflow is well defined on unsigned @@ -178,11 +188,10 @@ confineOwnerCount (std::uint32_t current, std::int32_t adjustment, { if (id) { - JLOG (j.fatal()) << - "Account " << *id << - " owner count exceeds max!"; + JLOG(j.fatal()) + << "Account " << *id << " owner count exceeds max!"; } - adjusted = std::numeric_limits::max (); + adjusted = std::numeric_limits::max(); } } else @@ -192,9 +201,8 @@ confineOwnerCount (std::uint32_t current, std::int32_t adjustment, { if (id) { - JLOG (j.fatal()) << - "Account " << *id << - " owner count set below 0!"; + JLOG(j.fatal()) + << "Account " << *id << " owner count set below 0!"; } adjusted = 0; assert(!id); @@ -204,81 +212,84 @@ confineOwnerCount (std::uint32_t current, std::int32_t adjustment, } XRPAmount -xrpLiquid (ReadView const& view, AccountID const& id, - std::int32_t ownerCountAdj, beast::Journal j) +xrpLiquid( + ReadView const& view, + AccountID const& id, + std::int32_t ownerCountAdj, + beast::Journal j) { auto const sle = view.read(keylet::account(id)); if (sle == nullptr) return beast::zero; // Return balance minus reserve - std::uint32_t const ownerCount = confineOwnerCount ( - view.ownerCountHook (id, sle->getFieldU32 (sfOwnerCount)), - ownerCountAdj); + std::uint32_t const ownerCount = confineOwnerCount( + view.ownerCountHook(id, sle->getFieldU32(sfOwnerCount)), ownerCountAdj); - auto const reserve = - view.fees().accountReserve(ownerCount); + auto const reserve = view.fees().accountReserve(ownerCount); - auto const fullBalance = - sle->getFieldAmount(sfBalance); + auto const fullBalance = sle->getFieldAmount(sfBalance); auto const balance = view.balanceHook(id, xrpAccount(), fullBalance); STAmount amount = balance - reserve; if (balance < reserve) - amount.clear (); + amount.clear(); - JLOG (j.trace()) << "accountHolds:" << - " account=" << to_string (id) << - " amount=" << amount.getFullText() << - " fullBalance=" << fullBalance.getFullText() << - " balance=" << balance.getFullText() << - " reserve=" << reserve << - " ownerCount=" << ownerCount << - " ownerCountAdj=" << ownerCountAdj; + JLOG(j.trace()) << "accountHolds:" + << " account=" << to_string(id) + << " amount=" << amount.getFullText() + << " fullBalance=" << fullBalance.getFullText() + << " balance=" << balance.getFullText() + << " reserve=" << reserve << " ownerCount=" << ownerCount + << " ownerCountAdj=" << ownerCountAdj; return amount.xrp(); } void -forEachItem (ReadView const& view, AccountID const& id, +forEachItem( + ReadView const& view, + AccountID const& id, std::function const&)> f) { auto const root = keylet::ownerDir(id); auto pos = root; - for(;;) + for (;;) { auto sle = view.read(pos); - if (! sle) + if (!sle) return; // VFALCO NOTE We aren't checking field exists? for (auto const& key : sle->getFieldV256(sfIndexes)) f(view.read(keylet::child(key))); - auto const next = - sle->getFieldU64 (sfIndexNext); - if (! next) + auto const next = sle->getFieldU64(sfIndexNext); + if (!next) return; pos = keylet::page(root, next); } } bool -forEachItemAfter (ReadView const& view, AccountID const& id, - uint256 const& after, std::uint64_t const hint, - unsigned int limit, std::function< - bool (std::shared_ptr const&)> f) +forEachItemAfter( + ReadView const& view, + AccountID const& id, + uint256 const& after, + std::uint64_t const hint, + unsigned int limit, + std::function const&)> f) { auto const rootIndex = keylet::ownerDir(id); auto currentIndex = rootIndex; // If startAfter is not zero try jumping to that page using the hint - if (after.isNonZero ()) + if (after.isNonZero()) { auto const hintIndex = keylet::page(rootIndex, hint); auto hintDir = view.read(hintIndex); if (hintDir) { - for (auto const& key : hintDir->getFieldV256 (sfIndexes)) + for (auto const& key : hintDir->getFieldV256(sfIndexes)) { if (key == after) { @@ -293,23 +304,22 @@ forEachItemAfter (ReadView const& view, AccountID const& id, for (;;) { auto const ownerDir = view.read(currentIndex); - if (! ownerDir) + if (!ownerDir) return found; - for (auto const& key : ownerDir->getFieldV256 (sfIndexes)) + for (auto const& key : ownerDir->getFieldV256(sfIndexes)) { - if (! found) + if (!found) { if (key == after) found = true; } - else if (f (view.read(keylet::child(key))) && limit-- <= 1) + else if (f(view.read(keylet::child(key))) && limit-- <= 1) { return found; } } - auto const uNodeNext = - ownerDir->getFieldU64(sfIndexNext); + auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext); if (uNodeNext == 0) return found; currentIndex = keylet::page(rootIndex, uNodeNext); @@ -320,13 +330,12 @@ forEachItemAfter (ReadView const& view, AccountID const& id, for (;;) { auto const ownerDir = view.read(currentIndex); - if (! ownerDir) + if (!ownerDir) return true; - for (auto const& key : ownerDir->getFieldV256 (sfIndexes)) - if (f (view.read(keylet::child(key))) && limit-- <= 1) + for (auto const& key : ownerDir->getFieldV256(sfIndexes)) + if (f(view.read(keylet::child(key))) && limit-- <= 1) return true; - auto const uNodeNext = - ownerDir->getFieldU64 (sfIndexNext); + auto const uNodeNext = ownerDir->getFieldU64(sfIndexNext); if (uNodeNext == 0) return true; currentIndex = keylet::page(rootIndex, uNodeNext); @@ -335,33 +344,37 @@ forEachItemAfter (ReadView const& view, AccountID const& id, } Rate -transferRate (ReadView const& view, - AccountID const& issuer) +transferRate(ReadView const& view, AccountID const& issuer) { auto const sle = view.read(keylet::account(issuer)); - if (sle && sle->isFieldPresent (sfTransferRate)) - return Rate{ sle->getFieldU32 (sfTransferRate) }; + if (sle && sle->isFieldPresent(sfTransferRate)) + return Rate{sle->getFieldU32(sfTransferRate)}; return parityRate; } bool -areCompatible (ReadView const& validLedger, ReadView const& testLedger, - beast::Journal::Stream& s, const char* reason) +areCompatible( + ReadView const& validLedger, + ReadView const& testLedger, + beast::Journal::Stream& s, + const char* reason) { bool ret = true; if (validLedger.info().seq < testLedger.info().seq) { // valid -> ... -> test - auto hash = hashOfSeq (testLedger, validLedger.info().seq, - beast::Journal {beast::Journal::getNullSink()}); + auto hash = hashOfSeq( + testLedger, + validLedger.info().seq, + beast::Journal{beast::Journal::getNullSink()}); if (hash && (*hash != validLedger.info().hash)) { JLOG(s) << reason << " incompatible with valid ledger"; - JLOG(s) << "Hash(VSeq): " << to_string (*hash); + JLOG(s) << "Hash(VSeq): " << to_string(*hash); ret = false; } @@ -369,19 +382,22 @@ areCompatible (ReadView const& validLedger, ReadView const& testLedger, else if (validLedger.info().seq > testLedger.info().seq) { // test -> ... -> valid - auto hash = hashOfSeq (validLedger, testLedger.info().seq, - beast::Journal {beast::Journal::getNullSink()}); + auto hash = hashOfSeq( + validLedger, + testLedger.info().seq, + beast::Journal{beast::Journal::getNullSink()}); if (hash && (*hash != testLedger.info().hash)) { JLOG(s) << reason << " incompatible preceding ledger"; - JLOG(s) << "Hash(NSeq): " << to_string (*hash); + JLOG(s) << "Hash(NSeq): " << to_string(*hash); ret = false; } } - else if ((validLedger.info().seq == testLedger.info().seq) && - (validLedger.info().hash != testLedger.info().hash)) + else if ( + (validLedger.info().seq == testLedger.info().seq) && + (validLedger.info().hash != testLedger.info().hash)) { // Same sequence number, different hash JLOG(s) << reason << " incompatible ledger"; @@ -389,37 +405,45 @@ areCompatible (ReadView const& validLedger, ReadView const& testLedger, ret = false; } - if (! ret) + if (!ret) { - JLOG(s) << "Val: " << validLedger.info().seq << - " " << to_string (validLedger.info().hash); + JLOG(s) << "Val: " << validLedger.info().seq << " " + << to_string(validLedger.info().hash); - JLOG(s) << "New: " << testLedger.info().seq << - " " << to_string (testLedger.info().hash); + JLOG(s) << "New: " << testLedger.info().seq << " " + << to_string(testLedger.info().hash); } return ret; } -bool areCompatible (uint256 const& validHash, LedgerIndex validIndex, - ReadView const& testLedger, beast::Journal::Stream& s, const char* reason) +bool +areCompatible( + uint256 const& validHash, + LedgerIndex validIndex, + ReadView const& testLedger, + beast::Journal::Stream& s, + const char* reason) { bool ret = true; if (testLedger.info().seq > validIndex) { // Ledger we are testing follows last valid ledger - auto hash = hashOfSeq (testLedger, validIndex, - beast::Journal {beast::Journal::getNullSink()}); + auto hash = hashOfSeq( + testLedger, + validIndex, + beast::Journal{beast::Journal::getNullSink()}); if (hash && (*hash != validHash)) { JLOG(s) << reason << " incompatible following ledger"; - JLOG(s) << "Hash(VSeq): " << to_string (*hash); + JLOG(s) << "Hash(VSeq): " << to_string(*hash); ret = false; } } - else if ((validIndex == testLedger.info().seq) && + else if ( + (validIndex == testLedger.info().seq) && (testLedger.info().hash != validHash)) { JLOG(s) << reason << " incompatible ledger"; @@ -427,99 +451,95 @@ bool areCompatible (uint256 const& validHash, LedgerIndex validIndex, ret = false; } - if (! ret) + if (!ret) { - JLOG(s) << "Val: " << validIndex << - " " << to_string (validHash); + JLOG(s) << "Val: " << validIndex << " " << to_string(validHash); - JLOG(s) << "New: " << testLedger.info().seq << - " " << to_string (testLedger.info().hash); + JLOG(s) << "New: " << testLedger.info().seq << " " + << to_string(testLedger.info().hash); } return ret; } bool -dirIsEmpty (ReadView const& view, - Keylet const& k) +dirIsEmpty(ReadView const& view, Keylet const& k) { auto const sleNode = view.read(k); - if (! sleNode) + if (!sleNode) return true; - if (! sleNode->getFieldV256 (sfIndexes).empty ()) + if (!sleNode->getFieldV256(sfIndexes).empty()) return false; // The first page of a directory may legitimately be empty even if there // are other pages (the first page is the anchor page) so check to see if // there is another page. If there is, the directory isn't empty. - return sleNode->getFieldU64 (sfIndexNext) == 0; + return sleNode->getFieldU64(sfIndexNext) == 0; } bool -cdirFirst (ReadView const& view, - uint256 const& uRootIndex, // --> Root of directory. - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-- next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +cdirFirst( + ReadView const& view, + uint256 const& uRootIndex, // --> Root of directory. + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-- next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j) { sleNode = view.read(keylet::page(uRootIndex)); - uDirEntry = 0; - assert (sleNode); // Never probe for directories. - return cdirNext (view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); + uDirEntry = 0; + assert(sleNode); // Never probe for directories. + return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); } bool -cdirNext (ReadView const& view, - uint256 const& uRootIndex, // --> Root of directory - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-> next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +cdirNext( + ReadView const& view, + uint256 const& uRootIndex, // --> Root of directory + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-> next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j) { - auto const& svIndexes = sleNode->getFieldV256 (sfIndexes); - assert (uDirEntry <= svIndexes.size ()); - if (uDirEntry >= svIndexes.size ()) + auto const& svIndexes = sleNode->getFieldV256(sfIndexes); + assert(uDirEntry <= svIndexes.size()); + if (uDirEntry >= svIndexes.size()) { - auto const uNodeNext = - sleNode->getFieldU64 (sfIndexNext); - if (! uNodeNext) + auto const uNodeNext = sleNode->getFieldU64(sfIndexNext); + if (!uNodeNext) { - uEntryIndex.zero (); + uEntryIndex.zero(); return false; } - auto const sleNext = view.read( - keylet::page(uRootIndex, uNodeNext)); + auto const sleNext = view.read(keylet::page(uRootIndex, uNodeNext)); uDirEntry = 0; if (!sleNext) { // This should never happen - JLOG (j.fatal()) - << "Corrupt directory: index:" - << uRootIndex << " next:" << uNodeNext; + JLOG(j.fatal()) << "Corrupt directory: index:" << uRootIndex + << " next:" << uNodeNext; return false; } sleNode = sleNext; - return cdirNext (view, uRootIndex, - sleNode, uDirEntry, uEntryIndex, j); + return cdirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); } uEntryIndex = svIndexes[uDirEntry++]; - JLOG (j.trace()) << "dirNext:" << - " uDirEntry=" << uDirEntry << - " uEntryIndex=" << uEntryIndex; + JLOG(j.trace()) << "dirNext:" + << " uDirEntry=" << uDirEntry + << " uEntryIndex=" << uEntryIndex; return true; } -std::set -getEnabledAmendments (ReadView const& view) +std::set +getEnabledAmendments(ReadView const& view) { std::set amendments; if (auto const sle = view.read(keylet::amendments())) { - if (sle->isFieldPresent (sfAmendments)) + if (sle->isFieldPresent(sfAmendments)) { - auto const& v = sle->getFieldV256 (sfAmendments); - amendments.insert (v.begin(), v.end()); + auto const& v = sle->getFieldV256(sfAmendments); + amendments.insert(v.begin(), v.end()); } } @@ -527,21 +547,21 @@ getEnabledAmendments (ReadView const& view) } majorityAmendments_t -getMajorityAmendments (ReadView const& view) +getMajorityAmendments(ReadView const& view) { majorityAmendments_t ret; if (auto const sle = view.read(keylet::amendments())) { - if (sle->isFieldPresent (sfMajorities)) + if (sle->isFieldPresent(sfMajorities)) { using tp = NetClock::time_point; using d = tp::duration; - auto const majorities = sle->getFieldArray (sfMajorities); + auto const majorities = sle->getFieldArray(sfMajorities); for (auto const& m : majorities) - ret[m.getFieldH256 (sfAmendment)] = + ret[m.getFieldH256(sfAmendment)] = tp(d(m.getFieldU32(sfCloseTime))); } } @@ -550,15 +570,13 @@ getMajorityAmendments (ReadView const& view) } boost::optional -hashOfSeq (ReadView const& ledger, LedgerIndex seq, - beast::Journal journal) +hashOfSeq(ReadView const& ledger, LedgerIndex seq, beast::Journal journal) { // Easy cases... if (seq > ledger.seq()) { - JLOG (journal.warn()) << - "Can't get seq " << seq << - " from " << ledger.seq() << " future"; + JLOG(journal.warn()) + << "Can't get seq " << seq << " from " << ledger.seq() << " future"; return boost::none; } if (seq == ledger.seq()) @@ -569,53 +587,48 @@ hashOfSeq (ReadView const& ledger, LedgerIndex seq, if (int diff = ledger.seq() - seq; diff <= 256) { // Within 256... - auto const hashIndex = - ledger.read(keylet::skip()); + auto const hashIndex = ledger.read(keylet::skip()); if (hashIndex) { - assert (hashIndex->getFieldU32 (sfLastLedgerSequence) == - (ledger.seq() - 1)); - STVector256 vec = hashIndex->getFieldV256 (sfHashes); - if (vec.size () >= diff) - return vec[vec.size () - diff]; - JLOG (journal.warn()) << - "Ledger " << ledger.seq() << - " missing hash for " << seq << - " (" << vec.size () << "," << diff << ")"; + assert( + hashIndex->getFieldU32(sfLastLedgerSequence) == + (ledger.seq() - 1)); + STVector256 vec = hashIndex->getFieldV256(sfHashes); + if (vec.size() >= diff) + return vec[vec.size() - diff]; + JLOG(journal.warn()) + << "Ledger " << ledger.seq() << " missing hash for " << seq + << " (" << vec.size() << "," << diff << ")"; } else { - JLOG (journal.warn()) << - "Ledger " << ledger.seq() << - ":" << ledger.info().hash << " missing normal list"; + JLOG(journal.warn()) + << "Ledger " << ledger.seq() << ":" << ledger.info().hash + << " missing normal list"; } } if ((seq & 0xff) != 0) { - JLOG (journal.debug()) << - "Can't get seq " << seq << - " from " << ledger.seq() << " past"; + JLOG(journal.debug()) + << "Can't get seq " << seq << " from " << ledger.seq() << " past"; return boost::none; } // in skiplist - auto const hashIndex = - ledger.read(keylet::skip(seq)); + auto const hashIndex = ledger.read(keylet::skip(seq)); if (hashIndex) { - auto const lastSeq = - hashIndex->getFieldU32 (sfLastLedgerSequence); - assert (lastSeq >= seq); - assert ((lastSeq & 0xff) == 0); + auto const lastSeq = hashIndex->getFieldU32(sfLastLedgerSequence); + assert(lastSeq >= seq); + assert((lastSeq & 0xff) == 0); auto const diff = (lastSeq - seq) >> 8; - STVector256 vec = hashIndex->getFieldV256 (sfHashes); - if (vec.size () > diff) - return vec[vec.size () - diff - 1]; + STVector256 vec = hashIndex->getFieldV256(sfHashes); + if (vec.size() > diff) + return vec[vec.size() - diff - 1]; } - JLOG (journal.warn()) << - "Can't get seq " << seq << - " from " << ledger.seq() << " error"; + JLOG(journal.warn()) << "Can't get seq " << seq << " from " << ledger.seq() + << " error"; return boost::none; } @@ -626,91 +639,91 @@ hashOfSeq (ReadView const& ledger, LedgerIndex seq, //------------------------------------------------------------------------------ void -adjustOwnerCount (ApplyView& view, +adjustOwnerCount( + ApplyView& view, std::shared_ptr const& sle, - std::int32_t amount, beast::Journal j) + std::int32_t amount, + beast::Journal j) { - if (! sle) + if (!sle) return; assert(amount != 0); - std::uint32_t const current {sle->getFieldU32 (sfOwnerCount)}; + std::uint32_t const current{sle->getFieldU32(sfOwnerCount)}; AccountID const id = (*sle)[sfAccount]; - std::uint32_t const adjusted = confineOwnerCount (current, amount, id, j); - view.adjustOwnerCountHook (id, current, adjusted); - sle->setFieldU32 (sfOwnerCount, adjusted); + std::uint32_t const adjusted = confineOwnerCount(current, amount, id, j); + view.adjustOwnerCountHook(id, current, adjusted); + sle->setFieldU32(sfOwnerCount, adjusted); view.update(sle); } bool -dirFirst (ApplyView& view, - uint256 const& uRootIndex, // --> Root of directory. - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-- next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +dirFirst( + ApplyView& view, + uint256 const& uRootIndex, // --> Root of directory. + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-- next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j) { sleNode = view.peek(keylet::page(uRootIndex)); - uDirEntry = 0; - assert (sleNode); // Never probe for directories. - return dirNext (view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); + uDirEntry = 0; + assert(sleNode); // Never probe for directories. + return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); } bool -dirNext (ApplyView& view, - uint256 const& uRootIndex, // --> Root of directory - std::shared_ptr& sleNode, // <-> current node - unsigned int& uDirEntry, // <-> next entry - uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. +dirNext( + ApplyView& view, + uint256 const& uRootIndex, // --> Root of directory + std::shared_ptr& sleNode, // <-> current node + unsigned int& uDirEntry, // <-> next entry + uint256& uEntryIndex, // <-- The entry, if available. Otherwise, zero. beast::Journal j) { - auto const& svIndexes = sleNode->getFieldV256 (sfIndexes); - assert (uDirEntry <= svIndexes.size ()); - if (uDirEntry >= svIndexes.size ()) + auto const& svIndexes = sleNode->getFieldV256(sfIndexes); + assert(uDirEntry <= svIndexes.size()); + if (uDirEntry >= svIndexes.size()) { - auto const uNodeNext = - sleNode->getFieldU64 (sfIndexNext); - if (! uNodeNext) + auto const uNodeNext = sleNode->getFieldU64(sfIndexNext); + if (!uNodeNext) { - uEntryIndex.zero (); + uEntryIndex.zero(); return false; } - auto const sleNext = view.peek( - keylet::page(uRootIndex, uNodeNext)); + auto const sleNext = view.peek(keylet::page(uRootIndex, uNodeNext)); uDirEntry = 0; if (!sleNext) { // This should never happen - JLOG (j.fatal()) - << "Corrupt directory: index:" - << uRootIndex << " next:" << uNodeNext; + JLOG(j.fatal()) << "Corrupt directory: index:" << uRootIndex + << " next:" << uNodeNext; return false; } sleNode = sleNext; - return dirNext (view, uRootIndex, - sleNode, uDirEntry, uEntryIndex, j); + return dirNext(view, uRootIndex, sleNode, uDirEntry, uEntryIndex, j); } uEntryIndex = svIndexes[uDirEntry++]; - JLOG (j.trace()) << "dirNext:" << - " uDirEntry=" << uDirEntry << - " uEntryIndex=" << uEntryIndex; + JLOG(j.trace()) << "dirNext:" + << " uDirEntry=" << uDirEntry + << " uEntryIndex=" << uEntryIndex; return true; } -std::function +std::function describeOwnerDir(AccountID const& account) { - return [&account](std::shared_ptr const& sle) - { + return [&account](std::shared_ptr const& sle) { (*sle)[sfOwner] = account; }; } boost::optional -dirAdd (ApplyView& view, - Keylet const& dir, - uint256 const& uLedgerIndex, - bool strictOrder, - std::function fDescriber, +dirAdd( + ApplyView& view, + Keylet const& dir, + uint256 const& uLedgerIndex, + bool strictOrder, + std::function fDescriber, beast::Journal j) { if (strictOrder) @@ -720,77 +733,89 @@ dirAdd (ApplyView& view, } TER -trustCreate (ApplyView& view, - const bool bSrcHigh, - AccountID const& uSrcAccountID, - AccountID const& uDstAccountID, - uint256 const& uIndex, // --> ripple state entry - SLE::ref sleAccount, // --> the account being set. - const bool bAuth, // --> authorize account. - const bool bNoRipple, // --> others cannot ripple through - const bool bFreeze, // --> funds cannot leave - STAmount const& saBalance, // --> balance of account being set. - // Issuer should be noAccount() - STAmount const& saLimit, // --> limit for account being set. - // Issuer should be the account being set. +trustCreate( + ApplyView& view, + const bool bSrcHigh, + AccountID const& uSrcAccountID, + AccountID const& uDstAccountID, + uint256 const& uIndex, // --> ripple state entry + SLE::ref sleAccount, // --> the account being set. + const bool bAuth, // --> authorize account. + const bool bNoRipple, // --> others cannot ripple through + const bool bFreeze, // --> funds cannot leave + STAmount const& saBalance, // --> balance of account being set. + // Issuer should be noAccount() + STAmount const& saLimit, // --> limit for account being set. + // Issuer should be the account being set. std::uint32_t uQualityIn, std::uint32_t uQualityOut, beast::Journal j) { - JLOG (j.trace()) - << "trustCreate: " << to_string (uSrcAccountID) << ", " - << to_string (uDstAccountID) << ", " << saBalance.getFullText (); + JLOG(j.trace()) << "trustCreate: " << to_string(uSrcAccountID) << ", " + << to_string(uDstAccountID) << ", " + << saBalance.getFullText(); - auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID; - auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID; + auto const& uLowAccountID = !bSrcHigh ? uSrcAccountID : uDstAccountID; + auto const& uHighAccountID = bSrcHigh ? uSrcAccountID : uDstAccountID; - auto const sleRippleState = std::make_shared( - ltRIPPLE_STATE, uIndex); - view.insert (sleRippleState); + auto const sleRippleState = std::make_shared(ltRIPPLE_STATE, uIndex); + view.insert(sleRippleState); - auto lowNode = dirAdd (view, keylet::ownerDir (uLowAccountID), - sleRippleState->key(), false, describeOwnerDir (uLowAccountID), j); + auto lowNode = dirAdd( + view, + keylet::ownerDir(uLowAccountID), + sleRippleState->key(), + false, + describeOwnerDir(uLowAccountID), + j); if (!lowNode) return tecDIR_FULL; - auto highNode = dirAdd (view, keylet::ownerDir (uHighAccountID), - sleRippleState->key(), false, describeOwnerDir (uHighAccountID), j); + auto highNode = dirAdd( + view, + keylet::ownerDir(uHighAccountID), + sleRippleState->key(), + false, + describeOwnerDir(uHighAccountID), + j); if (!highNode) return tecDIR_FULL; - const bool bSetDst = saLimit.getIssuer () == uDstAccountID; + const bool bSetDst = saLimit.getIssuer() == uDstAccountID; const bool bSetHigh = bSrcHigh ^ bSetDst; - assert (sleAccount); - if (! sleAccount) + assert(sleAccount); + if (!sleAccount) return tefINTERNAL; - assert (sleAccount->getAccountID (sfAccount) == + assert( + sleAccount->getAccountID(sfAccount) == (bSetHigh ? uHighAccountID : uLowAccountID)); - auto const slePeer = view.peek (keylet::account( - bSetHigh ? uLowAccountID : uHighAccountID)); - if (! slePeer) + auto const slePeer = + view.peek(keylet::account(bSetHigh ? uLowAccountID : uHighAccountID)); + if (!slePeer) return tecNO_TARGET; // Remember deletion hints. - sleRippleState->setFieldU64 (sfLowNode, *lowNode); - sleRippleState->setFieldU64 (sfHighNode, *highNode); + sleRippleState->setFieldU64(sfLowNode, *lowNode); + sleRippleState->setFieldU64(sfHighNode, *highNode); - sleRippleState->setFieldAmount ( + sleRippleState->setFieldAmount( bSetHigh ? sfHighLimit : sfLowLimit, saLimit); - sleRippleState->setFieldAmount ( + sleRippleState->setFieldAmount( bSetHigh ? sfLowLimit : sfHighLimit, - STAmount ({saBalance.getCurrency (), - bSetDst ? uSrcAccountID : uDstAccountID})); + STAmount( + {saBalance.getCurrency(), + bSetDst ? uSrcAccountID : uDstAccountID})); if (uQualityIn) - sleRippleState->setFieldU32 ( + sleRippleState->setFieldU32( bSetHigh ? sfHighQualityIn : sfLowQualityIn, uQualityIn); if (uQualityOut) - sleRippleState->setFieldU32 ( + sleRippleState->setFieldU32( bSetHigh ? sfHighQualityOut : sfLowQualityOut, uQualityOut); std::uint32_t uFlags = bSetHigh ? lsfHighReserve : lsfLowReserve; @@ -814,33 +839,34 @@ trustCreate (ApplyView& view, uFlags |= (bSetHigh ? lsfLowNoRipple : lsfHighNoRipple); } - sleRippleState->setFieldU32 (sfFlags, uFlags); + sleRippleState->setFieldU32(sfFlags, uFlags); adjustOwnerCount(view, sleAccount, 1, j); // ONLY: Create ripple balance. - sleRippleState->setFieldAmount (sfBalance, bSetHigh ? -saBalance : saBalance); + sleRippleState->setFieldAmount( + sfBalance, bSetHigh ? -saBalance : saBalance); - view.creditHook (uSrcAccountID, - uDstAccountID, saBalance, saBalance.zeroed()); + view.creditHook( + uSrcAccountID, uDstAccountID, saBalance, saBalance.zeroed()); return tesSUCCESS; } TER -trustDelete (ApplyView& view, +trustDelete( + ApplyView& view, std::shared_ptr const& sleRippleState, - AccountID const& uLowAccountID, - AccountID const& uHighAccountID, - beast::Journal j) + AccountID const& uLowAccountID, + AccountID const& uHighAccountID, + beast::Journal j) { // Detect legacy dirs. - std::uint64_t uLowNode = sleRippleState->getFieldU64 (sfLowNode); - std::uint64_t uHighNode = sleRippleState->getFieldU64 (sfHighNode); + std::uint64_t uLowNode = sleRippleState->getFieldU64(sfLowNode); + std::uint64_t uHighNode = sleRippleState->getFieldU64(sfHighNode); - JLOG (j.trace()) - << "trustDelete: Deleting ripple line: low"; + JLOG(j.trace()) << "trustDelete: Deleting ripple line: low"; - if (! view.dirRemove( + if (!view.dirRemove( keylet::ownerDir(uLowAccountID), uLowNode, sleRippleState->key(), @@ -849,10 +875,9 @@ trustDelete (ApplyView& view, return tefBAD_LEDGER; } - JLOG (j.trace()) - << "trustDelete: Deleting ripple line: high"; + JLOG(j.trace()) << "trustDelete: Deleting ripple line: high"; - if (! view.dirRemove( + if (!view.dirRemove( keylet::ownerDir(uHighAccountID), uHighNode, sleRippleState->key(), @@ -861,45 +886,42 @@ trustDelete (ApplyView& view, return tefBAD_LEDGER; } - JLOG (j.trace()) << "trustDelete: Deleting ripple line: state"; + JLOG(j.trace()) << "trustDelete: Deleting ripple line: state"; view.erase(sleRippleState); return tesSUCCESS; } TER -offerDelete (ApplyView& view, - std::shared_ptr const& sle, - beast::Journal j) +offerDelete(ApplyView& view, std::shared_ptr const& sle, beast::Journal j) { - if (! sle) + if (!sle) return tesSUCCESS; auto offerIndex = sle->key(); - auto owner = sle->getAccountID (sfAccount); + auto owner = sle->getAccountID(sfAccount); // Detect legacy directories. - uint256 uDirectory = sle->getFieldH256 (sfBookDirectory); + uint256 uDirectory = sle->getFieldH256(sfBookDirectory); - if (! view.dirRemove( - keylet::ownerDir(owner), - sle->getFieldU64(sfOwnerNode), - offerIndex, - false)) + if (!view.dirRemove( + keylet::ownerDir(owner), + sle->getFieldU64(sfOwnerNode), + offerIndex, + false)) { return tefBAD_LEDGER; } - if (! view.dirRemove( - keylet::page(uDirectory), - sle->getFieldU64(sfBookNode), - offerIndex, - false)) + if (!view.dirRemove( + keylet::page(uDirectory), + sle->getFieldU64(sfBookNode), + offerIndex, + false)) { return tefBAD_LEDGER; } - adjustOwnerCount(view, view.peek( - keylet::account(owner)), -1, j); + adjustOwnerCount(view, view.peek(keylet::account(owner)), -1, j); view.erase(sle); @@ -911,52 +933,55 @@ offerDelete (ApplyView& view, // - Create trust line if needed. // --> bCheckIssuer : normally require issuer to be involved. TER -rippleCredit (ApplyView& view, - AccountID const& uSenderID, AccountID const& uReceiverID, - STAmount const& saAmount, bool bCheckIssuer, +rippleCredit( + ApplyView& view, + AccountID const& uSenderID, + AccountID const& uReceiverID, + STAmount const& saAmount, + bool bCheckIssuer, beast::Journal j) { - AccountID const& issuer = saAmount.getIssuer (); - Currency const& currency = saAmount.getCurrency (); + AccountID const& issuer = saAmount.getIssuer(); + Currency const& currency = saAmount.getCurrency(); // Make sure issuer is involved. - assert ( - !bCheckIssuer || uSenderID == issuer || uReceiverID == issuer); - (void) issuer; + assert(!bCheckIssuer || uSenderID == issuer || uReceiverID == issuer); + (void)issuer; // Disallow sending to self. - assert (uSenderID != uReceiverID); + assert(uSenderID != uReceiverID); bool const bSenderHigh = uSenderID > uReceiverID; - uint256 const uIndex = getRippleStateIndex ( - uSenderID, uReceiverID, currency); - auto const sleRippleState = view.peek (keylet::line(uIndex)); + uint256 const uIndex = + getRippleStateIndex(uSenderID, uReceiverID, currency); + auto const sleRippleState = view.peek(keylet::line(uIndex)); TER terResult; - assert (!isXRP (uSenderID) && uSenderID != noAccount()); - assert (!isXRP (uReceiverID) && uReceiverID != noAccount()); + assert(!isXRP(uSenderID) && uSenderID != noAccount()); + assert(!isXRP(uReceiverID) && uReceiverID != noAccount()); if (!sleRippleState) { - STAmount const saReceiverLimit ({currency, uReceiverID}); - STAmount saBalance {saAmount}; + STAmount const saReceiverLimit({currency, uReceiverID}); + STAmount saBalance{saAmount}; - saBalance.setIssuer (noAccount()); + saBalance.setIssuer(noAccount()); - JLOG (j.debug()) << "rippleCredit: " - "create line: " << to_string (uSenderID) << - " -> " << to_string (uReceiverID) << - " : " << saAmount.getFullText (); + JLOG(j.debug()) << "rippleCredit: " + "create line: " + << to_string(uSenderID) << " -> " + << to_string(uReceiverID) << " : " + << saAmount.getFullText(); - auto const sleAccount = - view.peek(keylet::account(uReceiverID)); - if (! sleAccount) + auto const sleAccount = view.peek(keylet::account(uReceiverID)); + if (!sleAccount) return tefINTERNAL; bool const noRipple = (sleAccount->getFlags() & lsfDefaultRipple) == 0; - terResult = trustCreate (view, + terResult = trustCreate( + view, bSenderHigh, uSenderID, uReceiverID, @@ -973,25 +998,24 @@ rippleCredit (ApplyView& view, } else { - STAmount saBalance = sleRippleState->getFieldAmount (sfBalance); + STAmount saBalance = sleRippleState->getFieldAmount(sfBalance); if (bSenderHigh) - saBalance.negate (); // Put balance in sender terms. + saBalance.negate(); // Put balance in sender terms. - view.creditHook (uSenderID, uReceiverID, saAmount, saBalance); + view.creditHook(uSenderID, uReceiverID, saAmount, saBalance); STAmount const saBefore = saBalance; saBalance -= saAmount; - JLOG (j.trace()) << "rippleCredit: " << - to_string (uSenderID) << - " -> " << to_string (uReceiverID) << - " : before=" << saBefore.getFullText () << - " amount=" << saAmount.getFullText () << - " after=" << saBalance.getFullText (); + JLOG(j.trace()) << "rippleCredit: " << to_string(uSenderID) << " -> " + << to_string(uReceiverID) + << " : before=" << saBefore.getFullText() + << " amount=" << saAmount.getFullText() + << " after=" << saBalance.getFullText(); - std::uint32_t const uFlags (sleRippleState->getFieldU32 (sfFlags)); + std::uint32_t const uFlags(sleRippleState->getFieldU32(sfFlags)); bool bDelete = false; // YYY Could skip this if rippling in reverse. @@ -1001,52 +1025,58 @@ rippleCredit (ApplyView& view, // Sender is zero or negative. && (uFlags & (!bSenderHigh ? lsfLowReserve : lsfHighReserve)) // Sender reserve is set. - && static_cast (uFlags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) != - static_cast (view.read (keylet::account(uSenderID))->getFlags() & lsfDefaultRipple) - && !(uFlags & (!bSenderHigh ? lsfLowFreeze : lsfHighFreeze)) - && !sleRippleState->getFieldAmount ( + && + static_cast( + uFlags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) != + static_cast( + view.read(keylet::account(uSenderID))->getFlags() & + lsfDefaultRipple) && + !(uFlags & (!bSenderHigh ? lsfLowFreeze : lsfHighFreeze)) && + !sleRippleState->getFieldAmount( !bSenderHigh ? sfLowLimit : sfHighLimit) // Sender trust limit is 0. - && !sleRippleState->getFieldU32 ( - !bSenderHigh ? sfLowQualityIn : sfHighQualityIn) + && !sleRippleState->getFieldU32( + !bSenderHigh ? sfLowQualityIn : sfHighQualityIn) // Sender quality in is 0. - && !sleRippleState->getFieldU32 ( - !bSenderHigh ? sfLowQualityOut : sfHighQualityOut)) - // Sender quality out is 0. + && !sleRippleState->getFieldU32( + !bSenderHigh ? sfLowQualityOut : sfHighQualityOut)) + // Sender quality out is 0. { // Clear the reserve of the sender, possibly delete the line! - adjustOwnerCount(view, - view.peek(keylet::account(uSenderID)), -1, j); + adjustOwnerCount( + view, view.peek(keylet::account(uSenderID)), -1, j); // Clear reserve flag. - sleRippleState->setFieldU32 ( + sleRippleState->setFieldU32( sfFlags, uFlags & (!bSenderHigh ? ~lsfLowReserve : ~lsfHighReserve)); // Balance is zero, receiver reserve is clear. - bDelete = !saBalance // Balance is zero. + bDelete = !saBalance // Balance is zero. && !(uFlags & (bSenderHigh ? lsfLowReserve : lsfHighReserve)); // Receiver reserve is clear. } if (bSenderHigh) - saBalance.negate (); + saBalance.negate(); // Want to reflect balance to zero even if we are deleting line. - sleRippleState->setFieldAmount (sfBalance, saBalance); + sleRippleState->setFieldAmount(sfBalance, saBalance); // ONLY: Adjust ripple balance. if (bDelete) { - terResult = trustDelete (view, + terResult = trustDelete( + view, sleRippleState, bSenderHigh ? uReceiverID : uSenderID, - !bSenderHigh ? uReceiverID : uSenderID, j); + !bSenderHigh ? uReceiverID : uSenderID, + j); } else { - view.update (sleRippleState); - terResult = tesSUCCESS; + view.update(sleRippleState); + terResult = tesSUCCESS; } } @@ -1056,21 +1086,25 @@ rippleCredit (ApplyView& view, // Send regardless of limits. // --> saAmount: Amount/currency/issuer to deliver to receiver. // <-- saActual: Amount actually cost. Sender pays fees. -static -TER -rippleSend (ApplyView& view, - AccountID const& uSenderID, AccountID const& uReceiverID, - STAmount const& saAmount, STAmount& saActual, beast::Journal j) +static TER +rippleSend( + ApplyView& view, + AccountID const& uSenderID, + AccountID const& uReceiverID, + STAmount const& saAmount, + STAmount& saActual, + beast::Journal j) { - auto const issuer = saAmount.getIssuer (); + auto const issuer = saAmount.getIssuer(); - assert (!isXRP (uSenderID) && !isXRP (uReceiverID)); - assert (uSenderID != uReceiverID); + assert(!isXRP(uSenderID) && !isXRP(uReceiverID)); + assert(uSenderID != uReceiverID); if (uSenderID == issuer || uReceiverID == issuer || issuer == noAccount()) { // Direct send: redeeming IOUs and/or sending own IOUs. - auto const ter = rippleCredit (view, uSenderID, uReceiverID, saAmount, false, j); + auto const ter = + rippleCredit(view, uSenderID, uReceiverID, saAmount, false, j); if (view.rules().enabled(featureDeletableAccounts) && ter != tesSUCCESS) return ter; saActual = saAmount; @@ -1081,28 +1115,30 @@ rippleSend (ApplyView& view, // Calculate the amount to transfer accounting // for any transfer fees: - saActual = multiply (saAmount, transferRate (view, issuer)); + saActual = multiply(saAmount, transferRate(view, issuer)); - JLOG (j.debug()) << "rippleSend> " << - to_string (uSenderID) << - " - > " << to_string (uReceiverID) << - " : deliver=" << saAmount.getFullText () << - " cost=" << saActual.getFullText (); + JLOG(j.debug()) << "rippleSend> " << to_string(uSenderID) << " - > " + << to_string(uReceiverID) + << " : deliver=" << saAmount.getFullText() + << " cost=" << saActual.getFullText(); - TER terResult = rippleCredit (view, issuer, uReceiverID, saAmount, true, j); + TER terResult = rippleCredit(view, issuer, uReceiverID, saAmount, true, j); if (tesSUCCESS == terResult) - terResult = rippleCredit (view, uSenderID, issuer, saActual, true, j); + terResult = rippleCredit(view, uSenderID, issuer, saActual, true, j); return terResult; } TER -accountSend (ApplyView& view, - AccountID const& uSenderID, AccountID const& uReceiverID, - STAmount const& saAmount, beast::Journal j) +accountSend( + ApplyView& view, + AccountID const& uSenderID, + AccountID const& uReceiverID, + STAmount const& saAmount, + beast::Journal j) { - assert (saAmount >= beast::zero); + assert(saAmount >= beast::zero); /* If we aren't sending anything or if the sender is the same as the * receiver then we don't need to do anything. @@ -1110,15 +1146,15 @@ accountSend (ApplyView& view, if (!saAmount || (uSenderID == uReceiverID)) return tesSUCCESS; - if (!saAmount.native ()) + if (!saAmount.native()) { STAmount saActual; - JLOG (j.trace()) << "accountSend: " << - to_string (uSenderID) << " -> " << to_string (uReceiverID) << - " : " << saAmount.getFullText (); + JLOG(j.trace()) << "accountSend: " << to_string(uSenderID) << " -> " + << to_string(uReceiverID) << " : " + << saAmount.getFullText(); - return rippleSend (view, uSenderID, uReceiverID, saAmount, saActual, j); + return rippleSend(view, uSenderID, uReceiverID, saAmount, saActual, j); } /* XRP send which does not check reserve and can do pure adjustment. @@ -1126,86 +1162,82 @@ accountSend (ApplyView& view, * setup is used during pathfinding and it is carefully controlled to * ensure that transfers are balanced. */ - TER terResult (tesSUCCESS); + TER terResult(tesSUCCESS); SLE::pointer sender = uSenderID != beast::zero - ? view.peek (keylet::account(uSenderID)) - : SLE::pointer (); + ? view.peek(keylet::account(uSenderID)) + : SLE::pointer(); SLE::pointer receiver = uReceiverID != beast::zero - ? view.peek (keylet::account(uReceiverID)) - : SLE::pointer (); + ? view.peek(keylet::account(uReceiverID)) + : SLE::pointer(); if (auto stream = j.trace()) { - std::string sender_bal ("-"); - std::string receiver_bal ("-"); + std::string sender_bal("-"); + std::string receiver_bal("-"); if (sender) - sender_bal = sender->getFieldAmount (sfBalance).getFullText (); + sender_bal = sender->getFieldAmount(sfBalance).getFullText(); if (receiver) - receiver_bal = receiver->getFieldAmount (sfBalance).getFullText (); + receiver_bal = receiver->getFieldAmount(sfBalance).getFullText(); - stream << "accountSend> " << - to_string (uSenderID) << " (" << sender_bal << - ") -> " << to_string (uReceiverID) << " (" << receiver_bal << - ") : " << saAmount.getFullText (); + stream << "accountSend> " << to_string(uSenderID) << " (" << sender_bal + << ") -> " << to_string(uReceiverID) << " (" << receiver_bal + << ") : " << saAmount.getFullText(); } if (sender) { - if (sender->getFieldAmount (sfBalance) < saAmount) + if (sender->getFieldAmount(sfBalance) < saAmount) { // VFALCO Its laborious to have to mutate the // TER based on params everywhere - terResult = view.open() - ? TER {telFAILED_PROCESSING} - : TER {tecFAILED_PROCESSING}; + terResult = view.open() ? TER{telFAILED_PROCESSING} + : TER{tecFAILED_PROCESSING}; } else { - auto const sndBal = sender->getFieldAmount (sfBalance); - view.creditHook (uSenderID, xrpAccount (), saAmount, sndBal); + auto const sndBal = sender->getFieldAmount(sfBalance); + view.creditHook(uSenderID, xrpAccount(), saAmount, sndBal); // Decrement XRP balance. - sender->setFieldAmount (sfBalance, sndBal - saAmount); - view.update (sender); + sender->setFieldAmount(sfBalance, sndBal - saAmount); + view.update(sender); } } if (tesSUCCESS == terResult && receiver) { // Increment XRP balance. - auto const rcvBal = receiver->getFieldAmount (sfBalance); - receiver->setFieldAmount (sfBalance, rcvBal + saAmount); - view.creditHook (xrpAccount (), uReceiverID, saAmount, -rcvBal); + auto const rcvBal = receiver->getFieldAmount(sfBalance); + receiver->setFieldAmount(sfBalance, rcvBal + saAmount); + view.creditHook(xrpAccount(), uReceiverID, saAmount, -rcvBal); - view.update (receiver); + view.update(receiver); } if (auto stream = j.trace()) { - std::string sender_bal ("-"); - std::string receiver_bal ("-"); + std::string sender_bal("-"); + std::string receiver_bal("-"); if (sender) - sender_bal = sender->getFieldAmount (sfBalance).getFullText (); + sender_bal = sender->getFieldAmount(sfBalance).getFullText(); if (receiver) - receiver_bal = receiver->getFieldAmount (sfBalance).getFullText (); + receiver_bal = receiver->getFieldAmount(sfBalance).getFullText(); - stream << "accountSend< " << - to_string (uSenderID) << " (" << sender_bal << - ") -> " << to_string (uReceiverID) << " (" << receiver_bal << - ") : " << saAmount.getFullText (); + stream << "accountSend< " << to_string(uSenderID) << " (" << sender_bal + << ") -> " << to_string(uReceiverID) << " (" << receiver_bal + << ") : " << saAmount.getFullText(); } return terResult; } -static -bool -updateTrustLine ( +static bool +updateTrustLine( ApplyView& view, SLE::pointer state, bool bSenderHigh, @@ -1216,10 +1248,10 @@ updateTrustLine ( { if (!state) return false; - std::uint32_t const flags (state->getFieldU32 (sfFlags)); + std::uint32_t const flags(state->getFieldU32(sfFlags)); - auto sle = view.peek (keylet::account(sender)); - if (! sle) + auto sle = view.peek(keylet::account(sender)); + if (!sle) return false; // YYY Could skip this if rippling in reverse. @@ -1229,29 +1261,28 @@ updateTrustLine ( // Sender is zero or negative. && (flags & (!bSenderHigh ? lsfLowReserve : lsfHighReserve)) // Sender reserve is set. - && static_cast (flags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) != - static_cast (sle->getFlags() & lsfDefaultRipple) - && !(flags & (!bSenderHigh ? lsfLowFreeze : lsfHighFreeze)) - && !state->getFieldAmount ( - !bSenderHigh ? sfLowLimit : sfHighLimit) + && static_cast( + flags & (!bSenderHigh ? lsfLowNoRipple : lsfHighNoRipple)) != + static_cast(sle->getFlags() & lsfDefaultRipple) && + !(flags & (!bSenderHigh ? lsfLowFreeze : lsfHighFreeze)) && + !state->getFieldAmount(!bSenderHigh ? sfLowLimit : sfHighLimit) // Sender trust limit is 0. - && !state->getFieldU32 ( - !bSenderHigh ? sfLowQualityIn : sfHighQualityIn) + && !state->getFieldU32(!bSenderHigh ? sfLowQualityIn : sfHighQualityIn) // Sender quality in is 0. - && !state->getFieldU32 ( - !bSenderHigh ? sfLowQualityOut : sfHighQualityOut)) - // Sender quality out is 0. + && + !state->getFieldU32(!bSenderHigh ? sfLowQualityOut : sfHighQualityOut)) + // Sender quality out is 0. { // VFALCO Where is the line being deleted? // Clear the reserve of the sender, possibly delete the line! adjustOwnerCount(view, sle, -1, j); // Clear reserve flag. - state->setFieldU32 (sfFlags, - flags & (!bSenderHigh ? ~lsfLowReserve : ~lsfHighReserve)); + state->setFieldU32( + sfFlags, flags & (!bSenderHigh ? ~lsfLowReserve : ~lsfHighReserve)); // Balance is zero, receiver reserve is clear. - if (!after // Balance is zero. + if (!after // Balance is zero. && !(flags & (bSenderHigh ? lsfLowReserve : lsfHighReserve))) return true; } @@ -1259,26 +1290,28 @@ updateTrustLine ( } TER -issueIOU (ApplyView& view, +issueIOU( + ApplyView& view, AccountID const& account, - STAmount const& amount, Issue const& issue, beast::Journal j) + STAmount const& amount, + Issue const& issue, + beast::Journal j) { - assert (!isXRP (account) && !isXRP (issue.account)); + assert(!isXRP(account) && !isXRP(issue.account)); // Consistency check - assert (issue == amount.issue ()); + assert(issue == amount.issue()); // Can't send to self! - assert (issue.account != account); + assert(issue.account != account); - JLOG (j.trace()) << "issueIOU: " << - to_string (account) << ": " << - amount.getFullText (); + JLOG(j.trace()) << "issueIOU: " << to_string(account) << ": " + << amount.getFullText(); bool bSenderHigh = issue.account > account; - uint256 const index = getRippleStateIndex ( - issue.account, account, issue.currency); - auto state = view.peek (keylet::line(index)); + uint256 const index = + getRippleStateIndex(issue.account, account, issue.currency); + auto state = view.peek(keylet::line(index)); if (!state) { @@ -1288,160 +1321,183 @@ issueIOU (ApplyView& view, STAmount const limit({issue.currency, account}); STAmount final_balance = amount; - final_balance.setIssuer (noAccount()); + final_balance.setIssuer(noAccount()); - auto const receiverAccount = view.peek (keylet::account(account)); - if (! receiverAccount) + auto const receiverAccount = view.peek(keylet::account(account)); + if (!receiverAccount) return tefINTERNAL; bool noRipple = (receiverAccount->getFlags() & lsfDefaultRipple) == 0; - return trustCreate (view, bSenderHigh, issue.account, account, index, - receiverAccount, false, noRipple, false, final_balance, limit, 0, 0, j); + return trustCreate( + view, + bSenderHigh, + issue.account, + account, + index, + receiverAccount, + false, + noRipple, + false, + final_balance, + limit, + 0, + 0, + j); } - STAmount final_balance = state->getFieldAmount (sfBalance); + STAmount final_balance = state->getFieldAmount(sfBalance); if (bSenderHigh) - final_balance.negate (); // Put balance in sender terms. + final_balance.negate(); // Put balance in sender terms. STAmount const start_balance = final_balance; final_balance -= amount; - auto const must_delete = updateTrustLine(view, state, bSenderHigh, issue.account, - start_balance, final_balance, j); + auto const must_delete = updateTrustLine( + view, + state, + bSenderHigh, + issue.account, + start_balance, + final_balance, + j); - view.creditHook (issue.account, account, amount, start_balance); + view.creditHook(issue.account, account, amount, start_balance); if (bSenderHigh) - final_balance.negate (); + final_balance.negate(); // Adjust the balance on the trust line if necessary. We do this even if we // are going to delete the line to reflect the correct balance at the time // of deletion. - state->setFieldAmount (sfBalance, final_balance); + state->setFieldAmount(sfBalance, final_balance); if (must_delete) - return trustDelete (view, state, + return trustDelete( + view, + state, bSenderHigh ? account : issue.account, - bSenderHigh ? issue.account : account, j); + bSenderHigh ? issue.account : account, + j); - view.update (state); + view.update(state); return tesSUCCESS; } TER -redeemIOU (ApplyView& view, +redeemIOU( + ApplyView& view, AccountID const& account, STAmount const& amount, Issue const& issue, beast::Journal j) { - assert (!isXRP (account) && !isXRP (issue.account)); + assert(!isXRP(account) && !isXRP(issue.account)); // Consistency check - assert (issue == amount.issue ()); + assert(issue == amount.issue()); // Can't send to self! - assert (issue.account != account); + assert(issue.account != account); - JLOG (j.trace()) << "redeemIOU: " << - to_string (account) << ": " << - amount.getFullText (); + JLOG(j.trace()) << "redeemIOU: " << to_string(account) << ": " + << amount.getFullText(); bool bSenderHigh = account > issue.account; - uint256 const index = getRippleStateIndex ( - account, issue.account, issue.currency); - auto state = view.peek (keylet::line(index)); + uint256 const index = + getRippleStateIndex(account, issue.account, issue.currency); + auto state = view.peek(keylet::line(index)); if (!state) { // In order to hold an IOU, a trust line *MUST* exist to track the // balance. If it doesn't, then something is very wrong. Don't try // to continue. - JLOG (j.fatal()) << "redeemIOU: " << - to_string (account) << " attempts to redeem " << - amount.getFullText () << " but no trust line exists!"; + JLOG(j.fatal()) << "redeemIOU: " << to_string(account) + << " attempts to redeem " << amount.getFullText() + << " but no trust line exists!"; return tefINTERNAL; } - STAmount final_balance = state->getFieldAmount (sfBalance); + STAmount final_balance = state->getFieldAmount(sfBalance); if (bSenderHigh) - final_balance.negate (); // Put balance in sender terms. + final_balance.negate(); // Put balance in sender terms. STAmount const start_balance = final_balance; final_balance -= amount; - auto const must_delete = updateTrustLine (view, state, bSenderHigh, account, - start_balance, final_balance, j); + auto const must_delete = updateTrustLine( + view, state, bSenderHigh, account, start_balance, final_balance, j); - view.creditHook (account, issue.account, amount, start_balance); + view.creditHook(account, issue.account, amount, start_balance); if (bSenderHigh) - final_balance.negate (); - + final_balance.negate(); // Adjust the balance on the trust line if necessary. We do this even if we // are going to delete the line to reflect the correct balance at the time // of deletion. - state->setFieldAmount (sfBalance, final_balance); + state->setFieldAmount(sfBalance, final_balance); if (must_delete) { - return trustDelete (view, state, + return trustDelete( + view, + state, bSenderHigh ? issue.account : account, - bSenderHigh ? account : issue.account, j); + bSenderHigh ? account : issue.account, + j); } - view.update (state); + view.update(state); return tesSUCCESS; } TER -transferXRP (ApplyView& view, +transferXRP( + ApplyView& view, AccountID const& from, AccountID const& to, STAmount const& amount, beast::Journal j) { - assert (from != beast::zero); - assert (to != beast::zero); - assert (from != to); - assert (amount.native ()); + assert(from != beast::zero); + assert(to != beast::zero); + assert(from != to); + assert(amount.native()); - SLE::pointer const sender = view.peek (keylet::account(from)); - SLE::pointer const receiver = view.peek (keylet::account(to)); + SLE::pointer const sender = view.peek(keylet::account(from)); + SLE::pointer const receiver = view.peek(keylet::account(to)); if (!sender || !receiver) return tefINTERNAL; - JLOG (j.trace()) << "transferXRP: " << - to_string (from) << " -> " << to_string (to) << - ") : " << amount.getFullText (); + JLOG(j.trace()) << "transferXRP: " << to_string(from) << " -> " + << to_string(to) << ") : " << amount.getFullText(); - if (sender->getFieldAmount (sfBalance) < amount) + if (sender->getFieldAmount(sfBalance) < amount) { // VFALCO Its unfortunate we have to keep // mutating these TER everywhere // FIXME: this logic should be moved to callers maybe? - return view.open() - ? TER {telFAILED_PROCESSING} - : TER {tecFAILED_PROCESSING}; + return view.open() ? TER{telFAILED_PROCESSING} + : TER{tecFAILED_PROCESSING}; } // Decrement XRP balance. - sender->setFieldAmount (sfBalance, - sender->getFieldAmount (sfBalance) - amount); - view.update (sender); + sender->setFieldAmount( + sfBalance, sender->getFieldAmount(sfBalance) - amount); + view.update(sender); - receiver->setFieldAmount (sfBalance, - receiver->getFieldAmount (sfBalance) + amount); - view.update (receiver); + receiver->setFieldAmount( + sfBalance, receiver->getFieldAmount(sfBalance) + amount); + view.update(receiver); return tesSUCCESS; } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/AutoSocket.h b/src/ripple/net/AutoSocket.h index 18702209ae..edb4ffac04 100644 --- a/src/ripple/net/AutoSocket.h +++ b/src/ripple/net/AutoSocket.h @@ -22,10 +22,10 @@ #include #include -#include #include #include #include +#include // Socket wrapper that supports both SSL and non-SSL connections. // Generally, handle it as you would an SSL connection. @@ -35,96 +35,101 @@ class AutoSocket { public: - using ssl_socket = boost::asio::ssl::stream; - using endpoint_type = boost::asio::ip::tcp::socket::endpoint_type; - using socket_ptr = std::unique_ptr; - using plain_socket = ssl_socket::next_layer_type; + using ssl_socket = boost::asio::ssl::stream; + using endpoint_type = boost::asio::ip::tcp::socket::endpoint_type; + using socket_ptr = std::unique_ptr; + using plain_socket = ssl_socket::next_layer_type; using lowest_layer_type = ssl_socket::lowest_layer_type; - using handshake_type = ssl_socket::handshake_type; - using error_code = boost::system::error_code; - using callback = std::function ; + using handshake_type = ssl_socket::handshake_type; + using error_code = boost::system::error_code; + using callback = std::function; public: - AutoSocket ( - boost::asio::io_service& s, - boost::asio::ssl::context& c, - bool secureOnly, - bool plainOnly) - : mSecure (secureOnly) - , mBuffer ((plainOnly || secureOnly) ? 0 : 4) - , j_ {beast::Journal::getNullSink()} + AutoSocket( + boost::asio::io_service& s, + boost::asio::ssl::context& c, + bool secureOnly, + bool plainOnly) + : mSecure(secureOnly) + , mBuffer((plainOnly || secureOnly) ? 0 : 4) + , j_{beast::Journal::getNullSink()} { - mSocket = std::make_unique (s, c); + mSocket = std::make_unique(s, c); } - AutoSocket ( - boost::asio::io_service& s, - boost::asio::ssl::context& c) - : AutoSocket (s, c, false, false) + AutoSocket(boost::asio::io_service& s, boost::asio::ssl::context& c) + : AutoSocket(s, c, false, false) { } - bool isSecure () + bool + isSecure() { return mSecure; } - ssl_socket& SSLSocket () + ssl_socket& + SSLSocket() { return *mSocket; } - plain_socket& PlainSocket () + plain_socket& + PlainSocket() { - return mSocket->next_layer (); + return mSocket->next_layer(); } - void setSSLOnly () + void + setSSLOnly() { mSecure = true; } - void setPlainOnly () + void + setPlainOnly() { - mBuffer.clear (); + mBuffer.clear(); } beast::IP::Endpoint local_endpoint() { - return beast::IP::from_asio( - lowest_layer().local_endpoint()); + return beast::IP::from_asio(lowest_layer().local_endpoint()); } beast::IP::Endpoint remote_endpoint() { - return beast::IP::from_asio( - lowest_layer().remote_endpoint()); + return beast::IP::from_asio(lowest_layer().remote_endpoint()); } - lowest_layer_type& lowest_layer () + lowest_layer_type& + lowest_layer() { - return mSocket->lowest_layer (); + return mSocket->lowest_layer(); } - void swap (AutoSocket& s) noexcept + void + swap(AutoSocket& s) noexcept { - mBuffer.swap (s.mBuffer); - mSocket.swap (s.mSocket); - std::swap (mSecure, s.mSecure); + mBuffer.swap(s.mBuffer); + mSocket.swap(s.mSocket); + std::swap(mSecure, s.mSecure); } - boost::system::error_code cancel (boost::system::error_code& ec) + boost::system::error_code + cancel(boost::system::error_code& ec) { - return lowest_layer ().cancel (ec); + return lowest_layer().cancel(ec); } - void async_handshake (handshake_type type, callback cbFunc) + void + async_handshake(handshake_type type, callback cbFunc) { if ((type == ssl_socket::client) || (mSecure)) { // must be ssl mSecure = true; - mSocket->async_handshake (type, cbFunc); + mSocket->async_handshake(type, cbFunc); } - else if (mBuffer.empty ()) + else if (mBuffer.empty()) { // must be plain mSecure = false; @@ -135,28 +140,30 @@ public: else { // autodetect - mSocket->next_layer ().async_receive ( - boost::asio::buffer (mBuffer), + mSocket->next_layer().async_receive( + boost::asio::buffer(mBuffer), boost::asio::socket_base::message_peek, - std::bind ( + std::bind( &AutoSocket::handle_autodetect, - this, cbFunc, + this, + cbFunc, std::placeholders::_1, std::placeholders::_2)); } } template - void async_shutdown (ShutdownHandler handler) + void + async_shutdown(ShutdownHandler handler) { - if (isSecure ()) - mSocket->async_shutdown (handler); + if (isSecure()) + mSocket->async_shutdown(handler); else { error_code ec; try { - lowest_layer ().shutdown (plain_socket::shutdown_both); + lowest_layer().shutdown(plain_socket::shutdown_both); } catch (boost::system::system_error& e) { @@ -169,31 +176,35 @@ public: } template - void async_read_some (const Seq& buffers, Handler handler) + void + async_read_some(const Seq& buffers, Handler handler) { - if (isSecure ()) - mSocket->async_read_some (buffers, handler); + if (isSecure()) + mSocket->async_read_some(buffers, handler); else - PlainSocket ().async_read_some (buffers, handler); + PlainSocket().async_read_some(buffers, handler); } template - void async_read_until( - const Seq& buffers, Condition condition, Handler handler) + void + async_read_until(const Seq& buffers, Condition condition, Handler handler) { if (isSecure()) boost::asio::async_read_until( *mSocket, buffers, condition, handler); else boost::asio::async_read_until( - PlainSocket (), buffers, condition, handler); + PlainSocket(), buffers, condition, handler); } template - void async_read_until(boost::asio::basic_streambuf& buffers, - std::string const& delim, Handler handler) + void + async_read_until( + boost::asio::basic_streambuf& buffers, + std::string const& delim, + Handler handler) { - if (isSecure ()) + if (isSecure()) boost::asio::async_read_until(*mSocket, buffers, delim, handler); else boost::asio::async_read_until( @@ -201,10 +212,13 @@ public: } template - void async_read_until (boost::asio::basic_streambuf& buffers, - MatchCondition cond, Handler handler) + void + async_read_until( + boost::asio::basic_streambuf& buffers, + MatchCondition cond, + Handler handler) { - if (isSecure ()) + if (isSecure()) boost::asio::async_read_until(*mSocket, buffers, cond, handler); else boost::asio::async_read_until( @@ -212,100 +226,112 @@ public: } template - void async_write (const Buf& buffers, Handler handler) + void + async_write(const Buf& buffers, Handler handler) { - if (isSecure ()) + if (isSecure()) boost::asio::async_write(*mSocket, buffers, handler); else - boost::asio::async_write(PlainSocket (), buffers, handler); + boost::asio::async_write(PlainSocket(), buffers, handler); } template - void async_write (boost::asio::basic_streambuf& buffers, - Handler handler) + void + async_write( + boost::asio::basic_streambuf& buffers, + Handler handler) { - if (isSecure ()) + if (isSecure()) boost::asio::async_write(*mSocket, buffers, handler); else boost::asio::async_write(PlainSocket(), buffers, handler); } template - void async_read (const Buf& buffers, Condition cond, Handler handler) + void + async_read(const Buf& buffers, Condition cond, Handler handler) { - if (isSecure ()) + if (isSecure()) boost::asio::async_read(*mSocket, buffers, cond, handler); else boost::asio::async_read(PlainSocket(), buffers, cond, handler); } template - void async_read (boost::asio::basic_streambuf& buffers, - Condition cond, Handler handler) + void + async_read( + boost::asio::basic_streambuf& buffers, + Condition cond, + Handler handler) { - if (isSecure ()) - boost::asio::async_read (*mSocket, buffers, cond, handler); + if (isSecure()) + boost::asio::async_read(*mSocket, buffers, cond, handler); else - boost::asio::async_read (PlainSocket (), buffers, cond, handler); + boost::asio::async_read(PlainSocket(), buffers, cond, handler); } template - void async_read (const Buf& buffers, Handler handler) + void + async_read(const Buf& buffers, Handler handler) { - if (isSecure ()) - boost::asio::async_read (*mSocket, buffers, handler); + if (isSecure()) + boost::asio::async_read(*mSocket, buffers, handler); else - boost::asio::async_read (PlainSocket (), buffers, handler); + boost::asio::async_read(PlainSocket(), buffers, handler); } template - void async_write_some (const Seq& buffers, Handler handler) + void + async_write_some(const Seq& buffers, Handler handler) { - if (isSecure ()) - mSocket->async_write_some (buffers, handler); + if (isSecure()) + mSocket->async_write_some(buffers, handler); else - PlainSocket ().async_write_some (buffers, handler); + PlainSocket().async_write_some(buffers, handler); } protected: - void handle_autodetect ( - callback cbFunc, const error_code& ec, size_t bytesTransferred) + void + handle_autodetect( + callback cbFunc, + const error_code& ec, + size_t bytesTransferred) { using namespace ripple; if (ec) { - JLOG (j_.warn()) << - "Handle autodetect error: " << ec; - cbFunc (ec); + JLOG(j_.warn()) << "Handle autodetect error: " << ec; + cbFunc(ec); } - else if ((mBuffer[0] < 127) && (mBuffer[0] > 31) && - ((bytesTransferred < 2) - || ((mBuffer[1] < 127) && (mBuffer[1] > 31))) && - ((bytesTransferred < 3) - || ((mBuffer[2] < 127) && (mBuffer[2] > 31))) && - ((bytesTransferred < 4) - || ((mBuffer[3] < 127) && (mBuffer[3] > 31)))) + else if ( + (mBuffer[0] < 127) && (mBuffer[0] > 31) && + ((bytesTransferred < 2) || + ((mBuffer[1] < 127) && (mBuffer[1] > 31))) && + ((bytesTransferred < 3) || + ((mBuffer[2] < 127) && (mBuffer[2] > 31))) && + ((bytesTransferred < 4) || + ((mBuffer[3] < 127) && (mBuffer[3] > 31)))) { // not ssl - JLOG (j_.trace()) << "non-SSL"; + JLOG(j_.trace()) << "non-SSL"; mSecure = false; - cbFunc (ec); + cbFunc(ec); } else { // ssl - JLOG (j_.trace()) << "SSL"; + JLOG(j_.trace()) << "SSL"; mSecure = true; - mSocket->async_handshake (ssl_socket::server, cbFunc); + mSocket->async_handshake(ssl_socket::server, cbFunc); } } private: - socket_ptr mSocket; - bool mSecure; - std::vector mBuffer; - beast::Journal j_; + socket_ptr mSocket; + bool mSecure; + std::vector mBuffer; + beast::Journal j_; }; #endif diff --git a/src/ripple/net/HTTPClient.h b/src/ripple/net/HTTPClient.h index 30ed29da54..8cadc84bcb 100644 --- a/src/ripple/net/HTTPClient.h +++ b/src/ripple/net/HTTPClient.h @@ -29,7 +29,7 @@ namespace ripple { /** Provides an asynchronous HTTP client implementation with optional SSL. -*/ + */ class HTTPClient { public: @@ -37,42 +37,54 @@ public: static constexpr auto maxClientHeaderBytes = kilobytes(32); - static void initializeSSLContext (Config const& config, beast::Journal j); + static void + initializeSSLContext(Config const& config, beast::Journal j); - static void get ( - bool bSSL, + static void + get(bool bSSL, boost::asio::io_service& io_service, - std::deque deqSites, + std::deque deqSites, const unsigned short port, std::string const& strPath, - std::size_t responseMax, // if no Content-Length header + std::size_t responseMax, // if no Content-Length header std::chrono::seconds timeout, - std::function complete, + std::function complete, beast::Journal& j); - static void get ( - bool bSSL, + static void + get(bool bSSL, boost::asio::io_service& io_service, std::string strSite, const unsigned short port, std::string const& strPath, - std::size_t responseMax, // if no Content-Length header + std::size_t responseMax, // if no Content-Length header std::chrono::seconds timeout, - std::function complete, + std::function complete, beast::Journal& j); - static void request ( + static void + request( bool bSSL, boost::asio::io_service& io_service, std::string strSite, const unsigned short port, - std::function build, - std::size_t responseMax, // if no Content-Length header + std::function< + void(boost::asio::streambuf& sb, std::string const& strHost)> build, + std::size_t responseMax, // if no Content-Length header std::chrono::seconds timeout, - std::function complete, + std::function complete, beast::Journal& j); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/HTTPClientSSLContext.h b/src/ripple/net/HTTPClientSSLContext.h index e91f6329ea..b0f6942ae1 100644 --- a/src/ripple/net/HTTPClientSSLContext.h +++ b/src/ripple/net/HTTPClientSSLContext.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_NET_HTTPCLIENTSSLCONTEXT_H_INCLUDED #define RIPPLE_NET_HTTPCLIENTSSLCONTEXT_H_INCLUDED -#include #include +#include #include #include #include @@ -34,51 +34,48 @@ namespace ripple { class HTTPClientSSLContext { public: - - explicit - HTTPClientSSLContext ( + explicit HTTPClientSSLContext( Config const& config, beast::Journal j, boost::asio::ssl::context_base::method method = boost::asio::ssl::context::sslv23) - : ssl_context_ {method} - , j_(j) - , verify_ {config.SSL_VERIFY} + : ssl_context_{method}, j_(j), verify_{config.SSL_VERIFY} { boost::system::error_code ec; - if (config.SSL_VERIFY_FILE.empty ()) + if (config.SSL_VERIFY_FILE.empty()) { registerSSLCerts(ssl_context_, ec, j_); - if (ec && config.SSL_VERIFY_DIR.empty ()) - Throw ( - boost::str (boost::format ( - "Failed to set_default_verify_paths: %s") % - ec.message ())); + if (ec && config.SSL_VERIFY_DIR.empty()) + Throw(boost::str( + boost::format("Failed to set_default_verify_paths: %s") % + ec.message())); } else { - ssl_context_.load_verify_file (config.SSL_VERIFY_FILE); + ssl_context_.load_verify_file(config.SSL_VERIFY_FILE); } - if (! config.SSL_VERIFY_DIR.empty ()) + if (!config.SSL_VERIFY_DIR.empty()) { - ssl_context_.add_verify_path (config.SSL_VERIFY_DIR, ec); + ssl_context_.add_verify_path(config.SSL_VERIFY_DIR, ec); if (ec) - Throw ( - boost::str (boost::format ( - "Failed to add verify path: %s") % ec.message ())); + Throw(boost::str( + boost::format("Failed to add verify path: %s") % + ec.message())); } } - boost::asio::ssl::context& context() + boost::asio::ssl::context& + context() { return ssl_context_; } - bool sslVerify() const + bool + sslVerify() const { return verify_; } @@ -95,37 +92,45 @@ public: * * @return error_code indicating failures, if any */ - template>::value || - std::is_same>::value - > - > + std::is_same< + T, + boost::asio::ssl::stream>:: + value || + std::is_same< + T, + boost::asio::ssl::stream>:: + value>> boost::system::error_code - preConnectVerify ( - T& strm, - std::string const& host) + preConnectVerify(T& strm, std::string const& host) { boost::system::error_code ec; if (!SSL_set_tlsext_host_name(strm.native_handle(), host.c_str())) { - ec.assign(static_cast( - ::ERR_get_error()), boost::asio::error::get_ssl_category()); + ec.assign( + static_cast(::ERR_get_error()), + boost::asio::error::get_ssl_category()); } else if (!sslVerify()) { strm.set_verify_mode(boost::asio::ssl::verify_none, ec); } return ec; - } - template>::value || - std::is_same>::value - > - > + std::is_same< + T, + boost::asio::ssl::stream>:: + value || + std::is_same< + T, + boost::asio::ssl::stream>:: + value>> /** * @brief invoked after connect/async_connect but before sending data * on an ssl stream - to setup name verification. @@ -134,20 +139,23 @@ public: * @param host hostname to verify */ boost::system::error_code - postConnectVerify ( - T& strm, - std::string const& host) + postConnectVerify(T& strm, std::string const& host) { boost::system::error_code ec; if (sslVerify()) { - strm.set_verify_mode (boost::asio::ssl::verify_peer, ec); + strm.set_verify_mode(boost::asio::ssl::verify_peer, ec); if (!ec) { - strm.set_verify_callback ( - std::bind (&rfc2818_verify, host, - std::placeholders::_1, std::placeholders::_2, j_), ec); + strm.set_verify_callback( + std::bind( + &rfc2818_verify, + host, + std::placeholders::_1, + std::placeholders::_2, + j_), + ec); } } @@ -163,20 +171,18 @@ public: * @param ctx passed by implementation * @param j journal for logging */ - static - bool - rfc2818_verify ( + static bool + rfc2818_verify( std::string const& domain, bool preverified, boost::asio::ssl::verify_context& ctx, beast::Journal j) { - if (boost::asio::ssl::rfc2818_verification (domain) (preverified, ctx)) + if (boost::asio::ssl::rfc2818_verification(domain)(preverified, ctx)) return true; - JLOG (j.warn()) << - "Outbound SSL connection to " << domain << - " fails certificate verification"; + JLOG(j.warn()) << "Outbound SSL connection to " << domain + << " fails certificate verification"; return false; } @@ -186,6 +192,6 @@ private: const bool verify_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/InfoSub.h b/src/ripple/net/InfoSub.h index a8c221b123..aaf6293d2b 100644 --- a/src/ripple/net/InfoSub.h +++ b/src/ripple/net/InfoSub.h @@ -20,12 +20,12 @@ #ifndef RIPPLE_NET_INFOSUB_H_INCLUDED #define RIPPLE_NET_INFOSUB_H_INCLUDED -#include -#include #include -#include -#include +#include #include +#include +#include +#include #include namespace ripple { @@ -36,16 +36,20 @@ namespace ripple { class PathRequest; /** Manages a client's subscription to data feeds. -*/ -class InfoSub - : public CountedObject + */ +class InfoSub : public CountedObject { public: - static char const* getCountedObjectName () { return "InfoSub"; } + static char const* + getCountedObjectName() + { + return "InfoSub"; + } using pointer = std::shared_ptr; - // VFALCO TODO Standardize on the names of weak / strong pointer type aliases. + // VFALCO TODO Standardize on the names of weak / strong pointer type + // aliases. using wptr = std::weak_ptr; using ref = const std::shared_ptr&; @@ -54,114 +58,145 @@ public: public: /** Abstracts the source of subscription data. - */ + */ class Source : public Stoppable { protected: - Source (char const* name, Stoppable& parent); + Source(char const* name, Stoppable& parent); public: - // For some reason, these were originally called "rt" // for "real time". They actually refer to whether // you get transactions as they occur or once their // results are confirmed - virtual void subAccount (ref ispListener, + virtual void + subAccount( + ref ispListener, hash_set const& vnaAccountIDs, bool realTime) = 0; // for normal use, removes from InfoSub and server - virtual void unsubAccount (ref isplistener, + virtual void + unsubAccount( + ref isplistener, hash_set const& vnaAccountIDs, bool realTime) = 0; // for use during InfoSub destruction // Removes only from the server - virtual void unsubAccountInternal (std::uint64_t uListener, + virtual void + unsubAccountInternal( + std::uint64_t uListener, hash_set const& vnaAccountIDs, bool realTime) = 0; // VFALCO TODO Document the bool return value - virtual bool subLedger (ref ispListener, Json::Value& jvResult) = 0; - virtual bool unsubLedger (std::uint64_t uListener) = 0; + virtual bool + subLedger(ref ispListener, Json::Value& jvResult) = 0; + virtual bool + unsubLedger(std::uint64_t uListener) = 0; - virtual bool subManifests (ref ispListener) = 0; - virtual bool unsubManifests (std::uint64_t uListener) = 0; - virtual void pubManifest (Manifest const&) = 0; + virtual bool + subManifests(ref ispListener) = 0; + virtual bool + unsubManifests(std::uint64_t uListener) = 0; + virtual void + pubManifest(Manifest const&) = 0; - virtual bool subServer (ref ispListener, Json::Value& jvResult, - bool admin) = 0; - virtual bool unsubServer (std::uint64_t uListener) = 0; + virtual bool + subServer(ref ispListener, Json::Value& jvResult, bool admin) = 0; + virtual bool + unsubServer(std::uint64_t uListener) = 0; - virtual bool subBook (ref ispListener, Book const&) = 0; - virtual bool unsubBook (std::uint64_t uListener, Book const&) = 0; + virtual bool + subBook(ref ispListener, Book const&) = 0; + virtual bool + unsubBook(std::uint64_t uListener, Book const&) = 0; - virtual bool subTransactions (ref ispListener) = 0; - virtual bool unsubTransactions (std::uint64_t uListener) = 0; + virtual bool + subTransactions(ref ispListener) = 0; + virtual bool + unsubTransactions(std::uint64_t uListener) = 0; - virtual bool subRTTransactions (ref ispListener) = 0; - virtual bool unsubRTTransactions (std::uint64_t uListener) = 0; + virtual bool + subRTTransactions(ref ispListener) = 0; + virtual bool + unsubRTTransactions(std::uint64_t uListener) = 0; - virtual bool subValidations (ref ispListener) = 0; - virtual bool unsubValidations (std::uint64_t uListener) = 0; + virtual bool + subValidations(ref ispListener) = 0; + virtual bool + unsubValidations(std::uint64_t uListener) = 0; - virtual bool subPeerStatus (ref ispListener) = 0; - virtual bool unsubPeerStatus (std::uint64_t uListener) = 0; - virtual void pubPeerStatus (std::function const&) = 0; + virtual bool + subPeerStatus(ref ispListener) = 0; + virtual bool + unsubPeerStatus(std::uint64_t uListener) = 0; + virtual void + pubPeerStatus(std::function const&) = 0; - virtual bool subConsensus (ref ispListener) = 0; - virtual bool unsubConsensus (std::uint64_t uListener) = 0; + virtual bool + subConsensus(ref ispListener) = 0; + virtual bool + unsubConsensus(std::uint64_t uListener) = 0; // VFALCO TODO Remove // This was added for one particular partner, it // "pushes" subscription data to a particular URL. // - virtual pointer findRpcSub (std::string const& strUrl) = 0; - virtual pointer addRpcSub (std::string const& strUrl, ref rspEntry) = 0; - virtual bool tryRemoveRpcSub (std::string const& strUrl) = 0; + virtual pointer + findRpcSub(std::string const& strUrl) = 0; + virtual pointer + addRpcSub(std::string const& strUrl, ref rspEntry) = 0; + virtual bool + tryRemoveRpcSub(std::string const& strUrl) = 0; }; public: - InfoSub (Source& source); - InfoSub (Source& source, Consumer consumer); + InfoSub(Source& source); + InfoSub(Source& source, Consumer consumer); - virtual ~InfoSub (); + virtual ~InfoSub(); - Consumer& getConsumer(); + Consumer& + getConsumer(); - virtual void send (Json::Value const& jvObj, bool broadcast) = 0; + virtual void + send(Json::Value const& jvObj, bool broadcast) = 0; - std::uint64_t getSeq (); + std::uint64_t + getSeq(); - void onSendEmpty (); + void + onSendEmpty(); - void insertSubAccountInfo ( - AccountID const& account, - bool rt); + void + insertSubAccountInfo(AccountID const& account, bool rt); - void deleteSubAccountInfo ( - AccountID const& account, - bool rt); + void + deleteSubAccountInfo(AccountID const& account, bool rt); - void clearPathRequest (); + void + clearPathRequest(); - void setPathRequest (const std::shared_ptr& req); + void + setPathRequest(const std::shared_ptr& req); - std::shared_ptr const& getPathRequest (); + std::shared_ptr const& + getPathRequest(); protected: std::mutex mLock; private: - Consumer m_consumer; - Source& m_source; - hash_set realTimeSubscriptions_; - hash_set normalSubscriptions_; - std::shared_ptr mPathRequest; - std::uint64_t mSeq; + Consumer m_consumer; + Source& m_source; + hash_set realTimeSubscriptions_; + hash_set normalSubscriptions_; + std::shared_ptr mPathRequest; + std::uint64_t mSeq; - static - int + static int assign_id() { static std::atomic id(0); @@ -169,6 +204,6 @@ private: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/RPCCall.h b/src/ripple/net/RPCCall.h index 0f9af52beb..a97c617710 100644 --- a/src/ripple/net/RPCCall.h +++ b/src/ripple/net/RPCCall.h @@ -41,34 +41,44 @@ namespace ripple { /** Processes Ripple RPC calls. */ namespace RPCCall { -int fromCommandLine ( +int +fromCommandLine( Config const& config, const std::vector& vCmd, Logs& logs); -void fromNetwork ( +void +fromNetwork( boost::asio::io_service& io_service, - std::string const& strIp, const std::uint16_t iPort, - std::string const& strUsername, std::string const& strPassword, - std::string const& strPath, std::string const& strMethod, - Json::Value const& jvParams, const bool bSSL, bool quiet, + std::string const& strIp, + const std::uint16_t iPort, + std::string const& strUsername, + std::string const& strPassword, + std::string const& strPath, + std::string const& strMethod, + Json::Value const& jvParams, + const bool bSSL, + bool quiet, Logs& logs, - std::function callbackFuncP = std::function (), + std::function callbackFuncP = + std::function(), std::unordered_map headers = {}); -} +} // namespace RPCCall /** Given a rippled command line, return the corresponding JSON. -*/ + */ Json::Value -cmdLineToJSONRPC (std::vector const& args, beast::Journal j); +cmdLineToJSONRPC(std::vector const& args, beast::Journal j); /** Internal invocation of RPC client. -*/ + */ std::pair -rpcClient(std::vector const& args, - Config const& config, Logs& logs, +rpcClient( + std::vector const& args, + Config const& config, + Logs& logs, std::unordered_map const& headers = {}); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/RPCErr.h b/src/ripple/net/RPCErr.h index 0841de2b08..f2bce86398 100644 --- a/src/ripple/net/RPCErr.h +++ b/src/ripple/net/RPCErr.h @@ -25,10 +25,11 @@ namespace ripple { // VFALCO NOTE these are deprecated -bool isRpcError (Json::Value jvResult); -Json::Value rpcError (int iError, - Json::Value jvResult = Json::Value (Json::objectValue)); +bool +isRpcError(Json::Value jvResult); +Json::Value +rpcError(int iError, Json::Value jvResult = Json::Value(Json::objectValue)); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/RPCSub.h b/src/ripple/net/RPCSub.h index 3147554372..e62aeeb4ca 100644 --- a/src/ripple/net/RPCSub.h +++ b/src/ripple/net/RPCSub.h @@ -21,8 +21,8 @@ #define RIPPLE_NET_RPCSUB_H_INCLUDED #include -#include #include +#include #include namespace ripple { @@ -31,20 +31,26 @@ namespace ripple { class RPCSub : public InfoSub { public: - virtual void setUsername (std::string const& strUsername) = 0; - virtual void setPassword (std::string const& strPassword) = 0; + virtual void + setUsername(std::string const& strUsername) = 0; + virtual void + setPassword(std::string const& strPassword) = 0; protected: - explicit RPCSub (InfoSub::Source& source); + explicit RPCSub(InfoSub::Source& source); }; // VFALCO Why is the io_service needed? -std::shared_ptr make_RPCSub ( - InfoSub::Source& source, boost::asio::io_service& io_service, - JobQueue& jobQueue, std::string const& strUrl, - std::string const& strUsername, std::string const& strPassword, +std::shared_ptr +make_RPCSub( + InfoSub::Source& source, + boost::asio::io_service& io_service, + JobQueue& jobQueue, + std::string const& strUrl, + std::string const& strUsername, + std::string const& strPassword, Logs& logs); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/RegisterSSLCerts.h b/src/ripple/net/RegisterSSLCerts.h index abfbb0c564..8b09f1031d 100644 --- a/src/ripple/net/RegisterSSLCerts.h +++ b/src/ripple/net/RegisterSSLCerts.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_NET_REGISTER_SSL_CERTS_H_INCLUDED #define RIPPLE_NET_REGISTER_SSL_CERTS_H_INCLUDED -#include #include +#include namespace ripple { /** Register default SSL certificates. diff --git a/src/ripple/net/SSLHTTPDownloader.h b/src/ripple/net/SSLHTTPDownloader.h index b70383373c..f8499d0493 100644 --- a/src/ripple/net/SSLHTTPDownloader.h +++ b/src/ripple/net/SSLHTTPDownloader.h @@ -40,9 +40,8 @@ namespace ripple { /** Provides an asynchronous HTTPS file downloader -*/ -class SSLHTTPDownloader - : public std::enable_shared_from_this + */ +class SSLHTTPDownloader : public std::enable_shared_from_this { public: using error_code = boost::system::error_code; @@ -65,11 +64,9 @@ public: void onStop(); - virtual - ~SSLHTTPDownloader() = default; + virtual ~SSLHTTPDownloader() = default; protected: - using parser = boost::beast::http::basic_parser; beast::Journal const j_; @@ -85,8 +82,8 @@ protected: private: HTTPClientSSLContext ssl_ctx_; boost::asio::io_service::strand strand_; - boost::optional< - boost::asio::ssl::stream> stream_; + boost::optional> + stream_; boost::beast::flat_buffer read_buf_; std::atomic isStopped_; bool sessionActive_; @@ -103,27 +100,22 @@ private: std::function complete, boost::asio::yield_context yield); - virtual - std::shared_ptr + virtual std::shared_ptr getParser( boost::filesystem::path dstPath, std::function complete, - boost::system::error_code & ec) = 0; + boost::system::error_code& ec) = 0; - virtual - bool - checkPath( - boost::filesystem::path const& dstPath) = 0; + virtual bool + checkPath(boost::filesystem::path const& dstPath) = 0; - virtual - void + virtual void closeBody(std::shared_ptr p) = 0; - virtual - uint64_t + virtual uint64_t size(std::shared_ptr p) = 0; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/net/impl/DatabaseDownloader.cpp b/src/ripple/net/impl/DatabaseDownloader.cpp index 46f82d6a75..f96e0133c3 100644 --- a/src/ripple/net/impl/DatabaseDownloader.cpp +++ b/src/ripple/net/impl/DatabaseDownloader.cpp @@ -19,34 +19,30 @@ #include -namespace ripple -{ +namespace ripple { DatabaseDownloader::DatabaseDownloader( - boost::asio::io_service & io_service, + boost::asio::io_service& io_service, beast::Journal j, - Config const & config) + Config const& config) : SSLHTTPDownloader(io_service, j, config) - , config_(config) - , io_service_(io_service) + , config_(config) + , io_service_(io_service) { } auto -DatabaseDownloader::getParser(boost::filesystem::path dstPath, +DatabaseDownloader::getParser( + boost::filesystem::path dstPath, std::function complete, - boost::system::error_code & ec) -> std::shared_ptr + boost::system::error_code& ec) -> std::shared_ptr { using namespace boost::beast; - auto p = std::make_shared>(); + auto p = std::make_shared>(); p->body_limit(std::numeric_limits::max()); - p->get().body().open( - dstPath, - config_, - io_service_, - ec); - if(ec) + p->get().body().open(dstPath, config_, io_service_, ec); + if (ec) { p->get().body().close(); fail(dstPath, complete, ec, "open"); @@ -56,7 +52,7 @@ DatabaseDownloader::getParser(boost::filesystem::path dstPath, } bool -DatabaseDownloader::checkPath(boost::filesystem::path const & dstPath) +DatabaseDownloader::checkPath(boost::filesystem::path const& dstPath) { return dstPath.string().size() <= MAX_PATH_LEN; } @@ -66,8 +62,8 @@ DatabaseDownloader::closeBody(std::shared_ptr p) { using namespace boost::beast; - auto databaseBodyParser = std::dynamic_pointer_cast< - http::response_parser>(p); + auto databaseBodyParser = + std::dynamic_pointer_cast>(p); assert(databaseBodyParser); databaseBodyParser->get().body().close(); @@ -78,11 +74,11 @@ DatabaseDownloader::size(std::shared_ptr p) { using namespace boost::beast; - auto databaseBodyParser = std::dynamic_pointer_cast< - http::response_parser>(p); + auto databaseBodyParser = + std::dynamic_pointer_cast>(p); assert(databaseBodyParser); return databaseBodyParser->get().body().size(); } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/HTTPClient.cpp b/src/ripple/net/impl/HTTPClient.cpp index d1497b7bdf..79779065ce 100644 --- a/src/ripple/net/impl/HTTPClient.cpp +++ b/src/ripple/net/impl/HTTPClient.cpp @@ -17,26 +17,27 @@ */ //============================================================================== -#include #include #include +#include +#include +#include #include #include -#include -#include #include -#include #include -#include +#include #include +#include namespace ripple { boost::optional httpClientSSLContext; -void HTTPClient::initializeSSLContext (Config const& config, beast::Journal j) +void +HTTPClient::initializeSSLContext(Config const& config, beast::Journal j) { - httpClientSSLContext.emplace (config, j); + httpClientSSLContext.emplace(config, j); } //------------------------------------------------------------------------------ @@ -45,37 +46,42 @@ void HTTPClient::initializeSSLContext (Config const& config, beast::Journal j) // //------------------------------------------------------------------------------ -class HTTPClientImp - : public std::enable_shared_from_this - , public HTTPClient +class HTTPClientImp : public std::enable_shared_from_this, + public HTTPClient { public: - HTTPClientImp (boost::asio::io_service& io_service, + HTTPClientImp( + boost::asio::io_service& io_service, const unsigned short port, std::size_t maxResponseSize, beast::Journal& j) - : mSocket (io_service, httpClientSSLContext->context ()) - , mResolver (io_service) - , mHeader (maxClientHeaderBytes) - , mPort (port) - , maxResponseSize_ (maxResponseSize) - , mDeadline (io_service) - , j_ (j) + : mSocket(io_service, httpClientSSLContext->context()) + , mResolver(io_service) + , mHeader(maxClientHeaderBytes) + , mPort(port) + , maxResponseSize_(maxResponseSize) + , mDeadline(io_service) + , j_(j) { } //-------------------------------------------------------------------------- - void makeGet (std::string const& strPath, boost::asio::streambuf& sb, + void + makeGet( + std::string const& strPath, + boost::asio::streambuf& sb, std::string const& strHost) { - std::ostream osRequest (&sb); + std::ostream osRequest(&sb); - osRequest << - "GET " << strPath << " HTTP/1.0\r\n" - "Host: " << strHost << "\r\n" - "Accept: */*\r\n" // YYY Do we need this line? - "Connection: close\r\n\r\n"; + osRequest << "GET " << strPath + << " HTTP/1.0\r\n" + "Host: " + << strHost + << "\r\n" + "Accept: */*\r\n" // YYY Do we need this line? + "Connection: close\r\n\r\n"; } //-------------------------------------------------------------------------- @@ -84,22 +90,21 @@ public: request( bool bSSL, std::deque deqSites, - std::function build, + std::function< + void(boost::asio::streambuf& sb, std::string const& strHost)> build, std::chrono::seconds timeout, - std::function complete) { - mSSL = bSSL; - mDeqSites = deqSites; - mBuild = build; - mComplete = complete; - mTimeout = timeout; + mSSL = bSSL; + mDeqSites = deqSites; + mBuild = build; + mComplete = complete; + mTimeout = timeout; - httpsNext (); + httpsNext(); } //-------------------------------------------------------------------------- @@ -109,15 +114,15 @@ public: std::deque deqSites, std::string const& strPath, std::chrono::seconds timeout, - std::function complete) { - mComplete = complete; - mTimeout = timeout; + mComplete = complete; + mTimeout = timeout; - request ( + request( bSSL, deqSites, std::bind( @@ -132,404 +137,410 @@ public: //-------------------------------------------------------------------------- - void httpsNext () + void + httpsNext() { - JLOG (j_.trace()) << "Fetch: " << mDeqSites[0]; + JLOG(j_.trace()) << "Fetch: " << mDeqSites[0]; auto query = std::make_shared( - mDeqSites[0], - beast::lexicalCast (mPort), - boost::asio::ip::resolver_query_base::numeric_service); - mQuery = query; + mDeqSites[0], + beast::lexicalCast(mPort), + boost::asio::ip::resolver_query_base::numeric_service); + mQuery = query; - mDeadline.expires_from_now (mTimeout, mShutdown); + mDeadline.expires_from_now(mTimeout, mShutdown); - JLOG (j_.trace()) << "expires_from_now: " << mShutdown.message (); + JLOG(j_.trace()) << "expires_from_now: " << mShutdown.message(); if (!mShutdown) { - mDeadline.async_wait ( - std::bind ( - &HTTPClientImp::handleDeadline, - shared_from_this (), - std::placeholders::_1)); + mDeadline.async_wait(std::bind( + &HTTPClientImp::handleDeadline, + shared_from_this(), + std::placeholders::_1)); } if (!mShutdown) { - JLOG (j_.trace()) << "Resolving: " << mDeqSites[0]; + JLOG(j_.trace()) << "Resolving: " << mDeqSites[0]; - mResolver.async_resolve (*mQuery, - std::bind ( - &HTTPClientImp::handleResolve, - shared_from_this (), - std::placeholders::_1, - std::placeholders::_2)); + mResolver.async_resolve( + *mQuery, + std::bind( + &HTTPClientImp::handleResolve, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2)); } if (mShutdown) - invokeComplete (mShutdown); + invokeComplete(mShutdown); } - void handleDeadline (const boost::system::error_code& ecResult) + void + handleDeadline(const boost::system::error_code& ecResult) { if (ecResult == boost::asio::error::operation_aborted) { // Timer canceled because deadline no longer needed. - JLOG (j_.trace()) << "Deadline cancelled."; + JLOG(j_.trace()) << "Deadline cancelled."; // Aborter is done. } else if (ecResult) { - JLOG (j_.trace()) << "Deadline error: " - << mDeqSites[0] << ": " << ecResult.message (); + JLOG(j_.trace()) << "Deadline error: " << mDeqSites[0] << ": " + << ecResult.message(); // Can't do anything sound. - abort (); + abort(); } else { - JLOG (j_.trace()) << "Deadline arrived."; + JLOG(j_.trace()) << "Deadline arrived."; // Mark us as shutting down. // XXX Use our own error code. - mShutdown = boost::system::error_code { + mShutdown = boost::system::error_code{ boost::system::errc::bad_address, - boost::system::system_category ()}; + boost::system::system_category()}; // Cancel any resolving. - mResolver.cancel (); + mResolver.cancel(); // Stop the transaction. - mSocket.async_shutdown (std::bind ( - &HTTPClientImp::handleShutdown, - shared_from_this (), - std::placeholders::_1)); - + mSocket.async_shutdown(std::bind( + &HTTPClientImp::handleShutdown, + shared_from_this(), + std::placeholders::_1)); } } - void handleShutdown ( - const boost::system::error_code& ecResult - ) + void + handleShutdown(const boost::system::error_code& ecResult) { if (ecResult) { - JLOG (j_.trace()) << "Shutdown error: " - << mDeqSites[0] << ": " << ecResult.message (); + JLOG(j_.trace()) << "Shutdown error: " << mDeqSites[0] << ": " + << ecResult.message(); } } - void handleResolve ( + void + handleResolve( const boost::system::error_code& ecResult, - boost::asio::ip::tcp::resolver::iterator itrEndpoint - ) + boost::asio::ip::tcp::resolver::iterator itrEndpoint) { if (!mShutdown) { - mShutdown = - ecResult ? - ecResult : - httpClientSSLContext->preConnectVerify ( - mSocket.SSLSocket(), mDeqSites[0]); + mShutdown = ecResult ? ecResult + : httpClientSSLContext->preConnectVerify( + mSocket.SSLSocket(), mDeqSites[0]); } if (mShutdown) { - JLOG (j_.trace()) << "Resolve error: " - << mDeqSites[0] << ": " << mShutdown.message (); + JLOG(j_.trace()) << "Resolve error: " << mDeqSites[0] << ": " + << mShutdown.message(); - invokeComplete (mShutdown); + invokeComplete(mShutdown); } else { - JLOG (j_.trace()) << "Resolve complete."; + JLOG(j_.trace()) << "Resolve complete."; - boost::asio::async_connect ( - mSocket.lowest_layer (), + boost::asio::async_connect( + mSocket.lowest_layer(), itrEndpoint, - std::bind ( + std::bind( &HTTPClientImp::handleConnect, - shared_from_this (), + shared_from_this(), std::placeholders::_1)); } } - void handleConnect (const boost::system::error_code& ecResult) + void + handleConnect(const boost::system::error_code& ecResult) { if (!mShutdown) - mShutdown = ecResult; + mShutdown = ecResult; if (mShutdown) { - JLOG (j_.trace()) << "Connect error: " << mShutdown.message (); + JLOG(j_.trace()) << "Connect error: " << mShutdown.message(); } if (!mShutdown) { - JLOG (j_.trace()) << "Connected."; + JLOG(j_.trace()) << "Connected."; - mShutdown = httpClientSSLContext->postConnectVerify ( + mShutdown = httpClientSSLContext->postConnectVerify( mSocket.SSLSocket(), mDeqSites[0]); if (mShutdown) { - JLOG (j_.trace()) << "postConnectVerify: " - << mDeqSites[0] << ": " << mShutdown.message (); + JLOG(j_.trace()) << "postConnectVerify: " << mDeqSites[0] + << ": " << mShutdown.message(); } } if (mShutdown) { - invokeComplete (mShutdown); + invokeComplete(mShutdown); } else if (mSSL) { - mSocket.async_handshake ( + mSocket.async_handshake( AutoSocket::ssl_socket::client, - std::bind ( + std::bind( &HTTPClientImp::handleRequest, - shared_from_this (), + shared_from_this(), std::placeholders::_1)); } else { - handleRequest (ecResult); + handleRequest(ecResult); } } - void handleRequest (const boost::system::error_code& ecResult) + void + handleRequest(const boost::system::error_code& ecResult) { if (!mShutdown) - mShutdown = ecResult; + mShutdown = ecResult; if (mShutdown) { - JLOG (j_.trace()) << "Handshake error:" << mShutdown.message (); + JLOG(j_.trace()) << "Handshake error:" << mShutdown.message(); - invokeComplete (mShutdown); + invokeComplete(mShutdown); } else { - JLOG (j_.trace()) << "Session started."; + JLOG(j_.trace()) << "Session started."; - mBuild (mRequest, mDeqSites[0]); + mBuild(mRequest, mDeqSites[0]); - mSocket.async_write ( + mSocket.async_write( mRequest, - std::bind (&HTTPClientImp::handleWrite, - shared_from_this (), - std::placeholders::_1, - std::placeholders::_2)); + std::bind( + &HTTPClientImp::handleWrite, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2)); } } - void handleWrite ( + void + handleWrite( const boost::system::error_code& ecResult, std::size_t bytes_transferred) { if (!mShutdown) - mShutdown = ecResult; + mShutdown = ecResult; if (mShutdown) { - JLOG (j_.trace()) << "Write error: " << mShutdown.message (); + JLOG(j_.trace()) << "Write error: " << mShutdown.message(); - invokeComplete (mShutdown); + invokeComplete(mShutdown); } else { - JLOG (j_.trace()) << "Wrote."; + JLOG(j_.trace()) << "Wrote."; - mSocket.async_read_until ( + mSocket.async_read_until( mHeader, "\r\n\r\n", - std::bind (&HTTPClientImp::handleHeader, - shared_from_this (), - std::placeholders::_1, - std::placeholders::_2)); + std::bind( + &HTTPClientImp::handleHeader, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2)); } } - void handleHeader ( + void + handleHeader( const boost::system::error_code& ecResult, std::size_t bytes_transferred) { - std::string strHeader { - {std::istreambuf_iterator (&mHeader)}, - std::istreambuf_iterator ()}; - JLOG (j_.trace()) << "Header: \"" << strHeader << "\""; + std::string strHeader{ + {std::istreambuf_iterator(&mHeader)}, + std::istreambuf_iterator()}; + JLOG(j_.trace()) << "Header: \"" << strHeader << "\""; - static boost::regex reStatus { - "\\`HTTP/1\\S+ (\\d{3}) .*\\'"}; // HTTP/1.1 200 OK - static boost::regex reSize { + static boost::regex reStatus{ + "\\`HTTP/1\\S+ (\\d{3}) .*\\'"}; // HTTP/1.1 200 OK + static boost::regex reSize{ "\\`.*\\r\\nContent-Length:\\s+([0-9]+).*\\'"}; - static boost::regex reBody { - "\\`.*\\r\\n\\r\\n(.*)\\'"}; + static boost::regex reBody{"\\`.*\\r\\n\\r\\n(.*)\\'"}; - boost::smatch smMatch; + boost::smatch smMatch; // Match status code. - if (!boost::regex_match (strHeader, smMatch, reStatus)) + if (!boost::regex_match(strHeader, smMatch, reStatus)) { // XXX Use our own error code. - JLOG (j_.trace()) << "No status code"; - invokeComplete ( - boost::system::error_code { - boost::system::errc::bad_address, - boost::system::system_category ()}); + JLOG(j_.trace()) << "No status code"; + invokeComplete(boost::system::error_code{ + boost::system::errc::bad_address, + boost::system::system_category()}); return; } - mStatus = beast::lexicalCastThrow (std::string (smMatch[1])); + mStatus = beast::lexicalCastThrow(std::string(smMatch[1])); - if (boost::regex_match (strHeader, smMatch, reBody)) // we got some body + if (boost::regex_match(strHeader, smMatch, reBody)) // we got some body mBody = smMatch[1]; std::size_t const responseSize = [&] { if (boost::regex_match(strHeader, smMatch, reSize)) - return beast::lexicalCast ( + return beast::lexicalCast( std::string(smMatch[1]), maxResponseSize_); return maxResponseSize_; }(); if (responseSize > maxResponseSize_) { - JLOG (j_.trace()) << "Response field too large"; - invokeComplete ( - boost::system::error_code { - boost::system::errc::value_too_large, - boost::system::system_category ()}); + JLOG(j_.trace()) << "Response field too large"; + invokeComplete(boost::system::error_code{ + boost::system::errc::value_too_large, + boost::system::system_category()}); return; } if (responseSize == 0) { // no body wanted or available - invokeComplete (ecResult, mStatus); + invokeComplete(ecResult, mStatus); } - else if (mBody.size () >= responseSize) + else if (mBody.size() >= responseSize) { // we got the whole thing - invokeComplete (ecResult, mStatus, mBody); + invokeComplete(ecResult, mStatus, mBody); } else { - mSocket.async_read ( - mResponse.prepare (responseSize - mBody.size ()), - boost::asio::transfer_all (), - std::bind (&HTTPClientImp::handleData, - shared_from_this (), - std::placeholders::_1, - std::placeholders::_2)); + mSocket.async_read( + mResponse.prepare(responseSize - mBody.size()), + boost::asio::transfer_all(), + std::bind( + &HTTPClientImp::handleData, + shared_from_this(), + std::placeholders::_1, + std::placeholders::_2)); } } - void handleData ( + void + handleData( const boost::system::error_code& ecResult, std::size_t bytes_transferred) { if (!mShutdown) - mShutdown = ecResult; + mShutdown = ecResult; if (mShutdown && mShutdown != boost::asio::error::eof) { - JLOG (j_.trace()) << "Read error: " << mShutdown.message (); + JLOG(j_.trace()) << "Read error: " << mShutdown.message(); - invokeComplete (mShutdown); + invokeComplete(mShutdown); } else { if (mShutdown) { - JLOG (j_.trace()) << "Complete."; + JLOG(j_.trace()) << "Complete."; } else { - mResponse.commit (bytes_transferred); - std::string strBody { - {std::istreambuf_iterator (&mResponse)}, - std::istreambuf_iterator ()}; - invokeComplete (ecResult, mStatus, mBody + strBody); + mResponse.commit(bytes_transferred); + std::string strBody{ + {std::istreambuf_iterator(&mResponse)}, + std::istreambuf_iterator()}; + invokeComplete(ecResult, mStatus, mBody + strBody); } } } // Call cancel the deadline timer and invoke the completion routine. - void invokeComplete ( + void + invokeComplete( const boost::system::error_code& ecResult, int iStatus = 0, std::string const& strData = "") { boost::system::error_code ecCancel; - (void) mDeadline.cancel (ecCancel); + (void)mDeadline.cancel(ecCancel); if (ecCancel) { - JLOG (j_.trace()) << "invokeComplete: Deadline cancel error: " - << ecCancel.message (); + JLOG(j_.trace()) << "invokeComplete: Deadline cancel error: " + << ecCancel.message(); } - JLOG (j_.debug()) << "invokeComplete: Deadline popping: " - << mDeqSites.size (); + JLOG(j_.debug()) << "invokeComplete: Deadline popping: " + << mDeqSites.size(); - if (!mDeqSites.empty ()) + if (!mDeqSites.empty()) { - mDeqSites.pop_front (); + mDeqSites.pop_front(); } - bool bAgain = true; + bool bAgain = true; - if (mDeqSites.empty () || !ecResult) + if (mDeqSites.empty() || !ecResult) { // ecResult: !0 = had an error, last entry // iStatus: result, if no error // strData: data, if no error - bAgain = mComplete && - mComplete (ecResult ? ecResult : ecCancel, iStatus, strData); + bAgain = mComplete && + mComplete(ecResult ? ecResult : ecCancel, iStatus, strData); } - if (!mDeqSites.empty () && bAgain) + if (!mDeqSites.empty() && bAgain) { - httpsNext (); + httpsNext(); } } private: using pointer = std::shared_ptr; - bool mSSL; - AutoSocket mSocket; - boost::asio::ip::tcp::resolver mResolver; - std::shared_ptr mQuery; - boost::asio::streambuf mRequest; - boost::asio::streambuf mHeader; - boost::asio::streambuf mResponse; - std::string mBody; - const unsigned short mPort; - std::size_t const maxResponseSize_; - int mStatus; - std::function - mBuild; - std::function mQuery; + boost::asio::streambuf mRequest; + boost::asio::streambuf mHeader; + boost::asio::streambuf mResponse; + std::string mBody; + const unsigned short mPort; + std::size_t const maxResponseSize_; + int mStatus; + std::function + mBuild; + std::function mComplete; + std::string const& strData)> + mComplete; boost::asio::basic_waitable_timer mDeadline; // If not success, we are shutting down. - boost::system::error_code mShutdown; + boost::system::error_code mShutdown; - std::deque mDeqSites; - std::chrono::seconds mTimeout; - beast::Journal j_; + std::deque mDeqSites; + std::chrono::seconds mTimeout; + beast::Journal j_; }; //------------------------------------------------------------------------------ -void HTTPClient::get ( +void +HTTPClient::get( bool bSSL, boost::asio::io_service& io_service, std::deque deqSites, @@ -537,16 +548,19 @@ void HTTPClient::get ( std::string const& strPath, std::size_t responseMax, std::chrono::seconds timeout, - std::function complete, beast::Journal& j) { - auto client = std::make_shared ( - io_service, port, responseMax, j); - client->get (bSSL, deqSites, strPath, timeout, complete); + auto client = + std::make_shared(io_service, port, responseMax, j); + client->get(bSSL, deqSites, strPath, timeout, complete); } -void HTTPClient::get ( +void +HTTPClient::get( bool bSSL, boost::asio::io_service& io_service, std::string strSite, @@ -554,15 +568,17 @@ void HTTPClient::get ( std::string const& strPath, std::size_t responseMax, std::chrono::seconds timeout, - std::function complete, beast::Journal& j) { - std::deque deqSites (1, strSite); + std::deque deqSites(1, strSite); - auto client = std::make_shared ( - io_service, port, responseMax, j); - client->get (bSSL, deqSites, strPath, timeout, complete); + auto client = + std::make_shared(io_service, port, responseMax, j); + client->get(bSSL, deqSites, strPath, timeout, complete); } void @@ -571,21 +587,21 @@ HTTPClient::request( boost::asio::io_service& io_service, std::string strSite, const unsigned short port, - std::function + std::function setRequest, std::size_t responseMax, std::chrono::seconds timeout, - std::function complete, beast::Journal& j) { - std::deque deqSites (1, strSite); + std::deque deqSites(1, strSite); - auto client = std::make_shared ( - io_service, port, responseMax, j); - client->request (bSSL, deqSites, setRequest, timeout, complete); + auto client = + std::make_shared(io_service, port, responseMax, j); + client->request(bSSL, deqSites, setRequest, timeout, complete); } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/InfoSub.cpp b/src/ripple/net/impl/InfoSub.cpp index 0513ec1926..a13b7f736b 100644 --- a/src/ripple/net/impl/InfoSub.cpp +++ b/src/ripple/net/impl/InfoSub.cpp @@ -35,95 +35,97 @@ namespace ripple { //------------------------------------------------------------------------------ -InfoSub::Source::Source (char const* name, Stoppable& parent) - : Stoppable (name, parent) +InfoSub::Source::Source(char const* name, Stoppable& parent) + : Stoppable(name, parent) { } //------------------------------------------------------------------------------ -InfoSub::InfoSub(Source& source) - : m_source(source) - , mSeq(assign_id()) +InfoSub::InfoSub(Source& source) : m_source(source), mSeq(assign_id()) { } InfoSub::InfoSub(Source& source, Consumer consumer) - : m_consumer(consumer) - , m_source(source) - , mSeq(assign_id()) + : m_consumer(consumer), m_source(source), mSeq(assign_id()) { } -InfoSub::~InfoSub () +InfoSub::~InfoSub() { - m_source.unsubTransactions (mSeq); - m_source.unsubRTTransactions (mSeq); - m_source.unsubLedger (mSeq); - m_source.unsubManifests (mSeq); - m_source.unsubServer (mSeq); - m_source.unsubValidations (mSeq); - m_source.unsubPeerStatus (mSeq); - m_source.unsubConsensus (mSeq); + m_source.unsubTransactions(mSeq); + m_source.unsubRTTransactions(mSeq); + m_source.unsubLedger(mSeq); + m_source.unsubManifests(mSeq); + m_source.unsubServer(mSeq); + m_source.unsubValidations(mSeq); + m_source.unsubPeerStatus(mSeq); + m_source.unsubConsensus(mSeq); // Use the internal unsubscribe so that it won't call // back to us and modify its own parameter - if (! realTimeSubscriptions_.empty ()) - m_source.unsubAccountInternal - (mSeq, realTimeSubscriptions_, true); + if (!realTimeSubscriptions_.empty()) + m_source.unsubAccountInternal(mSeq, realTimeSubscriptions_, true); - if (! normalSubscriptions_.empty ()) - m_source.unsubAccountInternal - (mSeq, normalSubscriptions_, false); + if (!normalSubscriptions_.empty()) + m_source.unsubAccountInternal(mSeq, normalSubscriptions_, false); } -Resource::Consumer& InfoSub::getConsumer() +Resource::Consumer& +InfoSub::getConsumer() { return m_consumer; } -std::uint64_t InfoSub::getSeq () +std::uint64_t +InfoSub::getSeq() { return mSeq; } -void InfoSub::onSendEmpty () +void +InfoSub::onSendEmpty() { } -void InfoSub::insertSubAccountInfo (AccountID const& account, bool rt) +void +InfoSub::insertSubAccountInfo(AccountID const& account, bool rt) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); if (rt) - realTimeSubscriptions_.insert (account); + realTimeSubscriptions_.insert(account); else - normalSubscriptions_.insert (account); + normalSubscriptions_.insert(account); } -void InfoSub::deleteSubAccountInfo (AccountID const& account, bool rt) +void +InfoSub::deleteSubAccountInfo(AccountID const& account, bool rt) { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); if (rt) - realTimeSubscriptions_.erase (account); + realTimeSubscriptions_.erase(account); else - normalSubscriptions_.erase (account); + normalSubscriptions_.erase(account); } -void InfoSub::clearPathRequest () +void +InfoSub::clearPathRequest() { - mPathRequest.reset (); + mPathRequest.reset(); } -void InfoSub::setPathRequest (const std::shared_ptr& req) +void +InfoSub::setPathRequest(const std::shared_ptr& req) { mPathRequest = req; } -const std::shared_ptr& InfoSub::getPathRequest () +const std::shared_ptr& +InfoSub::getPathRequest() { return mPathRequest; } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/RPCCall.cpp b/src/ripple/net/impl/RPCCall.cpp index bca8fefb5b..a565d4bc32 100644 --- a/src/ripple/net/impl/RPCCall.cpp +++ b/src/ripple/net/impl/RPCCall.cpp @@ -18,30 +18,30 @@ //============================================================================== #include -#include #include -#include -#include +#include +#include #include #include -#include +#include #include +#include #include #include -#include #include +#include +#include #include #include -#include #include #include +#include #include #include -#include #include -#include #include +#include #include #include @@ -61,7 +61,8 @@ class RPCParser; // and to be compatible with other JSON-RPC implementations. // -std::string createHTTPPost ( +std::string +createHTTPPost( std::string const& strHost, std::string const& strPath, std::string const& strMsg, @@ -73,21 +74,19 @@ std::string createHTTPPost ( // this by design or an accident or should it be using // BuildInfo::getFullVersionString () as well? - s << "POST " - << (strPath.empty () ? "/" : strPath) - << " HTTP/1.0\r\n" - << "User-Agent: " << systemName () << "-json-rpc/v1\r\n" + s << "POST " << (strPath.empty() ? "/" : strPath) << " HTTP/1.0\r\n" + << "User-Agent: " << systemName() << "-json-rpc/v1\r\n" << "Host: " << strHost << "\r\n" << "Content-Type: application/json\r\n" - << "Content-Length: " << strMsg.size () << "\r\n" + << "Content-Length: " << strMsg.size() << "\r\n" << "Accept: application/json\r\n"; - for (auto const& [k,v] : mapRequestHeaders) + for (auto const& [k, v] : mapRequestHeaders) s << k << ": " << v << "\r\n"; s << "\r\n" << strMsg; - return s.str (); + return s.str(); } class RPCParser @@ -95,62 +94,69 @@ class RPCParser private: beast::Journal const j_; - // TODO New routine for parsing ledger parameters, other routines should standardize on this. - static bool jvParseLedger (Json::Value& jvRequest, std::string const& strLedger) + // TODO New routine for parsing ledger parameters, other routines should + // standardize on this. + static bool + jvParseLedger(Json::Value& jvRequest, std::string const& strLedger) { - if (strLedger == "current" || strLedger == "closed" || strLedger == "validated") + if (strLedger == "current" || strLedger == "closed" || + strLedger == "validated") { - jvRequest[jss::ledger_index] = strLedger; + jvRequest[jss::ledger_index] = strLedger; } - else if (strLedger.length () == 64) + else if (strLedger.length() == 64) { // YYY Could confirm this is a uint256. - jvRequest[jss::ledger_hash] = strLedger; + jvRequest[jss::ledger_hash] = strLedger; } else { - jvRequest[jss::ledger_index] = beast::lexicalCast (strLedger); + jvRequest[jss::ledger_index] = + beast::lexicalCast(strLedger); } return true; } // Build a object { "currency" : "XYZ", "issuer" : "rXYX" } - static Json::Value jvParseCurrencyIssuer (std::string const& strCurrencyIssuer) + static Json::Value + jvParseCurrencyIssuer(std::string const& strCurrencyIssuer) { - static boost::regex reCurIss ("\\`([[:alpha:]]{3})(?:/(.+))?\\'"); + static boost::regex reCurIss("\\`([[:alpha:]]{3})(?:/(.+))?\\'"); - boost::smatch smMatch; + boost::smatch smMatch; - if (boost::regex_match (strCurrencyIssuer, smMatch, reCurIss)) + if (boost::regex_match(strCurrencyIssuer, smMatch, reCurIss)) { - Json::Value jvResult (Json::objectValue); + Json::Value jvResult(Json::objectValue); std::string strCurrency = smMatch[1]; - std::string strIssuer = smMatch[2]; + std::string strIssuer = smMatch[2]; - jvResult[jss::currency] = strCurrency; + jvResult[jss::currency] = strCurrency; - if (strIssuer.length ()) + if (strIssuer.length()) { // Could confirm issuer is a valid Ripple address. - jvResult[jss::issuer] = strIssuer; + jvResult[jss::issuer] = strIssuer; } return jvResult; } else { - return RPC::make_param_error (std::string ("Invalid currency/issuer '") + - strCurrencyIssuer + "'"); + return RPC::make_param_error( + std::string("Invalid currency/issuer '") + strCurrencyIssuer + + "'"); } } - static bool validPublicKey (std::string const& strPk) + static bool + validPublicKey(std::string const& strPk) { - if (parseBase58 (TokenType::AccountPublic, strPk)) + if (parseBase58(TokenType::AccountPublic, strPk)) return true; - auto pkHex = strUnHex (strPk); + auto pkHex = strUnHex(strPk); if (!pkHex) return false; @@ -161,23 +167,26 @@ private: } private: - using parseFuncPtr = Json::Value (RPCParser::*) (Json::Value const& jvParams); + using parseFuncPtr = + Json::Value (RPCParser::*)(Json::Value const& jvParams); - Json::Value parseAsIs (Json::Value const& jvParams) + Json::Value + parseAsIs(Json::Value const& jvParams) { - Json::Value v (Json::objectValue); + Json::Value v(Json::objectValue); - if (jvParams.isArray() && (jvParams.size () > 0)) + if (jvParams.isArray() && (jvParams.size() > 0)) v[jss::params] = jvParams; return v; } - Json::Value parseDownloadShard(Json::Value const& jvParams) + Json::Value + parseDownloadShard(Json::Value const& jvParams) { Json::Value jvResult(Json::objectValue); - unsigned int sz {jvParams.size()}; - unsigned int i {0}; + unsigned int sz{jvParams.size()}; + unsigned int i{0}; // If odd number of params then 'novalidate' may have been specified if (sz & 1) @@ -202,44 +211,47 @@ private: return jvResult; } - Json::Value parseInternal (Json::Value const& jvParams) + Json::Value + parseInternal(Json::Value const& jvParams) { - Json::Value v (Json::objectValue); + Json::Value v(Json::objectValue); v[jss::internal_command] = jvParams[0u]; - Json::Value params (Json::arrayValue); + Json::Value params(Json::arrayValue); - for (unsigned i = 1; i < jvParams.size (); ++i) - params.append (jvParams[i]); + for (unsigned i = 1; i < jvParams.size(); ++i) + params.append(jvParams[i]); v[jss::params] = params; return v; } - Json::Value parseManifest (Json::Value const& jvParams) + Json::Value + parseManifest(Json::Value const& jvParams) { - if (jvParams.size () == 1) + if (jvParams.size() == 1) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - std::string const strPk = jvParams[0u].asString (); - if (!validPublicKey (strPk)) - return rpcError (rpcPUBLIC_MALFORMED); + std::string const strPk = jvParams[0u].asString(); + if (!validPublicKey(strPk)) + return rpcError(rpcPUBLIC_MALFORMED); jvRequest[jss::public_key] = strPk; return jvRequest; } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } // fetch_info [clear] - Json::Value parseFetchInfo (Json::Value const& jvParams) + Json::Value + parseFetchInfo(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); - unsigned int iParams = jvParams.size (); + Json::Value jvRequest(Json::objectValue); + unsigned int iParams = jvParams.size(); if (iParams != 0) jvRequest[jvParams[0u].asString()] = true; @@ -247,43 +259,43 @@ private: return jvRequest; } - // account_tx accountID [ledger_min [ledger_max [limit [offset]]]] [binary] [count] [descending] + // account_tx accountID [ledger_min [ledger_max [limit [offset]]]] [binary] + // [count] [descending] Json::Value - parseAccountTransactions (Json::Value const& jvParams) + parseAccountTransactions(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); - unsigned int iParams = jvParams.size (); + Json::Value jvRequest(Json::objectValue); + unsigned int iParams = jvParams.size(); - auto const account = - parseBase58(jvParams[0u].asString()); - if (! account) - return rpcError (rpcACT_MALFORMED); + auto const account = parseBase58(jvParams[0u].asString()); + if (!account) + return rpcError(rpcACT_MALFORMED); - jvRequest[jss::account]= toBase58(*account); + jvRequest[jss::account] = toBase58(*account); - bool bDone = false; + bool bDone = false; while (!bDone && iParams >= 2) { // VFALCO Why is Json::StaticString appearing on the right side? - if (jvParams[iParams - 1].asString () == jss::binary) + if (jvParams[iParams - 1].asString() == jss::binary) { - jvRequest[jss::binary] = true; + jvRequest[jss::binary] = true; --iParams; } - else if (jvParams[iParams - 1].asString () == jss::count) + else if (jvParams[iParams - 1].asString() == jss::count) { - jvRequest[jss::count] = true; + jvRequest[jss::count] = true; --iParams; } - else if (jvParams[iParams - 1].asString () == jss::descending) + else if (jvParams[iParams - 1].asString() == jss::descending) { jvRequest[jss::descending] = true; --iParams; } else { - bDone = true; + bDone = true; } } @@ -292,67 +304,68 @@ private: } else if (2 == iParams) { - if (!jvParseLedger (jvRequest, jvParams[1u].asString ())) + if (!jvParseLedger(jvRequest, jvParams[1u].asString())) return jvRequest; } else { - std::int64_t uLedgerMin = jvParams[1u].asInt (); - std::int64_t uLedgerMax = jvParams[2u].asInt (); + std::int64_t uLedgerMin = jvParams[1u].asInt(); + std::int64_t uLedgerMax = jvParams[2u].asInt(); if (uLedgerMax != -1 && uLedgerMax < uLedgerMin) { - return rpcError (rpcLGR_IDXS_INVALID); + return rpcError(rpcLGR_IDXS_INVALID); } - jvRequest[jss::ledger_index_min] = jvParams[1u].asInt (); - jvRequest[jss::ledger_index_max] = jvParams[2u].asInt (); + jvRequest[jss::ledger_index_min] = jvParams[1u].asInt(); + jvRequest[jss::ledger_index_max] = jvParams[2u].asInt(); if (iParams >= 4) - jvRequest[jss::limit] = jvParams[3u].asInt (); + jvRequest[jss::limit] = jvParams[3u].asInt(); if (iParams >= 5) - jvRequest[jss::offset] = jvParams[4u].asInt (); + jvRequest[jss::offset] = jvParams[4u].asInt(); } return jvRequest; } - // tx_account accountID [ledger_min [ledger_max [limit]]]] [binary] [count] [forward] - Json::Value parseTxAccount (Json::Value const& jvParams) + // tx_account accountID [ledger_min [ledger_max [limit]]]] [binary] [count] + // [forward] + Json::Value + parseTxAccount(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); - unsigned int iParams = jvParams.size (); + Json::Value jvRequest(Json::objectValue); + unsigned int iParams = jvParams.size(); - auto const account = - parseBase58(jvParams[0u].asString()); - if (! account) - return rpcError (rpcACT_MALFORMED); + auto const account = parseBase58(jvParams[0u].asString()); + if (!account) + return rpcError(rpcACT_MALFORMED); - jvRequest[jss::account] = toBase58(*account); + jvRequest[jss::account] = toBase58(*account); - bool bDone = false; + bool bDone = false; while (!bDone && iParams >= 2) { - if (jvParams[iParams - 1].asString () == jss::binary) + if (jvParams[iParams - 1].asString() == jss::binary) { - jvRequest[jss::binary] = true; + jvRequest[jss::binary] = true; --iParams; } - else if (jvParams[iParams - 1].asString () == jss::count) + else if (jvParams[iParams - 1].asString() == jss::count) { - jvRequest[jss::count] = true; + jvRequest[jss::count] = true; --iParams; } - else if (jvParams[iParams - 1].asString () == jss::forward) + else if (jvParams[iParams - 1].asString() == jss::forward) { jvRequest[jss::forward] = true; --iParams; } else { - bDone = true; + bDone = true; } } @@ -361,42 +374,44 @@ private: } else if (2 == iParams) { - if (!jvParseLedger (jvRequest, jvParams[1u].asString ())) + if (!jvParseLedger(jvRequest, jvParams[1u].asString())) return jvRequest; } else { - std::int64_t uLedgerMin = jvParams[1u].asInt (); - std::int64_t uLedgerMax = jvParams[2u].asInt (); + std::int64_t uLedgerMin = jvParams[1u].asInt(); + std::int64_t uLedgerMax = jvParams[2u].asInt(); if (uLedgerMax != -1 && uLedgerMax < uLedgerMin) { - return rpcError (rpcLGR_IDXS_INVALID); + return rpcError(rpcLGR_IDXS_INVALID); } - jvRequest[jss::ledger_index_min] = jvParams[1u].asInt (); - jvRequest[jss::ledger_index_max] = jvParams[2u].asInt (); + jvRequest[jss::ledger_index_min] = jvParams[1u].asInt(); + jvRequest[jss::ledger_index_max] = jvParams[2u].asInt(); if (iParams >= 4) - jvRequest[jss::limit] = jvParams[3u].asInt (); + jvRequest[jss::limit] = jvParams[3u].asInt(); } return jvRequest; } - // book_offers [ [ [ [ []]]]] - // limit: 0 = no limit - // proof: 0 or 1 + // book_offers [ [ [ + // [ []]]]] limit: 0 = no limit proof: 0 or 1 // // Mnemonic: taker pays --> offer --> taker gets - Json::Value parseBookOffers (Json::Value const& jvParams) + Json::Value + parseBookOffers(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - Json::Value jvTakerPays = jvParseCurrencyIssuer (jvParams[0u].asString ()); - Json::Value jvTakerGets = jvParseCurrencyIssuer (jvParams[1u].asString ()); + Json::Value jvTakerPays = + jvParseCurrencyIssuer(jvParams[0u].asString()); + Json::Value jvTakerGets = + jvParseCurrencyIssuer(jvParams[1u].asString()); - if (isRpcError (jvTakerPays)) + if (isRpcError(jvTakerPays)) { return jvTakerPays; } @@ -405,7 +420,7 @@ private: jvRequest[jss::taker_pays] = jvTakerPays; } - if (isRpcError (jvTakerGets)) + if (isRpcError(jvTakerGets)) { return jvTakerGets; } @@ -414,44 +429,45 @@ private: jvRequest[jss::taker_gets] = jvTakerGets; } - if (jvParams.size () >= 3) + if (jvParams.size() >= 3) { - jvRequest[jss::issuer] = jvParams[2u].asString (); + jvRequest[jss::issuer] = jvParams[2u].asString(); } - if (jvParams.size () >= 4 && !jvParseLedger (jvRequest, jvParams[3u].asString ())) + if (jvParams.size() >= 4 && + !jvParseLedger(jvRequest, jvParams[3u].asString())) return jvRequest; - if (jvParams.size () >= 5) + if (jvParams.size() >= 5) { - int iLimit = jvParams[5u].asInt (); + int iLimit = jvParams[5u].asInt(); if (iLimit > 0) - jvRequest[jss::limit] = iLimit; + jvRequest[jss::limit] = iLimit; } - if (jvParams.size () >= 6 && jvParams[5u].asInt ()) + if (jvParams.size() >= 6 && jvParams[5u].asInt()) { - jvRequest[jss::proof] = true; + jvRequest[jss::proof] = true; } - if (jvParams.size () == 7) + if (jvParams.size() == 7) jvRequest[jss::marker] = jvParams[6u]; return jvRequest; } // can_delete [||now|always|never] - Json::Value parseCanDelete (Json::Value const& jvParams) + Json::Value + parseCanDelete(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - if (!jvParams.size ()) + if (!jvParams.size()) return jvRequest; std::string input = jvParams[0u].asString(); - if (input.find_first_not_of("0123456789") == - std::string::npos) + if (input.find_first_not_of("0123456789") == std::string::npos) jvRequest["can_delete"] = jvParams[0u].asUInt(); else jvRequest["can_delete"] = input; @@ -460,91 +476,98 @@ private: } // connect [port] - Json::Value parseConnect (Json::Value const& jvParams) + Json::Value + parseConnect(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - jvRequest[jss::ip] = jvParams[0u].asString (); + jvRequest[jss::ip] = jvParams[0u].asString(); - if (jvParams.size () == 2) - jvRequest[jss::port] = jvParams[1u].asUInt (); + if (jvParams.size() == 2) + jvRequest[jss::port] = jvParams[1u].asUInt(); return jvRequest; } // deposit_authorized [] - Json::Value parseDepositAuthorized (Json::Value const& jvParams) + Json::Value + parseDepositAuthorized(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); - jvRequest[jss::source_account] = jvParams[0u].asString (); - jvRequest[jss::destination_account] = jvParams[1u].asString (); + Json::Value jvRequest(Json::objectValue); + jvRequest[jss::source_account] = jvParams[0u].asString(); + jvRequest[jss::destination_account] = jvParams[1u].asString(); - if (jvParams.size () == 3) - jvParseLedger (jvRequest, jvParams[2u].asString ()); + if (jvParams.size() == 3) + jvParseLedger(jvRequest, jvParams[2u].asString()); return jvRequest; } // Return an error for attemping to subscribe/unsubscribe via RPC. - Json::Value parseEvented (Json::Value const& jvParams) + Json::Value + parseEvented(Json::Value const& jvParams) { - return rpcError (rpcNO_EVENTS); + return rpcError(rpcNO_EVENTS); } // feature [] [accept|reject] - Json::Value parseFeature (Json::Value const& jvParams) + Json::Value + parseFeature(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - if (jvParams.size () > 0) - jvRequest[jss::feature] = jvParams[0u].asString (); + if (jvParams.size() > 0) + jvRequest[jss::feature] = jvParams[0u].asString(); - if (jvParams.size () > 1) + if (jvParams.size() > 1) { - auto const action = jvParams[1u].asString (); + auto const action = jvParams[1u].asString(); // This may look reversed, but it's intentional: jss::vetoed // determines whether an amendment is vetoed - so "reject" means // that jss::vetoed is true. if (boost::iequals(action, "reject")) - jvRequest[jss::vetoed] = Json::Value (true); + jvRequest[jss::vetoed] = Json::Value(true); else if (boost::iequals(action, "accept")) - jvRequest[jss::vetoed] = Json::Value (false); + jvRequest[jss::vetoed] = Json::Value(false); else - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } return jvRequest; } // get_counts [] - Json::Value parseGetCounts (Json::Value const& jvParams) + Json::Value + parseGetCounts(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - if (jvParams.size ()) - jvRequest[jss::min_count] = jvParams[0u].asUInt (); + if (jvParams.size()) + jvRequest[jss::min_count] = jvParams[0u].asUInt(); return jvRequest; } // sign_for offline // sign_for - Json::Value parseSignFor (Json::Value const& jvParams) + Json::Value + parseSignFor(Json::Value const& jvParams) { - bool const bOffline = 4 == jvParams.size () && jvParams[3u].asString () == "offline"; + bool const bOffline = + 4 == jvParams.size() && jvParams[3u].asString() == "offline"; - if (3 == jvParams.size () || bOffline) + if (3 == jvParams.size() || bOffline) { Json::Value txJSON; Json::Reader reader; - if (reader.parse (jvParams[2u].asString (), txJSON)) + if (reader.parse(jvParams[2u].asString(), txJSON)) { // sign_for txJSON. Json::Value jvRequest{Json::objectValue}; - jvRequest[jss::account] = jvParams[0u].asString (); - jvRequest[jss::secret] = jvParams[1u].asString (); + jvRequest[jss::account] = jvParams[0u].asString(); + jvRequest[jss::secret] = jvParams[1u].asString(); jvRequest[jss::tx_json] = txJSON; if (bOffline) @@ -553,32 +576,34 @@ private: return jvRequest; } } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } // json - Json::Value parseJson (Json::Value const& jvParams) + Json::Value + parseJson(Json::Value const& jvParams) { - Json::Reader reader; - Json::Value jvRequest; + Json::Reader reader; + Json::Value jvRequest; - JLOG (j_.trace()) << "RPC method: " << jvParams[0u]; - JLOG (j_.trace()) << "RPC json: " << jvParams[1u]; + JLOG(j_.trace()) << "RPC method: " << jvParams[0u]; + JLOG(j_.trace()) << "RPC json: " << jvParams[1u]; - if (reader.parse (jvParams[1u].asString (), jvRequest)) + if (reader.parse(jvParams[1u].asString(), jvRequest)) { - if (!jvRequest.isObjectOrNull ()) - return rpcError (rpcINVALID_PARAMS); + if (!jvRequest.isObjectOrNull()) + return rpcError(rpcINVALID_PARAMS); jvRequest[jss::method] = jvParams[0u]; return jvRequest; } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } - bool isValidJson2(Json::Value const& jv) + bool + isValidJson2(Json::Value const& jv) { if (jv.isArray()) { @@ -598,8 +623,8 @@ private: jv.isMember(jss::id) && jv.isMember(jss::method)) { if (jv.isMember(jss::params) && - !(jv[jss::params].isNull() || jv[jss::params].isArray() || - jv[jss::params].isObject())) + !(jv[jss::params].isNull() || jv[jss::params].isArray() || + jv[jss::params].isObject())) return false; return true; } @@ -607,7 +632,8 @@ private: return false; } - Json::Value parseJson2(Json::Value const& jvParams) + Json::Value + parseJson2(Json::Value const& jvParams) { Json::Reader reader; Json::Value jv; @@ -657,24 +683,25 @@ private: } // ledger [id|index|current|closed|validated] [full|tx] - Json::Value parseLedger (Json::Value const& jvParams) + Json::Value + parseLedger(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - if (!jvParams.size ()) + if (!jvParams.size()) { return jvRequest; } - jvParseLedger (jvRequest, jvParams[0u].asString ()); + jvParseLedger(jvRequest, jvParams[0u].asString()); - if (2 == jvParams.size ()) + if (2 == jvParams.size()) { - if (jvParams[1u].asString () == "full") + if (jvParams[1u].asString() == "full") { - jvRequest[jss::full] = true; + jvRequest[jss::full] = true; } - else if (jvParams[1u].asString () == "tx") + else if (jvParams[1u].asString() == "tx") { jvRequest[jss::transactions] = true; jvRequest[jss::expand] = true; @@ -685,39 +712,43 @@ private: } // ledger_header | - Json::Value parseLedgerId (Json::Value const& jvParams) + Json::Value + parseLedgerId(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - std::string strLedger = jvParams[0u].asString (); + std::string strLedger = jvParams[0u].asString(); - if (strLedger.length () == 64) + if (strLedger.length() == 64) { - jvRequest[jss::ledger_hash] = strLedger; + jvRequest[jss::ledger_hash] = strLedger; } else { - jvRequest[jss::ledger_index] = beast::lexicalCast (strLedger); + jvRequest[jss::ledger_index] = + beast::lexicalCast(strLedger); } return jvRequest; } // log_level: Get log levels - // log_level : Set master log level to the specified severity - // log_level : Set specified partition to specified severity - Json::Value parseLogLevel (Json::Value const& jvParams) + // log_level : Set master log level to the + // specified severity log_level : Set specified + // partition to specified severity + Json::Value + parseLogLevel(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - if (jvParams.size () == 1) + if (jvParams.size() == 1) { - jvRequest[jss::severity] = jvParams[0u].asString (); + jvRequest[jss::severity] = jvParams[0u].asString(); } - else if (jvParams.size () == 2) + else if (jvParams.size() == 2) { - jvRequest[jss::partition] = jvParams[0u].asString (); - jvRequest[jss::severity] = jvParams[1u].asString (); + jvRequest[jss::partition] = jvParams[0u].asString(); + jvRequest[jss::severity] = jvParams[1u].asString(); } return jvRequest; @@ -728,32 +759,37 @@ private: // account_info | [strict] // account_info || [] [strict] // account_offers | [] [strict] - Json::Value parseAccountItems (Json::Value const& jvParams) + Json::Value + parseAccountItems(Json::Value const& jvParams) { - return parseAccountRaw1 (jvParams); + return parseAccountRaw1(jvParams); } - Json::Value parseAccountCurrencies (Json::Value const& jvParams) + Json::Value + parseAccountCurrencies(Json::Value const& jvParams) { - return parseAccountRaw1 (jvParams); + return parseAccountRaw1(jvParams); } // account_lines |"" [] - Json::Value parseAccountLines (Json::Value const& jvParams) + Json::Value + parseAccountLines(Json::Value const& jvParams) { - return parseAccountRaw2 (jvParams, jss::peer); + return parseAccountRaw2(jvParams, jss::peer); } // account_channels |"" [] - Json::Value parseAccountChannels (Json::Value const& jvParams) + Json::Value + parseAccountChannels(Json::Value const& jvParams) { - return parseAccountRaw2 (jvParams, jss::destination_account); + return parseAccountRaw2(jvParams, jss::destination_account); } // channel_authorize: [] - Json::Value parseChannelAuthorize (Json::Value const& jvParams) + Json::Value + parseChannelAuthorize(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); unsigned int index = 0; @@ -763,7 +799,7 @@ private: index++; if (!keyTypeFromString(jvParams[index].asString())) - return rpcError (rpcBAD_KEY_TYPE); + return rpcError(rpcBAD_KEY_TYPE); jvRequest[jss::key_type] = jvParams[index]; index++; } @@ -776,82 +812,85 @@ private: { // verify the channel id is a valid 256 bit number uint256 channelId; - if (!channelId.SetHexExact (jvParams[index].asString())) - return rpcError (rpcCHANNEL_MALFORMED); + if (!channelId.SetHexExact(jvParams[index].asString())) + return rpcError(rpcCHANNEL_MALFORMED); jvRequest[jss::channel_id] = to_string(channelId); index++; } - if (!jvParams[index].isString() || !to_uint64(jvParams[index].asString())) + if (!jvParams[index].isString() || + !to_uint64(jvParams[index].asString())) return rpcError(rpcCHANNEL_AMT_MALFORMED); jvRequest[jss::amount] = jvParams[index]; - // If additional parameters are appended, be sure to increment index here + // If additional parameters are appended, be sure to increment index + // here return jvRequest; } // channel_verify - Json::Value parseChannelVerify (Json::Value const& jvParams) + Json::Value + parseChannelVerify(Json::Value const& jvParams) { - std::string const strPk = jvParams[0u].asString (); + std::string const strPk = jvParams[0u].asString(); if (!validPublicKey(strPk)) - return rpcError (rpcPUBLIC_MALFORMED); + return rpcError(rpcPUBLIC_MALFORMED); - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); jvRequest[jss::public_key] = strPk; { // verify the channel id is a valid 256 bit number uint256 channelId; - if (!channelId.SetHexExact (jvParams[1u].asString ())) - return rpcError (rpcCHANNEL_MALFORMED); + if (!channelId.SetHexExact(jvParams[1u].asString())) + return rpcError(rpcCHANNEL_MALFORMED); } - jvRequest[jss::channel_id] = jvParams[1u].asString (); + jvRequest[jss::channel_id] = jvParams[1u].asString(); if (!jvParams[2u].isString() || !to_uint64(jvParams[2u].asString())) return rpcError(rpcCHANNEL_AMT_MALFORMED); jvRequest[jss::amount] = jvParams[2u]; - jvRequest[jss::signature] = jvParams[3u].asString (); + jvRequest[jss::signature] = jvParams[3u].asString(); return jvRequest; } - Json::Value parseAccountRaw2 (Json::Value const& jvParams, - char const * const acc2Field) + Json::Value + parseAccountRaw2(Json::Value const& jvParams, char const* const acc2Field) { std::array accFields{{jss::account, acc2Field}}; - auto const nParams = jvParams.size (); - Json::Value jvRequest (Json::objectValue); + auto const nParams = jvParams.size(); + Json::Value jvRequest(Json::objectValue); for (auto i = 0; i < nParams; ++i) { - std::string strParam = jvParams[i].asString (); + std::string strParam = jvParams[i].asString(); - if (i==1 && strParam.empty()) + if (i == 1 && strParam.empty()) continue; // Parameters 0 and 1 are accounts if (i < 2) { - if (parseBase58 ( + if (parseBase58( TokenType::AccountPublic, strParam) || - parseBase58 (strParam) || - parseGenericSeed (strParam)) + parseBase58(strParam) || + parseGenericSeed(strParam)) { - jvRequest[accFields[i]] = std::move (strParam); + jvRequest[accFields[i]] = std::move(strParam); } else { - return rpcError (rpcACT_MALFORMED); + return rpcError(rpcACT_MALFORMED); } } else { - if (jvParseLedger (jvRequest, strParam)) + if (jvParseLedger(jvRequest, strParam)) return jvRequest; - return rpcError (rpcLGR_IDX_MALFORMED); + return rpcError(rpcLGR_IDX_MALFORMED); } } @@ -859,11 +898,12 @@ private: } // TODO: Get index from an alternate syntax: rXYZ: - Json::Value parseAccountRaw1 (Json::Value const& jvParams) + Json::Value + parseAccountRaw1(Json::Value const& jvParams) { - std::string strIdent = jvParams[0u].asString (); - unsigned int iCursor = jvParams.size (); - bool bStrict = false; + std::string strIdent = jvParams[0u].asString(); + unsigned int iCursor = jvParams.size(); + bool bStrict = false; if (iCursor >= 2 && jvParams[iCursor - 1] == jss::strict) { @@ -871,27 +911,27 @@ private: --iCursor; } - if (! parseBase58(TokenType::AccountPublic, strIdent) && - ! parseBase58(strIdent) && - ! parseGenericSeed(strIdent)) - return rpcError (rpcACT_MALFORMED); + if (!parseBase58(TokenType::AccountPublic, strIdent) && + !parseBase58(strIdent) && !parseGenericSeed(strIdent)) + return rpcError(rpcACT_MALFORMED); // Get info on account. - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - jvRequest[jss::account] = strIdent; + jvRequest[jss::account] = strIdent; if (bStrict) - jvRequest[jss::strict] = 1; + jvRequest[jss::strict] = 1; - if (iCursor == 2 && !jvParseLedger (jvRequest, jvParams[1u].asString ())) - return rpcError (rpcLGR_IDX_MALFORMED); + if (iCursor == 2 && !jvParseLedger(jvRequest, jvParams[1u].asString())) + return rpcError(rpcLGR_IDX_MALFORMED); return jvRequest; } // peer_reservations_add [] - Json::Value parsePeerReservationsAdd (Json::Value const& jvParams) + Json::Value + parsePeerReservationsAdd(Json::Value const& jvParams) { Json::Value jvRequest; jvRequest[jss::public_key] = jvParams[0u].asString(); @@ -903,7 +943,8 @@ private: } // peer_reservations_del - Json::Value parsePeerReservationsDel (Json::Value const& jvParams) + Json::Value + parsePeerReservationsDel(Json::Value const& jvParams) { Json::Value jvRequest; jvRequest[jss::public_key] = jvParams[0u].asString(); @@ -911,25 +952,26 @@ private: } // ripple_path_find [] - Json::Value parseRipplePathFind (Json::Value const& jvParams) + Json::Value + parseRipplePathFind(Json::Value const& jvParams) { - Json::Reader reader; - Json::Value jvRequest{Json::objectValue}; - bool bLedger = 2 == jvParams.size (); + Json::Reader reader; + Json::Value jvRequest{Json::objectValue}; + bool bLedger = 2 == jvParams.size(); - JLOG (j_.trace()) << "RPC json: " << jvParams[0u]; + JLOG(j_.trace()) << "RPC json: " << jvParams[0u]; - if (reader.parse (jvParams[0u].asString (), jvRequest)) + if (reader.parse(jvParams[0u].asString(), jvRequest)) { if (bLedger) { - jvParseLedger (jvRequest, jvParams[1u].asString ()); + jvParseLedger(jvRequest, jvParams[1u].asString()); } return jvRequest; } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } // sign/submit any transaction to the network @@ -937,50 +979,54 @@ private: // sign offline // submit // submit - Json::Value parseSignSubmit (Json::Value const& jvParams) + Json::Value + parseSignSubmit(Json::Value const& jvParams) { - Json::Value txJSON; - Json::Reader reader; - bool const bOffline = 3 == jvParams.size () && jvParams[2u].asString () == "offline"; + Json::Value txJSON; + Json::Reader reader; + bool const bOffline = + 3 == jvParams.size() && jvParams[2u].asString() == "offline"; - if (1 == jvParams.size ()) + if (1 == jvParams.size()) { // Submitting tx_blob Json::Value jvRequest{Json::objectValue}; - jvRequest[jss::tx_blob] = jvParams[0u].asString (); + jvRequest[jss::tx_blob] = jvParams[0u].asString(); return jvRequest; } - else if ((2 == jvParams.size () || bOffline) - && reader.parse (jvParams[1u].asString (), txJSON)) + else if ( + (2 == jvParams.size() || bOffline) && + reader.parse(jvParams[1u].asString(), txJSON)) { // Signing or submitting tx_json. Json::Value jvRequest{Json::objectValue}; - jvRequest[jss::secret] = jvParams[0u].asString (); - jvRequest[jss::tx_json] = txJSON; + jvRequest[jss::secret] = jvParams[0u].asString(); + jvRequest[jss::tx_json] = txJSON; if (bOffline) - jvRequest[jss::offline] = true; + jvRequest[jss::offline] = true; return jvRequest; } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } // submit any multisigned transaction to the network // // submit_multisigned - Json::Value parseSubmitMultiSigned (Json::Value const& jvParams) + Json::Value + parseSubmitMultiSigned(Json::Value const& jvParams) { - if (1 == jvParams.size ()) + if (1 == jvParams.size()) { - Json::Value txJSON; - Json::Reader reader; - if (reader.parse (jvParams[0u].asString (), txJSON)) + Json::Value txJSON; + Json::Reader reader; + if (reader.parse(jvParams[0u].asString(), txJSON)) { Json::Value jvRequest{Json::objectValue}; jvRequest[jss::tx_json] = txJSON; @@ -988,105 +1034,115 @@ private: } } - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); } // transaction_entry - Json::Value parseTransactionEntry (Json::Value const& jvParams) + Json::Value + parseTransactionEntry(Json::Value const& jvParams) { // Parameter count should have already been verified. - assert (jvParams.size() == 2); + assert(jvParams.size() == 2); std::string const txHash = jvParams[0u].asString(); if (txHash.length() != 64) - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); Json::Value jvRequest{Json::objectValue}; jvRequest[jss::tx_hash] = txHash; - jvParseLedger (jvRequest, jvParams[1u].asString()); + jvParseLedger(jvRequest, jvParams[1u].asString()); // jvParseLedger inserts a "ledger_index" of 0 if it doesn't // find a match. if (jvRequest.isMember(jss::ledger_index) && jvRequest[jss::ledger_index] == 0) - return rpcError (rpcINVALID_PARAMS); + return rpcError(rpcINVALID_PARAMS); return jvRequest; } // tx - Json::Value parseTx (Json::Value const& jvParams) + Json::Value + parseTx(Json::Value const& jvParams) { Json::Value jvRequest{Json::objectValue}; - if (jvParams.size () == 2 || jvParams.size () == 4) + if (jvParams.size() == 2 || jvParams.size() == 4) { - if (jvParams[1u].asString () == jss::binary) + if (jvParams[1u].asString() == jss::binary) jvRequest[jss::binary] = true; } - if (jvParams.size () >= 3) + if (jvParams.size() >= 3) { - const auto offset = jvParams.size () == 3 ? 0 : 1; + const auto offset = jvParams.size() == 3 ? 0 : 1; - jvRequest[jss::min_ledger] = jvParams[1u + offset].asString (); - jvRequest[jss::max_ledger] = jvParams[2u + offset].asString (); + jvRequest[jss::min_ledger] = jvParams[1u + offset].asString(); + jvRequest[jss::max_ledger] = jvParams[2u + offset].asString(); } - jvRequest[jss::transaction] = jvParams[0u].asString (); + jvRequest[jss::transaction] = jvParams[0u].asString(); return jvRequest; } // tx_history - Json::Value parseTxHistory (Json::Value const& jvParams) + Json::Value + parseTxHistory(Json::Value const& jvParams) { Json::Value jvRequest{Json::objectValue}; - jvRequest[jss::start] = jvParams[0u].asUInt (); + jvRequest[jss::start] = jvParams[0u].asUInt(); return jvRequest; } // validation_create [||] // - // NOTE: It is poor security to specify secret information on the command line. This information might be saved in the command - // shell history file (e.g. .bash_history) and it may be leaked via the process status command (i.e. ps). - Json::Value parseValidationCreate (Json::Value const& jvParams) + // NOTE: It is poor security to specify secret information on the command + // line. This information might be saved in the command shell history file + // (e.g. .bash_history) and it may be leaked via the process status command + // (i.e. ps). + Json::Value + parseValidationCreate(Json::Value const& jvParams) { Json::Value jvRequest{Json::objectValue}; - if (jvParams.size ()) - jvRequest[jss::secret] = jvParams[0u].asString (); + if (jvParams.size()) + jvRequest[jss::secret] = jvParams[0u].asString(); return jvRequest; } // wallet_propose [] - // is only for testing. Master seeds should only be generated randomly. - Json::Value parseWalletPropose (Json::Value const& jvParams) + // is only for testing. Master seeds should only be generated + // randomly. + Json::Value + parseWalletPropose(Json::Value const& jvParams) { Json::Value jvRequest{Json::objectValue}; - if (jvParams.size ()) - jvRequest[jss::passphrase] = jvParams[0u].asString (); + if (jvParams.size()) + jvRequest[jss::passphrase] = jvParams[0u].asString(); return jvRequest; } // parse gateway balances - // gateway_balances [] [ [ ]] + // gateway_balances [] [ [ + // ]] - Json::Value parseGatewayBalances (Json::Value const& jvParams) + Json::Value + parseGatewayBalances(Json::Value const& jvParams) { unsigned int index = 0; - const unsigned int size = jvParams.size (); + const unsigned int size = jvParams.size(); Json::Value jvRequest{Json::objectValue}; - std::string param = jvParams[index++].asString (); - if (param.empty ()) - return RPC::make_param_error ("Invalid first parameter"); + std::string param = jvParams[index++].asString(); + if (param.empty()) + return RPC::make_param_error("Invalid first parameter"); if (param[0] != 'r') { @@ -1096,9 +1152,9 @@ private: jvRequest[jss::ledger_index] = param; if (size <= index) - return RPC::make_param_error ("Invalid hotwallet"); + return RPC::make_param_error("Invalid hotwallet"); - param = jvParams[index++].asString (); + param = jvParams[index++].asString(); } jvRequest[jss::account] = param; @@ -1108,16 +1164,17 @@ private: Json::Value& hotWallets = (jvRequest["hotwallet"] = Json::arrayValue); while (index < size) - hotWallets.append (jvParams[index++].asString ()); + hotWallets.append(jvParams[index++].asString()); } return jvRequest; } // server_info [counters] - Json::Value parseServerInfo (Json::Value const& jvParams) + Json::Value + parseServerInfo(Json::Value const& jvParams) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); if (jvParams.size() == 1 && jvParams[0u].asString() == "counters") jvRequest[jss::counters] = true; return jvRequest; @@ -1126,15 +1183,19 @@ private: public: //-------------------------------------------------------------------------- - explicit - RPCParser (beast::Journal j) - :j_ (j){} + explicit RPCParser(beast::Journal j) : j_(j) + { + } //-------------------------------------------------------------------------- // Convert a rpc method and params to a request. // <-- { method: xyz, params: [... ] } or { error: ..., ... } - Json::Value parseCommand (std::string strMethod, Json::Value jvParams, bool allowAnyCommand) + Json::Value + parseCommand( + std::string strMethod, + Json::Value jvParams, + bool allowAnyCommand) { if (auto stream = j_.trace()) { @@ -1144,88 +1205,94 @@ public: struct Command { - const char* name; - parseFuncPtr parse; - int minParams; - int maxParams; + const char* name; + parseFuncPtr parse; + int minParams; + int maxParams; }; // FIXME: replace this with a function-static std::map and the lookup // code with std::map::find when the problem with magic statics on // Visual Studio is fixed. - static - Command const commands[] = - { + static Command const commands[] = { // Request-response methods // - Returns an error, or the request. // - To modify the method, provide a new method in the request. - { "account_currencies", &RPCParser::parseAccountCurrencies, 1, 3 }, - { "account_info", &RPCParser::parseAccountItems, 1, 3 }, - { "account_lines", &RPCParser::parseAccountLines, 1, 5 }, - { "account_channels", &RPCParser::parseAccountChannels, 1, 3 }, - { "account_objects", &RPCParser::parseAccountItems, 1, 5 }, - { "account_offers", &RPCParser::parseAccountItems, 1, 4 }, - { "account_tx", &RPCParser::parseAccountTransactions, 1, 8 }, - { "book_offers", &RPCParser::parseBookOffers, 2, 7 }, - { "can_delete", &RPCParser::parseCanDelete, 0, 1 }, - { "channel_authorize", &RPCParser::parseChannelAuthorize, 3, 4 }, - { "channel_verify", &RPCParser::parseChannelVerify, 4, 4 }, - { "connect", &RPCParser::parseConnect, 1, 2 }, - { "consensus_info", &RPCParser::parseAsIs, 0, 0 }, - { "deposit_authorized", &RPCParser::parseDepositAuthorized, 2, 3 }, - { "download_shard", &RPCParser::parseDownloadShard, 2, -1 }, - { "feature", &RPCParser::parseFeature, 0, 2 }, - { "fetch_info", &RPCParser::parseFetchInfo, 0, 1 }, - { "gateway_balances", &RPCParser::parseGatewayBalances, 1, -1 }, - { "get_counts", &RPCParser::parseGetCounts, 0, 1 }, - { "json", &RPCParser::parseJson, 2, 2 }, - { "json2", &RPCParser::parseJson2, 1, 1 }, - { "ledger", &RPCParser::parseLedger, 0, 2 }, - { "ledger_accept", &RPCParser::parseAsIs, 0, 0 }, - { "ledger_closed", &RPCParser::parseAsIs, 0, 0 }, - { "ledger_current", &RPCParser::parseAsIs, 0, 0 }, - // { "ledger_entry", &RPCParser::parseLedgerEntry, -1, -1 }, - { "ledger_header", &RPCParser::parseLedgerId, 1, 1 }, - { "ledger_request", &RPCParser::parseLedgerId, 1, 1 }, - { "log_level", &RPCParser::parseLogLevel, 0, 2 }, - { "logrotate", &RPCParser::parseAsIs, 0, 0 }, - { "manifest", &RPCParser::parseManifest, 1, 1 }, - { "owner_info", &RPCParser::parseAccountItems, 1, 3 }, - { "peers", &RPCParser::parseAsIs, 0, 0 }, - { "ping", &RPCParser::parseAsIs, 0, 0 }, - { "print", &RPCParser::parseAsIs, 0, 1 }, - // { "profile", &RPCParser::parseProfile, 1, 9 }, - { "random", &RPCParser::parseAsIs, 0, 0 }, - { "peer_reservations_add", &RPCParser::parsePeerReservationsAdd, 1, 2 }, - { "peer_reservations_del", &RPCParser::parsePeerReservationsDel, 1, 1 }, - { "peer_reservations_list", &RPCParser::parseAsIs, 0, 0 }, - { "ripple_path_find", &RPCParser::parseRipplePathFind, 1, 2 }, - { "sign", &RPCParser::parseSignSubmit, 2, 3 }, - { "sign_for", &RPCParser::parseSignFor, 3, 4 }, - { "submit", &RPCParser::parseSignSubmit, 1, 3 }, - { "submit_multisigned", &RPCParser::parseSubmitMultiSigned, 1, 1 }, - { "server_info", &RPCParser::parseServerInfo, 0, 1 }, - { "server_state", &RPCParser::parseServerInfo, 0, 1 }, - { "crawl_shards", &RPCParser::parseAsIs, 0, 2 }, - { "stop", &RPCParser::parseAsIs, 0, 0 }, - { "transaction_entry", &RPCParser::parseTransactionEntry, 2, 2 }, - { "tx", &RPCParser::parseTx, 1, 4 }, - { "tx_account", &RPCParser::parseTxAccount, 1, 7 }, - { "tx_history", &RPCParser::parseTxHistory, 1, 1 }, - { "unl_list", &RPCParser::parseAsIs, 0, 0 }, - { "validation_create", &RPCParser::parseValidationCreate, 0, 1 }, - { "validator_info", &RPCParser::parseAsIs, 0, 0 }, - { "version", &RPCParser::parseAsIs, 0, 0 }, - { "wallet_propose", &RPCParser::parseWalletPropose, 0, 1 }, - { "internal", &RPCParser::parseInternal, 1, -1 }, + {"account_currencies", &RPCParser::parseAccountCurrencies, 1, 3}, + {"account_info", &RPCParser::parseAccountItems, 1, 3}, + {"account_lines", &RPCParser::parseAccountLines, 1, 5}, + {"account_channels", &RPCParser::parseAccountChannels, 1, 3}, + {"account_objects", &RPCParser::parseAccountItems, 1, 5}, + {"account_offers", &RPCParser::parseAccountItems, 1, 4}, + {"account_tx", &RPCParser::parseAccountTransactions, 1, 8}, + {"book_offers", &RPCParser::parseBookOffers, 2, 7}, + {"can_delete", &RPCParser::parseCanDelete, 0, 1}, + {"channel_authorize", &RPCParser::parseChannelAuthorize, 3, 4}, + {"channel_verify", &RPCParser::parseChannelVerify, 4, 4}, + {"connect", &RPCParser::parseConnect, 1, 2}, + {"consensus_info", &RPCParser::parseAsIs, 0, 0}, + {"deposit_authorized", &RPCParser::parseDepositAuthorized, 2, 3}, + {"download_shard", &RPCParser::parseDownloadShard, 2, -1}, + {"feature", &RPCParser::parseFeature, 0, 2}, + {"fetch_info", &RPCParser::parseFetchInfo, 0, 1}, + {"gateway_balances", &RPCParser::parseGatewayBalances, 1, -1}, + {"get_counts", &RPCParser::parseGetCounts, 0, 1}, + {"json", &RPCParser::parseJson, 2, 2}, + {"json2", &RPCParser::parseJson2, 1, 1}, + {"ledger", &RPCParser::parseLedger, 0, 2}, + {"ledger_accept", &RPCParser::parseAsIs, 0, 0}, + {"ledger_closed", &RPCParser::parseAsIs, 0, 0}, + {"ledger_current", &RPCParser::parseAsIs, 0, 0}, + // { "ledger_entry", &RPCParser::parseLedgerEntry, + // -1, -1 }, + {"ledger_header", &RPCParser::parseLedgerId, 1, 1}, + {"ledger_request", &RPCParser::parseLedgerId, 1, 1}, + {"log_level", &RPCParser::parseLogLevel, 0, 2}, + {"logrotate", &RPCParser::parseAsIs, 0, 0}, + {"manifest", &RPCParser::parseManifest, 1, 1}, + {"owner_info", &RPCParser::parseAccountItems, 1, 3}, + {"peers", &RPCParser::parseAsIs, 0, 0}, + {"ping", &RPCParser::parseAsIs, 0, 0}, + {"print", &RPCParser::parseAsIs, 0, 1}, + // { "profile", &RPCParser::parseProfile, 1, 9 + // }, + {"random", &RPCParser::parseAsIs, 0, 0}, + {"peer_reservations_add", + &RPCParser::parsePeerReservationsAdd, + 1, + 2}, + {"peer_reservations_del", + &RPCParser::parsePeerReservationsDel, + 1, + 1}, + {"peer_reservations_list", &RPCParser::parseAsIs, 0, 0}, + {"ripple_path_find", &RPCParser::parseRipplePathFind, 1, 2}, + {"sign", &RPCParser::parseSignSubmit, 2, 3}, + {"sign_for", &RPCParser::parseSignFor, 3, 4}, + {"submit", &RPCParser::parseSignSubmit, 1, 3}, + {"submit_multisigned", &RPCParser::parseSubmitMultiSigned, 1, 1}, + {"server_info", &RPCParser::parseServerInfo, 0, 1}, + {"server_state", &RPCParser::parseServerInfo, 0, 1}, + {"crawl_shards", &RPCParser::parseAsIs, 0, 2}, + {"stop", &RPCParser::parseAsIs, 0, 0}, + {"transaction_entry", &RPCParser::parseTransactionEntry, 2, 2}, + {"tx", &RPCParser::parseTx, 1, 4}, + {"tx_account", &RPCParser::parseTxAccount, 1, 7}, + {"tx_history", &RPCParser::parseTxHistory, 1, 1}, + {"unl_list", &RPCParser::parseAsIs, 0, 0}, + {"validation_create", &RPCParser::parseValidationCreate, 0, 1}, + {"validator_info", &RPCParser::parseAsIs, 0, 0}, + {"version", &RPCParser::parseAsIs, 0, 0}, + {"wallet_propose", &RPCParser::parseWalletPropose, 0, 1}, + {"internal", &RPCParser::parseInternal, 1, -1}, // Evented methods - { "path_find", &RPCParser::parseEvented, -1, -1 }, - { "subscribe", &RPCParser::parseEvented, -1, -1 }, - { "unsubscribe", &RPCParser::parseEvented, -1, -1 }, + {"path_find", &RPCParser::parseEvented, -1, -1}, + {"subscribe", &RPCParser::parseEvented, -1, -1}, + {"unsubscribe", &RPCParser::parseEvented, -1, -1}, }; - auto const count = jvParams.size (); + auto const count = jvParams.size(); for (auto const& command : commands) { @@ -1234,24 +1301,24 @@ public: if ((command.minParams >= 0 && count < command.minParams) || (command.maxParams >= 0 && count > command.maxParams)) { - JLOG (j_.debug()) << - "Wrong number of parameters for " << command.name << - " minimum=" << command.minParams << - " maximum=" << command.maxParams << - " actual=" << count; + JLOG(j_.debug()) + << "Wrong number of parameters for " << command.name + << " minimum=" << command.minParams + << " maximum=" << command.maxParams + << " actual=" << count; - return rpcError (rpcBAD_SYNTAX); + return rpcError(rpcBAD_SYNTAX); } - return (this->* (command.parse)) (jvParams); + return (this->*(command.parse))(jvParams); } } // The command could not be found if (!allowAnyCommand) - return rpcError (rpcUNKNOWN_COMMAND); + return rpcError(rpcUNKNOWN_COMMAND); - return parseAsIs (jvParams); + return parseAsIs(jvParams); } }; @@ -1266,23 +1333,26 @@ public: // 1.2 spec: http://groups.google.com/group/json-rpc/web/json-rpc-over-http // -std::string JSONRPCRequest (std::string const& strMethod, Json::Value const& params, Json::Value const& id) +std::string +JSONRPCRequest( + std::string const& strMethod, + Json::Value const& params, + Json::Value const& id) { Json::Value request; request[jss::method] = strMethod; request[jss::params] = params; request[jss::id] = id; - return to_string (request) + "\n"; + return to_string(request) + "\n"; } -namespace +namespace { +// Special local exception type thrown when request can't be parsed. +class RequestNotParseable : public std::runtime_error { - // Special local exception type thrown when request can't be parsed. - class RequestNotParseable : public std::runtime_error - { - using std::runtime_error::runtime_error; // Inherit constructors - }; + using std::runtime_error::runtime_error; // Inherit constructors }; +}; // namespace struct RPCCallImp { @@ -1290,55 +1360,64 @@ struct RPCCallImp // VFALCO NOTE Is this a to-do comment or a doc comment? // Place the async result somewhere useful. - static void callRPCHandler (Json::Value* jvOutput, Json::Value const& jvInput) + static void + callRPCHandler(Json::Value* jvOutput, Json::Value const& jvInput) { (*jvOutput) = jvInput; } - static bool onResponse ( - std::function callbackFuncP, - const boost::system::error_code& ecResult, int iStatus, - std::string const& strData, beast::Journal j) + static bool + onResponse( + std::function callbackFuncP, + const boost::system::error_code& ecResult, + int iStatus, + std::string const& strData, + beast::Journal j) { if (callbackFuncP) { - // Only care about the result, if we care to deliver it callbackFuncP. + // Only care about the result, if we care to deliver it + // callbackFuncP. // Receive reply if (iStatus == 401) - Throw ( + Throw( "incorrect rpcuser or rpcpassword (authorization failed)"); - else if ((iStatus >= 400) && (iStatus != 400) && (iStatus != 404) && (iStatus != 500)) // ? - Throw ( - std::string ("server returned HTTP error ") + - std::to_string (iStatus)); - else if (strData.empty ()) - Throw ("no response from server"); + else if ( + (iStatus >= 400) && (iStatus != 400) && (iStatus != 404) && + (iStatus != 500)) // ? + Throw( + std::string("server returned HTTP error ") + + std::to_string(iStatus)); + else if (strData.empty()) + Throw("no response from server"); // Parse reply - JLOG (j.debug()) << "RPC reply: " << strData << std::endl; + JLOG(j.debug()) << "RPC reply: " << strData << std::endl; if (strData.find("Unable to parse request") == 0) - Throw (strData); - Json::Reader reader; - Json::Value jvReply; - if (!reader.parse (strData, jvReply)) - Throw ("couldn't parse reply from server"); + Throw(strData); + Json::Reader reader; + Json::Value jvReply; + if (!reader.parse(strData, jvReply)) + Throw("couldn't parse reply from server"); if (!jvReply) - Throw ("expected reply to have result, error and id properties"); + Throw( + "expected reply to have result, error and id properties"); - Json::Value jvResult (Json::objectValue); + Json::Value jvResult(Json::objectValue); jvResult["result"] = jvReply; - (callbackFuncP) (jvResult); + (callbackFuncP)(jvResult); } return false; } // Build the request. - static void onRequest ( + static void + onRequest( std::string const& strMethod, Json::Value const& jvParams, std::unordered_map const& headers, @@ -1347,15 +1426,14 @@ struct RPCCallImp std::string const& strHost, beast::Journal j) { - JLOG (j.debug()) << "requestRPC: strPath='" << strPath << "'"; + JLOG(j.debug()) << "requestRPC: strPath='" << strPath << "'"; - std::ostream osRequest (&sb); - osRequest << - createHTTPPost ( - strHost, - strPath, - JSONRPCRequest (strMethod, jvParams, Json::Value (1)), - headers); + std::ostream osRequest(&sb); + osRequest << createHTTPPost( + strHost, + strPath, + JSONRPCRequest(strMethod, jvParams, Json::Value(1)), + headers); } }; @@ -1363,60 +1441,62 @@ struct RPCCallImp // Used internally by rpcClient. static Json::Value -rpcCmdLineToJson (std::vector const& args, - Json::Value& retParams, beast::Journal j) +rpcCmdLineToJson( + std::vector const& args, + Json::Value& retParams, + beast::Journal j) { - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); - RPCParser rpParser (j); - Json::Value jvRpcParams (Json::arrayValue); + RPCParser rpParser(j); + Json::Value jvRpcParams(Json::arrayValue); - for (int i = 1; i != args.size (); i++) - jvRpcParams.append (args[i]); + for (int i = 1; i != args.size(); i++) + jvRpcParams.append(args[i]); - retParams = Json::Value (Json::objectValue); + retParams = Json::Value(Json::objectValue); retParams[jss::method] = args[0]; retParams[jss::params] = jvRpcParams; - jvRequest = rpParser.parseCommand (args[0], jvRpcParams, true); + jvRequest = rpParser.parseCommand(args[0], jvRpcParams, true); - auto insert_api_version = [](Json::Value & jr){ - if( jr.isObject() && - !jr.isMember(jss::error) && + auto insert_api_version = [](Json::Value& jr) { + if (jr.isObject() && !jr.isMember(jss::error) && !jr.isMember(jss::api_version)) { jr[jss::api_version] = RPC::ApiMaximumSupportedVersion; } }; - if(jvRequest.isObject()) + if (jvRequest.isObject()) insert_api_version(jvRequest); - else if(jvRequest.isArray()) + else if (jvRequest.isArray()) std::for_each(jvRequest.begin(), jvRequest.end(), insert_api_version); - JLOG (j.trace()) << "RPC Request: " << jvRequest << std::endl; + JLOG(j.trace()) << "RPC Request: " << jvRequest << std::endl; return jvRequest; } Json::Value -cmdLineToJSONRPC (std::vector const& args, beast::Journal j) +cmdLineToJSONRPC(std::vector const& args, beast::Journal j) { - Json::Value jv = Json::Value (Json::objectValue); - auto const paramsObj = rpcCmdLineToJson (args, jv, j); + Json::Value jv = Json::Value(Json::objectValue); + auto const paramsObj = rpcCmdLineToJson(args, jv, j); // Re-use jv to return our formatted result. jv.clear(); // Allow parser to rewrite method. - jv[jss::method] = paramsObj.isMember (jss::method) ? - paramsObj[jss::method].asString() : args[0]; + jv[jss::method] = paramsObj.isMember(jss::method) + ? paramsObj[jss::method].asString() + : args[0]; // If paramsObj is not empty, put it in a [params] array. if (paramsObj.begin() != paramsObj.end()) { - auto& paramsArray = Json::setArray (jv, jss::params); - paramsArray.append (paramsObj); + auto& paramsArray = Json::setArray(jv, jss::params); + paramsArray.append(paramsObj); } if (paramsObj.isMember(jss::jsonrpc)) jv[jss::jsonrpc] = paramsObj[jss::jsonrpc]; @@ -1430,28 +1510,31 @@ cmdLineToJSONRPC (std::vector const& args, beast::Journal j) //------------------------------------------------------------------------------ std::pair -rpcClient(std::vector const& args, - Config const& config, Logs& logs, +rpcClient( + std::vector const& args, + Config const& config, + Logs& logs, std::unordered_map const& headers) { - static_assert(rpcBAD_SYNTAX == 1 && rpcSUCCESS == 0, + static_assert( + rpcBAD_SYNTAX == 1 && rpcSUCCESS == 0, "Expect specific rpc enum values."); - if (args.empty ()) - return { rpcBAD_SYNTAX, {} }; // rpcBAD_SYNTAX = print usage + if (args.empty()) + return {rpcBAD_SYNTAX, {}}; // rpcBAD_SYNTAX = print usage - int nRet = rpcSUCCESS; + int nRet = rpcSUCCESS; Json::Value jvOutput; - Json::Value jvRequest (Json::objectValue); + Json::Value jvRequest(Json::objectValue); try { - Json::Value jvRpc = Json::Value (Json::objectValue); - jvRequest = rpcCmdLineToJson (args, jvRpc, logs.journal ("RPCParser")); + Json::Value jvRpc = Json::Value(Json::objectValue); + jvRequest = rpcCmdLineToJson(args, jvRpc, logs.journal("RPCParser")); - if (jvRequest.isMember (jss::error)) + if (jvRequest.isMember(jss::error)) { - jvOutput = jvRequest; - jvOutput["rpc"] = jvRpc; + jvOutput = jvRequest; + jvOutput["rpc"] = jvRpc; } else { @@ -1460,7 +1543,7 @@ rpcClient(std::vector const& args, { setup = setup_ServerHandler( config, - beast::logstream { logs.journal ("HTTPClient").warn() }); + beast::logstream{logs.journal("HTTPClient").warn()}); } catch (std::exception const&) { @@ -1474,16 +1557,16 @@ rpcClient(std::vector const& args, setup.client.port = config.rpc_ip->port(); } - Json::Value jvParams (Json::arrayValue); + Json::Value jvParams(Json::arrayValue); - if (!setup.client.admin_user.empty ()) + if (!setup.client.admin_user.empty()) jvRequest["admin_user"] = setup.client.admin_user; - if (!setup.client.admin_password.empty ()) + if (!setup.client.admin_password.empty()) jvRequest["admin_password"] = setup.client.admin_password; if (jvRequest.isObject()) - jvParams.append (jvRequest); + jvParams.append(jvRequest); else if (jvRequest.isArray()) { for (Json::UInt i = 0; i < jvRequest.size(); ++i) @@ -1492,31 +1575,33 @@ rpcClient(std::vector const& args, { boost::asio::io_service isService; - RPCCall::fromNetwork ( + RPCCall::fromNetwork( isService, setup.client.ip, setup.client.port, setup.client.user, setup.client.password, "", - jvRequest.isMember (jss::method) // Allow parser to rewrite method. - ? jvRequest[jss::method].asString () - : jvRequest.isArray() - ? "batch" - : args[0], - jvParams, // Parsed, execute. - setup.client.secure != 0, // Use SSL + jvRequest.isMember( + jss::method) // Allow parser to rewrite method. + ? jvRequest[jss::method].asString() + : jvRequest.isArray() ? "batch" : args[0], + jvParams, // Parsed, execute. + setup.client.secure != 0, // Use SSL config.quiet(), logs, - std::bind (RPCCallImp::callRPCHandler, &jvOutput, - std::placeholders::_1), + std::bind( + RPCCallImp::callRPCHandler, + &jvOutput, + std::placeholders::_1), headers); - isService.run(); // This blocks until there is no more outstanding async calls. + isService.run(); // This blocks until there is no more + // outstanding async calls. } - if (jvOutput.isMember ("result")) + if (jvOutput.isMember("result")) { // Had a successful JSON-RPC 2.0 call. - jvOutput = jvOutput["result"]; + jvOutput = jvOutput["result"]; // jvOutput may report a server side error. // It should report "status". @@ -1524,90 +1609,100 @@ rpcClient(std::vector const& args, else { // Transport error. - Json::Value jvRpcError = jvOutput; + Json::Value jvRpcError = jvOutput; - jvOutput = rpcError (rpcJSON_RPC); - jvOutput["result"] = jvRpcError; + jvOutput = rpcError(rpcJSON_RPC); + jvOutput["result"] = jvRpcError; } // If had an error, supply invocation in result. - if (jvOutput.isMember (jss::error)) + if (jvOutput.isMember(jss::error)) { - jvOutput["rpc"] = jvRpc; // How the command was seen as method + params. - jvOutput["request_sent"] = jvRequest; // How the command was translated. + jvOutput["rpc"] = + jvRpc; // How the command was seen as method + params. + jvOutput["request_sent"] = + jvRequest; // How the command was translated. } } - if (jvOutput.isMember (jss::error)) + if (jvOutput.isMember(jss::error)) { - jvOutput[jss::status] = "error"; + jvOutput[jss::status] = "error"; if (jvOutput.isMember(jss::error_code)) nRet = std::stoi(jvOutput[jss::error_code].asString()); else if (jvOutput[jss::error].isMember(jss::error_code)) - nRet = std::stoi(jvOutput[jss::error][jss::error_code].asString()); + nRet = + std::stoi(jvOutput[jss::error][jss::error_code].asString()); else nRet = rpcBAD_SYNTAX; } - // YYY We could have a command line flag for single line output for scripts. - // YYY We would intercept output here and simplify it. + // YYY We could have a command line flag for single line output for + // scripts. YYY We would intercept output here and simplify it. } catch (RequestNotParseable& e) { - jvOutput = rpcError(rpcINVALID_PARAMS); - jvOutput["error_what"] = e.what(); - nRet = rpcINVALID_PARAMS; + jvOutput = rpcError(rpcINVALID_PARAMS); + jvOutput["error_what"] = e.what(); + nRet = rpcINVALID_PARAMS; } catch (std::exception& e) { - jvOutput = rpcError (rpcINTERNAL); - jvOutput["error_what"] = e.what (); - nRet = rpcINTERNAL; + jvOutput = rpcError(rpcINTERNAL); + jvOutput["error_what"] = e.what(); + nRet = rpcINTERNAL; } - return { nRet, std::move(jvOutput) }; + return {nRet, std::move(jvOutput)}; } //------------------------------------------------------------------------------ namespace RPCCall { -int fromCommandLine ( +int +fromCommandLine( Config const& config, const std::vector& vCmd, Logs& logs) { auto const result = rpcClient(vCmd, config, logs); - std::cout << result.second.toStyledString (); + std::cout << result.second.toStyledString(); return result.first; } //------------------------------------------------------------------------------ -void fromNetwork ( +void +fromNetwork( boost::asio::io_service& io_service, - std::string const& strIp, const std::uint16_t iPort, - std::string const& strUsername, std::string const& strPassword, - std::string const& strPath, std::string const& strMethod, - Json::Value const& jvParams, const bool bSSL, const bool quiet, + std::string const& strIp, + const std::uint16_t iPort, + std::string const& strUsername, + std::string const& strPassword, + std::string const& strPath, + std::string const& strMethod, + Json::Value const& jvParams, + const bool bSSL, + const bool quiet, Logs& logs, - std::function callbackFuncP, + std::function callbackFuncP, std::unordered_map headers) { - auto j = logs.journal ("HTTPClient"); + auto j = logs.journal("HTTPClient"); // Connect to localhost if (!quiet) { - JLOG(j.info()) << (bSSL ? "Securely connecting to " : "Connecting to ") << - strIp << ":" << iPort << std::endl; + JLOG(j.info()) << (bSSL ? "Securely connecting to " : "Connecting to ") + << strIp << ":" << iPort << std::endl; } // HTTP basic authentication - headers["Authorization"] = std::string("Basic ") + base64_encode( - strUsername + ":" + strPassword); + headers["Authorization"] = + std::string("Basic ") + base64_encode(strUsername + ":" + strPassword); // Send request @@ -1618,25 +1713,32 @@ void fromNetwork ( using namespace std::chrono_literals; auto constexpr RPC_NOTIFY = 10min; - HTTPClient::request ( + HTTPClient::request( bSSL, io_service, strIp, iPort, - std::bind ( + std::bind( &RPCCallImp::onRequest, strMethod, jvParams, headers, - strPath, std::placeholders::_1, std::placeholders::_2, j), + strPath, + std::placeholders::_1, + std::placeholders::_2, + j), RPC_REPLY_MAX_BYTES, RPC_NOTIFY, - std::bind (&RPCCallImp::onResponse, callbackFuncP, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, j), + std::bind( + &RPCCallImp::onResponse, + callbackFuncP, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3, + j), j); } -} // RPCCall +} // namespace RPCCall -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/RPCErr.cpp b/src/ripple/net/impl/RPCErr.cpp index 77dd8782cb..1c28eda000 100644 --- a/src/ripple/net/impl/RPCErr.cpp +++ b/src/ripple/net/impl/RPCErr.cpp @@ -25,16 +25,18 @@ namespace ripple { struct RPCErr; // VFALCO NOTE Deprecated function -Json::Value rpcError (int iError, Json::Value jvResult) +Json::Value +rpcError(int iError, Json::Value jvResult) { - RPC::inject_error (iError, jvResult); + RPC::inject_error(iError, jvResult); return jvResult; } // VFALCO NOTE Deprecated function -bool isRpcError (Json::Value jvResult) +bool +isRpcError(Json::Value jvResult) { - return jvResult.isObject() && jvResult.isMember (jss::error); + return jvResult.isObject() && jvResult.isMember(jss::error); } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/RPCSub.cpp b/src/ripple/net/impl/RPCSub.cpp index ef509f2dcd..f9b08e5c07 100644 --- a/src/ripple/net/impl/RPCSub.cpp +++ b/src/ripple/net/impl/RPCSub.cpp @@ -17,105 +17,110 @@ */ //============================================================================== -#include -#include #include #include +#include #include #include +#include #include namespace ripple { // Subscription object for JSON-RPC -class RPCSubImp - : public RPCSub +class RPCSubImp : public RPCSub { public: - RPCSubImp (InfoSub::Source& source, boost::asio::io_service& io_service, - JobQueue& jobQueue, std::string const& strUrl, std::string const& strUsername, - std::string const& strPassword, Logs& logs) - : RPCSub (source) - , m_io_service (io_service) - , m_jobQueue (jobQueue) - , mUrl (strUrl) - , mSSL (false) - , mUsername (strUsername) - , mPassword (strPassword) - , mSending (false) - , j_ (logs.journal ("RPCSub")) - , logs_ (logs) + RPCSubImp( + InfoSub::Source& source, + boost::asio::io_service& io_service, + JobQueue& jobQueue, + std::string const& strUrl, + std::string const& strUsername, + std::string const& strPassword, + Logs& logs) + : RPCSub(source) + , m_io_service(io_service) + , m_jobQueue(jobQueue) + , mUrl(strUrl) + , mSSL(false) + , mUsername(strUsername) + , mPassword(strPassword) + , mSending(false) + , j_(logs.journal("RPCSub")) + , logs_(logs) { parsedURL pUrl; - if (!parseUrl (pUrl, strUrl)) - Throw ("Failed to parse url."); + if (!parseUrl(pUrl, strUrl)) + Throw("Failed to parse url."); else if (pUrl.scheme == "https") mSSL = true; else if (pUrl.scheme != "http") - Throw ("Only http and https is supported."); + Throw("Only http and https is supported."); mSeq = 1; mIp = pUrl.domain; - mPort = (! pUrl.port) ? (mSSL ? 443 : 80) : *pUrl.port; + mPort = (!pUrl.port) ? (mSSL ? 443 : 80) : *pUrl.port; mPath = pUrl.path; - JLOG (j_.info()) << - "RPCCall::fromNetwork sub: ip=" << mIp << - " port=" << mPort << - " ssl= "<< (mSSL ? "yes" : "no") << - " path='" << mPath << "'"; + JLOG(j_.info()) << "RPCCall::fromNetwork sub: ip=" << mIp + << " port=" << mPort + << " ssl= " << (mSSL ? "yes" : "no") << " path='" + << mPath << "'"; } ~RPCSubImp() = default; - void send (Json::Value const& jvObj, bool broadcast) override + void + send(Json::Value const& jvObj, bool broadcast) override { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - if (mDeque.size () >= eventQueueMax) + if (mDeque.size() >= eventQueueMax) { // Drop the previous event. - JLOG (j_.warn()) << "RPCCall::fromNetwork drop"; - mDeque.pop_back (); + JLOG(j_.warn()) << "RPCCall::fromNetwork drop"; + mDeque.pop_back(); } auto jm = broadcast ? j_.debug() : j_.info(); - JLOG (jm) << - "RPCCall::fromNetwork push: " << jvObj; + JLOG(jm) << "RPCCall::fromNetwork push: " << jvObj; - mDeque.push_back (std::make_pair (mSeq++, jvObj)); + mDeque.push_back(std::make_pair(mSeq++, jvObj)); if (!mSending) { // Start a sending thread. - JLOG (j_.info()) << "RPCCall::fromNetwork start"; + JLOG(j_.info()) << "RPCCall::fromNetwork start"; - mSending = m_jobQueue.addJob ( - jtCLIENT, "RPCSub::sendThread", [this] (Job&) { - sendThread(); - }); + mSending = m_jobQueue.addJob( + jtCLIENT, "RPCSub::sendThread", [this](Job&) { sendThread(); }); } } - void setUsername (std::string const& strUsername) override + void + setUsername(std::string const& strUsername) override { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); mUsername = strUsername; } - void setPassword (std::string const& strPassword) override + void + setPassword(std::string const& strPassword) override { - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); mPassword = strPassword; } private: - // XXX Could probably create a bunch of send jobs in a single get of the lock. - void sendThread () + // XXX Could probably create a bunch of send jobs in a single get of the + // lock. + void + sendThread() { Json::Value jvEvent; bool bSend; @@ -124,23 +129,23 @@ private: { { // Obtain the lock to manipulate the queue and change sending. - std::lock_guard sl (mLock); + std::lock_guard sl(mLock); - if (mDeque.empty ()) + if (mDeque.empty()) { - mSending = false; - bSend = false; + mSending = false; + bSend = false; } else { - auto const [seq, env] = mDeque.front (); + auto const [seq, env] = mDeque.front(); - mDeque.pop_front (); + mDeque.pop_front(); - jvEvent = env; - jvEvent["seq"] = seq; + jvEvent = env; + jvEvent["seq"] = seq; - bSend = true; + bSend = true; } } @@ -150,13 +155,16 @@ private: // XXX Might not need this in a try. try { - JLOG (j_.info()) << "RPCCall::fromNetwork: " << mIp; + JLOG(j_.info()) << "RPCCall::fromNetwork: " << mIp; - RPCCall::fromNetwork ( + RPCCall::fromNetwork( m_io_service, - mIp, mPort, - mUsername, mPassword, - mPath, "event", + mIp, + mPort, + mUsername, + mPassword, + mPath, + "event", jvEvent, mSSL, true, @@ -164,35 +172,32 @@ private: } catch (const std::exception& e) { - JLOG (j_.info()) << "RPCCall::fromNetwork exception: " << e.what (); + JLOG(j_.info()) + << "RPCCall::fromNetwork exception: " << e.what(); } } - } - while (bSend); + } while (bSend); } private: - enum - { - eventQueueMax = 32 - }; + enum { eventQueueMax = 32 }; boost::asio::io_service& m_io_service; JobQueue& m_jobQueue; - std::string mUrl; - std::string mIp; - std::uint16_t mPort; - bool mSSL; - std::string mUsername; - std::string mPassword; - std::string mPath; + std::string mUrl; + std::string mIp; + std::uint16_t mPort; + bool mSSL; + std::string mUsername; + std::string mPassword; + std::string mPath; - int mSeq; // Next id to allocate. + int mSeq; // Next id to allocate. - bool mSending; // Sending threead is active. + bool mSending; // Sending threead is active. - std::deque > mDeque; + std::deque> mDeque; beast::Journal const j_; Logs& logs_; @@ -200,20 +205,28 @@ private: //------------------------------------------------------------------------------ -RPCSub::RPCSub (InfoSub::Source& source) - : InfoSub (source, Consumer()) +RPCSub::RPCSub(InfoSub::Source& source) : InfoSub(source, Consumer()) { } -std::shared_ptr make_RPCSub ( - InfoSub::Source& source, boost::asio::io_service& io_service, - JobQueue& jobQueue, std::string const& strUrl, - std::string const& strUsername, std::string const& strPassword, +std::shared_ptr +make_RPCSub( + InfoSub::Source& source, + boost::asio::io_service& io_service, + JobQueue& jobQueue, + std::string const& strUrl, + std::string const& strUsername, + std::string const& strPassword, Logs& logs) { - return std::make_shared (std::ref (source), - std::ref (io_service), std::ref (jobQueue), - strUrl, strUsername, strPassword, logs); + return std::make_shared( + std::ref(source), + std::ref(io_service), + std::ref(jobQueue), + strUrl, + strUsername, + strPassword, + logs); } -} // ripple +} // namespace ripple diff --git a/src/ripple/net/impl/RegisterSSLCerts.cpp b/src/ripple/net/impl/RegisterSSLCerts.cpp index 2ac9ff4a48..b4e3f51a93 100644 --- a/src/ripple/net/impl/RegisterSSLCerts.cpp +++ b/src/ripple/net/impl/RegisterSSLCerts.cpp @@ -112,10 +112,10 @@ registerSSLCerts( // on the latter), these undefs allow this TU to be safely used in // unity builds without messing up subsequent TUs. #if BOOST_OS_WINDOWS -# undef X509_NAME -# undef X509_EXTENSIONS -# undef X509_CERT_PAIR -# undef PKCS7_ISSUER_AND_SERIAL -# undef OCSP_REQUEST -# undef OCSP_RESPONSE +#undef X509_NAME +#undef X509_EXTENSIONS +#undef X509_CERT_PAIR +#undef PKCS7_ISSUER_AND_SERIAL +#undef OCSP_REQUEST +#undef OCSP_RESPONSE #endif diff --git a/src/ripple/net/impl/SSLHTTPDownloader.cpp b/src/ripple/net/impl/SSLHTTPDownloader.cpp index 3bf0622fab..6988a1e930 100644 --- a/src/ripple/net/impl/SSLHTTPDownloader.cpp +++ b/src/ripple/net/impl/SSLHTTPDownloader.cpp @@ -48,16 +48,15 @@ SSLHTTPDownloader::download( return false; if (!strand_.running_in_this_thread()) - strand_.post( - std::bind( - &SSLHTTPDownloader::download, - this->shared_from_this(), - host, - port, - target, - version, - dstPath, - complete)); + strand_.post(std::bind( + &SSLHTTPDownloader::download, + this->shared_from_this(), + host, + port, + target, + version, + dstPath, + complete)); else boost::asio::spawn( strand_, @@ -81,14 +80,10 @@ SSLHTTPDownloader::onStop() isStopped_ = true; - if(sessionActive_) + if (sessionActive_) { // Wait for the handler to exit. - c_.wait(lock, - [this]() - { - return !sessionActive_; - }); + c_.wait(lock, [this]() { return !sessionActive_; }); } } @@ -111,11 +106,10 @@ SSLHTTPDownloader::do_session( ////////////////////////////////////////////// // Define lambdas for encapsulating download // operations: - auto connect = [&](std::shared_ptr parser) - { + auto connect = [&](std::shared_ptr parser) { uint64_t const rangeStart = size(parser); - ip::tcp::resolver resolver {strand_.context()}; + ip::tcp::resolver resolver{strand_.context()}; auto const results = resolver.async_resolve(host, port, yield[ec]); if (ec) return fail(dstPath, complete, ec, "async_resolve", parser); @@ -126,8 +120,12 @@ SSLHTTPDownloader::do_session( } catch (std::exception const& e) { - return fail(dstPath, complete, ec, - std::string("exception: ") + e.what(), parser); + return fail( + dstPath, + complete, + ec, + std::string("exception: ") + e.what(), + parser); } ec = ssl_ctx_.preConnectVerify(*stream_, host); @@ -148,19 +146,20 @@ SSLHTTPDownloader::do_session( return fail(dstPath, complete, ec, "async_handshake", parser); // Set up an HTTP HEAD request message to find the file size - http::request req {http::verb::head, target, version}; + http::request req{http::verb::head, target, version}; req.set(http::field::host, host); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); // Requesting a portion of the file if (rangeStart) { - req.set(http::field::range, + req.set( + http::field::range, (boost::format("bytes=%llu-") % rangeStart).str()); } http::async_write(*stream_, req, yield[ec]); - if(ec) + if (ec) return fail(dstPath, complete, ec, "async_write", parser); { @@ -168,42 +167,51 @@ SSLHTTPDownloader::do_session( http::response_parser p; p.skip(true); http::async_read(*stream_, read_buf_, p, yield[ec]); - if(ec) + if (ec) return fail(dstPath, complete, ec, "async_read", parser); // Range request was rejected - if(p.get().result() == http::status::range_not_satisfiable) + if (p.get().result() == http::status::range_not_satisfiable) { req.erase(http::field::range); http::async_write(*stream_, req, yield[ec]); - if(ec) - return fail(dstPath, complete, ec, - "async_write_range_verify", parser); + if (ec) + return fail( + dstPath, + complete, + ec, + "async_write_range_verify", + parser); http::response_parser p; p.skip(true); http::async_read(*stream_, read_buf_, p, yield[ec]); - if(ec) - return fail(dstPath, complete, ec, - "async_read_range_verify", parser); + if (ec) + return fail( + dstPath, + complete, + ec, + "async_read_range_verify", + parser); // The entire file is downloaded already. - if(p.content_length() == rangeStart) + if (p.content_length() == rangeStart) skip = true; else - return fail(dstPath, complete, ec, - "range_not_satisfiable", parser); + return fail( + dstPath, complete, ec, "range_not_satisfiable", parser); } - else if (rangeStart && - p.get().result() != http::status::partial_content) + else if ( + rangeStart && p.get().result() != http::status::partial_content) { - ec.assign(boost::system::errc::not_supported, + ec.assign( + boost::system::errc::not_supported, boost::system::generic_category()); - return fail(dstPath, complete, ec, - "Range request ignored", parser); + return fail( + dstPath, complete, ec, "Range request ignored", parser); } else if (auto len = p.content_length()) { @@ -211,39 +219,47 @@ SSLHTTPDownloader::do_session( { if (*len > space(dstPath.parent_path()).available) { - return fail(dstPath, complete, ec, - "Insufficient disk space for download", parser); + return fail( + dstPath, + complete, + ec, + "Insufficient disk space for download", + parser); } } catch (std::exception const& e) { - return fail(dstPath, complete, ec, - std::string("exception: ") + e.what(), parser); + return fail( + dstPath, + complete, + ec, + std::string("exception: ") + e.what(), + parser); } } } - if(!skip) + if (!skip) { // Set up an HTTP GET request message to download the file req.method(http::verb::get); if (rangeStart) { - req.set(http::field::range, + req.set( + http::field::range, (boost::format("bytes=%llu-") % rangeStart).str()); } } http::async_write(*stream_, req, yield[ec]); - if(ec) + if (ec) return fail(dstPath, complete, ec, "async_write", parser); return true; }; - auto close = [&](auto p) - { + auto close = [&](auto p) { closeBody(p); // Gracefully close the stream @@ -254,15 +270,13 @@ SSLHTTPDownloader::do_session( { // Most web servers don't bother with performing // the SSL shutdown handshake, for speed. - JLOG(j_.trace()) << - "async_shutdown: " << ec.message(); + JLOG(j_.trace()) << "async_shutdown: " << ec.message(); } // The socket cannot be reused stream_ = boost::none; }; - auto getParser = [&] - { + auto getParser = [&] { auto p = this->getParser(dstPath, complete, ec); if (ec) fail(dstPath, complete, ec, "getParser", p); @@ -274,8 +288,7 @@ SSLHTTPDownloader::do_session( // because the server is shutting down, // this method notifies a 'Stoppable' // object that the session has ended. - auto exit = [this]() - { + auto exit = [this]() { std::lock_guard lock(m_); sessionActive_ = false; c_.notify_one(); @@ -289,7 +302,7 @@ SSLHTTPDownloader::do_session( sessionActive_ = true; } - if(isStopped_.load()) + if (isStopped_.load()) return exit(); auto p = getParser(); @@ -299,13 +312,13 @@ SSLHTTPDownloader::do_session( if (!connect(p) || ec) return exit(); - if(skip) + if (skip) p->skip(true); // Download the file while (!p->is_done()) { - if(isStopped_.load()) + if (isStopped_.load()) { close(p); return exit(); @@ -314,8 +327,7 @@ SSLHTTPDownloader::do_session( http::async_read_some(*stream_, read_buf_, *p, yield[ec]); } - JLOG(j_.trace()) << - "download completed: " << dstPath.string(); + JLOG(j_.trace()) << "download completed: " << dstPath.string(); close(p); exit(); @@ -334,13 +346,11 @@ SSLHTTPDownloader::fail( { if (!ec) { - JLOG(j_.error()) << - errMsg; + JLOG(j_.error()) << errMsg; } else if (ec != boost::asio::error::operation_aborted) { - JLOG(j_.error()) << - errMsg << ": " << ec.message(); + JLOG(j_.error()) << errMsg << ": " << ec.message(); } if (parser) @@ -353,11 +363,11 @@ SSLHTTPDownloader::fail( catch (std::exception const& e) { JLOG(j_.error()) << "exception: " << e.what() - << " in function: " << __func__; + << " in function: " << __func__; } complete(std::move(dstPath)); return false; } -}// ripple +} // namespace ripple diff --git a/src/ripple/nodestore/Backend.h b/src/ripple/nodestore/Backend.h index 5b2292e9fa..d3abc33142 100644 --- a/src/ripple/nodestore/Backend.h +++ b/src/ripple/nodestore/Backend.h @@ -48,18 +48,21 @@ public: /** Get the human-readable name of this backend. This is used for diagnostic output. */ - virtual std::string getName() = 0; + virtual std::string + getName() = 0; /** Open the backend. @param createIfMissing Create the database files if necessary. This allows the caller to catch exceptions. */ - virtual void open(bool createIfMissing = true) = 0; + virtual void + open(bool createIfMissing = true) = 0; /** Close the backend. This allows the caller to catch exceptions. */ - virtual void close() = 0; + virtual void + close() = 0; /** Fetch a single object. If the object is not found or an error is encountered, the @@ -69,17 +72,16 @@ public: @param pObject [out] The created object if successful. @return The result of the operation. */ - virtual Status fetch (void const* key, std::shared_ptr* pObject) = 0; + virtual Status + fetch(void const* key, std::shared_ptr* pObject) = 0; /** Return `true` if batch fetches are optimized. */ - virtual - bool + virtual bool canFetchBatch() = 0; /** Fetch a batch synchronously. */ - virtual - std::vector> - fetchBatch (std::size_t n, void const* const* keys) = 0; + virtual std::vector> + fetchBatch(std::size_t n, void const* const* keys) = 0; /** Store a single object. Depending on the implementation this may happen immediately @@ -87,13 +89,15 @@ public: @note This will be called concurrently. @param object The object to store. */ - virtual void store (std::shared_ptr const& object) = 0; + virtual void + store(std::shared_ptr const& object) = 0; /** Store a group of objects. @note This function will not be called concurrently with itself or @ref store. */ - virtual void storeBatch (Batch const& batch) = 0; + virtual void + storeBatch(Batch const& batch) = 0; /** Visit every object in the database This is usually called during import. @@ -101,19 +105,24 @@ public: or other methods. @see import */ - virtual void for_each (std::function )> f) = 0; + virtual void + for_each(std::function)> f) = 0; /** Estimate the number of write operations pending. */ - virtual int getWriteLoad () = 0; + virtual int + getWriteLoad() = 0; /** Remove contents on disk upon destruction. */ - virtual void setDeletePath() = 0; + virtual void + setDeletePath() = 0; /** Perform consistency checks on database. */ - virtual void verify() = 0; + virtual void + verify() = 0; /** Returns the number of file descriptors the backend expects to need. */ - virtual int fdRequired() const = 0; + virtual int + fdRequired() const = 0; /** Returns true if the backend uses permanent storage. */ bool @@ -123,7 +132,7 @@ public: } }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Database.h b/src/ripple/nodestore/Database.h index 3d91d45a02..a82d7a700f 100644 --- a/src/ripple/nodestore/Database.h +++ b/src/ripple/nodestore/Database.h @@ -20,13 +20,13 @@ #ifndef RIPPLE_NODESTORE_DATABASE_H_INCLUDED #define RIPPLE_NODESTORE_DATABASE_H_INCLUDED -#include #include +#include #include #include -#include -#include #include +#include +#include #include #include @@ -64,34 +64,35 @@ public: @param config The configuration settings @param journal Destination for logging output. */ - Database(std::string name, Stoppable& parent, Scheduler& scheduler, - int readThreads, Section const& config, beast::Journal j); + Database( + std::string name, + Stoppable& parent, + Scheduler& scheduler, + int readThreads, + Section const& config, + beast::Journal j); /** Destroy the node store. All pending operations are completed, pending writes flushed, and files closed before this returns. */ - virtual - ~Database(); + virtual ~Database(); /** Retrieve the name associated with this backend. This is used for diagnostics and may not reflect the actual path or paths used by the underlying backend. */ - virtual - std::string + virtual std::string getName() const = 0; /** Import objects from another database. */ - virtual - void + virtual void import(Database& source) = 0; /** Retrieve the estimated number of pending write operations. This is used for diagnostics. */ - virtual - std::int32_t + virtual std::int32_t getWriteLoad() const = 0; /** Store the object. @@ -106,10 +107,12 @@ public: @return `true` if the object was stored? */ - virtual - void - store(NodeObjectType type, Blob&& data, - uint256 const& hash, std::uint32_t seq) = 0; + virtual void + store( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + std::uint32_t seq) = 0; /** Fetch an object. If the object is known to be not in the database, isn't found in the @@ -121,8 +124,7 @@ public: @param seq The sequence of the ledger where the object is stored. @return The object, or nullptr if it couldn't be retrieved. */ - virtual - std::shared_ptr + virtual std::shared_ptr fetch(uint256 const& hash, std::uint32_t seq) = 0; /** Fetch an object without waiting. @@ -137,9 +139,10 @@ public: @param object The object retrieved @return Whether the operation completed */ - virtual - bool - asyncFetch(uint256 const& hash, std::uint32_t seq, + virtual bool + asyncFetch( + uint256 const& hash, + std::uint32_t seq, std::shared_ptr& object) = 0; /** Copies a ledger stored in a different database to this one. @@ -147,12 +150,11 @@ public: @param ledger The ledger to copy. @return true if the operation was successful */ - virtual - bool + virtual bool storeLedger(std::shared_ptr const& srcLedger) = 0; /** Wait for all currently pending async reads to complete. - */ + */ void waitReads(); @@ -162,13 +164,11 @@ public: @return The number of async reads preferred. @note The sequence is only used with the shard store. */ - virtual - int + virtual int getDesiredAsyncReadCount(std::uint32_t seq) = 0; /** Get the positive cache hits to total attempts ratio. */ - virtual - float + virtual float getCacheHitRate() = 0; /** Set the maximum number of entries and maximum cache age for both caches. @@ -176,13 +176,11 @@ public: @param size Number of cache entries (0 = ignore) @param age Maximum cache age in seconds */ - virtual - void + virtual void tune(int size, std::chrono::seconds age) = 0; /** Remove expired entries from the positive and negative caches. */ - virtual - void + virtual void sweep() = 0; /** Gather statistics pertaining to read and write activities. @@ -190,23 +188,41 @@ public: @return The total read and written bytes. */ std::uint32_t - getStoreCount() const { return storeCount_; } + getStoreCount() const + { + return storeCount_; + } std::uint32_t - getFetchTotalCount() const { return fetchTotalCount_; } + getFetchTotalCount() const + { + return fetchTotalCount_; + } std::uint32_t - getFetchHitCount() const { return fetchHitCount_; } + getFetchHitCount() const + { + return fetchHitCount_; + } std::uint32_t - getStoreSize() const { return storeSz_; } + getStoreSize() const + { + return storeSz_; + } std::uint32_t - getFetchSize() const { return fetchSz_; } + getFetchSize() const + { + return fetchSz_; + } /** Returns the number of file descriptors the database expects to need */ int - fdRequired() const { return fdRequired_; } + fdRequired() const + { + return fdRequired_; + } void onStop() override; @@ -215,7 +231,7 @@ public: onChildrenStopped() override; /** @return The earliest ledger sequence allowed - */ + */ std::uint32_t earliestLedgerSeq() const { @@ -225,7 +241,7 @@ public: protected: beast::Journal const j_; Scheduler& scheduler_; - int fdRequired_ {0}; + int fdRequired_{0}; void stopThreads(); @@ -239,9 +255,11 @@ protected: // Called by the public asyncFetch function void - asyncFetch(uint256 const& hash, std::uint32_t seq, + asyncFetch( + uint256 const& hash, + std::uint32_t seq, std::shared_ptr> const& pCache, - std::shared_ptr> const& nCache); + std::shared_ptr> const& nCache); // Called by the public fetch function std::shared_ptr @@ -252,9 +270,12 @@ protected: importInternal(Backend& dstBackend, Database& srcDB); std::shared_ptr - doFetch(uint256 const& hash, std::uint32_t seq, + doFetch( + uint256 const& hash, + std::uint32_t seq, TaggedCache& pCache, - KeyCache& nCache, bool isAsync); + KeyCache& nCache, + bool isAsync); // Called by the public storeLedger function bool @@ -266,36 +287,39 @@ protected: std::shared_ptr next); private: - std::atomic storeCount_ {0}; - std::atomic fetchTotalCount_ {0}; - std::atomic fetchHitCount_ {0}; - std::atomic storeSz_ {0}; - std::atomic fetchSz_ {0}; + std::atomic storeCount_{0}; + std::atomic fetchTotalCount_{0}; + std::atomic fetchHitCount_{0}; + std::atomic storeSz_{0}; + std::atomic fetchSz_{0}; std::mutex readLock_; std::condition_variable readCondVar_; std::condition_variable readGenCondVar_; // reads to do - std::map>, - std::weak_ptr>>> read_; + std::map< + uint256, + std::tuple< + std::uint32_t, + std::weak_ptr>, + std::weak_ptr>>> + read_; // last read uint256 readLastHash_; std::vector readThreads_; - bool readShut_ {false}; + bool readShut_{false}; // current read generation - uint64_t readGen_ {0}; + uint64_t readGen_{0}; // The default is 32570 to match the XRP ledger network's earliest // allowed sequence. Alternate networks may set this value. std::uint32_t const earliestLedgerSeq_; - virtual - std::shared_ptr + virtual std::shared_ptr fetchFrom(uint256 const& hash, std::uint32_t seq) = 0; /** Visit every object in the database @@ -305,15 +329,14 @@ private: or other methods. @see import */ - virtual - void - for_each(std::function )> f) = 0; + virtual void + for_each(std::function)> f) = 0; void threadEntry(); }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/DatabaseRotating.h b/src/ripple/nodestore/DatabaseRotating.h index b44c6849c2..caf5a9c4a9 100644 --- a/src/ripple/nodestore/DatabaseRotating.h +++ b/src/ripple/nodestore/DatabaseRotating.h @@ -41,26 +41,25 @@ public: Section const& config, beast::Journal journal) : Database(name, parent, scheduler, readThreads, config, journal) - {} + { + } - virtual - TaggedCache const& + virtual TaggedCache const& getPositiveCache() = 0; - virtual std::mutex& peekMutex() const = 0; + virtual std::mutex& + peekMutex() const = 0; - virtual - std::shared_ptr const& + virtual std::shared_ptr const& getWritableBackend() const = 0; - virtual - std::shared_ptr + virtual std::shared_ptr rotateBackends( std::shared_ptr newBackend, std::lock_guard const&) = 0; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/DatabaseShard.h b/src/ripple/nodestore/DatabaseShard.h index 0e86664ca8..3d08602720 100644 --- a/src/ripple/nodestore/DatabaseShard.h +++ b/src/ripple/nodestore/DatabaseShard.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_NODESTORE_DATABASESHARD_H_INCLUDED #define RIPPLE_NODESTORE_DATABASESHARD_H_INCLUDED -#include #include #include +#include #include #include @@ -33,7 +33,7 @@ namespace ripple { namespace NodeStore { /** A collection of historical shards -*/ + */ class DatabaseShard : public Database { public: @@ -61,23 +61,21 @@ public: @return `true` if the database initialized without error */ - virtual - bool + virtual bool init() = 0; /** Prepare to store a new ledger in the shard being acquired @param validLedgerSeq The index of the maximum valid ledgers - @return If a ledger should be fetched and stored, then returns the ledger - index of the ledger to request. Otherwise returns boost::none. + @return If a ledger should be fetched and stored, then returns the + ledger index of the ledger to request. Otherwise returns boost::none. Some reasons this may return boost::none are: all shards are stored and full, max allowed disk space would be exceeded, or a ledger was recently requested and not enough time has passed between requests. @implNote adds a new writable shard if necessary */ - virtual - boost::optional + virtual boost::optional prepareLedger(std::uint32_t validLedgerSeq) = 0; /** Prepare a shard index to be imported into the database @@ -85,24 +83,21 @@ public: @param shardIndex Shard index to be prepared for import @return true if shard index successfully prepared for import */ - virtual - bool + virtual bool prepareShard(std::uint32_t shardIndex) = 0; /** Remove a previously prepared shard index for import @param shardIndex Shard index to be removed from import */ - virtual - void + virtual void removePreShard(std::uint32_t shardIndex) = 0; /** Get shard indexes being imported @return a string representing the shards prepared for import */ - virtual - std::string + virtual std::string getPreShards() = 0; /** Import a shard into the shard database @@ -112,8 +107,7 @@ public: @return true If the shard was successfully imported @implNote if successful, srcDir is moved to the database directory */ - virtual - bool + virtual bool importShard( std::uint32_t shardIndex, boost::filesystem::path const& srcDir) = 0; @@ -124,8 +118,7 @@ public: @param seq The sequence of the ledger @return The ledger if found, nullptr otherwise */ - virtual - std::shared_ptr + virtual std::shared_ptr fetchLedger(uint256 const& hash, std::uint32_t seq) = 0; /** Notifies the database that the given ledger has been @@ -133,36 +126,31 @@ public: @param ledger The stored ledger to be marked as complete */ - virtual - void + virtual void setStored(std::shared_ptr const& ledger) = 0; /** Query which complete shards are stored @return the indexes of complete shards */ - virtual - std::string + virtual std::string getCompleteShards() = 0; /** Verifies shard store data is valid. @param app The application object */ - virtual - void + virtual void validate() = 0; /** @return The maximum number of ledgers stored in a shard - */ - virtual - std::uint32_t + */ + virtual std::uint32_t ledgersPerShard() const = 0; /** @return The earliest shard index - */ - virtual - std::uint32_t + */ + virtual std::uint32_t earliestShardIndex() const = 0; /** Calculates the shard index for a given ledger sequence @@ -170,8 +158,7 @@ public: @param seq ledger sequence @return The shard index of the ledger sequence */ - virtual - std::uint32_t + virtual std::uint32_t seqToShardIndex(std::uint32_t seq) const = 0; /** Calculates the first ledger sequence for a given shard index @@ -179,8 +166,7 @@ public: @param shardIndex The shard index considered @return The first ledger sequence pertaining to the shard index */ - virtual - std::uint32_t + virtual std::uint32_t firstLedgerSeq(std::uint32_t shardIndex) const = 0; /** Calculates the last ledger sequence for a given shard index @@ -188,30 +174,27 @@ public: @param shardIndex The shard index considered @return The last ledger sequence pertaining to the shard index */ - virtual - std::uint32_t + virtual std::uint32_t lastLedgerSeq(std::uint32_t shardIndex) const = 0; /** Returns the root database directory - */ - virtual - boost::filesystem::path const& + */ + virtual boost::filesystem::path const& getRootDir() const = 0; /** The number of ledgers in a shard */ - static constexpr std::uint32_t ledgersPerShardDefault {16384u}; + static constexpr std::uint32_t ledgersPerShardDefault{16384u}; }; -constexpr -std::uint32_t -seqToShardIndex(std::uint32_t seq, +constexpr std::uint32_t +seqToShardIndex( + std::uint32_t seq, std::uint32_t ledgersPerShard = DatabaseShard::ledgersPerShardDefault) { return (seq - 1) / ledgersPerShard; } -extern -std::unique_ptr +extern std::unique_ptr make_ShardStore( Application& app, Stoppable& parent, @@ -219,7 +202,7 @@ make_ShardStore( int readThreads, beast::Journal j); -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/DummyScheduler.h b/src/ripple/nodestore/DummyScheduler.h index 595edbfebb..d5c13b8084 100644 --- a/src/ripple/nodestore/DummyScheduler.h +++ b/src/ripple/nodestore/DummyScheduler.h @@ -29,15 +29,19 @@ namespace NodeStore { class DummyScheduler : public Scheduler { public: - DummyScheduler () = default; - ~DummyScheduler () = default; - void scheduleTask (Task& task) override; - void scheduledTasksStopped (); - void onFetch (FetchReport const& report) override; - void onBatchWrite (BatchWriteReport const& report) override; + DummyScheduler() = default; + ~DummyScheduler() = default; + void + scheduleTask(Task& task) override; + void + scheduledTasksStopped(); + void + onFetch(FetchReport const& report) override; + void + onBatchWrite(BatchWriteReport const& report) override; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Factory.h b/src/ripple/nodestore/Factory.h index 6152276487..eabad965f4 100644 --- a/src/ripple/nodestore/Factory.h +++ b/src/ripple/nodestore/Factory.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_NODESTORE_FACTORY_H_INCLUDED #define RIPPLE_NODESTORE_FACTORY_H_INCLUDED +#include #include #include -#include #include namespace ripple { @@ -32,12 +32,10 @@ namespace NodeStore { class Factory { public: - virtual - ~Factory() = default; + virtual ~Factory() = default; /** Retrieve the name of this factory. */ - virtual - std::string + virtual std::string getName() const = 0; /** Create an instance of this factory's backend. @@ -47,9 +45,8 @@ public: @param scheduler The scheduler to use for running tasks. @return A pointer to the Backend object. */ - virtual - std::unique_ptr - createInstance ( + virtual std::unique_ptr + createInstance( size_t keyBytes, Section const& parameters, Scheduler& scheduler, @@ -63,9 +60,8 @@ public: @param context The context used by database. @return A pointer to the Backend object. */ - virtual - std::unique_ptr - createInstance ( + virtual std::unique_ptr + createInstance( size_t keyBytes, Section const& parameters, Scheduler& scheduler, @@ -76,7 +72,7 @@ public: } }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Manager.h b/src/ripple/nodestore/Manager.h index eb95b3b223..60350e4d48 100644 --- a/src/ripple/nodestore/Manager.h +++ b/src/ripple/nodestore/Manager.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_NODESTORE_MANAGER_H_INCLUDED #define RIPPLE_NODESTORE_MANAGER_H_INCLUDED -#include #include #include +#include namespace ripple { namespace NodeStore { @@ -31,39 +31,37 @@ namespace NodeStore { class Manager { public: - virtual ~Manager () = default; + virtual ~Manager() = default; Manager() = default; Manager(Manager const&) = delete; - Manager& operator=(Manager const&) = delete; + Manager& + operator=(Manager const&) = delete; /** Returns the instance of the manager singleton. */ - static - Manager& + static Manager& instance(); /** Add a factory. */ - virtual - void - insert (Factory& factory) = 0; + virtual void + insert(Factory& factory) = 0; /** Remove a factory. */ - virtual - void - erase (Factory& factory) = 0; + virtual void + erase(Factory& factory) = 0; /** Return a pointer to the matching factory if it exists. @param name The name to match, performed case-insensitive. @return `nullptr` if a match was not found. */ - virtual - Factory* + virtual Factory* find(std::string const& name) = 0; /** Create a backend. */ - virtual - std::unique_ptr - make_Backend (Section const& parameters, - Scheduler& scheduler, beast::Journal journal) = 0; + virtual std::unique_ptr + make_Backend( + Section const& parameters, + Scheduler& scheduler, + beast::Journal journal) = 0; /** Construct a NodeStore database. @@ -79,32 +77,39 @@ public: synchronous scheduler is used which performs all tasks immediately on the caller's thread. - @note If the database cannot be opened or created, an exception is thrown. + @note If the database cannot be opened or created, an exception is + thrown. @param name A diagnostic label for the database. @param scheduler The scheduler to use for performing asynchronous tasks. @param readThreads The number of async read threads to create - @param backendParameters The parameter string for the persistent backend. - @param fastBackendParameters [optional] The parameter string for the ephemeral backend. + @param backendParameters The parameter string for the persistent + backend. + @param fastBackendParameters [optional] The parameter string for the + ephemeral backend. @return The opened database. */ - virtual - std::unique_ptr - make_Database (std::string const& name, Scheduler& scheduler, - int readThreads, Stoppable& parent, - Section const& backendParameters, - beast::Journal journal) = 0; + virtual std::unique_ptr + make_Database( + std::string const& name, + Scheduler& scheduler, + int readThreads, + Stoppable& parent, + Section const& backendParameters, + beast::Journal journal) = 0; }; //------------------------------------------------------------------------------ /** Create a Backend. */ -std::unique_ptr -make_Backend (Section const& config, - Scheduler& scheduler, beast::Journal journal); +std::unique_ptr +make_Backend( + Section const& config, + Scheduler& scheduler, + beast::Journal journal); -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/NodeObject.h b/src/ripple/nodestore/NodeObject.h index 90438deb3e..7c33169939 100644 --- a/src/ripple/nodestore/NodeObject.h +++ b/src/ripple/nodestore/NodeObject.h @@ -29,9 +29,7 @@ namespace ripple { /** The types of node objects. */ -enum NodeObjectType - : std::uint32_t -{ +enum NodeObjectType : std::uint32_t { hotUNKNOWN = 0, hotLEDGER = 1, hotACCOUNT_NODE = 3, @@ -47,10 +45,14 @@ enum NodeObjectType @note No checking is performed to make sure the hash matches the data. @see SHAMap */ -class NodeObject : public CountedObject +class NodeObject : public CountedObject { public: - static char const* getCountedObjectName () { return "NodeObject"; } + static char const* + getCountedObjectName() + { + return "NodeObject"; + } static constexpr std::size_t keyBytes = 32; @@ -62,12 +64,14 @@ private: { explicit PrivateAccess() = default; }; + public: // This constructor is private, use createObject instead. - NodeObject (NodeObjectType type, - Blob&& data, - uint256 const& hash, - PrivateAccess); + NodeObject( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + PrivateAccess); /** Create an object from fields. @@ -80,19 +84,20 @@ public: is overwritten. @param hash The 256-bit hash of the payload data. */ - static - std::shared_ptr - createObject (NodeObjectType type, - Blob&& data, uint256 const& hash); + static std::shared_ptr + createObject(NodeObjectType type, Blob&& data, uint256 const& hash); /** Returns the type of this object. */ - NodeObjectType getType () const; + NodeObjectType + getType() const; /** Returns the hash of the data. */ - uint256 const& getHash () const; + uint256 const& + getHash() const; /** Returns the underlying data. */ - Blob const& getData () const; + Blob const& + getData() const; private: NodeObjectType const mType; @@ -100,6 +105,6 @@ private: Blob const mData; }; -} +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Scheduler.h b/src/ripple/nodestore/Scheduler.h index cf7bd62d09..9d12f6d2dc 100644 --- a/src/ripple/nodestore/Scheduler.h +++ b/src/ripple/nodestore/Scheduler.h @@ -61,23 +61,26 @@ public: /** Schedules a task. Depending on the implementation, the task may be invoked either on - the current thread of execution, or an unspecified implementation-defined - foreign thread. + the current thread of execution, or an unspecified + implementation-defined foreign thread. */ - virtual void scheduleTask (Task& task) = 0; + virtual void + scheduleTask(Task& task) = 0; /** Reports completion of a fetch Allows the scheduler to monitor the node store's performance */ - virtual void onFetch (FetchReport const& report) = 0; + virtual void + onFetch(FetchReport const& report) = 0; /** Reports the completion of a batch write Allows the scheduler to monitor the node store's performance */ - virtual void onBatchWrite (BatchWriteReport const& report) = 0; + virtual void + onBatchWrite(BatchWriteReport const& report) = 0; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Task.h b/src/ripple/nodestore/Task.h index 6f9e85e8ee..290d83588a 100644 --- a/src/ripple/nodestore/Task.h +++ b/src/ripple/nodestore/Task.h @@ -31,10 +31,11 @@ struct Task /** Performs the task. The call may take place on a foreign thread. */ - virtual void performScheduledTask() = 0; + virtual void + performScheduledTask() = 0; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/Types.h b/src/ripple/nodestore/Types.h index 7d2be19562..fc7164c746 100644 --- a/src/ripple/nodestore/Types.h +++ b/src/ripple/nodestore/Types.h @@ -20,15 +20,14 @@ #ifndef RIPPLE_NODESTORE_TYPES_H_INCLUDED #define RIPPLE_NODESTORE_TYPES_H_INCLUDED -#include #include +#include #include namespace ripple { namespace NodeStore { -enum -{ +enum { // This is only used to pre-allocate the array for // batch objects and does not affect the amount written. // @@ -42,8 +41,7 @@ enum }; /** Return codes from Backend operations. */ -enum Status -{ +enum Status { ok, notFound, dataCorrupt, @@ -53,9 +51,9 @@ enum Status }; /** A batch of NodeObjects to write at once. */ -using Batch = std::vector >; +using Batch = std::vector>; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/VisitCallback.h b/src/ripple/nodestore/VisitCallback.h index 56443879d7..b21b7b6ee4 100644 --- a/src/ripple/nodestore/VisitCallback.h +++ b/src/ripple/nodestore/VisitCallback.h @@ -30,10 +30,11 @@ namespace NodeStore { // VFALCO DEPRECATED Use std::function instead struct VisitCallback { - virtual void visitObject (NodeObject::Ptr const& object) = 0; + virtual void + visitObject(NodeObject::Ptr const& object) = 0; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/backend/MemoryFactory.cpp b/src/ripple/nodestore/backend/MemoryFactory.cpp index fa121a3bfc..a71343ef30 100644 --- a/src/ripple/nodestore/backend/MemoryFactory.cpp +++ b/src/ripple/nodestore/backend/MemoryFactory.cpp @@ -35,14 +35,14 @@ struct MemoryDB std::mutex mutex; bool open = false; - std::map > table; + std::map> table; }; class MemoryFactory : public Factory { private: std::mutex mutex_; - std::map map_; + std::map map_; public: MemoryFactory(); @@ -51,22 +51,22 @@ public: std::string getName() const override; - std::unique_ptr - createInstance ( + std::unique_ptr + createInstance( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) override; MemoryDB& - open (std::string const& path) + open(std::string const& path) { std::lock_guard _(mutex_); - auto const result = map_.emplace (std::piecewise_construct, - std::make_tuple(path), std::make_tuple()); + auto const result = map_.emplace( + std::piecewise_construct, std::make_tuple(path), std::make_tuple()); MemoryDB& db = result.first->second; if (db.open) - Throw ("already open"); + Throw("already open"); return db; } }; @@ -78,30 +78,32 @@ static MemoryFactory memoryFactory; class MemoryBackend : public Backend { private: - using Map = std::map >; + using Map = std::map>; std::string name_; beast::Journal const journal_; - MemoryDB* db_ {nullptr}; + MemoryDB* db_{nullptr}; public: - MemoryBackend (size_t keyBytes, Section const& keyValues, - Scheduler& scheduler, beast::Journal journal) - : name_ (get(keyValues, "path")) - , journal_ (journal) + MemoryBackend( + size_t keyBytes, + Section const& keyValues, + Scheduler& scheduler, + beast::Journal journal) + : name_(get(keyValues, "path")), journal_(journal) { - boost::ignore_unused (journal_); // Keep unused journal_ just in case. + boost::ignore_unused(journal_); // Keep unused journal_ just in case. if (name_.empty()) - Throw ("Missing path in Memory backend"); + Throw("Missing path in Memory backend"); } - ~MemoryBackend () override + ~MemoryBackend() override { close(); } std::string - getName () override + getName() override { return name_; } @@ -121,14 +123,14 @@ public: //-------------------------------------------------------------------------- Status - fetch (void const* key, std::shared_ptr* pObject) override + fetch(void const* key, std::shared_ptr* pObject) override { assert(db_); - uint256 const hash (uint256::fromVoid (key)); + uint256 const hash(uint256::fromVoid(key)); std::lock_guard _(db_->mutex); - Map::iterator iter = db_->table.find (hash); + Map::iterator iter = db_->table.find(hash); if (iter == db_->table.end()) { pObject->reset(); @@ -145,33 +147,33 @@ public: } std::vector> - fetchBatch (std::size_t n, void const* const* keys) override + fetchBatch(std::size_t n, void const* const* keys) override { - Throw ("pure virtual called"); + Throw("pure virtual called"); return {}; } void - store (std::shared_ptr const& object) override + store(std::shared_ptr const& object) override { assert(db_); std::lock_guard _(db_->mutex); - db_->table.emplace (object->getHash(), object); + db_->table.emplace(object->getHash(), object); } void - storeBatch (Batch const& batch) override + storeBatch(Batch const& batch) override { for (auto const& e : batch) - store (e); + store(e); } void - for_each (std::function )> f) override + for_each(std::function)> f) override { assert(db_); for (auto const& e : db_->table) - f (e.second); + f(e.second); } int @@ -215,16 +217,16 @@ MemoryFactory::getName() const return "Memory"; } -std::unique_ptr -MemoryFactory::createInstance ( +std::unique_ptr +MemoryFactory::createInstance( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) { - return std::make_unique ( + return std::make_unique( keyBytes, keyValues, scheduler, journal); } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/backend/NuDBFactory.cpp b/src/ripple/nodestore/backend/NuDBFactory.cpp index bf56609339..8147f218cc 100644 --- a/src/ripple/nodestore/backend/NuDBFactory.cpp +++ b/src/ripple/nodestore/backend/NuDBFactory.cpp @@ -17,27 +17,25 @@ */ //============================================================================== - #include #include #include -#include #include #include -#include +#include #include #include #include -#include #include +#include #include #include +#include namespace ripple { namespace NodeStore { -class NuDBBackend - : public Backend +class NuDBBackend : public Backend { public: static constexpr std::size_t currentType = 1; @@ -46,44 +44,44 @@ public: size_t const keyBytes_; std::string const name_; nudb::store db_; - std::atomic deletePath_; + std::atomic deletePath_; Scheduler& scheduler_; - NuDBBackend ( + NuDBBackend( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) : j_(journal) - , keyBytes_ (keyBytes) - , name_ (get(keyValues, "path")) + , keyBytes_(keyBytes) + , name_(get(keyValues, "path")) , deletePath_(false) - , scheduler_ (scheduler) + , scheduler_(scheduler) { if (name_.empty()) - Throw ( + Throw( "nodestore: Missing path in NuDB backend"); } - NuDBBackend ( + NuDBBackend( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, nudb::context& context, beast::Journal journal) : j_(journal) - , keyBytes_ (keyBytes) - , name_ (get(keyValues, "path")) - , db_ (context) + , keyBytes_(keyBytes) + , name_(get(keyValues, "path")) + , db_(context) , deletePath_(false) - , scheduler_ (scheduler) + , scheduler_(scheduler) { if (name_.empty()) - Throw ( + Throw( "nodestore: Missing path in NuDB backend"); } - ~NuDBBackend () override + ~NuDBBackend() override { close(); } @@ -101,8 +99,7 @@ public: if (db_.is_open()) { assert(false); - JLOG(j_.error()) << - "database is already open"; + JLOG(j_.error()) << "database is already open"; return; } auto const folder = path(name_); @@ -113,20 +110,26 @@ public: if (createIfMissing) { create_directories(folder); - nudb::create(dp, kp, lp, - currentType, nudb::make_salt(), keyBytes_, - nudb::block_size(kp), 0.50, ec); - if(ec == nudb::errc::file_exists) + nudb::create( + dp, + kp, + lp, + currentType, + nudb::make_salt(), + keyBytes_, + nudb::block_size(kp), + 0.50, + ec); + if (ec == nudb::errc::file_exists) ec = {}; - if(ec) + if (ec) Throw(ec); } - db_.open (dp, kp, lp, ec); - if(ec) + db_.open(dp, kp, lp, ec); + if (ec) Throw(ec); if (db_.appnum() != currentType) - Throw( - "nodestore: unknown appnum"); + Throw("nodestore: unknown appnum"); } void @@ -136,39 +139,39 @@ public: { nudb::error_code ec; db_.close(ec); - if(ec) + if (ec) Throw(ec); if (deletePath_) { - boost::filesystem::remove_all (name_); + boost::filesystem::remove_all(name_); } } } Status - fetch (void const* key, std::shared_ptr* pno) override + fetch(void const* key, std::shared_ptr* pno) override { Status status; pno->reset(); nudb::error_code ec; - db_.fetch (key, - [key, pno, &status](void const* data, std::size_t size) - { + db_.fetch( + key, + [key, pno, &status](void const* data, std::size_t size) { nudb::detail::buffer bf; - auto const result = - nodeobject_decompress(data, size, bf); - DecodedBlob decoded (key, result.first, result.second); - if (! decoded.wasOk ()) + auto const result = nodeobject_decompress(data, size, bf); + DecodedBlob decoded(key, result.first, result.second); + if (!decoded.wasOk()) { status = dataCorrupt; return; } *pno = decoded.createObject(); status = ok; - }, ec); - if(ec == nudb::error::key_not_found) + }, + ec); + if (ec == nudb::error::key_not_found) return notFound; - if(ec) + if (ec) Throw(ec); return status; } @@ -180,92 +183,89 @@ public: } std::vector> - fetchBatch (std::size_t n, void const* const* keys) override + fetchBatch(std::size_t n, void const* const* keys) override { - Throw ("pure virtual called"); + Throw("pure virtual called"); return {}; } void - do_insert (std::shared_ptr const& no) + do_insert(std::shared_ptr const& no) { EncodedBlob e; - e.prepare (no); + e.prepare(no); nudb::error_code ec; nudb::detail::buffer bf; - auto const result = nodeobject_compress( - e.getData(), e.getSize(), bf); - db_.insert (e.getKey(), result.first, result.second, ec); - if(ec && ec != nudb::error::key_exists) + auto const result = nodeobject_compress(e.getData(), e.getSize(), bf); + db_.insert(e.getKey(), result.first, result.second, ec); + if (ec && ec != nudb::error::key_exists) Throw(ec); } void - store (std::shared_ptr const& no) override + store(std::shared_ptr const& no) override { BatchWriteReport report; report.writeCount = 1; - auto const start = - std::chrono::steady_clock::now(); - do_insert (no); - report.elapsed = std::chrono::duration_cast < - std::chrono::milliseconds>( - std::chrono::steady_clock::now() - start); - scheduler_.onBatchWrite (report); + auto const start = std::chrono::steady_clock::now(); + do_insert(no); + report.elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start); + scheduler_.onBatchWrite(report); } void - storeBatch (Batch const& batch) override + storeBatch(Batch const& batch) override { BatchWriteReport report; report.writeCount = batch.size(); - auto const start = - std::chrono::steady_clock::now(); + auto const start = std::chrono::steady_clock::now(); for (auto const& e : batch) - do_insert (e); - report.elapsed = std::chrono::duration_cast < - std::chrono::milliseconds>( - std::chrono::steady_clock::now() - start); - scheduler_.onBatchWrite (report); + do_insert(e); + report.elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - start); + scheduler_.onBatchWrite(report); } void - for_each (std::function )> f) override + for_each(std::function)> f) override { auto const dp = db_.dat_path(); auto const kp = db_.key_path(); auto const lp = db_.log_path(); - //auto const appnum = db_.appnum(); + // auto const appnum = db_.appnum(); nudb::error_code ec; db_.close(ec); - if(ec) + if (ec) Throw(ec); - nudb::visit(dp, - [&]( - void const* key, std::size_t key_bytes, - void const* data, std::size_t size, - nudb::error_code&) - { + nudb::visit( + dp, + [&](void const* key, + std::size_t key_bytes, + void const* data, + std::size_t size, + nudb::error_code&) { nudb::detail::buffer bf; - auto const result = - nodeobject_decompress(data, size, bf); - DecodedBlob decoded (key, result.first, result.second); - if (! decoded.wasOk ()) + auto const result = nodeobject_decompress(data, size, bf); + DecodedBlob decoded(key, result.first, result.second); + if (!decoded.wasOk()) { ec = make_error_code(nudb::error::missing_value); return; } - f (decoded.createObject()); - }, nudb::no_progress{}, ec); - if(ec) + f(decoded.createObject()); + }, + nudb::no_progress{}, + ec); + if (ec) Throw(ec); db_.open(dp, kp, lp, ec); - if(ec) + if (ec) Throw(ec); } int - getWriteLoad () override + getWriteLoad() override { return 0; } @@ -284,15 +284,14 @@ public: auto const lp = db_.log_path(); nudb::error_code ec; db_.close(ec); - if(ec) + if (ec) Throw(ec); nudb::verify_info vi; - nudb::verify( - vi, dp, kp, 0, nudb::no_progress{}, ec); - if(ec) + nudb::verify(vi, dp, kp, 0, nudb::no_progress{}, ec); + if (ec) Throw(ec); - db_.open (dp, kp, lp, ec); - if(ec) + db_.open(dp, kp, lp, ec); + if (ec) Throw(ec); } @@ -324,31 +323,31 @@ public: return "NuDB"; } - std::unique_ptr - createInstance ( + std::unique_ptr + createInstance( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) override { - return std::make_unique ( + return std::make_unique( keyBytes, keyValues, scheduler, journal); } - std::unique_ptr - createInstance ( + std::unique_ptr + createInstance( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, nudb::context& context, beast::Journal journal) override { - return std::make_unique ( + return std::make_unique( keyBytes, keyValues, scheduler, context, journal); } }; static NuDBFactory nuDBFactory; -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/backend/NullFactory.cpp b/src/ripple/nodestore/backend/NullFactory.cpp index 4db5274eb1..2624fa9709 100644 --- a/src/ripple/nodestore/backend/NullFactory.cpp +++ b/src/ripple/nodestore/backend/NullFactory.cpp @@ -35,7 +35,7 @@ public: std::string getName() override { - return std::string (); + return std::string(); } void @@ -49,7 +49,7 @@ public: } Status - fetch (void const*, std::shared_ptr*) override + fetch(void const*, std::shared_ptr*) override { return notFound; } @@ -61,29 +61,29 @@ public: } std::vector> - fetchBatch (std::size_t n, void const* const* keys) override + fetchBatch(std::size_t n, void const* const* keys) override { - Throw ("pure virtual called"); + Throw("pure virtual called"); return {}; } void - store (std::shared_ptr const& object) override + store(std::shared_ptr const& object) override { } void - storeBatch (Batch const& batch) override + storeBatch(Batch const& batch) override { } void - for_each (std::function )> f) override + for_each(std::function)> f) override { } int - getWriteLoad () override + getWriteLoad() override { return 0; } @@ -124,22 +124,19 @@ public: } std::string - getName () const override + getName() const override { return "none"; } - std::unique_ptr - createInstance ( - size_t, - Section const&, - Scheduler&, beast::Journal) override + std::unique_ptr + createInstance(size_t, Section const&, Scheduler&, beast::Journal) override { - return std::make_unique (); + return std::make_unique(); } }; static NullFactory nullFactory; -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/backend/RocksDBFactory.cpp b/src/ripple/nodestore/backend/RocksDBFactory.cpp index ff6710ca8c..87ef712cdd 100644 --- a/src/ripple/nodestore/backend/RocksDBFactory.cpp +++ b/src/ripple/nodestore/backend/RocksDBFactory.cpp @@ -17,20 +17,19 @@ */ //============================================================================== - #include #if RIPPLE_ROCKSDB_AVAILABLE -#include #include -#include // VFALCO Bad dependency +#include +#include +#include // VFALCO Bad dependency #include #include #include #include #include -#include #include #include @@ -40,16 +39,13 @@ namespace NodeStore { class RocksDBEnv : public rocksdb::EnvWrapper { public: - RocksDBEnv () - : EnvWrapper (rocksdb::Env::Default()) + RocksDBEnv() : EnvWrapper(rocksdb::Env::Default()) { } struct ThreadParams { - ThreadParams (void (*f_)(void*), void* a_) - : f (f_) - , a (a_) + ThreadParams(void (*f_)(void*), void* a_) : f(f_), a(a_) { } @@ -57,40 +53,37 @@ public: void* a; }; - static - void - thread_entry (void* ptr) + static void + thread_entry(void* ptr) { - ThreadParams* const p (reinterpret_cast (ptr)); + ThreadParams* const p(reinterpret_cast(ptr)); void (*f)(void*) = p->f; - void* a (p->a); + void* a(p->a); delete p; - static std::atomic n; - std::size_t const id (++n); + static std::atomic n; + std::size_t const id(++n); std::stringstream ss; ss << "rocksdb #" << id; - beast::setCurrentThreadName (ss.str()); + beast::setCurrentThreadName(ss.str()); (*f)(a); } void - StartThread (void (*f)(void*), void* a) override + StartThread(void (*f)(void*), void* a) override { - ThreadParams* const p (new ThreadParams (f, a)); - EnvWrapper::StartThread (&RocksDBEnv::thread_entry, p); + ThreadParams* const p(new ThreadParams(f, a)); + EnvWrapper::StartThread(&RocksDBEnv::thread_entry, p); } }; //------------------------------------------------------------------------------ -class RocksDBBackend - : public Backend - , public BatchWriter::Callback +class RocksDBBackend : public Backend, public BatchWriter::Callback { private: - std::atomic m_deletePath; + std::atomic m_deletePath; public: beast::Journal m_journal; @@ -98,57 +91,66 @@ public: Scheduler& m_scheduler; BatchWriter m_batch; std::string m_name; - std::unique_ptr m_db; + std::unique_ptr m_db; int fdRequired_ = 2048; rocksdb::Options m_options; - RocksDBBackend (int keyBytes, Section const& keyValues, - Scheduler& scheduler, beast::Journal journal, RocksDBEnv* env) - : m_deletePath (false) - , m_journal (journal) - , m_keyBytes (keyBytes) - , m_scheduler (scheduler) - , m_batch (*this, scheduler) + RocksDBBackend( + int keyBytes, + Section const& keyValues, + Scheduler& scheduler, + beast::Journal journal, + RocksDBEnv* env) + : m_deletePath(false) + , m_journal(journal) + , m_keyBytes(keyBytes) + , m_scheduler(scheduler) + , m_batch(*this, scheduler) { - if (! get_if_exists(keyValues, "path", m_name)) - Throw ("Missing path in RocksDBFactory backend"); + if (!get_if_exists(keyValues, "path", m_name)) + Throw("Missing path in RocksDBFactory backend"); rocksdb::BlockBasedTableOptions table_options; m_options.env = env; - if (keyValues.exists ("cache_mb")) - table_options.block_cache = rocksdb::NewLRUCache ( + if (keyValues.exists("cache_mb")) + table_options.block_cache = rocksdb::NewLRUCache( get(keyValues, "cache_mb") * megabytes(1)); if (auto const v = get(keyValues, "filter_bits")) { - bool const filter_blocks = !keyValues.exists ("filter_full") || + bool const filter_blocks = !keyValues.exists("filter_full") || (get(keyValues, "filter_full") == 0); - table_options.filter_policy.reset (rocksdb::NewBloomFilterPolicy (v, filter_blocks)); + table_options.filter_policy.reset( + rocksdb::NewBloomFilterPolicy(v, filter_blocks)); } - if (get_if_exists (keyValues, "open_files", m_options.max_open_files)) + if (get_if_exists(keyValues, "open_files", m_options.max_open_files)) fdRequired_ = m_options.max_open_files; - if (keyValues.exists ("file_size_mb")) + if (keyValues.exists("file_size_mb")) { - m_options.target_file_size_base = megabytes(1) * get(keyValues,"file_size_mb"); - m_options.max_bytes_for_level_base = 5 * m_options.target_file_size_base; + m_options.target_file_size_base = + megabytes(1) * get(keyValues, "file_size_mb"); + m_options.max_bytes_for_level_base = + 5 * m_options.target_file_size_base; m_options.write_buffer_size = 2 * m_options.target_file_size_base; } - get_if_exists (keyValues, "file_size_mult", m_options.target_file_size_multiplier); + get_if_exists( + keyValues, "file_size_mult", m_options.target_file_size_multiplier); - if (keyValues.exists ("bg_threads")) + if (keyValues.exists("bg_threads")) { - m_options.env->SetBackgroundThreads - (get(keyValues, "bg_threads"), rocksdb::Env::LOW); + m_options.env->SetBackgroundThreads( + get(keyValues, "bg_threads"), rocksdb::Env::LOW); } - if (keyValues.exists ("high_threads")) + if (keyValues.exists("high_threads")) { auto const highThreads = get(keyValues, "high_threads"); - m_options.env->SetBackgroundThreads (highThreads, rocksdb::Env::HIGH); + m_options.env->SetBackgroundThreads( + highThreads, rocksdb::Env::HIGH); // If we have high-priority threads, presumably we want to // use them for background flushes @@ -158,9 +160,9 @@ public: m_options.compression = rocksdb::kSnappyCompression; - get_if_exists (keyValues, "block_size", table_options.block_size); + get_if_exists(keyValues, "block_size", table_options.block_size); - if (keyValues.exists ("universal_compaction") && + if (keyValues.exists("universal_compaction") && (get(keyValues, "universal_compaction") != 0)) { m_options.compaction_style = rocksdb::kCompactionStyleUniversal; @@ -175,9 +177,10 @@ public: table_options, get(keyValues, "bbt_options"), &table_options); - if (! s.ok()) - Throw ( - std::string("Unable to set RocksDB bbt_options: ") + s.ToString()); + if (!s.ok()) + Throw( + std::string("Unable to set RocksDB bbt_options: ") + + s.ToString()); } m_options.table_factory.reset(NewBlockBasedTableFactory(table_options)); @@ -186,9 +189,10 @@ public: { auto const s = rocksdb::GetOptionsFromString( m_options, get(keyValues, "options"), &m_options); - if (! s.ok()) - Throw ( - std::string("Unable to set RocksDB options: ") + s.ToString()); + if (!s.ok()) + Throw( + std::string("Unable to set RocksDB options: ") + + s.ToString()); } std::string s1, s2; @@ -198,7 +202,7 @@ public: JLOG(m_journal.debug()) << "RocksDB CFOptions: " << s2; } - ~RocksDBBackend () override + ~RocksDBBackend() override { close(); } @@ -209,8 +213,7 @@ public: if (m_db) { assert(false); - JLOG(m_journal.error()) << - "database is already open"; + JLOG(m_journal.error()) << "database is already open"; return; } rocksdb::DB* db = nullptr; @@ -232,7 +235,7 @@ public: if (m_deletePath) { boost::filesystem::path dir = m_name; - boost::filesystem::remove_all (dir); + boost::filesystem::remove_all(dir); } } } @@ -246,27 +249,27 @@ public: //-------------------------------------------------------------------------- Status - fetch (void const* key, std::shared_ptr* pObject) override + fetch(void const* key, std::shared_ptr* pObject) override { assert(m_db); - pObject->reset (); + pObject->reset(); - Status status (ok); + Status status(ok); rocksdb::ReadOptions const options; - rocksdb::Slice const slice (static_cast (key), m_keyBytes); + rocksdb::Slice const slice(static_cast(key), m_keyBytes); std::string string; - rocksdb::Status getStatus = m_db->Get (options, slice, &string); + rocksdb::Status getStatus = m_db->Get(options, slice, &string); - if (getStatus.ok ()) + if (getStatus.ok()) { - DecodedBlob decoded (key, string.data (), string.size ()); + DecodedBlob decoded(key, string.data(), string.size()); - if (decoded.wasOk ()) + if (decoded.wasOk()) { - *pObject = decoded.createObject (); + *pObject = decoded.createObject(); } else { @@ -277,19 +280,19 @@ public: } else { - if (getStatus.IsCorruption ()) + if (getStatus.IsCorruption()) { status = dataCorrupt; } - else if (getStatus.IsNotFound ()) + else if (getStatus.IsNotFound()) { status = notFound; } else { - status = Status (customCode + getStatus.code()); + status = Status(customCode + getStatus.code()); - JLOG(m_journal.error()) << getStatus.ToString (); + JLOG(m_journal.error()) << getStatus.ToString(); } } @@ -303,20 +306,20 @@ public: } std::vector> - fetchBatch (std::size_t n, void const* const* keys) override + fetchBatch(std::size_t n, void const* const* keys) override { - Throw ("pure virtual called"); + Throw("pure virtual called"); return {}; } void - store (std::shared_ptr const& object) override + store(std::shared_ptr const& object) override { - m_batch.store (object); + m_batch.store(object); } void - storeBatch (Batch const& batch) override + storeBatch(Batch const& batch) override { assert(m_db); rocksdb::WriteBatch wb; @@ -325,65 +328,66 @@ public: for (auto const& e : batch) { - encoded.prepare (e); + encoded.prepare(e); - wb.Put ( - rocksdb::Slice (reinterpret_cast ( - encoded.getKey ()), m_keyBytes), - rocksdb::Slice (reinterpret_cast ( - encoded.getData ()), encoded.getSize ())); + wb.Put( + rocksdb::Slice( + reinterpret_cast(encoded.getKey()), + m_keyBytes), + rocksdb::Slice( + reinterpret_cast(encoded.getData()), + encoded.getSize())); } rocksdb::WriteOptions const options; - auto ret = m_db->Write (options, &wb); + auto ret = m_db->Write(options, &wb); - if (! ret.ok ()) - Throw ("storeBatch failed: " + ret.ToString()); + if (!ret.ok()) + Throw("storeBatch failed: " + ret.ToString()); } void - for_each (std::function )> f) override + for_each(std::function)> f) override { assert(m_db); rocksdb::ReadOptions const options; - std::unique_ptr it (m_db->NewIterator (options)); + std::unique_ptr it(m_db->NewIterator(options)); - for (it->SeekToFirst (); it->Valid (); it->Next ()) + for (it->SeekToFirst(); it->Valid(); it->Next()) { - if (it->key ().size () == m_keyBytes) + if (it->key().size() == m_keyBytes) { - DecodedBlob decoded (it->key ().data (), - it->value ().data (), - it->value ().size ()); + DecodedBlob decoded( + it->key().data(), it->value().data(), it->value().size()); - if (decoded.wasOk ()) + if (decoded.wasOk()) { - f (decoded.createObject ()); + f(decoded.createObject()); } else { // Uh oh, corrupted data! - JLOG(m_journal.fatal()) << - "Corrupt NodeObject #" << - from_hex_text(it->key ().data ()); + JLOG(m_journal.fatal()) + << "Corrupt NodeObject #" + << from_hex_text(it->key().data()); } } else { // VFALCO NOTE What does it mean to find an // incorrectly sized key? Corruption? - JLOG(m_journal.fatal()) << - "Bad key size = " << it->key ().size (); + JLOG(m_journal.fatal()) + << "Bad key size = " << it->key().size(); } } } int - getWriteLoad () override + getWriteLoad() override { - return m_batch.getWriteLoad (); + return m_batch.getWriteLoad(); } void @@ -395,9 +399,9 @@ public: //-------------------------------------------------------------------------- void - writeBatch (Batch const& batch) override + writeBatch(Batch const& batch) override { - storeBatch (batch); + storeBatch(batch); } void @@ -420,37 +424,37 @@ class RocksDBFactory : public Factory public: RocksDBEnv m_env; - RocksDBFactory () + RocksDBFactory() { Manager::instance().insert(*this); } - ~RocksDBFactory () override + ~RocksDBFactory() override { Manager::instance().erase(*this); } std::string - getName () const override + getName() const override { return "RocksDB"; } - std::unique_ptr - createInstance ( + std::unique_ptr + createInstance( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) override { - return std::make_unique ( + return std::make_unique( keyBytes, keyValues, scheduler, journal, &m_env); } }; static RocksDBFactory rocksDBFactory; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/BatchWriter.cpp b/src/ripple/nodestore/impl/BatchWriter.cpp index a81dcf736e..692032016c 100644 --- a/src/ripple/nodestore/impl/BatchWriter.cpp +++ b/src/ripple/nodestore/impl/BatchWriter.cpp @@ -22,102 +22,101 @@ namespace ripple { namespace NodeStore { -BatchWriter::BatchWriter (Callback& callback, Scheduler& scheduler) - : m_callback (callback) - , m_scheduler (scheduler) - , mWriteLoad (0) - , mWritePending (false) +BatchWriter::BatchWriter(Callback& callback, Scheduler& scheduler) + : m_callback(callback) + , m_scheduler(scheduler) + , mWriteLoad(0) + , mWritePending(false) { - mWriteSet.reserve (batchWritePreallocationSize); + mWriteSet.reserve(batchWritePreallocationSize); } -BatchWriter::~BatchWriter () +BatchWriter::~BatchWriter() { - waitForWriting (); + waitForWriting(); } void -BatchWriter::store (std::shared_ptr const& object) +BatchWriter::store(std::shared_ptr const& object) { - std::unique_lock sl (mWriteMutex); + std::unique_lock sl(mWriteMutex); // If the batch has reached its limit, we wait // until the batch writer is finished while (mWriteSet.size() >= batchWriteLimitSize) - mWriteCondition.wait (sl); + mWriteCondition.wait(sl); - mWriteSet.push_back (object); + mWriteSet.push_back(object); - if (! mWritePending) + if (!mWritePending) { mWritePending = true; - m_scheduler.scheduleTask (*this); + m_scheduler.scheduleTask(*this); } } int -BatchWriter::getWriteLoad () +BatchWriter::getWriteLoad() { - std::lock_guard sl (mWriteMutex); + std::lock_guard sl(mWriteMutex); - return std::max (mWriteLoad, static_cast (mWriteSet.size ())); + return std::max(mWriteLoad, static_cast(mWriteSet.size())); } void -BatchWriter::performScheduledTask () +BatchWriter::performScheduledTask() { - writeBatch (); + writeBatch(); } void -BatchWriter::writeBatch () +BatchWriter::writeBatch() { for (;;) { - std::vector< std::shared_ptr > set; + std::vector> set; - set.reserve (batchWritePreallocationSize); + set.reserve(batchWritePreallocationSize); { - std::lock_guard sl (mWriteMutex); + std::lock_guard sl(mWriteMutex); - mWriteSet.swap (set); - assert (mWriteSet.empty ()); - mWriteLoad = set.size (); + mWriteSet.swap(set); + assert(mWriteSet.empty()); + mWriteLoad = set.size(); - if (set.empty ()) + if (set.empty()) { mWritePending = false; - mWriteCondition.notify_all (); + mWriteCondition.notify_all(); // VFALCO NOTE Fix this function to not return from the middle return; } - } BatchWriteReport report; report.writeCount = set.size(); auto const before = std::chrono::steady_clock::now(); - m_callback.writeBatch (set); + m_callback.writeBatch(set); - report.elapsed = std::chrono::duration_cast - (std::chrono::steady_clock::now() - before); + report.elapsed = std::chrono::duration_cast( + std::chrono::steady_clock::now() - before); - m_scheduler.onBatchWrite (report); + m_scheduler.onBatchWrite(report); } } void -BatchWriter::waitForWriting () +BatchWriter::waitForWriting() { - std::unique_lock sl (mWriteMutex); + std::unique_lock sl(mWriteMutex); while (mWritePending) - mWriteCondition.wait (sl); + mWriteCondition.wait(sl); } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/BatchWriter.h b/src/ripple/nodestore/impl/BatchWriter.h index b341c8ff21..9ce4f12032 100644 --- a/src/ripple/nodestore/impl/BatchWriter.h +++ b/src/ripple/nodestore/impl/BatchWriter.h @@ -43,37 +43,44 @@ public: /** This callback does the actual writing. */ struct Callback { - virtual ~Callback () = default; + virtual ~Callback() = default; Callback() = default; Callback(Callback const&) = delete; - Callback& operator=(Callback const&) = delete; + Callback& + operator=(Callback const&) = delete; - virtual void writeBatch (Batch const& batch) = 0; + virtual void + writeBatch(Batch const& batch) = 0; }; /** Create a batch writer. */ - BatchWriter (Callback& callback, Scheduler& scheduler); + BatchWriter(Callback& callback, Scheduler& scheduler); /** Destroy a batch writer. Anything pending in the batch is written out before this returns. */ - ~BatchWriter (); + ~BatchWriter(); /** Store the object. This will add to the batch and initiate a scheduled task to write the batch out. */ - void store (std::shared_ptr const& object); + void + store(std::shared_ptr const& object); /** Get an estimate of the amount of writing I/O pending. */ - int getWriteLoad (); + int + getWriteLoad(); private: - void performScheduledTask () override; - void writeBatch (); - void waitForWriting (); + void + performScheduledTask() override; + void + writeBatch(); + void + waitForWriting(); private: using LockType = std::recursive_mutex; @@ -88,7 +95,7 @@ private: Batch mWriteSet; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/Database.cpp b/src/ripple/nodestore/impl/Database.cpp index 3f96d6974b..1f1ca0152e 100644 --- a/src/ripple/nodestore/impl/Database.cpp +++ b/src/ripple/nodestore/impl/Database.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include #include #include #include +#include #include namespace ripple { @@ -36,10 +36,8 @@ Database::Database( : Stoppable(name, parent.getRoot()) , j_(journal) , scheduler_(scheduler) - , earliestLedgerSeq_(get( - config, - "earliest_seq", - XRP_LEDGER_EARLIEST_SEQ)) + , earliestLedgerSeq_( + get(config, "earliest_seq", XRP_LEDGER_EARLIEST_SEQ)) { if (earliestLedgerSeq_ < 1) Throw("Invalid earliest_seq"); @@ -73,7 +71,7 @@ Database::waitReads() // even started. So when you reach generation N+2, // you know the request is done. std::uint64_t const wakeGen = readGen_ + 2; - while (! readShut_ && ! read_.empty() && (readGen_ < wakeGen)) + while (!readShut_ && !read_.empty() && (readGen_ < wakeGen)) readGenCondVar_.wait(lock); } @@ -96,7 +94,7 @@ Database::stopThreads() { { std::lock_guard lock(readLock_); - if (readShut_) // Only stop threads once. + if (readShut_) // Only stop threads once. return; readShut_ = true; @@ -109,9 +107,11 @@ Database::stopThreads() } void -Database::asyncFetch(uint256 const& hash, std::uint32_t seq, +Database::asyncFetch( + uint256 const& hash, + std::uint32_t seq, std::shared_ptr> const& pCache, - std::shared_ptr> const& nCache) + std::shared_ptr> const& nCache) { // Post a read std::lock_guard lock(readLock_); @@ -130,29 +130,26 @@ Database::fetchInternal(uint256 const& hash, std::shared_ptr backend) } catch (std::exception const& e) { - JLOG(j_.fatal()) << - "Exception, " << e.what(); + JLOG(j_.fatal()) << "Exception, " << e.what(); Rethrow(); } - switch(status) + switch (status) { - case ok: - ++fetchHitCount_; - if (nObj) - fetchSz_ += nObj->getData().size(); - break; - case notFound: - break; - case dataCorrupt: - // VFALCO TODO Deal with encountering corrupt data! - JLOG(j_.fatal()) << - "Corrupt NodeObject #" << hash; - break; - default: - JLOG(j_.warn()) << - "Unknown status=" << status; - break; + case ok: + ++fetchHitCount_; + if (nObj) + fetchSz_ += nObj->getData().size(); + break; + case notFound: + break; + case dataCorrupt: + // VFALCO TODO Deal with encountering corrupt data! + JLOG(j_.fatal()) << "Corrupt NodeObject #" << hash; + break; + default: + JLOG(j_.warn()) << "Unknown status=" << status; + break; } return nObj; } @@ -162,33 +159,34 @@ Database::importInternal(Backend& dstBackend, Database& srcDB) { Batch b; b.reserve(batchWritePreallocationSize); - srcDB.for_each( - [&](std::shared_ptr nObj) + srcDB.for_each([&](std::shared_ptr nObj) { + assert(nObj); + if (!nObj) // This should never happen + return; + + ++storeCount_; + storeSz_ += nObj->getData().size(); + + b.push_back(nObj); + if (b.size() >= batchWritePreallocationSize) { - assert(nObj); - if (! nObj) // This should never happen - return; - - ++storeCount_; - storeSz_ += nObj->getData().size(); - - b.push_back(nObj); - if (b.size() >= batchWritePreallocationSize) - { - dstBackend.storeBatch(b); - b.clear(); - b.reserve(batchWritePreallocationSize); - } - }); - if (! b.empty()) + dstBackend.storeBatch(b); + b.clear(); + b.reserve(batchWritePreallocationSize); + } + }); + if (!b.empty()) dstBackend.storeBatch(b); } // Perform a fetch and report the time it took std::shared_ptr -Database::doFetch(uint256 const& hash, std::uint32_t seq, +Database::doFetch( + uint256 const& hash, + std::uint32_t seq, TaggedCache& pCache, - KeyCache& nCache, bool isAsync) + KeyCache& nCache, + bool isAsync) { FetchReport report; report.isAsync = isAsync; @@ -199,17 +197,17 @@ Database::doFetch(uint256 const& hash, std::uint32_t seq, // See if the object already exists in the cache auto nObj = pCache.fetch(hash); - if (! nObj && ! nCache.touch_if_exists(hash)) + if (!nObj && !nCache.touch_if_exists(hash)) { // Try the database(s) report.wentToDisk = true; nObj = fetchFrom(hash, seq); ++fetchTotalCount_; - if (! nObj) + if (!nObj) { // Just in case a write occurred nObj = pCache.fetch(hash); - if (! nObj) + if (!nObj) // We give up nCache.insert(hash); } @@ -219,13 +217,11 @@ Database::doFetch(uint256 const& hash, std::uint32_t seq, pCache.canonicalize_replace_client(hash, nObj); // Since this was a 'hard' fetch, we will log it. - JLOG(j_.trace()) << - "HOS: " << hash << " fetch: in db"; + JLOG(j_.trace()) << "HOS: " << hash << " fetch: in db"; } } report.wasFound = static_cast(nObj); - report.elapsed = duration_cast( - steady_clock::now() - before); + report.elapsed = duration_cast(steady_clock::now() - before); scheduler_.onFetch(report); return nObj; } @@ -239,22 +235,18 @@ Database::storeLedger( std::shared_ptr next) { assert(static_cast(dstPCache) == static_cast(dstNCache)); - if (srcLedger.info().hash.isZero() || - srcLedger.info().accountHash.isZero()) + if (srcLedger.info().hash.isZero() || srcLedger.info().accountHash.isZero()) { assert(false); - JLOG(j_.error()) << - "source ledger seq " << srcLedger.info().seq << - " is invalid"; + JLOG(j_.error()) << "source ledger seq " << srcLedger.info().seq + << " is invalid"; return false; } - auto& srcDB = const_cast( - srcLedger.stateMap().family().db()); + auto& srcDB = const_cast(srcLedger.stateMap().family().db()); if (&srcDB == this) { assert(false); - JLOG(j_.error()) << - "source and destination databases are the same"; + JLOG(j_.error()) << "source and destination databases are the same"; return false; } @@ -275,10 +267,9 @@ Database::storeLedger( batch.reserve(batchWritePreallocationSize); }; bool error = false; - auto visit = [&](SHAMapAbstractNode& node) - { + auto visit = [&](SHAMapAbstractNode& node) { if (auto nObj = srcDB.fetch( - node.getNodeHash().as_uint256(), srcLedger.info().seq)) + node.getNodeHash().as_uint256(), srcLedger.info().seq)) { batch.emplace_back(std::move(nObj)); if (batch.size() < batchWritePreallocationSize) @@ -299,8 +290,8 @@ Database::storeLedger( Serializer s(sizeof(std::uint32_t) + sizeof(LedgerInfo)); s.add32(HashPrefix::ledgerMaster); addRaw(srcLedger.info(), s); - auto nObj = NodeObject::createObject(hotLEDGER, - std::move(s.modData()), srcLedger.info().hash); + auto nObj = NodeObject::createObject( + hotLEDGER, std::move(s.modData()), srcLedger.info().hash); batch.emplace_back(std::move(nObj)); } @@ -309,16 +300,15 @@ Database::storeLedger( { if (!srcLedger.stateMap().isValid()) { - JLOG(j_.error()) << - "source ledger seq " << srcLedger.info().seq << - " state map invalid"; + JLOG(j_.error()) << "source ledger seq " << srcLedger.info().seq + << " state map invalid"; return false; } if (next && next->info().parentHash == srcLedger.info().hash) { auto have = next->stateMap().snapShot(false); - srcLedger.stateMap().snapShot( - false)->visitDifferences(&(*have), visit); + srcLedger.stateMap().snapShot(false)->visitDifferences( + &(*have), visit); } else srcLedger.stateMap().snapShot(false)->visitNodes(visit); @@ -331,9 +321,8 @@ Database::storeLedger( { if (!srcLedger.txMap().isValid()) { - JLOG(j_.error()) << - "source ledger seq " << srcLedger.info().seq << - " transaction map invalid"; + JLOG(j_.error()) << "source ledger seq " << srcLedger.info().seq + << " transaction map invalid"; return false; } srcLedger.txMap().snapShot(false)->visitNodes(visit); @@ -359,7 +348,7 @@ Database::threadEntry() std::shared_ptr> lastNcache; { std::unique_lock lock(readLock_); - while (! readShut_ && read_.empty()) + while (!readShut_ && read_.empty()) { // All work is done readGenCondVar_.notify_all(); @@ -391,5 +380,5 @@ Database::threadEntry() } } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/DatabaseNodeImp.cpp b/src/ripple/nodestore/impl/DatabaseNodeImp.cpp index e0879a617c..7054471913 100644 --- a/src/ripple/nodestore/impl/DatabaseNodeImp.cpp +++ b/src/ripple/nodestore/impl/DatabaseNodeImp.cpp @@ -17,16 +17,19 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { namespace NodeStore { void -DatabaseNodeImp::store(NodeObjectType type, Blob&& data, - uint256 const& hash, std::uint32_t seq) +DatabaseNodeImp::store( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + std::uint32_t seq) { auto nObj = NodeObject::createObject(type, std::move(data), hash); pCache_->canonicalize_replace_cache(hash, nObj); @@ -36,8 +39,10 @@ DatabaseNodeImp::store(NodeObjectType type, Blob&& data, } bool -DatabaseNodeImp::asyncFetch(uint256 const& hash, - std::uint32_t seq, std::shared_ptr& object) +DatabaseNodeImp::asyncFetch( + uint256 const& hash, + std::uint32_t seq, + std::shared_ptr& object) { // See if the object is in cache object = pCache_->fetch(hash); @@ -64,5 +69,5 @@ DatabaseNodeImp::sweep() nCache_->sweep(); } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/DatabaseNodeImp.h b/src/ripple/nodestore/impl/DatabaseNodeImp.h index 7543434e22..2e9525e968 100644 --- a/src/ripple/nodestore/impl/DatabaseNodeImp.h +++ b/src/ripple/nodestore/impl/DatabaseNodeImp.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED #define RIPPLE_NODESTORE_DATABASENODEIMP_H_INCLUDED -#include #include +#include namespace ripple { namespace NodeStore { @@ -31,7 +31,8 @@ class DatabaseNodeImp : public Database public: DatabaseNodeImp() = delete; DatabaseNodeImp(DatabaseNodeImp const&) = delete; - DatabaseNodeImp& operator=(DatabaseNodeImp const&) = delete; + DatabaseNodeImp& + operator=(DatabaseNodeImp const&) = delete; DatabaseNodeImp( std::string const& name, @@ -43,9 +44,16 @@ public: beast::Journal j) : Database(name, parent, scheduler, readThreads, config, j) , pCache_(std::make_shared>( - name, cacheTargetSize, cacheTargetAge, stopwatch(), j)) + name, + cacheTargetSize, + cacheTargetAge, + stopwatch(), + j)) , nCache_(std::make_shared>( - name, stopwatch(), cacheTargetSize, cacheTargetAge)) + name, + stopwatch(), + cacheTargetSize, + cacheTargetAge)) , backend_(std::move(backend)) { assert(backend_); @@ -77,8 +85,11 @@ public: } void - store(NodeObjectType type, Blob&& data, - uint256 const& hash, std::uint32_t seq) override; + store( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + std::uint32_t seq) override; std::shared_ptr fetch(uint256 const& hash, std::uint32_t seq) override @@ -87,7 +98,9 @@ public: } bool - asyncFetch(uint256 const& hash, std::uint32_t seq, + asyncFetch( + uint256 const& hash, + std::uint32_t seq, std::shared_ptr& object) override; bool @@ -107,7 +120,10 @@ public: } float - getCacheHitRate() override {return pCache_->getHitRate();} + getCacheHitRate() override + { + return pCache_->getHitRate(); + } void tune(int size, std::chrono::seconds age) override; @@ -138,7 +154,7 @@ private: } }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/DatabaseRotatingImp.cpp b/src/ripple/nodestore/impl/DatabaseRotatingImp.cpp index 00a9365cc7..f09229efde 100644 --- a/src/ripple/nodestore/impl/DatabaseRotatingImp.cpp +++ b/src/ripple/nodestore/impl/DatabaseRotatingImp.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { @@ -35,9 +35,16 @@ DatabaseRotatingImp::DatabaseRotatingImp( beast::Journal j) : DatabaseRotating(name, parent, scheduler, readThreads, config, j) , pCache_(std::make_shared>( - name, cacheTargetSize, cacheTargetAge, stopwatch(), j)) + name, + cacheTargetSize, + cacheTargetAge, + stopwatch(), + j)) , nCache_(std::make_shared>( - name, stopwatch(), cacheTargetSize, cacheTargetAge)) + name, + stopwatch(), + cacheTargetSize, + cacheTargetAge)) , writableBackend_(std::move(writableBackend)) , archiveBackend_(std::move(archiveBackend)) { @@ -53,15 +60,18 @@ DatabaseRotatingImp::rotateBackends( std::shared_ptr newBackend, std::lock_guard const&) { - auto oldBackend {std::move(archiveBackend_)}; + auto oldBackend{std::move(archiveBackend_)}; archiveBackend_ = std::move(writableBackend_); writableBackend_ = std::move(newBackend); return oldBackend; } void -DatabaseRotatingImp::store(NodeObjectType type, Blob&& data, - uint256 const& hash, std::uint32_t seq) +DatabaseRotatingImp::store( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + std::uint32_t seq) { auto nObj = NodeObject::createObject(type, std::move(data), hash); pCache_->canonicalize_replace_cache(hash, nObj); @@ -71,8 +81,10 @@ DatabaseRotatingImp::store(NodeObjectType type, Blob&& data, } bool -DatabaseRotatingImp::asyncFetch(uint256 const& hash, - std::uint32_t seq, std::shared_ptr& object) +DatabaseRotatingImp::asyncFetch( + uint256 const& hash, + std::uint32_t seq, + std::shared_ptr& object) { // See if the object is in cache object = pCache_->fetch(hash); @@ -104,7 +116,7 @@ DatabaseRotatingImp::fetchFrom(uint256 const& hash, std::uint32_t seq) { Backends b = getBackends(); auto nObj = fetchInternal(hash, b.writableBackend); - if (! nObj) + if (!nObj) { nObj = fetchInternal(hash, b.archiveBackend); if (nObj) @@ -116,5 +128,5 @@ DatabaseRotatingImp::fetchFrom(uint256 const& hash, std::uint32_t seq) return nObj; } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/DatabaseRotatingImp.h b/src/ripple/nodestore/impl/DatabaseRotatingImp.h index 4cdf6396f8..8a29d117a2 100644 --- a/src/ripple/nodestore/impl/DatabaseRotatingImp.h +++ b/src/ripple/nodestore/impl/DatabaseRotatingImp.h @@ -30,7 +30,8 @@ class DatabaseRotatingImp : public DatabaseRotating public: DatabaseRotatingImp() = delete; DatabaseRotatingImp(DatabaseRotatingImp const&) = delete; - DatabaseRotatingImp& operator=(DatabaseRotatingImp const&) = delete; + DatabaseRotatingImp& + operator=(DatabaseRotatingImp const&) = delete; DatabaseRotatingImp( std::string const& name, @@ -51,7 +52,7 @@ public: std::shared_ptr const& getWritableBackend() const override { - std::lock_guard lock (rotateMutex_); + std::lock_guard lock(rotateMutex_); return writableBackend_; } @@ -60,28 +61,36 @@ public: std::shared_ptr newBackend, std::lock_guard const&) override; - std::mutex& peekMutex() const override + std::mutex& + peekMutex() const override { return rotateMutex_; } - std::string getName() const override + std::string + getName() const override { return getWritableBackend()->getName(); } - std::int32_t getWriteLoad() const override + std::int32_t + getWriteLoad() const override { return getWritableBackend()->getWriteLoad(); } - void import (Database& source) override + void + import(Database& source) override { - importInternal (*getWritableBackend(), source); + importInternal(*getWritableBackend(), source); } - void store(NodeObjectType type, Blob&& data, - uint256 const& hash, std::uint32_t seq) override; + void + store( + NodeObjectType type, + Blob&& data, + uint256 const& hash, + std::uint32_t seq) override; std::shared_ptr fetch(uint256 const& hash, std::uint32_t seq) override @@ -90,7 +99,9 @@ public: } bool - asyncFetch(uint256 const& hash, std::uint32_t seq, + asyncFetch( + uint256 const& hash, + std::uint32_t seq, std::shared_ptr& object) override; bool @@ -110,7 +121,10 @@ public: } float - getCacheHitRate() override {return pCache_->getHitRate();} + getCacheHitRate() override + { + return pCache_->getHitRate(); + } void tune(int size, std::chrono::seconds age) override; @@ -119,7 +133,10 @@ public: sweep() override; TaggedCache const& - getPositiveCache() override {return *pCache_;} + getPositiveCache() override + { + return *pCache_; + } private: // Positive cache @@ -132,22 +149,24 @@ private: std::shared_ptr archiveBackend_; mutable std::mutex rotateMutex_; - struct Backends { + struct Backends + { std::shared_ptr const& writableBackend; std::shared_ptr const& archiveBackend; }; - Backends getBackends() const + Backends + getBackends() const { - std::lock_guard lock (rotateMutex_); - return Backends {writableBackend_, archiveBackend_}; + std::lock_guard lock(rotateMutex_); + return Backends{writableBackend_, archiveBackend_}; } - std::shared_ptr fetchFrom( - uint256 const& hash, std::uint32_t seq) override; + std::shared_ptr + fetchFrom(uint256 const& hash, std::uint32_t seq) override; void - for_each(std::function )> f) override + for_each(std::function)> f) override { Backends b = getBackends(); b.archiveBackend->for_each(f); @@ -155,7 +174,7 @@ private: } }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/DatabaseShardImp.cpp b/src/ripple/nodestore/impl/DatabaseShardImp.cpp index fecd7febe9..1cf00b3ea9 100644 --- a/src/ripple/nodestore/impl/DatabaseShardImp.cpp +++ b/src/ripple/nodestore/impl/DatabaseShardImp.cpp @@ -17,7 +17,6 @@ */ //============================================================================== -#include #include #include #include @@ -26,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -43,12 +43,12 @@ DatabaseShardImp::DatabaseShardImp( int readThreads, beast::Journal j) : DatabaseShard( - name, - parent, - scheduler, - readThreads, - app.config().section(ConfigSection::shardDatabase()), - j) + name, + parent, + scheduler, + readThreads, + app.config().section(ConfigSection::shardDatabase()), + j) , app_(app) , parent_(parent) , taskQueue_(std::make_unique(*this)) @@ -104,43 +104,35 @@ DatabaseShardImp::init() // Check shard directory name is numeric auto dirName = d.path().stem().string(); - if (!std::all_of( - dirName.begin(), - dirName.end(), - [](auto c) { - return ::isdigit(static_cast(c)); - })) + if (!std::all_of(dirName.begin(), dirName.end(), [](auto c) { + return ::isdigit(static_cast(c)); + })) { continue; } - auto const shardIndex {std::stoul(dirName)}; + auto const shardIndex{std::stoul(dirName)}; if (shardIndex < earliestShardIndex()) { - JLOG(j_.error()) << - "shard " << shardIndex << - " comes before earliest shard index " << - earliestShardIndex(); + JLOG(j_.error()) << "shard " << shardIndex + << " comes before earliest shard index " + << earliestShardIndex(); return false; } - auto const shardDir {dir_ / std::to_string(shardIndex)}; + auto const shardDir{dir_ / std::to_string(shardIndex)}; // Check if a previous import failed if (is_regular_file(shardDir / importMarker_)) { - JLOG(j_.warn()) << - "shard " << shardIndex << - " previously failed import, removing"; + JLOG(j_.warn()) << "shard " << shardIndex + << " previously failed import, removing"; remove_all(shardDir); continue; } - auto shard {std::make_unique( - app_, - *this, - shardIndex, - j_)}; + auto shard{ + std::make_unique(app_, *this, shardIndex, j_)}; if (!shard->open(scheduler_, *ctx_)) { if (!shard->isLegacy()) @@ -148,9 +140,8 @@ DatabaseShardImp::init() // Remove legacy shard shard->removeOnDestroy(); - JLOG(j_.warn()) << - "shard " << shardIndex << - " removed, legacy shard"; + JLOG(j_.warn()) + << "shard " << shardIndex << " removed, legacy shard"; continue; } @@ -162,7 +153,7 @@ DatabaseShardImp::init() } else if (shard->isBackendComplete()) { - auto const result {shards_.emplace( + auto const result{shards_.emplace( shardIndex, ShardInfo(std::move(shard), ShardInfo::State::none))}; finalizeShard(result.first->second, true, lock); @@ -171,8 +162,8 @@ DatabaseShardImp::init() { if (acquireIndex_ != 0) { - JLOG(j_.error()) << - "more than one shard being acquired"; + JLOG(j_.error()) + << "more than one shard being acquired"; return false; } @@ -185,8 +176,8 @@ DatabaseShardImp::init() } catch (std::exception const& e) { - JLOG(j_.error()) << - "exception " << e.what() << " in function " << __func__; + JLOG(j_.error()) + << "exception " << e.what() << " in function " << __func__; } updateStatus(lock); @@ -209,7 +200,7 @@ DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq) if (acquireIndex_ != 0) { - if (auto it {shards_.find(acquireIndex_)}; it != shards_.end()) + if (auto it{shards_.find(acquireIndex_)}; it != shards_.end()) return it->second.shard->prepare(); assert(false); return boost::none; @@ -245,11 +236,11 @@ DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq) return boost::none; } - auto shard {std::make_unique(app_, *this, *shardIndex, j_)}; + auto shard{std::make_unique(app_, *this, *shardIndex, j_)}; if (!shard->open(scheduler_, *ctx_)) return boost::none; - auto const seq {shard->prepare()}; + auto const seq{shard->prepare()}; { std::lock_guard lock(mutex_); shards_.emplace( @@ -263,8 +254,7 @@ DatabaseShardImp::prepareLedger(std::uint32_t validLedgerSeq) bool DatabaseShardImp::prepareShard(std::uint32_t shardIndex) { - auto fail = [j = j_, shardIndex](std::string const& msg) - { + auto fail = [j = j_, shardIndex](std::string const& msg) { JLOG(j.error()) << "shard " << shardIndex << " " << msg; return false; }; @@ -276,14 +266,14 @@ DatabaseShardImp::prepareShard(std::uint32_t shardIndex) if (shardIndex < earliestShardIndex()) { - return fail("comes before earliest shard index " + + return fail( + "comes before earliest shard index " + std::to_string(earliestShardIndex())); } // If we are synced to the network, check if the shard index // is greater or equal to the current shard. - auto seqCheck = [&](std::uint32_t seq) - { + auto seqCheck = [&](std::uint32_t seq) { // seq will be greater than zero if valid if (seq > earliestLedgerSeq() && shardIndex >= seqToShardIndex(seq)) return fail("has an invalid index"); @@ -297,9 +287,8 @@ DatabaseShardImp::prepareShard(std::uint32_t shardIndex) if (shards_.find(shardIndex) != shards_.end()) { - JLOG(j_.debug()) << - "shard " << shardIndex << - " is already stored or queued for import"; + JLOG(j_.debug()) << "shard " << shardIndex + << " is already stored or queued for import"; return false; } @@ -319,7 +308,7 @@ DatabaseShardImp::removePreShard(std::uint32_t shardIndex) std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(shardIndex)}; + if (auto const it{shards_.find(shardIndex)}; it != shards_.end() && it->second.state == ShardInfo::State::import) { shards_.erase(it); @@ -355,28 +344,26 @@ DatabaseShardImp::importShard( { if (!is_directory(srcDir) || is_empty(srcDir)) { - JLOG(j_.error()) << - "invalid source directory " << srcDir.string(); + JLOG(j_.error()) << "invalid source directory " << srcDir.string(); return false; } } catch (std::exception const& e) { - JLOG(j_.error()) << - "exception " << e.what() << " in function " << __func__; + JLOG(j_.error()) << "exception " << e.what() << " in function " + << __func__; return false; } - auto renameDir = [&](path const& src, path const& dst) - { + auto renameDir = [&](path const& src, path const& dst) { try { rename(src, dst); } catch (std::exception const& e) { - JLOG(j_.error()) << - "exception " << e.what() << " in function " << __func__; + JLOG(j_.error()) + << "exception " << e.what() << " in function " << __func__; return false; } return true; @@ -388,13 +375,10 @@ DatabaseShardImp::importShard( assert(init_); // Check shard is prepared - if (auto const it {shards_.find(shardIndex)}; - it == shards_.end() || - it->second.shard || - it->second.state != ShardInfo::State::import) + if (auto const it{shards_.find(shardIndex)}; it == shards_.end() || + it->second.shard || it->second.state != ShardInfo::State::import) { - JLOG(j_.error()) << - "shard " << shardIndex << " failed to import"; + JLOG(j_.error()) << "shard " << shardIndex << " failed to import"; return false; } @@ -406,24 +390,21 @@ DatabaseShardImp::importShard( return false; // Create the new shard - auto shard {std::make_unique(app_, *this, shardIndex, j_)}; + auto shard{std::make_unique(app_, *this, shardIndex, j_)}; if (!shard->open(scheduler_, *ctx_) || !shard->isBackendComplete()) { - JLOG(j_.error()) << - "shard " << shardIndex << " failed to import"; + JLOG(j_.error()) << "shard " << shardIndex << " failed to import"; shard.reset(); renameDir(dstDir, srcDir); return false; } std::lock_guard lock(mutex_); - auto const it {shards_.find(shardIndex)}; - if (it == shards_.end() || - it->second.shard || + auto const it{shards_.find(shardIndex)}; + if (it == shards_.end() || it->second.shard || it->second.state != ShardInfo::State::import) { - JLOG(j_.error()) << - "shard " << shardIndex << " failed to import"; + JLOG(j_.error()) << "shard " << shardIndex << " failed to import"; return false; } @@ -435,14 +416,14 @@ DatabaseShardImp::importShard( std::shared_ptr DatabaseShardImp::fetchLedger(uint256 const& hash, std::uint32_t seq) { - auto const shardIndex {seqToShardIndex(seq)}; + auto const shardIndex{seqToShardIndex(seq)}; { ShardInfo shardInfo; { std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(shardIndex)}; it != shards_.end()) + if (auto const it{shards_.find(shardIndex)}; it != shards_.end()) shardInfo = it->second; else return {}; @@ -452,58 +433,60 @@ DatabaseShardImp::fetchLedger(uint256 const& hash, std::uint32_t seq) // or in the shard being acquired switch (shardInfo.state) { - case ShardInfo::State::final: - break; - case ShardInfo::State::acquire: - if (shardInfo.shard->containsLedger(seq)) + case ShardInfo::State::final: break; - [[fallthrough]]; - default: - return {}; + case ShardInfo::State::acquire: + if (shardInfo.shard->containsLedger(seq)) + break; + [[fallthrough]]; + default: + return {}; } } - auto nObj {fetch(hash, seq)}; + auto nObj{fetch(hash, seq)}; if (!nObj) return {}; - auto fail = [this, seq](std::string const& msg)-> std::shared_ptr - { + auto fail = [this, seq](std::string const& msg) -> std::shared_ptr { JLOG(j_.error()) << "shard " << seqToShardIndex(seq) << " " << msg; return {}; }; - auto ledger {std::make_shared( + auto ledger{std::make_shared( InboundLedger::deserializeHeader(makeSlice(nObj->getData()), true), app_.config(), *app_.shardFamily())}; if (ledger->info().seq != seq) { - return fail("encountered invalid ledger sequence " + - std::to_string(seq)); + return fail( + "encountered invalid ledger sequence " + std::to_string(seq)); } if (ledger->info().hash != hash) { - return fail("encountered invalid ledger hash " + - to_string(hash) + " on sequence " + std::to_string(seq)); + return fail( + "encountered invalid ledger hash " + to_string(hash) + + " on sequence " + std::to_string(seq)); } ledger->setFull(); if (!ledger->stateMap().fetchRoot( - SHAMapHash {ledger->info().accountHash}, nullptr)) + SHAMapHash{ledger->info().accountHash}, nullptr)) { - return fail("is missing root STATE node on hash " + - to_string(hash) + " on sequence " + std::to_string(seq)); + return fail( + "is missing root STATE node on hash " + to_string(hash) + + " on sequence " + std::to_string(seq)); } if (ledger->info().txHash.isNonZero()) { if (!ledger->txMap().fetchRoot( - SHAMapHash {ledger->info().txHash}, nullptr)) + SHAMapHash{ledger->info().txHash}, nullptr)) { - return fail("is missing root TXN node on hash " + - to_string(hash) + " on sequence " + std::to_string(seq)); + return fail( + "is missing root TXN node on hash " + to_string(hash) + + " on sequence " + std::to_string(seq)); } } return ledger; @@ -514,32 +497,31 @@ DatabaseShardImp::setStored(std::shared_ptr const& ledger) { if (ledger->info().hash.isZero()) { - JLOG(j_.error()) << - "zero ledger hash for ledger sequence " << ledger->info().seq; + JLOG(j_.error()) << "zero ledger hash for ledger sequence " + << ledger->info().seq; return; } if (ledger->info().accountHash.isZero()) { - JLOG(j_.error()) << - "zero account hash for ledger sequence " << ledger->info().seq; + JLOG(j_.error()) << "zero account hash for ledger sequence " + << ledger->info().seq; return; } if (ledger->stateMap().getHash().isNonZero() && !ledger->stateMap().isValid()) { - JLOG(j_.error()) << - "invalid state map for ledger sequence " << ledger->info().seq; + JLOG(j_.error()) << "invalid state map for ledger sequence " + << ledger->info().seq; return; } if (ledger->info().txHash.isNonZero() && !ledger->txMap().isValid()) { - JLOG(j_.error()) << - "invalid transaction map for ledger sequence " << - ledger->info().seq; + JLOG(j_.error()) << "invalid transaction map for ledger sequence " + << ledger->info().seq; return; } - auto const shardIndex {seqToShardIndex(ledger->info().seq)}; + auto const shardIndex{seqToShardIndex(ledger->info().seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); @@ -547,17 +529,17 @@ DatabaseShardImp::setStored(std::shared_ptr const& ledger) if (shardIndex != acquireIndex_) { - JLOG(j_.trace()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.trace()) + << "shard " << shardIndex << " is not being acquired"; return; } - if (auto const it {shards_.find(shardIndex)}; it != shards_.end()) + if (auto const it{shards_.find(shardIndex)}; it != shards_.end()) shard = it->second.shard; else { - JLOG(j_.error()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.error()) + << "shard " << shardIndex << " is not being acquired"; return; } } @@ -595,7 +577,7 @@ DatabaseShardImp::validate() for (auto const& e : shards) { - if (auto shard {e.lock()}; shard) + if (auto shard{e.lock()}; shard) shard->finalize(true); } @@ -637,27 +619,28 @@ DatabaseShardImp::import(Database& source) std::uint32_t earliestIndex; std::uint32_t latestIndex; { - auto loadLedger = [&](bool ascendSort = true) -> - boost::optional - { + auto loadLedger = [&](bool ascendSort = + true) -> boost::optional { std::shared_ptr ledger; std::uint32_t seq; std::tie(ledger, seq, std::ignore) = loadLedgerHelper( - "WHERE LedgerSeq >= " + std::to_string(earliestLedgerSeq()) + - " order by LedgerSeq " + (ascendSort ? "asc" : "desc") + - " limit 1", app_, false); + "WHERE LedgerSeq >= " + + std::to_string(earliestLedgerSeq()) + + " order by LedgerSeq " + (ascendSort ? "asc" : "desc") + + " limit 1", + app_, + false); if (!ledger || seq == 0) { - JLOG(j_.error()) << - "no suitable ledgers were found in" - " the SQLite database to import"; + JLOG(j_.error()) << "no suitable ledgers were found in" + " the SQLite database to import"; return boost::none; } return seq; }; // Find earliest ledger sequence stored - auto seq {loadLedger()}; + auto seq{loadLedger()}; if (!seq) return; earliestIndex = seqToShardIndex(*seq); @@ -678,16 +661,16 @@ DatabaseShardImp::import(Database& source) if (latestIndex < earliestIndex) { - JLOG(j_.error()) << - "no suitable ledgers were found in" - " the SQLite database to import"; + JLOG(j_.error()) << "no suitable ledgers were found in" + " the SQLite database to import"; return; } } // Import the shards for (std::uint32_t shardIndex = earliestIndex; - shardIndex <= latestIndex; ++shardIndex) + shardIndex <= latestIndex; + ++shardIndex) { if (fileSz_ + avgShardFileSz_ > maxFileSz_) { @@ -706,30 +689,29 @@ DatabaseShardImp::import(Database& source) if (shardIndex == acquireIndex_ || shards_.find(shardIndex) != shards_.end()) { - JLOG(j_.debug()) << - "shard " << shardIndex << " already exists"; + JLOG(j_.debug()) << "shard " << shardIndex << " already exists"; continue; } // Verify SQLite ledgers are in the node store { - auto const firstSeq {firstLedgerSeq(shardIndex)}; - auto const lastSeq { + auto const firstSeq{firstLedgerSeq(shardIndex)}; + auto const lastSeq{ std::max(firstSeq, lastLedgerSeq(shardIndex))}; - auto const numLedgers {shardIndex == earliestShardIndex() - ? lastSeq - firstSeq + 1 : ledgersPerShard_}; - auto ledgerHashes {getHashesByIndex(firstSeq, lastSeq, app_)}; + auto const numLedgers{ + shardIndex == earliestShardIndex() ? lastSeq - firstSeq + 1 + : ledgersPerShard_}; + auto ledgerHashes{getHashesByIndex(firstSeq, lastSeq, app_)}; if (ledgerHashes.size() != numLedgers) continue; - bool valid {true}; + bool valid{true}; for (std::uint32_t n = firstSeq; n <= lastSeq; n += 256) { if (!source.fetch(ledgerHashes[n].first, n)) { - JLOG(j_.warn()) << - "SQLite ledger sequence " << n << - " mismatches node store"; + JLOG(j_.warn()) << "SQLite ledger sequence " << n + << " mismatches node store"; valid = false; break; } @@ -740,20 +722,19 @@ DatabaseShardImp::import(Database& source) // Create the new shard app_.shardFamily()->reset(); - auto shard {std::make_unique(app_, *this, shardIndex, j_)}; + auto shard{std::make_unique(app_, *this, shardIndex, j_)}; if (!shard->open(scheduler_, *ctx_)) continue; // Create a marker file to signify an import in progress - auto const shardDir {dir_ / std::to_string(shardIndex)}; - auto const markerFile {shardDir / importMarker_}; + auto const shardDir{dir_ / std::to_string(shardIndex)}; + auto const markerFile{shardDir / importMarker_}; { - std::ofstream ofs {markerFile.string()}; + std::ofstream ofs{markerFile.string()}; if (!ofs.is_open()) { - JLOG(j_.error()) << - "shard " << shardIndex << - " failed to create temp marker file"; + JLOG(j_.error()) << "shard " << shardIndex + << " failed to create temp marker file"; shard->removeOnDestroy(); continue; } @@ -766,16 +747,16 @@ DatabaseShardImp::import(Database& source) while (auto seq = shard->prepare()) { - auto ledger {loadByIndex(*seq, app_, false)}; + auto ledger{loadByIndex(*seq, app_, false)}; if (!ledger || ledger->info().seq != seq) break; if (!Database::storeLedger( - *ledger, - shard->getBackend(), - nullptr, - nullptr, - recentStored)) + *ledger, + shard->getBackend(), + nullptr, + nullptr, + recentStored)) { break; } @@ -798,10 +779,8 @@ DatabaseShardImp::import(Database& source) s.add32(firstLedgerSeq(shardIndex)); s.add32(lastLedgerSeq(shardIndex)); s.add256(*lastLedgerHash); - auto nObj {NodeObject::createObject( - hotUNKNOWN, - std::move(s.modData()), - Shard::finalKey)}; + auto nObj{NodeObject::createObject( + hotUNKNOWN, std::move(s.modData()), Shard::finalKey)}; try { @@ -811,27 +790,25 @@ DatabaseShardImp::import(Database& source) // marker file is no longer required remove_all(markerFile); - JLOG(j_.debug()) << - "shard " << shardIndex << - " was successfully imported"; + JLOG(j_.debug()) << "shard " << shardIndex + << " was successfully imported"; - auto const result {shards_.emplace( + auto const result{shards_.emplace( shardIndex, ShardInfo(std::move(shard), ShardInfo::State::none))}; finalizeShard(result.first->second, true, lock); } catch (std::exception const& e) { - JLOG(j_.error()) << - "exception " << e.what() << - " in function " << __func__; + JLOG(j_.error()) << "exception " << e.what() + << " in function " << __func__; shard->removeOnDestroy(); } } else { - JLOG(j_.error()) << - "shard " << shardIndex << " failed to import"; + JLOG(j_.error()) + << "shard " << shardIndex << " failed to import"; shard->removeOnDestroy(); } } @@ -850,7 +827,7 @@ DatabaseShardImp::getWriteLoad() const std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(acquireIndex_)}; it != shards_.end()) + if (auto const it{shards_.find(acquireIndex_)}; it != shards_.end()) shard = it->second.shard; else return 0; @@ -866,7 +843,7 @@ DatabaseShardImp::store( uint256 const& hash, std::uint32_t seq) { - auto const shardIndex {seqToShardIndex(seq)}; + auto const shardIndex{seqToShardIndex(seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); @@ -874,23 +851,23 @@ DatabaseShardImp::store( if (shardIndex != acquireIndex_) { - JLOG(j_.trace()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.trace()) + << "shard " << shardIndex << " is not being acquired"; return; } - if (auto const it {shards_.find(shardIndex)}; it != shards_.end()) + if (auto const it{shards_.find(shardIndex)}; it != shards_.end()) shard = it->second.shard; else { - JLOG(j_.error()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.error()) + << "shard " << shardIndex << " is not being acquired"; return; } } auto [backend, pCache, nCache] = shard->getBackendAll(); - auto nObj {NodeObject::createObject(type, std::move(data), hash)}; + auto nObj{NodeObject::createObject(type, std::move(data), hash)}; pCache->canonicalize_replace_cache(hash, nObj); backend->store(nObj); @@ -902,7 +879,7 @@ DatabaseShardImp::store( std::shared_ptr DatabaseShardImp::fetch(uint256 const& hash, std::uint32_t seq) { - auto cache {getCache(seq)}; + auto cache{getCache(seq)}; if (cache.first) return doFetch(hash, seq, *cache.first, *cache.second, false); return {}; @@ -914,7 +891,7 @@ DatabaseShardImp::asyncFetch( std::uint32_t seq, std::shared_ptr& object) { - auto cache {getCache(seq)}; + auto cache{getCache(seq)}; if (cache.first) { // See if the object is in cache @@ -930,8 +907,8 @@ DatabaseShardImp::asyncFetch( bool DatabaseShardImp::storeLedger(std::shared_ptr const& srcLedger) { - auto const seq {srcLedger->info().seq}; - auto const shardIndex {seqToShardIndex(seq)}; + auto const seq{srcLedger->info().seq}; + auto const shardIndex{seqToShardIndex(seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); @@ -939,36 +916,31 @@ DatabaseShardImp::storeLedger(std::shared_ptr const& srcLedger) if (shardIndex != acquireIndex_) { - JLOG(j_.trace()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.trace()) + << "shard " << shardIndex << " is not being acquired"; return false; } - if (auto const it {shards_.find(shardIndex)}; it != shards_.end()) + if (auto const it{shards_.find(shardIndex)}; it != shards_.end()) shard = it->second.shard; else { - JLOG(j_.error()) << - "shard " << shardIndex << " is not being acquired"; + JLOG(j_.error()) + << "shard " << shardIndex << " is not being acquired"; return false; } } if (shard->containsLedger(seq)) { - JLOG(j_.trace()) << - "shard " << shardIndex << " ledger already stored"; + JLOG(j_.trace()) << "shard " << shardIndex << " ledger already stored"; return false; } { auto [backend, pCache, nCache] = shard->getBackendAll(); if (!Database::storeLedger( - *srcLedger, - backend, - pCache, - nCache, - nullptr)) + *srcLedger, backend, pCache, nCache, nullptr)) { return false; } @@ -980,16 +952,15 @@ DatabaseShardImp::storeLedger(std::shared_ptr const& srcLedger) int DatabaseShardImp::getDesiredAsyncReadCount(std::uint32_t seq) { - auto const shardIndex {seqToShardIndex(seq)}; + auto const shardIndex{seqToShardIndex(seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(shardIndex)}; - it != shards_.end() && + if (auto const it{shards_.find(shardIndex)}; it != shards_.end() && (it->second.state == ShardInfo::State::final || - it->second.state == ShardInfo::State::acquire)) + it->second.state == ShardInfo::State::acquire)) { shard = it->second.shard; } @@ -1008,7 +979,7 @@ DatabaseShardImp::getCacheHitRate() std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(acquireIndex_)}; it != shards_.end()) + if (auto const it{shards_.find(acquireIndex_)}; it != shards_.end()) shard = it->second.shard; else return 0; @@ -1035,7 +1006,7 @@ DatabaseShardImp::sweep() for (auto const& e : shards) { - if (auto shard {e.lock()}; shard) + if (auto shard{e.lock()}; shard) shard->sweep(); } } @@ -1043,15 +1014,13 @@ DatabaseShardImp::sweep() bool DatabaseShardImp::initConfig(std::lock_guard&) { - auto fail = [j = j_](std::string const& msg) - { - JLOG(j.error()) << - "[" << ConfigSection::shardDatabase() << "] " << msg; + auto fail = [j = j_](std::string const& msg) { + JLOG(j.error()) << "[" << ConfigSection::shardDatabase() << "] " << msg; return false; }; - Config const& config {app_.config()}; - Section const& section {config.section(ConfigSection::shardDatabase())}; + Config const& config{app_.config()}; + Section const& section{config.section(ConfigSection::shardDatabase())}; { // The earliest ledger sequence defaults to XRP_LEDGER_EARLIEST_SEQ. @@ -1061,13 +1030,11 @@ DatabaseShardImp::initConfig(std::lock_guard&) // have a value greater than zero and be equally assigned in // both stanzas. - std::uint32_t shardDBEarliestSeq {0}; + std::uint32_t shardDBEarliestSeq{0}; get_if_exists( - section, - "earliest_seq", - shardDBEarliestSeq); + section, "earliest_seq", shardDBEarliestSeq); - std::uint32_t nodeDBEarliestSeq {0}; + std::uint32_t nodeDBEarliestSeq{0}; get_if_exists( config.section(ConfigSection::nodeDatabase()), "earliest_seq", @@ -1075,7 +1042,8 @@ DatabaseShardImp::initConfig(std::lock_guard&) if (shardDBEarliestSeq != nodeDBEarliestSeq) { - return fail("and [" + ConfigSection::nodeDatabase() + + return fail( + "and [" + ConfigSection::nodeDatabase() + "] define different 'earliest_seq' values"); } } @@ -1122,15 +1090,14 @@ DatabaseShardImp::initConfig(std::lock_guard&) std::shared_ptr DatabaseShardImp::fetchFrom(uint256 const& hash, std::uint32_t seq) { - auto const shardIndex {seqToShardIndex(seq)}; + auto const shardIndex{seqToShardIndex(seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(shardIndex)}; - it != shards_.end() && - it->second.shard) + if (auto const it{shards_.find(shardIndex)}; + it != shards_.end() && it->second.shard) { shard = it->second.shard; } @@ -1149,14 +1116,13 @@ DatabaseShardImp::findAcquireIndex( if (validLedgerSeq < earliestLedgerSeq()) return boost::none; - auto const maxShardIndex {[this, validLedgerSeq]() - { - auto shardIndex {seqToShardIndex(validLedgerSeq)}; + auto const maxShardIndex{[this, validLedgerSeq]() { + auto shardIndex{seqToShardIndex(validLedgerSeq)}; if (validLedgerSeq != lastLedgerSeq(shardIndex)) --shardIndex; return shardIndex; }()}; - auto const maxNumShards {maxShardIndex - earliestShardIndex() + 1}; + auto const maxNumShards{maxShardIndex - earliestShardIndex() + 1}; // Check if the shard store has all shards if (shards_.size() >= maxNumShards) @@ -1171,8 +1137,8 @@ DatabaseShardImp::findAcquireIndex( available.reserve(maxNumShards - shards_.size()); for (auto shardIndex = earliestShardIndex(); - shardIndex <= maxShardIndex; - ++shardIndex) + shardIndex <= maxShardIndex; + ++shardIndex) { if (shards_.find(shardIndex) == shards_.end()) available.push_back(shardIndex); @@ -1184,8 +1150,8 @@ DatabaseShardImp::findAcquireIndex( if (available.size() == 1) return available.front(); - return available[rand_int(0u, - static_cast(available.size() - 1))]; + return available[rand_int( + 0u, static_cast(available.size() - 1))]; } // Large, sparse index space to sample @@ -1193,7 +1159,7 @@ DatabaseShardImp::findAcquireIndex( // chances of running more than 30 times is less than 1 in a billion for (int i = 0; i < 40; ++i) { - auto const shardIndex {rand_int(earliestShardIndex(), maxShardIndex)}; + auto const shardIndex{rand_int(earliestShardIndex(), maxShardIndex)}; if (shards_.find(shardIndex) == shards_.end()) return shardIndex; } @@ -1213,23 +1179,21 @@ DatabaseShardImp::finalizeShard( assert(shardInfo.shard->isBackendComplete()); assert(shardInfo.state != ShardInfo::State::finalize); - auto const shardIndex {shardInfo.shard->index()}; + auto const shardIndex{shardInfo.shard->index()}; shardInfo.state = ShardInfo::State::finalize; - taskQueue_->addTask([this, shardIndex, writeSQLite]() - { + taskQueue_->addTask([this, shardIndex, writeSQLite]() { if (isStopping()) return; std::shared_ptr shard; { std::lock_guard lock(mutex_); - if (auto const it {shards_.find(shardIndex)}; it != shards_.end()) + if (auto const it{shards_.find(shardIndex)}; it != shards_.end()) shard = it->second.shard; else { - JLOG(j_.error()) << - "Unable to finalize shard " << shardIndex; + JLOG(j_.error()) << "Unable to finalize shard " << shardIndex; return; } } @@ -1257,7 +1221,7 @@ DatabaseShardImp::finalizeShard( { std::lock_guard lock(mutex_); - auto const it {shards_.find(shardIndex)}; + auto const it{shards_.find(shardIndex)}; if (it == shards_.end()) return; it->second.state = ShardInfo::State::final; @@ -1271,13 +1235,11 @@ DatabaseShardImp::finalizeShard( app_.getOPs().getOperatingMode() != OperatingMode::DISCONNECTED) { protocol::TMPeerShardInfo message; - PublicKey const& publicKey {app_.nodeIdentity().first}; + PublicKey const& publicKey{app_.nodeIdentity().first}; message.set_nodepubkey(publicKey.data(), publicKey.size()); message.set_shardindexes(std::to_string(shardIndex)); - app_.overlay().foreach(send_always( - std::make_shared( - message, - protocol::mtPEER_SHARD_INFO))); + app_.overlay().foreach (send_always(std::make_shared( + message, protocol::mtPEER_SHARD_INFO))); } }); } @@ -1298,14 +1260,14 @@ DatabaseShardImp::setFileStats() shards.push_back(e.second.shard); } - std::uint64_t sumSz {0}; - std::uint32_t sumFd {0}; - std::uint32_t numShards {0}; + std::uint64_t sumSz{0}; + std::uint32_t sumFd{0}; + std::uint32_t numShards{0}; for (auto const& e : shards) { - if (auto shard {e.lock()}; shard) + if (auto shard{e.lock()}; shard) { - auto[sz, fd] = shard->fileInfo(); + auto [sz, fd] = shard->fileInfo(); sumSz += sz; sumFd += fd; ++numShards; @@ -1324,8 +1286,8 @@ DatabaseShardImp::setFileStats() } else if (maxFileSz_ - fileSz_ > available()) { - JLOG(j_.warn()) << - "maximum shard store size exceeds available storage space"; + JLOG(j_.warn()) + << "maximum shard store size exceeds available storage space"; } } @@ -1347,13 +1309,13 @@ DatabaseShardImp::updateStatus(std::lock_guard&) std::pair, std::shared_ptr> DatabaseShardImp::getCache(std::uint32_t seq) { - auto const shardIndex {seqToShardIndex(seq)}; + auto const shardIndex{seqToShardIndex(seq)}; std::shared_ptr shard; { std::lock_guard lock(mutex_); assert(init_); - if (auto const it {shards_.find(shardIndex)}; + if (auto const it{shards_.find(shardIndex)}; it != shards_.end() && it->second.shard) { shard = it->second.shard; @@ -1378,8 +1340,8 @@ DatabaseShardImp::available() const } catch (std::exception const& e) { - JLOG(j_.error()) << - "exception " << e.what() << " in function " << __func__; + JLOG(j_.error()) << "exception " << e.what() << " in function " + << __func__; return 0; } } @@ -1389,7 +1351,7 @@ DatabaseShardImp::storeLedgerInShard( std::shared_ptr& shard, std::shared_ptr const& ledger) { - bool result {true}; + bool result{true}; if (!shard->store(ledger)) { @@ -1412,8 +1374,7 @@ DatabaseShardImp::storeLedgerInShard( { std::lock_guard lock(mutex_); - if (auto const it {shards_.find(shard->index())}; - it != shards_.end()) + if (auto const it{shards_.find(shard->index())}; it != shards_.end()) { if (shard->index() == acquireIndex_) acquireIndex_ = 0; @@ -1423,9 +1384,8 @@ DatabaseShardImp::storeLedgerInShard( } else { - JLOG(j_.debug()) << - "shard " << shard->index() << - " is no longer being acquired"; + JLOG(j_.debug()) + << "shard " << shard->index() << " is no longer being acquired"; } } @@ -1444,19 +1404,14 @@ make_ShardStore( beast::Journal j) { // The shard store is optional. Future changes will require it. - Section const& section { + Section const& section{ app.config().section(ConfigSection::shardDatabase())}; if (section.empty()) return nullptr; return std::make_unique( - app, - parent, - "ShardStore", - scheduler, - readThreads, - j); + app, parent, "ShardStore", scheduler, readThreads, j); } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/DatabaseShardImp.h b/src/ripple/nodestore/impl/DatabaseShardImp.h index 21a117bdab..d53d287445 100644 --- a/src/ripple/nodestore/impl/DatabaseShardImp.h +++ b/src/ripple/nodestore/impl/DatabaseShardImp.h @@ -33,8 +33,10 @@ public: DatabaseShardImp() = delete; DatabaseShardImp(DatabaseShardImp const&) = delete; DatabaseShardImp(DatabaseShardImp&&) = delete; - DatabaseShardImp& operator=(DatabaseShardImp const&) = delete; - DatabaseShardImp& operator=(DatabaseShardImp&&) = delete; + DatabaseShardImp& + operator=(DatabaseShardImp const&) = delete; + DatabaseShardImp& + operator=(DatabaseShardImp&&) = delete; DatabaseShardImp( Application& app, @@ -62,9 +64,8 @@ public: getPreShards() override; bool - importShard( - std::uint32_t shardIndex, - boost::filesystem::path const& srcDir) override; + importShard(std::uint32_t shardIndex, boost::filesystem::path const& srcDir) + override; std::shared_ptr fetchLedger(uint256 const& hash, std::uint32_t seq) override; @@ -164,7 +165,7 @@ public: getCacheHitRate() override; void - tune(int size, std::chrono::seconds age) override {}; + tune(int size, std::chrono::seconds age) override{}; void sweep() override; @@ -172,29 +173,28 @@ public: private: struct ShardInfo { - enum class State - { + enum class State { none, - final, // Immutable, complete and validated - acquire, // Being acquired - import, // Being imported - finalize // Being finalized + final, // Immutable, complete and validated + acquire, // Being acquired + import, // Being imported + finalize // Being finalized }; ShardInfo() = default; ShardInfo(std::shared_ptr shard_, State state_) - : shard(std::move(shard_)) - , state(state_) - {} + : shard(std::move(shard_)), state(state_) + { + } std::shared_ptr shard; - State state {State::none}; + State state{State::none}; }; Application& app_; Stoppable& parent_; mutable std::mutex mutex_; - bool init_ {false}; + bool init_{false}; // The context shared with all shard backend databases std::unique_ptr ctx_; @@ -206,13 +206,13 @@ private: std::map shards_; // Shard index being acquired from the peer network - std::uint32_t acquireIndex_ {0}; + std::uint32_t acquireIndex_{0}; // The shard store root directory boost::filesystem::path dir_; // If new shards can be stored - bool canAdd_ {true}; + bool canAdd_{true}; // Complete shard indexes std::string status_; @@ -224,7 +224,7 @@ private: std::uint64_t maxFileSz_; // Storage space utilized by the shard store (in bytes) - std::uint64_t fileSz_ {0}; + std::uint64_t fileSz_{0}; // Each shard stores 16384 ledgers. The earliest shard may store // less if the earliest ledger sequence truncates its beginning. @@ -249,7 +249,7 @@ private: fetchFrom(uint256 const& hash, std::uint32_t seq) override; void - for_each(std::function )> f) override + for_each(std::function)> f) override { Throw("Shard store import not supported"); } @@ -292,7 +292,7 @@ private: std::shared_ptr const& ledger); }; -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/DecodedBlob.cpp b/src/ripple/nodestore/impl/DecodedBlob.cpp index 28514dc29e..0c5a5de20c 100644 --- a/src/ripple/nodestore/impl/DecodedBlob.cpp +++ b/src/ripple/nodestore/impl/DecodedBlob.cpp @@ -25,7 +25,7 @@ namespace ripple { namespace NodeStore { -DecodedBlob::DecodedBlob (void const* key, void const* value, int valueBytes) +DecodedBlob::DecodedBlob(void const* key, void const* value, int valueBytes) { /* Data format: @@ -41,38 +41,39 @@ DecodedBlob::DecodedBlob (void const* key, void const* value, int valueBytes) // VFALCO NOTE Ledger indexes should have started at 1 m_objectType = hotUNKNOWN; m_objectData = nullptr; - m_dataBytes = std::max (0, valueBytes - 9); + m_dataBytes = std::max(0, valueBytes - 9); // VFALCO NOTE What about bytes 4 through 7 inclusive? if (valueBytes > 8) { - unsigned char const* byte = static_cast (value); - m_objectType = safe_cast (byte [8]); + unsigned char const* byte = static_cast(value); + m_objectType = safe_cast(byte[8]); } if (valueBytes > 9) { - m_objectData = static_cast (value) + 9; + m_objectData = static_cast(value) + 9; switch (m_objectType) { - default: - break; + default: + break; - case hotUNKNOWN: - case hotLEDGER: - case hotACCOUNT_NODE: - case hotTRANSACTION_NODE: - m_success = true; - break; + case hotUNKNOWN: + case hotLEDGER: + case hotACCOUNT_NODE: + case hotTRANSACTION_NODE: + m_success = true; + break; } } } -std::shared_ptr DecodedBlob::createObject () +std::shared_ptr +DecodedBlob::createObject() { - assert (m_success); + assert(m_success); std::shared_ptr object; @@ -80,12 +81,12 @@ std::shared_ptr DecodedBlob::createObject () { Blob data(m_objectData, m_objectData + m_dataBytes); - object = NodeObject::createObject ( + object = NodeObject::createObject( m_objectType, std::move(data), uint256::fromVoid(m_key)); } return object; } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/DecodedBlob.h b/src/ripple/nodestore/impl/DecodedBlob.h index 1e50a4504c..623e1f18dc 100644 --- a/src/ripple/nodestore/impl/DecodedBlob.h +++ b/src/ripple/nodestore/impl/DecodedBlob.h @@ -39,13 +39,18 @@ class DecodedBlob { public: /** Construct the decoded blob from raw data. */ - DecodedBlob (void const* key, void const* value, int valueBytes); + DecodedBlob(void const* key, void const* value, int valueBytes); /** Determine if the decoding was successful. */ - bool wasOk () const noexcept { return m_success; } + bool + wasOk() const noexcept + { + return m_success; + } /** Create a NodeObject from this data. */ - std::shared_ptr createObject (); + std::shared_ptr + createObject(); private: bool m_success; @@ -56,7 +61,7 @@ private: int m_dataBytes; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/DummyScheduler.cpp b/src/ripple/nodestore/impl/DummyScheduler.cpp index adbb4d405e..b15ec25eb6 100644 --- a/src/ripple/nodestore/impl/DummyScheduler.cpp +++ b/src/ripple/nodestore/impl/DummyScheduler.cpp @@ -23,26 +23,26 @@ namespace ripple { namespace NodeStore { void -DummyScheduler::scheduleTask (Task& task) +DummyScheduler::scheduleTask(Task& task) { // Invoke the task synchronously. task.performScheduledTask(); } void -DummyScheduler::scheduledTasksStopped () +DummyScheduler::scheduledTasksStopped() { } void -DummyScheduler::onFetch (const FetchReport& report) +DummyScheduler::onFetch(const FetchReport& report) { } void -DummyScheduler::onBatchWrite (const BatchWriteReport& report) +DummyScheduler::onBatchWrite(const BatchWriteReport& report) { } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/EncodedBlob.cpp b/src/ripple/nodestore/impl/EncodedBlob.cpp index e86d81ec79..4ec15b1020 100644 --- a/src/ripple/nodestore/impl/EncodedBlob.cpp +++ b/src/ripple/nodestore/impl/EncodedBlob.cpp @@ -24,22 +24,19 @@ namespace ripple { namespace NodeStore { void -EncodedBlob::prepare ( - std::shared_ptr const& object) +EncodedBlob::prepare(std::shared_ptr const& object) { - m_key = object->getHash().begin (); + m_key = object->getHash().begin(); - auto ret = m_data.alloc(object->getData ().size () + 9); + auto ret = m_data.alloc(object->getData().size() + 9); // the first 8 bytes are unused - std::memset (ret, 0, 8); + std::memset(ret, 0, 8); - ret[8] = static_cast (object->getType ()); + ret[8] = static_cast(object->getType()); - std::memcpy (ret + 9, - object->getData ().data(), - object->getData ().size()); + std::memcpy(ret + 9, object->getData().data(), object->getData().size()); } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/EncodedBlob.h b/src/ripple/nodestore/impl/EncodedBlob.h index 849a80ad29..2094b52d33 100644 --- a/src/ripple/nodestore/impl/EncodedBlob.h +++ b/src/ripple/nodestore/impl/EncodedBlob.h @@ -34,19 +34,23 @@ namespace NodeStore { struct EncodedBlob { public: - void prepare (std::shared_ptr const& object); + void + prepare(std::shared_ptr const& object); - void const* getKey () const noexcept + void const* + getKey() const noexcept { return m_key; } - std::size_t getSize () const noexcept + std::size_t + getSize() const noexcept { return m_data.size(); } - void const* getData () const noexcept + void const* + getData() const noexcept { return reinterpret_cast(m_data.data()); } @@ -56,7 +60,7 @@ private: Buffer m_data; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/ManagerImp.cpp b/src/ripple/nodestore/impl/ManagerImp.cpp index 6059c9deee..30c40fedcf 100644 --- a/src/ripple/nodestore/impl/ManagerImp.cpp +++ b/src/ripple/nodestore/impl/ManagerImp.cpp @@ -17,8 +17,8 @@ */ //============================================================================== -#include #include +#include #include @@ -35,32 +35,31 @@ ManagerImp::instance() void ManagerImp::missing_backend() { - Throw ( + Throw( "Your rippled.cfg is missing a [node_db] entry, " - "please see the rippled-example.cfg file!" - ); + "please see the rippled-example.cfg file!"); } -std::unique_ptr -ManagerImp::make_Backend ( +std::unique_ptr +ManagerImp::make_Backend( Section const& parameters, Scheduler& scheduler, beast::Journal journal) { - std::string const type {get(parameters, "type")}; + std::string const type{get(parameters, "type")}; if (type.empty()) missing_backend(); - auto factory {find(type)}; - if(!factory) + auto factory{find(type)}; + if (!factory) missing_backend(); return factory->createInstance( NodeObject::keyBytes, parameters, scheduler, journal); } -std::unique_ptr -ManagerImp::make_Database ( +std::unique_ptr +ManagerImp::make_Database( std::string const& name, Scheduler& scheduler, int readThreads, @@ -68,9 +67,9 @@ ManagerImp::make_Database ( Section const& config, beast::Journal journal) { - auto backend {make_Backend(config, scheduler, journal)}; + auto backend{make_Backend(config, scheduler, journal)}; backend->open(); - return std::make_unique ( + return std::make_unique( name, scheduler, readThreads, @@ -81,31 +80,32 @@ ManagerImp::make_Database ( } void -ManagerImp::insert (Factory& factory) +ManagerImp::insert(Factory& factory) { std::lock_guard _(mutex_); list_.push_back(&factory); } void -ManagerImp::erase (Factory& factory) +ManagerImp::erase(Factory& factory) { std::lock_guard _(mutex_); - auto const iter = std::find_if(list_.begin(), list_.end(), - [&factory](Factory* other) { return other == &factory; }); + auto const iter = + std::find_if(list_.begin(), list_.end(), [&factory](Factory* other) { + return other == &factory; + }); assert(iter != list_.end()); list_.erase(iter); } Factory* -ManagerImp::find (std::string const& name) +ManagerImp::find(std::string const& name) { std::lock_guard _(mutex_); - auto const iter = std::find_if(list_.begin(), list_.end(), - [&name](Factory* other) - { + auto const iter = + std::find_if(list_.begin(), list_.end(), [&name](Factory* other) { return boost::iequals(name, other->getName()); - } ); + }); if (iter == list_.end()) return nullptr; return *iter; @@ -121,13 +121,14 @@ Manager::instance() //------------------------------------------------------------------------------ -std::unique_ptr -make_Backend (Section const& config, - Scheduler& scheduler, beast::Journal journal) +std::unique_ptr +make_Backend( + Section const& config, + Scheduler& scheduler, + beast::Journal journal) { - return Manager::instance().make_Backend ( - config, scheduler, journal); + return Manager::instance().make_Backend(config, scheduler, journal); } -} -} +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/ManagerImp.h b/src/ripple/nodestore/impl/ManagerImp.h index f8b3880df0..585e2720a2 100644 --- a/src/ripple/nodestore/impl/ManagerImp.h +++ b/src/ripple/nodestore/impl/ManagerImp.h @@ -32,12 +32,10 @@ private: std::vector list_; public: - static - ManagerImp& + static ManagerImp& instance(); - static - void + static void missing_backend(); ManagerImp() = default; @@ -45,22 +43,22 @@ public: ~ManagerImp() = default; Factory* - find (std::string const& name) override; + find(std::string const& name) override; void - insert (Factory& factory) override; + insert(Factory& factory) override; void - erase (Factory& factory) override; + erase(Factory& factory) override; - std::unique_ptr - make_Backend ( + std::unique_ptr + make_Backend( Section const& parameters, Scheduler& scheduler, beast::Journal journal) override; - std::unique_ptr - make_Database ( + std::unique_ptr + make_Database( std::string const& name, Scheduler& scheduler, int readThreads, @@ -69,7 +67,7 @@ public: beast::Journal journal) override; }; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/NodeObject.cpp b/src/ripple/nodestore/impl/NodeObject.cpp index 682b3b3b4d..fb476107c8 100644 --- a/src/ripple/nodestore/impl/NodeObject.cpp +++ b/src/ripple/nodestore/impl/NodeObject.cpp @@ -24,43 +24,38 @@ namespace ripple { //------------------------------------------------------------------------------ -NodeObject::NodeObject ( +NodeObject::NodeObject( NodeObjectType type, Blob&& data, uint256 const& hash, PrivateAccess) - : mType (type) - , mHash (hash) - , mData (std::move(data)) + : mType(type), mHash(hash), mData(std::move(data)) { } std::shared_ptr -NodeObject::createObject ( - NodeObjectType type, - Blob&& data, - uint256 const& hash) +NodeObject::createObject(NodeObjectType type, Blob&& data, uint256 const& hash) { - return std::make_shared ( - type, std::move (data), hash, PrivateAccess ()); + return std::make_shared( + type, std::move(data), hash, PrivateAccess()); } NodeObjectType -NodeObject::getType () const +NodeObject::getType() const { return mType; } uint256 const& -NodeObject::getHash () const +NodeObject::getHash() const { return mHash; } Blob const& -NodeObject::getData () const +NodeObject::getData() const { return mData; } -} +} // namespace ripple diff --git a/src/ripple/nodestore/impl/Shard.cpp b/src/ripple/nodestore/impl/Shard.cpp index 6ae52d4f20..a4dceb55cf 100644 --- a/src/ripple/nodestore/impl/Shard.cpp +++ b/src/ripple/nodestore/impl/Shard.cpp @@ -17,13 +17,13 @@ */ //============================================================================== -#include #include #include #include #include -#include #include +#include +#include #include #include @@ -32,7 +32,7 @@ namespace ripple { namespace NodeStore { -uint256 const Shard::finalKey {0}; +uint256 const Shard::finalKey{0}; Shard::Shard( Application& app, @@ -43,8 +43,9 @@ Shard::Shard( , index_(index) , firstSeq_(db.firstLedgerSeq(index)) , lastSeq_(std::max(firstSeq_, db.lastLedgerSeq(index))) - , maxLedgers_(index == db.earliestShardIndex() ? - lastSeq_ - firstSeq_ + 1 : db.ledgersPerShard()) + , maxLedgers_( + index == db.earliestShardIndex() ? lastSeq_ - firstSeq_ + 1 + : db.ledgersPerShard()) , dir_(db.getRootDir() / std::to_string(index_)) , j_(j) { @@ -67,10 +68,8 @@ Shard::~Shard() } catch (std::exception const& e) { - JLOG(j_.error()) << - "shard " << index_ << - " exception " << e.what() << - " in function " << __func__; + JLOG(j_.error()) << "shard " << index_ << " exception " << e.what() + << " in function " << __func__; } } } @@ -78,19 +77,18 @@ Shard::~Shard() bool Shard::open(Scheduler& scheduler, nudb::context& ctx) { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; assert(!backend_); - Config const& config {app_.config()}; + Config const& config{app_.config()}; { - Section section {config.section(ConfigSection::shardDatabase())}; - std::string const type {get(section, "type", "nudb")}; - auto factory {Manager::instance().find(type)}; + Section section{config.section(ConfigSection::shardDatabase())}; + std::string const type{get(section, "type", "nudb")}; + auto factory{Manager::instance().find(type)}; if (!factory) { - JLOG(j_.error()) << - "shard " << index_ << - " failed to create backend type " << type; + JLOG(j_.error()) << "shard " << index_ + << " failed to create backend type " << type; return false; } @@ -100,9 +98,8 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) } using namespace boost::filesystem; - auto preexist {false}; - auto fail = [this, &preexist](std::string const& msg) - { + auto preexist{false}; + auto fail = [this, &preexist](std::string const& msg) { pCache_.reset(); nCache_.reset(); backend_.reset(); @@ -120,8 +117,7 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) return false; }; - auto createAcquireInfo = [this, &config]() - { + auto createAcquireInfo = [this, &config]() { acquireInfo_ = std::make_unique(); DatabaseCon::Setup setup; @@ -135,8 +131,7 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) AcquireShardDBPragma, AcquireShardDBInit); acquireInfo_->SQLiteDB->setupCheckpointing( - &app_.getJobQueue(), - app_.logs()); + &app_.getJobQueue(), app_.logs()); }; try @@ -149,28 +144,26 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) { // A new shard createAcquireInfo(); - acquireInfo_->SQLiteDB->getSession() << - "INSERT INTO Shard (ShardIndex) " - "VALUES (:shardIndex);" - , soci::use(index_); + acquireInfo_->SQLiteDB->getSession() + << "INSERT INTO Shard (ShardIndex) " + "VALUES (:shardIndex);", + soci::use(index_); } else if (exists(dir_ / AcquireShardDBName)) { // An incomplete shard, being acquired createAcquireInfo(); - auto& session {acquireInfo_->SQLiteDB->getSession()}; + auto& session{acquireInfo_->SQLiteDB->getSession()}; boost::optional index; soci::blob sociBlob(session); soci::indicator blobPresent; - session << - "SELECT ShardIndex, StoredLedgerSeqs " - "FROM Shard " - "WHERE ShardIndex = :index;" - , soci::into(index) - , soci::into(sociBlob, blobPresent) - , soci::use(index_); + session << "SELECT ShardIndex, StoredLedgerSeqs " + "FROM Shard " + "WHERE ShardIndex = :index;", + soci::into(index), soci::into(sociBlob, blobPresent), + soci::use(index_); if (!index || index != index_) return fail("invalid acquire SQLite database"); @@ -178,7 +171,7 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) if (blobPresent == soci::i_ok) { std::string s; - auto& storedSeqs {acquireInfo_->storedSeqs}; + auto& storedSeqs{acquireInfo_->storedSeqs}; if (convert(sociBlob, s); !from_string(storedSeqs, s)) return fail("invalid StoredLedgerSeqs"); @@ -225,8 +218,8 @@ Shard::open(Scheduler& scheduler, nudb::context& ctx) } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + __func__); } setBackendCache(lock); @@ -245,28 +238,26 @@ Shard::prepare() if (backendComplete_) { - JLOG(j_.warn()) << - "shard " << index_ << - " prepare called when shard is complete"; + JLOG(j_.warn()) << "shard " << index_ + << " prepare called when shard is complete"; return {}; } assert(acquireInfo_); - auto const& storedSeqs {acquireInfo_->storedSeqs}; + auto const& storedSeqs{acquireInfo_->storedSeqs}; if (storedSeqs.empty()) - return lastSeq_; + return lastSeq_; return prevMissing(storedSeqs, 1 + lastSeq_, firstSeq_); } bool Shard::store(std::shared_ptr const& ledger) { - auto const seq {ledger->info().seq}; + auto const seq{ledger->info().seq}; if (seq < firstSeq_ || seq > lastSeq_) { - JLOG(j_.error()) << - "shard " << index_ << - " invalid ledger sequence " << seq; + JLOG(j_.error()) << "shard " << index_ << " invalid ledger sequence " + << seq; return false; } @@ -275,19 +266,17 @@ Shard::store(std::shared_ptr const& ledger) if (backendComplete_) { - JLOG(j_.debug()) << - "shard " << index_ << - " ledger sequence " << seq << " already stored"; + JLOG(j_.debug()) << "shard " << index_ << " ledger sequence " << seq + << " already stored"; return true; } assert(acquireInfo_); - auto& storedSeqs {acquireInfo_->storedSeqs}; + auto& storedSeqs{acquireInfo_->storedSeqs}; if (boost::icl::contains(storedSeqs, seq)) { - JLOG(j_.debug()) << - "shard " << index_ << - " ledger sequence " << seq << " already stored"; + JLOG(j_.debug()) << "shard " << index_ << " ledger sequence " << seq + << " already stored"; return true; } // storeSQLite looks at storedSeqs so insert before the call @@ -306,10 +295,8 @@ Shard::store(std::shared_ptr const& ledger) setBackendCache(lock); } - JLOG(j_.debug()) << - "shard " << index_ << - " stored ledger sequence " << seq << - (backendComplete_ ? " . All ledgers stored" : ""); + JLOG(j_.debug()) << "shard " << index_ << " stored ledger sequence " << seq + << (backendComplete_ ? " . All ledgers stored" : ""); setFileStats(lock); return true; @@ -414,16 +401,16 @@ Shard::finalize(const bool writeSQLite) if (stop_) return false; - uint256 hash {0}; - std::uint32_t seq {0}; - auto fail = [j = j_, index = index_, &hash, &seq](std::string const& msg) - { - JLOG(j.fatal()) << - "shard " << index << ". " << msg << - (hash.isZero() ? "" : ". Ledger hash " + to_string(hash)) << - (seq == 0 ? "" : ". Ledger sequence " + std::to_string(seq)); - return false; - }; + uint256 hash{0}; + std::uint32_t seq{0}; + auto fail = + [j = j_, index = index_, &hash, &seq](std::string const& msg) { + JLOG(j.fatal()) + << "shard " << index << ". " << msg + << (hash.isZero() ? "" : ". Ledger hash " + to_string(hash)) + << (seq == 0 ? "" : ". Ledger sequence " + std::to_string(seq)); + return false; + }; try { @@ -466,19 +453,16 @@ Shard::finalize(const bool writeSQLite) if (!acquireInfo_) return fail("missing acquire SQLite database"); - auto& session {acquireInfo_->SQLiteDB->getSession()}; + auto& session{acquireInfo_->SQLiteDB->getSession()}; boost::optional index; boost::optional sHash; soci::blob sociBlob(session); soci::indicator blobPresent; - session << - "SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs " - "FROM Shard " - "WHERE ShardIndex = :index;" - , soci::into(index) - , soci::into(sHash) - , soci::into(sociBlob, blobPresent) - , soci::use(index_); + session << "SELECT ShardIndex, LastLedgerHash, StoredLedgerSeqs " + "FROM Shard " + "WHERE ShardIndex = :index;", + soci::into(index), soci::into(sHash), + soci::into(sociBlob, blobPresent), soci::use(index_); lock.unlock(); if (!index || index != index_) @@ -498,7 +482,7 @@ Shard::finalize(const bool writeSQLite) lock.lock(); - auto& storedSeqs {acquireInfo_->storedSeqs}; + auto& storedSeqs{acquireInfo_->storedSeqs}; if (!from_string(storedSeqs, s) || boost::icl::first(storedSeqs) != firstSeq_ || boost::icl::last(storedSeqs) != lastSeq_ || @@ -510,14 +494,14 @@ Shard::finalize(const bool writeSQLite) } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + __func__); } // Validate every ledger stored in the backend std::shared_ptr ledger; std::shared_ptr next; - auto const lastLedgerHash {hash}; + auto const lastLedgerHash{hash}; // Start with the last ledger in the shard and walk backwards from // child to parent until we reach the first ledger @@ -544,14 +528,13 @@ Shard::finalize(const bool writeSQLite) ledger->txMap().setLedgerSeq(seq); ledger->setImmutable(app_.config()); if (!ledger->stateMap().fetchRoot( - SHAMapHash {ledger->info().accountHash}, nullptr)) + SHAMapHash{ledger->info().accountHash}, nullptr)) { return fail("missing root STATE node"); } if (ledger->info().txHash.isNonZero() && !ledger->txMap().fetchRoot( - SHAMapHash {ledger->info().txHash}, - nullptr)) + SHAMapHash{ledger->info().txHash}, nullptr)) { return fail("missing root TXN node"); } @@ -612,10 +595,8 @@ Shard::finalize(const bool writeSQLite) s.add32(firstSeq_); s.add32(lastSeq_); s.add256(lastLedgerHash); - auto nObj {NodeObject::createObject( - hotUNKNOWN, - std::move(s.modData()), - finalKey)}; + auto nObj{ + NodeObject::createObject(hotUNKNOWN, std::move(s.modData()), finalKey)}; try { backend_->store(nObj); @@ -635,8 +616,8 @@ Shard::finalize(const bool writeSQLite) } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + __func__); } return true; @@ -649,14 +630,14 @@ Shard::setBackendCache(std::lock_guard const&) // fastest expiration to reduce memory consumption. // An incomplete shard is set according to configuration. - Config const& config {app_.config()}; + Config const& config{app_.config()}; if (!pCache_) { - auto const name {"shard " + std::to_string(index_)}; - auto const sz {config.getValueFor( + auto const name{"shard " + std::to_string(index_)}; + auto const sz{config.getValueFor( SizedItem::nodeCacheSize, backendComplete_ ? boost::optional(0) : boost::none)}; - auto const age {std::chrono::seconds{config.getValueFor( + auto const age{std::chrono::seconds{config.getValueFor( SizedItem::nodeCacheAge, backendComplete_ ? boost::optional(0) : boost::none)}}; @@ -665,11 +646,11 @@ Shard::setBackendCache(std::lock_guard const&) } else { - auto const sz {config.getValueFor(SizedItem::nodeCacheSize, 0)}; + auto const sz{config.getValueFor(SizedItem::nodeCacheSize, 0)}; pCache_->setTargetSize(sz); nCache_->setTargetSize(sz); - auto const age {std::chrono::seconds{ + auto const age{std::chrono::seconds{ config.getValueFor(SizedItem::nodeCacheAge, 0)}}; pCache_->setTargetAge(age); nCache_->setTargetAge(age); @@ -679,7 +660,7 @@ Shard::setBackendCache(std::lock_guard const&) bool Shard::initSQLite(std::lock_guard const&) { - Config const& config {app_.config()}; + Config const& config{app_.config()}; DatabaseCon::Setup setup; setup.startUp = config.START_UP; setup.standAlone = config.standalone(); @@ -696,57 +677,41 @@ Shard::initSQLite(std::lock_guard const&) if (backendComplete_) { lgrSQLiteDB_ = std::make_unique( - setup, - LgrDBName, - CompleteShardDBPragma, - LgrDBInit); - lgrSQLiteDB_->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % - kilobytes(config.getValueFor( - SizedItem::lgrDBCache, - boost::none))); + setup, LgrDBName, CompleteShardDBPragma, LgrDBInit); + lgrSQLiteDB_->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % + kilobytes( + config.getValueFor(SizedItem::lgrDBCache, boost::none))); txSQLiteDB_ = std::make_unique( - setup, - TxDBName, - CompleteShardDBPragma, - TxDBInit); - txSQLiteDB_->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % - kilobytes(config.getValueFor( - SizedItem::txnDBCache, - boost::none))); + setup, TxDBName, CompleteShardDBPragma, TxDBInit); + txSQLiteDB_->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % + kilobytes( + config.getValueFor(SizedItem::txnDBCache, boost::none))); } else { // The incomplete shard uses a Write Ahead Log for performance lgrSQLiteDB_ = std::make_unique( - setup, - LgrDBName, - LgrDBPragma, - LgrDBInit); - lgrSQLiteDB_->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % + setup, LgrDBName, LgrDBPragma, LgrDBInit); + lgrSQLiteDB_->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % kilobytes(config.getValueFor(SizedItem::lgrDBCache))); lgrSQLiteDB_->setupCheckpointing(&app_.getJobQueue(), app_.logs()); txSQLiteDB_ = std::make_unique( - setup, - TxDBName, - TxDBPragma, - TxDBInit); - txSQLiteDB_->getSession() << - boost::str(boost::format("PRAGMA cache_size=-%d;") % + setup, TxDBName, TxDBPragma, TxDBInit); + txSQLiteDB_->getSession() << boost::str( + boost::format("PRAGMA cache_size=-%d;") % kilobytes(config.getValueFor(SizedItem::txnDBCache))); txSQLiteDB_->setupCheckpointing(&app_.getJobQueue(), app_.logs()); } } catch (std::exception const& e) { - JLOG(j_.fatal()) << - "shard " << index_ << - " exception " << e.what() << - " in function " << __func__; + JLOG(j_.fatal()) << "shard " << index_ << " exception " << e.what() + << " in function " << __func__; return false; } return true; @@ -760,33 +725,30 @@ Shard::storeSQLite( if (stop_) return false; - auto const seq {ledger->info().seq}; + auto const seq{ledger->info().seq}; try { // Update the transactions database { - auto& session {txSQLiteDB_->getSession()}; + auto& session{txSQLiteDB_->getSession()}; soci::transaction tr(session); - session << - "DELETE FROM Transactions " - "WHERE LedgerSeq = :seq;" - , soci::use(seq); - session << - "DELETE FROM AccountTransactions " - "WHERE LedgerSeq = :seq;" - , soci::use(seq); + session << "DELETE FROM Transactions " + "WHERE LedgerSeq = :seq;", + soci::use(seq); + session << "DELETE FROM AccountTransactions " + "WHERE LedgerSeq = :seq;", + soci::use(seq); if (ledger->info().txHash.isNonZero()) { - auto const sSeq {std::to_string(seq)}; + auto const sSeq{std::to_string(seq)}; if (!ledger->txMap().isValid()) { - JLOG(j_.error()) << - "shard " << index_ << - " has an invalid transaction map" << - " on sequence " << sSeq; + JLOG(j_.error()) << "shard " << index_ + << " has an invalid transaction map" + << " on sequence " << sSeq; return false; } @@ -795,102 +757,93 @@ Shard::storeSQLite( if (stop_) return false; - auto const txID {item.first->getTransactionID()}; - auto const sTxID {to_string(txID)}; - auto const txMeta {std::make_shared( + auto const txID{item.first->getTransactionID()}; + auto const sTxID{to_string(txID)}; + auto const txMeta{std::make_shared( txID, ledger->seq(), *item.second)}; - session << - "DELETE FROM AccountTransactions " - "WHERE TransID = :txID;" - , soci::use(sTxID); + session << "DELETE FROM AccountTransactions " + "WHERE TransID = :txID;", + soci::use(sTxID); auto const& accounts = txMeta->getAffectedAccounts(j_); if (!accounts.empty()) { - auto const sTxnSeq {std::to_string(txMeta->getIndex())}; - auto const s {boost::str(boost::format( - "('%s','%s',%s,%s)") - % sTxID - % "%s" - % sSeq - % sTxnSeq)}; + auto const sTxnSeq{std::to_string(txMeta->getIndex())}; + auto const s{boost::str( + boost::format("('%s','%s',%s,%s)") % sTxID % "%s" % + sSeq % sTxnSeq)}; std::string sql; sql.reserve((accounts.size() + 1) * 128); - sql = "INSERT INTO AccountTransactions " + sql = + "INSERT INTO AccountTransactions " "(TransID, Account, LedgerSeq, TxnSeq) VALUES "; sql += boost::algorithm::join( - accounts | boost::adaptors::transformed( - [&](AccountID const& accountID) - { - return boost::str(boost::format(s) - % ripple::toBase58(accountID)); - }), - ","); + accounts | + boost::adaptors::transformed( + [&](AccountID const& accountID) { + return boost::str( + boost::format(s) % + ripple::toBase58(accountID)); + }), + ","); sql += ';'; session << sql; - JLOG(j_.trace()) << - "shard " << index_ << - " account transaction: " << sql; + JLOG(j_.trace()) << "shard " << index_ + << " account transaction: " << sql; } else { - JLOG(j_.warn()) << - "shard " << index_ << - " transaction in ledger " << sSeq << - " affects no accounts"; + JLOG(j_.warn()) + << "shard " << index_ << " transaction in ledger " + << sSeq << " affects no accounts"; } Serializer s; item.second->add(s); - session << - (STTx::getMetaSQLInsertReplaceHeader() + - item.first->getMetaSQL( - seq, - sqlEscape(std::move(s.modData()))) - + ';'); + session + << (STTx::getMetaSQLInsertReplaceHeader() + + item.first->getMetaSQL( + seq, sqlEscape(std::move(s.modData()))) + + ';'); } } - tr.commit (); + tr.commit(); } - auto const sHash {to_string(ledger->info().hash)}; + auto const sHash{to_string(ledger->info().hash)}; // Update the ledger database { - auto& session {lgrSQLiteDB_->getSession()}; + auto& session{lgrSQLiteDB_->getSession()}; soci::transaction tr(session); - auto const sParentHash {to_string(ledger->info().parentHash)}; - auto const sDrops {to_string(ledger->info().drops)}; - auto const sAccountHash {to_string(ledger->info().accountHash)}; - auto const sTxHash {to_string(ledger->info().txHash)}; + auto const sParentHash{to_string(ledger->info().parentHash)}; + auto const sDrops{to_string(ledger->info().drops)}; + auto const sAccountHash{to_string(ledger->info().accountHash)}; + auto const sTxHash{to_string(ledger->info().txHash)}; - session << - "DELETE FROM Ledgers " - "WHERE LedgerSeq = :seq;" - , soci::use(seq); - session << - "INSERT OR REPLACE INTO Ledgers (" - "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime," - "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash," - "TransSetHash)" - "VALUES (" - ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins," - ":closingTime, :prevClosingTime, :closeTimeRes," - ":closeFlags, :accountSetHash, :transSetHash);", - soci::use(sHash), - soci::use(seq), - soci::use(sParentHash), + session << "DELETE FROM Ledgers " + "WHERE LedgerSeq = :seq;", + soci::use(seq); + session + << "INSERT OR REPLACE INTO Ledgers (" + "LedgerHash, LedgerSeq, PrevHash, TotalCoins, ClosingTime," + "PrevClosingTime, CloseTimeRes, CloseFlags, AccountSetHash," + "TransSetHash)" + "VALUES (" + ":ledgerHash, :ledgerSeq, :prevHash, :totalCoins," + ":closingTime, :prevClosingTime, :closeTimeRes," + ":closeFlags, :accountSetHash, :transSetHash);", + soci::use(sHash), soci::use(seq), soci::use(sParentHash), soci::use(sDrops), soci::use(ledger->info().closeTime.time_since_epoch().count()), soci::use( ledger->info().parentCloseTime.time_since_epoch().count()), soci::use(ledger->info().closeTimeResolution.count()), - soci::use(ledger->info().closeFlags), - soci::use(sAccountHash), + soci::use(ledger->info().closeFlags), soci::use(sAccountHash), soci::use(sTxHash); tr.commit(); @@ -899,7 +852,7 @@ Shard::storeSQLite( // Update the acquire database if present if (acquireInfo_) { - auto& session {acquireInfo_->SQLiteDB->getSession()}; + auto& session{acquireInfo_->SQLiteDB->getSession()}; soci::blob sociBlob(session); if (!acquireInfo_->storedSeqs.empty()) @@ -908,32 +861,25 @@ Shard::storeSQLite( if (ledger->info().seq == lastSeq_) { // Store shard's last ledger hash - session << - "UPDATE Shard " - "SET LastLedgerHash = :lastLedgerHash," - "StoredLedgerSeqs = :storedLedgerSeqs " - "WHERE ShardIndex = :shardIndex;" - , soci::use(sHash) - , soci::use(sociBlob) - , soci::use(index_); + session << "UPDATE Shard " + "SET LastLedgerHash = :lastLedgerHash," + "StoredLedgerSeqs = :storedLedgerSeqs " + "WHERE ShardIndex = :shardIndex;", + soci::use(sHash), soci::use(sociBlob), soci::use(index_); } else { - session << - "UPDATE Shard " - "SET StoredLedgerSeqs = :storedLedgerSeqs " - "WHERE ShardIndex = :shardIndex;" - , soci::use(sociBlob) - , soci::use(index_); + session << "UPDATE Shard " + "SET StoredLedgerSeqs = :storedLedgerSeqs " + "WHERE ShardIndex = :shardIndex;", + soci::use(sociBlob), soci::use(index_); } } } catch (std::exception const& e) { - JLOG(j_.fatal()) << - "shard " << index_ << - " exception " << e.what() << - " in function " << __func__; + JLOG(j_.fatal()) << "shard " << index_ << " exception " << e.what() + << " in function " << __func__; return false; } return true; @@ -958,10 +904,8 @@ Shard::setFileStats(std::lock_guard const&) } catch (std::exception const& e) { - JLOG(j_.error()) << - "shard " << index_ << - " exception " << e.what() << - " in function " << __func__; + JLOG(j_.error()) << "shard " << index_ << " exception " << e.what() + << " in function " << __func__; } } @@ -970,16 +914,14 @@ Shard::valLedger( std::shared_ptr const& ledger, std::shared_ptr const& next) const { - auto fail = [j = j_, index = index_, &ledger](std::string const& msg) - { - JLOG(j.fatal()) << - "shard " << index << ". " << msg << - (ledger->info().hash.isZero() ? - "" : ". Ledger hash " + - to_string(ledger->info().hash)) << - (ledger->info().seq == 0 ? - "" : ". Ledger sequence " + - std::to_string(ledger->info().seq)); + auto fail = [j = j_, index = index_, &ledger](std::string const& msg) { + JLOG(j.fatal()) << "shard " << index << ". " << msg + << (ledger->info().hash.isZero() ? "" + : ". Ledger hash " + + to_string(ledger->info().hash)) + << (ledger->info().seq == 0 ? "" + : ". Ledger sequence " + + std::to_string(ledger->info().seq)); return false; }; @@ -988,9 +930,8 @@ Shard::valLedger( if (ledger->info().accountHash.isZero()) return fail("Invalid ledger account hash"); - bool error {false}; - auto visit = [this, &error](SHAMapAbstractNode& node) - { + bool error{false}; + auto visit = [this, &error](SHAMapAbstractNode& node) { if (stop_) return false; if (!valFetch(node.getNodeHash().as_uint256())) @@ -1013,8 +954,9 @@ Shard::valLedger( } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + + __func__); } if (stop_) return false; @@ -1034,8 +976,9 @@ Shard::valLedger( } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + + __func__); } if (stop_) return false; @@ -1050,11 +993,9 @@ std::shared_ptr Shard::valFetch(uint256 const& hash) const { std::shared_ptr nObj; - auto fail = [j = j_, index = index_, &hash, &nObj](std::string const& msg) - { - JLOG(j.fatal()) << - "shard " << index << ". " << msg << - ". Node object hash " << to_string(hash); + auto fail = [j = j_, index = index_, &hash, &nObj](std::string const& msg) { + JLOG(j.fatal()) << "shard " << index << ". " << msg + << ". Node object hash " << to_string(hash); nObj.reset(); return nObj; }; @@ -1063,25 +1004,26 @@ Shard::valFetch(uint256 const& hash) const { switch (backend_->fetch(hash.data(), &nObj)) { - case ok: - // This verifies that the hash of node object matches the payload - if (nObj->getHash() != sha512Half(makeSlice(nObj->getData()))) - return fail("Node object hash does not match payload"); - return nObj; - case notFound: - return fail("Missing node object"); - case dataCorrupt: - return fail("Corrupt node object"); - default: - return fail("Unknown error"); + case ok: + // This verifies that the hash of node object matches the + // payload + if (nObj->getHash() != sha512Half(makeSlice(nObj->getData()))) + return fail("Node object hash does not match payload"); + return nObj; + case notFound: + return fail("Missing node object"); + case dataCorrupt: + return fail("Corrupt node object"); + default: + return fail("Unknown error"); } } catch (std::exception const& e) { - return fail(std::string("exception ") + - e.what() + " in function " + __func__); + return fail( + std::string("exception ") + e.what() + " in function " + __func__); } } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/Shard.h b/src/ripple/nodestore/impl/Shard.h index 6bdf89c4ca..769a9d52cc 100644 --- a/src/ripple/nodestore/impl/Shard.h +++ b/src/ripple/nodestore/impl/Shard.h @@ -75,10 +75,16 @@ public: sweep(); std::uint32_t - index() const {return index_;} + index() const + { + return index_; + } boost::filesystem::path const& - getDir() const {return dir_;} + getDir() const + { + return dir_; + } std::tuple< std::shared_ptr, @@ -90,7 +96,7 @@ public: getBackend() const; /** Returns `true` if all shard ledgers have been stored in the backend - */ + */ bool isBackendComplete() const; @@ -107,12 +113,12 @@ public: fileInfo() const; /** Returns `true` if the shard is complete, validated, and immutable. - */ + */ bool isFinal() const; /** Returns `true` if the shard is older, without final key data - */ + */ bool isLegacy() const; @@ -125,16 +131,22 @@ public: finalize(const bool writeSQLite); void - stop() {stop_ = true;} + stop() + { + stop_ = true; + } /** If called, the shard directory will be removed when the shard is destroyed. */ void - removeOnDestroy() {removeOnDestroy_ = true;} + removeOnDestroy() + { + removeOnDestroy_ = true; + } // Current shard version - static constexpr std::uint32_t version {2}; + static constexpr std::uint32_t version{2}; // The finalKey is a hard coded value of zero. It is used to store // finalizing shard data to the backend. The data contains a version, @@ -177,10 +189,10 @@ private: boost::filesystem::path const dir_; // Storage space utilized by the shard - std::uint64_t fileSz_ {0}; + std::uint64_t fileSz_{0}; // Number of file descriptors required by the shard - std::uint32_t fdRequired_ {0}; + std::uint32_t fdRequired_{0}; // NuDB key/value store for node objects std::shared_ptr backend_; @@ -198,20 +210,20 @@ private: beast::Journal const j_; // True if backend has stored all ledgers pertaining to the shard - bool backendComplete_ {false}; + bool backendComplete_{false}; // Older shard without an acquire database or final key // Eventually there will be no need for this and should be removed - bool legacy_ {false}; + bool legacy_{false}; // True if the backend has a final key stored - bool final_ {false}; + bool final_{false}; // Determines if the shard needs to stop processing for shutdown - std::atomic stop_ {false}; + std::atomic stop_{false}; // Determines if the shard directory should be removed in the destructor - std::atomic removeOnDestroy_ {false}; + std::atomic removeOnDestroy_{false}; // Set the backend cache // Lock over mutex_ required diff --git a/src/ripple/nodestore/impl/TaskQueue.cpp b/src/ripple/nodestore/impl/TaskQueue.cpp index 1ee718679f..000b664b0b 100644 --- a/src/ripple/nodestore/impl/TaskQueue.cpp +++ b/src/ripple/nodestore/impl/TaskQueue.cpp @@ -40,7 +40,7 @@ TaskQueue::onStop() void TaskQueue::addTask(std::function task) { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; tasks_.emplace(std::move(task)); workers_.addTask(); @@ -52,7 +52,7 @@ TaskQueue::processTask(int instance) std::function task; { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; assert(!tasks_.empty()); task = std::move(tasks_.front()); @@ -62,5 +62,5 @@ TaskQueue::processTask(int instance) task(); } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple diff --git a/src/ripple/nodestore/impl/TaskQueue.h b/src/ripple/nodestore/impl/TaskQueue.h index ab53ea090e..9f2121a450 100644 --- a/src/ripple/nodestore/impl/TaskQueue.h +++ b/src/ripple/nodestore/impl/TaskQueue.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_NODESTORE_TASKQUEUE_H_INCLUDED #define RIPPLE_NODESTORE_TASKQUEUE_H_INCLUDED -#include #include +#include #include #include @@ -29,13 +29,10 @@ namespace ripple { namespace NodeStore { -class TaskQueue - : public Stoppable - , private Workers::Callback +class TaskQueue : public Stoppable, private Workers::Callback { public: - explicit - TaskQueue(Stoppable& parent); + explicit TaskQueue(Stoppable& parent); void onStop() override; @@ -56,7 +53,7 @@ private: processTask(int instance) override; }; -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/Tuning.h b/src/ripple/nodestore/impl/Tuning.h index ffa5b852d2..0c6aac19a9 100644 --- a/src/ripple/nodestore/impl/Tuning.h +++ b/src/ripple/nodestore/impl/Tuning.h @@ -23,19 +23,19 @@ namespace ripple { namespace NodeStore { -enum -{ +enum { // Target cache size of the TaggedCache used to hold nodes - cacheTargetSize = 16384 + cacheTargetSize = 16384 // Fraction of the cache one query source can take - ,asyncDivider = 8 + , + asyncDivider = 8 }; // Expiration time for cached nodes std::chrono::seconds constexpr cacheTargetAge = std::chrono::minutes{5}; -} -} +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/codec.h b/src/ripple/nodestore/impl/codec.h index 27d0d5478c..15d5f90926 100644 --- a/src/ripple/nodestore/impl/codec.h +++ b/src/ripple/nodestore/impl/codec.h @@ -25,13 +25,13 @@ #include #include -#include -#include #include +#include #include -#include #include #include +#include +#include #include #include @@ -40,55 +40,47 @@ namespace NodeStore { template std::pair -lz4_decompress (void const* in, - std::size_t in_size, BufferFactory&& bf) +lz4_decompress(void const* in, std::size_t in_size, BufferFactory&& bf) { using std::runtime_error; using namespace nudb::detail; std::pair result; - std::uint8_t const* p = reinterpret_cast< - std::uint8_t const*>(in); - auto const n = read_varint( - p, in_size, result.second); + std::uint8_t const* p = reinterpret_cast(in); + auto const n = read_varint(p, in_size, result.second); if (n == 0) - Throw ( - "lz4 decompress: n == 0"); + Throw("lz4 decompress: n == 0"); void* const out = bf(result.second); result.first = out; if (LZ4_decompress_fast( - reinterpret_cast(in) + n, + reinterpret_cast(in) + n, reinterpret_cast(out), - result.second) + n != in_size) - Throw ( - "lz4 decompress: LZ4_decompress_fast"); + result.second) + + n != + in_size) + Throw("lz4 decompress: LZ4_decompress_fast"); return result; } template std::pair -lz4_compress (void const* in, - std::size_t in_size, BufferFactory&& bf) +lz4_compress(void const* in, std::size_t in_size, BufferFactory&& bf) { using std::runtime_error; using namespace nudb::detail; std::pair result; - std::array::max> vi; - auto const n = write_varint( - vi.data(), in_size); - auto const out_max = - LZ4_compressBound(in_size); - std::uint8_t* out = reinterpret_cast< - std::uint8_t*>(bf(n + out_max)); + std::array::max> vi; + auto const n = write_varint(vi.data(), in_size); + auto const out_max = LZ4_compressBound(in_size); + std::uint8_t* out = reinterpret_cast(bf(n + out_max)); result.first = out; std::memcpy(out, vi.data(), n); auto const out_size = LZ4_compress_default( reinterpret_cast(in), - reinterpret_cast(out + n), - in_size, out_max); + reinterpret_cast(out + n), + in_size, + out_max); if (out_size == 0) - Throw ( - "lz4 compress"); + Throw("lz4 compress"); result.second = n + out_size; return result; } @@ -106,109 +98,103 @@ lz4_compress (void const* in, template std::pair -nodeobject_decompress (void const* in, - std::size_t in_size, BufferFactory&& bf) +nodeobject_decompress(void const* in, std::size_t in_size, BufferFactory&& bf) { using namespace nudb::detail; - std::uint8_t const* p = reinterpret_cast< - std::uint8_t const*>(in); + std::uint8_t const* p = reinterpret_cast(in); std::size_t type; - auto const vn = read_varint( - p, in_size, type); + auto const vn = read_varint(p, in_size, type); if (vn == 0) - Throw ( - "nodeobject decompress"); + Throw("nodeobject decompress"); p += vn; in_size -= vn; std::pair result; - switch(type) + switch (type) { - case 0: // uncompressed - { - result.first = p; - result.second = in_size; - break; - } - case 1: // lz4 - { - result = lz4_decompress( - p, in_size, bf); - break; - } - case 2: // compressed v1 inner node - { - auto const hs = - field::size; // Mask - if (in_size < hs + 32) - Throw ( - "nodeobject codec v1: short inner node size: " - + std::string("in_size = ") + std::to_string(in_size) - + " hs = " + std::to_string(hs)); - istream is(p, in_size); - std::uint16_t mask; - read(is, mask); // Mask - in_size -= hs; - result.second = 525; - void* const out = bf(result.second); - result.first = out; - ostream os(out, result.second); - write(os, 0); - write(os, 0); - write (os, hotUNKNOWN); - write(os, - static_cast(HashPrefix::innerNode)); - if (mask == 0) - Throw ( - "nodeobject codec v1: empty inner node"); - std::uint16_t bit = 0x8000; - for (int i = 16; i--; bit >>= 1) + case 0: // uncompressed { - if (mask & bit) - { - if (in_size < 32) - Throw ( - "nodeobject codec v1: short inner node subsize: " - + std::string("in_size = ") + std::to_string(in_size) - + " i = " + std::to_string(i)); - std::memcpy(os.data(32), is(32), 32); - in_size -= 32; - } - else - { - std::memset(os.data(32), 0, 32); - } + result.first = p; + result.second = in_size; + break; } - if (in_size > 0) - Throw ( - "nodeobject codec v1: long inner node, in_size = " - + std::to_string(in_size)); - break; - } - case 3: // full v1 inner node - { - if (in_size != 16 * 32) // hashes - Throw ( - "nodeobject codec v1: short full inner node, in_size = " - + std::to_string(in_size)); - istream is(p, in_size); - result.second = 525; - void* const out = bf(result.second); - result.first = out; - ostream os(out, result.second); - write(os, 0); - write(os, 0); - write (os, hotUNKNOWN); - write(os, - static_cast(HashPrefix::innerNode)); - write(os, is(512), 512); - break; - } - default: - Throw ( - "nodeobject codec: bad type=" + - std::to_string(type)); + case 1: // lz4 + { + result = lz4_decompress(p, in_size, bf); + break; + } + case 2: // compressed v1 inner node + { + auto const hs = field::size; // Mask + if (in_size < hs + 32) + Throw( + "nodeobject codec v1: short inner node size: " + + std::string("in_size = ") + std::to_string(in_size) + + " hs = " + std::to_string(hs)); + istream is(p, in_size); + std::uint16_t mask; + read(is, mask); // Mask + in_size -= hs; + result.second = 525; + void* const out = bf(result.second); + result.first = out; + ostream os(out, result.second); + write(os, 0); + write(os, 0); + write(os, hotUNKNOWN); + write( + os, static_cast(HashPrefix::innerNode)); + if (mask == 0) + Throw( + "nodeobject codec v1: empty inner node"); + std::uint16_t bit = 0x8000; + for (int i = 16; i--; bit >>= 1) + { + if (mask & bit) + { + if (in_size < 32) + Throw( + "nodeobject codec v1: short inner node subsize: " + + std::string("in_size = ") + + std::to_string(in_size) + + " i = " + std::to_string(i)); + std::memcpy(os.data(32), is(32), 32); + in_size -= 32; + } + else + { + std::memset(os.data(32), 0, 32); + } + } + if (in_size > 0) + Throw( + "nodeobject codec v1: long inner node, in_size = " + + std::to_string(in_size)); + break; + } + case 3: // full v1 inner node + { + if (in_size != 16 * 32) // hashes + Throw( + "nodeobject codec v1: short full inner node, in_size = " + + std::to_string(in_size)); + istream is(p, in_size); + result.second = 525; + void* const out = bf(result.second); + result.first = out; + ostream os(out, result.second); + write(os, 0); + write(os, 0); + write(os, hotUNKNOWN); + write( + os, static_cast(HashPrefix::innerNode)); + write(os, is(512), 512); + break; + } + default: + Throw( + "nodeobject codec: bad type=" + std::to_string(type)); }; return result; } @@ -223,8 +209,7 @@ zero32() template std::pair -nodeobject_compress (void const* in, - std::size_t in_size, BufferFactory&& bf) +nodeobject_compress(void const* in, std::size_t in_size, BufferFactory&& bf) { using std::runtime_error; using namespace nudb::detail; @@ -235,42 +220,36 @@ nodeobject_compress (void const* in, istream is(in, in_size); std::uint32_t index; std::uint32_t unused; - std::uint8_t kind; + std::uint8_t kind; std::uint32_t prefix; read(is, index); read(is, unused); - read (is, kind); + read(is, kind); read(is, prefix); if (safe_cast(prefix) == HashPrefix::innerNode) { std::size_t n = 0; std::uint16_t mask = 0; - std::array< - std::uint8_t, 512> vh; - for (unsigned bit = 0x8000; - bit; bit >>= 1) + std::array vh; + for (unsigned bit = 0x8000; bit; bit >>= 1) { void const* const h = is(32); if (std::memcmp(h, zero32(), 32) == 0) continue; - std::memcpy( - vh.data() + 32 * n, h, 32); + std::memcpy(vh.data() + 32 * n, h, 32); mask |= bit; ++n; } - std::pair result; + std::pair result; if (n < 16) { // 2 = v1 inner node compressed auto const type = 2U; auto const vs = size_varint(type); - result.second = - vs + - field::size + // mask - n * 32; // hashes - std::uint8_t* out = reinterpret_cast< - std::uint8_t*>(bf(result.second)); + result.second = vs + field::size + // mask + n * 32; // hashes + std::uint8_t* out = + reinterpret_cast(bf(result.second)); result.first = out; ostream os(out, result.second); write(os, type); @@ -281,11 +260,9 @@ nodeobject_compress (void const* in, // 3 = full v1 inner node auto const type = 3U; auto const vs = size_varint(type); - result.second = - vs + - n * 32; // hashes - std::uint8_t* out = reinterpret_cast< - std::uint8_t*>(bf(result.second)); + result.second = vs + n * 32; // hashes + std::uint8_t* out = + reinterpret_cast(bf(result.second)); result.first = out; ostream os(out, result.second); write(os, type); @@ -294,37 +271,30 @@ nodeobject_compress (void const* in, } } - std::array::max> vi; + std::array::max> vi; constexpr std::size_t codecType = 1; - auto const vn = write_varint( - vi.data(), codecType); + auto const vn = write_varint(vi.data(), codecType); std::pair result; - switch(codecType) + switch (codecType) { - // case 0 was uncompressed data; we always compress now. - case 1: // lz4 - { - std::uint8_t* p; - auto const lzr = NodeStore::lz4_compress( - in, in_size, [&p, &vn, &bf] - (std::size_t n) - { - p = reinterpret_cast< - std::uint8_t*>( - bf(vn + n)); - return p + vn; - }); - std::memcpy(p, vi.data(), vn); - result.first = p; - result.second = vn + lzr.second; - break; - } - default: - Throw ( - "nodeobject codec: unknown=" + - std::to_string(codecType)); + // case 0 was uncompressed data; we always compress now. + case 1: // lz4 + { + std::uint8_t* p; + auto const lzr = NodeStore::lz4_compress( + in, in_size, [&p, &vn, &bf](std::size_t n) { + p = reinterpret_cast(bf(vn + n)); + return p + vn; + }); + std::memcpy(p, vi.data(), vn); + result.first = p; + result.second = vn + lzr.second; + break; + } + default: + Throw( + "nodeobject codec: unknown=" + std::to_string(codecType)); }; return result; } @@ -335,7 +305,7 @@ nodeobject_compress (void const* in, // template void -filter_inner (void* in, std::size_t in_size) +filter_inner(void* in, std::size_t in_size) { using namespace nudb::detail; @@ -345,23 +315,23 @@ filter_inner (void* in, std::size_t in_size) istream is(in, in_size); std::uint32_t index; std::uint32_t unused; - std::uint8_t kind; + std::uint8_t kind; std::uint32_t prefix; read(is, index); read(is, unused); - read (is, kind); + read(is, kind); read(is, prefix); if (safe_cast(prefix) == HashPrefix::innerNode) { ostream os(in, 9); write(os, 0); write(os, 0); - write (os, hotUNKNOWN); + write(os, hotUNKNOWN); } } } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/nodestore/impl/varint.h b/src/ripple/nodestore/impl/varint.h index 41e7bbddf3..e74f53afd5 100644 --- a/src/ripple/nodestore/impl/varint.h +++ b/src/ripple/nodestore/impl/varint.h @@ -20,8 +20,8 @@ #ifndef BEAST_NUDB_VARINT_H_INCLUDED #define BEAST_NUDB_VARINT_H_INCLUDED -#include #include +#include #include namespace ripple { @@ -37,8 +37,7 @@ struct varint; // Metafuncton to return largest // possible size of T represented as varint. // T must be unsigned -template ::value> +template ::value> struct varint_traits; template @@ -46,8 +45,7 @@ struct varint_traits { explicit varint_traits() = default; - static std::size_t constexpr max = - (8 * sizeof(T) + 6) / 7; + static std::size_t constexpr max = (8 * sizeof(T) + 6) / 7; }; // Returns: Number of bytes consumed or 0 on error, @@ -55,13 +53,10 @@ struct varint_traits // template std::size_t -read_varint (void const* buf, - std::size_t buflen, std::size_t& t) +read_varint(void const* buf, std::size_t buflen, std::size_t& t) { t = 0; - std::uint8_t const* p = - reinterpret_cast< - std::uint8_t const*>(buf); + std::uint8_t const* p = reinterpret_cast(buf); std::size_t n = 0; while (p[n] & 0x80) if (++n >= buflen) @@ -82,53 +77,45 @@ read_varint (void const* buf, t *= 127; t += d & 0x7f; if (t <= t0) - return 0; // overflow + return 0; // overflow } return used; } -template ::value>* = nullptr> +template ::value>* = nullptr> std::size_t -size_varint (T v) +size_varint(T v) { std::size_t n = 0; do { v /= 127; ++n; - } - while (v != 0); + } while (v != 0); return n; } template std::size_t -write_varint (void* p0, std::size_t v) +write_varint(void* p0, std::size_t v) { - std::uint8_t* p = reinterpret_cast< - std::uint8_t*>(p0); + std::uint8_t* p = reinterpret_cast(p0); do { - std::uint8_t d = - v % 127; + std::uint8_t d = v % 127; v /= 127; if (v != 0) d |= 0x80; *p++ = d; - } - while (v != 0); - return p - reinterpret_cast< - std::uint8_t*>(p0); + } while (v != 0); + return p - reinterpret_cast(p0); } // input stream -template ::value>* = nullptr> +template ::value>* = nullptr> void -read (nudb::detail::istream& is, std::size_t& u) +read(nudb::detail::istream& is, std::size_t& u) { auto p0 = is(1); auto p1 = p0; @@ -139,16 +126,14 @@ read (nudb::detail::istream& is, std::size_t& u) // output stream -template ::value>* = nullptr> +template ::value>* = nullptr> void -write (nudb::detail::ostream& os, std::size_t t) +write(nudb::detail::ostream& os, std::size_t t) { - write_varint(os.data( - size_varint(t)), t); + write_varint(os.data(size_varint(t)), t); } -} // NodeStore -} // ripple +} // namespace NodeStore +} // namespace ripple #endif diff --git a/src/ripple/overlay/Cluster.h b/src/ripple/overlay/Cluster.h index b65cfa2202..687409f802 100644 --- a/src/ripple/overlay/Cluster.h +++ b/src/ripple/overlay/Cluster.h @@ -21,12 +21,12 @@ #define RIPPLE_OVERLAY_CLUSTER_H_INCLUDED #include -#include #include -#include -#include +#include #include #include +#include +#include #include #include #include @@ -45,25 +45,19 @@ private: using is_transparent = std::true_type; bool - operator() ( - ClusterNode const& lhs, - ClusterNode const& rhs) const + operator()(ClusterNode const& lhs, ClusterNode const& rhs) const { return lhs.identity() < rhs.identity(); } bool - operator() ( - ClusterNode const& lhs, - PublicKey const& rhs) const + operator()(ClusterNode const& lhs, PublicKey const& rhs) const { return lhs.identity() < rhs; } bool - operator() ( - PublicKey const& lhs, - ClusterNode const& rhs) const + operator()(PublicKey const& lhs, ClusterNode const& rhs) const { return lhs < rhs.identity(); } @@ -74,7 +68,7 @@ private: beast::Journal mutable j_; public: - Cluster (beast::Journal j); + Cluster(beast::Journal j); /** Determines whether a node belongs in the cluster @return boost::none if the node isn't a member, @@ -82,7 +76,7 @@ public: node (which may be an empty string). */ boost::optional - member (PublicKey const& node) const; + member(PublicKey const& node) const; /** The number of nodes in the cluster list. */ std::size_t @@ -94,7 +88,7 @@ public: @return true if we updated our information */ bool - update ( + update( PublicKey const& identity, std::string name, std::uint32_t loadFee = 0, @@ -105,8 +99,7 @@ public: within the callback. */ void - for_each ( - std::function func) const; + for_each(std::function func) const; /** Load the list of cluster nodes. @@ -119,9 +112,9 @@ public: true otherwise. */ bool - load (Section const& nodes); + load(Section const& nodes); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/ClusterNode.h b/src/ripple/overlay/ClusterNode.h index 156d934d50..7996f8ffd2 100644 --- a/src/ripple/overlay/ClusterNode.h +++ b/src/ripple/overlay/ClusterNode.h @@ -33,33 +33,34 @@ public: ClusterNode() = delete; ClusterNode( - PublicKey const& identity, - std::string const& name, - std::uint32_t fee = 0, - NetClock::time_point rtime = NetClock::time_point{}) - : identity_ (identity) - , name_(name) - , mLoadFee(fee) - , mReportTime(rtime) - { } + PublicKey const& identity, + std::string const& name, + std::uint32_t fee = 0, + NetClock::time_point rtime = NetClock::time_point{}) + : identity_(identity), name_(name), mLoadFee(fee), mReportTime(rtime) + { + } - std::string const& name() const + std::string const& + name() const { return name_; } - std::uint32_t getLoadFee() const + std::uint32_t + getLoadFee() const { return mLoadFee; } - NetClock::time_point getReportTime() const + NetClock::time_point + getReportTime() const { return mReportTime; } PublicKey const& - identity () const + identity() const { return identity_; } @@ -71,6 +72,6 @@ private: NetClock::time_point mReportTime = {}; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/Compression.h b/src/ripple/overlay/Compression.h index efec6b524e..5d45dbda88 100644 --- a/src/ripple/overlay/Compression.h +++ b/src/ripple/overlay/Compression.h @@ -31,15 +31,9 @@ namespace compression { std::size_t constexpr headerBytes = 6; std::size_t constexpr headerBytesCompressed = 10; -enum class Algorithm : std::uint8_t { - None = 0x00, - LZ4 = 0x01 -}; +enum class Algorithm : std::uint8_t { None = 0x00, LZ4 = 0x01 }; -enum class Compressed : std::uint8_t { - On, - Off -}; +enum class Compressed : std::uint8_t { On, Off }; /** Decompress input stream. * @tparam InputStream ZeroCopyInputStream @@ -49,23 +43,31 @@ enum class Compressed : std::uint8_t { * @param algorithm Compression algorithm type * @return Size of decompressed data or zero if failed to decompress */ -template +template std::size_t -decompress(InputStream& in, std::size_t inSize, std::uint8_t* decompressed, - std::size_t decompressedSize, Algorithm algorithm = Algorithm::LZ4) { +decompress( + InputStream& in, + std::size_t inSize, + std::uint8_t* decompressed, + std::size_t decompressedSize, + Algorithm algorithm = Algorithm::LZ4) +{ try { if (algorithm == Algorithm::LZ4) - return ripple::compression_algorithms::lz4Decompress(in, inSize, - decompressed, decompressedSize); + return ripple::compression_algorithms::lz4Decompress( + in, inSize, decompressed, decompressedSize); else { - JLOG(debugLog().warn()) << "decompress: invalid compression algorithm " - << static_cast(algorithm); + JLOG(debugLog().warn()) + << "decompress: invalid compression algorithm " + << static_cast(algorithm); assert(0); } } - catch (...) {} + catch (...) + { + } return 0; } @@ -78,14 +80,19 @@ decompress(InputStream& in, std::size_t inSize, std::uint8_t* decompressed, * @param algorithm Compression algorithm type * @return Size of compressed data, or zero if failed to compress */ -template +template std::size_t -compress(void const* in, - std::size_t inSize, BufferFactory&& bf, Algorithm algorithm = Algorithm::LZ4) { +compress( + void const* in, + std::size_t inSize, + BufferFactory&& bf, + Algorithm algorithm = Algorithm::LZ4) +{ try { if (algorithm == Algorithm::LZ4) - return ripple::compression_algorithms::lz4Compress(in, inSize, std::forward(bf)); + return ripple::compression_algorithms::lz4Compress( + in, inSize, std::forward(bf)); else { JLOG(debugLog().warn()) << "compress: invalid compression algorithm" @@ -93,11 +100,13 @@ compress(void const* in, assert(0); } } - catch (...) {} + catch (...) + { + } return 0; } -} // compression +} // namespace compression -} // ripple +} // namespace ripple -#endif //RIPPLED_COMPRESSION_H_INCLUDED +#endif // RIPPLED_COMPRESSION_H_INCLUDED diff --git a/src/ripple/overlay/Message.h b/src/ripple/overlay/Message.h index b3ae036a50..af7a168ad0 100644 --- a/src/ripple/overlay/Message.h +++ b/src/ripple/overlay/Message.h @@ -34,8 +34,8 @@ namespace ripple { // VFALCO NOTE If we forward declare Message and write out shared_ptr -// instead of using the in-class type alias, we can remove the entire -// ripple.pb.h from the main headers. +// instead of using the in-class type alias, we can remove the +// entire ripple.pb.h from the main headers. // // packaging of messages into length/type-prepended buffers @@ -46,36 +46,37 @@ namespace ripple { // MessageType should be a Message class generated by the protobuf compiler. // -class Message : public std::enable_shared_from_this +class Message : public std::enable_shared_from_this { using Compressed = compression::Compressed; using Algorithm = compression::Algorithm; + public: /** Constructor * @param message Protocol message to serialize * @param type Protocol message type */ - Message (::google::protobuf::Message const& message, int type); + Message(::google::protobuf::Message const& message, int type); - /** Retrieve the packed message data. If compressed message is requested but the message - * is not compressible then the uncompressed buffer is returned. + /** Retrieve the packed message data. If compressed message is requested but + * the message is not compressible then the uncompressed buffer is returned. * @param compressed Request compressed (Compress::On) or * uncompressed (Compress::Off) payload buffer * @return Payload buffer */ - std::vector const& - getBuffer (Compressed tryCompressed); + std::vector const& + getBuffer(Compressed tryCompressed); /** Get the traffic category */ std::size_t - getCategory () const + getCategory() const { return category_; } private: - std::vector buffer_; - std::vector bufferCompressed_; + std::vector buffer_; + std::vector bufferCompressed_; std::size_t category_; std::once_flag once_flag_; @@ -87,14 +88,20 @@ private: * currently LZ4 only. If None then the message is uncompressed. * @param uncompressedBytes Size of the uncompressed message */ - void setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type, - Algorithm comprAlgorithm, std::uint32_t uncompressedBytes); + void + setHeader( + std::uint8_t* in, + std::uint32_t payloadBytes, + int type, + Algorithm comprAlgorithm, + std::uint32_t uncompressedBytes); /** Try to compress the payload. * Can be called concurrently by multiple peers but is compressed once. * If the message is not compressible then the serialized buffer_ is used. */ - void compress(); + void + compress(); /** Get the message type from the payload header. * First four bytes are the compression/algorithm flag and the payload size. @@ -102,9 +109,10 @@ private: * @param in Payload header pointer * @return Message type */ - int getType(std::uint8_t const* in) const; + int + getType(std::uint8_t const* in) const; }; -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/Overlay.h b/src/ripple/overlay/Overlay.h index 1683092324..34498c0ed7 100644 --- a/src/ripple/overlay/Overlay.h +++ b/src/ripple/overlay/Overlay.h @@ -20,55 +20,52 @@ #ifndef RIPPLE_OVERLAY_OVERLAY_H_INCLUDED #define RIPPLE_OVERLAY_OVERLAY_H_INCLUDED +#include +#include #include #include #include #include +#include #include #include #include -#include #include +#include #include -#include -#include -#include -#include -#include -#include #include #include +#include +#include -namespace boost { namespace asio { namespace ssl { class context; } } } +namespace boost { +namespace asio { +namespace ssl { +class context; +} +} // namespace asio +} // namespace boost namespace ripple { /** Manages the set of connected peers. */ -class Overlay - : public Stoppable - , public beast::PropertyStream::Source +class Overlay : public Stoppable, public beast::PropertyStream::Source { protected: using socket_type = boost::beast::tcp_stream; - using stream_type = boost::beast::ssl_stream ; + using stream_type = boost::beast::ssl_stream; // VFALCO NOTE The requirement of this constructor is an // unfortunate problem with the API for // Stoppable and PropertyStream // - Overlay (Stoppable& parent) - : Stoppable ("Overlay", parent) - , beast::PropertyStream::Source ("peers") + Overlay(Stoppable& parent) + : Stoppable("Overlay", parent), beast::PropertyStream::Source("peers") { } public: - enum class Promote - { - automatic, - never, - always - }; + enum class Promote { automatic, never, always }; struct Setup { @@ -83,96 +80,81 @@ public: bool vlEnabled = true; }; - using PeerSequence = std::vector >; + using PeerSequence = std::vector>; virtual ~Overlay() = default; /** Conditionally accept an incoming HTTP request. */ - virtual - Handoff - onHandoff (std::unique_ptr && bundle, + virtual Handoff + onHandoff( + std::unique_ptr&& bundle, http_request_type&& request, - boost::asio::ip::tcp::endpoint remote_address) = 0; + boost::asio::ip::tcp::endpoint remote_address) = 0; /** Establish a peer connection to the specified endpoint. The call returns immediately, the connection attempt is performed asynchronously. */ - virtual - void - connect (beast::IP::Endpoint const& address) = 0; + virtual void + connect(beast::IP::Endpoint const& address) = 0; /** Returns the maximum number of peers we are configured to allow. */ - virtual - int - limit () = 0; + virtual int + limit() = 0; /** Returns the number of active peers. Active peers are only those peers that have completed the handshake and are using the peer protocol. */ - virtual - std::size_t - size () = 0; + virtual std::size_t + size() = 0; /** Return diagnostics on the status of all peers. @deprecated This is superceded by PropertyStream */ - virtual - Json::Value - json () = 0; + virtual Json::Value + json() = 0; /** Returns a sequence representing the current list of peers. The snapshot is made at the time of the call. */ - virtual - PeerSequence - getActivePeers () = 0; + virtual PeerSequence + getActivePeers() = 0; /** Calls the checkSanity function on each peer @param index the value to pass to the peer's checkSanity function */ - virtual - void - checkSanity (std::uint32_t index) = 0; + virtual void + checkSanity(std::uint32_t index) = 0; /** Calls the check function on each peer - */ - virtual - void - check () = 0; + */ + virtual void + check() = 0; /** Returns the peer with the matching short id, or null. */ - virtual - std::shared_ptr - findPeerByShortID (Peer::id_t const& id) = 0; + virtual std::shared_ptr + findPeerByShortID(Peer::id_t const& id) = 0; /** Returns the peer with the matching public key, or null. */ - virtual - std::shared_ptr - findPeerByPublicKey (PublicKey const& pubKey) = 0; + virtual std::shared_ptr + findPeerByPublicKey(PublicKey const& pubKey) = 0; /** Broadcast a proposal. */ - virtual - void - send (protocol::TMProposeSet& m) = 0; + virtual void + send(protocol::TMProposeSet& m) = 0; /** Broadcast a validation. */ - virtual - void - send (protocol::TMValidation& m) = 0; + virtual void + send(protocol::TMValidation& m) = 0; /** Relay a proposal. */ - virtual - void - relay (protocol::TMProposeSet& m, - uint256 const& uid) = 0; + virtual void + relay(protocol::TMProposeSet& m, uint256 const& uid) = 0; /** Relay a validation. */ - virtual - void - relay (protocol::TMValidation& m, - uint256 const& uid) = 0; + virtual void + relay(protocol::TMValidation& m, uint256 const& uid) = 0; /** Visit every active peer and return a value The functor must: @@ -189,13 +171,12 @@ public: @note The functor is passed by value! */ template - std::enable_if_t::value, - typename UnaryFunc::return_type> - foreach (UnaryFunc f) + std::enable_if_t< + !std::is_void::value, + typename UnaryFunc::return_type> foreach (UnaryFunc f) { for (auto const& p : getActivePeers()) - f (p); + f(p); return f(); } @@ -209,14 +190,12 @@ public: @param f the functor to call with every peer */ template - std::enable_if_t < - std::is_void ::value, - typename Function::return_type - > - foreach(Function f) + std::enable_if_t< + std::is_void::value, + typename Function::return_type> foreach (Function f) { for (auto const& p : getActivePeers()) - f (p); + f(p); } /** Select from active peers @@ -236,30 +215,36 @@ public: - Return a true if the peer is accepted */ - virtual - std::size_t - selectPeers (PeerSet& set, std::size_t limit, std::function< - bool(std::shared_ptr const&)> score) = 0; + virtual std::size_t + selectPeers( + PeerSet& set, + std::size_t limit, + std::function const&)> score) = 0; /** Increment and retrieve counter for transaction job queue overflows. */ - virtual void incJqTransOverflow() = 0; - virtual std::uint64_t getJqTransOverflow() const = 0; + virtual void + incJqTransOverflow() = 0; + virtual std::uint64_t + getJqTransOverflow() const = 0; /** Increment and retrieve counters for total peer disconnects, and * disconnects we initiate for excessive resource consumption. - */ - virtual void incPeerDisconnect() = 0; - virtual std::uint64_t getPeerDisconnect() const = 0; - virtual void incPeerDisconnectCharges() = 0; - virtual std::uint64_t getPeerDisconnectCharges() const = 0; + */ + virtual void + incPeerDisconnect() = 0; + virtual std::uint64_t + getPeerDisconnect() const = 0; + virtual void + incPeerDisconnectCharges() = 0; + virtual std::uint64_t + getPeerDisconnectCharges() const = 0; /** Returns information reported to the crawl shard RPC command. @param hops the maximum jumps the crawler will attempt. The number of hops achieved is not guaranteed. */ - virtual - Json::Value + virtual Json::Value crawlShards(bool pubKey, std::uint32_t hops) = 0; /** Returns the ID of the network this server is configured for, if any. @@ -270,8 +255,7 @@ public: @return The numerical identifier configured by the administrator of the server. An unseated optional, otherwise. */ - virtual - boost::optional + virtual boost::optional networkID() const = 0; }; @@ -279,22 +263,26 @@ struct ScoreHasLedger { uint256 const& hash_; std::uint32_t seq_; - bool operator()(std::shared_ptr const&) const; + bool + operator()(std::shared_ptr const&) const; - ScoreHasLedger (uint256 const& hash, std::uint32_t seq) - : hash_ (hash), seq_ (seq) - {} + ScoreHasLedger(uint256 const& hash, std::uint32_t seq) + : hash_(hash), seq_(seq) + { + } }; struct ScoreHasTxSet { uint256 const& hash_; - bool operator()(std::shared_ptr const&) const; + bool + operator()(std::shared_ptr const&) const; - ScoreHasTxSet (uint256 const& hash) : hash_ (hash) - {} + ScoreHasTxSet(uint256 const& hash) : hash_(hash) + { + } }; -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/Peer.h b/src/ripple/overlay/Peer.h index 113d4802ed..d16433c1d0 100644 --- a/src/ripple/overlay/Peer.h +++ b/src/ripple/overlay/Peer.h @@ -20,11 +20,11 @@ #ifndef RIPPLE_OVERLAY_PEER_H_INCLUDED #define RIPPLE_OVERLAY_PEER_H_INCLUDED -#include #include -#include -#include #include +#include +#include +#include namespace ripple { @@ -58,71 +58,68 @@ public: // Network // - virtual - void - send (std::shared_ptr const& m) = 0; + virtual void + send(std::shared_ptr const& m) = 0; - virtual - beast::IP::Endpoint + virtual beast::IP::Endpoint getRemoteAddress() const = 0; /** Adjust this peer's load balance based on the type of load imposed. */ - virtual - void - charge (Resource::Charge const& fee) = 0; + virtual void + charge(Resource::Charge const& fee) = 0; // // Identity // - virtual - id_t + virtual id_t id() const = 0; /** Returns `true` if this connection is a member of the cluster. */ - virtual - bool + virtual bool cluster() const = 0; - virtual - bool + virtual bool isHighLatency() const = 0; - virtual - int - getScore (bool) const = 0; + virtual int + getScore(bool) const = 0; - virtual - PublicKey const& + virtual PublicKey const& getNodePublic() const = 0; - virtual - Json::Value json() = 0; + virtual Json::Value + json() = 0; virtual bool supportsFeature(ProtocolFeature f) const = 0; - virtual - boost::optional + virtual boost::optional publisherListSequence(PublicKey const&) const = 0; - virtual - void + virtual void setPublisherListSequence(PublicKey const&, std::size_t const) = 0; // // Ledger // - virtual uint256 const& getClosedLedgerHash () const = 0; - virtual bool hasLedger (uint256 const& hash, std::uint32_t seq) const = 0; - virtual void ledgerRange (std::uint32_t& minSeq, std::uint32_t& maxSeq) const = 0; - virtual bool hasShard (std::uint32_t shardIndex) const = 0; - virtual bool hasTxSet (uint256 const& hash) const = 0; - virtual void cycleStatus () = 0; - virtual bool hasRange (std::uint32_t uMin, std::uint32_t uMax) = 0; + virtual uint256 const& + getClosedLedgerHash() const = 0; + virtual bool + hasLedger(uint256 const& hash, std::uint32_t seq) const = 0; + virtual void + ledgerRange(std::uint32_t& minSeq, std::uint32_t& maxSeq) const = 0; + virtual bool + hasShard(std::uint32_t shardIndex) const = 0; + virtual bool + hasTxSet(uint256 const& hash) const = 0; + virtual void + cycleStatus() = 0; + virtual bool + hasRange(std::uint32_t uMin, std::uint32_t uMax) = 0; }; -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/PeerReservationTable.h b/src/ripple/overlay/PeerReservationTable.h index cb0819dcee..72d22b9a8d 100644 --- a/src/ripple/overlay/PeerReservationTable.h +++ b/src/ripple/overlay/PeerReservationTable.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED #define RIPPLE_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED -#include #include +#include #include #include #include @@ -44,19 +44,21 @@ struct PeerReservation final { public: PublicKey nodeId; - std::string description {}; + std::string description{}; auto toJson() const -> Json::Value; template - friend void hash_append(Hasher& h, PeerReservation const& x) noexcept + friend void + hash_append(Hasher& h, PeerReservation const& x) noexcept { using beast::hash_append; hash_append(h, x.nodeId); } - friend bool operator<(PeerReservation const& a, PeerReservation const& b) + friend bool + operator<(PeerReservation const& a, PeerReservation const& b) { return a.nodeId < b.nodeId; } @@ -67,8 +69,8 @@ public: // pass a `PublicKey` directly to `unordered_set.find`. struct KeyEqual final { - bool operator() ( - PeerReservation const& lhs, PeerReservation const& rhs) const + bool + operator()(PeerReservation const& lhs, PeerReservation const& rhs) const { return lhs.nodeId == rhs.nodeId; } @@ -83,7 +85,8 @@ public: { } - std::vector list() const; + std::vector + list() const; bool contains(PublicKey const& nodeId) diff --git a/src/ripple/overlay/PeerSet.h b/src/ripple/overlay/PeerSet.h index 0f20d667ec..8402e47719 100644 --- a/src/ripple/overlay/PeerSet.h +++ b/src/ripple/overlay/PeerSet.h @@ -48,46 +48,54 @@ namespace ripple { class PeerSet { public: - using clock_type = beast::abstract_clock ; + using clock_type = beast::abstract_clock; /** Returns the hash of the data we want. */ - uint256 const& getHash () const + uint256 const& + getHash() const { return mHash; } /** Returns true if we got all the data. */ - bool isComplete () const + bool + isComplete() const { return mComplete; } /** Returns false if we failed to get the data. */ - bool isFailed () const + bool + isFailed() const { return mFailed; } /** Returns the number of times we timed out. */ - int getTimeouts () const + int + getTimeouts() const { return mTimeouts; } - bool isActive (); + bool + isActive(); /** Called to indicate that forward progress has been made. */ - void progress () + void + progress() { mProgress = true; } - void touch () + void + touch() { mLastAction = m_clock.now(); } - clock_type::time_point getLastAction () const + clock_type::time_point + getLastAction() const { return mLastAction; } @@ -96,9 +104,11 @@ public: This will call the derived class hook function. @return `true` If the peer was added */ - bool insert (std::shared_ptr const&); + bool + insert(std::shared_ptr const&); - virtual bool isDone () const + virtual bool + isDone() const { return mComplete || mFailed; } @@ -110,44 +120,62 @@ public: } protected: - using ScopedLockType = std::unique_lock ; + using ScopedLockType = std::unique_lock; - PeerSet (Application& app, uint256 const& hash, std::chrono::milliseconds interval, - clock_type& clock, beast::Journal journal); + PeerSet( + Application& app, + uint256 const& hash, + std::chrono::milliseconds interval, + clock_type& clock, + beast::Journal journal); virtual ~PeerSet() = 0; - virtual void newPeer (std::shared_ptr const&) = 0; + virtual void + newPeer(std::shared_ptr const&) = 0; - virtual void onTimer (bool progress, ScopedLockType&) = 0; + virtual void + onTimer(bool progress, ScopedLockType&) = 0; - virtual void execute () = 0; + virtual void + execute() = 0; - virtual std::weak_ptr pmDowncast () = 0; + virtual std::weak_ptr + pmDowncast() = 0; - bool isProgress () + bool + isProgress() { return mProgress; } - void setComplete () + void + setComplete() { mComplete = true; } - void setFailed () + void + setFailed() { mFailed = true; } - void invokeOnTimer (); + void + invokeOnTimer(); - void sendRequest (const protocol::TMGetLedger& message); + void + sendRequest(const protocol::TMGetLedger& message); - void sendRequest (const protocol::TMGetLedger& message, std::shared_ptr const& peer); + void + sendRequest( + const protocol::TMGetLedger& message, + std::shared_ptr const& peer); - void setTimer (); + void + setTimer(); - std::size_t getPeerCount () const; + std::size_t + getPeerCount() const; protected: Application& app_; @@ -168,9 +196,9 @@ protected: boost::asio::basic_waitable_timer mTimer; // The identifiers of the peers we are tracking. - std::set mPeers; + std::set mPeers; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/Cluster.cpp b/src/ripple/overlay/impl/Cluster.cpp index ca7020d65e..08a543af23 100644 --- a/src/ripple/overlay/impl/Cluster.cpp +++ b/src/ripple/overlay/impl/Cluster.cpp @@ -31,20 +31,19 @@ namespace ripple { -Cluster::Cluster (beast::Journal j) - : j_ (j) +Cluster::Cluster(beast::Journal j) : j_(j) { } boost::optional -Cluster::member (PublicKey const& identity) const +Cluster::member(PublicKey const& identity) const { std::lock_guard lock(mutex_); - auto iter = nodes_.find (identity); - if (iter == nodes_.end ()) + auto iter = nodes_.find(identity); + if (iter == nodes_.end()) return boost::none; - return iter->name (); + return iter->name(); } std::size_t @@ -56,7 +55,7 @@ Cluster::size() const } bool -Cluster::update ( +Cluster::update( PublicKey const& identity, std::string name, std::uint32_t loadFee, @@ -66,10 +65,9 @@ Cluster::update ( // We can't use auto here yet due to the libstdc++ issue // described at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68190 - std::set::iterator iter = - nodes_.find (identity); + std::set::iterator iter = nodes_.find(identity); - if (iter != nodes_.end ()) + if (iter != nodes_.end()) { if (reportTime <= iter->getReportTime()) return false; @@ -77,62 +75,57 @@ Cluster::update ( if (name.empty()) name = iter->name(); - iter = nodes_.erase (iter); + iter = nodes_.erase(iter); } - nodes_.emplace_hint (iter, identity, name, loadFee, reportTime); + nodes_.emplace_hint(iter, identity, name, loadFee, reportTime); return true; } void -Cluster::for_each ( - std::function func) const +Cluster::for_each(std::function func) const { std::lock_guard lock(mutex_); for (auto const& ni : nodes_) - func (ni); + func(ni); } bool -Cluster::load (Section const& nodes) +Cluster::load(Section const& nodes) { - static boost::regex const re ( - "[[:space:]]*" // skip leading whitespace - "([[:alnum:]]+)" // node identity - "(?:" // begin optional comment block - "[[:space:]]+" // (skip all leading whitespace) - "(?:" // begin optional comment - "(.*[^[:space:]]+)" // the comment - "[[:space:]]*" // (skip all trailing whitespace) - ")?" // end optional comment - ")?" // end optional comment block + static boost::regex const re( + "[[:space:]]*" // skip leading whitespace + "([[:alnum:]]+)" // node identity + "(?:" // begin optional comment block + "[[:space:]]+" // (skip all leading whitespace) + "(?:" // begin optional comment + "(.*[^[:space:]]+)" // the comment + "[[:space:]]*" // (skip all trailing whitespace) + ")?" // end optional comment + ")?" // end optional comment block ); for (auto const& n : nodes.values()) { boost::smatch match; - if (!boost::regex_match (n, match, re)) + if (!boost::regex_match(n, match, re)) { - JLOG (j_.error()) << - "Malformed entry: '" << n << "'"; + JLOG(j_.error()) << "Malformed entry: '" << n << "'"; return false; } - auto const id = parseBase58( - TokenType::NodePublic, match[1]); + auto const id = parseBase58(TokenType::NodePublic, match[1]); if (!id) { - JLOG (j_.error()) << - "Invalid node identity: " << match[1]; + JLOG(j_.error()) << "Invalid node identity: " << match[1]; return false; } - if (member (*id)) + if (member(*id)) { - JLOG (j_.warn()) << - "Duplicate node identity: " << match[1]; + JLOG(j_.warn()) << "Duplicate node identity: " << match[1]; continue; } @@ -142,4 +135,4 @@ Cluster::load (Section const& nodes) return true; } -} // ripple +} // namespace ripple diff --git a/src/ripple/overlay/impl/ConnectAttempt.cpp b/src/ripple/overlay/impl/ConnectAttempt.cpp index 4cd38715fc..f980cdb129 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.cpp +++ b/src/ripple/overlay/impl/ConnectAttempt.cpp @@ -17,56 +17,59 @@ */ //============================================================================== +#include +#include #include #include #include -#include -#include namespace ripple { -ConnectAttempt::ConnectAttempt (Application& app, boost::asio::io_service& io_service, - endpoint_type const& remote_endpoint, Resource::Consumer usage, - shared_context const& context, - std::uint32_t id, std::shared_ptr const& slot, - beast::Journal journal, OverlayImpl& overlay) - : Child (overlay) - , app_ (app) - , id_ (id) - , sink_ (journal, OverlayImpl::makePrefix(id)) - , journal_ (sink_) - , remote_endpoint_ (remote_endpoint) - , usage_ (usage) - , strand_ (io_service) - , timer_ (io_service) - , stream_ptr_ (std::make_unique( - socket_type(std::forward(io_service)), *context)) - , socket_ (stream_ptr_->next_layer().socket()) - , stream_ (*stream_ptr_) - , slot_ (slot) +ConnectAttempt::ConnectAttempt( + Application& app, + boost::asio::io_service& io_service, + endpoint_type const& remote_endpoint, + Resource::Consumer usage, + shared_context const& context, + std::uint32_t id, + std::shared_ptr const& slot, + beast::Journal journal, + OverlayImpl& overlay) + : Child(overlay) + , app_(app) + , id_(id) + , sink_(journal, OverlayImpl::makePrefix(id)) + , journal_(sink_) + , remote_endpoint_(remote_endpoint) + , usage_(usage) + , strand_(io_service) + , timer_(io_service) + , stream_ptr_(std::make_unique( + socket_type(std::forward(io_service)), + *context)) + , socket_(stream_ptr_->next_layer().socket()) + , stream_(*stream_ptr_) + , slot_(slot) { - JLOG(journal_.debug()) << - "Connect " << remote_endpoint; + JLOG(journal_.debug()) << "Connect " << remote_endpoint; } ConnectAttempt::~ConnectAttempt() { if (slot_ != nullptr) overlay_.peerFinder().on_closed(slot_); - JLOG(journal_.trace()) << - "~ConnectAttempt"; + JLOG(journal_.trace()) << "~ConnectAttempt"; } void ConnectAttempt::stop() { - if (! strand_.running_in_this_thread()) - return strand_.post(std::bind( - &ConnectAttempt::stop, shared_from_this())); + if (!strand_.running_in_this_thread()) + return strand_.post( + std::bind(&ConnectAttempt::stop, shared_from_this())); if (socket_.is_open()) { - JLOG(journal_.debug()) << - "Stop"; + JLOG(journal_.debug()) << "Stop"; } close(); } @@ -74,9 +77,12 @@ ConnectAttempt::stop() void ConnectAttempt::run() { - stream_.next_layer().async_connect (remote_endpoint_, - strand_.wrap (std::bind (&ConnectAttempt::onConnect, - shared_from_this(), std::placeholders::_1))); + stream_.next_layer().async_connect( + remote_endpoint_, + strand_.wrap(std::bind( + &ConnectAttempt::onConnect, + shared_from_this(), + std::placeholders::_1))); } //------------------------------------------------------------------------------ @@ -95,14 +101,14 @@ ConnectAttempt::close() } void -ConnectAttempt::fail (std::string const& reason) +ConnectAttempt::fail(std::string const& reason) { JLOG(journal_.debug()) << reason; close(); } void -ConnectAttempt::fail (std::string const& name, error_code ec) +ConnectAttempt::fail(std::string const& name, error_code ec) { JLOG(journal_.debug()) << name << ": " << ec.message(); close(); @@ -115,14 +121,12 @@ ConnectAttempt::setTimer() timer_.expires_from_now(std::chrono::seconds(15), ec); if (ec) { - JLOG(journal_.error()) << - "setTimer: " << ec.message(); + JLOG(journal_.error()) << "setTimer: " << ec.message(); return; } timer_.async_wait(strand_.wrap(std::bind( - &ConnectAttempt::onTimer, shared_from_this(), - std::placeholders::_1))); + &ConnectAttempt::onTimer, shared_from_this(), std::placeholders::_1))); } void @@ -133,127 +137,141 @@ ConnectAttempt::cancelTimer() } void -ConnectAttempt::onTimer (error_code ec) +ConnectAttempt::onTimer(error_code ec) { - if (! socket_.is_open()) + if (!socket_.is_open()) return; if (ec == boost::asio::error::operation_aborted) return; if (ec) { // This should never happen - JLOG(journal_.error()) << - "onTimer: " << ec.message(); + JLOG(journal_.error()) << "onTimer: " << ec.message(); return close(); } fail("Timeout"); } void -ConnectAttempt::onConnect (error_code ec) +ConnectAttempt::onConnect(error_code ec) { cancelTimer(); - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; endpoint_type local_endpoint; - if(! ec) + if (!ec) local_endpoint = socket_.local_endpoint(ec); - if(ec) + if (ec) return fail("onConnect", ec); - if(! socket_.is_open()) + if (!socket_.is_open()) return; - JLOG(journal_.trace()) << - "onConnect"; + JLOG(journal_.trace()) << "onConnect"; setTimer(); - stream_.set_verify_mode (boost::asio::ssl::verify_none); - stream_.async_handshake (boost::asio::ssl::stream_base::client, - strand_.wrap (std::bind (&ConnectAttempt::onHandshake, - shared_from_this(), std::placeholders::_1))); + stream_.set_verify_mode(boost::asio::ssl::verify_none); + stream_.async_handshake( + boost::asio::ssl::stream_base::client, + strand_.wrap(std::bind( + &ConnectAttempt::onHandshake, + shared_from_this(), + std::placeholders::_1))); } void -ConnectAttempt::onHandshake (error_code ec) +ConnectAttempt::onHandshake(error_code ec) { cancelTimer(); - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; endpoint_type local_endpoint; - if (! ec) + if (!ec) local_endpoint = socket_.local_endpoint(ec); - if(ec) + if (ec) return fail("onHandshake", ec); - JLOG(journal_.trace()) << - "onHandshake"; + JLOG(journal_.trace()) << "onHandshake"; - if (! overlay_.peerFinder().onConnected (slot_, - beast::IPAddressConversion::from_asio (local_endpoint))) + if (!overlay_.peerFinder().onConnected( + slot_, beast::IPAddressConversion::from_asio(local_endpoint))) return fail("Duplicate connection"); auto const sharedValue = makeSharedValue(*stream_ptr_, journal_); - if (! sharedValue) - return close(); // makeSharedValue logs + if (!sharedValue) + return close(); // makeSharedValue logs - req_ = makeRequest(!overlay_.peerFinder().config().peerPrivate, app_.config().COMPRESSION); + req_ = makeRequest( + !overlay_.peerFinder().config().peerPrivate, app_.config().COMPRESSION); - buildHandshake(req_, *sharedValue, overlay_.setup().networkID, - overlay_.setup().public_ip, remote_endpoint_.address(), app_); + buildHandshake( + req_, + *sharedValue, + overlay_.setup().networkID, + overlay_.setup().public_ip, + remote_endpoint_.address(), + app_); setTimer(); - boost::beast::http::async_write(stream_, req_, - strand_.wrap (std::bind (&ConnectAttempt::onWrite, - shared_from_this(), std::placeholders::_1))); + boost::beast::http::async_write( + stream_, + req_, + strand_.wrap(std::bind( + &ConnectAttempt::onWrite, + shared_from_this(), + std::placeholders::_1))); } void -ConnectAttempt::onWrite (error_code ec) +ConnectAttempt::onWrite(error_code ec) { cancelTimer(); - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; - if(ec) + if (ec) return fail("onWrite", ec); - boost::beast::http::async_read(stream_, read_buf_, response_, - strand_.wrap(std::bind(&ConnectAttempt::onRead, - shared_from_this(), std::placeholders::_1))); + boost::beast::http::async_read( + stream_, + read_buf_, + response_, + strand_.wrap(std::bind( + &ConnectAttempt::onRead, + shared_from_this(), + std::placeholders::_1))); } void -ConnectAttempt::onRead (error_code ec) +ConnectAttempt::onRead(error_code ec) { cancelTimer(); - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; - if(ec == boost::asio::error::eof) + if (ec == boost::asio::error::eof) { - JLOG(journal_.info()) << - "EOF"; + JLOG(journal_.info()) << "EOF"; setTimer(); return stream_.async_shutdown(strand_.wrap(std::bind( - &ConnectAttempt::onShutdown, shared_from_this(), - std::placeholders::_1))); + &ConnectAttempt::onShutdown, + shared_from_this(), + std::placeholders::_1))); } - if(ec) + if (ec) return fail("onRead", ec); processResponse(); } void -ConnectAttempt::onShutdown (error_code ec) +ConnectAttempt::onShutdown(error_code ec) { cancelTimer(); - if (! ec) + if (!ec) { - JLOG(journal_.error()) << - "onShutdown: expected error condition"; + JLOG(journal_.error()) << "onShutdown: expected error condition"; return close(); } if (ec != boost::asio::error::eof) @@ -264,17 +282,17 @@ ConnectAttempt::onShutdown (error_code ec) //-------------------------------------------------------------------------- auto -ConnectAttempt::makeRequest (bool crawl, bool compressionEnabled) -> request_type +ConnectAttempt::makeRequest(bool crawl, bool compressionEnabled) -> request_type { request_type m; m.method(boost::beast::http::verb::get); m.target("/"); m.version(11); - m.insert ("User-Agent", BuildInfo::getFullVersionString()); - m.insert ("Upgrade", supportedProtocolVersions()); - m.insert ("Connection", "Upgrade"); - m.insert ("Connect-As", "Peer"); - m.insert ("Crawl", crawl ? "public" : "private"); + m.insert("User-Agent", BuildInfo::getFullVersionString()); + m.insert("Upgrade", supportedProtocolVersions()); + m.insert("Connection", "Upgrade"); + m.insert("Connect-As", "Peer"); + m.insert("Crawl", crawl ? "public" : "private"); if (compressionEnabled) m.insert("X-Offer-Compression", "lz4"); return m; @@ -321,8 +339,9 @@ ConnectAttempt::processResponse() if (!OverlayImpl::isPeerUpgrade(response_)) { - JLOG(journal_.info()) << "Unable to upgrade to peer protocol: " << - response_.result() << " (" << response_.reason() << ")"; + JLOG(journal_.info()) + << "Unable to upgrade to peer protocol: " << response_.result() + << " (" << response_.reason() << ")"; return close(); } @@ -337,24 +356,29 @@ ConnectAttempt::processResponse() negotiatedProtocol = pvs[0]; if (!negotiatedProtocol) - return fail("processResponse: Unable to negotiate protocol version"); + return fail( + "processResponse: Unable to negotiate protocol version"); } auto const sharedValue = makeSharedValue(*stream_ptr_, journal_); - if(! sharedValue) - return close(); // makeSharedValue logs + if (!sharedValue) + return close(); // makeSharedValue logs try { - auto publicKey = verifyHandshake(response_, *sharedValue, - overlay_.setup().networkID, overlay_.setup().public_ip, - remote_endpoint_.address(), app_); + auto publicKey = verifyHandshake( + response_, + *sharedValue, + overlay_.setup().networkID, + overlay_.setup().public_ip, + remote_endpoint_.address(), + app_); - JLOG(journal_.info()) << - "Public Key: " << toBase58(TokenType::NodePublic, publicKey); + JLOG(journal_.info()) + << "Public Key: " << toBase58(TokenType::NodePublic, publicKey); - JLOG(journal_.debug()) << - "Protocol: " << to_string(*negotiatedProtocol); + JLOG(journal_.debug()) + << "Protocol: " << to_string(*negotiatedProtocol); auto const member = app_.cluster().member(publicKey); if (member) @@ -362,21 +386,29 @@ ConnectAttempt::processResponse() JLOG(journal_.info()) << "Cluster name: " << *member; } - auto const result = overlay_.peerFinder().activate (slot_, - publicKey, static_cast(member)); + auto const result = overlay_.peerFinder().activate( + slot_, publicKey, static_cast(member)); if (result != PeerFinder::Result::success) return fail("Outbound slots full"); - auto const peer = std::make_shared(app_, std::move(stream_ptr_), - read_buf_.data(), std::move(slot_), std::move(response_), usage_, - publicKey, *negotiatedProtocol, id_, overlay_); + auto const peer = std::make_shared( + app_, + std::move(stream_ptr_), + read_buf_.data(), + std::move(slot_), + std::move(response_), + usage_, + publicKey, + *negotiatedProtocol, + id_, + overlay_); - overlay_.add_active (peer); + overlay_.add_active(peer); } catch (std::exception const& e) { - return fail(std::string("Handshake failure (")+ e.what() + ")"); + return fail(std::string("Handshake failure (") + e.what() + ")"); } } -} // ripple +} // namespace ripple diff --git a/src/ripple/overlay/impl/ConnectAttempt.h b/src/ripple/overlay/impl/ConnectAttempt.h index 5464ae8cdf..92c31f28e7 100644 --- a/src/ripple/overlay/impl/ConnectAttempt.h +++ b/src/ripple/overlay/impl/ConnectAttempt.h @@ -26,9 +26,8 @@ namespace ripple { /** Manages an outbound connection attempt. */ -class ConnectAttempt - : public OverlayImpl::Child - , public std::enable_shared_from_this +class ConnectAttempt : public OverlayImpl::Child, + public std::enable_shared_from_this { private: using error_code = boost::system::error_code; @@ -43,7 +42,7 @@ private: using socket_type = boost::asio::ip::tcp::socket; using middle_type = boost::beast::tcp_stream; - using stream_type = boost::beast::ssl_stream ; + using stream_type = boost::beast::ssl_stream; using shared_context = std::shared_ptr; Application& app_; @@ -63,11 +62,16 @@ private: request_type req_; public: - ConnectAttempt (Application& app, boost::asio::io_service& io_service, - endpoint_type const& remote_endpoint, Resource::Consumer usage, - shared_context const& context, - std::uint32_t id, std::shared_ptr const& slot, - beast::Journal journal, OverlayImpl& overlay); + ConnectAttempt( + Application& app, + boost::asio::io_service& io_service, + endpoint_type const& remote_endpoint, + Resource::Consumer usage, + shared_context const& context, + std::uint32_t id, + std::shared_ptr const& slot, + beast::Journal journal, + OverlayImpl& overlay); ~ConnectAttempt(); @@ -78,29 +82,38 @@ public: run(); private: + void + close(); + void + fail(std::string const& reason); + void + fail(std::string const& name, error_code ec); + void + setTimer(); + void + cancelTimer(); + void + onTimer(error_code ec); + void + onConnect(error_code ec); + void + onHandshake(error_code ec); + void + onWrite(error_code ec); + void + onRead(error_code ec); + void + onShutdown(error_code ec); - void close(); - void fail (std::string const& reason); - void fail (std::string const& name, error_code ec); - void setTimer(); - void cancelTimer(); - void onTimer (error_code ec); - void onConnect (error_code ec); - void onHandshake (error_code ec); - void onWrite (error_code ec); - void onRead (error_code ec); - void onShutdown (error_code ec); + static request_type + makeRequest(bool crawl, bool compressionEnabled); - static - request_type - makeRequest (bool crawl, bool compressionEnabled); - - void processResponse(); + void + processResponse(); template - static - boost::asio::ip::tcp::endpoint - parse_endpoint (std::string const& s, boost::system::error_code& ec) + static boost::asio::ip::tcp::endpoint + parse_endpoint(std::string const& s, boost::system::error_code& ec) { beast::IP::Endpoint bep; std::istringstream is(s); @@ -116,6 +129,6 @@ private: } }; -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/Handshake.cpp b/src/ripple/overlay/impl/Handshake.cpp index bb81f8ed47..190a12122f 100644 --- a/src/ripple/overlay/impl/Handshake.cpp +++ b/src/ripple/overlay/impl/Handshake.cpp @@ -17,19 +17,18 @@ */ //============================================================================== -#include #include #include #include #include -#include #include +#include +#include #include #include #include #include - // VFALCO Shouldn't we have to include the OpenSSL // headers or something for SSL_get_finished? @@ -49,42 +48,39 @@ namespace ripple { this topic, see https://github.com/openssl/openssl/issues/5509 and https://github.com/ripple/rippled/issues/2413. */ -static -boost::optional> -hashLastMessage (SSL const* ssl, - size_t (*get)(const SSL *, void *, size_t)) +static boost::optional> +hashLastMessage(SSL const* ssl, size_t (*get)(const SSL*, void*, size_t)) { constexpr std::size_t sslMinimumFinishedLength = 12; unsigned char buf[1024]; size_t len = get(ssl, buf, sizeof(buf)); - if(len < sslMinimumFinishedLength) + if (len < sslMinimumFinishedLength) return boost::none; sha512_hasher h; base_uint<512> cookie; - SHA512 (buf, len, cookie.data()); + SHA512(buf, len, cookie.data()); return cookie; } boost::optional -makeSharedValue (stream_type& ssl, beast::Journal journal) +makeSharedValue(stream_type& ssl, beast::Journal journal) { - auto const cookie1 = hashLastMessage( - ssl.native_handle(), SSL_get_finished); + auto const cookie1 = hashLastMessage(ssl.native_handle(), SSL_get_finished); if (!cookie1) { - JLOG (journal.error()) << "Cookie generation: local setup not complete"; + JLOG(journal.error()) << "Cookie generation: local setup not complete"; return boost::none; } - auto const cookie2 = hashLastMessage( - ssl.native_handle(), SSL_get_peer_finished); + auto const cookie2 = + hashLastMessage(ssl.native_handle(), SSL_get_peer_finished); if (!cookie2) { - JLOG (journal.error()) << "Cookie generation: peer setup not complete"; + JLOG(journal.error()) << "Cookie generation: peer setup not complete"; return boost::none; } @@ -94,11 +90,12 @@ makeSharedValue (stream_type& ssl, beast::Journal journal) // is 0. Don't allow this. if (result == beast::zero) { - JLOG(journal.error()) << "Cookie generation: identical finished messages"; + JLOG(journal.error()) + << "Cookie generation: identical finished messages"; return boost::none; } - return sha512Half (Slice (result.data(), result.size())); + return sha512Half(Slice(result.data(), result.size())); } void @@ -118,32 +115,36 @@ buildHandshake( h.insert("Network-ID", std::to_string(*networkID)); } - h.insert ("Network-Time", + h.insert( + "Network-Time", std::to_string(app.timeKeeper().now().time_since_epoch().count())); - h.insert ("Public-Key", + h.insert( + "Public-Key", toBase58(TokenType::NodePublic, app.nodeIdentity().first)); { - auto const sig = signDigest(app.nodeIdentity().first, - app.nodeIdentity().second, sharedValue); - h.insert("Session-Signature", - base64_encode(sig.data(), sig.size())); + auto const sig = signDigest( + app.nodeIdentity().first, app.nodeIdentity().second, sharedValue); + h.insert("Session-Signature", base64_encode(sig.data(), sig.size())); } - if (beast::IP::is_public (remote_ip)) - h.insert ("Remote-IP", remote_ip.to_string()); + if (beast::IP::is_public(remote_ip)) + h.insert("Remote-IP", remote_ip.to_string()); if (!public_ip.is_unspecified()) - h.insert ("Local-IP", public_ip.to_string()); + h.insert("Local-IP", public_ip.to_string()); if (auto const cl = app.getLedgerMaster().getClosedLedger()) { // TODO: Use hex for these - h.insert ("Closed-Ledger", base64_encode( - cl->info().hash.begin(), cl->info().hash.size())); - h.insert ("Previous-Ledger", base64_encode( - cl->info().parentHash.begin(), cl->info().parentHash.size())); + h.insert( + "Closed-Ledger", + base64_encode(cl->info().hash.begin(), cl->info().hash.size())); + h.insert( + "Previous-Ledger", + base64_encode( + cl->info().parentHash.begin(), cl->info().parentHash.size())); } } @@ -165,7 +166,7 @@ verifyHandshake( if (!beast::lexicalCastChecked(nid, iter->value().to_string())) throw std::runtime_error("Invalid peer network identifier"); - if(nid != *networkID) + if (nid != *networkID) throw std::runtime_error("Peer is on a different network"); } } @@ -173,8 +174,7 @@ verifyHandshake( if (auto const iter = headers.find("Network-Time"); iter != headers.end()) { auto const netTime = - [str = iter->value().to_string()]() -> TimeKeeper::time_point - { + [str = iter->value().to_string()]() -> TimeKeeper::time_point { TimeKeeper::duration::rep val; if (beast::lexicalCastChecked(val, str)) @@ -194,12 +194,11 @@ verifyHandshake( // uses an unsigned integer for representing durations, which is // a problem when trying to subtract time points. // FIXME: @HowardHinnant, should we migrate to using std::int64_t? - auto calculateOffset = []( - TimeKeeper::time_point a, TimeKeeper::time_point b) - { + auto calculateOffset = [](TimeKeeper::time_point a, + TimeKeeper::time_point b) { if (a > b) return duration_cast(a - b); - return - duration_cast(b - a); + return -duration_cast(b - a); }; auto const offset = calculateOffset(netTime, ourTime); @@ -208,14 +207,13 @@ verifyHandshake( throw std::runtime_error("Peer clock is too far off"); } - PublicKey const publicKey = [&headers] - { - if (auto const iter = headers.find ("Public-Key"); iter != headers.end()) + PublicKey const publicKey = [&headers] { + if (auto const iter = headers.find("Public-Key"); iter != headers.end()) { auto pk = parseBase58( TokenType::NodePublic, iter->value().to_string()); - if(pk) + if (pk) { if (publicKeyType(*pk) != KeyType::secp256k1) throw std::runtime_error("Unsupported public key type"); @@ -244,11 +242,11 @@ verifyHandshake( auto sig = base64_decode(iter->value().to_string()); - if (! verifyDigest(publicKey, sharedValue, makeSlice(sig), false)) + if (!verifyDigest(publicKey, sharedValue, makeSlice(sig), false)) throw std::runtime_error("Failed to verify session"); } - if (auto const iter = headers.find ("Local-IP"); iter != headers.end()) + if (auto const iter = headers.find("Local-IP"); iter != headers.end()) { boost::system::error_code ec; auto const local_ip = boost::asio::ip::address::from_string( @@ -258,8 +256,9 @@ verifyHandshake( throw std::runtime_error("Invalid Local-IP"); if (beast::IP::is_public(remote) && remote != local_ip) - throw std::runtime_error("Incorrect Local-IP: " + - remote.to_string() + " instead of " + local_ip.to_string()); + throw std::runtime_error( + "Incorrect Local-IP: " + remote.to_string() + " instead of " + + local_ip.to_string()); } if (auto const iter = headers.find("Remote-IP"); iter != headers.end()) @@ -271,17 +270,19 @@ verifyHandshake( if (ec) throw std::runtime_error("Invalid Remote-IP"); - if (beast::IP::is_public(remote) && !beast::IP::is_unspecified(public_ip)) + if (beast::IP::is_public(remote) && + !beast::IP::is_unspecified(public_ip)) { // We know our public IP and peer reports our connection came // from some other IP. if (remote_ip != public_ip) - throw std::runtime_error("Incorrect Remote-IP: " + - public_ip.to_string() + " instead of " + remote_ip.to_string()); + throw std::runtime_error( + "Incorrect Remote-IP: " + public_ip.to_string() + + " instead of " + remote_ip.to_string()); } } return publicKey; } -} +} // namespace ripple diff --git a/src/ripple/overlay/impl/Handshake.h b/src/ripple/overlay/impl/Handshake.h index 40bd1b173a..8a92a1fd94 100644 --- a/src/ripple/overlay/impl/Handshake.h +++ b/src/ripple/overlay/impl/Handshake.h @@ -21,23 +21,23 @@ #define RIPPLE_OVERLAY_HANDSHAKE_H_INCLUDED #include +#include +#include #include #include #include #include #include -#include -#include -#include #include +#include #include #include namespace ripple { -using socket_type = boost::beast::tcp_stream; -using stream_type = boost::beast::ssl_stream ; +using socket_type = boost::beast::tcp_stream; +using stream_type = boost::beast::ssl_stream; /** Computes a shared value based on the SSL connection state. @@ -49,9 +49,10 @@ using stream_type = boost::beast::ssl_stream ; @return A 256-bit value on success; an unseated optional otherwise. */ boost::optional -makeSharedValue (stream_type& ssl, beast::Journal journal); +makeSharedValue(stream_type& ssl, beast::Journal journal); -/** Insert fields headers necessary for upgrading the link to the peer protocol. */ +/** Insert fields headers necessary for upgrading the link to the peer protocol. + */ void buildHandshake( boost::beast::http::fields& h, @@ -61,7 +62,8 @@ buildHandshake( beast::IP::Address remote_ip, Application& app); -/** Validate header fields necessary for upgrading the link to the peer protocol. +/** Validate header fields necessary for upgrading the link to the peer + protocol. This performs critical security checks that ensure that prevent MITM attacks on our peer-to-peer links and that the remote peer @@ -80,6 +82,6 @@ verifyHandshake( beast::IP::Address remote, Application& app); -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/Message.cpp b/src/ripple/overlay/impl/Message.cpp index edac6b4124..e2c1f2fb54 100644 --- a/src/ripple/overlay/impl/Message.cpp +++ b/src/ripple/overlay/impl/Message.cpp @@ -23,20 +23,20 @@ namespace ripple { -Message::Message (::google::protobuf::Message const& message, int type) +Message::Message(::google::protobuf::Message const& message, int type) : category_(TrafficCount::categorize(message, type, false)) { using namespace ripple::compression; #if defined(GOOGLE_PROTOBUF_VERSION) && (GOOGLE_PROTOBUF_VERSION >= 3011000) - auto const messageBytes = message.ByteSizeLong (); + auto const messageBytes = message.ByteSizeLong(); #else - unsigned const messageBytes = message.ByteSize (); + unsigned const messageBytes = message.ByteSize(); #endif - assert (messageBytes != 0); + assert(messageBytes != 0); - buffer_.resize (headerBytes + messageBytes); + buffer_.resize(headerBytes + messageBytes); setHeader(buffer_.data(), messageBytes, type, Algorithm::None, 0); @@ -48,14 +48,14 @@ void Message::compress() { using namespace ripple::compression; - auto const messageBytes = buffer_.size () - headerBytes; + auto const messageBytes = buffer_.size() - headerBytes; auto type = getType(buffer_.data()); - bool const compressible = [&]{ + bool const compressible = [&] { if (messageBytes <= 70) return false; - switch(type) + switch (type) { case protocol::mtMANIFESTS: case protocol::mtENDPOINTS: @@ -85,17 +85,23 @@ Message::compress() auto payload = static_cast(buffer_.data() + headerBytes); auto compressedSize = ripple::compression::compress( - payload, - messageBytes, - [&](std::size_t inSize) { // size of required compressed buffer - bufferCompressed_.resize(inSize + headerBytesCompressed); - return (bufferCompressed_.data() + headerBytesCompressed); - }); + payload, + messageBytes, + [&](std::size_t inSize) { // size of required compressed buffer + bufferCompressed_.resize(inSize + headerBytesCompressed); + return (bufferCompressed_.data() + headerBytesCompressed); + }); - if (compressedSize < (messageBytes - (headerBytesCompressed - headerBytes))) + if (compressedSize < + (messageBytes - (headerBytesCompressed - headerBytes))) { bufferCompressed_.resize(headerBytesCompressed + compressedSize); - setHeader(bufferCompressed_.data(), compressedSize, type, Algorithm::LZ4, messageBytes); + setHeader( + bufferCompressed_.data(), + compressedSize, + type, + Algorithm::LZ4, + messageBytes); } else bufferCompressed_.resize(0); @@ -109,20 +115,23 @@ Message::compress() * 15-0 Message Type * Compressed message header * 79 Set to 0, indicates the message is compressed - * 78-76 Compression algorithm, value 1-7. Set to 1 to indicate LZ4 compression - * 75-74 Set to 0 - * 73-48 Payload size - * 47-32 Message Type + * 78-76 Compression algorithm, value 1-7. Set to 1 to indicate LZ4 + * compression 75-74 Set to 0 73-48 Payload size 47-32 Message Type * 31-0 Uncompressed message size -*/ + */ void -Message::setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type, - Algorithm comprAlgorithm, std::uint32_t uncompressedBytes) +Message::setHeader( + std::uint8_t* in, + std::uint32_t payloadBytes, + int type, + Algorithm comprAlgorithm, + std::uint32_t uncompressedBytes) { auto h = in; auto pack = [](std::uint8_t*& in, std::uint32_t size) { - *in++ = static_cast((size >> 24) & 0x0F); // leftmost 4 are compression bits + *in++ = static_cast( + (size >> 24) & 0x0F); // leftmost 4 are compression bits *in++ = static_cast((size >> 16) & 0xFF); *in++ = static_cast((size >> 8) & 0xFF); *in++ = static_cast(size & 0xFF); @@ -131,7 +140,7 @@ Message::setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type, pack(in, payloadBytes); *in++ = static_cast((type >> 8) & 0xFF); - *in++ = static_cast (type & 0xFF); + *in++ = static_cast(type & 0xFF); if (comprAlgorithm != Algorithm::None) { @@ -140,8 +149,8 @@ Message::setHeader(std::uint8_t* in, std::uint32_t payloadBytes, int type, } } -std::vector const& -Message::getBuffer (Compressed tryCompressed) +std::vector const& +Message::getBuffer(Compressed tryCompressed) { if (tryCompressed == Compressed::Off) return buffer_; @@ -161,4 +170,4 @@ Message::getType(std::uint8_t const* in) const return type; } -} +} // namespace ripple diff --git a/src/ripple/overlay/impl/OverlayImpl.cpp b/src/ripple/overlay/impl/OverlayImpl.cpp index db01cee0cf..14fb679174 100644 --- a/src/ripple/overlay/impl/OverlayImpl.cpp +++ b/src/ripple/overlay/impl/OverlayImpl.cpp @@ -28,12 +28,12 @@ #include #include #include -#include #include #include +#include #include -#include #include +#include #include #include @@ -50,33 +50,32 @@ struct get_peer_json get_peer_json() = default; - void operator() (std::shared_ptr const& peer) + void + operator()(std::shared_ptr const& peer) { - json.append (peer->json ()); + json.append(peer->json()); } - Json::Value operator() () + Json::Value + operator()() { return json; } }; -namespace CrawlOptions -{ - enum - { - Disabled = 0, - Overlay = (1 << 0), - ServerInfo = (1 << 1), - ServerCounts = (1 << 2), - Unl = (1 << 3) - }; +namespace CrawlOptions { +enum { + Disabled = 0, + Overlay = (1 << 0), + ServerInfo = (1 << 1), + ServerCounts = (1 << 2), + Unl = (1 << 3) +}; } //------------------------------------------------------------------------------ -OverlayImpl::Child::Child (OverlayImpl& overlay) - : overlay_(overlay) +OverlayImpl::Child::Child(OverlayImpl& overlay) : overlay_(overlay) { } @@ -87,9 +86,8 @@ OverlayImpl::Child::~Child() //------------------------------------------------------------------------------ -OverlayImpl::Timer::Timer (OverlayImpl& overlay) - : Child(overlay) - , timer_(overlay_.io_service_) +OverlayImpl::Timer::Timer(OverlayImpl& overlay) + : Child(overlay), timer_(overlay_.io_service_) { } @@ -103,14 +101,13 @@ OverlayImpl::Timer::stop() void OverlayImpl::Timer::run() { - timer_.expires_from_now (std::chrono::seconds(1)); - timer_.async_wait(overlay_.strand_.wrap( - std::bind(&Timer::on_timer, shared_from_this(), - std::placeholders::_1))); + timer_.expires_from_now(std::chrono::seconds(1)); + timer_.async_wait(overlay_.strand_.wrap(std::bind( + &Timer::on_timer, shared_from_this(), std::placeholders::_1))); } void -OverlayImpl::Timer::on_timer (error_code ec) +OverlayImpl::Timer::on_timer(error_code ec) { if (ec || overlay_.isStopping()) { @@ -128,15 +125,14 @@ OverlayImpl::Timer::on_timer (error_code ec) if ((++overlay_.timer_count_ % Tuning::checkSeconds) == 0) overlay_.check(); - timer_.expires_from_now (std::chrono::seconds(1)); + timer_.expires_from_now(std::chrono::seconds(1)); timer_.async_wait(overlay_.strand_.wrap(std::bind( - &Timer::on_timer, shared_from_this(), - std::placeholders::_1))); + &Timer::on_timer, shared_from_this(), std::placeholders::_1))); } //------------------------------------------------------------------------------ -OverlayImpl::OverlayImpl ( +OverlayImpl::OverlayImpl( Application& app, Setup const& setup, Stoppable& parent, @@ -146,65 +142,70 @@ OverlayImpl::OverlayImpl ( boost::asio::io_service& io_service, BasicConfig const& config, beast::insight::Collector::ptr const& collector) - : Overlay (parent) - , app_ (app) - , io_service_ (io_service) - , work_ (boost::in_place(std::ref(io_service_))) - , strand_ (io_service_) + : Overlay(parent) + , app_(app) + , io_service_(io_service) + , work_(boost::in_place(std::ref(io_service_))) + , strand_(io_service_) , setup_(setup) - , journal_ (app_.journal("Overlay")) + , journal_(app_.journal("Overlay")) , serverHandler_(serverHandler) - , m_resourceManager (resourceManager) - , m_peerFinder (PeerFinder::make_Manager (*this, io_service, - stopwatch(), app_.journal("PeerFinder"), config, collector)) - , m_resolver (resolver) + , m_resourceManager(resourceManager) + , m_peerFinder(PeerFinder::make_Manager( + *this, + io_service, + stopwatch(), + app_.journal("PeerFinder"), + config, + collector)) + , m_resolver(resolver) , next_id_(1) , timer_count_(0) - , m_stats ( - std::bind(&OverlayImpl::collect_metrics, this), - collector, - [counts = m_traffic.getCounts(), collector]() - { - std::vector ret; - ret.reserve(counts.size()); + , m_stats( + std::bind(&OverlayImpl::collect_metrics, this), + collector, + [counts = m_traffic.getCounts(), collector]() { + std::vector ret; + ret.reserve(counts.size()); - for (size_t i = 0; i < counts.size(); ++i) - { - ret.push_back(TrafficGauges(counts[i].name, collector)); - } + for (size_t i = 0; i < counts.size(); ++i) + { + ret.push_back(TrafficGauges(counts[i].name, collector)); + } - return ret; - }()) + return ret; + }()) { - beast::PropertyStream::Source::add (m_peerFinder.get()); + beast::PropertyStream::Source::add(m_peerFinder.get()); } -OverlayImpl::~OverlayImpl () +OverlayImpl::~OverlayImpl() { stop(); // Block until dependent objects have been destroyed. // This is just to catch improper use of the Stoppable API. // - std::unique_lock lock (mutex_); - cond_.wait (lock, [this] { return list_.empty(); }); + std::unique_lock lock(mutex_); + cond_.wait(lock, [this] { return list_.empty(); }); } //------------------------------------------------------------------------------ Handoff -OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, +OverlayImpl::onHandoff( + std::unique_ptr&& stream_ptr, http_request_type&& request, - endpoint_type remote_endpoint) + endpoint_type remote_endpoint) { auto const id = next_id_++; - beast::WrappedSink sink (app_.logs()["Peer"], makePrefix(id)); - beast::Journal journal (sink); + beast::WrappedSink sink(app_.logs()["Peer"], makePrefix(id)); + beast::Journal journal(sink); Handoff handoff; if (processRequest(request, handoff)) return handoff; - if (! isPeerUpgrade(request)) + if (!isPeerUpgrade(request)) return handoff; handoff.moved = true; @@ -212,7 +213,8 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, JLOG(journal.debug()) << "Peer connection upgrade from " << remote_endpoint; error_code ec; - auto const local_endpoint (stream_ptr->next_layer().socket().local_endpoint(ec)); + auto const local_endpoint( + stream_ptr->next_layer().socket().local_endpoint(ec)); if (ec) { JLOG(journal.debug()) << remote_endpoint << " failed: " << ec.message(); @@ -224,9 +226,9 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, if (consumer.disconnect()) return handoff; - auto const slot = m_peerFinder->new_inbound_slot ( + auto const slot = m_peerFinder->new_inbound_slot( beast::IPAddressConversion::from_asio(local_endpoint), - beast::IPAddressConversion::from_asio(remote_endpoint)); + beast::IPAddressConversion::from_asio(remote_endpoint)); if (slot == nullptr) { @@ -238,17 +240,14 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, // TODO Validate HTTP request { - auto const types = beast::rfc2616::split_commas( - request["Connect-As"]); - if (std::find_if(types.begin(), types.end(), - [](std::string const& s) - { - return boost::iequals(s, "peer"); - }) == types.end()) + auto const types = beast::rfc2616::split_commas(request["Connect-As"]); + if (std::find_if(types.begin(), types.end(), [](std::string const& s) { + return boost::iequals(s, "peer"); + }) == types.end()) { handoff.moved = false; - handoff.response = makeRedirectResponse(slot, request, - remote_endpoint.address()); + handoff.response = + makeRedirectResponse(slot, request, remote_endpoint.address()); handoff.keep_alive = beast::rfc2616::is_keep_alive(request); return handoff; } @@ -259,59 +258,79 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, { m_peerFinder->on_closed(slot); handoff.moved = false; - handoff.response = makeErrorResponse (slot, request, - remote_endpoint.address(), "Unable to agree on a protocol version"); + handoff.response = makeErrorResponse( + slot, + request, + remote_endpoint.address(), + "Unable to agree on a protocol version"); handoff.keep_alive = false; return handoff; } auto const sharedValue = makeSharedValue(*stream_ptr, journal); - if(! sharedValue) + if (!sharedValue) { m_peerFinder->on_closed(slot); handoff.moved = false; - handoff.response = makeErrorResponse (slot, request, - remote_endpoint.address(), "Incorrect security cookie"); + handoff.response = makeErrorResponse( + slot, + request, + remote_endpoint.address(), + "Incorrect security cookie"); handoff.keep_alive = false; return handoff; } try { - auto publicKey = verifyHandshake(request, *sharedValue, - setup_.networkID, setup_.public_ip, remote_endpoint.address(), app_); + auto publicKey = verifyHandshake( + request, + *sharedValue, + setup_.networkID, + setup_.public_ip, + remote_endpoint.address(), + app_); { // The node gets a reserved slot if it is in our cluster // or if it has a reservation. bool const reserved = - static_cast(app_.cluster().member(publicKey)) - || app_.peerReservations().contains(publicKey); - auto const result = m_peerFinder->activate(slot, publicKey, reserved); + static_cast(app_.cluster().member(publicKey)) || + app_.peerReservations().contains(publicKey); + auto const result = + m_peerFinder->activate(slot, publicKey, reserved); if (result != PeerFinder::Result::success) { m_peerFinder->on_closed(slot); - JLOG(journal.debug()) << "Peer " << remote_endpoint << " redirected, slots full"; + JLOG(journal.debug()) + << "Peer " << remote_endpoint << " redirected, slots full"; handoff.moved = false; - handoff.response = makeRedirectResponse(slot, request, - remote_endpoint.address()); + handoff.response = makeRedirectResponse( + slot, request, remote_endpoint.address()); handoff.keep_alive = false; return handoff; } } - auto const peer = std::make_shared(app_, id, slot, - std::move(request), publicKey, *negotiatedVersion, consumer, - std::move(stream_ptr), *this); + auto const peer = std::make_shared( + app_, + id, + slot, + std::move(request), + publicKey, + *negotiatedVersion, + consumer, + std::move(stream_ptr), + *this); { // As we are not on the strand, run() must be called // while holding the lock, otherwise new I/O can be // queued after a call to stop(). - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); { - auto const result = m_peers.emplace (peer->slot(), peer); - assert (result.second); - (void) result.second; + auto const result = m_peers.emplace(peer->slot(), peer); + assert(result.second); + (void)result.second; } list_.emplace(peer.get(), peer); @@ -322,13 +341,13 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, } catch (std::exception const& e) { - JLOG(journal.debug()) << "Peer " << remote_endpoint << - " fails handshake (" << e.what() << ")"; + JLOG(journal.debug()) << "Peer " << remote_endpoint + << " fails handshake (" << e.what() << ")"; m_peerFinder->on_closed(slot); handoff.moved = false; - handoff.response = makeErrorResponse (slot, request, - remote_endpoint.address(), e.what()); + handoff.response = makeErrorResponse( + slot, request, remote_endpoint.address(), e.what()); handoff.keep_alive = false; return handoff; } @@ -339,14 +358,14 @@ OverlayImpl::onHandoff (std::unique_ptr && stream_ptr, bool OverlayImpl::isPeerUpgrade(http_request_type const& request) { - if (! is_upgrade(request)) + if (!is_upgrade(request)) return false; auto const versions = parseProtocolVersions(request["Upgrade"]); return !versions.empty(); } std::string -OverlayImpl::makePrefix (std::uint32_t id) +OverlayImpl::makePrefix(std::uint32_t id) { std::stringstream ss; ss << "[" << std::setfill('0') << std::setw(3) << id << "] "; @@ -354,8 +373,10 @@ OverlayImpl::makePrefix (std::uint32_t id) } std::shared_ptr -OverlayImpl::makeRedirectResponse (std::shared_ptr const& slot, - http_request_type const& request, address_type remote_address) +OverlayImpl::makeRedirectResponse( + std::shared_ptr const& slot, + http_request_type const& request, + address_type remote_address) { boost::beast::http::response msg; msg.version(request.version()); @@ -376,7 +397,8 @@ OverlayImpl::makeRedirectResponse (std::shared_ptr const& slot } std::shared_ptr -OverlayImpl::makeErrorResponse (std::shared_ptr const& slot, +OverlayImpl::makeErrorResponse( + std::shared_ptr const& slot, http_request_type const& request, address_type remote_address, std::string text) @@ -395,11 +417,11 @@ OverlayImpl::makeErrorResponse (std::shared_ptr const& slot, //------------------------------------------------------------------------------ void -OverlayImpl::connect (beast::IP::Endpoint const& remote_endpoint) +OverlayImpl::connect(beast::IP::Endpoint const& remote_endpoint) { assert(work_); - auto usage = resourceManager().newOutboundEndpoint (remote_endpoint); + auto usage = resourceManager().newOutboundEndpoint(remote_endpoint); if (usage.disconnect()) { JLOG(journal_.info()) << "Over resource limit: " << remote_endpoint; @@ -413,10 +435,16 @@ OverlayImpl::connect (beast::IP::Endpoint const& remote_endpoint) return; } - auto const p = std::make_shared(app_, - io_service_, beast::IPAddressConversion::to_asio_endpoint(remote_endpoint), - usage, setup_.context, next_id_++, slot, - app_.journal("Peer"), *this); + auto const p = std::make_shared( + app_, + io_service_, + beast::IPAddressConversion::to_asio_endpoint(remote_endpoint), + usage, + setup_.context, + next_id_++, + slot, + app_.journal("Peer"), + *this); std::lock_guard lock(mutex_); list_.emplace(p.get(), p); @@ -427,34 +455,32 @@ OverlayImpl::connect (beast::IP::Endpoint const& remote_endpoint) // Adds a peer that is already handshaked and active void -OverlayImpl::add_active (std::shared_ptr const& peer) +OverlayImpl::add_active(std::shared_ptr const& peer) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); { - auto const result = - m_peers.emplace (peer->slot(), peer); - assert (result.second); - (void) result.second; + auto const result = m_peers.emplace(peer->slot(), peer); + assert(result.second); + (void)result.second; } { - auto const result = ids_.emplace ( + auto const result = ids_.emplace( std::piecewise_construct, - std::make_tuple (peer->id()), - std::make_tuple (peer)); + std::make_tuple(peer->id()), + std::make_tuple(peer)); assert(result.second); - (void) result.second; + (void)result.second; } list_.emplace(peer.get(), peer); - JLOG(journal_.debug()) << - "activated " << peer->getRemoteAddress() << - " (" << peer->id() << ":" << - toBase58 ( - TokenType::NodePublic, - peer->getNodePublic()) << ")"; + JLOG(journal_.debug()) << "activated " << peer->getRemoteAddress() << " (" + << peer->id() << ":" + << toBase58( + TokenType::NodePublic, peer->getNodePublic()) + << ")"; // As we are not on the strand, run() must be called // while holding the lock, otherwise new I/O can be @@ -463,12 +489,12 @@ OverlayImpl::add_active (std::shared_ptr const& peer) } void -OverlayImpl::remove (std::shared_ptr const& slot) +OverlayImpl::remove(std::shared_ptr const& slot) { - std::lock_guard lock (mutex_); - auto const iter = m_peers.find (slot); - assert(iter != m_peers.end ()); - m_peers.erase (iter); + std::lock_guard lock(mutex_); + auto const iter = m_peers.find(slot); + assert(iter != m_peers.end()); + m_peers.erase(iter); } //------------------------------------------------------------------------------ @@ -479,9 +505,9 @@ OverlayImpl::remove (std::shared_ptr const& slot) // Caller must hold the mutex void -OverlayImpl::checkStopped () +OverlayImpl::checkStopped() { - if (isStopping() && areChildrenStopped () && list_.empty()) + if (isStopping() && areChildrenStopped() && list_.empty()) stopped(); } @@ -500,7 +526,7 @@ OverlayImpl::onPrepare() config.peerPrivate = app_.config().PEER_PRIVATE; // Servers with peer privacy don't want to allow incoming connections - config.wantIncoming = (! config.peerPrivate) && (port != 0); + config.wantIncoming = (!config.peerPrivate) && (port != 0); // This will cause servers configured as validators to request that // peers they connect to never report their IP address. We set this @@ -512,8 +538,7 @@ OverlayImpl::onPrepare() // if it's a private peer or we are running as standalone // automatic connections would defeat the purpose. config.autoConnect = - !app_.config().standalone() && - !app_.config().PEER_PRIVATE; + !app_.config().standalone() && !app_.config().PEER_PRIVATE; config.listeningPort = port; config.features = ""; config.ipLimit = setup_.ipLimit; @@ -521,86 +546,87 @@ OverlayImpl::onPrepare() // Enforce business rules config.applyTuning(); - m_peerFinder->setConfig (config); + m_peerFinder->setConfig(config); // Populate our boot cache: if there are no entries in [ips] then we use // the entries in [ips_fixed]. - auto bootstrapIps = app_.config().IPS.empty() - ? app_.config().IPS_FIXED - : app_.config().IPS; - + auto bootstrapIps = + app_.config().IPS.empty() ? app_.config().IPS_FIXED : app_.config().IPS; // If nothing is specified, default to several well-known high-capacity // servers to serve as bootstrap: - if (bootstrapIps.empty ()) + if (bootstrapIps.empty()) { // Pool of servers operated by Ripple Labs Inc. - https://ripple.com bootstrapIps.push_back("r.ripple.com 51235"); // Pool of servers operated by Alloy Networks - https://www.alloy.ee bootstrapIps.push_back("zaphod.alloy.ee 51235"); - + // Pool of servers operated by ISRDC - https://isrdc.in bootstrapIps.push_back("sahyadri.isrdc.in 51235"); } - m_resolver.resolve (bootstrapIps, - [this](std::string const& name, - std::vector const& addresses) - { - std::vector ips; + m_resolver.resolve( + bootstrapIps, + [this]( + std::string const& name, + std::vector const& addresses) { + std::vector ips; ips.reserve(addresses.size()); for (auto const& addr : addresses) { - if (addr.port () == 0) + if (addr.port() == 0) { - Throw ("Port not specified for " - "address:" + addr.to_string ()); + Throw( + "Port not specified for " + "address:" + + addr.to_string()); } - ips.push_back (to_string (addr)); + ips.push_back(to_string(addr)); } - std::string const base ("config: "); - if (!ips.empty ()) - m_peerFinder->addFallbackStrings (base + name, ips); + std::string const base("config: "); + if (!ips.empty()) + m_peerFinder->addFallbackStrings(base + name, ips); }); // Add the ips_fixed from the rippled.cfg file - if (! app_.config().standalone() && !app_.config().IPS_FIXED.empty ()) + if (!app_.config().standalone() && !app_.config().IPS_FIXED.empty()) { - m_resolver.resolve (app_.config().IPS_FIXED, + m_resolver.resolve( + app_.config().IPS_FIXED, [this]( std::string const& name, - std::vector const& addresses) - { - if (!addresses.empty ()) - m_peerFinder->addFixedPeer (name, addresses); + std::vector const& addresses) { + if (!addresses.empty()) + m_peerFinder->addFixedPeer(name, addresses); }); } } void -OverlayImpl::onStart () +OverlayImpl::onStart() { auto const timer = std::make_shared(*this); - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); list_.emplace(timer.get(), timer); timer_ = timer; timer->run(); } void -OverlayImpl::onStop () +OverlayImpl::onStop() { strand_.dispatch(std::bind(&OverlayImpl::stop, this)); } void -OverlayImpl::onChildrenStopped () +OverlayImpl::onChildrenStopped() { - std::lock_guard lock (mutex_); - checkStopped (); + std::lock_guard lock(mutex_); + checkStopped(); } //------------------------------------------------------------------------------ @@ -610,9 +636,9 @@ OverlayImpl::onChildrenStopped () //------------------------------------------------------------------------------ void -OverlayImpl::onWrite (beast::PropertyStream::Map& stream) +OverlayImpl::onWrite(beast::PropertyStream::Map& stream) { - beast::PropertyStream::Set set ("traffic", stream); + beast::PropertyStream::Set set("traffic", stream); auto const stats = m_traffic.getCounts(); for (auto const& i : stats) { @@ -635,102 +661,105 @@ OverlayImpl::onWrite (beast::PropertyStream::Map& stream) are known. */ void -OverlayImpl::activate (std::shared_ptr const& peer) +OverlayImpl::activate(std::shared_ptr const& peer) { // Now track this peer { - std::lock_guard lock (mutex_); - auto const result (ids_.emplace ( + std::lock_guard lock(mutex_); + auto const result(ids_.emplace( std::piecewise_construct, - std::make_tuple (peer->id()), - std::make_tuple (peer))); + std::make_tuple(peer->id()), + std::make_tuple(peer))); assert(result.second); - (void) result.second; + (void)result.second; } - JLOG(journal_.debug()) << - "activated " << peer->getRemoteAddress() << - " (" << peer->id() << - ":" << toBase58 ( - TokenType::NodePublic, - peer->getNodePublic()) << ")"; + JLOG(journal_.debug()) << "activated " << peer->getRemoteAddress() << " (" + << peer->id() << ":" + << toBase58( + TokenType::NodePublic, peer->getNodePublic()) + << ")"; // We just accepted this peer so we have non-zero active peers assert(size() != 0); } void -OverlayImpl::onPeerDeactivate (Peer::id_t id) +OverlayImpl::onPeerDeactivate(Peer::id_t id) { - std::lock_guard lock (mutex_); + std::lock_guard lock(mutex_); ids_.erase(id); } void -OverlayImpl::onManifests ( +OverlayImpl::onManifests( std::shared_ptr const& m, - std::shared_ptr const& from) + std::shared_ptr const& from) { auto& hashRouter = app_.getHashRouter(); auto const n = m->list_size(); auto const& journal = from->pjournal(); - JLOG(journal.debug()) << "TMManifest, " << n << (n == 1 ? " item" : " items"); + JLOG(journal.debug()) << "TMManifest, " << n + << (n == 1 ? " item" : " items"); for (std::size_t i = 0; i < n; ++i) { - auto& s = m->list ().Get (i).stobject (); + auto& s = m->list().Get(i).stobject(); if (auto mo = deserializeManifest(s)) { - uint256 const hash = mo->hash (); - if (!hashRouter.addSuppressionPeer (hash, from->id ())) { + uint256 const hash = mo->hash(); + if (!hashRouter.addSuppressionPeer(hash, from->id())) + { JLOG(journal.info()) << "Duplicate manifest #" << i + 1; continue; } - if (! app_.validators().listed (mo->masterKey)) + if (!app_.validators().listed(mo->masterKey)) { JLOG(journal.info()) << "Untrusted manifest #" << i + 1; - app_.getOPs().pubManifest (*mo); + app_.getOPs().pubManifest(*mo); continue; } auto const serialized = mo->serialized; - auto const result = app_.validatorManifests ().applyManifest ( - std::move(*mo)); + auto const result = + app_.validatorManifests().applyManifest(std::move(*mo)); if (result == ManifestDisposition::accepted) { - app_.getOPs().pubManifest (*deserializeManifest(serialized)); + app_.getOPs().pubManifest(*deserializeManifest(serialized)); } if (result == ManifestDisposition::accepted) { - auto db = app_.getWalletDB ().checkoutDb (); + auto db = app_.getWalletDB().checkoutDb(); soci::transaction tr(*db); static const char* const sql = - "INSERT INTO ValidatorManifests (RawData) VALUES (:rawData);"; + "INSERT INTO ValidatorManifests (RawData) VALUES " + "(:rawData);"; soci::blob rawData(*db); - convert (serialized, rawData); - *db << sql, soci::use (rawData); - tr.commit (); + convert(serialized, rawData); + *db << sql, soci::use(rawData); + tr.commit(); protocol::TMManifests o; - o.add_list ()->set_stobject (s); + o.add_list()->set_stobject(s); - auto const toSkip = hashRouter.shouldRelay (hash); - if(toSkip) - foreach (send_if_not ( + auto const toSkip = hashRouter.shouldRelay(hash); + if (toSkip) + foreach (send_if_not( std::make_shared(o, protocol::mtMANIFESTS), - peer_in_set (*toSkip))); + peer_in_set(*toSkip))) + ; } else { - JLOG(journal.info()) << "Bad manifest #" << i + 1 << - ": " << to_string(result); + JLOG(journal.info()) + << "Bad manifest #" << i + 1 << ": " << to_string(result); } } else @@ -742,12 +771,12 @@ OverlayImpl::onManifests ( } void -OverlayImpl::reportTraffic ( +OverlayImpl::reportTraffic( TrafficCount::category cat, bool isInbound, int number) { - m_traffic.addCount (cat, isInbound, number); + m_traffic.addCount(cat, isInbound, number); } Json::Value @@ -757,7 +786,7 @@ OverlayImpl::crawlShards(bool pubKey, std::uint32_t hops) using namespace std::chrono_literals; Json::Value jv(Json::objectValue); - auto const numPeers {size()}; + auto const numPeers{size()}; if (numPeers == 0) return jv; @@ -769,13 +798,13 @@ OverlayImpl::crawlShards(bool pubKey, std::uint32_t hops) if ((clock_type::now() - last) > 60s) { auto const timeout(seconds((hops * hops) * 10)); - std::unique_lock l {csMutex_}; + std::unique_lock l{csMutex_}; // Check if already requested if (csIDs_.empty()) { { - std::lock_guard lock {mutex_}; + std::lock_guard lock{mutex_}; for (auto& id : ids_) csIDs_.emplace(id.first); } @@ -783,8 +812,9 @@ OverlayImpl::crawlShards(bool pubKey, std::uint32_t hops) // Relay request to active peers protocol::TMGetPeerShardInfo tmGPS; tmGPS.set_hops(hops); - foreach(send_always(std::make_shared( - tmGPS, protocol::mtGET_PEER_SHARD_INFO))); + foreach (send_always(std::make_shared( + tmGPS, protocol::mtGET_PEER_SHARD_INFO))) + ; if (csCV_.wait_for(l, timeout) == std::cv_status::timeout) { @@ -801,13 +831,12 @@ OverlayImpl::crawlShards(bool pubKey, std::uint32_t hops) // Combine the shard info from peers and their sub peers hash_map peerShardInfo; - for_each([&](std::shared_ptr const& peer) - { + for_each([&](std::shared_ptr const& peer) { if (auto psi = peer->getPeerShardInfo()) { for (auto const& e : *psi) { - auto it {peerShardInfo.find(e.first)}; + auto it{peerShardInfo.find(e.first)}; if (it != peerShardInfo.end()) // The key exists so join the shard indexes. it->second.shardIndexes += e.second.shardIndexes; @@ -821,11 +850,11 @@ OverlayImpl::crawlShards(bool pubKey, std::uint32_t hops) auto& av = jv[jss::peers] = Json::Value(Json::arrayValue); for (auto const& e : peerShardInfo) { - auto& pv {av.append(Json::Value(Json::objectValue))}; + auto& pv{av.append(Json::Value(Json::objectValue))}; if (pubKey) pv[jss::public_key] = toBase58(TokenType::NodePublic, e.first); - auto const& address {e.second.endpoint.address()}; + auto const& address{e.second.endpoint.address()}; if (!address.is_unspecified()) pv[jss::ip] = address.to_string(); @@ -841,13 +870,15 @@ OverlayImpl::lastLink(std::uint32_t id) // Notify threads when every peer has received a last link. // This doesn't account for every node that might reply but // it is adequate. - std::lock_guard l {csMutex_}; + std::lock_guard l{csMutex_}; if (csIDs_.erase(id) && csIDs_.empty()) csCV_.notify_all(); } std::size_t -OverlayImpl::selectPeers (PeerSet& set, std::size_t limit, +OverlayImpl::selectPeers( + PeerSet& set, + std::size_t limit, std::function const&)> score) { using item = std::pair>; @@ -855,17 +886,14 @@ OverlayImpl::selectPeers (PeerSet& set, std::size_t limit, std::vector v; v.reserve(size()); - for_each ([&](std::shared_ptr&& e) - { + for_each([&](std::shared_ptr&& e) { auto const s = e->getScore(score(e)); v.emplace_back(s, std::move(e)); }); - std::sort(v.begin(), v.end(), - [](item const& lhs, item const&rhs) - { - return lhs.first > rhs.first; - }); + std::sort(v.begin(), v.end(), [](item const& lhs, item const& rhs) { + return lhs.first > rhs.first; + }); std::size_t accepted = 0; for (auto const& e : v) @@ -883,8 +911,8 @@ OverlayImpl::selectPeers (PeerSet& set, std::size_t limit, std::size_t OverlayImpl::size() { - std::lock_guard lock (mutex_); - return ids_.size (); + std::lock_guard lock(mutex_); + return ids_.size(); } int @@ -900,17 +928,13 @@ OverlayImpl::getOverlayInfo() Json::Value jv; auto& av = jv["active"] = Json::Value(Json::arrayValue); - for_each ([&](std::shared_ptr&& sp) - { + for_each([&](std::shared_ptr&& sp) { auto& pv = av.append(Json::Value(Json::objectValue)); pv[jss::public_key] = base64_encode( - sp->getNodePublic().data(), - sp->getNodePublic().size()); - pv[jss::type] = sp->slot()->inbound() ? - "in" : "out"; - pv[jss::uptime] = - static_cast(duration_cast( - sp->uptime()).count()); + sp->getNodePublic().data(), sp->getNodePublic().size()); + pv[jss::type] = sp->slot()->inbound() ? "in" : "out"; + pv[jss::uptime] = static_cast( + duration_cast(sp->uptime()).count()); if (sp->crawl()) { pv[jss::ip] = sp->getRemoteAddress().address().to_string(); @@ -921,13 +945,12 @@ OverlayImpl::getOverlayInfo() } else { - pv[jss::port] = std::to_string( - sp->getRemoteAddress().port()); + pv[jss::port] = std::to_string(sp->getRemoteAddress().port()); } } { - auto version {sp->getVersion()}; + auto version{sp->getVersion()}; if (!version.empty()) pv[jss::version] = std::move(version); } @@ -936,8 +959,7 @@ OverlayImpl::getOverlayInfo() sp->ledgerRange(minSeq, maxSeq); if (minSeq != 0 || maxSeq != 0) pv[jss::complete_ledgers] = - std::to_string(minSeq) + "-" + - std::to_string(maxSeq); + std::to_string(minSeq) + "-" + std::to_string(maxSeq); if (auto shardIndexes = sp->getShardIndexes()) pv[jss::complete_shards] = to_string(*shardIndexes); @@ -953,7 +975,8 @@ OverlayImpl::getServerInfo() bool const admin = false; bool const counters = false; - Json::Value server_info = app_.getOPs().getServerInfo(humanReadable, admin, counters); + Json::Value server_info = + app_.getOPs().getServerInfo(humanReadable, admin, counters); // Filter out some information server_info.removeMember(jss::hostid); @@ -1002,7 +1025,8 @@ OverlayImpl::getUnlInfo() if (validatorSites.isMember(jss::validator_sites)) { - validators[jss::validator_sites] = std::move(validatorSites[jss::validator_sites]); + validators[jss::validator_sites] = + std::move(validatorSites[jss::validator_sites]); } return validators; @@ -1010,16 +1034,16 @@ OverlayImpl::getUnlInfo() // Returns information on verified peers. Json::Value -OverlayImpl::json () +OverlayImpl::json() { return foreach (get_peer_json()); } bool -OverlayImpl::processCrawl (http_request_type const& req, - Handoff& handoff) +OverlayImpl::processCrawl(http_request_type const& req, Handoff& handoff) { - if (req.target() != "/crawl" || setup_.crawlOptions == CrawlOptions::Disabled) + if (req.target() != "/crawl" || + setup_.crawlOptions == CrawlOptions::Disabled) return false; boost::beast::http::response msg; @@ -1053,13 +1077,13 @@ OverlayImpl::processCrawl (http_request_type const& req, } bool -OverlayImpl::processValidatorList (http_request_type const& req, +OverlayImpl::processValidatorList( + http_request_type const& req, Handoff& handoff) { // If the target is in the form "/vl/", // return the most recent validator list for that key. - if (!req.target().starts_with("/vl/") || - !setup_.vlEnabled) + if (!req.target().starts_with("/vl/") || !setup_.vlEnabled) return false; auto key = req.target(); @@ -1067,7 +1091,7 @@ OverlayImpl::processValidatorList (http_request_type const& req, key.remove_prefix(strlen("/vl/")); else key.remove_prefix(strlen("/unl/")); - if(key.empty()) + if (key.empty()) return false; // find the list @@ -1100,12 +1124,10 @@ OverlayImpl::processValidatorList (http_request_type const& req, } bool -OverlayImpl::processRequest (http_request_type const& req, - Handoff& handoff) +OverlayImpl::processRequest(http_request_type const& req, Handoff& handoff) { // Take advantage of || short-circuiting - return processCrawl(req, handoff) || - processValidatorList(req, handoff); + return processCrawl(req, handoff) || processValidatorList(req, handoff); } Overlay::PeerSequence @@ -1114,8 +1136,7 @@ OverlayImpl::getActivePeers() Overlay::PeerSequence ret; ret.reserve(size()); - for_each ([&ret](std::shared_ptr&& sp) - { + for_each([&ret](std::shared_ptr&& sp) { ret.emplace_back(std::move(sp)); }); @@ -1123,29 +1144,24 @@ OverlayImpl::getActivePeers() } void -OverlayImpl::checkSanity (std::uint32_t index) +OverlayImpl::checkSanity(std::uint32_t index) { - for_each ([index](std::shared_ptr&& sp) - { - sp->checkSanity (index); - }); + for_each( + [index](std::shared_ptr&& sp) { sp->checkSanity(index); }); } void -OverlayImpl::check () +OverlayImpl::check() { - for_each ([](std::shared_ptr&& sp) - { - sp->check (); - }); + for_each([](std::shared_ptr&& sp) { sp->check(); }); } std::shared_ptr -OverlayImpl::findPeerByShortID (Peer::id_t const& id) +OverlayImpl::findPeerByShortID(Peer::id_t const& id) { - std::lock_guard lock (mutex_); - auto const iter = ids_.find (id); - if (iter != ids_.end ()) + std::lock_guard lock(mutex_); + auto const iter = ids_.find(id); + if (iter != ids_.end()) return iter->second.lock(); return {}; } @@ -1153,7 +1169,7 @@ OverlayImpl::findPeerByShortID (Peer::id_t const& id) // A public key hash map was not used due to the peer connect/disconnect // update overhead outweighing the performance of a small set linear search. std::shared_ptr -OverlayImpl::findPeerByPublicKey (PublicKey const& pubKey) +OverlayImpl::findPeerByPublicKey(PublicKey const& pubKey) { std::lock_guard lock(mutex_); for (auto const& e : ids_) @@ -1168,47 +1184,41 @@ OverlayImpl::findPeerByPublicKey (PublicKey const& pubKey) } void -OverlayImpl::send (protocol::TMProposeSet& m) +OverlayImpl::send(protocol::TMProposeSet& m) { if (setup_.expire) m.set_hops(0); auto const sm = std::make_shared(m, protocol::mtPROPOSE_LEDGER); - for_each([&](std::shared_ptr&& p) - { - p->send(sm); - }); + for_each([&](std::shared_ptr&& p) { p->send(sm); }); } void -OverlayImpl::send (protocol::TMValidation& m) +OverlayImpl::send(protocol::TMValidation& m) { if (setup_.expire) m.set_hops(0); auto const sm = std::make_shared(m, protocol::mtVALIDATION); - for_each([&](std::shared_ptr&& p) - { - p->send(sm); - }); + for_each([&](std::shared_ptr&& p) { p->send(sm); }); - SerialIter sit (m.validation().data(), m.validation().size()); + SerialIter sit(m.validation().data(), m.validation().size()); auto val = std::make_shared( std::ref(sit), [this](PublicKey const& pk) { return calcNodeID(app_.validatorManifests().getMasterKey(pk)); }, false); - app_.getOPs().pubValidation (val); + app_.getOPs().pubValidation(val); } void -OverlayImpl::relay (protocol::TMProposeSet& m, uint256 const& uid) +OverlayImpl::relay(protocol::TMProposeSet& m, uint256 const& uid) { if (m.has_hops() && m.hops() >= maxTTL) return; if (auto const toSkip = app_.getHashRouter().shouldRelay(uid)) { - auto const sm = std::make_shared(m, protocol::mtPROPOSE_LEDGER); - for_each([&](std::shared_ptr&& p) - { + auto const sm = + std::make_shared(m, protocol::mtPROPOSE_LEDGER); + for_each([&](std::shared_ptr&& p) { if (toSkip->find(p->id()) == toSkip->end()) p->send(sm); }); @@ -1216,15 +1226,14 @@ OverlayImpl::relay (protocol::TMProposeSet& m, uint256 const& uid) } void -OverlayImpl::relay (protocol::TMValidation& m, uint256 const& uid) +OverlayImpl::relay(protocol::TMValidation& m, uint256 const& uid) { if (m.has_hops() && m.hops() >= maxTTL) return; if (auto const toSkip = app_.getHashRouter().shouldRelay(uid)) { auto const sm = std::make_shared(m, protocol::mtVALIDATION); - for_each([&](std::shared_ptr&& p) - { + for_each([&](std::shared_ptr&& p) { if (toSkip->find(p->id()) == toSkip->end()) p->send(sm); }); @@ -1234,7 +1243,7 @@ OverlayImpl::relay (protocol::TMValidation& m, uint256 const& uid) //------------------------------------------------------------------------------ void -OverlayImpl::remove (Child& child) +OverlayImpl::remove(Child& child) { std::lock_guard lock(mutex_); list_.erase(&child); @@ -1260,12 +1269,12 @@ OverlayImpl::stop() return; work_ = boost::none; - children.reserve (list_.size()); + children.reserve(list_.size()); for (auto const& element : list_) { - children.emplace_back (element.second.lock()); + children.emplace_back(element.second.lock()); } - } // lock released + } // lock released for (auto const& child : children) { @@ -1279,7 +1288,7 @@ OverlayImpl::autoConnect() { auto const result = m_peerFinder->autoconnect(); for (auto addr : result) - connect (addr); + connect(addr); } void @@ -1290,34 +1299,36 @@ OverlayImpl::sendEndpoints() { std::shared_ptr peer; { - std::lock_guard lock (mutex_); - auto const iter = m_peers.find (e.first); + std::lock_guard lock(mutex_); + auto const iter = m_peers.find(e.first); if (iter != m_peers.end()) peer = iter->second.lock(); } if (peer) - peer->sendEndpoints (e.second.begin(), e.second.end()); + peer->sendEndpoints(e.second.begin(), e.second.end()); } } //------------------------------------------------------------------------------ -bool ScoreHasLedger::operator()(std::shared_ptr const& bp) const +bool +ScoreHasLedger::operator()(std::shared_ptr const& bp) const { auto const& p = std::dynamic_pointer_cast(bp); - return p->hasLedger (hash_, seq_); + return p->hasLedger(hash_, seq_); } -bool ScoreHasTxSet::operator()(std::shared_ptr const& bp) const +bool +ScoreHasTxSet::operator()(std::shared_ptr const& bp) const { auto const& p = std::dynamic_pointer_cast(bp); - return p->hasTxSet (hash_); + return p->hasTxSet(hash_); } //------------------------------------------------------------------------------ Overlay::Setup -setup_Overlay (BasicConfig const& config) +setup_Overlay(BasicConfig const& config) { Overlay::Setup setup; @@ -1363,7 +1374,8 @@ setup_Overlay (BasicConfig const& config) catch (boost::bad_lexical_cast const&) { Throw( - "Configured [crawl] section has invalid value: " + values.front()); + "Configured [crawl] section has invalid value: " + + values.front()); } } @@ -1421,8 +1433,8 @@ setup_Overlay (BasicConfig const& config) return setup; } -std::unique_ptr -make_Overlay ( +std::unique_ptr +make_Overlay( Application& app, Overlay::Setup const& setup, Stoppable& parent, @@ -1433,8 +1445,16 @@ make_Overlay ( BasicConfig const& config, beast::insight::Collector::ptr const& collector) { - return std::make_unique(app, setup, parent, serverHandler, - resourceManager, resolver, io_service, config, collector); + return std::make_unique( + app, + setup, + parent, + serverHandler, + resourceManager, + resolver, + io_service, + config, + collector); } -} +} // namespace ripple diff --git a/src/ripple/overlay/impl/OverlayImpl.h b/src/ripple/overlay/impl/OverlayImpl.h index f04c7129bd..a87db3cc31 100644 --- a/src/ripple/overlay/impl/OverlayImpl.h +++ b/src/ripple/overlay/impl/OverlayImpl.h @@ -21,21 +21,21 @@ #define RIPPLE_OVERLAY_OVERLAYIMPL_H_INCLUDED #include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -#include #include +#include #include #include +#include +#include +#include #include #include #include -#include #include #include #include @@ -62,13 +62,13 @@ public: protected: OverlayImpl& overlay_; - explicit - Child (OverlayImpl& overlay); + explicit Child(OverlayImpl& overlay); virtual ~Child(); public: - virtual void stop() = 0; + virtual void + stop() = 0; }; private: @@ -78,14 +78,11 @@ private: using endpoint_type = boost::asio::ip::tcp::endpoint; using error_code = boost::system::error_code; - struct Timer - : Child - , std::enable_shared_from_this + struct Timer : Child, std::enable_shared_from_this { - boost::asio::basic_waitable_timer timer_; + boost::asio::basic_waitable_timer timer_; - explicit - Timer (OverlayImpl& overlay); + explicit Timer(OverlayImpl& overlay); void stop() override; @@ -94,33 +91,31 @@ private: run(); void - on_timer (error_code ec); + on_timer(error_code ec); }; Application& app_; boost::asio::io_service& io_service_; boost::optional work_; boost::asio::io_service::strand strand_; - std::recursive_mutex mutex_; // VFALCO use std::mutex + std::recursive_mutex mutex_; // VFALCO use std::mutex std::condition_variable_any cond_; std::weak_ptr timer_; - boost::container::flat_map< - Child*, std::weak_ptr> list_; + boost::container::flat_map> list_; Setup setup_; beast::Journal const journal_; ServerHandler& serverHandler_; Resource::Manager& m_resourceManager; - std::unique_ptr m_peerFinder; + std::unique_ptr m_peerFinder; TrafficCount m_traffic; - hash_map , - std::weak_ptr > m_peers; + hash_map, std::weak_ptr> m_peers; hash_map> ids_; Resolver& m_resolver; - std::atomic next_id_; + std::atomic next_id_; int timer_count_; - std::atomic jqTransOverflow_ {0}; - std::atomic peerDisconnects_ {0}; - std::atomic peerDisconnectsCharges_ {0}; + std::atomic jqTransOverflow_{0}; + std::atomic peerDisconnects_{0}; + std::atomic peerDisconnectsCharges_{0}; // Last time we crawled peers for shard info. 'cs' = crawl shards std::atomic csLast_{std::chrono::seconds{0}}; @@ -134,15 +129,22 @@ private: //-------------------------------------------------------------------------- public: - OverlayImpl (Application& app, Setup const& setup, Stoppable& parent, - ServerHandler& serverHandler, Resource::Manager& resourceManager, - Resolver& resolver, boost::asio::io_service& io_service, - BasicConfig const& config, beast::insight::Collector::ptr const& collector); + OverlayImpl( + Application& app, + Setup const& setup, + Stoppable& parent, + ServerHandler& serverHandler, + Resource::Manager& resourceManager, + Resolver& resolver, + boost::asio::io_service& io_service, + BasicConfig const& config, + beast::insight::Collector::ptr const& collector); ~OverlayImpl(); - OverlayImpl (OverlayImpl const&) = delete; - OverlayImpl& operator= (OverlayImpl const&) = delete; + OverlayImpl(OverlayImpl const&) = delete; + OverlayImpl& + operator=(OverlayImpl const&) = delete; PeerFinder::Manager& peerFinder() @@ -169,9 +171,10 @@ public: } Handoff - onHandoff (std::unique_ptr && bundle, + onHandoff( + std::unique_ptr&& bundle, http_request_type&& request, - endpoint_type remote_endpoint) override; + endpoint_type remote_endpoint) override; void connect(beast::IP::Endpoint const& remote_endpoint) override; @@ -189,30 +192,27 @@ public: getActivePeers() override; void - check () override; + check() override; - void - checkSanity (std::uint32_t) override; + void checkSanity(std::uint32_t) override; std::shared_ptr - findPeerByShortID (Peer::id_t const& id) override; + findPeerByShortID(Peer::id_t const& id) override; std::shared_ptr - findPeerByPublicKey (PublicKey const& pubKey) override; + findPeerByPublicKey(PublicKey const& pubKey) override; void - send (protocol::TMProposeSet& m) override; + send(protocol::TMProposeSet& m) override; void - send (protocol::TMValidation& m) override; + send(protocol::TMValidation& m) override; void - relay (protocol::TMProposeSet& m, - uint256 const& uid) override; + relay(protocol::TMProposeSet& m, uint256 const& uid) override; void - relay (protocol::TMValidation& m, - uint256 const& uid) override; + relay(protocol::TMValidation& m, uint256 const& uid) override; //-------------------------------------------------------------------------- // @@ -220,10 +220,10 @@ public: // void - add_active (std::shared_ptr const& peer); + add_active(std::shared_ptr const& peer); void - remove (std::shared_ptr const& slot); + remove(std::shared_ptr const& slot); /** Called when a peer has connected successfully This is called after the peer handshake has been completed and during @@ -231,18 +231,18 @@ public: are known. */ void - activate (std::shared_ptr const& peer); + activate(std::shared_ptr const& peer); // Called when an active peer is destroyed. void - onPeerDeactivate (Peer::id_t id); + onPeerDeactivate(Peer::id_t id); // UnaryFunc will be called as // void(std::shared_ptr&&) // template void - for_each (UnaryFunc&& f) + for_each(UnaryFunc&& f) { std::vector> wp; { @@ -264,64 +264,61 @@ public: } std::size_t - selectPeers (PeerSet& set, std::size_t limit, std::function< - bool(std::shared_ptr const&)> score) override; + selectPeers( + PeerSet& set, + std::size_t limit, + std::function const&)> score) override; // Called when TMManifests is received from a peer void - onManifests ( + onManifests( std::shared_ptr const& m, - std::shared_ptr const& from); + std::shared_ptr const& from); - static - bool - isPeerUpgrade (http_request_type const& request); + static bool + isPeerUpgrade(http_request_type const& request); - template - static - bool - isPeerUpgrade (boost::beast::http::response const& response) + template + static bool + isPeerUpgrade(boost::beast::http::response const& response) { - if (! is_upgrade(response)) + if (!is_upgrade(response)) return false; - return response.result() == boost::beast::http::status::switching_protocols; + return response.result() == + boost::beast::http::status::switching_protocols; } - template - static - bool + template + static bool is_upgrade(boost::beast::http::header const& req) { - if(req.version() < 11) + if (req.version() < 11) return false; - if(req.method() != boost::beast::http::verb::get) + if (req.method() != boost::beast::http::verb::get) return false; - if(! boost::beast::http::token_list{req["Connection"]}.exists("upgrade")) + if (!boost::beast::http::token_list{req["Connection"]}.exists( + "upgrade")) return false; return true; } - template - static - bool + template + static bool is_upgrade(boost::beast::http::header const& req) { - if(req.version() < 11) + if (req.version() < 11) return false; - if(! boost::beast::http::token_list{req["Connection"]}.exists("upgrade")) + if (!boost::beast::http::token_list{req["Connection"]}.exists( + "upgrade")) return false; return true; } - static - std::string - makePrefix (std::uint32_t id); + static std::string + makePrefix(std::uint32_t id); void - reportTraffic ( - TrafficCount::category cat, - bool isInbound, - int bytes); + reportTraffic(TrafficCount::category cat, bool isInbound, int bytes); void incJqTransOverflow() override @@ -377,12 +374,16 @@ public: private: std::shared_ptr - makeRedirectResponse (std::shared_ptr const& slot, - http_request_type const& request, address_type remote_address); + makeRedirectResponse( + std::shared_ptr const& slot, + http_request_type const& request, + address_type remote_address); std::shared_ptr - makeErrorResponse (std::shared_ptr const& slot, - http_request_type const& request, address_type remote_address, + makeErrorResponse( + std::shared_ptr const& slot, + http_request_type const& request, + address_type remote_address, std::string msg); /** Handles crawl requests. Crawl returns information about the @@ -391,8 +392,7 @@ private: @return true if the request was handled. */ bool - processCrawl (http_request_type const& req, - Handoff& handoff); + processCrawl(http_request_type const& req, Handoff& handoff); /** Handles validator list requests. Using a /vl/ URL, will retrieve the @@ -402,16 +402,14 @@ private: @return true if the request was handled. */ bool - processValidatorList (http_request_type const& req, - Handoff& handoff); + processValidatorList(http_request_type const& req, Handoff& handoff); /** Handles non-peer protocol requests. @return true if the request was handled. */ bool - processRequest (http_request_type const& req, - Handoff& handoff); + processRequest(http_request_type const& req, Handoff& handoff); /** Returns information about peers on the overlay network. Reported through the /crawl API @@ -467,12 +465,12 @@ private: // void - onWrite (beast::PropertyStream::Map& stream) override; + onWrite(beast::PropertyStream::Map& stream) override; //-------------------------------------------------------------------------- void - remove (Child& child); + remove(Child& child); void stop(); @@ -484,14 +482,16 @@ private: sendEndpoints(); private: - - struct TrafficGauges{ - TrafficGauges (char const* name, beast::insight::Collector::ptr const& collector) - : bytesIn(collector->make_gauge(name,"Bytes_In")) - , bytesOut(collector->make_gauge(name,"Bytes_Out")) - , messagesIn(collector->make_gauge(name,"Messages_In")) - , messagesOut(collector->make_gauge(name,"Messages_Out")) - { + struct TrafficGauges + { + TrafficGauges( + char const* name, + beast::insight::Collector::ptr const& collector) + : bytesIn(collector->make_gauge(name, "Bytes_In")) + , bytesOut(collector->make_gauge(name, "Bytes_Out")) + , messagesIn(collector->make_gauge(name, "Messages_In")) + , messagesOut(collector->make_gauge(name, "Messages_Out")) + { } beast::insight::Gauge bytesIn; beast::insight::Gauge bytesOut; @@ -499,36 +499,36 @@ private: beast::insight::Gauge messagesOut; }; - struct Stats { - template - Stats ( - Handler const& handler, - beast::insight::Collector::ptr const& collector, - std::vector&& trafficGauges_) - : peerDisconnects (collector->make_gauge("Overlay","Peer_Disconnects")) - , trafficGauges (std::move(trafficGauges_)) - , hook (collector->make_hook (handler)) - { - } - + Stats( + Handler const& handler, + beast::insight::Collector::ptr const& collector, + std::vector&& trafficGauges_) + : peerDisconnects( + collector->make_gauge("Overlay", "Peer_Disconnects")) + , trafficGauges(std::move(trafficGauges_)) + , hook(collector->make_hook(handler)) + { + } + beast::insight::Gauge peerDisconnects; std::vector trafficGauges; beast::insight::Hook hook; }; - + Stats m_stats; std::mutex m_statsMutex; private: - void collect_metrics() - { + void + collect_metrics() + { auto counts = m_traffic.getCounts(); - std::lock_guard lock (m_statsMutex); + std::lock_guard lock(m_statsMutex); assert(counts.size() == m_stats.trafficGauges.size()); - + for (std::size_t i = 0; i < counts.size(); ++i) { m_stats.trafficGauges[i].bytesIn = counts[i].bytesIn; @@ -540,6 +540,6 @@ private: } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/PeerImp.cpp b/src/ripple/overlay/impl/PeerImp.cpp index 0733bfe99a..6a780f1c23 100644 --- a/src/ripple/overlay/impl/PeerImp.cpp +++ b/src/ripple/overlay/impl/PeerImp.cpp @@ -17,87 +17,93 @@ */ //============================================================================== -#include -#include #include #include -#include #include +#include #include #include #include #include #include #include +#include #include #include #include -#include #include #include #include #include +#include +#include #include #include #include -#include #include +#include #include #include #include -#include #include +#include using namespace std::chrono_literals; namespace ripple { -PeerImp::PeerImp (Application& app, id_t id, - std::shared_ptr const& slot, http_request_type&& request, - PublicKey const& publicKey, - ProtocolVersion protocol, Resource::Consumer consumer, - std::unique_ptr&& stream_ptr, - OverlayImpl& overlay) - : Child (overlay) - , app_ (app) +PeerImp::PeerImp( + Application& app, + id_t id, + std::shared_ptr const& slot, + http_request_type&& request, + PublicKey const& publicKey, + ProtocolVersion protocol, + Resource::Consumer consumer, + std::unique_ptr&& stream_ptr, + OverlayImpl& overlay) + : Child(overlay) + , app_(app) , id_(id) , sink_(app_.journal("Peer"), makePrefix(id)) , p_sink_(app_.journal("Protocol"), makePrefix(id)) - , journal_ (sink_) + , journal_(sink_) , p_journal_(p_sink_) , stream_ptr_(std::move(stream_ptr)) - , socket_ (stream_ptr_->next_layer().socket()) - , stream_ (*stream_ptr_) - , strand_ (socket_.get_executor()) - , timer_ (waitable_timer{socket_.get_executor()}) - , remote_address_ (slot->remote_endpoint()) - , overlay_ (overlay) - , m_inbound (true) - , protocol_ (protocol) - , state_ (State::active) - , sanity_ (Sanity::unknown) - , insaneTime_ (clock_type::now()) + , socket_(stream_ptr_->next_layer().socket()) + , stream_(*stream_ptr_) + , strand_(socket_.get_executor()) + , timer_(waitable_timer{socket_.get_executor()}) + , remote_address_(slot->remote_endpoint()) + , overlay_(overlay) + , m_inbound(true) + , protocol_(protocol) + , state_(State::active) + , sanity_(Sanity::unknown) + , insaneTime_(clock_type::now()) , publicKey_(publicKey) - , creationTime_ (clock_type::now()) + , creationTime_(clock_type::now()) , usage_(consumer) - , fee_ (Resource::feeLightPeer) - , slot_ (slot) + , fee_(Resource::feeLightPeer) + , slot_(slot) , request_(std::move(request)) , headers_(request_) - , compressionEnabled_(headers_["X-Offer-Compression"] == "lz4" ? Compressed::On : Compressed::Off) + , compressionEnabled_( + headers_["X-Offer-Compression"] == "lz4" ? Compressed::On + : Compressed::Off) { } -PeerImp::~PeerImp () +PeerImp::~PeerImp() { - const bool inCluster {cluster()}; + const bool inCluster{cluster()}; if (state_ == State::active) overlay_.onPeerDeactivate(id_); - overlay_.peerFinder().on_closed (slot_); - overlay_.remove (slot_); + overlay_.peerFinder().on_closed(slot_); + overlay_.remove(slot_); if (inCluster) { @@ -106,9 +112,8 @@ PeerImp::~PeerImp () } // Helper function to check for valid uint256 values in protobuf buffers -static -bool -stringIsUint256Sized (std::string const& pBuffStr) +static bool +stringIsUint256Sized(std::string const& pBuffStr) { return pBuffStr.size() == uint256::size(); } @@ -116,26 +121,27 @@ stringIsUint256Sized (std::string const& pBuffStr) void PeerImp::run() { - if(! strand_.running_in_this_thread()) + if (!strand_.running_in_this_thread()) return post(strand_, std::bind(&PeerImp::run, shared_from_this())); // We need to decipher - auto parseLedgerHash = [](std::string const& value) -> boost::optional - { + auto parseLedgerHash = + [](std::string const& value) -> boost::optional { uint256 ret; if (ret.SetHexExact(value)) - return { ret }; + return {ret}; auto const s = base64_decode(value); if (s.size() != uint256::size()) return boost::none; - return uint256{ s }; + return uint256{s}; }; boost::optional closed; boost::optional previous; - if (auto const iter = headers_.find("Closed-Ledger"); iter != headers_.end()) + if (auto const iter = headers_.find("Closed-Ledger"); + iter != headers_.end()) { closed = parseLedgerHash(iter->value().to_string()); @@ -143,7 +149,8 @@ PeerImp::run() fail("Malformed handshake data (1)"); } - if (auto const iter = headers_.find("Previous-Ledger"); iter != headers_.end()) + if (auto const iter = headers_.find("Previous-Ledger"); + iter != headers_.end()) { previous = parseLedgerHash(iter->value().to_string()); @@ -168,9 +175,10 @@ PeerImp::run() } else { - assert (state_ == State::active); + assert(state_ == State::active); // XXX Set timer: connection is in grace period to be useful. - // XXX Set timer: connection idle (idle may vary depending on connection type.) + // XXX Set timer: connection idle (idle may vary depending on connection + // type.) doProtocolStart(); } @@ -185,7 +193,7 @@ PeerImp::run() void PeerImp::stop() { - if(! strand_.running_in_this_thread()) + if (!strand_.running_in_this_thread()) return post(strand_, std::bind(&PeerImp::stop, shared_from_this())); if (socket_.is_open()) { @@ -194,7 +202,7 @@ PeerImp::stop() // at a higher level, but inbound connections are more numerous and // uncontrolled so to prevent log flooding the severity is reduced. // - if(m_inbound) + if (m_inbound) { JLOG(journal_.debug()) << "Stop"; } @@ -209,18 +217,19 @@ PeerImp::stop() //------------------------------------------------------------------------------ void -PeerImp::send (std::shared_ptr const& m) +PeerImp::send(std::shared_ptr const& m) { - if (! strand_.running_in_this_thread()) + if (!strand_.running_in_this_thread()) return post(strand_, std::bind(&PeerImp::send, shared_from_this(), m)); - if(gracefulClose_) + if (gracefulClose_) return; - if(detaching_) + if (detaching_) return; - overlay_.reportTraffic ( + overlay_.reportTraffic( safe_cast(m->getCategory()), - false, static_cast(m->getBuffer(compressionEnabled_).size())); + false, + static_cast(m->getBuffer(compressionEnabled_).size())); auto sendq_size = send_queue_.size(); @@ -231,23 +240,25 @@ PeerImp::send (std::shared_ptr const& m) // a small senq periodically large_sendq_ = 0; } - else if (journal_.active (beast::severities::kDebug) && + else if ( + journal_.active(beast::severities::kDebug) && (sendq_size % Tuning::sendQueueLogFreq) == 0) { - std::string const name {getName()}; - JLOG (journal_.debug()) << - (name.empty() ? remote_address_.to_string() : name) << - " sendq: " << sendq_size; + std::string const name{getName()}; + JLOG(journal_.debug()) + << (name.empty() ? remote_address_.to_string() : name) + << " sendq: " << sendq_size; } send_queue_.push(m); - if(sendq_size != 0) + if (sendq_size != 0) return; boost::asio::async_write( stream_, - boost::asio::buffer(send_queue_.front()->getBuffer(compressionEnabled_)), + boost::asio::buffer( + send_queue_.front()->getBuffer(compressionEnabled_)), bind_executor( strand_, std::bind( @@ -258,10 +269,10 @@ PeerImp::send (std::shared_ptr const& m) } void -PeerImp::charge (Resource::Charge const& fee) +PeerImp::charge(Resource::Charge const& fee) { - if ((usage_.charge(fee) == Resource::drop) && - usage_.disconnect() && strand_.running_in_this_thread()) + if ((usage_.charge(fee) == Resource::drop) && usage_.disconnect() && + strand_.running_in_this_thread()) { // Sever the connection overlay_.incPeerDisconnectCharges(); @@ -297,11 +308,10 @@ PeerImp::getVersion() const Json::Value PeerImp::json() { - Json::Value ret (Json::objectValue); + Json::Value ret(Json::objectValue); - ret[jss::public_key] = toBase58 ( - TokenType::NodePublic, publicKey_); - ret[jss::address] = remote_address_.to_string(); + ret[jss::public_key] = toBase58(TokenType::NodePublic, publicKey_); + ret[jss::address] = remote_address_.to_string(); if (m_inbound) ret[jss::inbound] = true; @@ -310,12 +320,12 @@ PeerImp::json() { ret[jss::cluster] = true; - std::string name {getName()}; - if (!name.empty ()) + std::string name{getName()}; + if (!name.empty()) ret[jss::name] = std::move(name); } - ret[jss::load] = usage_.balance (); + ret[jss::load] = usage_.balance(); { auto const version = getVersion(); @@ -323,12 +333,12 @@ PeerImp::json() ret[jss::version] = version; } - ret[jss::protocol] = to_string (protocol_); + ret[jss::protocol] = to_string(protocol_); { - std::lock_guard sl (recentLock_); + std::lock_guard sl(recentLock_); if (latency_) - ret[jss::latency] = static_cast (latency_->count()); + ret[jss::latency] = static_cast(latency_->count()); } ret[jss::uptime] = static_cast( @@ -338,10 +348,10 @@ PeerImp::json() ledgerRange(minSeq, maxSeq); if ((minSeq != 0) || (maxSeq != 0)) - ret[jss::complete_ledgers] = std::to_string(minSeq) + - " - " + std::to_string(maxSeq); + ret[jss::complete_ledgers] = + std::to_string(minSeq) + " - " + std::to_string(maxSeq); - switch (sanity_.load ()) + switch (sanity_.load()) { case Sanity::insane: ret[jss::sanity] = "insane"; @@ -365,43 +375,47 @@ PeerImp::json() } if (closedLedgerHash != beast::zero) - ret[jss::ledger] = to_string (closedLedgerHash); + ret[jss::ledger] = to_string(closedLedgerHash); - if (last_status.has_newstatus ()) + if (last_status.has_newstatus()) { - switch (last_status.newstatus ()) + switch (last_status.newstatus()) { - case protocol::nsCONNECTING: - ret[jss::status] = "connecting"; - break; + case protocol::nsCONNECTING: + ret[jss::status] = "connecting"; + break; - case protocol::nsCONNECTED: - ret[jss::status] = "connected"; - break; + case protocol::nsCONNECTED: + ret[jss::status] = "connected"; + break; - case protocol::nsMONITORING: - ret[jss::status] = "monitoring"; - break; + case protocol::nsMONITORING: + ret[jss::status] = "monitoring"; + break; - case protocol::nsVALIDATING: - ret[jss::status] = "validating"; - break; + case protocol::nsVALIDATING: + ret[jss::status] = "validating"; + break; - case protocol::nsSHUTTING: - ret[jss::status] = "shutting"; - break; + case protocol::nsSHUTTING: + ret[jss::status] = "shutting"; + break; - default: - JLOG(p_journal_.warn()) << - "Unknown status: " << last_status.newstatus (); + default: + JLOG(p_journal_.warn()) + << "Unknown status: " << last_status.newstatus(); } } ret[jss::metrics] = Json::Value(Json::objectValue); - ret[jss::metrics][jss::total_bytes_recv] = std::to_string(metrics_.recv.total_bytes()); - ret[jss::metrics][jss::total_bytes_sent] = std::to_string(metrics_.sent.total_bytes()); - ret[jss::metrics][jss::avg_bps_recv] = std::to_string(metrics_.recv.average_bytes()); - ret[jss::metrics][jss::avg_bps_sent] = std::to_string(metrics_.sent.average_bytes()); + ret[jss::metrics][jss::total_bytes_recv] = + std::to_string(metrics_.recv.total_bytes()); + ret[jss::metrics][jss::total_bytes_sent] = + std::to_string(metrics_.sent.total_bytes()); + ret[jss::metrics][jss::avg_bps_recv] = + std::to_string(metrics_.recv.average_bytes()); + ret[jss::metrics][jss::avg_bps_sent] = + std::to_string(metrics_.sent.average_bytes()); return ret; } @@ -420,15 +434,15 @@ PeerImp::supportsFeature(ProtocolFeature f) const //------------------------------------------------------------------------------ bool -PeerImp::hasLedger (uint256 const& hash, std::uint32_t seq) const +PeerImp::hasLedger(uint256 const& hash, std::uint32_t seq) const { { std::lock_guard sl(recentLock_); if ((seq != 0) && (seq >= minLedger_) && (seq <= maxLedger_) && - (sanity_.load() == Sanity::sane)) + (sanity_.load() == Sanity::sane)) return true; - if (std::find(recentLedgers_.begin(), - recentLedgers_.end(), hash) != recentLedgers_.end()) + if (std::find(recentLedgers_.begin(), recentLedgers_.end(), hash) != + recentLedgers_.end()) return true; } @@ -437,8 +451,7 @@ PeerImp::hasLedger (uint256 const& hash, std::uint32_t seq) const } void -PeerImp::ledgerRange (std::uint32_t& minSeq, - std::uint32_t& maxSeq) const +PeerImp::ledgerRange(std::uint32_t& minSeq, std::uint32_t& maxSeq) const { std::lock_guard sl(recentLock_); @@ -447,39 +460,38 @@ PeerImp::ledgerRange (std::uint32_t& minSeq, } bool -PeerImp::hasShard (std::uint32_t shardIndex) const +PeerImp::hasShard(std::uint32_t shardIndex) const { - std::lock_guard l {shardInfoMutex_}; - auto const it {shardInfo_.find(publicKey_)}; + std::lock_guard l{shardInfoMutex_}; + auto const it{shardInfo_.find(publicKey_)}; if (it != shardInfo_.end()) return boost::icl::contains(it->second.shardIndexes, shardIndex); return false; } bool -PeerImp::hasTxSet (uint256 const& hash) const +PeerImp::hasTxSet(uint256 const& hash) const { std::lock_guard sl(recentLock_); - return std::find (recentTxSets_.begin(), - recentTxSets_.end(), hash) != recentTxSets_.end(); + return std::find(recentTxSets_.begin(), recentTxSets_.end(), hash) != + recentTxSets_.end(); } void -PeerImp::cycleStatus () +PeerImp::cycleStatus() { // Operations on closedLedgerHash_ and previousLedgerHash_ must be // guarded by recentLock_. std::lock_guard sl(recentLock_); previousLedgerHash_ = closedLedgerHash_; - closedLedgerHash_.zero (); + closedLedgerHash_.zero(); } bool -PeerImp::hasRange (std::uint32_t uMin, std::uint32_t uMax) +PeerImp::hasRange(std::uint32_t uMin, std::uint32_t uMax) { std::lock_guard sl(recentLock_); - return (sanity_ != Sanity::insane) && - (uMin >= minLedger_) && + return (sanity_ != Sanity::insane) && (uMin >= minLedger_) && (uMax <= maxLedger_); } @@ -491,12 +503,12 @@ PeerImp::close() assert(strand_.running_in_this_thread()); if (socket_.is_open()) { - detaching_ = true; // DEPRECATED + detaching_ = true; // DEPRECATED error_code ec; timer_.cancel(ec); socket_.close(ec); overlay_.incPeerDisconnect(); - if(m_inbound) + if (m_inbound) { JLOG(journal_.debug()) << "Closed"; } @@ -510,19 +522,19 @@ PeerImp::close() void PeerImp::fail(std::string const& reason) { - if(! strand_.running_in_this_thread()) + if (!strand_.running_in_this_thread()) return post( strand_, std::bind( (void (Peer::*)(std::string const&)) & PeerImp::fail, shared_from_this(), reason)); - if (journal_.active (beast::severities::kWarning) && socket_.is_open()) + if (journal_.active(beast::severities::kWarning) && socket_.is_open()) { - std::string const name {getName()}; - JLOG (journal_.warn()) << - (name.empty() ? remote_address_.to_string() : name) << - " failed: " << reason; + std::string const name{getName()}; + JLOG(journal_.warn()) + << (name.empty() ? remote_address_.to_string() : name) + << " failed: " << reason; } close(); } @@ -533,10 +545,9 @@ PeerImp::fail(std::string const& name, error_code ec) assert(strand_.running_in_this_thread()); if (socket_.is_open()) { - JLOG(journal_.warn()) << name << " from " << - toBase58(TokenType::NodePublic, publicKey_) << - " at " << remote_address_.to_string() << - ": " << ec.message(); + JLOG(journal_.warn()) + << name << " from " << toBase58(TokenType::NodePublic, publicKey_) + << " at " << remote_address_.to_string() << ": " << ec.message(); } close(); } @@ -544,7 +555,7 @@ PeerImp::fail(std::string const& name, error_code ec) boost::optional> PeerImp::getShardIndexes() const { - std::lock_guard l {shardInfoMutex_}; + std::lock_guard l{shardInfoMutex_}; auto it{shardInfo_.find(publicKey_)}; if (it != shardInfo_.end()) return it->second.shardIndexes; @@ -554,7 +565,7 @@ PeerImp::getShardIndexes() const boost::optional> PeerImp::getPeerShardInfo() const { - std::lock_guard l {shardInfoMutex_}; + std::lock_guard l{shardInfoMutex_}; if (!shardInfo_.empty()) return shardInfo_; return boost::none; @@ -565,7 +576,7 @@ PeerImp::gracefulClose() { assert(strand_.running_in_this_thread()); assert(socket_.is_open()); - assert(! gracefulClose_); + assert(!gracefulClose_); gracefulClose_ = true; #if 0 // Flush messages @@ -585,8 +596,7 @@ void PeerImp::setTimer() { error_code ec; - timer_.expires_from_now( std::chrono::seconds( - Tuning::timerSeconds), ec); + timer_.expires_from_now(std::chrono::seconds(Tuning::timerSeconds), ec); if (ec) { @@ -618,9 +628,9 @@ PeerImp::makePrefix(id_t id) } void -PeerImp::onTimer (error_code const& ec) +PeerImp::onTimer(error_code const& ec) { - if (! socket_.is_open()) + if (!socket_.is_open()) return; if (ec == boost::asio::error::operation_aborted) @@ -635,11 +645,11 @@ PeerImp::onTimer (error_code const& ec) if (large_sendq_++ >= Tuning::sendqIntervals) { - fail ("Large send queue"); + fail("Large send queue"); return; } - bool failedNoPing {false}; + bool failedNoPing{false}; boost::optional pingSeq; // Operations on lastPingSeq_, lastPingTime_, no_ping_, and latency_ // must be guarded by recentLock_. @@ -660,8 +670,8 @@ PeerImp::onTimer (error_code const& ec) { // We have an outstanding ping, raise latency auto const minLatency = - std::chrono::duration_cast - (clock_type::now() - lastPingTime_); + std::chrono::duration_cast( + clock_type::now() - lastPingTime_); if (latency_ < minLatency) latency_ = minLatency; @@ -670,17 +680,17 @@ PeerImp::onTimer (error_code const& ec) if (failedNoPing) { - fail ("No ping reply received"); + fail("No ping reply received"); return; } if (pingSeq) { protocol::TMPing message; - message.set_type (protocol::TMPing::ptPING); - message.set_seq (*pingSeq); + message.set_type(protocol::TMPing::ptPING); + message.set_seq(*pingSeq); - send (std::make_shared (message, protocol::mtPING)); + send(std::make_shared(message, protocol::mtPING)); } setTimer(); @@ -691,7 +701,7 @@ PeerImp::onShutdown(error_code ec) { cancelTimer(); // If we don't get eof then something went wrong - if (! ec) + if (!ec) { JLOG(journal_.error()) << "onShutdown: expected error condition"; return close(); @@ -703,7 +713,8 @@ PeerImp::onShutdown(error_code ec) //------------------------------------------------------------------------------ -void PeerImp::doAccept() +void +PeerImp::doAccept() { assert(read_buffer_.size() == 0); @@ -713,19 +724,20 @@ void PeerImp::doAccept() // This shouldn't fail since we already computed // the shared value successfully in OverlayImpl - if(! sharedValue) + if (!sharedValue) return fail("makeSharedValue: Unexpected failure"); // TODO Apply headers to connection state. boost::beast::ostream(write_buffer_) << makeResponse( - ! overlay_.peerFinder().config().peerPrivate, - request_, remote_address_.address(), *sharedValue); + !overlay_.peerFinder().config().peerPrivate, + request_, + remote_address_.address(), + *sharedValue); - JLOG(journal_.info()) << "Protocol: " << - to_string(protocol_); - JLOG(journal_.info()) << "Public Key: " << - toBase58 (TokenType::NodePublic, publicKey_); + JLOG(journal_.info()) << "Protocol: " << to_string(protocol_); + JLOG(journal_.info()) << "Public Key: " + << toBase58(TokenType::NodePublic, publicKey_); if (auto member = app_.cluster().member(publicKey_)) { @@ -739,13 +751,15 @@ void PeerImp::doAccept() overlay_.activate(shared_from_this()); // XXX Set timer: connection is in grace period to be useful. - // XXX Set timer: connection idle (idle may vary depending on connection type.) + // XXX Set timer: connection idle (idle may vary depending on connection + // type.) onWriteResponse(error_code(), 0); } http_response_type -PeerImp::makeResponse (bool crawl, +PeerImp::makeResponse( + bool crawl, http_request_type const& req, beast::IP::Address remote_ip, uint256 const& sharedValue) @@ -761,32 +775,36 @@ PeerImp::makeResponse (bool crawl, if (req["X-Offer-Compression"] == "lz4" && app_.config().COMPRESSION) resp.insert("X-Offer-Compression", "lz4"); - buildHandshake(resp, sharedValue, overlay_.setup().networkID, - overlay_.setup().public_ip, remote_ip, app_); + buildHandshake( + resp, + sharedValue, + overlay_.setup().networkID, + overlay_.setup().public_ip, + remote_ip, + app_); return resp; } // Called repeatedly to send the bytes in the response void -PeerImp::onWriteResponse (error_code ec, std::size_t bytes_transferred) +PeerImp::onWriteResponse(error_code ec, std::size_t bytes_transferred) { - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; - if(ec) + if (ec) return fail("onWriteResponse", ec); - if(auto stream = journal_.trace()) + if (auto stream = journal_.trace()) { if (bytes_transferred > 0) - stream << - "onWriteResponse: " << bytes_transferred << " bytes"; + stream << "onWriteResponse: " << bytes_transferred << " bytes"; else stream << "onWriteResponse"; } - write_buffer_.consume (bytes_transferred); + write_buffer_.consume(bytes_transferred); if (write_buffer_.size() == 0) return doProtocolStart(); @@ -820,39 +838,37 @@ PeerImp::doProtocolStart() // Send all the validator lists that have been loaded if (supportsFeature(ProtocolFeature::ValidatorListPropagation)) { - app_.validators().for_each_available( - [&](std::string const& manifest, - std::string const& blob, std::string const& signature, - std::uint32_t version, - PublicKey const& pubKey, std::size_t sequence, - uint256 const& hash) - { - protocol::TMValidatorList vl; + app_.validators().for_each_available([&](std::string const& manifest, + std::string const& blob, + std::string const& signature, + std::uint32_t version, + PublicKey const& pubKey, + std::size_t sequence, + uint256 const& hash) { + protocol::TMValidatorList vl; - vl.set_manifest(manifest); - vl.set_blob(blob); - vl.set_signature(signature); - vl.set_version(version); + vl.set_manifest(manifest); + vl.set_blob(blob); + vl.set_signature(signature); + vl.set_version(version); - JLOG(p_journal_.debug()) << "Sending validator list for " << - strHex(pubKey) << " with sequence " << - sequence << " to " << - remote_address_.to_string() << " (" << id_ << ")"; - auto m = std::make_shared(vl, protocol::mtVALIDATORLIST); - send(m); - // Don't send it next time. - app_.getHashRouter().addSuppressionPeer(hash, id_); - setPublisherListSequence(pubKey, sequence); - } - ); + JLOG(p_journal_.debug()) + << "Sending validator list for " << strHex(pubKey) + << " with sequence " << sequence << " to " + << remote_address_.to_string() << " (" << id_ << ")"; + auto m = std::make_shared(vl, protocol::mtVALIDATORLIST); + send(m); + // Don't send it next time. + app_.getHashRouter().addSuppressionPeer(hash, id_); + setPublisherListSequence(pubKey, sequence); + }); } protocol::TMManifests tm; - app_.validatorManifests ().for_each_manifest ( - [&tm](std::size_t s){tm.mutable_list()->Reserve(s);}, - [&tm, &hr = app_.getHashRouter()](Manifest const& manifest) - { + app_.validatorManifests().for_each_manifest( + [&tm](std::size_t s) { tm.mutable_list()->Reserve(s); }, + [&tm, &hr = app_.getHashRouter()](Manifest const& manifest) { auto const& s = manifest.serialized; auto& tm_e = *tm.add_list(); tm_e.set_stobject(s.data(), s.size()); @@ -862,52 +878,51 @@ PeerImp::doProtocolStart() if (tm.list_size() > 0) { auto m = std::make_shared(tm, protocol::mtMANIFESTS); - send (m); + send(m); } } // Called repeatedly with protocol message data void -PeerImp::onReadMessage (error_code ec, std::size_t bytes_transferred) +PeerImp::onReadMessage(error_code ec, std::size_t bytes_transferred) { - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; - if(ec == boost::asio::error::eof) + if (ec == boost::asio::error::eof) { JLOG(journal_.info()) << "EOF"; return gracefulClose(); } - if(ec) + if (ec) return fail("onReadMessage", ec); - if(auto stream = journal_.trace()) + if (auto stream = journal_.trace()) { if (bytes_transferred > 0) - stream << - "onReadMessage: " << bytes_transferred << " bytes"; + stream << "onReadMessage: " << bytes_transferred << " bytes"; else stream << "onReadMessage"; } metrics_.recv.add_message(bytes_transferred); - read_buffer_.commit (bytes_transferred); + read_buffer_.commit(bytes_transferred); while (read_buffer_.size() > 0) { std::size_t bytes_consumed; - std::tie(bytes_consumed, ec) = invokeProtocolMessage( - read_buffer_.data(), *this); + std::tie(bytes_consumed, ec) = + invokeProtocolMessage(read_buffer_.data(), *this); if (ec) return fail("onReadMessage", ec); - if (! socket_.is_open()) + if (!socket_.is_open()) return; - if(gracefulClose_) + if (gracefulClose_) return; if (bytes_consumed == 0) break; - read_buffer_.consume (bytes_consumed); + read_buffer_.consume(bytes_consumed); } // Timeout on writes only stream_.async_read_some( @@ -922,33 +937,33 @@ PeerImp::onReadMessage (error_code ec, std::size_t bytes_transferred) } void -PeerImp::onWriteMessage (error_code ec, std::size_t bytes_transferred) +PeerImp::onWriteMessage(error_code ec, std::size_t bytes_transferred) { - if(! socket_.is_open()) + if (!socket_.is_open()) return; - if(ec == boost::asio::error::operation_aborted) + if (ec == boost::asio::error::operation_aborted) return; - if(ec) + if (ec) return fail("onWriteMessage", ec); - if(auto stream = journal_.trace()) + if (auto stream = journal_.trace()) { if (bytes_transferred > 0) - stream << - "onWriteMessage: " << bytes_transferred << " bytes"; + stream << "onWriteMessage: " << bytes_transferred << " bytes"; else stream << "onWriteMessage"; } metrics_.sent.add_message(bytes_transferred); - assert(! send_queue_.empty()); + assert(!send_queue_.empty()); send_queue_.pop(); - if (! send_queue_.empty()) + if (!send_queue_.empty()) { // Timeout on writes only return boost::asio::async_write( stream_, - boost::asio::buffer(send_queue_.front()->getBuffer(compressionEnabled_)), + boost::asio::buffer( + send_queue_.front()->getBuffer(compressionEnabled_)), bind_executor( strand_, std::bind( @@ -976,54 +991,57 @@ PeerImp::onWriteMessage (error_code ec, std::size_t bytes_transferred) //------------------------------------------------------------------------------ void -PeerImp::onMessageUnknown (std::uint16_t type) +PeerImp::onMessageUnknown(std::uint16_t type) { // TODO } void -PeerImp::onMessageBegin (std::uint16_t type, - std::shared_ptr <::google::protobuf::Message> const& m, +PeerImp::onMessageBegin( + std::uint16_t type, + std::shared_ptr<::google::protobuf::Message> const& m, std::size_t size) { - load_event_ = app_.getJobQueue ().makeLoadEvent ( - jtPEER, protocolMessageName(type)); + load_event_ = + app_.getJobQueue().makeLoadEvent(jtPEER, protocolMessageName(type)); fee_ = Resource::feeLightPeer; - overlay_.reportTraffic (TrafficCount::categorize (*m, type, true), - true, static_cast(size)); + overlay_.reportTraffic( + TrafficCount::categorize(*m, type, true), true, static_cast(size)); } void -PeerImp::onMessageEnd (std::uint16_t, - std::shared_ptr <::google::protobuf::Message> const&) +PeerImp::onMessageEnd( + std::uint16_t, + std::shared_ptr<::google::protobuf::Message> const&) { load_event_.reset(); - charge (fee_); + charge(fee_); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { // VFALCO What's the right job type? auto that = shared_from_this(); - app_.getJobQueue().addJob ( - jtVALIDATION_ut, "receiveManifests", - [this, that, m] (Job&) { overlay_.onManifests(m, that); }); + app_.getJobQueue().addJob( + jtVALIDATION_ut, "receiveManifests", [this, that, m](Job&) { + overlay_.onManifests(m, that); + }); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { - if (m->type () == protocol::TMPing::ptPING) + if (m->type() == protocol::TMPing::ptPING) { // We have received a ping request, reply with a pong fee_ = Resource::feeMediumBurdenPeer; - m->set_type (protocol::TMPing::ptPONG); - send (std::make_shared (*m, protocol::mtPING)); + m->set_type(protocol::TMPing::ptPONG); + send(std::make_shared(*m, protocol::mtPING)); return; } - if (m->type () == protocol::TMPing::ptPONG) + if (m->type() == protocol::TMPing::ptPONG) { // Operations on lastPingSeq_, lastPingTime_, no_ping_, and latency_ // must be guarded by recentLock_. @@ -1040,8 +1058,8 @@ PeerImp::onMessage (std::shared_ptr const& m) // Update latency estimate auto const estimate = - std::chrono::duration_cast - (clock_type::now() - lastPingTime_); + std::chrono::duration_cast( + clock_type::now() - lastPingTime_); // Calculate the cumulative moving average of the latency: if (latency_) @@ -1055,10 +1073,10 @@ PeerImp::onMessage (std::shared_ptr const& m) } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { // VFALCO NOTE I think we should drop the peer immediately - if (! cluster()) + if (!cluster()) { fee_ = Resource::feeUnwantedData; return; @@ -1072,22 +1090,18 @@ PeerImp::onMessage (std::shared_ptr const& m) if (node.has_nodename()) name = node.nodename(); - auto const publicKey = parseBase58( - TokenType::NodePublic, node.publickey()); + auto const publicKey = + parseBase58(TokenType::NodePublic, node.publickey()); // NIKB NOTE We should drop the peer immediately if // they send us a public key we can't parse if (publicKey) { auto const reportTime = - NetClock::time_point{ - NetClock::duration{node.reporttime()}}; + NetClock::time_point{NetClock::duration{node.reporttime()}}; app_.cluster().update( - *publicKey, - name, - node.nodeload(), - reportTime); + *publicKey, name, node.nodeload(), reportTime); } } @@ -1095,17 +1109,17 @@ PeerImp::onMessage (std::shared_ptr const& m) if (loadSources != 0) { Resource::Gossip gossip; - gossip.items.reserve (loadSources); + gossip.items.reserve(loadSources); for (int i = 0; i < m->loadsources().size(); ++i) { - protocol::TMLoadSource const& node = m->loadsources (i); + protocol::TMLoadSource const& node = m->loadsources(i); Resource::Gossip::Item item; - item.address = beast::IP::Endpoint::from_string (node.name()); + item.address = beast::IP::Endpoint::from_string(node.name()); item.balance = node.cost(); if (item.address != beast::IP::Endpoint()) gossip.items.push_back(item); } - overlay_.resourceManager().importConsumers (getName(), gossip); + overlay_.resourceManager().importConsumers(getName(), gossip); } // Calculate the cluster fee: @@ -1113,22 +1127,17 @@ PeerImp::onMessage (std::shared_ptr const& m) std::uint32_t clusterFee = 0; std::vector fees; - fees.reserve (app_.cluster().size()); + fees.reserve(app_.cluster().size()); - app_.cluster().for_each( - [&fees,thresh](ClusterNode const& status) - { - if (status.getReportTime() >= thresh) - fees.push_back (status.getLoadFee ()); - }); + app_.cluster().for_each([&fees, thresh](ClusterNode const& status) { + if (status.getReportTime() >= thresh) + fees.push_back(status.getLoadFee()); + }); if (!fees.empty()) { auto const index = fees.size() / 2; - std::nth_element ( - fees.begin(), - fees.begin () + index, - fees.end()); + std::nth_element(fees.begin(), fees.begin() + index, fees.end()); clusterFee = fees[index]; } @@ -1136,19 +1145,19 @@ PeerImp::onMessage (std::shared_ptr const& m) } void -PeerImp::onMessage(std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { // DEPRECATED } void -PeerImp::onMessage(std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { // DEPRECATED } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { auto badData = [&](std::string msg) { fee_ = Resource::feeBadData; @@ -1164,7 +1173,7 @@ PeerImp::onMessage (std::shared_ptr const& m) if (auto shardStore = app_.getShardStore()) { fee_ = Resource::feeLightPeer; - auto shards {shardStore->getCompleteShards()}; + auto shards{shardStore->getCompleteShards()}; if (!shards.empty()) { protocol::TMPeerShardInfo reply; @@ -1176,11 +1185,9 @@ PeerImp::onMessage (std::shared_ptr const& m) if (m->peerchain_size() > 0) *reply.mutable_peerchain() = m->peerchain(); - send(std::make_shared( - reply, protocol::mtPEER_SHARD_INFO)); + send(std::make_shared(reply, protocol::mtPEER_SHARD_INFO)); - JLOG(p_journal_.trace()) << - "Sent shard indexes " << shards; + JLOG(p_journal_.trace()) << "Sent shard indexes " << shards; } } @@ -1196,14 +1203,14 @@ PeerImp::onMessage (std::shared_ptr const& m) m->add_peerchain()->set_nodepubkey( publicKey_.data(), publicKey_.size()); - overlay_.foreach(send_if_not( + overlay_.foreach (send_if_not( std::make_shared(*m, protocol::mtGET_PEER_SHARD_INFO), match_peer(this))); } } void -PeerImp::onMessage(std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { auto badData = [&](std::string msg) { fee_ = Resource::feeBadData; @@ -1221,8 +1228,8 @@ PeerImp::onMessage(std::shared_ptr const& m) if (m->peerchain_size() > 0) { // Get the Public key of the last link in the peer chain - auto const s {makeSlice(m->peerchain( - m->peerchain_size() - 1).nodepubkey())}; + auto const s{ + makeSlice(m->peerchain(m->peerchain_size() - 1).nodepubkey())}; if (!publicKeyType(s)) return badData("Invalid pubKey"); PublicKey peerPubKey(s); @@ -1242,19 +1249,18 @@ PeerImp::onMessage(std::shared_ptr const& m) } m->mutable_peerchain()->RemoveLast(); - peer->send(std::make_shared( - *m, protocol::mtPEER_SHARD_INFO)); + peer->send( + std::make_shared(*m, protocol::mtPEER_SHARD_INFO)); - JLOG(p_journal_.trace()) << - "Relayed TMPeerShardInfo to peer with IP " << - remote_address_.address().to_string(); + JLOG(p_journal_.trace()) + << "Relayed TMPeerShardInfo to peer with IP " + << remote_address_.address().to_string(); } else { // Peer is no longer available so the relay ends fee_ = Resource::feeUnwantedData; - JLOG(p_journal_.info()) << - "Unable to route shard info"; + JLOG(p_journal_.info()) << "Unable to route shard info"; } return; } @@ -1268,7 +1274,7 @@ PeerImp::onMessage(std::shared_ptr const& m) std::uint32_t earliestShard; boost::optional latestShard; { - auto const curLedgerSeq { + auto const curLedgerSeq{ app_.getLedgerMaster().getCurrentLedgerIndex()}; if (auto shardStore = app_.getShardStore()) { @@ -1278,7 +1284,7 @@ PeerImp::onMessage(std::shared_ptr const& m) } else { - auto const earliestLedgerSeq { + auto const earliestLedgerSeq{ app_.getNodeStore().earliestLedgerSeq()}; earliestShard = NodeStore::seqToShardIndex(earliestLedgerSeq); if (curLedgerSeq >= earliestLedgerSeq) @@ -1306,7 +1312,7 @@ PeerImp::onMessage(std::shared_ptr const& m) endpoint = std::move(*result); } } - else if (crawl()) // Check if peer will share IP publicly + else if (crawl()) // Check if peer will share IP publicly { endpoint = remote_address_; } @@ -1319,8 +1325,8 @@ PeerImp::onMessage(std::shared_ptr const& m) publicKey = publicKey_; { - std::lock_guard l {shardInfoMutex_}; - auto it {shardInfo_.find(publicKey)}; + std::lock_guard l{shardInfoMutex_}; + auto it{shardInfo_.find(publicKey)}; if (it != shardInfo_.end()) { // Update the IP address for the node @@ -1339,17 +1345,17 @@ PeerImp::onMessage(std::shared_ptr const& m) } } - JLOG(p_journal_.trace()) << - "Consumed TMPeerShardInfo originating from public key " << - toBase58(TokenType::NodePublic, publicKey) << - " shard indexes " << m->shardindexes(); + JLOG(p_journal_.trace()) + << "Consumed TMPeerShardInfo originating from public key " + << toBase58(TokenType::NodePublic, publicKey) << " shard indexes " + << m->shardindexes(); if (m->has_lastlink()) overlay_.lastLink(id_); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { if (sanity_.load() != Sanity::sane) { @@ -1357,20 +1363,21 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - std::vector endpoints; + std::vector endpoints; if (m->endpoints_v2().size()) { - endpoints.reserve (m->endpoints_v2().size()); - for (auto const& tm : m->endpoints_v2 ()) + endpoints.reserve(m->endpoints_v2().size()); + for (auto const& tm : m->endpoints_v2()) { // these endpoint strings support ipv4 and ipv6 - auto result = beast::IP::Endpoint::from_string_checked(tm.endpoint()); - if (! result) + auto result = + beast::IP::Endpoint::from_string_checked(tm.endpoint()); + if (!result) { - JLOG(p_journal_.error()) << - "failed to parse incoming endpoint: {" << - tm.endpoint() << "}"; + JLOG(p_journal_.error()) + << "failed to parse incoming endpoint: {" << tm.endpoint() + << "}"; continue; } @@ -1382,24 +1389,23 @@ PeerImp::onMessage (std::shared_ptr const& m) // take the address/port we were given endpoints.emplace_back( - tm.hops() > 0 ? - *result : - remote_address_.at_port(result->port()), + tm.hops() > 0 ? *result + : remote_address_.at_port(result->port()), tm.hops()); - JLOG(p_journal_.trace()) << - "got v2 EP: " << endpoints.back().address << - ", hops = " << endpoints.back().hops; + JLOG(p_journal_.trace()) + << "got v2 EP: " << endpoints.back().address + << ", hops = " << endpoints.back().hops; } } else { // this branch can be removed once the entire network is operating with // endpoint_v2() items (strings) - endpoints.reserve (m->endpoints().size()); - for (int i = 0; i < m->endpoints ().size (); ++i) + endpoints.reserve(m->endpoints().size()); + for (int i = 0; i < m->endpoints().size(); ++i) { PeerFinder::Endpoint endpoint; - protocol::TMEndpoint const& tm (m->endpoints(i)); + protocol::TMEndpoint const& tm(m->endpoints(i)); // hops endpoint.hops = tm.hops(); @@ -1409,8 +1415,9 @@ PeerImp::onMessage (std::shared_ptr const& m) { in_addr addr; addr.s_addr = tm.ipv4().ipv4(); - beast::IP::AddressV4 v4 (ntohl (addr.s_addr)); - endpoint.address = beast::IP::Endpoint (v4, tm.ipv4().ipv4port ()); + beast::IP::AddressV4 v4(ntohl(addr.s_addr)); + endpoint.address = + beast::IP::Endpoint(v4, tm.ipv4().ipv4port()); } else { @@ -1420,55 +1427,52 @@ PeerImp::onMessage (std::shared_ptr const& m) // then we'll verify that their listener can receive incoming // by performing a connectivity test. // - endpoint.address = remote_address_.at_port ( - tm.ipv4().ipv4port ()); + endpoint.address = + remote_address_.at_port(tm.ipv4().ipv4port()); } - endpoints.push_back (endpoint); - JLOG(p_journal_.trace()) << - "got v1 EP: " << endpoints.back().address << - ", hops = " << endpoints.back().hops; + endpoints.push_back(endpoint); + JLOG(p_journal_.trace()) + << "got v1 EP: " << endpoints.back().address + << ", hops = " << endpoints.back().hops; } } - if (! endpoints.empty()) - overlay_.peerFinder().on_endpoints (slot_, endpoints); + if (!endpoints.empty()) + overlay_.peerFinder().on_endpoints(slot_, endpoints); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { - if (sanity_.load() == Sanity::insane) return; - if (app_.getOPs().isNeedNetworkLedger ()) + if (app_.getOPs().isNeedNetworkLedger()) { // If we've never been in synch, there's nothing we can do // with a transaction - JLOG(p_journal_.debug()) << "Ignoring incoming transaction: " << - "Need network ledger"; + JLOG(p_journal_.debug()) << "Ignoring incoming transaction: " + << "Need network ledger"; return; } - SerialIter sit (makeSlice(m->rawtransaction())); + SerialIter sit(makeSlice(m->rawtransaction())); try { auto stx = std::make_shared(sit); - uint256 txID = stx->getTransactionID (); + uint256 txID = stx->getTransactionID(); int flags; constexpr std::chrono::seconds tx_interval = 10s; - if (! app_.getHashRouter ().shouldProcess (txID, id_, flags, - tx_interval)) + if (!app_.getHashRouter().shouldProcess(txID, id_, flags, tx_interval)) { // we have seen this transaction recently if (flags & SF_BAD) { fee_ = Resource::feeInvalidSignature; - JLOG(p_journal_.debug()) << "Ignoring known bad tx " << - txID; + JLOG(p_journal_.debug()) << "Ignoring known bad tx " << txID; } return; @@ -1479,7 +1483,7 @@ PeerImp::onMessage (std::shared_ptr const& m) bool checkSignature = true; if (cluster()) { - if (! m->has_deferred () || ! m->deferred ()) + if (!m->has_deferred() || !m->deferred()) { // Skip local checks if a server we trust // put the transaction in its open ledger @@ -1503,59 +1507,61 @@ PeerImp::onMessage (std::shared_ptr const& m) } else if (app_.getLedgerMaster().getValidatedLedgerAge() > 4min) { - JLOG(p_journal_.trace()) << "No new transactions until synchronized"; + JLOG(p_journal_.trace()) + << "No new transactions until synchronized"; } else { - app_.getJobQueue ().addJob ( - jtTRANSACTION, "recvTransaction->checkTransaction", + app_.getJobQueue().addJob( + jtTRANSACTION, + "recvTransaction->checkTransaction", [weak = std::weak_ptr(shared_from_this()), - flags, checkSignature, stx] (Job&) { + flags, + checkSignature, + stx](Job&) { if (auto peer = weak.lock()) - peer->checkTransaction(flags, - checkSignature, stx); + peer->checkTransaction(flags, checkSignature, stx); }); } } catch (std::exception const&) { - JLOG(p_journal_.warn()) << "Transaction invalid: " << - strHex(m->rawtransaction ()); + JLOG(p_journal_.warn()) + << "Transaction invalid: " << strHex(m->rawtransaction()); } } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { fee_ = Resource::feeMediumBurdenPeer; std::weak_ptr weak = shared_from_this(); - app_.getJobQueue().addJob ( - jtLEDGER_REQ, "recvGetLedger", - [weak, m] (Job&) { - if (auto peer = weak.lock()) - peer->getLedger(m); - }); + app_.getJobQueue().addJob(jtLEDGER_REQ, "recvGetLedger", [weak, m](Job&) { + if (auto peer = weak.lock()) + peer->getLedger(m); + }); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { protocol::TMLedgerData& packet = *m; - if (m->nodes ().size () <= 0) + if (m->nodes().size() <= 0) { JLOG(p_journal_.warn()) << "Ledger/TXset data with no nodes"; return; } - if (m->has_requestcookie ()) + if (m->has_requestcookie()) { - std::shared_ptr target = overlay_.findPeerByShortID (m->requestcookie ()); + std::shared_ptr target = + overlay_.findPeerByShortID(m->requestcookie()); if (target) { - m->clear_requestcookie (); - target->send (std::make_shared ( - packet, protocol::mtLEDGER_DATA)); + m->clear_requestcookie(); + target->send( + std::make_shared(packet, protocol::mtLEDGER_DATA)); } else { @@ -1565,31 +1571,29 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - if (! stringIsUint256Sized (m->ledgerhash())) + if (!stringIsUint256Sized(m->ledgerhash())) { JLOG(p_journal_.warn()) << "TX candidate reply with invalid hash size"; fee_ = Resource::feeInvalidRequest; return; } - uint256 const hash {m->ledgerhash()}; + uint256 const hash{m->ledgerhash()}; - if (m->type () == protocol::liTS_CANDIDATE) + if (m->type() == protocol::liTS_CANDIDATE) { // got data for a candidate transaction set std::weak_ptr weak = shared_from_this(); auto& journal = p_journal_; app_.getJobQueue().addJob( - jtTXN_DATA, "recvPeerData", - [weak, hash, journal, m] (Job&) { + jtTXN_DATA, "recvPeerData", [weak, hash, journal, m](Job&) { if (auto peer = weak.lock()) peer->peerTXData(hash, m, journal); }); return; } - if (!app_.getInboundLedgers ().gotLedgerData ( - hash, shared_from_this(), m)) + if (!app_.getInboundLedgers().gotLedgerData(hash, shared_from_this(), m)) { JLOG(p_journal_.trace()) << "Got data for unwanted ledger"; fee_ = Resource::feeUnwantedData; @@ -1597,11 +1601,11 @@ PeerImp::onMessage (std::shared_ptr const& m) } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { protocol::TMProposeSet& set = *m; - if (set.has_hops() && ! cluster()) + if (set.has_hops() && !cluster()) set.set_hops(set.hops() + 1); auto const sig = makeSlice(set.signature()); @@ -1616,8 +1620,8 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - if (! stringIsUint256Sized (set.currenttxhash()) || - ! stringIsUint256Sized (set.previousledger())) + if (!stringIsUint256Sized(set.currenttxhash()) || + !stringIsUint256Sized(set.previousledger())) { JLOG(p_journal_.warn()) << "Proposal: malformed"; fee_ = Resource::feeInvalidRequest; @@ -1627,20 +1631,24 @@ PeerImp::onMessage (std::shared_ptr const& m) uint256 const proposeHash{set.currenttxhash()}; uint256 const prevLedger{set.previousledger()}; - PublicKey const publicKey {makeSlice(set.nodepubkey())}; - NetClock::time_point const closeTime { NetClock::duration{set.closetime()} }; + PublicKey const publicKey{makeSlice(set.nodepubkey())}; + NetClock::time_point const closeTime{NetClock::duration{set.closetime()}}; - uint256 const suppression = proposalUniqueId ( - proposeHash, prevLedger, set.proposeseq(), - closeTime, publicKey.slice(), sig); + uint256 const suppression = proposalUniqueId( + proposeHash, + prevLedger, + set.proposeseq(), + closeTime, + publicKey.slice(), + sig); - if (! app_.getHashRouter ().addSuppressionPeer (suppression, id_)) + if (!app_.getHashRouter().addSuppressionPeer(suppression, id_)) { JLOG(p_journal_.trace()) << "Proposal: duplicate"; return; } - auto const isTrusted = app_.validators().trusted (publicKey); + auto const isTrusted = app_.validators().trusted(publicKey); if (!isTrusted) { @@ -1650,15 +1658,15 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - if (! cluster() && app_.getFeeTrack ().isLoadedLocal()) + if (!cluster() && app_.getFeeTrack().isLoadedLocal()) { JLOG(p_journal_.debug()) << "Proposal: Dropping UNTRUSTED (load)"; return; } } - JLOG(p_journal_.trace()) << - "Proposal: " << (isTrusted ? "trusted" : "UNTRUSTED"); + JLOG(p_journal_.trace()) + << "Proposal: " << (isTrusted ? "trusted" : "UNTRUSTED"); auto proposal = RCLCxPeerPos( publicKey, @@ -1673,48 +1681,49 @@ PeerImp::onMessage (std::shared_ptr const& m) calcNodeID(app_.validatorManifests().getMasterKey(publicKey))}); std::weak_ptr weak = shared_from_this(); - app_.getJobQueue ().addJob ( - isTrusted ? jtPROPOSAL_t : jtPROPOSAL_ut, "recvPropose->checkPropose", - [weak, m, proposal] (Job& job) { + app_.getJobQueue().addJob( + isTrusted ? jtPROPOSAL_t : jtPROPOSAL_ut, + "recvPropose->checkPropose", + [weak, m, proposal](Job& job) { if (auto peer = weak.lock()) peer->checkPropose(job, m, proposal); }); } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { JLOG(p_journal_.trace()) << "Status: Change"; - if (!m->has_networktime ()) - m->set_networktime (app_.timeKeeper().now().time_since_epoch().count()); + if (!m->has_networktime()) + m->set_networktime(app_.timeKeeper().now().time_since_epoch().count()); { std::lock_guard sl(recentLock_); - if (!last_status_.has_newstatus () || m->has_newstatus ()) + if (!last_status_.has_newstatus() || m->has_newstatus()) last_status_ = *m; else { // preserve old status - protocol::NodeStatus status = last_status_.newstatus (); + protocol::NodeStatus status = last_status_.newstatus(); last_status_ = *m; - m->set_newstatus (status); + m->set_newstatus(status); } } - if (m->newevent () == protocol::neLOST_SYNC) + if (m->newevent() == protocol::neLOST_SYNC) { - bool outOfSync {false}; + bool outOfSync{false}; { // Operations on closedLedgerHash_ and previousLedgerHash_ must be // guarded by recentLock_. std::lock_guard sl(recentLock_); - if (!closedLedgerHash_.isZero ()) + if (!closedLedgerHash_.isZero()) { outOfSync = true; - closedLedgerHash_.zero (); + closedLedgerHash_.zero(); } - previousLedgerHash_.zero (); + previousLedgerHash_.zero(); } if (outOfSync) { @@ -1724,9 +1733,9 @@ PeerImp::onMessage (std::shared_ptr const& m) } { - uint256 closedLedgerHash {}; - bool const peerChangedLedgers { - m->has_ledgerhash() && stringIsUint256Sized (m->ledgerhash())}; + uint256 closedLedgerHash{}; + bool const peerChangedLedgers{ + m->has_ledgerhash() && stringIsUint256Sized(m->ledgerhash())}; { // Operations on closedLedgerHash_ and previousLedgerHash_ must be @@ -1736,7 +1745,7 @@ PeerImp::onMessage (std::shared_ptr const& m) { closedLedgerHash_ = m->ledgerhash(); closedLedgerHash = closedLedgerHash_; - addLedger (closedLedgerHash, sl); + addLedger(closedLedgerHash, sl); } else { @@ -1744,14 +1753,14 @@ PeerImp::onMessage (std::shared_ptr const& m) } if (m->has_ledgerhashprevious() && - stringIsUint256Sized (m->ledgerhashprevious())) + stringIsUint256Sized(m->ledgerhashprevious())) { previousLedgerHash_ = m->ledgerhashprevious(); - addLedger (previousLedgerHash_, sl); + addLedger(previousLedgerHash_, sl); } else { - previousLedgerHash_.zero (); + previousLedgerHash_.zero(); } } if (peerChangedLedgers) @@ -1764,13 +1773,12 @@ PeerImp::onMessage (std::shared_ptr const& m) } } - - if (m->has_firstseq () && m->has_lastseq()) + if (m->has_firstseq() && m->has_lastseq()) { - std::lock_guard sl (recentLock_); + std::lock_guard sl(recentLock_); - minLedger_ = m->firstseq (); - maxLedger_ = m->lastseq (); + minLedger_ = m->firstseq(); + maxLedger_ = m->lastseq(); if ((maxLedger_ < minLedger_) || (minLedger_ == 0) || (maxLedger_ == 0)) minLedger_ = maxLedger_ = 0; @@ -1779,95 +1787,92 @@ PeerImp::onMessage (std::shared_ptr const& m) if (m->has_ledgerseq() && app_.getLedgerMaster().getValidatedLedgerAge() < 2min) { - checkSanity (m->ledgerseq(), app_.getLedgerMaster().getValidLedgerIndex()); + checkSanity( + m->ledgerseq(), app_.getLedgerMaster().getValidLedgerIndex()); } - app_.getOPs().pubPeerStatus ( - [=]() -> Json::Value + app_.getOPs().pubPeerStatus([=]() -> Json::Value { + Json::Value j = Json::objectValue; + + if (m->has_newstatus()) { - Json::Value j = Json::objectValue; - - if (m->has_newstatus ()) + switch (m->newstatus()) { - switch (m->newstatus ()) - { - case protocol::nsCONNECTING: - j[jss::status] = "CONNECTING"; - break; - case protocol::nsCONNECTED: - j[jss::status] = "CONNECTED"; - break; - case protocol::nsMONITORING: - j[jss::status] = "MONITORING"; - break; - case protocol::nsVALIDATING: - j[jss::status] = "VALIDATING"; - break; - case protocol::nsSHUTTING: - j[jss::status] = "SHUTTING"; - break; - } + case protocol::nsCONNECTING: + j[jss::status] = "CONNECTING"; + break; + case protocol::nsCONNECTED: + j[jss::status] = "CONNECTED"; + break; + case protocol::nsMONITORING: + j[jss::status] = "MONITORING"; + break; + case protocol::nsVALIDATING: + j[jss::status] = "VALIDATING"; + break; + case protocol::nsSHUTTING: + j[jss::status] = "SHUTTING"; + break; } + } - if (m->has_newevent()) + if (m->has_newevent()) + { + switch (m->newevent()) { - switch (m->newevent ()) - { - case protocol::neCLOSING_LEDGER: - j[jss::action] = "CLOSING_LEDGER"; - break; - case protocol::neACCEPTED_LEDGER: - j[jss::action] = "ACCEPTED_LEDGER"; - break; - case protocol::neSWITCHED_LEDGER: - j[jss::action] = "SWITCHED_LEDGER"; - break; - case protocol::neLOST_SYNC: - j[jss::action] = "LOST_SYNC"; - break; - } + case protocol::neCLOSING_LEDGER: + j[jss::action] = "CLOSING_LEDGER"; + break; + case protocol::neACCEPTED_LEDGER: + j[jss::action] = "ACCEPTED_LEDGER"; + break; + case protocol::neSWITCHED_LEDGER: + j[jss::action] = "SWITCHED_LEDGER"; + break; + case protocol::neLOST_SYNC: + j[jss::action] = "LOST_SYNC"; + break; } + } - if (m->has_ledgerseq ()) + if (m->has_ledgerseq()) + { + j[jss::ledger_index] = m->ledgerseq(); + } + + if (m->has_ledgerhash()) + { + uint256 closedLedgerHash{}; { - j[jss::ledger_index] = m->ledgerseq(); + std::lock_guard sl(recentLock_); + closedLedgerHash = closedLedgerHash_; } + j[jss::ledger_hash] = to_string(closedLedgerHash); + } - if (m->has_ledgerhash ()) - { - uint256 closedLedgerHash {}; - { - std::lock_guard sl(recentLock_); - closedLedgerHash = closedLedgerHash_; - } - j[jss::ledger_hash] = to_string (closedLedgerHash); - } + if (m->has_networktime()) + { + j[jss::date] = Json::UInt(m->networktime()); + } - if (m->has_networktime ()) - { - j[jss::date] = Json::UInt (m->networktime()); - } + if (m->has_firstseq() && m->has_lastseq()) + { + j[jss::ledger_index_min] = Json::UInt(m->firstseq()); + j[jss::ledger_index_max] = Json::UInt(m->lastseq()); + } - if (m->has_firstseq () && m->has_lastseq ()) - { - j[jss::ledger_index_min] = - Json::UInt (m->firstseq ()); - j[jss::ledger_index_max] = - Json::UInt (m->lastseq ()); - } - - return j; - }); + return j; + }); } void -PeerImp::checkSanity (std::uint32_t validationSeq) +PeerImp::checkSanity(std::uint32_t validationSeq) { std::uint32_t serverSeq; { // Extract the seqeuence number of the highest // ledger this peer has - std::lock_guard sl (recentLock_); + std::lock_guard sl(recentLock_); serverSeq = maxLedger_; } @@ -1875,34 +1880,36 @@ PeerImp::checkSanity (std::uint32_t validationSeq) { // Compare the peer's ledger sequence to the // sequence of a recently-validated ledger - checkSanity (serverSeq, validationSeq); + checkSanity(serverSeq, validationSeq); } } void -PeerImp::checkSanity (std::uint32_t seq1, std::uint32_t seq2) +PeerImp::checkSanity(std::uint32_t seq1, std::uint32_t seq2) { - int diff = std::max (seq1, seq2) - std::min (seq1, seq2); + int diff = std::max(seq1, seq2) - std::min(seq1, seq2); - if (diff < Tuning::saneLedgerLimit) - { - // The peer's ledger sequence is close to the validation's - sanity_ = Sanity::sane; - } + if (diff < Tuning::saneLedgerLimit) + { + // The peer's ledger sequence is close to the validation's + sanity_ = Sanity::sane; + } - if ((diff > Tuning::insaneLedgerLimit) && (sanity_.load() != Sanity::insane)) - { - // The peer's ledger sequence is way off the validation's - std::lock_guard sl(recentLock_); + if ((diff > Tuning::insaneLedgerLimit) && + (sanity_.load() != Sanity::insane)) + { + // The peer's ledger sequence is way off the validation's + std::lock_guard sl(recentLock_); - sanity_ = Sanity::insane; - insaneTime_ = clock_type::now(); - } + sanity_ = Sanity::insane; + insaneTime_ = clock_type::now(); + } } // Should this connection be rejected // and considered a failure -void PeerImp::check () +void +PeerImp::check() { if (m_inbound || (sanity_.load() == Sanity::sane)) return; @@ -1917,16 +1924,16 @@ void PeerImp::check () bool reject = false; if (sanity_.load() == Sanity::insane) - reject = (insaneTime - clock_type::now()) - > std::chrono::seconds (Tuning::maxInsaneTime); + reject = (insaneTime - clock_type::now()) > + std::chrono::seconds(Tuning::maxInsaneTime); if (sanity_.load() == Sanity::unknown) - reject = (insaneTime - clock_type::now()) - > std::chrono::seconds (Tuning::maxUnknownTime); + reject = (insaneTime - clock_type::now()) > + std::chrono::seconds(Tuning::maxUnknownTime); if (reject) { - overlay_.peerFinder().on_failure (slot_); + overlay_.peerFinder().on_failure(slot_); post( strand_, std::bind( @@ -1937,36 +1944,36 @@ void PeerImp::check () } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { - if (! stringIsUint256Sized (m->hash())) + if (!stringIsUint256Sized(m->hash())) { fee_ = Resource::feeInvalidRequest; return; } - uint256 const hash {m->hash()}; + uint256 const hash{m->hash()}; - if (m->status () == protocol::tsHAVE) + if (m->status() == protocol::tsHAVE) { std::lock_guard sl(recentLock_); - if (std::find (recentTxSets_.begin (), - recentTxSets_.end (), hash) != recentTxSets_.end ()) + if (std::find(recentTxSets_.begin(), recentTxSets_.end(), hash) != + recentTxSets_.end()) { fee_ = Resource::feeUnwantedData; return; } - if (recentTxSets_.size () == 128) - recentTxSets_.pop_front (); + if (recentTxSets_.size() == 128) + recentTxSets_.pop_front(); - recentTxSets_.push_back (hash); + recentTxSets_.push_back(hash); } } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { try { @@ -1985,13 +1992,14 @@ PeerImp::onMessage (std::shared_ptr const& m) auto const version = m->version(); auto const hash = sha512Half(manifest, blob, signature, version); - JLOG(p_journal_.debug()) << "Received validator list from " << - remote_address_.to_string() << " (" << id_ << ")"; + JLOG(p_journal_.debug()) + << "Received validator list from " << remote_address_.to_string() + << " (" << id_ << ")"; - if (! app_.getHashRouter ().addSuppressionPeer(hash, id_)) + if (!app_.getHashRouter().addSuppressionPeer(hash, id_)) { - JLOG(p_journal_.debug()) << - "ValidatorList: received duplicate validator list"; + JLOG(p_journal_.debug()) + << "ValidatorList: received duplicate validator list"; // Charging this fee here won't hurt the peer in the normal // course of operation (ie. refresh every 5 minutes), but // will add up if the peer is misbehaving. @@ -1999,7 +2007,7 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - auto const applyResult = app_.validators().applyListAndBroadcast ( + auto const applyResult = app_.validators().applyListAndBroadcast( manifest, blob, signature, @@ -2010,101 +2018,103 @@ PeerImp::onMessage (std::shared_ptr const& m) app_.getHashRouter()); auto const disp = applyResult.disposition; - JLOG(p_journal_.debug()) << "Processed validator list from " << - (applyResult.publisherKey ? strHex(*applyResult.publisherKey) : - "unknown or invalid publisher") << " from " << - remote_address_.to_string() << " (" << id_ << ") with result " << - to_string(disp); + JLOG(p_journal_.debug()) + << "Processed validator list from " + << (applyResult.publisherKey ? strHex(*applyResult.publisherKey) + : "unknown or invalid publisher") + << " from " << remote_address_.to_string() << " (" << id_ + << ") with result " << to_string(disp); switch (disp) { - case ListDisposition::accepted: - JLOG (p_journal_.debug()) << - "Applied new validator list from peer " << remote_address_; - { - std::lock_guard sl(recentLock_); - - assert(applyResult.sequence && applyResult.publisherKey); - auto const& pubKey = *applyResult.publisherKey; -#ifndef NDEBUG - if (auto const iter = publisherListSequences_.find(pubKey); - iter != publisherListSequences_.end()) + case ListDisposition::accepted: + JLOG(p_journal_.debug()) + << "Applied new validator list from peer " + << remote_address_; { - assert(iter->second < *applyResult.sequence); - } -#endif - publisherListSequences_[pubKey] = *applyResult.sequence; - } - break; - case ListDisposition::same_sequence: - JLOG (p_journal_.warn()) << - "Validator list with current sequence from peer " << - remote_address_; - // Charging this fee here won't hurt the peer in the normal - // course of operation (ie. refresh every 5 minutes), but - // will add up if the peer is misbehaving. - fee_ = Resource::feeUnwantedData; -#ifndef NDEBUG - { - std::lock_guard sl(recentLock_); - assert(applyResult.sequence && applyResult.publisherKey); - assert(publisherListSequences_[*applyResult.publisherKey] - == *applyResult.sequence); - } -#endif // !NDEBUG + std::lock_guard sl(recentLock_); - break; - case ListDisposition::stale: - JLOG (p_journal_.warn()) << - "Stale validator list from peer " << remote_address_; - // There are very few good reasons for a peer to send an - // old list, particularly more than once. - fee_ = Resource::feeBadData; - break; - case ListDisposition::untrusted: - JLOG (p_journal_.warn()) << - "Untrusted validator list from peer " << remote_address_; - // Charging this fee here won't hurt the peer in the normal - // course of operation (ie. refresh every 5 minutes), but - // will add up if the peer is misbehaving. - fee_ = Resource::feeUnwantedData; - break; - case ListDisposition::invalid: - JLOG (p_journal_.warn()) << - "Invalid validator list from peer " << remote_address_; - // This shouldn't ever happen with a well-behaved peer - fee_ = Resource::feeInvalidSignature; - break; - case ListDisposition::unsupported_version: - JLOG (p_journal_.warn()) << - "Unsupported version validator list from peer " << - remote_address_; - // During a version transition, this may be legitimate. - // If it happens frequently, that's probably bad. - fee_ = Resource::feeBadData; - break; - default: - assert(false); + assert(applyResult.sequence && applyResult.publisherKey); + auto const& pubKey = *applyResult.publisherKey; +#ifndef NDEBUG + if (auto const iter = publisherListSequences_.find(pubKey); + iter != publisherListSequences_.end()) + { + assert(iter->second < *applyResult.sequence); + } +#endif + publisherListSequences_[pubKey] = *applyResult.sequence; + } + break; + case ListDisposition::same_sequence: + JLOG(p_journal_.warn()) + << "Validator list with current sequence from peer " + << remote_address_; + // Charging this fee here won't hurt the peer in the normal + // course of operation (ie. refresh every 5 minutes), but + // will add up if the peer is misbehaving. + fee_ = Resource::feeUnwantedData; +#ifndef NDEBUG + { + std::lock_guard sl(recentLock_); + assert(applyResult.sequence && applyResult.publisherKey); + assert( + publisherListSequences_[*applyResult.publisherKey] == + *applyResult.sequence); + } +#endif // !NDEBUG + + break; + case ListDisposition::stale: + JLOG(p_journal_.warn()) + << "Stale validator list from peer " << remote_address_; + // There are very few good reasons for a peer to send an + // old list, particularly more than once. + fee_ = Resource::feeBadData; + break; + case ListDisposition::untrusted: + JLOG(p_journal_.warn()) + << "Untrusted validator list from peer " << remote_address_; + // Charging this fee here won't hurt the peer in the normal + // course of operation (ie. refresh every 5 minutes), but + // will add up if the peer is misbehaving. + fee_ = Resource::feeUnwantedData; + break; + case ListDisposition::invalid: + JLOG(p_journal_.warn()) + << "Invalid validator list from peer " << remote_address_; + // This shouldn't ever happen with a well-behaved peer + fee_ = Resource::feeInvalidSignature; + break; + case ListDisposition::unsupported_version: + JLOG(p_journal_.warn()) + << "Unsupported version validator list from peer " + << remote_address_; + // During a version transition, this may be legitimate. + // If it happens frequently, that's probably bad. + fee_ = Resource::feeBadData; + break; + default: + assert(false); } } catch (std::exception const& e) { - JLOG(p_journal_.warn()) << - "ValidatorList: Exception, " << e.what() << - " from peer " << remote_address_; + JLOG(p_journal_.warn()) << "ValidatorList: Exception, " << e.what() + << " from peer " << remote_address_; fee_ = Resource::feeBadData; } } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { auto const closeTime = app_.timeKeeper().closeTime(); - if (m->has_hops() && ! cluster()) + if (m->has_hops() && !cluster()) m->set_hops(m->hops() + 1); - if (m->validation ().size () < 50) + if (m->validation().size() < 50) { JLOG(p_journal_.warn()) << "Validation: Too small"; fee_ = Resource::feeInvalidRequest; @@ -2115,7 +2125,7 @@ PeerImp::onMessage (std::shared_ptr const& m) { STValidation::pointer val; { - SerialIter sit (makeSlice(m->validation())); + SerialIter sit(makeSlice(m->validation())); val = std::make_shared( std::ref(sit), [this](PublicKey const& pk) { @@ -2123,67 +2133,64 @@ PeerImp::onMessage (std::shared_ptr const& m) app_.validatorManifests().getMasterKey(pk)); }, false); - val->setSeen (closeTime); + val->setSeen(closeTime); } - if (! isCurrent(app_.getValidations().parms(), - app_.timeKeeper().closeTime(), - val->getSignTime(), - val->getSeenTime())) + if (!isCurrent( + app_.getValidations().parms(), + app_.timeKeeper().closeTime(), + val->getSignTime(), + val->getSeenTime())) { JLOG(p_journal_.trace()) << "Validation: Not current"; fee_ = Resource::feeUnwantedData; return; } - if (! app_.getHashRouter ().addSuppressionPeer( - sha512Half(makeSlice(m->validation())), id_)) + if (!app_.getHashRouter().addSuppressionPeer( + sha512Half(makeSlice(m->validation())), id_)) { JLOG(p_journal_.trace()) << "Validation: duplicate"; return; } auto const isTrusted = - app_.validators().trusted(val->getSignerPublic ()); + app_.validators().trusted(val->getSignerPublic()); - if (!isTrusted && (sanity_.load () == Sanity::insane)) + if (!isTrusted && (sanity_.load() == Sanity::insane)) { - JLOG(p_journal_.debug()) << - "Validation: dropping untrusted from insane peer"; + JLOG(p_journal_.debug()) + << "Validation: dropping untrusted from insane peer"; } - if (isTrusted || cluster() || - ! app_.getFeeTrack ().isLoadedLocal ()) + if (isTrusted || cluster() || !app_.getFeeTrack().isLoadedLocal()) { std::weak_ptr weak = shared_from_this(); - app_.getJobQueue ().addJob ( + app_.getJobQueue().addJob( isTrusted ? jtVALIDATION_t : jtVALIDATION_ut, "recvValidation->checkValidation", - [weak, val, m] (Job&) - { + [weak, val, m](Job&) { if (auto peer = weak.lock()) peer->checkValidation(val, m); }); } else { - JLOG(p_journal_.debug()) << - "Validation: Dropping UNTRUSTED (load)"; + JLOG(p_journal_.debug()) << "Validation: Dropping UNTRUSTED (load)"; } } catch (std::exception const& e) { - JLOG(p_journal_.warn()) << - "Validation: Exception, " << e.what(); + JLOG(p_journal_.warn()) << "Validation: Exception, " << e.what(); fee_ = Resource::feeInvalidRequest; } } void -PeerImp::onMessage (std::shared_ptr const& m) +PeerImp::onMessage(std::shared_ptr const& m) { protocol::TMGetObjectByHash& packet = *m; - if (packet.query ()) + if (packet.query()) { // this is a query if (send_queue_.size() >= Tuning::dropSendQueue) @@ -2192,9 +2199,9 @@ PeerImp::onMessage (std::shared_ptr const& m) return; } - if (packet.type () == protocol::TMGetObjectByHash::otFETCH_PACK) + if (packet.type() == protocol::TMGetObjectByHash::otFETCH_PACK) { - doFetchPack (m); + doFetchPack(m); return; } @@ -2202,27 +2209,27 @@ PeerImp::onMessage (std::shared_ptr const& m) protocol::TMGetObjectByHash reply; - reply.set_query (false); + reply.set_query(false); if (packet.has_seq()) reply.set_seq(packet.seq()); - reply.set_type (packet.type ()); + reply.set_type(packet.type()); - if (packet.has_ledgerhash ()) - reply.set_ledgerhash (packet.ledgerhash ()); + if (packet.has_ledgerhash()) + reply.set_ledgerhash(packet.ledgerhash()); // This is a very minimal implementation - for (int i = 0; i < packet.objects_size (); ++i) + for (int i = 0; i < packet.objects_size(); ++i) { - auto const& obj = packet.objects (i); - if (obj.has_hash() && stringIsUint256Sized (obj.hash())) + auto const& obj = packet.objects(i); + if (obj.has_hash() && stringIsUint256Sized(obj.hash())) { - uint256 const hash {obj.hash()}; + uint256 const hash{obj.hash()}; // VFALCO TODO Move this someplace more sensible so we dont // need to inject the NodeStore interfaces. - std::uint32_t seq {obj.has_ledgerseq() ? obj.ledgerseq() : 0}; - auto hObj {app_.getNodeStore().fetch (hash, seq)}; + std::uint32_t seq{obj.has_ledgerseq() ? obj.ledgerseq() : 0}; + auto hObj{app_.getNodeStore().fetch(hash, seq)}; if (!hObj) { if (auto shardStore = app_.getShardStore()) @@ -2233,13 +2240,13 @@ PeerImp::onMessage (std::shared_ptr const& m) } if (hObj) { - protocol::TMIndexedObject& newObj = *reply.add_objects (); - newObj.set_hash (hash.begin (), hash.size ()); - newObj.set_data (&hObj->getData ().front (), - hObj->getData ().size ()); + protocol::TMIndexedObject& newObj = *reply.add_objects(); + newObj.set_hash(hash.begin(), hash.size()); + newObj.set_data( + &hObj->getData().front(), hObj->getData().size()); - if (obj.has_nodeid ()) - newObj.set_index (obj.nodeid ()); + if (obj.has_nodeid()) + newObj.set_index(obj.nodeid()); if (obj.has_ledgerseq()) newObj.set_ledgerseq(obj.ledgerseq()); @@ -2248,10 +2255,9 @@ PeerImp::onMessage (std::shared_ptr const& m) } } - JLOG(p_journal_.trace()) << - "GetObj: " << reply.objects_size () << - " of " << packet.objects_size (); - send (std::make_shared (reply, protocol::mtGET_OBJECTS)); + JLOG(p_journal_.trace()) << "GetObj: " << reply.objects_size() << " of " + << packet.objects_size(); + send(std::make_shared(reply, protocol::mtGET_OBJECTS)); } else { @@ -2262,9 +2268,9 @@ PeerImp::onMessage (std::shared_ptr const& m) for (int i = 0; i < packet.objects_size(); ++i) { - const protocol::TMIndexedObject& obj = packet.objects (i); + const protocol::TMIndexedObject& obj = packet.objects(i); - if (obj.has_hash() && stringIsUint256Sized (obj.hash())) + if (obj.has_hash() && stringIsUint256Sized(obj.hash())) { if (obj.has_ledgerseq()) { @@ -2272,16 +2278,16 @@ PeerImp::onMessage (std::shared_ptr const& m) { if (pLDo && (pLSeq != 0)) { - JLOG(p_journal_.debug()) << - "GetObj: Full fetch pack for " << pLSeq; + JLOG(p_journal_.debug()) + << "GetObj: Full fetch pack for " << pLSeq; } pLSeq = obj.ledgerseq(); - pLDo = !app_.getLedgerMaster().haveLedger (pLSeq); + pLDo = !app_.getLedgerMaster().haveLedger(pLSeq); if (!pLDo) { - JLOG(p_journal_.debug()) << - "GetObj: Late fetch pack for " << pLSeq; + JLOG(p_journal_.debug()) + << "GetObj: Late fetch pack for " << pLSeq; } else progress = true; @@ -2290,56 +2296,56 @@ PeerImp::onMessage (std::shared_ptr const& m) if (pLDo) { - uint256 const hash {obj.hash()}; + uint256 const hash{obj.hash()}; - std::shared_ptr< Blob > data ( - std::make_shared< Blob > ( - obj.data().begin(), obj.data().end())); + std::shared_ptr data(std::make_shared( + obj.data().begin(), obj.data().end())); - app_.getLedgerMaster().addFetchPack (hash, data); + app_.getLedgerMaster().addFetchPack(hash, data); } } } if (pLDo && (pLSeq != 0)) { - JLOG(p_journal_.debug()) << - "GetObj: Partial fetch pack for " << pLSeq; + JLOG(p_journal_.debug()) + << "GetObj: Partial fetch pack for " << pLSeq; } - if (packet.type () == protocol::TMGetObjectByHash::otFETCH_PACK) - app_.getLedgerMaster ().gotFetchPack (progress, pLSeq); + if (packet.type() == protocol::TMGetObjectByHash::otFETCH_PACK) + app_.getLedgerMaster().gotFetchPack(progress, pLSeq); } } //-------------------------------------------------------------------------- void -PeerImp::addLedger (uint256 const& hash, +PeerImp::addLedger( + uint256 const& hash, std::lock_guard const& lockedRecentLock) { // lockedRecentLock is passed as a reminder that recentLock_ must be // locked by the caller. - (void) lockedRecentLock; + (void)lockedRecentLock; - if (std::find (recentLedgers_.begin(), - recentLedgers_.end(), hash) != recentLedgers_.end()) + if (std::find(recentLedgers_.begin(), recentLedgers_.end(), hash) != + recentLedgers_.end()) return; // VFALCO TODO See if a sorted vector would be better. - if (recentLedgers_.size () == 128) - recentLedgers_.pop_front (); + if (recentLedgers_.size() == 128) + recentLedgers_.pop_front(); - recentLedgers_.push_back (hash); + recentLedgers_.push_back(hash); } void -PeerImp::doFetchPack (const std::shared_ptr& packet) +PeerImp::doFetchPack(const std::shared_ptr& packet) { - // VFALCO TODO Invert this dependency using an observer and shared state object. - // Don't queue fetch pack jobs if we're under load or we already have - // some queued. - if (app_.getFeeTrack ().isLoadedLocal () || + // VFALCO TODO Invert this dependency using an observer and shared state + // object. Don't queue fetch pack jobs if we're under load or we already + // have some queued. + if (app_.getFeeTrack().isLoadedLocal() || (app_.getLedgerMaster().getValidatedLedgerAge() > 40s) || (app_.getJobQueue().getJobCount(jtPACK) > 10)) { @@ -2347,7 +2353,7 @@ PeerImp::doFetchPack (const std::shared_ptr& packet return; } - if (! stringIsUint256Sized (packet->ledgerhash())) + if (!stringIsUint256Sized(packet->ledgerhash())) { JLOG(p_journal_.warn()) << "FetchPack hash size malformed"; fee_ = Resource::feeInvalidRequest; @@ -2356,33 +2362,33 @@ PeerImp::doFetchPack (const std::shared_ptr& packet fee_ = Resource::feeHighBurdenPeer; - uint256 const hash {packet->ledgerhash()}; + uint256 const hash{packet->ledgerhash()}; std::weak_ptr weak = shared_from_this(); auto elapsed = UptimeClock::now(); auto const pap = &app_; - app_.getJobQueue ().addJob ( - jtPACK, "MakeFetchPack", - [pap, weak, packet, hash, elapsed] (Job&) { - pap->getLedgerMaster().makeFetchPack( - weak, packet, hash, elapsed); + app_.getJobQueue().addJob( + jtPACK, "MakeFetchPack", [pap, weak, packet, hash, elapsed](Job&) { + pap->getLedgerMaster().makeFetchPack(weak, packet, hash, elapsed); }); } void -PeerImp::checkTransaction (int flags, - bool checkSignature, std::shared_ptr const& stx) +PeerImp::checkTransaction( + int flags, + bool checkSignature, + std::shared_ptr const& stx) { // VFALCO TODO Rewrite to not use exceptions try { // Expired? if (stx->isFieldPresent(sfLastLedgerSequence) && - (stx->getFieldU32 (sfLastLedgerSequence) < - app_.getLedgerMaster().getValidLedgerIndex())) + (stx->getFieldU32(sfLastLedgerSequence) < + app_.getLedgerMaster().getValidLedgerIndex())) { app_.getHashRouter().setFlags(stx->getTransactionID(), SF_BAD); - charge (Resource::feeUnwantedData); + charge(Resource::feeUnwantedData); return; } @@ -2398,9 +2404,8 @@ PeerImp::checkTransaction (int flags, { if (!validReason.empty()) { - JLOG(p_journal_.trace()) << - "Exception checking transaction: " << - validReason; + JLOG(p_journal_.trace()) + << "Exception checking transaction: " << validReason; } // Probably not necessary to set SF_BAD, but doesn't hurt. @@ -2411,129 +2416,123 @@ PeerImp::checkTransaction (int flags, } else { - forceValidity(app_.getHashRouter(), - stx->getTransactionID(), Validity::Valid); + forceValidity( + app_.getHashRouter(), stx->getTransactionID(), Validity::Valid); } std::string reason; - auto tx = std::make_shared ( - stx, reason, app_); + auto tx = std::make_shared(stx, reason, app_); - if (tx->getStatus () == INVALID) + if (tx->getStatus() == INVALID) { - if (! reason.empty ()) + if (!reason.empty()) { - JLOG(p_journal_.trace()) << - "Exception checking transaction: " << reason; + JLOG(p_journal_.trace()) + << "Exception checking transaction: " << reason; } - app_.getHashRouter ().setFlags (stx->getTransactionID (), SF_BAD); - charge (Resource::feeInvalidSignature); + app_.getHashRouter().setFlags(stx->getTransactionID(), SF_BAD); + charge(Resource::feeInvalidSignature); return; } - bool const trusted (flags & SF_TRUSTED); - app_.getOPs ().processTransaction ( + bool const trusted(flags & SF_TRUSTED); + app_.getOPs().processTransaction( tx, trusted, false, NetworkOPs::FailHard::no); } catch (std::exception const&) { - app_.getHashRouter ().setFlags (stx->getTransactionID (), SF_BAD); - charge (Resource::feeBadData); + app_.getHashRouter().setFlags(stx->getTransactionID(), SF_BAD); + charge(Resource::feeBadData); } } // Called from our JobQueue void -PeerImp::checkPropose (Job& job, - std::shared_ptr const& packet, - RCLCxPeerPos peerPos) +PeerImp::checkPropose( + Job& job, + std::shared_ptr const& packet, + RCLCxPeerPos peerPos) { - bool isTrusted = (job.getType () == jtPROPOSAL_t); + bool isTrusted = (job.getType() == jtPROPOSAL_t); - JLOG(p_journal_.trace()) << - "Checking " << (isTrusted ? "trusted" : "UNTRUSTED") << " proposal"; + JLOG(p_journal_.trace()) + << "Checking " << (isTrusted ? "trusted" : "UNTRUSTED") << " proposal"; - assert (packet); + assert(packet); protocol::TMProposeSet& set = *packet; - if (! cluster() && !peerPos.checkSign ()) + if (!cluster() && !peerPos.checkSign()) { - JLOG(p_journal_.warn()) << - "Proposal fails sig check"; - charge (Resource::feeInvalidSignature); + JLOG(p_journal_.warn()) << "Proposal fails sig check"; + charge(Resource::feeInvalidSignature); return; } if (isTrusted) { - app_.getOPs ().processTrustedProposal (peerPos, packet); + app_.getOPs().processTrustedProposal(peerPos, packet); } else { if (cluster() || - (app_.getOPs().getConsensusLCL() == peerPos.proposal().prevLedger())) + (app_.getOPs().getConsensusLCL() == + peerPos.proposal().prevLedger())) { // relay untrusted proposal - JLOG(p_journal_.trace()) << - "relaying UNTRUSTED proposal"; + JLOG(p_journal_.trace()) << "relaying UNTRUSTED proposal"; overlay_.relay(set, peerPos.suppressionID()); } else { - JLOG(p_journal_.debug()) << - "Not relaying UNTRUSTED proposal"; + JLOG(p_journal_.debug()) << "Not relaying UNTRUSTED proposal"; } } } void -PeerImp::checkValidation (STValidation::pointer val, +PeerImp::checkValidation( + STValidation::pointer val, std::shared_ptr const& packet) { try { // VFALCO Which functions throw? - if (! cluster() && !val->isValid()) + if (!cluster() && !val->isValid()) { - JLOG(p_journal_.warn()) << - "Validation is invalid"; - charge (Resource::feeInvalidRequest); + JLOG(p_journal_.warn()) << "Validation is invalid"; + charge(Resource::feeInvalidRequest); return; } - if (app_.getOPs ().recvValidation(val, std::to_string(id())) || + if (app_.getOPs().recvValidation(val, std::to_string(id())) || cluster()) { - auto const suppression = sha512Half( - makeSlice(val->getSerialized())); + auto const suppression = + sha512Half(makeSlice(val->getSerialized())); overlay_.relay(*packet, suppression); } } catch (std::exception const&) { - JLOG(p_journal_.trace()) << - "Exception processing validation"; - charge (Resource::feeInvalidRequest); + JLOG(p_journal_.trace()) << "Exception processing validation"; + charge(Resource::feeInvalidRequest); } } // Returns the set of peers that can help us get // the TX tree with the specified root hash. // -static -std::shared_ptr -getPeerWithTree (OverlayImpl& ov, - uint256 const& rootHash, PeerImp const* skip) +static std::shared_ptr +getPeerWithTree(OverlayImpl& ov, uint256 const& rootHash, PeerImp const* skip) { std::shared_ptr ret; int retScore = 0; - ov.for_each([&](std::shared_ptr&& p) - { + ov.for_each([&](std::shared_ptr&& p) { if (p->hasTxSet(rootHash) && p.get() != skip) { - auto score = p->getScore (true); - if (! ret || (score > retScore)) + auto score = p->getScore(true); + if (!ret || (score > retScore)) { ret = std::move(p); retScore = score; @@ -2547,22 +2546,21 @@ getPeerWithTree (OverlayImpl& ov, // Returns a random peer weighted by how likely to // have the ledger and how responsive it is. // -static -std::shared_ptr -getPeerWithLedger (OverlayImpl& ov, - uint256 const& ledgerHash, LedgerIndex ledger, - PeerImp const* skip) +static std::shared_ptr +getPeerWithLedger( + OverlayImpl& ov, + uint256 const& ledgerHash, + LedgerIndex ledger, + PeerImp const* skip) { std::shared_ptr ret; int retScore = 0; - ov.for_each([&](std::shared_ptr&& p) - { - if (p->hasLedger(ledgerHash, ledger) && - p.get() != skip) + ov.for_each([&](std::shared_ptr&& p) { + if (p->hasLedger(ledgerHash, ledger) && p.get() != skip) { - auto score = p->getScore (true); - if (! ret || (score > retScore)) + auto score = p->getScore(true); + if (!ret || (score > retScore)) { ret = std::move(p); retScore = score; @@ -2575,7 +2573,7 @@ getPeerWithLedger (OverlayImpl& ov, // VFALCO NOTE This function is way too big and cumbersome. void -PeerImp::getLedger (std::shared_ptr const& m) +PeerImp::getLedger(std::shared_ptr const& m) { protocol::TMGetLedger& packet = *m; std::shared_ptr shared; @@ -2584,58 +2582,57 @@ PeerImp::getLedger (std::shared_ptr const& m) bool fatLeaves = true; std::shared_ptr ledger; - if (packet.has_requestcookie ()) - reply.set_requestcookie (packet.requestcookie ()); + if (packet.has_requestcookie()) + reply.set_requestcookie(packet.requestcookie()); std::string logMe; - if (packet.itype () == protocol::liTS_CANDIDATE) + if (packet.itype() == protocol::liTS_CANDIDATE) { // Request is for a transaction candidate set JLOG(p_journal_.trace()) << "GetLedger: Tx candidate set"; if (!packet.has_ledgerhash() || - !stringIsUint256Sized (packet.ledgerhash())) + !stringIsUint256Sized(packet.ledgerhash())) { - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); JLOG(p_journal_.warn()) << "GetLedger: Tx candidate set invalid"; return; } - uint256 const txHash {packet.ledgerhash()}; + uint256 const txHash{packet.ledgerhash()}; - shared = app_.getInboundTransactions().getSet (txHash, false); + shared = app_.getInboundTransactions().getSet(txHash, false); map = shared.get(); - if (! map) + if (!map) { - if (packet.has_querytype () && !packet.has_requestcookie ()) + if (packet.has_querytype() && !packet.has_requestcookie()) { JLOG(p_journal_.debug()) << "GetLedger: Routing Tx set request"; - auto const v = getPeerWithTree( - overlay_, txHash, this); - if (! v) + auto const v = getPeerWithTree(overlay_, txHash, this); + if (!v) { JLOG(p_journal_.info()) << "GetLedger: Route TX set failed"; return; } - packet.set_requestcookie (id ()); - v->send (std::make_shared ( - packet, protocol::mtGET_LEDGER)); + packet.set_requestcookie(id()); + v->send( + std::make_shared(packet, protocol::mtGET_LEDGER)); return; } JLOG(p_journal_.debug()) << "GetLedger: Can't provide map "; - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); return; } - reply.set_ledgerseq (0); - reply.set_ledgerhash (txHash.begin (), txHash.size ()); - reply.set_type (protocol::liTS_CANDIDATE); - fatLeaves = false; // We'll already have most transactions + reply.set_ledgerseq(0); + reply.set_ledgerhash(txHash.begin(), txHash.size()); + reply.set_type(protocol::liTS_CANDIDATE); + fatLeaves = false; // We'll already have most transactions } else { @@ -2645,7 +2642,7 @@ PeerImp::getLedger (std::shared_ptr const& m) return; } - if (app_.getFeeTrack().isLoadedLocal() && ! cluster()) + if (app_.getFeeTrack().isLoadedLocal() && !cluster()) { JLOG(p_journal_.debug()) << "GetLedger: Too busy"; return; @@ -2654,19 +2651,19 @@ PeerImp::getLedger (std::shared_ptr const& m) // Figure out what ledger they want JLOG(p_journal_.trace()) << "GetLedger: Received"; - if (packet.has_ledgerhash ()) + if (packet.has_ledgerhash()) { - if (! stringIsUint256Sized (packet.ledgerhash())) + if (!stringIsUint256Sized(packet.ledgerhash())) { - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); JLOG(p_journal_.warn()) << "GetLedger: Invalid request"; return; } - uint256 const ledgerhash {packet.ledgerhash()}; + uint256 const ledgerhash{packet.ledgerhash()}; logMe += "LedgerHash:"; - logMe += to_string (ledgerhash); - ledger = app_.getLedgerMaster ().getLedgerByHash (ledgerhash); + logMe += to_string(ledgerhash); + ledger = app_.getLedgerMaster().getLedgerByHash(ledgerhash); if (!ledger && packet.has_ledgerseq()) { @@ -2680,69 +2677,71 @@ PeerImp::getLedger (std::shared_ptr const& m) if (!ledger) { - JLOG(p_journal_.trace()) << - "GetLedger: Don't have " << ledgerhash; + JLOG(p_journal_.trace()) + << "GetLedger: Don't have " << ledgerhash; } - if (!ledger && (packet.has_querytype () && - !packet.has_requestcookie ())) + if (!ledger && + (packet.has_querytype() && !packet.has_requestcookie())) { // We don't have the requested ledger // Search for a peer who might - auto const v = getPeerWithLedger(overlay_, ledgerhash, - packet.has_ledgerseq() ? packet.ledgerseq() : 0, this); + auto const v = getPeerWithLedger( + overlay_, + ledgerhash, + packet.has_ledgerseq() ? packet.ledgerseq() : 0, + this); if (!v) { JLOG(p_journal_.trace()) << "GetLedger: Cannot route"; return; } - packet.set_requestcookie (id ()); - v->send (std::make_shared( - packet, protocol::mtGET_LEDGER)); + packet.set_requestcookie(id()); + v->send( + std::make_shared(packet, protocol::mtGET_LEDGER)); JLOG(p_journal_.debug()) << "GetLedger: Request routed"; return; } } - else if (packet.has_ledgerseq ()) + else if (packet.has_ledgerseq()) { - if (packet.ledgerseq() < - app_.getLedgerMaster().getEarliestFetch()) + if (packet.ledgerseq() < app_.getLedgerMaster().getEarliestFetch()) { JLOG(p_journal_.debug()) << "GetLedger: Early ledger request"; return; } - ledger = app_.getLedgerMaster ().getLedgerBySeq ( - packet.ledgerseq ()); - if (! ledger) + ledger = app_.getLedgerMaster().getLedgerBySeq(packet.ledgerseq()); + if (!ledger) { - JLOG(p_journal_.debug()) << - "GetLedger: Don't have " << packet.ledgerseq (); + JLOG(p_journal_.debug()) + << "GetLedger: Don't have " << packet.ledgerseq(); } } - else if (packet.has_ltype () && (packet.ltype () == protocol::ltCLOSED) ) + else if (packet.has_ltype() && (packet.ltype() == protocol::ltCLOSED)) { - ledger = app_.getLedgerMaster ().getClosedLedger (); - assert(! ledger->open()); + ledger = app_.getLedgerMaster().getClosedLedger(); + assert(!ledger->open()); // VFALCO ledger should never be null! // VFALCO How can the closed ledger be open? - #if 0 +#if 0 if (ledger && ledger->info().open) ledger = app_.getLedgerMaster ().getLedgerBySeq ( ledger->info().seq - 1); - #endif +#endif } else { - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); JLOG(p_journal_.warn()) << "GetLedger: Unknown request"; return; } - if ((!ledger) || (packet.has_ledgerseq () && ( - packet.ledgerseq () != ledger->info().seq))) + if ((!ledger) || + (packet.has_ledgerseq() && + (packet.ledgerseq() != ledger->info().seq))) { - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); if (ledger) { @@ -2751,8 +2750,8 @@ PeerImp::getLedger (std::shared_ptr const& m) return; } - if (!packet.has_ledgerseq() && (ledger->info().seq < - app_.getLedgerMaster().getEarliestFetch())) + if (!packet.has_ledgerseq() && + (ledger->info().seq < app_.getLedgerMaster().getEarliestFetch())) { JLOG(p_journal_.debug()) << "GetLedger: Early ledger request"; return; @@ -2760,206 +2759,205 @@ PeerImp::getLedger (std::shared_ptr const& m) // Fill out the reply auto const lHash = ledger->info().hash; - reply.set_ledgerhash (lHash.begin (), lHash.size ()); - reply.set_ledgerseq (ledger->info().seq); - reply.set_type (packet.itype ()); + reply.set_ledgerhash(lHash.begin(), lHash.size()); + reply.set_ledgerseq(ledger->info().seq); + reply.set_type(packet.itype()); - if (packet.itype () == protocol::liBASE) + if (packet.itype() == protocol::liBASE) { // they want the ledger base data JLOG(p_journal_.trace()) << "GetLedger: Base data"; - Serializer nData (128); + Serializer nData(128); addRaw(ledger->info(), nData); - reply.add_nodes ()->set_nodedata ( - nData.getDataPtr (), nData.getLength ()); + reply.add_nodes()->set_nodedata( + nData.getDataPtr(), nData.getLength()); - auto const& stateMap = ledger->stateMap (); + auto const& stateMap = ledger->stateMap(); if (stateMap.getHash() != beast::zero) { // return account state root node if possible - Serializer rootNode (768); + Serializer rootNode(768); if (stateMap.getRootNode(rootNode, snfWIRE)) { - reply.add_nodes ()->set_nodedata ( - rootNode.getDataPtr (), rootNode.getLength ()); + reply.add_nodes()->set_nodedata( + rootNode.getDataPtr(), rootNode.getLength()); if (ledger->info().txHash != beast::zero) { - auto const& txMap = ledger->txMap (); + auto const& txMap = ledger->txMap(); if (txMap.getHash() != beast::zero) { - rootNode.erase (); + rootNode.erase(); - if (txMap.getRootNode (rootNode, snfWIRE)) - reply.add_nodes ()->set_nodedata ( - rootNode.getDataPtr (), - rootNode.getLength ()); + if (txMap.getRootNode(rootNode, snfWIRE)) + reply.add_nodes()->set_nodedata( + rootNode.getDataPtr(), + rootNode.getLength()); } } } } - auto oPacket = std::make_shared ( - reply, protocol::mtLEDGER_DATA); - send (oPacket); + auto oPacket = + std::make_shared(reply, protocol::mtLEDGER_DATA); + send(oPacket); return; } - if (packet.itype () == protocol::liTX_NODE) + if (packet.itype() == protocol::liTX_NODE) { - map = &ledger->txMap (); + map = &ledger->txMap(); logMe += " TX:"; - logMe += to_string (map->getHash ()); + logMe += to_string(map->getHash()); } - else if (packet.itype () == protocol::liAS_NODE) + else if (packet.itype() == protocol::liAS_NODE) { - map = &ledger->stateMap (); + map = &ledger->stateMap(); logMe += " AS:"; - logMe += to_string (map->getHash ()); + logMe += to_string(map->getHash()); } } - if (!map || (packet.nodeids_size () == 0)) + if (!map || (packet.nodeids_size() == 0)) { - JLOG(p_journal_.warn()) << - "GetLedger: Can't find map or empty request"; - charge (Resource::feeInvalidRequest); + JLOG(p_journal_.warn()) << "GetLedger: Can't find map or empty request"; + charge(Resource::feeInvalidRequest); return; } JLOG(p_journal_.trace()) << "GetLedger: " << logMe; - auto const depth = - packet.has_querydepth() ? - (std::min(packet.querydepth(), 3u)) : - (isHighLatency() ? 2 : 1); + auto const depth = packet.has_querydepth() + ? (std::min(packet.querydepth(), 3u)) + : (isHighLatency() ? 2 : 1); for (int i = 0; - (i < packet.nodeids().size() && - (reply.nodes().size() < Tuning::maxReplyNodes)); ++i) + (i < packet.nodeids().size() && + (reply.nodes().size() < Tuning::maxReplyNodes)); + ++i) { - SHAMapNodeID mn (packet.nodeids (i).data (), packet.nodeids (i).size ()); + SHAMapNodeID mn(packet.nodeids(i).data(), packet.nodeids(i).size()); - if (!mn.isValid ()) + if (!mn.isValid()) { JLOG(p_journal_.warn()) << "GetLedger: Invalid node " << logMe; - charge (Resource::feeInvalidRequest); + charge(Resource::feeInvalidRequest); return; } std::vector nodeIDs; - std::vector< Blob > rawNodes; + std::vector rawNodes; try { if (map->getNodeFat(mn, nodeIDs, rawNodes, fatLeaves, depth)) { - assert (nodeIDs.size () == rawNodes.size ()); - JLOG(p_journal_.trace()) << - "GetLedger: getNodeFat got " << rawNodes.size () << " nodes"; + assert(nodeIDs.size() == rawNodes.size()); + JLOG(p_journal_.trace()) << "GetLedger: getNodeFat got " + << rawNodes.size() << " nodes"; std::vector::iterator nodeIDIterator; - std::vector< Blob >::iterator rawNodeIterator; + std::vector::iterator rawNodeIterator; - for (nodeIDIterator = nodeIDs.begin (), - rawNodeIterator = rawNodes.begin (); - nodeIDIterator != nodeIDs.end (); - ++nodeIDIterator, ++rawNodeIterator) + for (nodeIDIterator = nodeIDs.begin(), + rawNodeIterator = rawNodes.begin(); + nodeIDIterator != nodeIDs.end(); + ++nodeIDIterator, ++rawNodeIterator) { - Serializer nID (33); - nodeIDIterator->addIDRaw (nID); - protocol::TMLedgerNode* node = reply.add_nodes (); - node->set_nodeid (nID.getDataPtr (), nID.getLength ()); - node->set_nodedata (&rawNodeIterator->front (), - rawNodeIterator->size ()); + Serializer nID(33); + nodeIDIterator->addIDRaw(nID); + protocol::TMLedgerNode* node = reply.add_nodes(); + node->set_nodeid(nID.getDataPtr(), nID.getLength()); + node->set_nodedata( + &rawNodeIterator->front(), rawNodeIterator->size()); } } else { - JLOG(p_journal_.warn()) << - "GetLedger: getNodeFat returns false"; + JLOG(p_journal_.warn()) + << "GetLedger: getNodeFat returns false"; } } catch (std::exception&) { std::string info; - if (packet.itype () == protocol::liTS_CANDIDATE) + if (packet.itype() == protocol::liTS_CANDIDATE) info = "TS candidate"; - else if (packet.itype () == protocol::liBASE) + else if (packet.itype() == protocol::liBASE) info = "Ledger base"; - else if (packet.itype () == protocol::liTX_NODE) + else if (packet.itype() == protocol::liTX_NODE) info = "TX node"; - else if (packet.itype () == protocol::liAS_NODE) + else if (packet.itype() == protocol::liAS_NODE) info = "AS node"; - if (!packet.has_ledgerhash ()) + if (!packet.has_ledgerhash()) info += ", no hash specified"; - JLOG(p_journal_.warn()) << - "getNodeFat( " << mn << ") throws exception: " << info; + JLOG(p_journal_.warn()) + << "getNodeFat( " << mn << ") throws exception: " << info; } } - JLOG(p_journal_.info()) << - "Got request for " << packet.nodeids().size() << " nodes at depth " << - depth << ", return " << reply.nodes().size() << " nodes"; + JLOG(p_journal_.info()) + << "Got request for " << packet.nodeids().size() << " nodes at depth " + << depth << ", return " << reply.nodes().size() << " nodes"; - auto oPacket = std::make_shared ( - reply, protocol::mtLEDGER_DATA); - send (oPacket); + auto oPacket = std::make_shared(reply, protocol::mtLEDGER_DATA); + send(oPacket); } void -PeerImp::peerTXData (uint256 const& hash, - std::shared_ptr const& pPacket, - beast::Journal journal) +PeerImp::peerTXData( + uint256 const& hash, + std::shared_ptr const& pPacket, + beast::Journal journal) { - app_.getInboundTransactions().gotData (hash, shared_from_this(), pPacket); + app_.getInboundTransactions().gotData(hash, shared_from_this(), pPacket); } int -PeerImp::getScore (bool haveItem) const +PeerImp::getScore(bool haveItem) const { - // Random component of score, used to break ties and avoid - // overloading the "best" peer - static const int spRandomMax = 9999; + // Random component of score, used to break ties and avoid + // overloading the "best" peer + static const int spRandomMax = 9999; - // Score for being very likely to have the thing we are - // look for; should be roughly spRandomMax - static const int spHaveItem = 10000; + // Score for being very likely to have the thing we are + // look for; should be roughly spRandomMax + static const int spHaveItem = 10000; - // Score reduction for each millisecond of latency; should - // be roughly spRandomMax divided by the maximum reasonable - // latency - static const int spLatency = 30; + // Score reduction for each millisecond of latency; should + // be roughly spRandomMax divided by the maximum reasonable + // latency + static const int spLatency = 30; - // Penalty for unknown latency; should be roughly spRandomMax - static const int spNoLatency = 8000; + // Penalty for unknown latency; should be roughly spRandomMax + static const int spNoLatency = 8000; - int score = rand_int(spRandomMax); + int score = rand_int(spRandomMax); - if (haveItem) - score += spHaveItem; + if (haveItem) + score += spHaveItem; - boost::optional latency; - { - std::lock_guard sl (recentLock_); - latency = latency_; - } + boost::optional latency; + { + std::lock_guard sl(recentLock_); + latency = latency_; + } - if (latency) - score -= latency->count() * spLatency; - else - score -= spNoLatency; + if (latency) + score -= latency->count() * spLatency; + else + score -= spNoLatency; - return score; + return score; } bool PeerImp::isHighLatency() const { - std::lock_guard sl (recentLock_); + std::lock_guard sl(recentLock_); return latency_ >= Tuning::peerHighLatency; } @@ -2967,19 +2965,21 @@ void PeerImp::Metrics::add_message(std::uint64_t bytes) { using namespace std::chrono_literals; - std::unique_lock lock{ mutex_ }; + std::unique_lock lock{mutex_}; totalBytes_ += bytes; accumBytes_ += bytes; auto const timeElapsed = clock_type::now() - intervalStart_; - auto const timeElapsedInSecs = std::chrono::duration_cast(timeElapsed); + auto const timeElapsedInSecs = + std::chrono::duration_cast(timeElapsed); if (timeElapsedInSecs >= 1s) { auto const avgBytes = accumBytes_ / timeElapsedInSecs.count(); rollingAvg_.push_back(avgBytes); - auto const totalBytes = std::accumulate(rollingAvg_.begin(), rollingAvg_.end(), 0ull); + auto const totalBytes = + std::accumulate(rollingAvg_.begin(), rollingAvg_.end(), 0ull); rollingAvgBytes_ = totalBytes / rollingAvg_.size(); intervalStart_ = clock_type::now(); @@ -2988,16 +2988,17 @@ PeerImp::Metrics::add_message(std::uint64_t bytes) } std::uint64_t -PeerImp::Metrics::average_bytes() const { - std::shared_lock lock{ mutex_ }; +PeerImp::Metrics::average_bytes() const +{ + std::shared_lock lock{mutex_}; return rollingAvgBytes_; } std::uint64_t -PeerImp::Metrics::total_bytes() const { - std::shared_lock lock{ mutex_ }; +PeerImp::Metrics::total_bytes() const +{ + std::shared_lock lock{mutex_}; return totalBytes_; } - -} // ripple +} // namespace ripple diff --git a/src/ripple/overlay/impl/PeerImp.h b/src/ripple/overlay/impl/PeerImp.h index 0d48451716..e302c6a608 100644 --- a/src/ripple/overlay/impl/PeerImp.h +++ b/src/ripple/overlay/impl/PeerImp.h @@ -24,9 +24,9 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -43,44 +43,35 @@ namespace ripple { -class PeerImp - : public Peer - , public std::enable_shared_from_this - , public OverlayImpl::Child +class PeerImp : public Peer, + public std::enable_shared_from_this, + public OverlayImpl::Child { public: /** Type of connection. This affects how messages are routed. */ - enum class Type - { - legacy, - leaf, - peer - }; + enum class Type { legacy, leaf, peer }; /** Current state */ - enum class State - { + enum class State { /** A connection is being established (outbound) */ connecting /** Connection has been successfully established */ - ,connected + , + connected /** Handshake has been received from this peer */ - ,handshaked + , + handshaked /** Running the Ripple protocol actively */ - ,active + , + active }; - enum class Sanity - { - insane - ,unknown - ,sane - }; + enum class Sanity { insane, unknown, sane }; struct ShardInfo { @@ -88,18 +79,19 @@ public: RangeSet shardIndexes; }; - using ptr = std::shared_ptr ; + using ptr = std::shared_ptr; private: - using clock_type = std::chrono::steady_clock; - using error_code = boost::system::error_code; - using socket_type = boost::asio::ip::tcp::socket; - using middle_type = boost::beast::tcp_stream; - using stream_type = boost::beast::ssl_stream ; - using address_type = boost::asio::ip::address; + using clock_type = std::chrono::steady_clock; + using error_code = boost::system::error_code; + using socket_type = boost::asio::ip::tcp::socket; + using middle_type = boost::beast::tcp_stream; + using stream_type = boost::beast::ssl_stream; + using address_type = boost::asio::ip::address; using endpoint_type = boost::asio::ip::tcp::endpoint; - using waitable_timer = boost::asio::basic_waitable_timer; - using Compressed = compression::Compressed; + using waitable_timer = + boost::asio::basic_waitable_timer; + using Compressed = compression::Compressed; Application& app_; id_t const id_; @@ -113,7 +105,7 @@ private: boost::asio::strand strand_; waitable_timer timer_; - //Type type_ = Type::legacy; + // Type type_ = Type::legacy; // Updated at each stage of the connection process to reflect // the current conditions as closely as possible. @@ -127,7 +119,7 @@ private: // Protocol version to use for this link ProtocolVersion protocol_; - State state_; // Current state + State state_; // Current state std::atomic sanity_; clock_type::time_point insaneTime_; bool detaching_ = false; @@ -177,7 +169,7 @@ private: // o lastPingSeq_ // o lastPingTime_ // o no_ping_ - // + // // June 2019 std::mutex mutable recentLock_; @@ -194,7 +186,7 @@ private: bool gracefulClose_ = false; int large_sendq_ = 0; int no_ping_ = 0; - std::unique_ptr load_event_; + std::unique_ptr load_event_; // The highest sequence of each PublisherList that has // been sent to or received from this peer. hash_map publisherListSequences_; @@ -206,55 +198,72 @@ private: friend class OverlayImpl; - class Metrics { + class Metrics + { public: Metrics() = default; Metrics(Metrics const&) = delete; - Metrics& operator=(Metrics const&) = delete; + Metrics& + operator=(Metrics const&) = delete; Metrics(Metrics&&) = delete; - Metrics& operator=(Metrics&&) = delete; + Metrics& + operator=(Metrics&&) = delete; - void add_message(std::uint64_t bytes); - std::uint64_t average_bytes() const; - std::uint64_t total_bytes() const; + void + add_message(std::uint64_t bytes); + std::uint64_t + average_bytes() const; + std::uint64_t + total_bytes() const; private: std::shared_mutex mutable mutex_; - boost::circular_buffer rollingAvg_{ 30, 0ull }; - clock_type::time_point intervalStart_{ clock_type::now() }; - std::uint64_t totalBytes_{ 0 }; - std::uint64_t accumBytes_{ 0 }; - std::uint64_t rollingAvgBytes_{ 0 }; + boost::circular_buffer rollingAvg_{30, 0ull}; + clock_type::time_point intervalStart_{clock_type::now()}; + std::uint64_t totalBytes_{0}; + std::uint64_t accumBytes_{0}; + std::uint64_t rollingAvgBytes_{0}; }; - struct { + struct + { Metrics sent; Metrics recv; } metrics_; public: - PeerImp (PeerImp const&) = delete; - PeerImp& operator= (PeerImp const&) = delete; + PeerImp(PeerImp const&) = delete; + PeerImp& + operator=(PeerImp const&) = delete; /** Create an active incoming peer from an established ssl connection. */ - PeerImp (Application& app, id_t id, - std::shared_ptr const& slot, http_request_type&& request, - PublicKey const& publicKey, - ProtocolVersion protocol, Resource::Consumer consumer, - std::unique_ptr&& stream_ptr, - OverlayImpl& overlay); + PeerImp( + Application& app, + id_t id, + std::shared_ptr const& slot, + http_request_type&& request, + PublicKey const& publicKey, + ProtocolVersion protocol, + Resource::Consumer consumer, + std::unique_ptr&& stream_ptr, + OverlayImpl& overlay); /** Create outgoing, handshaked peer. */ // VFALCO legacyPublicKey should be implied by the Slot template - PeerImp (Application& app, std::unique_ptr&& stream_ptr, - Buffers const& buffers, std::shared_ptr&& slot, - http_response_type&& response, Resource::Consumer usage, - PublicKey const& publicKey, - ProtocolVersion protocol, id_t id, OverlayImpl& overlay); + PeerImp( + Application& app, + std::unique_ptr&& stream_ptr, + Buffers const& buffers, + std::shared_ptr&& slot, + http_response_type&& response, + Resource::Consumer usage, + PublicKey const& publicKey, + ProtocolVersion protocol, + id_t id, + OverlayImpl& overlay); - virtual - ~PeerImp(); + virtual ~PeerImp(); beast::Journal const& pjournal() const @@ -281,14 +290,16 @@ public: // void - send (std::shared_ptr const& m) override; + send(std::shared_ptr const& m) override; /** Send a set of PeerFinder endpoints as a protocol message. */ - template ::value_type, + template < + class FwdIt, + class = typename std::enable_if_t::value_type, PeerFinder::Endpoint>::value>> void - sendEndpoints (FwdIt first, FwdIt last); + sendEndpoints(FwdIt first, FwdIt last); beast::IP::Endpoint getRemoteAddress() const override @@ -297,7 +308,7 @@ public: } void - charge (Resource::Charge const& fee) override; + charge(Resource::Charge const& fee) override; // // Identity @@ -323,13 +334,13 @@ public: @param validationSeq The ledger sequence of a recently-validated ledger */ void - checkSanity (std::uint32_t validationSeq); + checkSanity(std::uint32_t validationSeq); void - checkSanity (std::uint32_t seq1, std::uint32_t seq2); + checkSanity(std::uint32_t seq1, std::uint32_t seq2); PublicKey const& - getNodePublic () const override + getNodePublic() const override { return publicKey_; } @@ -364,7 +375,7 @@ public: void setPublisherListSequence(PublicKey const& pubKey, std::size_t const seq) - override + override { std::lock_guard sl(recentLock_); @@ -376,32 +387,32 @@ public: // uint256 const& - getClosedLedgerHash () const override + getClosedLedgerHash() const override { return closedLedgerHash_; } bool - hasLedger (uint256 const& hash, std::uint32_t seq) const override; + hasLedger(uint256 const& hash, std::uint32_t seq) const override; void - ledgerRange (std::uint32_t& minSeq, std::uint32_t& maxSeq) const override; + ledgerRange(std::uint32_t& minSeq, std::uint32_t& maxSeq) const override; bool - hasShard (std::uint32_t shardIndex) const override; + hasShard(std::uint32_t shardIndex) const override; bool - hasTxSet (uint256 const& hash) const override; + hasTxSet(uint256 const& hash) const override; void - cycleStatus () override; + cycleStatus() override; bool - hasRange (std::uint32_t uMin, std::uint32_t uMax) override; + hasRange(std::uint32_t uMin, std::uint32_t uMax) override; // Called to determine our priority for querying int - getScore (bool haveItem) const override; + getScore(bool haveItem) const override; bool isHighLatency() const override; @@ -433,28 +444,29 @@ private: void cancelTimer(); - static - std::string + static std::string makePrefix(id_t id); // Called when the timer wait completes void - onTimer (boost::system::error_code const& ec); + onTimer(boost::system::error_code const& ec); // Called when SSL shutdown completes void - onShutdown (error_code ec); + onShutdown(error_code ec); void doAccept(); http_response_type - makeResponse (bool crawl, http_request_type const& req, + makeResponse( + bool crawl, + http_request_type const& req, beast::IP::Address remote_ip, uint256 const& sharedValue); void - onWriteResponse (error_code ec, std::size_t bytes_transferred); + onWriteResponse(error_code ec, std::size_t bytes_transferred); // A thread-safe way of getting the name. std::string @@ -466,15 +478,15 @@ private: // Starts the protocol message loop void - doProtocolStart (); + doProtocolStart(); // Called when protocol message bytes are received void - onReadMessage (error_code ec, std::size_t bytes_transferred); + onReadMessage(error_code ec, std::size_t bytes_transferred); // Called when protocol messages bytes are sent void - onWriteMessage (error_code ec, std::size_t bytes_transferred); + onWriteMessage(error_code ec, std::size_t bytes_transferred); public: //-------------------------------------------------------------------------- @@ -483,51 +495,71 @@ public: // //-------------------------------------------------------------------------- - static - error_code + static error_code invalid_argument_error() { - return boost::system::errc::make_error_code ( + return boost::system::errc::make_error_code( boost::system::errc::invalid_argument); } void - onMessageUnknown (std::uint16_t type); + onMessageUnknown(std::uint16_t type); void - onMessageBegin (std::uint16_t type, - std::shared_ptr <::google::protobuf::Message> const& m, + onMessageBegin( + std::uint16_t type, + std::shared_ptr<::google::protobuf::Message> const& m, std::size_t size); void - onMessageEnd (std::uint16_t type, - std::shared_ptr <::google::protobuf::Message> const& m); + onMessageEnd( + std::uint16_t type, + std::shared_ptr<::google::protobuf::Message> const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); - void onMessage (std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); + void + onMessage(std::shared_ptr const& m); private: - State state() const + State + state() const { return state_; } - void state (State new_state) + void + state(State new_state) { state_ = new_state; } @@ -539,108 +571,119 @@ private: // lockedRecentLock is passed as a reminder to callers that recentLock_ // must be locked. void - addLedger (uint256 const& hash, + addLedger( + uint256 const& hash, std::lock_guard const& lockedRecentLock); void - doFetchPack (const std::shared_ptr& packet); + doFetchPack(const std::shared_ptr& packet); void - checkTransaction (int flags, bool checkSignature, + checkTransaction( + int flags, + bool checkSignature, std::shared_ptr const& stx); void - checkPropose (Job& job, + checkPropose( + Job& job, std::shared_ptr const& packet, - RCLCxPeerPos peerPos); + RCLCxPeerPos peerPos); void - checkValidation (STValidation::pointer val, + checkValidation( + STValidation::pointer val, std::shared_ptr const& packet); void - getLedger (std::shared_ptr const&packet); + getLedger(std::shared_ptr const& packet); // Called when we receive tx set data. void - peerTXData (uint256 const& hash, - std::shared_ptr const& pPacket, - beast::Journal journal); + peerTXData( + uint256 const& hash, + std::shared_ptr const& pPacket, + beast::Journal journal); }; - //------------------------------------------------------------------------------ template -PeerImp::PeerImp (Application& app, std::unique_ptr&& stream_ptr, - Buffers const& buffers, std::shared_ptr&& slot, - http_response_type&& response, Resource::Consumer usage, - PublicKey const& publicKey, - ProtocolVersion protocol, id_t id, OverlayImpl& overlay) - : Child (overlay) - , app_ (app) - , id_ (id) - , sink_ (app_.journal("Peer"), makePrefix(id)) - , p_sink_ (app_.journal("Protocol"), makePrefix(id)) - , journal_ (sink_) - , p_journal_ (p_sink_) +PeerImp::PeerImp( + Application& app, + std::unique_ptr&& stream_ptr, + Buffers const& buffers, + std::shared_ptr&& slot, + http_response_type&& response, + Resource::Consumer usage, + PublicKey const& publicKey, + ProtocolVersion protocol, + id_t id, + OverlayImpl& overlay) + : Child(overlay) + , app_(app) + , id_(id) + , sink_(app_.journal("Peer"), makePrefix(id)) + , p_sink_(app_.journal("Protocol"), makePrefix(id)) + , journal_(sink_) + , p_journal_(p_sink_) , stream_ptr_(std::move(stream_ptr)) - , socket_ (stream_ptr_->next_layer().socket()) - , stream_ (*stream_ptr_) - , strand_ (socket_.get_executor()) - , timer_ (waitable_timer{socket_.get_executor()}) - , remote_address_ (slot->remote_endpoint()) - , overlay_ (overlay) - , m_inbound (false) - , protocol_ (protocol) - , state_ (State::active) - , sanity_ (Sanity::unknown) - , insaneTime_ (clock_type::now()) - , publicKey_ (publicKey) - , creationTime_ (clock_type::now()) - , usage_ (usage) - , fee_ (Resource::feeLightPeer) - , slot_ (std::move(slot)) + , socket_(stream_ptr_->next_layer().socket()) + , stream_(*stream_ptr_) + , strand_(socket_.get_executor()) + , timer_(waitable_timer{socket_.get_executor()}) + , remote_address_(slot->remote_endpoint()) + , overlay_(overlay) + , m_inbound(false) + , protocol_(protocol) + , state_(State::active) + , sanity_(Sanity::unknown) + , insaneTime_(clock_type::now()) + , publicKey_(publicKey) + , creationTime_(clock_type::now()) + , usage_(usage) + , fee_(Resource::feeLightPeer) + , slot_(std::move(slot)) , response_(std::move(response)) , headers_(response_) , compressionEnabled_( - headers_["X-Offer-Compression"] == "lz4" && app_.config().COMPRESSION - ? Compressed::On : Compressed::Off) + headers_["X-Offer-Compression"] == "lz4" && app_.config().COMPRESSION + ? Compressed::On + : Compressed::Off) { - read_buffer_.commit (boost::asio::buffer_copy(read_buffer_.prepare( - boost::asio::buffer_size(buffers)), buffers)); + read_buffer_.commit(boost::asio::buffer_copy( + read_buffer_.prepare(boost::asio::buffer_size(buffers)), buffers)); } template void -PeerImp::sendEndpoints (FwdIt first, FwdIt last) +PeerImp::sendEndpoints(FwdIt first, FwdIt last) { protocol::TMEndpoints tm; - for (;first != last; ++first) + for (; first != last; ++first) { auto const& ep = *first; // eventually remove endpoints and just keep endpoints_v2 // (once we are sure the entire network understands endpoints_v2) - protocol::TMEndpoint& tme (*tm.add_endpoints()); + protocol::TMEndpoint& tme(*tm.add_endpoints()); if (ep.address.is_v4()) - tme.mutable_ipv4()->set_ipv4( - boost::endian::native_to_big( - static_cast(ep.address.to_v4().to_ulong()))); + tme.mutable_ipv4()->set_ipv4(boost::endian::native_to_big( + static_cast(ep.address.to_v4().to_ulong()))); else tme.mutable_ipv4()->set_ipv4(0); - tme.mutable_ipv4()->set_ipv4port (ep.address.port()); - tme.set_hops (ep.hops); + tme.mutable_ipv4()->set_ipv4port(ep.address.port()); + tme.set_hops(ep.hops); // add v2 endpoints (strings) - auto& tme2 (*tm.add_endpoints_v2()); + auto& tme2(*tm.add_endpoints_v2()); tme2.set_endpoint(ep.address.to_string()); - tme2.set_hops (ep.hops); + tme2.set_hops(ep.hops); } - tm.set_version (2); + tm.set_version(2); - send (std::make_shared (tm, protocol::mtENDPOINTS)); + send(std::make_shared(tm, protocol::mtENDPOINTS)); } -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/PeerReservationTable.cpp b/src/ripple/overlay/impl/PeerReservationTable.cpp index 4f6dd2d019..32d0875fe3 100644 --- a/src/ripple/overlay/impl/PeerReservationTable.cpp +++ b/src/ripple/overlay/impl/PeerReservationTable.cpp @@ -105,8 +105,7 @@ PeerReservationTable::load(DatabaseCon& connection) } auto -PeerReservationTable::insert_or_assign( - PeerReservation const& reservation) +PeerReservationTable::insert_or_assign(PeerReservation const& reservation) -> boost::optional { boost::optional previous; @@ -114,7 +113,8 @@ PeerReservationTable::insert_or_assign( std::lock_guard lock(mutex_); auto hint = table_.find(reservation); - if (hint != table_.end()) { + if (hint != table_.end()) + { // The node already has a reservation. Remove it. // `std::unordered_set` does not have an `insert_or_assign` method, // and sadly makes it impossible for us to implement one efficiently: diff --git a/src/ripple/overlay/impl/PeerSet.cpp b/src/ripple/overlay/impl/PeerSet.cpp index e17a04a6b1..79ad9d6893 100644 --- a/src/ripple/overlay/impl/PeerSet.cpp +++ b/src/ripple/overlay/impl/PeerSet.cpp @@ -17,10 +17,10 @@ */ //============================================================================== -#include #include #include #include +#include namespace ripple { @@ -35,116 +35,128 @@ class InboundLedger; // derived class. Why not just make the timer callback // function pure virtual? // -PeerSet::PeerSet (Application& app, uint256 const& hash, - std::chrono::milliseconds interval, clock_type& clock, +PeerSet::PeerSet( + Application& app, + uint256 const& hash, + std::chrono::milliseconds interval, + clock_type& clock, beast::Journal journal) - : app_ (app) - , m_journal (journal) - , m_clock (clock) - , mHash (hash) - , mTimerInterval (interval) - , mTimeouts (0) - , mComplete (false) - , mFailed (false) - , mProgress (false) - , mTimer (app_.getIOService ()) + : app_(app) + , m_journal(journal) + , m_clock(clock) + , mHash(hash) + , mTimerInterval(interval) + , mTimeouts(0) + , mComplete(false) + , mFailed(false) + , mProgress(false) + , mTimer(app_.getIOService()) { mLastAction = m_clock.now(); - assert ((mTimerInterval > 10ms) && (mTimerInterval < 30s)); + assert((mTimerInterval > 10ms) && (mTimerInterval < 30s)); } PeerSet::~PeerSet() = default; -bool PeerSet::insert (std::shared_ptr const& ptr) +bool +PeerSet::insert(std::shared_ptr const& ptr) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - if (!mPeers.insert (ptr->id ()).second) + if (!mPeers.insert(ptr->id()).second) return false; - newPeer (ptr); + newPeer(ptr); return true; } -void PeerSet::setTimer () +void +PeerSet::setTimer() { mTimer.expires_from_now(mTimerInterval); - mTimer.async_wait ( - [wptr=pmDowncast()](boost::system::error_code const& ec) - { + mTimer.async_wait( + [wptr = pmDowncast()](boost::system::error_code const& ec) { if (ec == boost::asio::error::operation_aborted) return; - if (auto ptr = wptr.lock ()) - ptr->execute (); + if (auto ptr = wptr.lock()) + ptr->execute(); }); } -void PeerSet::invokeOnTimer () +void +PeerSet::invokeOnTimer() { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - if (isDone ()) + if (isDone()) return; if (!isProgress()) { ++mTimeouts; - JLOG (m_journal.debug()) << "Timeout(" << mTimeouts - << ") pc=" << mPeers.size () << " acquiring " << mHash; - onTimer (false, sl); + JLOG(m_journal.debug()) + << "Timeout(" << mTimeouts << ") pc=" << mPeers.size() + << " acquiring " << mHash; + onTimer(false, sl); } else { mProgress = false; - onTimer (true, sl); + onTimer(true, sl); } - if (!isDone ()) - setTimer (); + if (!isDone()) + setTimer(); } -bool PeerSet::isActive () +bool +PeerSet::isActive() { - ScopedLockType sl (mLock); - return !isDone (); + ScopedLockType sl(mLock); + return !isDone(); } -void PeerSet::sendRequest (const protocol::TMGetLedger& tmGL, std::shared_ptr const& peer) +void +PeerSet::sendRequest( + const protocol::TMGetLedger& tmGL, + std::shared_ptr const& peer) { if (!peer) - sendRequest (tmGL); + sendRequest(tmGL); else - peer->send (std::make_shared (tmGL, protocol::mtGET_LEDGER)); + peer->send(std::make_shared(tmGL, protocol::mtGET_LEDGER)); } -void PeerSet::sendRequest (const protocol::TMGetLedger& tmGL) +void +PeerSet::sendRequest(const protocol::TMGetLedger& tmGL) { - ScopedLockType sl (mLock); + ScopedLockType sl(mLock); - if (mPeers.empty ()) + if (mPeers.empty()) return; auto packet = std::make_shared(tmGL, protocol::mtGET_LEDGER); for (auto id : mPeers) { - if (auto peer = app_.overlay ().findPeerByShortID (id)) - peer->send (packet); + if (auto peer = app_.overlay().findPeerByShortID(id)) + peer->send(packet); } } -std::size_t PeerSet::getPeerCount () const +std::size_t +PeerSet::getPeerCount() const { - std::size_t ret (0); + std::size_t ret(0); for (auto id : mPeers) { - if (app_.overlay ().findPeerByShortID (id)) + if (app_.overlay().findPeerByShortID(id)) ++ret; } return ret; } -} // ripple +} // namespace ripple diff --git a/src/ripple/overlay/impl/ProtocolMessage.h b/src/ripple/overlay/impl/ProtocolMessage.h index be4a93fbb4..0f97cdd11b 100644 --- a/src/ripple/overlay/impl/ProtocolMessage.h +++ b/src/ripple/overlay/impl/ProtocolMessage.h @@ -21,10 +21,10 @@ #define RIPPLE_OVERLAY_PROTOCOLMESSAGE_H_INCLUDED #include -#include #include #include #include +#include #include #include #include @@ -39,29 +39,46 @@ namespace ripple { /** Returns the name of a protocol message given its type. */ template std::string -protocolMessageName (int type) +protocolMessageName(int type) { switch (type) { - case protocol::mtMANIFESTS: return "manifests"; - case protocol::mtPING: return "ping"; - case protocol::mtCLUSTER: return "cluster"; - case protocol::mtGET_SHARD_INFO: return "get_shard_info"; - case protocol::mtSHARD_INFO: return "shard_info"; - case protocol::mtGET_PEER_SHARD_INFO: return "get_peer_shard_info"; - case protocol::mtPEER_SHARD_INFO: return "peer_shard_info"; - case protocol::mtENDPOINTS: return "endpoints"; - case protocol::mtTRANSACTION: return "tx"; - case protocol::mtGET_LEDGER: return "get_ledger"; - case protocol::mtLEDGER_DATA: return "ledger_data"; - case protocol::mtPROPOSE_LEDGER: return "propose"; - case protocol::mtSTATUS_CHANGE: return "status"; - case protocol::mtHAVE_SET: return "have_set"; - case protocol::mtVALIDATORLIST: return "validator_list"; - case protocol::mtVALIDATION: return "validation"; - case protocol::mtGET_OBJECTS: return "get_objects"; - default: - break; + case protocol::mtMANIFESTS: + return "manifests"; + case protocol::mtPING: + return "ping"; + case protocol::mtCLUSTER: + return "cluster"; + case protocol::mtGET_SHARD_INFO: + return "get_shard_info"; + case protocol::mtSHARD_INFO: + return "shard_info"; + case protocol::mtGET_PEER_SHARD_INFO: + return "get_peer_shard_info"; + case protocol::mtPEER_SHARD_INFO: + return "peer_shard_info"; + case protocol::mtENDPOINTS: + return "endpoints"; + case protocol::mtTRANSACTION: + return "tx"; + case protocol::mtGET_LEDGER: + return "get_ledger"; + case protocol::mtLEDGER_DATA: + return "ledger_data"; + case protocol::mtPROPOSE_LEDGER: + return "propose"; + case protocol::mtSTATUS_CHANGE: + return "status"; + case protocol::mtHAVE_SET: + return "have_set"; + case protocol::mtVALIDATORLIST: + return "validator_list"; + case protocol::mtVALIDATION: + return "validation"; + case protocol::mtGET_OBJECTS: + return "get_objects"; + default: + break; } return "unknown"; } @@ -89,22 +106,23 @@ struct MessageHeader std::uint16_t message_type = 0; /** Indicates which compression algorithm the payload is compressed with. - * Currenly only lz4 is supported. If None then the message is not compressed. + * Currenly only lz4 is supported. If None then the message is not + * compressed. */ compression::Algorithm algorithm = compression::Algorithm::None; }; -template +template auto -buffersBegin(BufferSequence const &bufs) +buffersBegin(BufferSequence const& bufs) { - return boost::asio::buffers_iterator::begin(bufs); + return boost::asio::buffers_iterator::begin( + bufs); } template -boost::optional parseMessageHeader( - BufferSequence const& bufs, - std::size_t size) +boost::optional +parseMessageHeader(BufferSequence const& bufs, std::size_t size) { using namespace ripple::compression; auto iter = buffersBegin(bufs); @@ -123,7 +141,8 @@ boost::optional parseMessageHeader( if (compressed) { uint8_t algorithm = (*iter & 0x70) >> 4; - if (algorithm != static_cast(compression::Algorithm::LZ4)) + if (algorithm != + static_cast(compression::Algorithm::LZ4)) return {}; hdr.algorithm = compression::Algorithm::LZ4; } @@ -148,13 +167,14 @@ boost::optional parseMessageHeader( return {}; } -template ::value>> +template < + class T, + class Buffers, + class Handler, + class = std::enable_if_t< + std::is_base_of<::google::protobuf::Message, T>::value>> bool -invoke ( - MessageHeader const& header, - Buffers const& buffers, - Handler& handler) +invoke(MessageHeader const& header, Buffers const& buffers, Handler& handler) { auto const m = std::make_shared(); @@ -179,14 +199,14 @@ invoke ( else if (!m->ParseFromZeroCopyStream(&stream)) return false; - handler.onMessageBegin (header.message_type, m, header.payload_wire_size); - handler.onMessage (m); - handler.onMessageEnd (header.message_type, m); + handler.onMessageBegin(header.message_type, m, header.payload_wire_size); + handler.onMessage(m); + handler.onMessageEnd(header.message_type, m); return true; } -} +} // namespace detail /** Calls the handler for up to one protocol message in the passed buffers. @@ -196,10 +216,10 @@ invoke ( @return The number of bytes consumed, or the error code if any. */ template -std::pair -invokeProtocolMessage (Buffers const& buffers, Handler& handler) +std::pair +invokeProtocolMessage(Buffers const& buffers, Handler& handler) { - std::pair result = { 0, {} }; + std::pair result = {0, {}}; auto const size = boost::asio::buffer_size(buffers); @@ -232,61 +252,78 @@ invokeProtocolMessage (Buffers const& buffers, Handler& handler) switch (header->message_type) { - case protocol::mtMANIFESTS: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtPING: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtCLUSTER: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtGET_SHARD_INFO: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtSHARD_INFO: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtGET_PEER_SHARD_INFO: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtPEER_SHARD_INFO: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtENDPOINTS: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtTRANSACTION: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtGET_LEDGER: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtLEDGER_DATA: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtPROPOSE_LEDGER: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtSTATUS_CHANGE: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtHAVE_SET: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtVALIDATION: - success = detail::invoke(*header, buffers, handler); - break; - case protocol::mtVALIDATORLIST: - success = detail::invoke (*header, buffers, handler); - break; - case protocol::mtGET_OBJECTS: - success = detail::invoke(*header, buffers, handler); - break; - default: - handler.onMessageUnknown (header->message_type); - success = true; - break; + case protocol::mtMANIFESTS: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtPING: + success = + detail::invoke(*header, buffers, handler); + break; + case protocol::mtCLUSTER: + success = + detail::invoke(*header, buffers, handler); + break; + case protocol::mtGET_SHARD_INFO: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtSHARD_INFO: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtGET_PEER_SHARD_INFO: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtPEER_SHARD_INFO: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtENDPOINTS: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtTRANSACTION: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtGET_LEDGER: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtLEDGER_DATA: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtPROPOSE_LEDGER: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtSTATUS_CHANGE: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtHAVE_SET: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtVALIDATION: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtVALIDATORLIST: + success = detail::invoke( + *header, buffers, handler); + break; + case protocol::mtGET_OBJECTS: + success = detail::invoke( + *header, buffers, handler); + break; + default: + handler.onMessageUnknown(header->message_type); + success = true; + break; } result.first = header->total_wire_size; @@ -297,6 +334,6 @@ invokeProtocolMessage (Buffers const& buffers, Handler& handler) return result; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/ProtocolVersion.cpp b/src/ripple/overlay/impl/ProtocolVersion.cpp index 156c7ed8e9..150f5fec61 100644 --- a/src/ripple/overlay/impl/ProtocolVersion.cpp +++ b/src/ripple/overlay/impl/ProtocolVersion.cpp @@ -17,9 +17,9 @@ */ //============================================================================== -#include #include #include +#include #include #include #include @@ -32,23 +32,15 @@ namespace ripple { @note The list must be sorted in strictly ascending order (and so it may not contain any duplicates!) */ -constexpr -ProtocolVersion const supportedProtocolList[] -{ - { 1, 2 }, - { 2, 0 }, - { 2, 1 } -}; +constexpr ProtocolVersion const supportedProtocolList[]{{1, 2}, {2, 0}, {2, 1}}; // This ugly construct ensures that supportedProtocolList is sorted in strictly // ascending order and doesn't contain any duplicates. // FIXME: With C++20 we can use std::is_sorted with an appropriate comparator static_assert( - []() constexpr -> bool - { + []() constexpr->bool { auto const len = std::distance( - std::begin(supportedProtocolList), - std::end(supportedProtocolList)); + std::begin(supportedProtocolList), std::end(supportedProtocolList)); // There should be at least one protocol we're willing to speak. if (len == 0) @@ -60,21 +52,21 @@ static_assert( { for (auto i = 0; i != len - 1; ++i) { - if (supportedProtocolList[i] >= supportedProtocolList[i+1]) + if (supportedProtocolList[i] >= supportedProtocolList[i + 1]) return false; } } return true; - }(), "The list of supported protocols isn't properly sorted."); - + }(), + "The list of supported protocols isn't properly sorted."); std::string to_string(ProtocolVersion const& p) { // The legacy protocol uses a different name. This can be removed when we // migrate away from it and require 2.0 or later. - if (p == ProtocolVersion{ 1, 2 }) + if (p == ProtocolVersion{1, 2}) return "RTXP/1.2"; return "XRPL/" + std::to_string(p.first) + "." + std::to_string(p.second); @@ -86,11 +78,14 @@ parseProtocolVersions(boost::beast::string_view const& value) static boost::regex re( "^" // start of line "XRPL/" // The string "XRPL/" - "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading zeroes) + "([2-9]|(?:[1-9][0-9]+))" // a number (greater than 2 with no leading + // zeroes) "\\." // a period - "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly zero) + "(0|(?:[1-9][0-9]*))" // a number (no leading zeroes unless exactly + // zero) "$" // The end of the string - , boost::regex_constants::optimize); + , + boost::regex_constants::optimize); std::vector result; @@ -141,14 +136,13 @@ negotiateProtocolVersion(std::vector const& versions) // to be the last one. So we get a little clever and avoid the need for // a container: std::function pickVersion = - [&result](ProtocolVersion const& v) - { - result = v; - }; + [&result](ProtocolVersion const& v) { result = v; }; std::set_intersection( - std::begin(versions), std::end(versions), - std::begin(supportedProtocolList), std::end(supportedProtocolList), + std::begin(versions), + std::end(versions), + std::begin(supportedProtocolList), + std::end(supportedProtocolList), boost::make_function_output_iterator(pickVersion)); return result; @@ -165,8 +159,7 @@ negotiateProtocolVersion(boost::beast::string_view const& versions) std::string const& supportedProtocolVersions() { - static std::string const supported = []() - { + static std::string const supported = []() { std::string ret; for (auto const& v : supportedProtocolList) { @@ -184,8 +177,11 @@ supportedProtocolVersions() bool isProtocolSupported(ProtocolVersion const& v) { - return std::end(supportedProtocolList) != std::find( - std::begin(supportedProtocolList), std::end(supportedProtocolList), v); + return std::end(supportedProtocolList) != + std::find( + std::begin(supportedProtocolList), + std::end(supportedProtocolList), + v); } -} +} // namespace ripple diff --git a/src/ripple/overlay/impl/ProtocolVersion.h b/src/ripple/overlay/impl/ProtocolVersion.h index 66f19a8bad..d06f607829 100644 --- a/src/ripple/overlay/impl/ProtocolVersion.h +++ b/src/ripple/overlay/impl/ProtocolVersion.h @@ -36,12 +36,10 @@ namespace ripple { * */ using ProtocolVersion = std::pair; -inline -constexpr -ProtocolVersion +inline constexpr ProtocolVersion make_protocol(std::uint16_t major, std::uint16_t minor) { - return { major, minor }; + return {major, minor}; } /** Print a protocol version a human-readable string. */ @@ -79,6 +77,6 @@ supportedProtocolVersions(); bool isProtocolSupported(ProtocolVersion const& v); -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/TrafficCount.cpp b/src/ripple/overlay/impl/TrafficCount.cpp index ca5952b68a..298052771d 100644 --- a/src/ripple/overlay/impl/TrafficCount.cpp +++ b/src/ripple/overlay/impl/TrafficCount.cpp @@ -21,9 +21,11 @@ namespace ripple { -TrafficCount::category TrafficCount::categorize ( +TrafficCount::category +TrafficCount::categorize( ::google::protobuf::Message const& message, - int type, bool inbound) + int type, + bool inbound) { if ((type == protocol::mtPING) || (type == protocol::mtSTATUS_CHANGE)) return TrafficCount::category::base; @@ -38,9 +40,9 @@ TrafficCount::category TrafficCount::categorize ( return TrafficCount::category::overlay; if ((type == protocol::mtGET_SHARD_INFO) || - (type == protocol::mtSHARD_INFO) || - (type == protocol::mtGET_PEER_SHARD_INFO) || - (type == protocol::mtPEER_SHARD_INFO)) + (type == protocol::mtSHARD_INFO) || + (type == protocol::mtGET_PEER_SHARD_INFO) || + (type == protocol::mtPEER_SHARD_INFO)) return TrafficCount::category::shards; if (type == protocol::mtTRANSACTION) @@ -56,92 +58,90 @@ TrafficCount::category TrafficCount::categorize ( return TrafficCount::category::proposal; if (type == protocol::mtHAVE_SET) - return inbound ? - TrafficCount::category::get_set: - TrafficCount::category::share_set; + return inbound ? TrafficCount::category::get_set + : TrafficCount::category::share_set; if (auto msg = dynamic_cast(&message)) { if (msg->type() == protocol::liTS_CANDIDATE) - return (inbound && !msg->has_requestcookie()) ? - TrafficCount::category::ld_tsc_get: - TrafficCount::category::ld_tsc_share; + return (inbound && !msg->has_requestcookie()) + ? TrafficCount::category::ld_tsc_get + : TrafficCount::category::ld_tsc_share; if (msg->type() == protocol::liTX_NODE) - return (inbound && !msg->has_requestcookie()) ? - TrafficCount::category::ld_txn_get : - TrafficCount::category::ld_txn_share; + return (inbound && !msg->has_requestcookie()) + ? TrafficCount::category::ld_txn_get + : TrafficCount::category::ld_txn_share; if (msg->type() == protocol::liAS_NODE) - return (inbound && !msg->has_requestcookie()) ? - TrafficCount::category::ld_asn_get : - TrafficCount::category::ld_asn_share; + return (inbound && !msg->has_requestcookie()) + ? TrafficCount::category::ld_asn_get + : TrafficCount::category::ld_asn_share; - return (inbound && !msg->has_requestcookie()) ? - TrafficCount::category::ld_get : - TrafficCount::category::ld_share; + return (inbound && !msg->has_requestcookie()) + ? TrafficCount::category::ld_get + : TrafficCount::category::ld_share; } if (auto msg = dynamic_cast(&message)) { if (msg->itype() == protocol::liTS_CANDIDATE) - return (inbound || msg->has_requestcookie()) ? - TrafficCount::category::gl_tsc_share : - TrafficCount::category::gl_tsc_get; + return (inbound || msg->has_requestcookie()) + ? TrafficCount::category::gl_tsc_share + : TrafficCount::category::gl_tsc_get; if (msg->itype() == protocol::liTX_NODE) - return (inbound || msg->has_requestcookie()) ? - TrafficCount::category::gl_txn_share : - TrafficCount::category::gl_txn_get; + return (inbound || msg->has_requestcookie()) + ? TrafficCount::category::gl_txn_share + : TrafficCount::category::gl_txn_get; if (msg->itype() == protocol::liAS_NODE) - return (inbound || msg->has_requestcookie()) ? - TrafficCount::category::gl_asn_share : - TrafficCount::category::gl_asn_get; + return (inbound || msg->has_requestcookie()) + ? TrafficCount::category::gl_asn_share + : TrafficCount::category::gl_asn_get; - return (inbound || msg->has_requestcookie()) ? - TrafficCount::category::gl_share : - TrafficCount::category::gl_get; + return (inbound || msg->has_requestcookie()) + ? TrafficCount::category::gl_share + : TrafficCount::category::gl_get; } if (auto msg = dynamic_cast(&message)) { if (msg->type() == protocol::TMGetObjectByHash::otLEDGER) - return (msg->query() == inbound) ? - TrafficCount::category::share_hash_ledger : - TrafficCount::category::get_hash_ledger; + return (msg->query() == inbound) + ? TrafficCount::category::share_hash_ledger + : TrafficCount::category::get_hash_ledger; if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION) - return (msg->query() == inbound) ? - TrafficCount::category::share_hash_tx : - TrafficCount::category::get_hash_tx; + return (msg->query() == inbound) + ? TrafficCount::category::share_hash_tx + : TrafficCount::category::get_hash_tx; if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION_NODE) - return (msg->query() == inbound) ? - TrafficCount::category::share_hash_txnode : - TrafficCount::category::get_hash_txnode; + return (msg->query() == inbound) + ? TrafficCount::category::share_hash_txnode + : TrafficCount::category::get_hash_txnode; if (msg->type() == protocol::TMGetObjectByHash::otSTATE_NODE) - return (msg->query() == inbound) ? - TrafficCount::category::share_hash_asnode : - TrafficCount::category::get_hash_asnode; + return (msg->query() == inbound) + ? TrafficCount::category::share_hash_asnode + : TrafficCount::category::get_hash_asnode; if (msg->type() == protocol::TMGetObjectByHash::otCAS_OBJECT) - return (msg->query() == inbound) ? - TrafficCount::category::share_cas_object : - TrafficCount::category::get_cas_object; + return (msg->query() == inbound) + ? TrafficCount::category::share_cas_object + : TrafficCount::category::get_cas_object; if (msg->type() == protocol::TMGetObjectByHash::otFETCH_PACK) - return (msg->query() == inbound) ? - TrafficCount::category::share_fetch_pack : - TrafficCount::category::get_fetch_pack; + return (msg->query() == inbound) + ? TrafficCount::category::share_fetch_pack + : TrafficCount::category::get_fetch_pack; - return (msg->query() == inbound) ? - TrafficCount::category::share_hash : - TrafficCount::category::get_hash; + return (msg->query() == inbound) ? TrafficCount::category::share_hash + : TrafficCount::category::get_hash; } return TrafficCount::category::unknown; } -} // ripple +} // namespace ripple diff --git a/src/ripple/overlay/impl/TrafficCount.h b/src/ripple/overlay/impl/TrafficCount.h index 602e885061..2dba07e864 100644 --- a/src/ripple/overlay/impl/TrafficCount.h +++ b/src/ripple/overlay/impl/TrafficCount.h @@ -37,26 +37,25 @@ public: public: char const* name; - std::atomic bytesIn {0}; - std::atomic bytesOut {0}; - std::atomic messagesIn {0}; - std::atomic messagesOut {0}; + std::atomic bytesIn{0}; + std::atomic bytesOut{0}; + std::atomic messagesIn{0}; + std::atomic messagesOut{0}; - TrafficStats(char const* n) - : name (n) + TrafficStats(char const* n) : name(n) { } TrafficStats(TrafficStats const& ts) - : name (ts.name) - , bytesIn (ts.bytesIn.load()) - , bytesOut (ts.bytesOut.load()) - , messagesIn (ts.messagesIn.load()) - , messagesOut (ts.messagesOut.load()) + : name(ts.name) + , bytesIn(ts.bytesIn.load()) + , bytesOut(ts.bytesOut.load()) + , messagesIn(ts.messagesIn.load()) + , messagesOut(ts.messagesOut.load()) { } - operator bool () const + operator bool() const { return messagesIn || messagesOut; } @@ -65,22 +64,21 @@ public: // If you add entries to this enum, you need to update the initialization // of the arrays at the bottom of this file which map array numbers to // human-readable, monitoring-tool friendly names. - enum category : std::size_t - { - base, // basic peer overhead, must be first + enum category : std::size_t { + base, // basic peer overhead, must be first - cluster, // cluster overhead - overlay, // overlay management - manifests, // manifest management + cluster, // cluster overhead + overlay, // overlay management + manifests, // manifest management transaction, proposal, validation, validatorlist, - shards, // shard-related traffic + shards, // shard-related traffic // TMHaveSet message: - get_set, // transaction sets we try to get - share_set, // transaction sets we get + get_set, // transaction sets we try to get + share_set, // transaction sets we get // TMLedgerData: transaction set candidate ld_tsc_get, @@ -142,18 +140,22 @@ public: share_hash, get_hash, - unknown // must be last + unknown // must be last }; - /** Given a protocol message, determine which traffic category it belongs to */ - static category categorize ( + /** Given a protocol message, determine which traffic category it belongs to + */ + static category + categorize( ::google::protobuf::Message const& message, - int type, bool inbound); + int type, + bool inbound); /** Account for traffic associated with the given category */ - void addCount (category cat, bool inbound, int bytes) + void + addCount(category cat, bool inbound, int bytes) { - assert (cat <= category::unknown); + assert(cat <= category::unknown); if (inbound) { @@ -174,59 +176,57 @@ public: @return an object which satisfies the requirements of Container */ auto const& - getCounts () const + getCounts() const { return counts_; } - protected: - std::array counts_ - {{ - {"overhead"}, // category::base - {"overhead_cluster"}, // category::cluster - {"overhead_overlay"}, // category::overlay - {"overhead_manifest"}, // category::manifests - {"transactions"}, // category::transaction - {"proposals"}, // category::proposal - {"validations"}, // category::validation - {"validator_lists"}, // category::validatorlist - {"shards"}, // category::shards - {"set_get"}, // category::get_set - {"set_share"}, // category::share_set - {"ledger_data_Transaction_Set_candidate_get"}, // category::ld_tsc_get - {"ledger_data_Transaction_Set_candidate_share"}, // category::ld_tsc_share - {"ledger_data_Transaction_Node_get"}, // category::ld_txn_get - {"ledger_data_Transaction_Node_share"}, // category::ld_txn_share - {"ledger_data_Account_State_Node_get"}, // category::ld_asn_get - {"ledger_data_Account_State_Node_share"}, // category::ld_asn_share - {"ledger_data_get"}, // category::ld_get - {"ledger_data_share"}, // category::ld_share - {"ledger_Transaction_Set_candidate_share"}, // category::gl_tsc_share - {"ledger_Transaction_Set_candidate_get"}, // category::gl_tsc_get - {"ledger_Transaction_node_share"}, // category::gl_txn_share - {"ledger_Transaction_node_get"}, // category::gl_txn_get - {"ledger_Account_State_node_share"}, // category::gl_asn_share - {"ledger_Account_State_node_get"}, // category::gl_asn_get - {"ledger_share"}, // category::gl_share - {"ledger_get"}, // category::gl_get - {"getobject_Ledger_share"}, // category::share_hash_ledger - {"getobject_Ledger_get"}, // category::get_hash_ledger - {"getobject_Transaction_share"}, // category::share_hash_tx - {"getobject_Transaction_get"}, // category::get_hash_tx - {"getobject_Transaction_node_share"}, // category::share_hash_txnode - {"getobject_Transaction_node_get"}, // category::get_hash_txnode - {"getobject_Account_State_node_share"}, // category::share_hash_asnode - {"getobject_Account_State_node_get"}, // category::get_hash_asnode - {"getobject_CAS_share"}, // category::share_cas_object - {"getobject_CAS_get"}, // category::get_cas_object - {"getobject_Fetch_Pack_share"}, // category::share_fetch_pack - {"getobject_Fetch Pack_get"}, // category::get_fetch_pack - {"getobject_share"}, // category::share_hash - {"getobject_get"}, // category::get_hash - {"unknown"} // category::unknown + std::array counts_{{ + {"overhead"}, // category::base + {"overhead_cluster"}, // category::cluster + {"overhead_overlay"}, // category::overlay + {"overhead_manifest"}, // category::manifests + {"transactions"}, // category::transaction + {"proposals"}, // category::proposal + {"validations"}, // category::validation + {"validator_lists"}, // category::validatorlist + {"shards"}, // category::shards + {"set_get"}, // category::get_set + {"set_share"}, // category::share_set + {"ledger_data_Transaction_Set_candidate_get"}, // category::ld_tsc_get + {"ledger_data_Transaction_Set_candidate_share"}, // category::ld_tsc_share + {"ledger_data_Transaction_Node_get"}, // category::ld_txn_get + {"ledger_data_Transaction_Node_share"}, // category::ld_txn_share + {"ledger_data_Account_State_Node_get"}, // category::ld_asn_get + {"ledger_data_Account_State_Node_share"}, // category::ld_asn_share + {"ledger_data_get"}, // category::ld_get + {"ledger_data_share"}, // category::ld_share + {"ledger_Transaction_Set_candidate_share"}, // category::gl_tsc_share + {"ledger_Transaction_Set_candidate_get"}, // category::gl_tsc_get + {"ledger_Transaction_node_share"}, // category::gl_txn_share + {"ledger_Transaction_node_get"}, // category::gl_txn_get + {"ledger_Account_State_node_share"}, // category::gl_asn_share + {"ledger_Account_State_node_get"}, // category::gl_asn_get + {"ledger_share"}, // category::gl_share + {"ledger_get"}, // category::gl_get + {"getobject_Ledger_share"}, // category::share_hash_ledger + {"getobject_Ledger_get"}, // category::get_hash_ledger + {"getobject_Transaction_share"}, // category::share_hash_tx + {"getobject_Transaction_get"}, // category::get_hash_tx + {"getobject_Transaction_node_share"}, // category::share_hash_txnode + {"getobject_Transaction_node_get"}, // category::get_hash_txnode + {"getobject_Account_State_node_share"}, // category::share_hash_asnode + {"getobject_Account_State_node_get"}, // category::get_hash_asnode + {"getobject_CAS_share"}, // category::share_cas_object + {"getobject_CAS_get"}, // category::get_cas_object + {"getobject_Fetch_Pack_share"}, // category::share_fetch_pack + {"getobject_Fetch Pack_get"}, // category::get_fetch_pack + {"getobject_share"}, // category::share_hash + {"getobject_get"}, // category::get_hash + {"unknown"} // category::unknown }}; }; -} +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/Tuning.h b/src/ripple/overlay/impl/Tuning.h index b6d8309325..0920c77e3a 100644 --- a/src/ripple/overlay/impl/Tuning.h +++ b/src/ripple/overlay/impl/Tuning.h @@ -24,61 +24,60 @@ namespace ripple { -namespace Tuning -{ +namespace Tuning { -enum -{ +enum { /** Size of buffer used to read from the socket. */ - readBufferBytes = 4096, + readBufferBytes = 4096, /** How long a server can remain insane before we disconnected it (if outbound) */ - maxInsaneTime = 60, + maxInsaneTime = 60, /** How long a server can remain unknown before we disconnect it (if outbound) */ - maxUnknownTime = 300, + maxUnknownTime = 300, /** How many ledgers off a server can be and we will still consider it sane */ - saneLedgerLimit = 24, + saneLedgerLimit = 24, /** How many ledgers off a server has to be before we consider it insane */ - insaneLedgerLimit = 128, + insaneLedgerLimit = 128, /** The maximum number of ledger entries in a single reply */ - maxReplyNodes = 8192, + maxReplyNodes = 8192, /** How often we check connections (seconds) */ - checkSeconds = 32, + checkSeconds = 32, /** How often we latency/sendq probe connections */ - timerSeconds = 8, + timerSeconds = 8, - /** How many timer intervals a sendq has to stay large before we disconnect */ - sendqIntervals = 4, + /** How many timer intervals a sendq has to stay large before we disconnect + */ + sendqIntervals = 4, /** How many timer intervals we can go without a ping reply */ - noPing = 10, + noPing = 10, /** How many messages on a send queue before we refuse queries */ - dropSendQueue = 192, + dropSendQueue = 192, /** How many messages we consider reasonable sustained on a send queue */ - targetSendQueue = 128, + targetSendQueue = 128, /** How often to log send queue size */ - sendQueueLogFreq = 64, + sendQueueLogFreq = 64, }; /** The threshold above which we treat a peer connection as high latency */ std::chrono::milliseconds constexpr peerHighLatency{300}; -} // Tuning +} // namespace Tuning -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/impl/ZeroCopyStream.h b/src/ripple/overlay/impl/ZeroCopyStream.h index a228c1aaf5..da0f0d380d 100644 --- a/src/ripple/overlay/impl/ZeroCopyStream.h +++ b/src/ripple/overlay/impl/ZeroCopyStream.h @@ -20,19 +20,19 @@ #ifndef RIPPLE_OVERLAY_ZEROCOPYSTREAM_H_INCLUDED #define RIPPLE_OVERLAY_ZEROCOPYSTREAM_H_INCLUDED -#include #include #include +#include namespace ripple { /** Implements ZeroCopyInputStream around a buffer sequence. @tparam Buffers A type meeting the requirements of ConstBufferSequence. - @see https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream + @see + https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream */ template -class ZeroCopyInputStream - : public ::google::protobuf::io::ZeroCopyInputStream +class ZeroCopyInputStream : public ::google::protobuf::io::ZeroCopyInputStream { private: using iterator = typename Buffers::const_iterator; @@ -44,17 +44,16 @@ private: const_buffer pos_; // What Next() will return public: - explicit - ZeroCopyInputStream (Buffers const& buffers); + explicit ZeroCopyInputStream(Buffers const& buffers); bool - Next (const void** data, int* size) override; + Next(const void** data, int* size) override; void - BackUp (int count) override; + BackUp(int count) override; bool - Skip (int count) override; + Skip(int count) override; google::protobuf::int64 ByteCount() const override @@ -66,50 +65,44 @@ public: //------------------------------------------------------------------------------ template -ZeroCopyInputStream::ZeroCopyInputStream ( - Buffers const& buffers) - : last_ (buffers.end()) - , first_ (buffers.begin()) - , pos_ ((first_ != last_) ? - *first_ : const_buffer(nullptr, 0)) +ZeroCopyInputStream::ZeroCopyInputStream(Buffers const& buffers) + : last_(buffers.end()) + , first_(buffers.begin()) + , pos_((first_ != last_) ? *first_ : const_buffer(nullptr, 0)) { } template bool -ZeroCopyInputStream::Next ( - const void** data, int* size) +ZeroCopyInputStream::Next(const void** data, int* size) { *data = boost::asio::buffer_cast(pos_); *size = boost::asio::buffer_size(pos_); if (first_ == last_) return false; count_ += *size; - pos_ = (++first_ != last_) ? *first_ : - const_buffer(nullptr, 0); + pos_ = (++first_ != last_) ? *first_ : const_buffer(nullptr, 0); return true; } template void -ZeroCopyInputStream::BackUp (int count) +ZeroCopyInputStream::BackUp(int count) { --first_; - pos_ = *first_ + - (boost::asio::buffer_size(*first_) - count); + pos_ = *first_ + (boost::asio::buffer_size(*first_) - count); count_ -= count; } template bool -ZeroCopyInputStream::Skip (int count) +ZeroCopyInputStream::Skip(int count) { if (first_ == last_) return false; while (count > 0) { - auto const size = - boost::asio::buffer_size(pos_); + auto const size = boost::asio::buffer_size(pos_); if (count < size) { pos_ = pos_ + count; @@ -132,8 +125,7 @@ ZeroCopyInputStream::Skip (int count) @tparam Streambuf A type meeting the requirements of Streambuf. */ template -class ZeroCopyOutputStream - : public ::google::protobuf::io::ZeroCopyOutputStream +class ZeroCopyOutputStream : public ::google::protobuf::io::ZeroCopyOutputStream { private: using buffers_type = typename Streambuf::mutable_buffers_type; @@ -148,17 +140,15 @@ private: iterator pos_; public: - explicit - ZeroCopyOutputStream (Streambuf& streambuf, - std::size_t blockSize); + explicit ZeroCopyOutputStream(Streambuf& streambuf, std::size_t blockSize); ~ZeroCopyOutputStream(); bool - Next (void** data, int* size) override; + Next(void** data, int* size) override; void - BackUp (int count) override; + BackUp(int count) override; google::protobuf::int64 ByteCount() const override @@ -171,11 +161,12 @@ public: template ZeroCopyOutputStream::ZeroCopyOutputStream( - Streambuf& streambuf, std::size_t blockSize) - : streambuf_ (streambuf) - , blockSize_ (blockSize) - , buffers_ (streambuf_.prepare(blockSize_)) - , pos_ (buffers_.begin()) + Streambuf& streambuf, + std::size_t blockSize) + : streambuf_(streambuf) + , blockSize_(blockSize) + , buffers_(streambuf_.prepare(blockSize_)) + , pos_(buffers_.begin()) { } @@ -188,8 +179,7 @@ ZeroCopyOutputStream::~ZeroCopyOutputStream() template bool -ZeroCopyOutputStream::Next( - void** data, int* size) +ZeroCopyOutputStream::Next(void** data, int* size) { if (commit_ != 0) { @@ -199,7 +189,7 @@ ZeroCopyOutputStream::Next( if (pos_ == buffers_.end()) { - buffers_ = streambuf_.prepare (blockSize_); + buffers_ = streambuf_.prepare(blockSize_); pos_ = buffers_.begin(); } @@ -212,7 +202,7 @@ ZeroCopyOutputStream::Next( template void -ZeroCopyOutputStream::BackUp (int count) +ZeroCopyOutputStream::BackUp(int count) { assert(count <= commit_); auto const n = commit_ - count; @@ -221,6 +211,6 @@ ZeroCopyOutputStream::BackUp (int count) commit_ = 0; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/make_Overlay.h b/src/ripple/overlay/make_Overlay.h index 0a0e942858..5a6b647b7a 100644 --- a/src/ripple/overlay/make_Overlay.h +++ b/src/ripple/overlay/make_Overlay.h @@ -20,22 +20,22 @@ #ifndef RIPPLE_OVERLAY_MAKE_OVERLAY_H_INCLUDED #define RIPPLE_OVERLAY_MAKE_OVERLAY_H_INCLUDED -#include -#include -#include #include #include +#include +#include +#include #include #include namespace ripple { Overlay::Setup -setup_Overlay (BasicConfig const& config); +setup_Overlay(BasicConfig const& config); /** Creates the implementation of Overlay. */ -std::unique_ptr -make_Overlay ( +std::unique_ptr +make_Overlay( Application& app, Overlay::Setup const& setup, Stoppable& parent, @@ -46,6 +46,6 @@ make_Overlay ( BasicConfig const& config, beast::insight::Collector::ptr const& collector); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/overlay/predicates.h b/src/ripple/overlay/predicates.h index ee88b6a35b..8e403b81fa 100644 --- a/src/ripple/overlay/predicates.h +++ b/src/ripple/overlay/predicates.h @@ -34,13 +34,14 @@ struct send_always std::shared_ptr const& msg; - send_always(std::shared_ptr const& m) - : msg(m) - { } - - void operator()(std::shared_ptr const& peer) const + send_always(std::shared_ptr const& m) : msg(m) { - peer->send (msg); + } + + void + operator()(std::shared_ptr const& peer) const + { + peer->send(msg); } }; @@ -56,21 +57,22 @@ struct send_if_pred Predicate const& predicate; send_if_pred(std::shared_ptr const& m, Predicate const& p) - : msg(m), predicate(p) - { } - - void operator()(std::shared_ptr const& peer) const + : msg(m), predicate(p) { - if (predicate (peer)) - peer->send (msg); + } + + void + operator()(std::shared_ptr const& peer) const + { + if (predicate(peer)) + peer->send(msg); } }; /** Helper function to aid in type deduction */ template -send_if_pred send_if ( - std::shared_ptr const& m, - Predicate const &f) +send_if_pred +send_if(std::shared_ptr const& m, Predicate const& f) { return send_if_pred(m, f); } @@ -88,20 +90,21 @@ struct send_if_not_pred send_if_not_pred(std::shared_ptr const& m, Predicate const& p) : msg(m), predicate(p) - { } - - void operator()(std::shared_ptr const& peer) const { - if (!predicate (peer)) - peer->send (msg); + } + + void + operator()(std::shared_ptr const& peer) const + { + if (!predicate(peer)) + peer->send(msg); } }; /** Helper function to aid in type deduction */ template -send_if_not_pred send_if_not ( - std::shared_ptr const& m, - Predicate const &f) +send_if_not_pred +send_if_not(std::shared_ptr const& m, Predicate const& f) { return send_if_not_pred(m, f); } @@ -113,13 +116,14 @@ struct match_peer { Peer const* matchPeer; - match_peer (Peer const* match = nullptr) - : matchPeer (match) - { } - - bool operator() (std::shared_ptr const& peer) const + match_peer(Peer const* match = nullptr) : matchPeer(match) { - if(matchPeer && (peer.get () == matchPeer)) + } + + bool + operator()(std::shared_ptr const& peer) const + { + if (matchPeer && (peer.get() == matchPeer)) return true; return false; @@ -133,16 +137,17 @@ struct peer_in_cluster { match_peer skipPeer; - peer_in_cluster (Peer const* skip = nullptr) - : skipPeer (skip) - { } - - bool operator() (std::shared_ptr const& peer) const + peer_in_cluster(Peer const* skip = nullptr) : skipPeer(skip) { - if (skipPeer (peer)) + } + + bool + operator()(std::shared_ptr const& peer) const + { + if (skipPeer(peer)) return false; - if (! peer->cluster()) + if (!peer->cluster()) return false; return true; @@ -154,21 +159,22 @@ struct peer_in_cluster /** Select all peers that are in the specified set */ struct peer_in_set { - std::set const& peerSet; + std::set const& peerSet; - peer_in_set (std::set const& peers) - : peerSet (peers) - { } - - bool operator() (std::shared_ptr const& peer) const + peer_in_set(std::set const& peers) : peerSet(peers) { - if (peerSet.count (peer->id ()) == 0) + } + + bool + operator()(std::shared_ptr const& peer) const + { + if (peerSet.count(peer->id()) == 0) return false; return true; } }; -} +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/PeerfinderManager.h b/src/ripple/peerfinder/PeerfinderManager.h index 4d651f7247..9c175cc976 100644 --- a/src/ripple/peerfinder/PeerfinderManager.h +++ b/src/ripple/peerfinder/PeerfinderManager.h @@ -20,19 +20,19 @@ #ifndef RIPPLE_PEERFINDER_MANAGER_H_INCLUDED #define RIPPLE_PEERFINDER_MANAGER_H_INCLUDED -#include #include -#include #include +#include +#include #include namespace ripple { namespace PeerFinder { -using clock_type = beast::abstract_clock ; +using clock_type = beast::abstract_clock; /** Represents a set of addresses. */ -using IPAddresses = std::vector ; +using IPAddresses = std::vector; //------------------------------------------------------------------------------ @@ -78,16 +78,19 @@ struct Config //-------------------------------------------------------------------------- /** Create a configuration with default values. */ - Config (); + Config(); /** Returns a suitable value for outPeers according to the rules. */ - double calcOutPeers () const; + double + calcOutPeers() const; /** Adjusts the values so they follow the business rules. */ - void applyTuning (); + void + applyTuning(); /** Write the configuration into a property stream */ - void onWrite (beast::PropertyStream::Map& map); + void + onWrite(beast::PropertyStream::Map& map); }; //------------------------------------------------------------------------------ @@ -95,36 +98,30 @@ struct Config /** Describes a connectible peer address along with some metadata. */ struct Endpoint { - Endpoint (); + Endpoint(); - Endpoint (beast::IP::Endpoint const& ep, int hops_); + Endpoint(beast::IP::Endpoint const& ep, int hops_); int hops; beast::IP::Endpoint address; }; -bool operator< (Endpoint const& lhs, Endpoint const& rhs); +bool +operator<(Endpoint const& lhs, Endpoint const& rhs); /** A set of Endpoint used for connecting. */ -using Endpoints = std::vector ; +using Endpoints = std::vector; //------------------------------------------------------------------------------ /** Possible results from activating a slot. */ -enum class Result -{ - duplicate, - full, - success -}; +enum class Result { duplicate, full, success }; /** Maintains a set of IP addresses used for getting into the network. */ -class Manager - : public Stoppable - , public beast::PropertyStream::Source +class Manager : public Stoppable, public beast::PropertyStream::Source { protected: - explicit Manager (Stoppable& parent); + explicit Manager(Stoppable& parent); public: /** Destroy the object. @@ -139,11 +136,11 @@ public: Thread safety: Can be called from any threads at any time. */ - virtual void setConfig (Config const& config) = 0; + virtual void + setConfig(Config const& config) = 0; /** Returns the configuration for the manager. */ - virtual - Config + virtual Config config() = 0; /** Add a peer that should always be connected. @@ -151,14 +148,18 @@ public: The string is the name as specified in the configuration file, along with the set of corresponding IP addresses. */ - virtual void addFixedPeer (std::string const& name, - std::vector const& addresses) = 0; + virtual void + addFixedPeer( + std::string const& name, + std::vector const& addresses) = 0; /** Add a set of strings as fallback IP::Endpoint sources. @param name A label used for diagnostics. */ - virtual void addFallbackStrings (std::string const& name, - std::vector const& strings) = 0; + virtual void + addFallbackStrings( + std::string const& name, + std::vector const& strings) = 0; /** Add a URL as a fallback location to obtain IP::Endpoint sources. @param name A label used for diagnostics. @@ -174,34 +175,39 @@ public: If nullptr is returned, then the slot could not be assigned. Usually this is because of a detected self-connection. */ - virtual std::shared_ptr new_inbound_slot ( + virtual std::shared_ptr + new_inbound_slot( beast::IP::Endpoint const& local_endpoint, - beast::IP::Endpoint const& remote_endpoint) = 0; + beast::IP::Endpoint const& remote_endpoint) = 0; /** Create a new outbound slot with the specified remote endpoint. If nullptr is returned, then the slot could not be assigned. Usually this is because of a duplicate connection. */ - virtual std::shared_ptr new_outbound_slot ( - beast::IP::Endpoint const& remote_endpoint) = 0; + virtual std::shared_ptr + new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) = 0; /** Called when mtENDPOINTS is received. */ - virtual void on_endpoints (std::shared_ptr const& slot, + virtual void + on_endpoints( + std::shared_ptr const& slot, Endpoints const& endpoints) = 0; /** Called when the slot is closed. This always happens when the socket is closed, unless the socket was canceled. */ - virtual void on_closed (std::shared_ptr const& slot) = 0; + virtual void + on_closed(std::shared_ptr const& slot) = 0; /** Called when an outbound connection is deemed to have failed */ - virtual void on_failure (std::shared_ptr const& slot) = 0; + virtual void + on_failure(std::shared_ptr const& slot) = 0; /** Called when we received redirect IPs from a busy peer. */ - virtual - void - onRedirects (boost::asio::ip::tcp::endpoint const& remote_address, + virtual void + onRedirects( + boost::asio::ip::tcp::endpoint const& remote_address, std::vector const& eps) = 0; //-------------------------------------------------------------------------- @@ -213,40 +219,37 @@ public: instead of on_connected. @return `true` if the connection should be kept */ - virtual - bool - onConnected (std::shared_ptr const& slot, + virtual bool + onConnected( + std::shared_ptr const& slot, beast::IP::Endpoint const& local_endpoint) = 0; /** Request an active slot type. */ - virtual - Result - activate (std::shared_ptr const& slot, - PublicKey const& key, bool reserved) = 0; + virtual Result + activate( + std::shared_ptr const& slot, + PublicKey const& key, + bool reserved) = 0; /** Returns a set of endpoints suitable for redirection. */ - virtual - std::vector - redirect (std::shared_ptr const& slot) = 0; + virtual std::vector + redirect(std::shared_ptr const& slot) = 0; /** Return a set of addresses we should connect to. */ - virtual - std::vector + virtual std::vector autoconnect() = 0; - virtual - std::vector, std::vector>> + virtual std::vector, std::vector>> buildEndpointsForPeers() = 0; /** Perform periodic activity. This should be called once per second. */ - virtual - void + virtual void once_per_second() = 0; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/Slot.h b/src/ripple/peerfinder/Slot.h index 6bec4aec85..dd1b7467fd 100644 --- a/src/ripple/peerfinder/Slot.h +++ b/src/ripple/peerfinder/Slot.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_PEERFINDER_SLOT_H_INCLUDED #define RIPPLE_PEERFINDER_SLOT_H_INCLUDED -#include #include +#include #include #include @@ -32,52 +32,53 @@ namespace PeerFinder { class Slot { public: - using ptr = std::shared_ptr ; + using ptr = std::shared_ptr; - enum State - { - accept, - connect, - connected, - active, - closing - }; + enum State { accept, connect, connected, active, closing }; - virtual ~Slot () = 0; + virtual ~Slot() = 0; /** Returns `true` if this is an inbound connection. */ - virtual bool inbound () const = 0; + virtual bool + inbound() const = 0; /** Returns `true` if this is a fixed connection. A connection is fixed if its remote endpoint is in the list of remote endpoints for fixed connections. */ - virtual bool fixed () const = 0; + virtual bool + fixed() const = 0; /** Returns `true` if this is a reserved connection. It might be a cluster peer, or a peer with a reservation. This is only known after then handshake completes. */ - virtual bool reserved () const = 0; + virtual bool + reserved() const = 0; /** Returns the state of the connection. */ - virtual State state () const = 0; + virtual State + state() const = 0; /** The remote endpoint of socket. */ - virtual beast::IP::Endpoint const& remote_endpoint () const = 0; + virtual beast::IP::Endpoint const& + remote_endpoint() const = 0; /** The local endpoint of the socket, when known. */ - virtual boost::optional const& local_endpoint () const = 0; + virtual boost::optional const& + local_endpoint() const = 0; - virtual boost::optional listening_port () const = 0; + virtual boost::optional + listening_port() const = 0; /** The peer's public key, when known. The public key is established when the handshake is complete. */ - virtual boost::optional const& public_key () const = 0; + virtual boost::optional const& + public_key() const = 0; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Bootcache.cpp b/src/ripple/peerfinder/impl/Bootcache.cpp index 88bae51dca..a07c53417d 100644 --- a/src/ripple/peerfinder/impl/Bootcache.cpp +++ b/src/ripple/peerfinder/impl/Bootcache.cpp @@ -17,27 +17,24 @@ */ //============================================================================== -#include -#include -#include #include +#include +#include +#include namespace ripple { namespace PeerFinder { -Bootcache::Bootcache ( - Store& store, - clock_type& clock, - beast::Journal journal) - : m_store (store) - , m_clock (clock) - , m_journal (journal) - , m_whenUpdate (m_clock.now ()) - , m_needsUpdate (false) +Bootcache::Bootcache(Store& store, clock_type& clock, beast::Journal journal) + : m_store(store) + , m_clock(clock) + , m_journal(journal) + , m_whenUpdate(m_clock.now()) + , m_needsUpdate(false) { } -Bootcache::~Bootcache () +Bootcache::~Bootcache() { update(); } @@ -57,25 +54,25 @@ Bootcache::size() const Bootcache::const_iterator Bootcache::begin() const { - return const_iterator (m_map.right.begin()); + return const_iterator(m_map.right.begin()); } Bootcache::const_iterator Bootcache::cbegin() const { - return const_iterator (m_map.right.begin()); + return const_iterator(m_map.right.begin()); } Bootcache::const_iterator Bootcache::end() const { - return const_iterator (m_map.right.end()); + return const_iterator(m_map.right.end()); } Bootcache::const_iterator Bootcache::cend() const { - return const_iterator (m_map.right.end()); + return const_iterator(m_map.right.end()); } void @@ -88,129 +85,118 @@ Bootcache::clear() //-------------------------------------------------------------------------- void -Bootcache::load () +Bootcache::load() { clear(); - auto const n (m_store.load ( - [this](beast::IP::Endpoint const& endpoint, int valence) - { - auto const result (this->m_map.insert ( - value_type (endpoint, valence))); - if (! result.second) + auto const n( + m_store.load([this](beast::IP::Endpoint const& endpoint, int valence) { + auto const result( + this->m_map.insert(value_type(endpoint, valence))); + if (!result.second) { JLOG(this->m_journal.error()) - << beast::leftw (18) << - "Bootcache discard " << endpoint; + << beast::leftw(18) << "Bootcache discard " << endpoint; } })); if (n > 0) { - JLOG(m_journal.info()) << beast::leftw (18) << - "Bootcache loaded " << n << - ((n > 1) ? " addresses" : " address"); - prune (); + JLOG(m_journal.info()) << beast::leftw(18) << "Bootcache loaded " << n + << ((n > 1) ? " addresses" : " address"); + prune(); } } bool -Bootcache::insert (beast::IP::Endpoint const& endpoint) +Bootcache::insert(beast::IP::Endpoint const& endpoint) { - auto const result (m_map.insert ( - value_type (endpoint, 0))); + auto const result(m_map.insert(value_type(endpoint, 0))); if (result.second) { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Bootcache insert " << endpoint; - prune (); + JLOG(m_journal.trace()) + << beast::leftw(18) << "Bootcache insert " << endpoint; + prune(); flagForUpdate(); } return result.second; } bool -Bootcache::insertStatic (beast::IP::Endpoint const& endpoint) +Bootcache::insertStatic(beast::IP::Endpoint const& endpoint) { - auto result (m_map.insert ( - value_type (endpoint, staticValence))); + auto result(m_map.insert(value_type(endpoint, staticValence))); - if (! result.second && (result.first->right.valence() < staticValence)) + if (!result.second && (result.first->right.valence() < staticValence)) { // An existing entry has too low a valence, replace it - m_map.erase (result.first); - result = m_map.insert ( - value_type (endpoint, staticValence)); + m_map.erase(result.first); + result = m_map.insert(value_type(endpoint, staticValence)); } if (result.second) { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Bootcache insert " << endpoint; - prune (); + JLOG(m_journal.trace()) + << beast::leftw(18) << "Bootcache insert " << endpoint; + prune(); flagForUpdate(); } return result.second; } void -Bootcache::on_success (beast::IP::Endpoint const& endpoint) +Bootcache::on_success(beast::IP::Endpoint const& endpoint) { - auto result (m_map.insert ( - value_type (endpoint, 1))); - if (result.second) - { - prune (); - } - else - { - Entry entry (result.first->right); - if (entry.valence() < 0) - entry.valence() = 0; - ++entry.valence(); - m_map.erase (result.first); - result = m_map.insert ( - value_type (endpoint, entry)); - assert (result.second); - } - Entry const& entry (result.first->right); - JLOG(m_journal.info()) << beast::leftw (18) << - "Bootcache connect " << endpoint << - " with " << entry.valence() << - ((entry.valence() > 1) ? " successes" : " success"); - flagForUpdate(); -} - -void -Bootcache::on_failure (beast::IP::Endpoint const& endpoint) -{ - auto result (m_map.insert ( - value_type (endpoint, -1))); + auto result(m_map.insert(value_type(endpoint, 1))); if (result.second) { prune(); } else { - Entry entry (result.first->right); - if (entry.valence() > 0) + Entry entry(result.first->right); + if (entry.valence() < 0) entry.valence() = 0; - --entry.valence(); - m_map.erase (result.first); - result = m_map.insert ( - value_type (endpoint, entry)); - assert (result.second); + ++entry.valence(); + m_map.erase(result.first); + result = m_map.insert(value_type(endpoint, entry)); + assert(result.second); } - Entry const& entry (result.first->right); - auto const n (std::abs (entry.valence())); - JLOG(m_journal.debug()) << beast::leftw (18) << - "Bootcache failed " << endpoint << - " with " << n << - ((n > 1) ? " attempts" : " attempt"); + Entry const& entry(result.first->right); + JLOG(m_journal.info()) << beast::leftw(18) << "Bootcache connect " + << endpoint << " with " << entry.valence() + << ((entry.valence() > 1) ? " successes" + : " success"); flagForUpdate(); } void -Bootcache::periodicActivity () +Bootcache::on_failure(beast::IP::Endpoint const& endpoint) +{ + auto result(m_map.insert(value_type(endpoint, -1))); + if (result.second) + { + prune(); + } + else + { + Entry entry(result.first->right); + if (entry.valence() > 0) + entry.valence() = 0; + --entry.valence(); + m_map.erase(result.first); + result = m_map.insert(value_type(endpoint, entry)); + assert(result.second); + } + Entry const& entry(result.first->right); + auto const n(std::abs(entry.valence())); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Bootcache failed " << endpoint << " with " << n + << ((n > 1) ? " attempts" : " attempt"); + flagForUpdate(); +} + +void +Bootcache::periodicActivity() { checkUpdate(); } @@ -218,64 +204,64 @@ Bootcache::periodicActivity () //-------------------------------------------------------------------------- void -Bootcache::onWrite (beast::PropertyStream::Map& map) +Bootcache::onWrite(beast::PropertyStream::Map& map) { - beast::PropertyStream::Set entries ("entries", map); + beast::PropertyStream::Set entries("entries", map); for (auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter) { - beast::PropertyStream::Map entry (entries); + beast::PropertyStream::Map entry(entries); entry["endpoint"] = iter->get_left().to_string(); - entry["valence"] = std::int32_t (iter->get_right().valence()); + entry["valence"] = std::int32_t(iter->get_right().valence()); } } - // Checks the cache size and prunes if its over the limit. +// Checks the cache size and prunes if its over the limit. void -Bootcache::prune () +Bootcache::prune() { if (size() <= Tuning::bootcacheSize) return; // Calculate the amount to remove - auto count ((size() * - Tuning::bootcachePrunePercent) / 100); - decltype(count) pruned (0); + auto count((size() * Tuning::bootcachePrunePercent) / 100); + decltype(count) pruned(0); // Work backwards because bimap doesn't handle // erasing using a reverse iterator very well. // - for (auto iter (m_map.right.end()); - count-- > 0 && iter != m_map.right.begin(); ++pruned) + for (auto iter(m_map.right.end()); + count-- > 0 && iter != m_map.right.begin(); + ++pruned) { --iter; - beast::IP::Endpoint const& endpoint (iter->get_left()); - Entry const& entry (iter->get_right()); - JLOG(m_journal.trace()) << beast::leftw (18) << - "Bootcache pruned" << endpoint << - " at valence " << entry.valence(); - iter = m_map.right.erase (iter); + beast::IP::Endpoint const& endpoint(iter->get_left()); + Entry const& entry(iter->get_right()); + JLOG(m_journal.trace()) + << beast::leftw(18) << "Bootcache pruned" << endpoint + << " at valence " << entry.valence(); + iter = m_map.right.erase(iter); } - JLOG(m_journal.debug()) << beast::leftw (18) << - "Bootcache pruned " << pruned << " entries total"; + JLOG(m_journal.debug()) << beast::leftw(18) << "Bootcache pruned " << pruned + << " entries total"; } // Updates the Store with the current set of entries if needed. void -Bootcache::update () +Bootcache::update() { - if (! m_needsUpdate) + if (!m_needsUpdate) return; - std::vector list; - list.reserve (m_map.size()); + std::vector list; + list.reserve(m_map.size()); for (auto const& e : m_map) { Store::Entry se; se.endpoint = e.get_left(); se.valence = e.get_right().valence(); - list.push_back (se); + list.push_back(se); } - m_store.save (list); + m_store.save(list); // Reset the flag and cooldown timer m_needsUpdate = false; m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime; @@ -283,19 +269,19 @@ Bootcache::update () // Checks the clock and calls update if we are off the cooldown. void -Bootcache::checkUpdate () +Bootcache::checkUpdate() { if (m_needsUpdate && m_whenUpdate < m_clock.now()) - update (); + update(); } // Called when changes to an entry will affect the Store. void -Bootcache::flagForUpdate () +Bootcache::flagForUpdate() { m_needsUpdate = true; - checkUpdate (); + checkUpdate(); } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/Bootcache.h b/src/ripple/peerfinder/impl/Bootcache.h index eb45bc81e3..bdcdffd43f 100644 --- a/src/ripple/peerfinder/impl/Bootcache.h +++ b/src/ripple/peerfinder/impl/Bootcache.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_PEERFINDER_BOOTCACHE_H_INCLUDED #define RIPPLE_PEERFINDER_BOOTCACHE_H_INCLUDED -#include -#include #include #include +#include +#include #include #include #include @@ -53,22 +53,24 @@ private: class Entry { public: - Entry (int valence) - : m_valence (valence) + Entry(int valence) : m_valence(valence) { } - int& valence () + int& + valence() { return m_valence; } - int valence () const + int + valence() const { return m_valence; } - friend bool operator< (Entry const& lhs, Entry const& rhs) + friend bool + operator<(Entry const& lhs, Entry const& rhs) { if (lhs.valence() > rhs.valence()) return true; @@ -79,9 +81,9 @@ private: int m_valence; }; - using left_t = boost::bimaps::unordered_set_of ; - using right_t = boost::bimaps::multiset_of ; - using map_type = boost::bimap ; + using left_t = boost::bimaps::unordered_set_of; + using right_t = boost::bimaps::multiset_of; + using map_type = boost::bimap; using value_type = map_type::value_type; struct Transform @@ -92,15 +94,16 @@ private: #endif { #ifndef _LIBCPP_VERSION - using first_argument_type = map_type::right_map::const_iterator::value_type const&; + using first_argument_type = + map_type::right_map::const_iterator::value_type const&; using result_type = beast::IP::Endpoint const&; #endif explicit Transform() = default; - beast::IP::Endpoint const& operator() ( - map_type::right_map:: - const_iterator::value_type const& v) const + beast::IP::Endpoint const& + operator()( + map_type::right_map::const_iterator::value_type const& v) const { return v.get_left(); } @@ -122,62 +125,77 @@ private: public: static constexpr int staticValence = 32; - using iterator = boost::transform_iterator ; + using iterator = boost:: + transform_iterator; using const_iterator = iterator; - Bootcache ( - Store& store, - clock_type& clock, - beast::Journal journal); + Bootcache(Store& store, clock_type& clock, beast::Journal journal); - ~Bootcache (); + ~Bootcache(); /** Returns `true` if the cache is empty. */ - bool empty() const; + bool + empty() const; /** Returns the number of entries in the cache. */ - map_type::size_type size() const; + map_type::size_type + size() const; /** IP::Endpoint iterators that traverse in decreasing valence. */ /** @{ */ - const_iterator begin() const; - const_iterator cbegin() const; - const_iterator end() const; - const_iterator cend() const; - void clear(); + const_iterator + begin() const; + const_iterator + cbegin() const; + const_iterator + end() const; + const_iterator + cend() const; + void + clear(); /** @} */ /** Load the persisted data from the Store into the container. */ - void load (); + void + load(); /** Add a newly-learned address to the cache. */ - bool insert (beast::IP::Endpoint const& endpoint); + bool + insert(beast::IP::Endpoint const& endpoint); /** Add a staticallyconfigured address to the cache. */ - bool insertStatic (beast::IP::Endpoint const& endpoint); + bool + insertStatic(beast::IP::Endpoint const& endpoint); /** Called when an outbound connection handshake completes. */ - void on_success (beast::IP::Endpoint const& endpoint); + void + on_success(beast::IP::Endpoint const& endpoint); /** Called when an outbound connection attempt fails to handshake. */ - void on_failure (beast::IP::Endpoint const& endpoint); + void + on_failure(beast::IP::Endpoint const& endpoint); /** Stores the cache in the persistent database on a timer. */ - void periodicActivity (); + void + periodicActivity(); /** Write the cache state to the property stream. */ - void onWrite (beast::PropertyStream::Map& map); + void + onWrite(beast::PropertyStream::Map& map); private: - void prune (); - void update (); - void checkUpdate (); - void flagForUpdate (); + void + prune(); + void + update(); + void + checkUpdate(); + void + flagForUpdate(); }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Checker.h b/src/ripple/peerfinder/impl/Checker.h index 435e7c7cd2..3a5ef90132 100644 --- a/src/ripple/peerfinder/impl/Checker.h +++ b/src/ripple/peerfinder/impl/Checker.h @@ -35,25 +35,23 @@ namespace ripple { namespace PeerFinder { /** Tests remote listening sockets to make sure they are connectible. */ - template +template class Checker { private: using error_code = boost::system::error_code; - struct basic_async_op : boost::intrusive::list_base_hook < - boost::intrusive::link_mode > + struct basic_async_op + : boost::intrusive::list_base_hook< + boost::intrusive::link_mode> { - virtual - ~basic_async_op() = default; + virtual ~basic_async_op() = default; - virtual - void + virtual void stop() = 0; - virtual - void - operator() (error_code const& ec) = 0; + virtual void + operator()(error_code const& ec) = 0; }; template @@ -66,7 +64,9 @@ private: socket_type socket_; Handler handler_; - async_op (Checker& owner, boost::asio::io_service& io_service, + async_op( + Checker& owner, + boost::asio::io_service& io_service, Handler&& handler); ~async_op(); @@ -75,13 +75,14 @@ private: stop() override; void - operator() (error_code const& ec) override; + operator()(error_code const& ec) override; }; //-------------------------------------------------------------------------- - using list_type = typename boost::intrusive::make_list >::type; + using list_type = typename boost::intrusive::make_list< + basic_async_op, + boost::intrusive::constant_time_size>::type; std::mutex mutex_; std::condition_variable cond_; @@ -90,8 +91,7 @@ private: bool stop_ = false; public: - explicit - Checker (boost::asio::io_service& io_service); + explicit Checker(boost::asio::io_service& io_service); /** Destroy the service. Any pending I/O operations will be canceled. This call blocks until @@ -120,22 +120,24 @@ public: */ template void - async_connect (beast::IP::Endpoint const& endpoint, Handler&& handler); + async_connect(beast::IP::Endpoint const& endpoint, Handler&& handler); private: void - remove (basic_async_op& op); + remove(basic_async_op& op); }; //------------------------------------------------------------------------------ template template -Checker::async_op::async_op (Checker& owner, - boost::asio::io_service& io_service, Handler&& handler) - : checker_ (owner) - , socket_ (io_service) - , handler_ (std::forward(handler)) +Checker::async_op::async_op( + Checker& owner, + boost::asio::io_service& io_service, + Handler&& handler) + : checker_(owner) + , socket_(io_service) + , handler_(std::forward(handler)) { } @@ -143,7 +145,7 @@ template template Checker::async_op::~async_op() { - checker_.remove (*this); + checker_.remove(*this); } template @@ -158,8 +160,7 @@ Checker::async_op::stop() template template void -Checker::async_op::operator() ( - error_code const& ec) +Checker::async_op::operator()(error_code const& ec) { handler_(ec); } @@ -167,7 +168,7 @@ Checker::async_op::operator() ( //------------------------------------------------------------------------------ template -Checker::Checker (boost::asio::io_service& io_service) +Checker::Checker(boost::asio::io_service& io_service) : io_service_(io_service) { } @@ -182,8 +183,8 @@ template void Checker::stop() { - std::lock_guard lock (mutex_); - if (! stop_) + std::lock_guard lock(mutex_); + if (!stop_) { stop_ = true; for (auto& c : list_) @@ -195,39 +196,40 @@ template void Checker::wait() { - std::unique_lock lock (mutex_); - while (! list_.empty()) - cond_.wait (lock); + std::unique_lock lock(mutex_); + while (!list_.empty()) + cond_.wait(lock); } template template void -Checker::async_connect ( - beast::IP::Endpoint const& endpoint, Handler&& handler) +Checker::async_connect( + beast::IP::Endpoint const& endpoint, + Handler&& handler) { - auto const op = std::make_shared> ( + auto const op = std::make_shared>( *this, io_service_, std::forward(handler)); { - std::lock_guard lock (mutex_); - list_.push_back (*op); + std::lock_guard lock(mutex_); + list_.push_back(*op); } - op->socket_.async_connect (beast::IPAddressConversion::to_asio_endpoint ( - endpoint), std::bind (&basic_async_op::operator(), op, - std::placeholders::_1)); + op->socket_.async_connect( + beast::IPAddressConversion::to_asio_endpoint(endpoint), + std::bind(&basic_async_op::operator(), op, std::placeholders::_1)); } template void -Checker::remove (basic_async_op& op) +Checker::remove(basic_async_op& op) { - std::lock_guard lock (mutex_); - list_.erase (list_.iterator_to (op)); + std::lock_guard lock(mutex_); + list_.erase(list_.iterator_to(op)); if (list_.size() == 0) cond_.notify_all(); } -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Counts.h b/src/ripple/peerfinder/impl/Counts.h index 1bf1328caf..7053c5789c 100644 --- a/src/ripple/peerfinder/impl/Counts.h +++ b/src/ripple/peerfinder/impl/Counts.h @@ -34,56 +34,59 @@ namespace PeerFinder { class Counts { public: - Counts () - : m_attempts (0) - , m_active (0) - , m_in_max (0) - , m_in_active (0) - , m_out_max (0) - , m_out_active (0) - , m_fixed (0) - , m_fixed_active (0) - , m_reserved (0) + Counts() + : m_attempts(0) + , m_active(0) + , m_in_max(0) + , m_in_active(0) + , m_out_max(0) + , m_out_active(0) + , m_fixed(0) + , m_fixed_active(0) + , m_reserved(0) - , m_acceptCount (0) - , m_closingCount (0) + , m_acceptCount(0) + , m_closingCount(0) { m_roundingThreshold = - std::generate_canonical ( - default_prng()); + std::generate_canonical(default_prng()); } //-------------------------------------------------------------------------- /** Adds the slot state and properties to the slot counts. */ - void add (Slot const& s) + void + add(Slot const& s) { - adjust (s, 1); + adjust(s, 1); } /** Removes the slot state and properties from the slot counts. */ - void remove (Slot const& s) + void + remove(Slot const& s) { - adjust (s, -1); + adjust(s, -1); } /** Returns `true` if the slot can become active. */ - bool can_activate (Slot const& s) const + bool + can_activate(Slot const& s) const { // Must be handshaked and in the right state - assert (s.state() == Slot::connected || s.state() == Slot::accept); + assert(s.state() == Slot::connected || s.state() == Slot::accept); - if (s.fixed () || s.reserved ()) + if (s.fixed() || s.reserved()) return true; - if (s.inbound ()) + if (s.inbound()) return m_in_active < m_in_max; return m_out_active < m_out_max; } /** Returns the number of attempts needed to bring us to the max. */ - std::size_t attempts_needed () const + std::size_t + attempts_needed() const { if (m_attempts >= Tuning::maxConnectAttempts) return 0; @@ -91,13 +94,15 @@ public: } /** Returns the number of outbound connection attempts. */ - std::size_t attempts () const + std::size_t + attempts() const { return m_attempts; } /** Returns the total number of outbound slots. */ - int out_max () const + int + out_max() const { return m_out_max; } @@ -105,19 +110,22 @@ public: /** Returns the number of outbound peers assigned an open slot. Fixed peers do not count towards outbound slots used. */ - int out_active () const + int + out_active() const { return m_out_active; } /** Returns the number of fixed connections. */ - std::size_t fixed () const + std::size_t + fixed() const { return m_fixed; } /** Returns the number of active fixed connections. */ - std::size_t fixed_active () const + std::size_t + fixed_active() const { return m_fixed_active; } @@ -125,17 +133,18 @@ public: //-------------------------------------------------------------------------- /** Called when the config is set or changed. */ - void onConfig (Config const& config) + void + onConfig(Config const& config) { - // Calculate the number of outbound peers we want. If we dont want or can't - // accept incoming, this will simply be equal to maxPeers. Otherwise - // we calculate a fractional amount based on percentages and pseudo-randomly - // round up or down. + // Calculate the number of outbound peers we want. If we dont want or + // can't accept incoming, this will simply be equal to maxPeers. + // Otherwise we calculate a fractional amount based on percentages and + // pseudo-randomly round up or down. // if (config.wantIncoming) { // Round outPeers upwards using a Bernoulli distribution - m_out_max = std::floor (config.outPeers); + m_out_max = std::floor(config.outPeers); if (m_roundingThreshold < (config.outPeers - m_out_max)) ++m_out_max; } @@ -152,37 +161,43 @@ public: } /** Returns the number of accepted connections that haven't handshaked. */ - int acceptCount() const + int + acceptCount() const { return m_acceptCount; } /** Returns the number of connection attempts currently active. */ - int connectCount() const + int + connectCount() const { return m_attempts; } /** Returns the number of connections that are gracefully closing. */ - int closingCount () const + int + closingCount() const { return m_closingCount; } /** Returns the total number of inbound slots. */ - int inboundSlots () const + int + inboundSlots() const { return m_in_max; } /** Returns the number of inbound peers assigned an open slot. */ - int inboundActive () const + int + inboundActive() const { return m_in_active; } /** Returns the total number of active peers excluding fixed peers. */ - int totalActive () const + int + totalActive() const { return m_in_active + m_out_active; } @@ -190,7 +205,8 @@ public: /** Returns the number of unused inbound slots. Fixed peers do not deduct from inbound slots or count towards totals. */ - int inboundSlotsFree () const + int + inboundSlotsFree() const { if (m_in_active < m_in_max) return m_in_max - m_in_active; @@ -200,7 +216,8 @@ public: /** Returns the number of unused outbound slots. Fixed peers do not deduct from outbound slots or count towards totals. */ - int outboundSlotsFree () const + int + outboundSlotsFree() const { if (m_out_active < m_out_max) return m_out_max - m_out_active; @@ -209,8 +226,10 @@ public: //-------------------------------------------------------------------------- - /** Returns true if the slot logic considers us "connected" to the network. */ - bool isConnectedToNetwork () const + /** Returns true if the slot logic considers us "connected" to the network. + */ + bool + isConnectedToNetwork() const { // We will consider ourselves connected if we have reached // the number of outgoing connections desired, or if connect @@ -225,75 +244,75 @@ public: } /** Output statistics. */ - void onWrite (beast::PropertyStream::Map& map) + void + onWrite(beast::PropertyStream::Map& map) { - map ["accept"] = acceptCount (); - map ["connect"] = connectCount (); - map ["close"] = closingCount (); - map ["in"] << m_in_active << "/" << m_in_max; - map ["out"] << m_out_active << "/" << m_out_max; - map ["fixed"] = m_fixed_active; - map ["reserved"] = m_reserved; - map ["total"] = m_active; + map["accept"] = acceptCount(); + map["connect"] = connectCount(); + map["close"] = closingCount(); + map["in"] << m_in_active << "/" << m_in_max; + map["out"] << m_out_active << "/" << m_out_max; + map["fixed"] = m_fixed_active; + map["reserved"] = m_reserved; + map["total"] = m_active; } /** Records the state for diagnostics. */ - std::string state_string () const + std::string + state_string() const { std::stringstream ss; - ss << - m_out_active << "/" << m_out_max << " out, " << - m_in_active << "/" << m_in_max << " in, " << - connectCount() << " connecting, " << - closingCount() << " closing" - ; + ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/" + << m_in_max << " in, " << connectCount() << " connecting, " + << closingCount() << " closing"; return ss.str(); } //-------------------------------------------------------------------------- private: // Adjusts counts based on the specified slot, in the direction indicated. - void adjust (Slot const& s, int const n) + void + adjust(Slot const& s, int const n) { - if (s.fixed ()) + if (s.fixed()) m_fixed += n; - if (s.reserved ()) + if (s.reserved()) m_reserved += n; - switch (s.state ()) + switch (s.state()) { - case Slot::accept: - assert (s.inbound ()); - m_acceptCount += n; - break; + case Slot::accept: + assert(s.inbound()); + m_acceptCount += n; + break; - case Slot::connect: - case Slot::connected: - assert (! s.inbound ()); - m_attempts += n; - break; + case Slot::connect: + case Slot::connected: + assert(!s.inbound()); + m_attempts += n; + break; - case Slot::active: - if (s.fixed ()) - m_fixed_active += n; - if (! s.fixed () && ! s.reserved ()) - { - if (s.inbound ()) - m_in_active += n; - else - m_out_active += n; - } - m_active += n; - break; + case Slot::active: + if (s.fixed()) + m_fixed_active += n; + if (!s.fixed() && !s.reserved()) + { + if (s.inbound()) + m_in_active += n; + else + m_out_active += n; + } + m_active += n; + break; - case Slot::closing: - m_closingCount += n; - break; + case Slot::closing: + m_closingCount += n; + break; - default: - assert (false); - break; + default: + assert(false); + break; }; } @@ -325,8 +344,6 @@ private: /** Reserved connections. */ std::size_t m_reserved; - - // Number of inbound connections that are // not active or gracefully closing. int m_acceptCount; @@ -342,7 +359,7 @@ private: double m_roundingThreshold; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Endpoint.cpp b/src/ripple/peerfinder/impl/Endpoint.cpp index 36679c3b33..e1c905883f 100644 --- a/src/ripple/peerfinder/impl/Endpoint.cpp +++ b/src/ripple/peerfinder/impl/Endpoint.cpp @@ -22,21 +22,20 @@ namespace ripple { namespace PeerFinder { -Endpoint::Endpoint () - : hops (0) +Endpoint::Endpoint() : hops(0) { } -Endpoint::Endpoint (beast::IP::Endpoint const& ep, int hops_) - : hops (hops_) - , address (ep) +Endpoint::Endpoint(beast::IP::Endpoint const& ep, int hops_) + : hops(hops_), address(ep) { } -bool operator< (Endpoint const& lhs, Endpoint const& rhs) +bool +operator<(Endpoint const& lhs, Endpoint const& rhs) { return lhs.address < rhs.address; } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/Fixed.h b/src/ripple/peerfinder/impl/Fixed.h index 4127554cd8..bf7a2b6b35 100644 --- a/src/ripple/peerfinder/impl/Fixed.h +++ b/src/ripple/peerfinder/impl/Fixed.h @@ -29,31 +29,32 @@ namespace PeerFinder { class Fixed { public: - explicit Fixed (clock_type& clock) - : m_when (clock.now ()) - , m_failures (0) + explicit Fixed(clock_type& clock) : m_when(clock.now()), m_failures(0) { } - Fixed (Fixed const&) = default; + Fixed(Fixed const&) = default; /** Returns the time after which we shoud allow a connection attempt. */ - clock_type::time_point const& when () const + clock_type::time_point const& + when() const { return m_when; } /** Updates metadata to reflect a failed connection. */ - void failure (clock_type::time_point const& now) + void + failure(clock_type::time_point const& now) { - m_failures = std::min (m_failures + 1, - Tuning::connectionBackoff.size() - 1); - m_when = now + std::chrono::minutes ( - Tuning::connectionBackoff [m_failures]); + m_failures = + std::min(m_failures + 1, Tuning::connectionBackoff.size() - 1); + m_when = + now + std::chrono::minutes(Tuning::connectionBackoff[m_failures]); } /** Updates metadata to reflect a successful connection. */ - void success (clock_type::time_point const& now) + void + success(clock_type::time_point const& now) { m_failures = 0; m_when = now; @@ -64,7 +65,7 @@ private: std::size_t m_failures; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Handouts.h b/src/ripple/peerfinder/impl/Handouts.h index a0bca49f64..00e163bbea 100644 --- a/src/ripple/peerfinder/impl/Handouts.h +++ b/src/ripple/peerfinder/impl/Handouts.h @@ -20,9 +20,9 @@ #ifndef RIPPLE_PEERFINDER_HANDOUTS_H_INCLUDED #define RIPPLE_PEERFINDER_HANDOUTS_H_INCLUDED +#include #include #include -#include #include #include #include @@ -41,22 +41,22 @@ namespace detail { // template std::size_t -handout_one (Target& t, HopContainer& h) +handout_one(Target& t, HopContainer& h) { - assert (! t.full()); + assert(!t.full()); for (auto it = h.begin(); it != h.end(); ++it) { auto const& e = *it; - if (t.try_insert (e)) + if (t.try_insert(e)) { - h.move_back (it); + h.move_back(it); return 1; } } return 0; } -} +} // namespace detail /** Distributes objects to targets according to business rules. A best effort is made to evenly distribute items in the sequence @@ -64,29 +64,32 @@ handout_one (Target& t, HopContainer& h) */ template void -handout (TargetFwdIter first, TargetFwdIter last, - SeqFwdIter seq_first, SeqFwdIter seq_last) +handout( + TargetFwdIter first, + TargetFwdIter last, + SeqFwdIter seq_first, + SeqFwdIter seq_last) { for (;;) { - std::size_t n (0); + std::size_t n(0); for (auto si = seq_first; si != seq_last; ++si) { auto c = *si; - bool all_full (true); + bool all_full(true); for (auto ti = first; ti != last; ++ti) { auto& t = *ti; - if (! t.full()) + if (!t.full()) { - n += detail::handout_one (t, c); + n += detail::handout_one(t, c); all_full = false; } } if (all_full) return; } - if (! n) + if (!n) break; } } @@ -100,49 +103,52 @@ class RedirectHandouts { public: template - explicit - RedirectHandouts (SlotImp::ptr const& slot); + explicit RedirectHandouts(SlotImp::ptr const& slot); template - bool try_insert (Endpoint const& ep); + bool + try_insert(Endpoint const& ep); - bool full () const + bool + full() const { return list_.size() >= Tuning::redirectEndpointCount; } - SlotImp::ptr const& slot () const + SlotImp::ptr const& + slot() const { return slot_; } - std::vector & list() + std::vector& + list() { return list_; } - std::vector const& list() const + std::vector const& + list() const { return list_; } private: SlotImp::ptr slot_; - std::vector list_; + std::vector list_; }; template -RedirectHandouts::RedirectHandouts (SlotImp::ptr const& slot) - : slot_ (slot) +RedirectHandouts::RedirectHandouts(SlotImp::ptr const& slot) : slot_(slot) { - list_.reserve (Tuning::redirectEndpointCount); + list_.reserve(Tuning::redirectEndpointCount); } template bool -RedirectHandouts::try_insert (Endpoint const& ep) +RedirectHandouts::try_insert(Endpoint const& ep) { - if (full ()) + if (full()) return false; // VFALCO NOTE This check can be removed when we provide the @@ -157,14 +163,11 @@ RedirectHandouts::try_insert (Endpoint const& ep) return false; // Don't send them their own address - if (slot_->remote_endpoint().address() == - ep.address.address()) + if (slot_->remote_endpoint().address() == ep.address.address()) return false; // Make sure the address isn't already in our list - if (std::any_of (list_.begin(), list_.end(), - [&ep](Endpoint const& other) - { + if (std::any_of(list_.begin(), list_.end(), [&ep](Endpoint const& other) { // Ignore port for security reasons return other.address.address() == ep.address.address(); })) @@ -172,7 +175,7 @@ RedirectHandouts::try_insert (Endpoint const& ep) return false; } - list_.emplace_back (ep.address, ep.hops); + list_.emplace_back(ep.address, ep.hops); return true; } @@ -184,79 +187,79 @@ class SlotHandouts { public: template - explicit - SlotHandouts (SlotImp::ptr const& slot); + explicit SlotHandouts(SlotImp::ptr const& slot); template - bool try_insert (Endpoint const& ep); + bool + try_insert(Endpoint const& ep); - bool full () const + bool + full() const { return list_.size() >= Tuning::numberOfEndpoints; } - void insert (Endpoint const& ep) + void + insert(Endpoint const& ep) { - list_.push_back (ep); + list_.push_back(ep); } - SlotImp::ptr const& slot () const + SlotImp::ptr const& + slot() const { return slot_; } - std::vector const& list() const + std::vector const& + list() const { return list_; } private: SlotImp::ptr slot_; - std::vector list_; + std::vector list_; }; template -SlotHandouts::SlotHandouts (SlotImp::ptr const& slot) - : slot_ (slot) +SlotHandouts::SlotHandouts(SlotImp::ptr const& slot) : slot_(slot) { - list_.reserve (Tuning::numberOfEndpoints); + list_.reserve(Tuning::numberOfEndpoints); } template bool -SlotHandouts::try_insert (Endpoint const& ep) +SlotHandouts::try_insert(Endpoint const& ep) { - if (full ()) + if (full()) return false; if (ep.hops > Tuning::maxHops) return false; - if (slot_->recent.filter (ep.address, ep.hops)) + if (slot_->recent.filter(ep.address, ep.hops)) return false; // Don't send them their own address - if (slot_->remote_endpoint().address() == - ep.address.address()) + if (slot_->remote_endpoint().address() == ep.address.address()) return false; // Make sure the address isn't already in our list - if (std::any_of (list_.begin(), list_.end(), - [&ep](Endpoint const& other) - { + if (std::any_of(list_.begin(), list_.end(), [&ep](Endpoint const& other) { // Ignore port for security reasons return other.address.address() == ep.address.address(); })) return false; - list_.emplace_back (ep.address, ep.hops); + list_.emplace_back(ep.address, ep.hops); // Insert into this slot's recent table. Although the endpoint // didn't come from the slot, adding it to the slot's table // prevents us from sending it again until it has expired from // the other end's cache. // - slot_->recent.insert (ep.address, ep.hops); + slot_->recent.insert(ep.address, ep.hops); return true; } @@ -269,9 +272,9 @@ class ConnectHandouts public: // Keeps track of addresses we have made outgoing connections // to, for the purposes of not connecting to them too frequently. - using Squelches = beast::aged_set ; + using Squelches = beast::aged_set; - using list_type = std::vector ; + using list_type = std::vector; private: std::size_t m_needed; @@ -280,78 +283,81 @@ private: public: template - ConnectHandouts (std::size_t needed, Squelches& squelches); + ConnectHandouts(std::size_t needed, Squelches& squelches); template - bool try_insert (beast::IP::Endpoint const& endpoint); + bool + try_insert(beast::IP::Endpoint const& endpoint); - bool empty() const + bool + empty() const { return m_list.empty(); } - bool full() const + bool + full() const { return m_list.size() >= m_needed; } - bool try_insert (Endpoint const& endpoint) + bool + try_insert(Endpoint const& endpoint) { - return try_insert (endpoint.address); + return try_insert(endpoint.address); } - list_type& list() + list_type& + list() { return m_list; } - list_type const& list() const + list_type const& + list() const { return m_list; } }; template -ConnectHandouts::ConnectHandouts ( - std::size_t needed, Squelches& squelches) - : m_needed (needed) - , m_squelches (squelches) +ConnectHandouts::ConnectHandouts(std::size_t needed, Squelches& squelches) + : m_needed(needed), m_squelches(squelches) { - m_list.reserve (needed); + m_list.reserve(needed); } template bool -ConnectHandouts::try_insert (beast::IP::Endpoint const& endpoint) +ConnectHandouts::try_insert(beast::IP::Endpoint const& endpoint) { - if (full ()) + if (full()) return false; // Make sure the address isn't already in our list - if (std::any_of (m_list.begin(), m_list.end(), - [&endpoint](beast::IP::Endpoint const& other) - { - // Ignore port for security reasons - return other.address() == - endpoint.address(); - })) + if (std::any_of( + m_list.begin(), + m_list.end(), + [&endpoint](beast::IP::Endpoint const& other) { + // Ignore port for security reasons + return other.address() == endpoint.address(); + })) { return false; } // Add to squelch list so we don't try it too often. // If its already there, then make try_insert fail. - auto const result (m_squelches.insert ( - endpoint.address())); - if (! result.second) + auto const result(m_squelches.insert(endpoint.address())); + if (!result.second) return false; - m_list.push_back (endpoint); + m_list.push_back(endpoint); return true; } -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Livecache.h b/src/ripple/peerfinder/impl/Livecache.h index 1234f0dc6e..d12e063e68 100644 --- a/src/ripple/peerfinder/impl/Livecache.h +++ b/src/ripple/peerfinder/impl/Livecache.h @@ -22,11 +22,11 @@ #include #include -#include -#include -#include #include #include +#include +#include +#include #include #include @@ -44,21 +44,19 @@ class LivecacheBase { public: explicit LivecacheBase() = default; + protected: - struct Element - : boost::intrusive::list_base_hook <> + struct Element : boost::intrusive::list_base_hook<> { - Element (Endpoint const& endpoint_) - : endpoint (endpoint_) + Element(Endpoint const& endpoint_) : endpoint(endpoint_) { } Endpoint endpoint; }; - using list_type = boost::intrusive::make_list - >::type; + using list_type = boost::intrusive:: + make_list>::type; public: /** A list of Endpoint at the same hops @@ -82,103 +80,107 @@ public: explicit Transform() = default; - Endpoint const& operator() (Element const& e) const + Endpoint const& + operator()(Element const& e) const { return e.endpoint; } }; public: - using iterator = boost::transform_iterator ; + using iterator = boost:: + transform_iterator; using const_iterator = iterator; - using reverse_iterator = boost::transform_iterator ; using const_reverse_iterator = reverse_iterator; - iterator begin () const + iterator + begin() const { - return iterator (m_list.get().cbegin(), - Transform()); + return iterator(m_list.get().cbegin(), Transform()); } - iterator cbegin () const + iterator + cbegin() const { - return iterator (m_list.get().cbegin(), - Transform()); + return iterator(m_list.get().cbegin(), Transform()); } - iterator end () const + iterator + end() const { - return iterator (m_list.get().cend(), - Transform()); + return iterator(m_list.get().cend(), Transform()); } - iterator cend () const + iterator + cend() const { - return iterator (m_list.get().cend(), - Transform()); + return iterator(m_list.get().cend(), Transform()); } - reverse_iterator rbegin () const + reverse_iterator + rbegin() const { - return reverse_iterator (m_list.get().crbegin(), - Transform()); + return reverse_iterator(m_list.get().crbegin(), Transform()); } - reverse_iterator crbegin () const + reverse_iterator + crbegin() const { - return reverse_iterator (m_list.get().crbegin(), - Transform()); + return reverse_iterator(m_list.get().crbegin(), Transform()); } - reverse_iterator rend () const + reverse_iterator + rend() const { - return reverse_iterator (m_list.get().crend(), - Transform()); + return reverse_iterator(m_list.get().crend(), Transform()); } - reverse_iterator crend () const + reverse_iterator + crend() const { - return reverse_iterator (m_list.get().crend(), - Transform()); + return reverse_iterator(m_list.get().crend(), Transform()); } // move the element to the end of the container - void move_back (const_iterator pos) + void + move_back(const_iterator pos) { - auto& e (const_cast (*pos.base())); - m_list.get().erase (m_list.get().iterator_to (e)); - m_list.get().push_back (e); + auto& e(const_cast(*pos.base())); + m_list.get().erase(m_list.get().iterator_to(e)); + m_list.get().push_back(e); } private: - explicit Hop (typename beast::maybe_const < - IsConst, list_type>::type& list) - : m_list (list) + explicit Hop( + typename beast::maybe_const::type& list) + : m_list(list) { } friend class LivecacheBase; - std::reference_wrapper ::type> m_list; + std::reference_wrapper< + typename beast::maybe_const::type> + m_list; }; protected: // Work-around to call Hop's private constructor from Livecache template - static Hop make_hop (typename beast::maybe_const < - IsConst, list_type>::type& list) + static Hop + make_hop(typename beast::maybe_const::type& list) { - return Hop (list); + return Hop(list); } }; -} +} // namespace detail //------------------------------------------------------------------------------ @@ -194,13 +196,16 @@ protected: launches or for bootstrapping, because they do not have verifiable and locally observed uptime and connectibility information. */ -template > +template > class Livecache : protected detail::LivecacheBase { private: - using cache_type = beast::aged_map , - Allocator>; + using cache_type = beast::aged_map< + beast::IP::Endpoint, + Element, + std::chrono::steady_clock, + std::less, + Allocator>; beast::Journal m_journal; cache_type m_cache; @@ -209,7 +214,7 @@ public: using allocator_type = Allocator; /** Create the cache. */ - Livecache ( + Livecache( clock_type& clock, beast::Journal journal, Allocator alloc = Allocator()); @@ -229,128 +234,139 @@ public: // but not given out (since they would exceed maxHops). They // are used for automatic connection attempts. // - using Histogram = std::array ; - using lists_type = std::array ; + using Histogram = std::array; + using lists_type = std::array; template struct Transform #ifdef _LIBCPP_VERSION - : public std::unary_function> + : public std:: + unary_function> #endif { #ifndef _LIBCPP_VERSION using first_argument = typename lists_type::value_type; - using result_type = Hop ; + using result_type = Hop; #endif explicit Transform() = default; - Hop operator() (typename beast::maybe_const < - IsConst, typename lists_type::value_type>::type& list) const + Hop + operator()(typename beast::maybe_const< + IsConst, + typename lists_type::value_type>::type& list) const { - return make_hop (list); + return make_hop(list); } }; public: - using iterator = boost::transform_iterator , - typename lists_type::iterator>; + using iterator = boost:: + transform_iterator, typename lists_type::iterator>; - using const_iterator = boost::transform_iterator , + using const_iterator = boost::transform_iterator< + Transform, typename lists_type::const_iterator>; - using reverse_iterator = boost::transform_iterator , + using reverse_iterator = boost::transform_iterator< + Transform, typename lists_type::reverse_iterator>; - using const_reverse_iterator = boost::transform_iterator , + using const_reverse_iterator = boost::transform_iterator< + Transform, typename lists_type::const_reverse_iterator>; - iterator begin () + iterator + begin() { - return iterator (m_lists.begin(), - Transform ()); + return iterator(m_lists.begin(), Transform()); } - const_iterator begin () const + const_iterator + begin() const { - return const_iterator (m_lists.cbegin(), - Transform ()); + return const_iterator(m_lists.cbegin(), Transform()); } - const_iterator cbegin () const + const_iterator + cbegin() const { - return const_iterator (m_lists.cbegin(), - Transform ()); + return const_iterator(m_lists.cbegin(), Transform()); } - iterator end () + iterator + end() { - return iterator (m_lists.end(), - Transform ()); + return iterator(m_lists.end(), Transform()); } - const_iterator end () const + const_iterator + end() const { - return const_iterator (m_lists.cend(), - Transform ()); + return const_iterator(m_lists.cend(), Transform()); } - const_iterator cend () const + const_iterator + cend() const { - return const_iterator (m_lists.cend(), - Transform ()); + return const_iterator(m_lists.cend(), Transform()); } - reverse_iterator rbegin () + reverse_iterator + rbegin() { - return reverse_iterator (m_lists.rbegin(), - Transform ()); + return reverse_iterator(m_lists.rbegin(), Transform()); } - const_reverse_iterator rbegin () const + const_reverse_iterator + rbegin() const { - return const_reverse_iterator (m_lists.crbegin(), - Transform ()); + return const_reverse_iterator(m_lists.crbegin(), Transform()); } - const_reverse_iterator crbegin () const + const_reverse_iterator + crbegin() const { - return const_reverse_iterator (m_lists.crbegin(), - Transform ()); + return const_reverse_iterator(m_lists.crbegin(), Transform()); } - reverse_iterator rend () + reverse_iterator + rend() { - return reverse_iterator (m_lists.rend(), - Transform ()); + return reverse_iterator(m_lists.rend(), Transform()); } - const_reverse_iterator rend () const + const_reverse_iterator + rend() const { - return const_reverse_iterator (m_lists.crend(), - Transform ()); + return const_reverse_iterator(m_lists.crend(), Transform()); } - const_reverse_iterator crend () const + const_reverse_iterator + crend() const { - return const_reverse_iterator (m_lists.crend(), - Transform ()); + return const_reverse_iterator(m_lists.crend(), Transform()); } /** Shuffle each hop list. */ - void shuffle (); + void + shuffle(); - std::string histogram() const; + std::string + histogram() const; private: - explicit hops_t (Allocator const& alloc); + explicit hops_t(Allocator const& alloc); - void insert (Element& e); + void + insert(Element& e); // Reinsert e at a new hops - void reinsert (Element& e, int hops); + void + reinsert(Element& e, int hops); - void remove (Element& e); + void + remove(Element& e); friend class Livecache; lists_type m_lists; @@ -358,65 +374,68 @@ public: } hops; /** Returns `true` if the cache is empty. */ - bool empty () const + bool + empty() const { - return m_cache.empty (); + return m_cache.empty(); } /** Returns the number of entries in the cache. */ - typename cache_type::size_type size() const + typename cache_type::size_type + size() const { return m_cache.size(); } /** Erase entries whose time has expired. */ - void expire (); + void + expire(); /** Creates or updates an existing Element based on a new message. */ - void insert (Endpoint const& ep); + void + insert(Endpoint const& ep); /** Output statistics. */ - void onWrite (beast::PropertyStream::Map& map); + void + onWrite(beast::PropertyStream::Map& map); }; //------------------------------------------------------------------------------ template -Livecache ::Livecache ( +Livecache::Livecache( clock_type& clock, beast::Journal journal, Allocator alloc) - : m_journal (journal) - , m_cache (clock, alloc) - , hops (alloc) + : m_journal(journal), m_cache(clock, alloc), hops(alloc) { } template void -Livecache ::expire() +Livecache::expire() { - std::size_t n (0); - typename cache_type::time_point const expired ( + std::size_t n(0); + typename cache_type::time_point const expired( m_cache.clock().now() - Tuning::liveCacheSecondsToLive); - for (auto iter (m_cache.chronological.begin()); - iter != m_cache.chronological.end() && iter.when() <= expired;) + for (auto iter(m_cache.chronological.begin()); + iter != m_cache.chronological.end() && iter.when() <= expired;) { - Element& e (iter->second); - hops.remove (e); - iter = m_cache.erase (iter); + Element& e(iter->second); + hops.remove(e); + iter = m_cache.erase(iter); ++n; } if (n > 0) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Livecache expired " << n << - ((n > 1) ? " entries" : " entry"); + JLOG(m_journal.debug()) << beast::leftw(18) << "Livecache expired " << n + << ((n > 1) ? " entries" : " entry"); } } template -void Livecache ::insert (Endpoint const& ep) +void +Livecache::insert(Endpoint const& ep) { // The caller already incremented hop, so if we got a // message at maxHops we will store it at maxHops + 1. @@ -424,65 +443,60 @@ void Livecache ::insert (Endpoint const& ep) // but we will use it to make connections and hand it out // when redirecting. // - assert (ep.hops <= (Tuning::maxHops + 1)); - std::pair result ( - m_cache.emplace (ep.address, ep)); - Element& e (result.first->second); + assert(ep.hops <= (Tuning::maxHops + 1)); + std::pair result( + m_cache.emplace(ep.address, ep)); + Element& e(result.first->second); if (result.second) { - hops.insert (e); - JLOG(m_journal.debug()) << beast::leftw (18) << - "Livecache insert " << ep.address << - " at hops " << ep.hops; + hops.insert(e); + JLOG(m_journal.debug()) << beast::leftw(18) << "Livecache insert " + << ep.address << " at hops " << ep.hops; return; } - else if (! result.second && (ep.hops > e.endpoint.hops)) + else if (!result.second && (ep.hops > e.endpoint.hops)) { // Drop duplicates at higher hops - std::size_t const excess ( - ep.hops - e.endpoint.hops); - JLOG(m_journal.trace()) << beast::leftw(18) << - "Livecache drop " << ep.address << - " at hops +" << excess; + std::size_t const excess(ep.hops - e.endpoint.hops); + JLOG(m_journal.trace()) << beast::leftw(18) << "Livecache drop " + << ep.address << " at hops +" << excess; return; } - m_cache.touch (result.first); + m_cache.touch(result.first); // Address already in the cache so update metadata if (ep.hops < e.endpoint.hops) { - hops.reinsert (e, ep.hops); - JLOG(m_journal.debug()) << beast::leftw (18) << - "Livecache update " << ep.address << - " at hops " << ep.hops; + hops.reinsert(e, ep.hops); + JLOG(m_journal.debug()) << beast::leftw(18) << "Livecache update " + << ep.address << " at hops " << ep.hops; } else { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Livecache refresh " << ep.address << - " at hops " << ep.hops; + JLOG(m_journal.trace()) << beast::leftw(18) << "Livecache refresh " + << ep.address << " at hops " << ep.hops; } } template void -Livecache ::onWrite (beast::PropertyStream::Map& map) +Livecache::onWrite(beast::PropertyStream::Map& map) { - typename cache_type::time_point const expired ( + typename cache_type::time_point const expired( m_cache.clock().now() - Tuning::liveCacheSecondsToLive); - map ["size"] = size (); - map ["hist"] = hops.histogram(); - beast::PropertyStream::Set set ("entries", map); - for (auto iter (m_cache.cbegin()); iter != m_cache.cend(); ++iter) + map["size"] = size(); + map["hist"] = hops.histogram(); + beast::PropertyStream::Set set("entries", map); + for (auto iter(m_cache.cbegin()); iter != m_cache.cend(); ++iter) { - auto const& e (iter->second); - beast::PropertyStream::Map item (set); - item ["hops"] = e.endpoint.hops; - item ["address"] = e.endpoint.address.to_string (); + auto const& e(iter->second); + beast::PropertyStream::Map item(set); + item["hops"] = e.endpoint.hops; + item["address"] = e.endpoint.address.to_string(); std::stringstream ss; ss << (iter.when() - expired).count(); - item ["expires"] = ss.str(); + item["expires"] = ss.str(); } } @@ -490,75 +504,71 @@ Livecache ::onWrite (beast::PropertyStream::Map& map) template void -Livecache ::hops_t::shuffle() +Livecache::hops_t::shuffle() { for (auto& list : m_lists) { - std::vector > v; - v.reserve (list.size()); - std::copy (list.begin(), list.end(), - std::back_inserter (v)); - std::shuffle (v.begin(), v.end(), default_prng()); + std::vector> v; + v.reserve(list.size()); + std::copy(list.begin(), list.end(), std::back_inserter(v)); + std::shuffle(v.begin(), v.end(), default_prng()); list.clear(); for (auto& e : v) - list.push_back (e); + list.push_back(e); } } template std::string -Livecache ::hops_t::histogram() const +Livecache::hops_t::histogram() const { std::stringstream ss; - for (typename decltype(m_hist)::size_type i (0); - i < m_hist.size(); ++i) + for (typename decltype(m_hist)::size_type i(0); i < m_hist.size(); ++i) { - ss << m_hist[i] << - ((i < Tuning::maxHops + 1) ? ", " : ""); + ss << m_hist[i] << ((i < Tuning::maxHops + 1) ? ", " : ""); } return ss.str(); } template -Livecache ::hops_t::hops_t (Allocator const& alloc) +Livecache::hops_t::hops_t(Allocator const& alloc) { - std::fill (m_hist.begin(), m_hist.end(), 0); + std::fill(m_hist.begin(), m_hist.end(), 0); } template void -Livecache ::hops_t::insert (Element& e) +Livecache::hops_t::insert(Element& e) { - assert (e.endpoint.hops >= 0 && - e.endpoint.hops <= Tuning::maxHops + 1); + assert(e.endpoint.hops >= 0 && e.endpoint.hops <= Tuning::maxHops + 1); // This has security implications without a shuffle - m_lists [e.endpoint.hops].push_front (e); - ++m_hist [e.endpoint.hops]; + m_lists[e.endpoint.hops].push_front(e); + ++m_hist[e.endpoint.hops]; } template void -Livecache ::hops_t::reinsert (Element& e, int numHops) +Livecache::hops_t::reinsert(Element& e, int numHops) { - assert (numHops >= 0 && numHops <= Tuning::maxHops + 1); - list_type& list (m_lists [e.endpoint.hops]); - list.erase (list.iterator_to (e)); - --m_hist [e.endpoint.hops]; + assert(numHops >= 0 && numHops <= Tuning::maxHops + 1); + list_type& list(m_lists[e.endpoint.hops]); + list.erase(list.iterator_to(e)); + --m_hist[e.endpoint.hops]; e.endpoint.hops = numHops; - insert (e); + insert(e); } template void -Livecache ::hops_t::remove (Element& e) +Livecache::hops_t::remove(Element& e) { - --m_hist [e.endpoint.hops]; - list_type& list (m_lists [e.endpoint.hops]); - list.erase (list.iterator_to (e)); + --m_hist[e.endpoint.hops]; + list_type& list(m_lists[e.endpoint.hops]); + list.erase(list.iterator_to(e)); } -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Logic.h b/src/ripple/peerfinder/impl/Logic.h index 931789e025..103d006254 100644 --- a/src/ripple/peerfinder/impl/Logic.h +++ b/src/ripple/peerfinder/impl/Logic.h @@ -21,8 +21,8 @@ #define RIPPLE_PEERFINDER_LOGIC_H_INCLUDED #include -#include #include +#include #include #include #include @@ -57,8 +57,7 @@ public: // Maps remote endpoints to slots. Since a slot has a // remote endpoint upon construction, this holds all counts. // - using Slots = std::map>; + using Slots = std::map>; beast::Journal m_journal; clock_type& m_clock; @@ -84,7 +83,7 @@ public: std::map fixed_; // Live livecache from mtENDPOINTS messages - Livecache <> livecache_; + Livecache<> livecache_; // LiveCache of addresses suitable for gaining initial connections Bootcache bootcache_; @@ -109,26 +108,30 @@ public: //-------------------------------------------------------------------------- - Logic (clock_type& clock, Store& store, - Checker& checker, beast::Journal journal) - : m_journal (journal) - , m_clock (clock) - , m_store (store) - , m_checker (checker) - , livecache_ (m_clock, journal) - , bootcache_ (store, m_clock, journal) - , m_whenBroadcast (m_clock.now()) - , m_squelches (m_clock) + Logic( + clock_type& clock, + Store& store, + Checker& checker, + beast::Journal journal) + : m_journal(journal) + , m_clock(clock) + , m_store(store) + , m_checker(checker) + , livecache_(m_clock, journal) + , bootcache_(store, m_clock, journal) + , m_whenBroadcast(m_clock.now()) + , m_squelches(m_clock) { - config ({}); + config({}); } // Load persistent state information from the Store // - void load () + void + load() { std::lock_guard _(lock_); - bootcache_.load (); + bootcache_.load(); } /** Stop the logic. @@ -137,12 +140,13 @@ public: Thread safety: Safe to call from any thread. */ - void stop () + void + stop() { std::lock_guard _(lock_); stopping_ = true; if (fetchSource_ != nullptr) - fetchSource_->cancel (); + fetchSource_->cancel(); } //-------------------------------------------------------------------------- @@ -152,11 +156,11 @@ public: //-------------------------------------------------------------------------- void - config (Config const& c) + config(Config const& c) { std::lock_guard _(lock_); config_ = c; - counts_.onConfig (config_); + counts_.onConfig(config_); } Config @@ -167,44 +171,46 @@ public: } void - addFixedPeer (std::string const& name, - beast::IP::Endpoint const& ep) + addFixedPeer(std::string const& name, beast::IP::Endpoint const& ep) { std::vector v; v.push_back(ep); - addFixedPeer (name, v); + addFixedPeer(name, v); } void - addFixedPeer (std::string const& name, - std::vector const& addresses) + addFixedPeer( + std::string const& name, + std::vector const& addresses) { std::lock_guard _(lock_); - if (addresses.empty ()) + if (addresses.empty()) { - JLOG(m_journal.info()) << - "Could not resolve fixed slot '" << name << "'"; + JLOG(m_journal.info()) + << "Could not resolve fixed slot '" << name << "'"; return; } for (auto const& remote_address : addresses) { - if (remote_address.port () == 0) + if (remote_address.port() == 0) { - Throw ("Port not specified for address:" + - remote_address.to_string ()); + Throw( + "Port not specified for address:" + + remote_address.to_string()); } - auto result (fixed_.emplace (std::piecewise_construct, - std::forward_as_tuple (remote_address), - std::make_tuple (std::ref (m_clock)))); + auto result(fixed_.emplace( + std::piecewise_construct, + std::forward_as_tuple(remote_address), + std::make_tuple(std::ref(m_clock)))); if (result.second) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic add fixed '" << name << - "' at " << remote_address; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic add fixed '" << name + << "' at " << remote_address; return; } } @@ -213,25 +219,27 @@ public: //-------------------------------------------------------------------------- // Called when the Checker completes a connectivity test - void checkComplete (beast::IP::Endpoint const& remoteAddress, + void + checkComplete( + beast::IP::Endpoint const& remoteAddress, beast::IP::Endpoint const& checkedAddress, - boost::system::error_code ec) + boost::system::error_code ec) { if (ec == boost::asio::error::operation_aborted) return; std::lock_guard _(lock_); - auto const iter (slots_.find (remoteAddress)); + auto const iter(slots_.find(remoteAddress)); if (iter == slots_.end()) { // The slot disconnected before we finished the check - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic tested " << checkedAddress << - " but the connection was closed"; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic tested " << checkedAddress + << " but the connection was closed"; return; } - SlotImp& slot (*iter->second); + SlotImp& slot(*iter->second); slot.checked = true; slot.connectivityCheckInProgress = false; @@ -239,200 +247,197 @@ public: { // VFALCO TODO Should we retry depending on the error? slot.canAccept = false; - JLOG(m_journal.error()) << beast::leftw (18) << - "Logic testing " << iter->first << " with error, " << - ec.message(); - bootcache_.on_failure (checkedAddress); + JLOG(m_journal.error()) + << beast::leftw(18) << "Logic testing " << iter->first + << " with error, " << ec.message(); + bootcache_.on_failure(checkedAddress); return; } slot.canAccept = true; - slot.set_listening_port (checkedAddress.port ()); - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic testing " << checkedAddress << " succeeded"; + slot.set_listening_port(checkedAddress.port()); + JLOG(m_journal.debug()) << beast::leftw(18) << "Logic testing " + << checkedAddress << " succeeded"; } //-------------------------------------------------------------------------- - SlotImp::ptr new_inbound_slot (beast::IP::Endpoint const& local_endpoint, + SlotImp::ptr + new_inbound_slot( + beast::IP::Endpoint const& local_endpoint, beast::IP::Endpoint const& remote_endpoint) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic accept" << remote_endpoint << - " on local " << local_endpoint; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic accept" << remote_endpoint + << " on local " << local_endpoint; std::lock_guard _(lock_); // Check for duplicate connection - if (is_public (remote_endpoint)) + if (is_public(remote_endpoint)) { - auto const count = connectedAddresses_.count ( - remote_endpoint.address()); + auto const count = + connectedAddresses_.count(remote_endpoint.address()); if (count > config_.ipLimit) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic dropping inbound " << remote_endpoint << - " because of ip limits."; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic dropping inbound " + << remote_endpoint << " because of ip limits."; return SlotImp::ptr(); } } // Create the slot - SlotImp::ptr const slot (std::make_shared (local_endpoint, - remote_endpoint, fixed (remote_endpoint.address ()), - m_clock)); + SlotImp::ptr const slot(std::make_shared( + local_endpoint, + remote_endpoint, + fixed(remote_endpoint.address()), + m_clock)); // Add slot to table - auto const result (slots_.emplace ( - slot->remote_endpoint (), slot)); + auto const result(slots_.emplace(slot->remote_endpoint(), slot)); // Remote address must not already exist - assert (result.second); + assert(result.second); // Add to the connected address list - connectedAddresses_.emplace (remote_endpoint.address()); + connectedAddresses_.emplace(remote_endpoint.address()); // Update counts - counts_.add (*slot); + counts_.add(*slot); return result.first->second; } // Can't check for self-connect because we don't know the local endpoint SlotImp::ptr - new_outbound_slot (beast::IP::Endpoint const& remote_endpoint) + new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic connect " << remote_endpoint; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic connect " << remote_endpoint; std::lock_guard _(lock_); // Check for duplicate connection - if (slots_.find (remote_endpoint) != - slots_.end ()) + if (slots_.find(remote_endpoint) != slots_.end()) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic dropping " << remote_endpoint << - " as duplicate connect"; - return SlotImp::ptr (); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic dropping " << remote_endpoint + << " as duplicate connect"; + return SlotImp::ptr(); } // Create the slot - SlotImp::ptr const slot (std::make_shared ( - remote_endpoint, fixed (remote_endpoint), m_clock)); + SlotImp::ptr const slot(std::make_shared( + remote_endpoint, fixed(remote_endpoint), m_clock)); // Add slot to table - auto const result = - slots_.emplace (slot->remote_endpoint (), - slot); + auto const result = slots_.emplace(slot->remote_endpoint(), slot); // Remote address must not already exist - assert (result.second); + assert(result.second); // Add to the connected address list - connectedAddresses_.emplace (remote_endpoint.address()); + connectedAddresses_.emplace(remote_endpoint.address()); // Update counts - counts_.add (*slot); + counts_.add(*slot); return result.first->second; } bool - onConnected (SlotImp::ptr const& slot, + onConnected( + SlotImp::ptr const& slot, beast::IP::Endpoint const& local_endpoint) { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic connected" << slot->remote_endpoint () << - " on local " << local_endpoint; + JLOG(m_journal.trace()) + << beast::leftw(18) << "Logic connected" << slot->remote_endpoint() + << " on local " << local_endpoint; std::lock_guard _(lock_); // The object must exist in our table - assert (slots_.find (slot->remote_endpoint ()) != - slots_.end ()); + assert(slots_.find(slot->remote_endpoint()) != slots_.end()); // Assign the local endpoint now that it's known - slot->local_endpoint (local_endpoint); + slot->local_endpoint(local_endpoint); // Check for self-connect by address { - auto const iter (slots_.find (local_endpoint)); - if (iter != slots_.end ()) + auto const iter(slots_.find(local_endpoint)); + if (iter != slots_.end()) { - assert (iter->second->local_endpoint () - == slot->remote_endpoint ()); - JLOG(m_journal.warn()) << beast::leftw (18) << - "Logic dropping " << slot->remote_endpoint () << - " as self connect"; + assert( + iter->second->local_endpoint() == slot->remote_endpoint()); + JLOG(m_journal.warn()) + << beast::leftw(18) << "Logic dropping " + << slot->remote_endpoint() << " as self connect"; return false; } } // Update counts - counts_.remove (*slot); - slot->state (Slot::connected); - counts_.add (*slot); + counts_.remove(*slot); + slot->state(Slot::connected); + counts_.add(*slot); return true; } Result - activate (SlotImp::ptr const& slot, PublicKey const& key, bool reserved) + activate(SlotImp::ptr const& slot, PublicKey const& key, bool reserved) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic handshake " << slot->remote_endpoint () << - " with " << (reserved ? "reserved " : "") << "key " << key; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic handshake " << slot->remote_endpoint() + << " with " << (reserved ? "reserved " : "") << "key " << key; std::lock_guard _(lock_); // The object must exist in our table - assert (slots_.find (slot->remote_endpoint ()) != - slots_.end ()); + assert(slots_.find(slot->remote_endpoint()) != slots_.end()); // Must be accepted or connected - assert (slot->state() == Slot::accept || - slot->state() == Slot::connected); + assert( + slot->state() == Slot::accept || slot->state() == Slot::connected); // Check for duplicate connection by key - if (keys_.find (key) != keys_.end()) + if (keys_.find(key) != keys_.end()) return Result::duplicate; // If the peer belongs to a cluster or is reserved, // update the slot to reflect that. - counts_.remove (*slot); - slot->reserved (reserved); - counts_.add (*slot); + counts_.remove(*slot); + slot->reserved(reserved); + counts_.add(*slot); // See if we have an open space for this slot - if (! counts_.can_activate (*slot)) + if (!counts_.can_activate(*slot)) { - if (! slot->inbound()) - bootcache_.on_success ( - slot->remote_endpoint()); + if (!slot->inbound()) + bootcache_.on_success(slot->remote_endpoint()); return Result::full; } // Set the key right before adding to the map, otherwise we might // assert later when erasing the key. - slot->public_key (key); + slot->public_key(key); { auto const result = keys_.insert(key); // Public key must not already exist - assert (result.second); + assert(result.second); (void)result.second; } // Change state and update counts - counts_.remove (*slot); - slot->activate (m_clock.now ()); - counts_.add (*slot); + counts_.remove(*slot); + slot->activate(m_clock.now()); + counts_.add(*slot); - if (! slot->inbound()) - bootcache_.on_success ( - slot->remote_endpoint()); + if (!slot->inbound()) + bootcache_.on_success(slot->remote_endpoint()); // Mark fixed slot success - if (slot->fixed() && ! slot->inbound()) + if (slot->fixed() && !slot->inbound()) { - auto iter (fixed_.find (slot->remote_endpoint())); - assert (iter != fixed_.end ()); - iter->second.success (m_clock.now ()); - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic fixed " << slot->remote_endpoint () << " success"; + auto iter(fixed_.find(slot->remote_endpoint())); + assert(iter != fixed_.end()); + iter->second.success(m_clock.now()); + JLOG(m_journal.trace()) << beast::leftw(18) << "Logic fixed " + << slot->remote_endpoint() << " success"; } return Result::success; @@ -442,15 +447,13 @@ public: This is a legacy function, redirects should be returned in the HTTP handshake and not via TMEndpoints. */ - std::vector - redirect (SlotImp::ptr const& slot) + std::vector + redirect(SlotImp::ptr const& slot) { std::lock_guard _(lock_); - RedirectHandouts h (slot); + RedirectHandouts h(slot); livecache_.hops.shuffle(); - handout (&h, (&h)+1, - livecache_.hops.begin(), - livecache_.hops.end()); + handout(&h, (&h) + 1, livecache_.hops.begin(), livecache_.hops.end()); return std::move(h.list()); } @@ -460,28 +463,28 @@ public: // VFALCO TODO This should add the returned addresses to the // squelch list in one go once the list is built, // rather than having each module add to the squelch list. - std::vector + std::vector autoconnect() { - std::vector const none; + std::vector const none; std::lock_guard _(lock_); // Count how many more outbound attempts to make // - auto needed (counts_.attempts_needed ()); + auto needed(counts_.attempts_needed()); if (needed == 0) return none; - ConnectHandouts h (needed, m_squelches); + ConnectHandouts h(needed, m_squelches); // Make sure we don't connect to already-connected entries. for (auto const& s : slots_) { - auto const result (m_squelches.insert ( - s.second->remote_endpoint().address())); - if (! result.second) - m_squelches.touch (result.first); + auto const result( + m_squelches.insert(s.second->remote_endpoint().address())); + if (!result.second) + m_squelches.touch(result.first); } // 1. Use Fixed if: @@ -489,22 +492,22 @@ public: // ( There are eligible fixed addresses to try OR // Any outbound attempts are in progress) // - if (counts_.fixed_active() < fixed_.size ()) + if (counts_.fixed_active() < fixed_.size()) { - get_fixed (needed, h.list(), m_squelches); + get_fixed(needed, h.list(), m_squelches); - if (! h.list().empty ()) + if (!h.list().empty()) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic connect " << h.list().size() << " fixed"; + JLOG(m_journal.debug()) << beast::leftw(18) << "Logic connect " + << h.list().size() << " fixed"; return h.list(); } if (counts_.attempts() > 0) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic waiting on " << - counts_.attempts() << " attempts"; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic waiting on " + << counts_.attempts() << " attempts"; return none; } } @@ -512,8 +515,7 @@ public: // Only proceed if auto connect is enabled and we // have less than the desired number of outbound slots // - if (! config_.autoConnect || - counts_.out_active () >= counts_.out_max ()) + if (!config_.autoConnect || counts_.out_active() >= counts_.out_max()) return none; // 2. Use Livecache if: @@ -522,21 +524,21 @@ public: // { livecache_.hops.shuffle(); - handout (&h, (&h)+1, - livecache_.hops.rbegin(), - livecache_.hops.rend()); - if (! h.list().empty ()) + handout( + &h, (&h) + 1, livecache_.hops.rbegin(), livecache_.hops.rend()); + if (!h.list().empty()) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic connect " << h.list().size () << " live " << - ((h.list().size () > 1) ? "endpoints" : "endpoint"); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic connect " << h.list().size() + << " live " + << ((h.list().size() > 1) ? "endpoints" : "endpoint"); return h.list(); } else if (counts_.attempts() > 0) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic waiting on " << - counts_.attempts() << " attempts"; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic waiting on " + << counts_.attempts() << " attempts"; return none; } } @@ -550,22 +552,24 @@ public: unusedBootstrapIPs.count() == 0 OR activeNameResolutions.count() > 0) ) ForOneOrMore (DomainName that hasn't been resolved recently) - Contact DomainName and add entries to the unusedBootstrapIPs - return; + Contact DomainName and add entries to the + unusedBootstrapIPs return; */ // 4. Use Bootcache if: // There are any entries we haven't tried lately // - for (auto iter (bootcache_.begin()); - ! h.full() && iter != bootcache_.end(); ++iter) - h.try_insert (*iter); + for (auto iter(bootcache_.begin()); + !h.full() && iter != bootcache_.end(); + ++iter) + h.try_insert(*iter); - if (! h.list().empty ()) + if (!h.list().empty()) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic connect " << h.list().size () << " boot " << - ((h.list().size () > 1) ? "addresses" : "address"); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic connect " << h.list().size() + << " boot " + << ((h.list().size() > 1) ? "addresses" : "address"); return h.list(); } @@ -576,33 +580,36 @@ public: std::vector, std::vector>> buildEndpointsForPeers() { - std::vector, std::vector>> result; + std::vector, std::vector>> + result; std::lock_guard _(lock_); clock_type::time_point const now = m_clock.now(); if (m_whenBroadcast <= now) { - std::vector targets; + std::vector targets; { // build list of active slots - std::vector slots; - slots.reserve (slots_.size()); - std::for_each (slots_.cbegin(), slots_.cend(), - [&slots](Slots::value_type const& value) - { + std::vector slots; + slots.reserve(slots_.size()); + std::for_each( + slots_.cbegin(), + slots_.cend(), + [&slots](Slots::value_type const& value) { if (value.second->state() == Slot::active) - slots.emplace_back (value.second); + slots.emplace_back(value.second); }); - std::shuffle (slots.begin(), slots.end(), default_prng()); + std::shuffle(slots.begin(), slots.end(), default_prng()); // build target vector - targets.reserve (slots.size()); - std::for_each (slots.cbegin(), slots.cend(), - [&targets](SlotImp::ptr const& slot) - { - targets.emplace_back (slot); + targets.reserve(slots.size()); + std::for_each( + slots.cbegin(), + slots.cend(), + [&targets](SlotImp::ptr const& slot) { + targets.emplace_back(slot); }); } @@ -618,8 +625,7 @@ public: // 2. We have slots // 3. We haven't failed the firewalled test // - if (config_.wantIncoming && - counts_.inboundSlots() > 0) + if (config_.wantIncoming && counts_.inboundSlots() > 0) { Endpoint ep; ep.hops = 0; @@ -630,29 +636,30 @@ public: // is ignored, the type/version (ipv4 vs ipv6) doesn't matter // either. ipv6 has a slightly more compact string // representation of 0, so use that for self entries. - ep.address = beast::IP::Endpoint ( - beast::IP::AddressV6 ()).at_port ( - config_.listeningPort); + ep.address = beast::IP::Endpoint(beast::IP::AddressV6()) + .at_port(config_.listeningPort); for (auto& t : targets) - t.insert (ep); + t.insert(ep); } // build sequence of endpoints by hops livecache_.hops.shuffle(); - handout (targets.begin(), targets.end(), + handout( + targets.begin(), + targets.end(), livecache_.hops.begin(), - livecache_.hops.end()); + livecache_.hops.end()); // broadcast for (auto const& t : targets) { SlotImp::ptr const& slot = t.slot(); auto const& list = t.list(); - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic sending " << slot->remote_endpoint() << - " with " << list.size() << - ((list.size() == 1) ? " endpoint" : " endpoints"); - result.push_back (std::make_pair (slot, list)); + JLOG(m_journal.trace()) + << beast::leftw(18) << "Logic sending " + << slot->remote_endpoint() << " with " << list.size() + << ((list.size() == 1) ? " endpoint" : " endpoints"); + result.push_back(std::make_pair(slot, list)); } m_whenBroadcast = now + Tuning::secondsPerMessage; @@ -661,85 +668,85 @@ public: return result; } - void once_per_second() + void + once_per_second() { std::lock_guard _(lock_); // Expire the Livecache - livecache_.expire (); + livecache_.expire(); // Expire the recent cache in each slot for (auto const& entry : slots_) entry.second->expire(); // Expire the recent attempts table - beast::expire (m_squelches, - Tuning::recentAttemptDuration); + beast::expire(m_squelches, Tuning::recentAttemptDuration); - bootcache_.periodicActivity (); + bootcache_.periodicActivity(); } //-------------------------------------------------------------------------- // Validate and clean up the list that we received from the slot. - void preprocess (SlotImp::ptr const& slot, Endpoints& list) + void + preprocess(SlotImp::ptr const& slot, Endpoints& list) { - bool neighbor (false); + bool neighbor(false); for (auto iter = list.begin(); iter != list.end();) { - Endpoint& ep (*iter); + Endpoint& ep(*iter); // Enforce hop limit if (ep.hops > Tuning::maxHops) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Endpoints drop " << ep.address << - " for excess hops " << ep.hops; - iter = list.erase (iter); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Endpoints drop " << ep.address + << " for excess hops " << ep.hops; + iter = list.erase(iter); continue; } // See if we are directly connected if (ep.hops == 0) { - if (! neighbor) + if (!neighbor) { // Fill in our neighbors remote address neighbor = true; - ep.address = slot->remote_endpoint().at_port ( - ep.address.port ()); + ep.address = + slot->remote_endpoint().at_port(ep.address.port()); } else { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Endpoints drop " << ep.address << - " for extra self"; - iter = list.erase (iter); + JLOG(m_journal.debug()) + << beast::leftw(18) << "Endpoints drop " << ep.address + << " for extra self"; + iter = list.erase(iter); continue; } } // Discard invalid addresses - if (! is_valid_address (ep.address)) + if (!is_valid_address(ep.address)) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Endpoints drop " << ep.address << - " as invalid"; - iter = list.erase (iter); + JLOG(m_journal.debug()) << beast::leftw(18) << "Endpoints drop " + << ep.address << " as invalid"; + iter = list.erase(iter); continue; } // Filter duplicates - if (std::any_of (list.begin(), iter, - [ep](Endpoints::value_type const& other) - { - return ep.address == other.address; - })) + if (std::any_of( + list.begin(), + iter, + [ep](Endpoints::value_type const& other) { + return ep.address == other.address; + })) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Endpoints drop " << ep.address << - " as duplicate"; - iter = list.erase (iter); + JLOG(m_journal.debug()) << beast::leftw(18) << "Endpoints drop " + << ep.address << " as duplicate"; + iter = list.erase(iter); continue; } @@ -752,31 +759,31 @@ public: } } - void on_endpoints (SlotImp::ptr const& slot, Endpoints list) + void + on_endpoints(SlotImp::ptr const& slot, Endpoints list) { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Endpoints from " << slot->remote_endpoint () << - " contained " << list.size () << - ((list.size() > 1) ? " entries" : " entry"); + JLOG(m_journal.trace()) + << beast::leftw(18) << "Endpoints from " << slot->remote_endpoint() + << " contained " << list.size() + << ((list.size() > 1) ? " entries" : " entry"); std::lock_guard _(lock_); // The object must exist in our table - assert (slots_.find (slot->remote_endpoint ()) != - slots_.end ()); + assert(slots_.find(slot->remote_endpoint()) != slots_.end()); // Must be handshaked! - assert (slot->state() == Slot::active); + assert(slot->state() == Slot::active); - preprocess (slot, list); + preprocess(slot, list); - clock_type::time_point const now (m_clock.now()); + clock_type::time_point const now(m_clock.now()); for (auto const& ep : list) { - assert (ep.hops != 0); + assert(ep.hops != 0); - slot->recent.insert (ep.address, ep.hops); + slot->recent.insert(ep.address, ep.hops); // Note hops has been incremented, so 1 // means a directly connected neighbor. @@ -785,12 +792,13 @@ public: { if (slot->connectivityCheckInProgress) { - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic testing " << ep.address << " already in progress"; + JLOG(m_journal.debug()) + << beast::leftw(18) << "Logic testing " << ep.address + << " already in progress"; continue; } - if (! slot->checked) + if (!slot->checked) { // Mark that a check for this slot is now in progress. slot->connectivityCheckInProgress = true; @@ -798,9 +806,14 @@ public: // Test the slot's listening port before // adding it to the livecache for the first time. // - m_checker.async_connect (ep.address, std::bind ( - &Logic::checkComplete, this, slot->remote_endpoint(), - ep.address, std::placeholders::_1)); + m_checker.async_connect( + ep.address, + std::bind( + &Logic::checkComplete, + this, + slot->remote_endpoint(), + ep.address, + std::placeholders::_1)); // Note that we simply discard the first Endpoint // that the neighbor sends when we perform the @@ -811,7 +824,7 @@ public: } // If they failed the test then skip the address - if (! slot->canAccept) + if (!slot->canAccept) continue; } @@ -819,8 +832,8 @@ public: // listening test, else we silently drop their messsage // since their listening port is misconfigured. // - livecache_.insert (ep); - bootcache_.insert (ep.address); + livecache_.insert(ep); + bootcache_.insert(ep.address); } slot->whenAcceptEndpoints = now + Tuning::secondsPerMessage; @@ -828,7 +841,8 @@ public: //-------------------------------------------------------------------------- - void remove (SlotImp::ptr const& slot) + void + remove(SlotImp::ptr const& slot) { { auto const iter = slots_.find(slot->remote_endpoint()); @@ -838,93 +852,99 @@ public: slots_.erase(iter); } // Remove the key if present - if (slot->public_key () != boost::none) + if (slot->public_key() != boost::none) { - auto const iter = keys_.find (*slot->public_key()); + auto const iter = keys_.find(*slot->public_key()); // Key must exist - assert (iter != keys_.end ()); - keys_.erase (iter); + assert(iter != keys_.end()); + keys_.erase(iter); } // Remove from connected address table { - auto const iter (connectedAddresses_.find ( - slot->remote_endpoint().address())); + auto const iter( + connectedAddresses_.find(slot->remote_endpoint().address())); // Address must exist - assert (iter != connectedAddresses_.end ()); - connectedAddresses_.erase (iter); + assert(iter != connectedAddresses_.end()); + connectedAddresses_.erase(iter); } // Update counts - counts_.remove (*slot); + counts_.remove(*slot); } - void on_closed (SlotImp::ptr const& slot) + void + on_closed(SlotImp::ptr const& slot) { std::lock_guard _(lock_); - remove (slot); + remove(slot); // Mark fixed slot failure - if (slot->fixed() && ! slot->inbound() && slot->state() != Slot::active) + if (slot->fixed() && !slot->inbound() && slot->state() != Slot::active) { - auto iter (fixed_.find (slot->remote_endpoint())); - assert (iter != fixed_.end ()); - iter->second.failure (m_clock.now ()); - JLOG(m_journal.debug()) << beast::leftw (18) << - "Logic fixed " << slot->remote_endpoint () << " failed"; + auto iter(fixed_.find(slot->remote_endpoint())); + assert(iter != fixed_.end()); + iter->second.failure(m_clock.now()); + JLOG(m_journal.debug()) << beast::leftw(18) << "Logic fixed " + << slot->remote_endpoint() << " failed"; } // Do state specific bookkeeping switch (slot->state()) { - case Slot::accept: - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic accept " << slot->remote_endpoint () << " failed"; - break; + case Slot::accept: + JLOG(m_journal.trace()) << beast::leftw(18) << "Logic accept " + << slot->remote_endpoint() << " failed"; + break; - case Slot::connect: - case Slot::connected: - bootcache_.on_failure (slot->remote_endpoint ()); - // VFALCO TODO If the address exists in the ephemeral/live - // endpoint livecache then we should mark the failure - // as if it didn't pass the listening test. We should also - // avoid propagating the address. - break; + case Slot::connect: + case Slot::connected: + bootcache_.on_failure(slot->remote_endpoint()); + // VFALCO TODO If the address exists in the ephemeral/live + // endpoint livecache then we should mark the + // failure + // as if it didn't pass the listening test. We should also + // avoid propagating the address. + break; - case Slot::active: - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic close " << slot->remote_endpoint(); - break; + case Slot::active: + JLOG(m_journal.trace()) << beast::leftw(18) << "Logic close " + << slot->remote_endpoint(); + break; - case Slot::closing: - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic finished " << slot->remote_endpoint(); - break; + case Slot::closing: + JLOG(m_journal.trace()) << beast::leftw(18) << "Logic finished " + << slot->remote_endpoint(); + break; - default: - assert (false); - break; + default: + assert(false); + break; } } - void on_failure (SlotImp::ptr const& slot) + void + on_failure(SlotImp::ptr const& slot) { std::lock_guard _(lock_); - bootcache_.on_failure (slot->remote_endpoint ()); + bootcache_.on_failure(slot->remote_endpoint()); } // Insert a set of redirect IP addresses into the Bootcache template void - onRedirects (FwdIter first, FwdIter last, + onRedirects( + FwdIter first, + FwdIter last, boost::asio::ip::tcp::endpoint const& remote_address); //-------------------------------------------------------------------------- // Returns `true` if the address matches a fixed slot address // Must have the lock held - bool fixed (beast::IP::Endpoint const& endpoint) const + bool + fixed(beast::IP::Endpoint const& endpoint) const { for (auto const& entry : fixed_) if (entry.first == endpoint) @@ -935,10 +955,11 @@ public: // Returns `true` if the address matches a fixed slot address // Note that this does not use the port information in the IP::Endpoint // Must have the lock held - bool fixed (beast::IP::Address const& address) const + bool + fixed(beast::IP::Address const& address) const { for (auto const& entry : fixed_) - if (entry.first.address () == address) + if (entry.first.address() == address) return true; return false; } @@ -951,24 +972,27 @@ public: /** Adds eligible Fixed addresses for outbound attempts. */ template - void get_fixed (std::size_t needed, Container& c, + void + get_fixed( + std::size_t needed, + Container& c, typename ConnectHandouts::Squelches& squelches) { - auto const now (m_clock.now()); - for (auto iter = fixed_.begin (); - needed && iter != fixed_.end (); ++iter) + auto const now(m_clock.now()); + for (auto iter = fixed_.begin(); needed && iter != fixed_.end(); ++iter) { - auto const& address (iter->first.address()); - if (iter->second.when() <= now && squelches.find(address) == - squelches.end() && std::none_of ( - slots_.cbegin(), slots_.cend(), - [address](Slots::value_type const& v) - { + auto const& address(iter->first.address()); + if (iter->second.when() <= now && + squelches.find(address) == squelches.end() && + std::none_of( + slots_.cbegin(), + slots_.cend(), + [address](Slots::value_type const& v) { return address == v.first.address(); })) { squelches.insert(iter->first.address()); - c.push_back (iter->first); + c.push_back(iter->first); --needed; } } @@ -977,15 +1001,15 @@ public: //-------------------------------------------------------------------------- void - addStaticSource (std::shared_ptr const& source) + addStaticSource(std::shared_ptr const& source) { - fetch (source); + fetch(source); } void - addSource (std::shared_ptr const& source) + addSource(std::shared_ptr const& source) { - m_sources.push_back (source); + m_sources.push_back(source); } //-------------------------------------------------------------------------- @@ -997,13 +1021,14 @@ public: // Add a set of addresses. // Returns the number of addresses added. // - int addBootcacheAddresses (IPAddresses const& list) + int + addBootcacheAddresses(IPAddresses const& list) { - int count (0); + int count(0); std::lock_guard _(lock_); for (auto addr : list) { - if (bootcache_.insertStatic (addr)) + if (bootcache_.insertStatic(addr)) ++count; } return count; @@ -1011,7 +1036,7 @@ public: // Fetch bootcache addresses from the specified source. void - fetch (std::shared_ptr const& source) + fetch(std::shared_ptr const& source) { Source::Results results; @@ -1026,7 +1051,7 @@ public: // VFALCO NOTE The fetch is synchronous, // not sure if that's a good thing. // - source->fetch (results, m_journal); + source->fetch(results, m_journal); { std::lock_guard _(lock_); @@ -1036,19 +1061,19 @@ public: } } - if (! results.error) + if (!results.error) { - int const count (addBootcacheAddresses (results.addresses)); - JLOG(m_journal.info()) << beast::leftw (18) << - "Logic added " << count << - " new " << ((count == 1) ? "address" : "addresses") << - " from " << source->name(); + int const count(addBootcacheAddresses(results.addresses)); + JLOG(m_journal.info()) + << beast::leftw(18) << "Logic added " << count << " new " + << ((count == 1) ? "address" : "addresses") << " from " + << source->name(); } else { - JLOG(m_journal.error()) << beast::leftw (18) << - "Logic failed " << "'" << source->name() << "' fetch, " << - results.error.message(); + JLOG(m_journal.error()) << beast::leftw(18) << "Logic failed " + << "'" << source->name() << "' fetch, " + << results.error.message(); } } @@ -1059,11 +1084,12 @@ public: //-------------------------------------------------------------------------- // Returns true if the IP::Endpoint contains no invalid data. - bool is_valid_address (beast::IP::Endpoint const& address) + bool + is_valid_address(beast::IP::Endpoint const& address) { - if (is_unspecified (address)) + if (is_unspecified(address)) return false; - if (! is_public (address)) + if (!is_public(address)) return false; if (address.port() == 0) return false; @@ -1076,59 +1102,61 @@ public: // //-------------------------------------------------------------------------- - void writeSlots (beast::PropertyStream::Set& set, Slots const& slots) + void + writeSlots(beast::PropertyStream::Set& set, Slots const& slots) { for (auto const& entry : slots) { - beast::PropertyStream::Map item (set); - SlotImp const& slot (*entry.second); - if (slot.local_endpoint () != boost::none) - item ["local_address"] = to_string (*slot.local_endpoint ()); - item ["remote_address"] = to_string (slot.remote_endpoint ()); + beast::PropertyStream::Map item(set); + SlotImp const& slot(*entry.second); + if (slot.local_endpoint() != boost::none) + item["local_address"] = to_string(*slot.local_endpoint()); + item["remote_address"] = to_string(slot.remote_endpoint()); if (slot.inbound()) - item ["inbound"] = "yes"; + item["inbound"] = "yes"; if (slot.fixed()) - item ["fixed"] = "yes"; + item["fixed"] = "yes"; if (slot.reserved()) - item ["reserved"] = "yes"; + item["reserved"] = "yes"; - item ["state"] = stateString (slot.state()); + item["state"] = stateString(slot.state()); } } - void onWrite (beast::PropertyStream::Map& map) + void + onWrite(beast::PropertyStream::Map& map) { std::lock_guard _(lock_); // VFALCO NOTE These ugly casts are needed because // of how std::size_t is declared on some linuxes // - map ["bootcache"] = std::uint32_t (bootcache_.size()); - map ["fixed"] = std::uint32_t (fixed_.size()); + map["bootcache"] = std::uint32_t(bootcache_.size()); + map["fixed"] = std::uint32_t(fixed_.size()); { - beast::PropertyStream::Set child ("peers", map); - writeSlots (child, slots_); + beast::PropertyStream::Set child("peers", map); + writeSlots(child, slots_); } { - beast::PropertyStream::Map child ("counts", map); - counts_.onWrite (child); + beast::PropertyStream::Map child("counts", map); + counts_.onWrite(child); } { - beast::PropertyStream::Map child ("config", map); - config_.onWrite (child); + beast::PropertyStream::Map child("config", map); + config_.onWrite(child); } { - beast::PropertyStream::Map child ("livecache", map); - livecache_.onWrite (child); + beast::PropertyStream::Map child("livecache", map); + livecache_.onWrite(child); } { - beast::PropertyStream::Map child ("bootcache", map); - bootcache_.onWrite (child); + beast::PropertyStream::Map child("bootcache", map); + bootcache_.onWrite(child); } } @@ -1138,22 +1166,29 @@ public: // //-------------------------------------------------------------------------- - Counts const& counts () const + Counts const& + counts() const { return counts_; } - static std::string stateString (Slot::State state) + static std::string + stateString(Slot::State state) { switch (state) { - case Slot::accept: return "accept"; - case Slot::connect: return "connect"; - case Slot::connected: return "connected"; - case Slot::active: return "active"; - case Slot::closing: return "closing"; - default: - break; + case Slot::accept: + return "accept"; + case Slot::connect: + return "connect"; + case Slot::connected: + return "connected"; + case Slot::active: + return "active"; + case Slot::closing: + return "closing"; + default: + break; }; return "?"; } @@ -1164,22 +1199,23 @@ public: template template void -Logic::onRedirects (FwdIter first, FwdIter last, +Logic::onRedirects( + FwdIter first, + FwdIter last, boost::asio::ip::tcp::endpoint const& remote_address) { std::lock_guard _(lock_); std::size_t n = 0; - for(;first != last && n < Tuning::maxRedirects; ++first, ++n) - bootcache_.insert( - beast::IPAddressConversion::from_asio(*first)); + for (; first != last && n < Tuning::maxRedirects; ++first, ++n) + bootcache_.insert(beast::IPAddressConversion::from_asio(*first)); if (n > 0) { - JLOG(m_journal.trace()) << beast::leftw (18) << - "Logic add " << n << " redirect IPs from " << remote_address; + JLOG(m_journal.trace()) << beast::leftw(18) << "Logic add " << n + << " redirect IPs from " << remote_address; } } -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/PeerfinderConfig.cpp b/src/ripple/peerfinder/impl/PeerfinderConfig.cpp index 76e69c39e4..3cd41e24f2 100644 --- a/src/ripple/peerfinder/impl/PeerfinderConfig.cpp +++ b/src/ripple/peerfinder/impl/PeerfinderConfig.cpp @@ -24,27 +24,28 @@ namespace ripple { namespace PeerFinder { Config::Config() - : maxPeers (Tuning::defaultMaxPeers) - , outPeers (calcOutPeers ()) - , wantIncoming (true) - , autoConnect (true) - , listeningPort (0) - , ipLimit (0) + : maxPeers(Tuning::defaultMaxPeers) + , outPeers(calcOutPeers()) + , wantIncoming(true) + , autoConnect(true) + , listeningPort(0) + , ipLimit(0) { } -double Config::calcOutPeers () const +double +Config::calcOutPeers() const { - return std::max ( - maxPeers * Tuning::outPercent * 0.01, - double (Tuning::minOutCount)); + return std::max( + maxPeers * Tuning::outPercent * 0.01, double(Tuning::minOutCount)); } -void Config::applyTuning () +void +Config::applyTuning() { if (maxPeers < Tuning::minOutCount) maxPeers = Tuning::minOutCount; - outPeers = calcOutPeers (); + outPeers = calcOutPeers(); auto const inPeers = maxPeers - outPeers; @@ -56,26 +57,26 @@ void Config::applyTuning () ipLimit = 2; if (inPeers > Tuning::defaultMaxPeers) - ipLimit += std::min(5, - static_cast(inPeers / Tuning::defaultMaxPeers)); + ipLimit += std::min( + 5, static_cast(inPeers / Tuning::defaultMaxPeers)); } // We don't allow a single IP to consume all incoming slots, // unless we only have one incoming slot available. - ipLimit = std::max(1, - std::min(ipLimit, static_cast(inPeers / 2))); + ipLimit = std::max(1, std::min(ipLimit, static_cast(inPeers / 2))); } -void Config::onWrite (beast::PropertyStream::Map &map) +void +Config::onWrite(beast::PropertyStream::Map& map) { - map ["max_peers"] = maxPeers; - map ["out_peers"] = outPeers; - map ["want_incoming"] = wantIncoming; - map ["auto_connect"] = autoConnect; - map ["port"] = listeningPort; - map ["features"] = features; - map ["ip_limit"] = ipLimit; + map["max_peers"] = maxPeers; + map["out_peers"] = outPeers; + map["want_incoming"] = wantIncoming; + map["auto_connect"] = autoConnect; + map["port"] = listeningPort; + map["features"] = features; + map["ip_limit"] = ipLimit; } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/PeerfinderManager.cpp b/src/ripple/peerfinder/impl/PeerfinderManager.cpp index 80a1ef3255..f5310e8c7d 100644 --- a/src/ripple/peerfinder/impl/PeerfinderManager.cpp +++ b/src/ripple/peerfinder/impl/PeerfinderManager.cpp @@ -17,12 +17,12 @@ */ //============================================================================== +#include #include #include #include #include #include -#include #include #include #include @@ -32,38 +32,37 @@ namespace ripple { namespace PeerFinder { -class ManagerImp - : public Manager +class ManagerImp : public Manager { public: - boost::asio::io_service &io_service_; - boost::optional work_; + boost::asio::io_service& io_service_; + boost::optional work_; clock_type& m_clock; beast::Journal m_journal; StoreSqdb m_store; Checker checker_; - Logic m_logic; + Logic m_logic; SociConfig m_sociConfig; //-------------------------------------------------------------------------- - ManagerImp ( + ManagerImp( Stoppable& stoppable, boost::asio::io_service& io_service, clock_type& clock, beast::Journal journal, BasicConfig const& config, beast::insight::Collector::ptr const& collector) - : Manager (stoppable) + : Manager(stoppable) , io_service_(io_service) , work_(boost::in_place(std::ref(io_service_))) - , m_clock (clock) - , m_journal (journal) - , m_store (journal) - , checker_ (io_service_) - , m_logic (clock, m_store, checker_, journal) - , m_sociConfig (config, "peerfinder") - , m_stats(std::bind(&ManagerImp::collect_metrics, this),collector) + , m_clock(clock) + , m_journal(journal) + , m_store(journal) + , checker_(io_service_) + , m_logic(clock, m_store, checker_, journal) + , m_sociConfig(config, "peerfinder") + , m_stats(std::bind(&ManagerImp::collect_metrics, this), collector) { } @@ -89,9 +88,10 @@ public: // //-------------------------------------------------------------------------- - void setConfig (Config const& config) override + void + setConfig(Config const& config) override { - m_logic.config (config); + m_logic.config(config); } Config @@ -100,21 +100,24 @@ public: return m_logic.config(); } - void addFixedPeer (std::string const& name, - std::vector const& addresses) override + void + addFixedPeer( + std::string const& name, + std::vector const& addresses) override { - m_logic.addFixedPeer (name, addresses); + m_logic.addFixedPeer(name, addresses); } void - addFallbackStrings (std::string const& name, - std::vector const& strings) override + addFallbackStrings( + std::string const& name, + std::vector const& strings) override { - m_logic.addStaticSource (SourceStrings::New (name, strings)); + m_logic.addStaticSource(SourceStrings::New(name, strings)); } - void addFallbackURL (std::string const& name, - std::string const& url) + void + addFallbackURL(std::string const& name, std::string const& url) { // VFALCO TODO This needs to be implemented } @@ -122,43 +125,44 @@ public: //-------------------------------------------------------------------------- std::shared_ptr - new_inbound_slot ( + new_inbound_slot( beast::IP::Endpoint const& local_endpoint, - beast::IP::Endpoint const& remote_endpoint) override + beast::IP::Endpoint const& remote_endpoint) override { - return m_logic.new_inbound_slot (local_endpoint, remote_endpoint); + return m_logic.new_inbound_slot(local_endpoint, remote_endpoint); } std::shared_ptr - new_outbound_slot (beast::IP::Endpoint const& remote_endpoint) override + new_outbound_slot(beast::IP::Endpoint const& remote_endpoint) override { - return m_logic.new_outbound_slot (remote_endpoint); + return m_logic.new_outbound_slot(remote_endpoint); } void - on_endpoints (std::shared_ptr const& slot, - Endpoints const& endpoints) override + on_endpoints(std::shared_ptr const& slot, Endpoints const& endpoints) + override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - m_logic.on_endpoints (impl, endpoints); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + m_logic.on_endpoints(impl, endpoints); } void - on_closed (std::shared_ptr const& slot) override + on_closed(std::shared_ptr const& slot) override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - m_logic.on_closed (impl); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + m_logic.on_closed(impl); } void - on_failure (std::shared_ptr const& slot) override + on_failure(std::shared_ptr const& slot) override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - m_logic.on_failure (impl); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + m_logic.on_failure(impl); } void - onRedirects (boost::asio::ip::tcp::endpoint const& remote_address, + onRedirects( + boost::asio::ip::tcp::endpoint const& remote_address, std::vector const& eps) override { m_logic.onRedirects(eps.begin(), eps.end(), remote_address); @@ -167,29 +171,32 @@ public: //-------------------------------------------------------------------------- bool - onConnected (std::shared_ptr const& slot, + onConnected( + std::shared_ptr const& slot, beast::IP::Endpoint const& local_endpoint) override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - return m_logic.onConnected (impl, local_endpoint); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + return m_logic.onConnected(impl, local_endpoint); } Result - activate (std::shared_ptr const& slot, - PublicKey const& key, bool reserved) override + activate( + std::shared_ptr const& slot, + PublicKey const& key, + bool reserved) override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - return m_logic.activate (impl, key, reserved); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + return m_logic.activate(impl, key, reserved); } - std::vector - redirect (std::shared_ptr const& slot) override + std::vector + redirect(std::shared_ptr const& slot) override { - SlotImp::ptr impl (std::dynamic_pointer_cast (slot)); - return m_logic.redirect (impl); + SlotImp::ptr impl(std::dynamic_pointer_cast(slot)); + return m_logic.redirect(impl); } - std::vector + std::vector autoconnect() override { return m_logic.autoconnect(); @@ -214,10 +221,10 @@ public: //-------------------------------------------------------------------------- void - onPrepare () override + onPrepare() override { - m_store.open (m_sociConfig); - m_logic.load (); + m_store.open(m_sociConfig); + m_logic.load(); } void @@ -225,7 +232,8 @@ public: { } - void onStop () override + void + onStop() override { close(); stopped(); @@ -237,35 +245,39 @@ public: // //-------------------------------------------------------------------------- - void onWrite (beast::PropertyStream::Map& map) override + void + onWrite(beast::PropertyStream::Map& map) override { - m_logic.onWrite (map); + m_logic.onWrite(map); } private: struct Stats { - template - Stats ( - Handler const& handler, - beast::insight::Collector::ptr const& collector) - : hook (collector->make_hook (handler)) - , activeInboundPeers(collector->make_gauge("Peer_Finder","Active_Inbound_Peers")) - , activeOutboundPeers(collector->make_gauge("Peer_Finder","Active_Outbound_Peers")) - { - } - + Stats( + Handler const& handler, + beast::insight::Collector::ptr const& collector) + : hook(collector->make_hook(handler)) + , activeInboundPeers( + collector->make_gauge("Peer_Finder", "Active_Inbound_Peers")) + , activeOutboundPeers( + collector->make_gauge("Peer_Finder", "Active_Outbound_Peers")) + { + } + beast::insight::Hook hook; beast::insight::Gauge activeInboundPeers; beast::insight::Gauge activeOutboundPeers; }; - + std::mutex m_statsMutex; Stats m_stats; - - void collect_metrics(){ - std::lock_guard lock (m_statsMutex); + + void + collect_metrics() + { + std::lock_guard lock(m_statsMutex); m_stats.activeInboundPeers = m_logic.counts_.inboundActive(); m_stats.activeOutboundPeers = m_logic.counts_.out_active(); } @@ -273,19 +285,24 @@ private: //------------------------------------------------------------------------------ -Manager::Manager (Stoppable& parent) - : Stoppable ("PeerFinder", parent) - , beast::PropertyStream::Source ("peerfinder") +Manager::Manager(Stoppable& parent) + : Stoppable("PeerFinder", parent) + , beast::PropertyStream::Source("peerfinder") { } std::unique_ptr -make_Manager (Stoppable& parent, boost::asio::io_service& io_service, - clock_type& clock, beast::Journal journal, BasicConfig const& config, beast::insight::Collector::ptr const& collector) +make_Manager( + Stoppable& parent, + boost::asio::io_service& io_service, + clock_type& clock, + beast::Journal journal, + BasicConfig const& config, + beast::insight::Collector::ptr const& collector) { - return std::make_unique ( + return std::make_unique( parent, io_service, clock, journal, config, collector); } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/Reporting.h b/src/ripple/peerfinder/impl/Reporting.h index 0d0df57bc5..25c36ea3f2 100644 --- a/src/ripple/peerfinder/impl/Reporting.h +++ b/src/ripple/peerfinder/impl/Reporting.h @@ -43,7 +43,7 @@ struct Reporting static bool const dump_nodes = false; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/SlotImp.cpp b/src/ripple/peerfinder/impl/SlotImp.cpp index e8411ec9ed..8feec58ce7 100644 --- a/src/ripple/peerfinder/impl/SlotImp.cpp +++ b/src/ripple/peerfinder/impl/SlotImp.cpp @@ -17,71 +17,75 @@ */ //============================================================================== -#include #include +#include #include namespace ripple { namespace PeerFinder { -SlotImp::SlotImp (beast::IP::Endpoint const& local_endpoint, - beast::IP::Endpoint const& remote_endpoint, bool fixed, - clock_type& clock) - : recent (clock) - , m_inbound (true) - , m_fixed (fixed) - , m_reserved (false) - , m_state (accept) - , m_remote_endpoint (remote_endpoint) - , m_local_endpoint (local_endpoint) - , m_listening_port (unknownPort) - , checked (false) - , canAccept (false) - , connectivityCheckInProgress (false) +SlotImp::SlotImp( + beast::IP::Endpoint const& local_endpoint, + beast::IP::Endpoint const& remote_endpoint, + bool fixed, + clock_type& clock) + : recent(clock) + , m_inbound(true) + , m_fixed(fixed) + , m_reserved(false) + , m_state(accept) + , m_remote_endpoint(remote_endpoint) + , m_local_endpoint(local_endpoint) + , m_listening_port(unknownPort) + , checked(false) + , canAccept(false) + , connectivityCheckInProgress(false) { } -SlotImp::SlotImp (beast::IP::Endpoint const& remote_endpoint, - bool fixed, clock_type& clock) - : recent (clock) - , m_inbound (false) - , m_fixed (fixed) - , m_reserved (false) - , m_state (connect) - , m_remote_endpoint (remote_endpoint) - , m_listening_port (unknownPort) - , checked (true) - , canAccept (true) - , connectivityCheckInProgress (false) +SlotImp::SlotImp( + beast::IP::Endpoint const& remote_endpoint, + bool fixed, + clock_type& clock) + : recent(clock) + , m_inbound(false) + , m_fixed(fixed) + , m_reserved(false) + , m_state(connect) + , m_remote_endpoint(remote_endpoint) + , m_listening_port(unknownPort) + , checked(true) + , canAccept(true) + , connectivityCheckInProgress(false) { } void -SlotImp::state (State state_) +SlotImp::state(State state_) { // Must go through activate() to set active state - assert (state_ != active); + assert(state_ != active); // The state must be different - assert (state_ != m_state); + assert(state_ != m_state); // You can't transition into the initial states - assert (state_ != accept && state_ != connect); + assert(state_ != accept && state_ != connect); // Can only become connected from outbound connect state - assert (state_ != connected || (! m_inbound && m_state == connect)); + assert(state_ != connected || (!m_inbound && m_state == connect)); // Can't gracefully close on an outbound connection attempt - assert (state_ != closing || m_state != connect); + assert(state_ != closing || m_state != connect); m_state = state_; } void -SlotImp::activate (clock_type::time_point const& now) +SlotImp::activate(clock_type::time_point const& now) { // Can only become active from the accept or connected state - assert (m_state == accept || m_state == connected); + assert(m_state == accept || m_state == connected); m_state = active; whenAcceptEndpoints = now; @@ -93,30 +97,29 @@ Slot::~Slot() = default; //------------------------------------------------------------------------------ -SlotImp::recent_t::recent_t (clock_type& clock) - : cache (clock) +SlotImp::recent_t::recent_t(clock_type& clock) : cache(clock) { } void -SlotImp::recent_t::insert (beast::IP::Endpoint const& ep, int hops) +SlotImp::recent_t::insert(beast::IP::Endpoint const& ep, int hops) { - auto const result (cache.emplace (ep, hops)); - if (! result.second) + auto const result(cache.emplace(ep, hops)); + if (!result.second) { // NOTE Other logic depends on this <= inequality. if (hops <= result.first->second) { result.first->second = hops; - cache.touch (result.first); + cache.touch(result.first); } } } bool -SlotImp::recent_t::filter (beast::IP::Endpoint const& ep, int hops) +SlotImp::recent_t::filter(beast::IP::Endpoint const& ep, int hops) { - auto const iter (cache.find (ep)); + auto const iter(cache.find(ep)); if (iter == cache.end()) return false; // We avoid sending an endpoint if we heard it @@ -126,11 +129,10 @@ SlotImp::recent_t::filter (beast::IP::Endpoint const& ep, int hops) } void -SlotImp::recent_t::expire () +SlotImp::recent_t::expire() { - beast::expire (cache, - Tuning::liveCacheSecondsToLive); + beast::expire(cache, Tuning::liveCacheSecondsToLive); } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/SlotImp.h b/src/ripple/peerfinder/impl/SlotImp.h index 39e652d3ab..91e1b83c30 100644 --- a/src/ripple/peerfinder/impl/SlotImp.h +++ b/src/ripple/peerfinder/impl/SlotImp.h @@ -20,10 +20,10 @@ #ifndef RIPPLE_PEERFINDER_SLOTIMP_H_INCLUDED #define RIPPLE_PEERFINDER_SLOTIMP_H_INCLUDED -#include -#include -#include #include +#include +#include +#include #include #include @@ -33,56 +33,68 @@ namespace PeerFinder { class SlotImp : public Slot { private: - using recent_type = beast::aged_unordered_map ; + using recent_type = beast::aged_unordered_map; public: - using ptr = std::shared_ptr ; + using ptr = std::shared_ptr; // inbound - SlotImp (beast::IP::Endpoint const& local_endpoint, - beast::IP::Endpoint const& remote_endpoint, bool fixed, - clock_type& clock); + SlotImp( + beast::IP::Endpoint const& local_endpoint, + beast::IP::Endpoint const& remote_endpoint, + bool fixed, + clock_type& clock); // outbound - SlotImp (beast::IP::Endpoint const& remote_endpoint, - bool fixed, clock_type& clock); + SlotImp( + beast::IP::Endpoint const& remote_endpoint, + bool fixed, + clock_type& clock); - bool inbound () const override + bool + inbound() const override { return m_inbound; } - bool fixed () const override + bool + fixed() const override { return m_fixed; } - bool reserved () const override + bool + reserved() const override { return m_reserved; } - State state () const override + State + state() const override { return m_state; } - beast::IP::Endpoint const& remote_endpoint () const override + beast::IP::Endpoint const& + remote_endpoint() const override { return m_remote_endpoint; } - boost::optional const& local_endpoint () const override + boost::optional const& + local_endpoint() const override { return m_local_endpoint; } - boost::optional const& public_key () const override + boost::optional const& + public_key() const override { return m_public_key; } - boost::optional listening_port () const override + boost::optional + listening_port() const override { std::uint32_t const value = m_listening_port; if (value == unknownPort) @@ -90,36 +102,43 @@ public: return value; } - void set_listening_port (std::uint16_t port) + void + set_listening_port(std::uint16_t port) { m_listening_port = port; } - void local_endpoint (beast::IP::Endpoint const& endpoint) + void + local_endpoint(beast::IP::Endpoint const& endpoint) { m_local_endpoint = endpoint; } - void remote_endpoint (beast::IP::Endpoint const& endpoint) + void + remote_endpoint(beast::IP::Endpoint const& endpoint) { m_remote_endpoint = endpoint; } - void public_key (PublicKey const& key) + void + public_key(PublicKey const& key) { m_public_key = key; } - void reserved (bool reserved_) + void + reserved(bool reserved_) { m_reserved = reserved_; } //-------------------------------------------------------------------------- - void state (State state_); + void + state(State state_); - void activate (clock_type::time_point const& now); + void + activate(clock_type::time_point const& now); // "Memberspace" // @@ -129,25 +148,29 @@ public: class recent_t { public: - explicit recent_t (clock_type& clock); + explicit recent_t(clock_type& clock); /** Called for each valid endpoint received for a slot. We also insert messages that we send to the slot to prevent sending a slot the same address too frequently. */ - void insert (beast::IP::Endpoint const& ep, int hops); + void + insert(beast::IP::Endpoint const& ep, int hops); /** Returns `true` if we should not send endpoint to the slot. */ - bool filter (beast::IP::Endpoint const& ep, int hops); + bool + filter(beast::IP::Endpoint const& ep, int hops); private: - void expire (); + void + expire(); friend class SlotImp; recent_type cache; } recent; - void expire() + void + expire() { recent.expire(); } @@ -158,11 +181,11 @@ private: bool m_reserved; State m_state; beast::IP::Endpoint m_remote_endpoint; - boost::optional m_local_endpoint; - boost::optional m_public_key; + boost::optional m_local_endpoint; + boost::optional m_public_key; static std::int32_t constexpr unknownPort = -1; - std::atomic m_listening_port; + std::atomic m_listening_port; public: // DEPRECATED public data members @@ -186,7 +209,7 @@ public: clock_type::time_point whenAcceptEndpoints; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Source.h b/src/ripple/peerfinder/impl/Source.h index cf7c4ea205..5c14843814 100644 --- a/src/ripple/peerfinder/impl/Source.h +++ b/src/ripple/peerfinder/impl/Source.h @@ -49,13 +49,20 @@ public: IPAddresses addresses; }; - virtual ~Source () { } - virtual std::string const& name () = 0; - virtual void cancel () { } - virtual void fetch (Results& results, beast::Journal journal) = 0; + virtual ~Source() + { + } + virtual std::string const& + name() = 0; + virtual void + cancel() + { + } + virtual void + fetch(Results& results, beast::Journal journal) = 0; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/SourceStrings.cpp b/src/ripple/peerfinder/impl/SourceStrings.cpp index 5a369c317e..4e5db79c75 100644 --- a/src/ripple/peerfinder/impl/SourceStrings.cpp +++ b/src/ripple/peerfinder/impl/SourceStrings.cpp @@ -25,30 +25,32 @@ namespace PeerFinder { class SourceStringsImp : public SourceStrings { public: - SourceStringsImp (std::string const& name, Strings const& strings) - : m_name (name) - , m_strings (strings) + SourceStringsImp(std::string const& name, Strings const& strings) + : m_name(name), m_strings(strings) { } ~SourceStringsImp() = default; - std::string const& name () override + std::string const& + name() override { return m_name; } - void fetch (Results& results, beast::Journal journal) override + void + fetch(Results& results, beast::Journal journal) override { - results.addresses.resize (0); - results.addresses.reserve (m_strings.size()); - for (int i = 0; i < m_strings.size (); ++i) + results.addresses.resize(0); + results.addresses.reserve(m_strings.size()); + for (int i = 0; i < m_strings.size(); ++i) { - beast::IP::Endpoint ep (beast::IP::Endpoint::from_string (m_strings [i])); - if (is_unspecified (ep)) - ep = beast::IP::Endpoint::from_string (m_strings [i]); - if (! is_unspecified (ep)) - results.addresses.push_back (ep); + beast::IP::Endpoint ep( + beast::IP::Endpoint::from_string(m_strings[i])); + if (is_unspecified(ep)) + ep = beast::IP::Endpoint::from_string(m_strings[i]); + if (!is_unspecified(ep)) + results.addresses.push_back(ep); } } @@ -60,10 +62,10 @@ private: //------------------------------------------------------------------------------ std::shared_ptr -SourceStrings::New (std::string const& name, Strings const& strings) +SourceStrings::New(std::string const& name, Strings const& strings) { - return std::make_shared (name, strings); + return std::make_shared(name, strings); } -} -} +} // namespace PeerFinder +} // namespace ripple diff --git a/src/ripple/peerfinder/impl/SourceStrings.h b/src/ripple/peerfinder/impl/SourceStrings.h index 147d88e4e7..5d67d0887f 100644 --- a/src/ripple/peerfinder/impl/SourceStrings.h +++ b/src/ripple/peerfinder/impl/SourceStrings.h @@ -32,13 +32,13 @@ class SourceStrings : public Source public: explicit SourceStrings() = default; - using Strings = std::vector ; + using Strings = std::vector; static std::shared_ptr - New (std::string const& name, Strings const& strings); + New(std::string const& name, Strings const& strings); }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Store.h b/src/ripple/peerfinder/impl/Store.h index b12f5735df..f0c033c7a6 100644 --- a/src/ripple/peerfinder/impl/Store.h +++ b/src/ripple/peerfinder/impl/Store.h @@ -27,11 +27,14 @@ namespace PeerFinder { class Store { public: - virtual ~Store () { } + virtual ~Store() + { + } // load the bootstrap cache - using load_callback = std::function ; - virtual std::size_t load (load_callback const& cb) = 0; + using load_callback = std::function; + virtual std::size_t + load(load_callback const& cb) = 0; // save the bootstrap cache struct Entry @@ -41,10 +44,11 @@ public: beast::IP::Endpoint endpoint; int valence; }; - virtual void save (std::vector const& v) = 0; + virtual void + save(std::vector const& v) = 0; }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/StoreSqdb.h b/src/ripple/peerfinder/impl/StoreSqdb.h index 12a40568b4..7b687a1515 100644 --- a/src/ripple/peerfinder/impl/StoreSqdb.h +++ b/src/ripple/peerfinder/impl/StoreSqdb.h @@ -28,71 +28,71 @@ namespace ripple { namespace PeerFinder { /** Database persistence for PeerFinder using SQLite */ -class StoreSqdb - : public Store +class StoreSqdb : public Store { private: beast::Journal m_journal; soci::session m_session; + public: - enum - { + enum { // This determines the on-database format of the data currentSchemaVersion = 4 }; - explicit StoreSqdb (beast::Journal journal = - beast::Journal {beast::Journal::getNullSink()}) - : m_journal (journal) + explicit StoreSqdb( + beast::Journal journal = beast::Journal{beast::Journal::getNullSink()}) + : m_journal(journal) { } - ~StoreSqdb () + ~StoreSqdb() { } - void open (SociConfig const& sociConfig) + void + open(SociConfig const& sociConfig) { - sociConfig.open (m_session); + sociConfig.open(m_session); - JLOG(m_journal.info()) << - "Opening database at '" << sociConfig.connectionString () << "'"; + JLOG(m_journal.info()) + << "Opening database at '" << sociConfig.connectionString() << "'"; - init (); - update (); + init(); + update(); } // Loads the bootstrap cache, calling the callback for each entry // - std::size_t load (load_callback const& cb) override + std::size_t + load(load_callback const& cb) override { - std::size_t n (0); + std::size_t n(0); std::string s; int valence; - soci::statement st = (m_session.prepare << - "SELECT " - " address, " - " valence " - "FROM PeerFinder_BootstrapCache;" - , soci::into (s) - , soci::into (valence) - ); + soci::statement st = + (m_session.prepare << "SELECT " + " address, " + " valence " + "FROM PeerFinder_BootstrapCache;", + soci::into(s), + soci::into(valence)); - st.execute (); - while (st.fetch ()) + st.execute(); + while (st.fetch()) { - beast::IP::Endpoint const endpoint ( - beast::IP::Endpoint::from_string (s)); + beast::IP::Endpoint const endpoint( + beast::IP::Endpoint::from_string(s)); - if (!is_unspecified (endpoint)) + if (!is_unspecified(endpoint)) { - cb (endpoint, valence); + cb(endpoint, valence); ++n; } else { - JLOG(m_journal.error()) << - "Bad address string '" << s << "' in Bootcache table"; + JLOG(m_journal.error()) + << "Bad address string '" << s << "' in Bootcache table"; } } return n; @@ -100,71 +100,68 @@ public: // Overwrites the stored bootstrap cache with the specified array. // - void save (std::vector const& v) override + void + save(std::vector const& v) override { - soci::transaction tr (m_session); - m_session << - "DELETE FROM PeerFinder_BootstrapCache;"; + soci::transaction tr(m_session); + m_session << "DELETE FROM PeerFinder_BootstrapCache;"; - if (!v.empty ()) + if (!v.empty()) { std::vector s; std::vector valence; - s.reserve (v.size ()); - valence.reserve (v.size ()); + s.reserve(v.size()); + valence.reserve(v.size()); for (auto const& e : v) { - s.emplace_back (to_string (e.endpoint)); - valence.emplace_back (e.valence); + s.emplace_back(to_string(e.endpoint)); + valence.emplace_back(e.valence); } - m_session << - "INSERT INTO PeerFinder_BootstrapCache ( " - " address, " - " valence " - ") VALUES ( " - " :s, :valence " - ");" - , soci::use (s) - , soci::use (valence); + m_session << "INSERT INTO PeerFinder_BootstrapCache ( " + " address, " + " valence " + ") VALUES ( " + " :s, :valence " + ");", + soci::use(s), soci::use(valence); } - tr.commit (); + tr.commit(); } // Convert any existing entries from an older schema to the // current one, if appropriate. - void update () + void + update() { - soci::transaction tr (m_session); + soci::transaction tr(m_session); // get version - int version (0); + int version(0); { boost::optional vO; - m_session << - "SELECT " - " version " - "FROM SchemaVersion WHERE " - " name = 'PeerFinder';" - , soci::into (vO) - ; + m_session << "SELECT " + " version " + "FROM SchemaVersion WHERE " + " name = 'PeerFinder';", + soci::into(vO); - version = vO.value_or (0); + version = vO.value_or(0); - JLOG(m_journal.info()) << - "Opened version " << version << " database"; + JLOG(m_journal.info()) + << "Opened version " << version << " database"; } { if (version < currentSchemaVersion) { - JLOG(m_journal.info()) << - "Updating database to version " << currentSchemaVersion; + JLOG(m_journal.info()) + << "Updating database to version " << currentSchemaVersion; } else if (version > currentSchemaVersion) { - Throw ( + Throw( "The PeerFinder database version is higher than expected"); } } @@ -175,105 +172,90 @@ public: // Remove the "uptime" column from the bootstrap table // - m_session << - "CREATE TABLE IF NOT EXISTS PeerFinder_BootstrapCache_Next ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT, " - " address TEXT UNIQUE NOT NULL, " - " valence INTEGER" - ");" - ; + m_session << "CREATE TABLE IF NOT EXISTS " + "PeerFinder_BootstrapCache_Next ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT, " + " address TEXT UNIQUE NOT NULL, " + " valence INTEGER" + ");"; - m_session << - "CREATE INDEX IF NOT EXISTS " - " PeerFinder_BootstrapCache_Next_Index ON " - " PeerFinder_BootstrapCache_Next " - " ( address ); " - ; + m_session << "CREATE INDEX IF NOT EXISTS " + " PeerFinder_BootstrapCache_Next_Index ON " + " PeerFinder_BootstrapCache_Next " + " ( address ); "; std::size_t count; - m_session << - "SELECT COUNT(*) FROM PeerFinder_BootstrapCache;" - , soci::into (count) - ; + m_session << "SELECT COUNT(*) FROM PeerFinder_BootstrapCache;", + soci::into(count); - std::vector list; + std::vector list; { - list.reserve (count); + list.reserve(count); std::string s; int valence; - soci::statement st = (m_session.prepare << - "SELECT " - " address, " - " valence " - "FROM PeerFinder_BootstrapCache;" - , soci::into (s) - , soci::into (valence) - ); + soci::statement st = + (m_session.prepare << "SELECT " + " address, " + " valence " + "FROM PeerFinder_BootstrapCache;", + soci::into(s), + soci::into(valence)); - st.execute (); - while (st.fetch ()) + st.execute(); + while (st.fetch()) { Store::Entry entry; - entry.endpoint = beast::IP::Endpoint::from_string (s); - if (!is_unspecified (entry.endpoint)) + entry.endpoint = beast::IP::Endpoint::from_string(s); + if (!is_unspecified(entry.endpoint)) { entry.valence = valence; - list.push_back (entry); + list.push_back(entry); } else { - JLOG(m_journal.error()) << - "Bad address string '" << s << "' in Bootcache table"; + JLOG(m_journal.error()) << "Bad address string '" << s + << "' in Bootcache table"; } } } - if (!list.empty ()) + if (!list.empty()) { std::vector s; std::vector valence; - s.reserve (list.size ()); - valence.reserve (list.size ()); + s.reserve(list.size()); + valence.reserve(list.size()); - for (auto iter (list.cbegin ()); - iter != list.cend (); ++iter) + for (auto iter(list.cbegin()); iter != list.cend(); ++iter) { - s.emplace_back (to_string (iter->endpoint)); - valence.emplace_back (iter->valence); + s.emplace_back(to_string(iter->endpoint)); + valence.emplace_back(iter->valence); } - m_session << - "INSERT INTO PeerFinder_BootstrapCache_Next ( " - " address, " - " valence " - ") VALUES ( " - " :s, :valence" - ");" - , soci::use (s) - , soci::use (valence) - ; - + m_session << "INSERT INTO PeerFinder_BootstrapCache_Next ( " + " address, " + " valence " + ") VALUES ( " + " :s, :valence" + ");", + soci::use(s), soci::use(valence); } - m_session << - "DROP TABLE IF EXISTS PeerFinder_BootstrapCache;"; + m_session << "DROP TABLE IF EXISTS PeerFinder_BootstrapCache;"; - m_session << - "DROP INDEX IF EXISTS PeerFinder_BootstrapCache_Index;"; + m_session + << "DROP INDEX IF EXISTS PeerFinder_BootstrapCache_Index;"; - m_session << - "ALTER TABLE PeerFinder_BootstrapCache_Next " - " RENAME TO PeerFinder_BootstrapCache;"; + m_session << "ALTER TABLE PeerFinder_BootstrapCache_Next " + " RENAME TO PeerFinder_BootstrapCache;"; - m_session << - "CREATE INDEX IF NOT EXISTS " - " PeerFinder_BootstrapCache_Index ON " - "PeerFinder_BootstrapCache " - " ( " - " address " - " ); " - ; + m_session << "CREATE INDEX IF NOT EXISTS " + " PeerFinder_BootstrapCache_Index ON " + "PeerFinder_BootstrapCache " + " ( " + " address " + " ); "; } if (version < 3) @@ -282,69 +264,60 @@ public: // Remove legacy endpoints from the schema // - m_session << - "DROP TABLE IF EXISTS LegacyEndpoints;"; + m_session << "DROP TABLE IF EXISTS LegacyEndpoints;"; - m_session << - "DROP TABLE IF EXISTS PeerFinderLegacyEndpoints;"; + m_session << "DROP TABLE IF EXISTS PeerFinderLegacyEndpoints;"; - m_session << - "DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints;"; + m_session << "DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints;"; - m_session << - "DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints_Index;"; + m_session + << "DROP TABLE IF EXISTS PeerFinder_LegacyEndpoints_Index;"; } { int const v(currentSchemaVersion); - m_session << - "INSERT OR REPLACE INTO SchemaVersion (" - " name " - " ,version " - ") VALUES ( " - " 'PeerFinder', :version " - ");" - , soci::use (v); + m_session << "INSERT OR REPLACE INTO SchemaVersion (" + " name " + " ,version " + ") VALUES ( " + " 'PeerFinder', :version " + ");", + soci::use(v); } - tr.commit (); + tr.commit(); } private: - void init () + void + init() { - soci::transaction tr (m_session); + soci::transaction tr(m_session); m_session << "PRAGMA encoding=\"UTF-8\";"; - m_session << - "CREATE TABLE IF NOT EXISTS SchemaVersion ( " - " name TEXT PRIMARY KEY, " - " version INTEGER" - ");" - ; + m_session << "CREATE TABLE IF NOT EXISTS SchemaVersion ( " + " name TEXT PRIMARY KEY, " + " version INTEGER" + ");"; - m_session << - "CREATE TABLE IF NOT EXISTS PeerFinder_BootstrapCache ( " - " id INTEGER PRIMARY KEY AUTOINCREMENT, " - " address TEXT UNIQUE NOT NULL, " - " valence INTEGER" - ");" - ; + m_session << "CREATE TABLE IF NOT EXISTS PeerFinder_BootstrapCache ( " + " id INTEGER PRIMARY KEY AUTOINCREMENT, " + " address TEXT UNIQUE NOT NULL, " + " valence INTEGER" + ");"; - m_session << - "CREATE INDEX IF NOT EXISTS " - " PeerFinder_BootstrapCache_Index ON " - "PeerFinder_BootstrapCache " - " ( " - " address " - " ); " - ; + m_session << "CREATE INDEX IF NOT EXISTS " + " PeerFinder_BootstrapCache_Index ON " + "PeerFinder_BootstrapCache " + " ( " + " address " + " ); "; - tr.commit (); + tr.commit(); } }; -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/Tuning.h b/src/ripple/peerfinder/impl/Tuning.h index 47433a9772..3fea49a944 100644 --- a/src/ripple/peerfinder/impl/Tuning.h +++ b/src/ripple/peerfinder/impl/Tuning.h @@ -29,8 +29,7 @@ namespace PeerFinder { /** @{ */ namespace Tuning { -enum -{ +enum { //--------------------------------------------------------- // // Automatic Connection Policy @@ -41,29 +40,34 @@ enum secondsPerConnect = 10 /** Maximum number of simultaneous connection attempts. */ - ,maxConnectAttempts = 20 + , + maxConnectAttempts = 20 /** The percentage of total peer slots that are outbound. The number of outbound peers will be the larger of the minOutCount and outPercent * Config::maxPeers specially rounded. */ - ,outPercent = 15 + , + outPercent = 15 /** A hard minimum on the number of outgoing connections. This is enforced outside the Logic, so that the unit test can use any settings it wants. */ - ,minOutCount = 10 + , + minOutCount = 10 /** The default value of Config::maxPeers. */ - ,defaultMaxPeers = 21 + , + defaultMaxPeers = 21 /** Max redirects we will accept from one connection. Redirects are limited for security purposes, to prevent the address caches from getting flooded. */ - ,maxRedirects = 30 + , + maxRedirects = 30 }; //------------------------------------------------------------------------------ @@ -72,8 +76,8 @@ enum // //------------------------------------------------------------------------------ -static std::array const connectionBackoff - {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }}; +static std::array const connectionBackoff{ + {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}}; //------------------------------------------------------------------------------ // @@ -81,20 +85,20 @@ static std::array const connectionBackoff // //------------------------------------------------------------------------------ -enum -{ +enum { // Threshold of cache entries above which we trim. bootcacheSize = 1000 // The percentage of addresses we prune when we trim the cache. - ,bootcachePrunePercent = 10 + , + bootcachePrunePercent = 10 }; // The cool down wait between database updates // Ideally this should be larger than the time it takes a full // peer to send us a set of addresses and then disconnect. // -static std::chrono::seconds const bootcacheCooldownTime (60); +static std::chrono::seconds const bootcacheCooldownTime(60); //------------------------------------------------------------------------------ // @@ -102,31 +106,34 @@ static std::chrono::seconds const bootcacheCooldownTime (60); // //------------------------------------------------------------------------------ -enum -{ +enum { // Drop incoming messages with hops greater than this number maxHops = 6 // How many Endpoint to send in each mtENDPOINTS - ,numberOfEndpoints = 2 * maxHops + , + numberOfEndpoints = 2 * maxHops // The most Endpoint we will accept in mtENDPOINTS - ,numberOfEndpointsMax = 20 + , + numberOfEndpointsMax = 20 // The number of peers that we want by default, unless an // explicit value is set in the config file. - ,defaultMaxPeerCount = 21 + , + defaultMaxPeerCount = 21 /** Number of addresses we provide when redirecting. */ - ,redirectEndpointCount = 10 + , + redirectEndpointCount = 10 }; // How often we send or accept mtENDPOINTS messages per peer -static std::chrono::seconds const secondsPerMessage (5); +static std::chrono::seconds const secondsPerMessage(5); // How long an Endpoint will stay in the cache // This should be a small multiple of the broadcast frequency -static std::chrono::seconds const liveCacheSecondsToLive (30); +static std::chrono::seconds const liveCacheSecondsToLive(30); // // @@ -134,12 +141,12 @@ static std::chrono::seconds const liveCacheSecondsToLive (30); // How much time to wait before trying an outgoing address again. // Note that we ignore the port for purposes of comparison. -static std::chrono::seconds const recentAttemptDuration (60); +static std::chrono::seconds const recentAttemptDuration(60); -} +} // namespace Tuning /** @} */ -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/impl/iosformat.h b/src/ripple/peerfinder/impl/iosformat.h index 51a4b3f94b..1e3c04f635 100644 --- a/src/ripple/peerfinder/impl/iosformat.h +++ b/src/ripple/peerfinder/impl/iosformat.h @@ -32,29 +32,31 @@ namespace beast { /** Left justifies a field at the specified width. */ struct leftw { - explicit leftw (int width_) - : width (width_) - { } + explicit leftw(int width_) : width(width_) + { + } int const width; template - friend std::basic_ios & operator<< ( - std::basic_ios & ios, leftw const& p) + friend std::basic_ios& + operator<<(std::basic_ios& ios, leftw const& p) { - ios.setf (std::ios_base::left, std::ios_base::adjustfield); - ios.width (p.width); + ios.setf(std::ios_base::left, std::ios_base::adjustfield); + ios.width(p.width); return ios; } }; /** Produce a section heading and fill the rest of the line with dashes. */ template -std::basic_string heading ( - std::basic_string title, - int width = 80, CharT fill = CharT ('-')) +std::basic_string +heading( + std::basic_string title, + int width = 80, + CharT fill = CharT('-')) { - title.reserve (width); - title.push_back (CharT (' ')); - title.resize (width, fill); + title.reserve(width); + title.push_back(CharT(' ')); + title.resize(width, fill); return title; } @@ -62,17 +64,17 @@ std::basic_string heading ( struct divider { using CharT = char; - explicit divider (int width_ = 80, CharT fill_ = CharT ('-')) - : width (width_) - , fill (fill_) - { } + explicit divider(int width_ = 80, CharT fill_ = CharT('-')) + : width(width_), fill(fill_) + { + } int const width; CharT const fill; template - friend std::basic_ostream & operator<< ( - std::basic_ostream & os, divider const& d) + friend std::basic_ostream& + operator<<(std::basic_ostream& os, divider const& d) { - os << std::basic_string (d.width, d.fill); + os << std::basic_string(d.width, d.fill); return os; } }; @@ -80,17 +82,17 @@ struct divider /** Creates a padded field with an optiona fill character. */ struct fpad { - explicit fpad (int width_, int pad_ = 0, char fill_ = ' ') - : width (width_ + pad_) - , fill (fill_) - { } + explicit fpad(int width_, int pad_ = 0, char fill_ = ' ') + : width(width_ + pad_), fill(fill_) + { + } int const width; char const fill; template - friend std::basic_ostream & operator<< ( - std::basic_ostream & os, fpad const& f) + friend std::basic_ostream& + operator<<(std::basic_ostream& os, fpad const& f) { - os << std::basic_string (f.width, f.fill); + os << std::basic_string(f.width, f.fill); return os; } }; @@ -100,115 +102,125 @@ struct fpad namespace detail { template -std::string to_string (T const& t) +std::string +to_string(T const& t) { std::stringstream ss; ss << t; return ss.str(); } -} +} // namespace detail /** Justifies a field at the specified width. */ /** @{ */ -template , - class Allocator = std::allocator > +template < + class CharT, + class Traits = std::char_traits, + class Allocator = std::allocator> class field_t { public: - using string_t = std::basic_string ; - field_t (string_t const& text_, int width_, int pad_, bool right_) - : text (text_) - , width (width_) - , pad (pad_) - , right (right_) - { } + using string_t = std::basic_string; + field_t(string_t const& text_, int width_, int pad_, bool right_) + : text(text_), width(width_), pad(pad_), right(right_) + { + } string_t const text; int const width; int const pad; bool const right; template - friend std::basic_ostream & operator<< ( - std::basic_ostream & os, - field_t const& f) + friend std::basic_ostream& + operator<<( + std::basic_ostream& os, + field_t const& f) { - std::size_t const length (f.text.length()); + std::size_t const length(f.text.length()); if (f.right) { if (length < f.width) - os << std::basic_string ( - f.width - length, CharT2 (' ')); + os << std::basic_string( + f.width - length, CharT2(' ')); os << f.text; } else { os << f.text; if (length < f.width) - os << std::basic_string ( - f.width - length, CharT2 (' ')); + os << std::basic_string( + f.width - length, CharT2(' ')); } if (f.pad != 0) - os << string_t (f.pad, CharT (' ')); + os << string_t(f.pad, CharT(' ')); return os; } }; template -field_t field ( - std::basic_string const& text, - int width = 8, int pad = 0, bool right = false) +field_t +field( + std::basic_string const& text, + int width = 8, + int pad = 0, + bool right = false) { - return field_t ( - text, width, pad, right); + return field_t(text, width, pad, right); } template -field_t field ( - CharT const* text, int width = 8, int pad = 0, bool right = false) +field_t +field(CharT const* text, int width = 8, int pad = 0, bool right = false) { - return field_t , - std::allocator > (std::basic_string , std::allocator > (text), - width, pad, right); + return field_t, std::allocator>( + std:: + basic_string, std::allocator>( + text), + width, + pad, + right); } template -field_t field ( - T const& t, int width = 8, int pad = 0, bool right = false) +field_t +field(T const& t, int width = 8, int pad = 0, bool right = false) { - std::string const text (detail::to_string (t)); - return field (text, width, pad, right); + std::string const text(detail::to_string(t)); + return field(text, width, pad, right); } template -field_t rfield ( - std::basic_string const& text, - int width = 8, int pad = 0) +field_t +rfield( + std::basic_string const& text, + int width = 8, + int pad = 0) { - return field_t ( - text, width, pad, true); + return field_t(text, width, pad, true); } template -field_t rfield ( - CharT const* text, int width = 8, int pad = 0) +field_t +rfield(CharT const* text, int width = 8, int pad = 0) { - return field_t , - std::allocator > (std::basic_string , std::allocator > (text), - width, pad, true); + return field_t, std::allocator>( + std:: + basic_string, std::allocator>( + text), + width, + pad, + true); } template -field_t rfield ( - T const& t, int width = 8, int pad = 0) +field_t +rfield(T const& t, int width = 8, int pad = 0) { - std::string const text (detail::to_string (t)); - return field (text, width, pad, true); + std::string const text(detail::to_string(t)); + return field(text, width, pad, true); } /** @} */ -} +} // namespace beast #endif diff --git a/src/ripple/peerfinder/make_Manager.h b/src/ripple/peerfinder/make_Manager.h index 49bd2a2337..2520fa360d 100644 --- a/src/ripple/peerfinder/make_Manager.h +++ b/src/ripple/peerfinder/make_Manager.h @@ -29,10 +29,15 @@ namespace PeerFinder { /** Create a new Manager. */ std::unique_ptr -make_Manager (Stoppable& parent, boost::asio::io_service& io_service, - clock_type& clock, beast::Journal journal, BasicConfig const& config, beast::insight::Collector::ptr const& collector); +make_Manager( + Stoppable& parent, + boost::asio::io_service& io_service, + clock_type& clock, + beast::Journal journal, + BasicConfig const& config, + beast::insight::Collector::ptr const& collector); -} -} +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/FunctionQueue.h b/src/ripple/peerfinder/sim/FunctionQueue.h index 4fb820cb6b..d2fbb6dc26 100644 --- a/src/ripple/peerfinder/sim/FunctionQueue.h +++ b/src/ripple/peerfinder/sim/FunctionQueue.h @@ -34,47 +34,58 @@ private: class BasicWork { public: - virtual ~BasicWork () - { } - virtual void operator() () = 0; + virtual ~BasicWork() + { + } + virtual void + operator()() = 0; }; template class Work : public BasicWork { public: - explicit Work (Function f) - : m_f (f) - { } - void operator() () - { (m_f)(); } + explicit Work(Function f) : m_f(f) + { + } + void + operator()() + { + (m_f)(); + } + private: Function m_f; }; - std::list > m_work; + std::list> m_work; public: /** Returns `true` if there is no remaining work */ - bool empty () - { return m_work.empty(); } + bool + empty() + { + return m_work.empty(); + } /** Queue a function. Function must be callable with this signature: void (void) */ template - void post (Function f) + void + post(Function f) { - m_work.emplace_back (std::make_unique>(f)); + m_work.emplace_back(std::make_unique>(f)); } /** Run all pending functions. The functions will be invoked in the order they were queued. */ - void run () + void + run() { - while (! m_work.empty ()) + while (!m_work.empty()) { (*m_work.front())(); m_work.pop_front(); @@ -82,8 +93,8 @@ public: } }; -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/GraphAlgorithms.h b/src/ripple/peerfinder/sim/GraphAlgorithms.h index 9d4fc6bf5f..b11ba42a7a 100644 --- a/src/ripple/peerfinder/sim/GraphAlgorithms.h +++ b/src/ripple/peerfinder/sim/GraphAlgorithms.h @@ -33,44 +33,44 @@ struct VertexTraits; */ template -void breadth_first_traverse (Vertex& start, Function f) +void +breadth_first_traverse(Vertex& start, Function f) { - using Traits = VertexTraits ; - using Edges = typename Traits::Edges; - using Edge = typename Traits::Edge; + using Traits = VertexTraits; + using Edges = typename Traits::Edges; + using Edge = typename Traits::Edge; - using Probe = std::pair ; - using Work = std::deque ; - using Visited = std::set ; + using Probe = std::pair; + using Work = std::deque; + using Visited = std::set; Work work; Visited visited; - work.emplace_back (&start, 0); - int diameter (0); - while (! work.empty ()) + work.emplace_back(&start, 0); + int diameter(0); + while (!work.empty()) { - Probe const p (work.front()); - work.pop_front (); - if (visited.find (p.first) != visited.end ()) + Probe const p(work.front()); + work.pop_front(); + if (visited.find(p.first) != visited.end()) continue; - diameter = std::max (p.second, diameter); - visited.insert (p.first); - for (typename Edges::iterator iter ( - Traits::edges (*p.first).begin()); - iter != Traits::edges (*p.first).end(); ++iter) + diameter = std::max(p.second, diameter); + visited.insert(p.first); + for (typename Edges::iterator iter(Traits::edges(*p.first).begin()); + iter != Traits::edges(*p.first).end(); + ++iter) { - Vertex* v (Traits::vertex (*iter)); - if (visited.find (v) != visited.end()) + Vertex* v(Traits::vertex(*iter)); + if (visited.find(v) != visited.end()) continue; - if (! iter->closed()) - work.emplace_back (v, p.second + 1); + if (!iter->closed()) + work.emplace_back(v, p.second + 1); } - f (*p.first, diameter); + f(*p.first, diameter); } } - -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/Message.h b/src/ripple/peerfinder/sim/Message.h index cc7c2e7140..69be553c01 100644 --- a/src/ripple/peerfinder/sim/Message.h +++ b/src/ripple/peerfinder/sim/Message.h @@ -27,17 +27,21 @@ namespace Sim { class Message { public: - explicit Message (Endpoints const& endpoints) - : m_payload (endpoints) - { } - Endpoints const& payload () const - { return m_payload; } + explicit Message(Endpoints const& endpoints) : m_payload(endpoints) + { + } + Endpoints const& + payload() const + { + return m_payload; + } + private: Endpoints m_payload; }; -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/NodeSnapshot.h b/src/ripple/peerfinder/sim/NodeSnapshot.h index 24485ffc5e..fbb08ece9a 100644 --- a/src/ripple/peerfinder/sim/NodeSnapshot.h +++ b/src/ripple/peerfinder/sim/NodeSnapshot.h @@ -30,8 +30,8 @@ struct NodeSnapshot explicit NodeSnapshot() = default; }; -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/Params.h b/src/ripple/peerfinder/sim/Params.h index 7224f97f61..c3c288bb98 100644 --- a/src/ripple/peerfinder/sim/Params.h +++ b/src/ripple/peerfinder/sim/Params.h @@ -27,12 +27,7 @@ namespace Sim { /** Defines the parameters for a network simulation. */ struct Params { - Params () - : steps (50) - , nodes (10) - , maxPeers (20) - , outPeers (9.5) - , firewalled (0) + Params() : steps(50), nodes(10), maxPeers(20), outPeers(9.5), firewalled(0) { } @@ -40,11 +35,11 @@ struct Params int nodes; int maxPeers; double outPeers; - double firewalled; // [0, 1) + double firewalled; // [0, 1) }; -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/peerfinder/sim/Predicates.h b/src/ripple/peerfinder/sim/Predicates.h index 5625191498..7bf125b383 100644 --- a/src/ripple/peerfinder/sim/Predicates.h +++ b/src/ripple/peerfinder/sim/Predicates.h @@ -30,26 +30,32 @@ template class is_remote_node_pred { public: - is_remote_node_pred (Node const& n) - : node (n) - { } + is_remote_node_pred(Node const& n) : node(n) + { + } template - bool operator() (Link const& l) const - { return &node == &l.remote_node(); } + bool + operator()(Link const& l) const + { + return &node == &l.remote_node(); + } + private: Node const& node; }; template -is_remote_node_pred is_remote_node (Node const& node) +is_remote_node_pred +is_remote_node(Node const& node) { - return is_remote_node_pred (node); + return is_remote_node_pred(node); } template -is_remote_node_pred is_remote_node (Node const* node) +is_remote_node_pred +is_remote_node(Node const* node) { - return is_remote_node_pred (*node); + return is_remote_node_pred(*node); } /** @} */ @@ -59,20 +65,23 @@ is_remote_node_pred is_remote_node (Node const* node) class is_remote_endpoint { public: - explicit is_remote_endpoint (beast::IP::Endpoint const& address) - : m_endpoint (address) - { } + explicit is_remote_endpoint(beast::IP::Endpoint const& address) + : m_endpoint(address) + { + } template - bool operator() (Link const& link) const + bool + operator()(Link const& link) const { return link.remote_endpoint() == m_endpoint; } + private: beast::IP::Endpoint const m_endpoint; }; -} -} -} +} // namespace Sim +} // namespace PeerFinder +} // namespace ripple #endif diff --git a/src/ripple/protocol/AccountID.h b/src/ripple/protocol/AccountID.h index b4bc8fcb62..d4de74029f 100644 --- a/src/ripple/protocol/AccountID.h +++ b/src/ripple/protocol/AccountID.h @@ -23,8 +23,8 @@ #include // VFALCO Uncomment when the header issues are resolved //#include -#include #include +#include #include #include #include @@ -41,33 +41,31 @@ public: explicit AccountIDTag() = default; }; -} // detail +} // namespace detail /** A 160-bit unsigned that uniquely identifies an account. */ -using AccountID = base_uint< - 160, detail::AccountIDTag>; +using AccountID = base_uint<160, detail::AccountIDTag>; /** Convert AccountID to base58 checked string */ std::string -toBase58 (AccountID const& v); +toBase58(AccountID const& v); /** Parse AccountID from checked, base58 string. @return boost::none if a parse error occurs */ -template<> +template <> boost::optional -parseBase58 (std::string const& s); +parseBase58(std::string const& s); // Parses AccountID using Bitcoin's alphabet // This is to catch user error. Likely not needed // DEPRECATED boost::optional -deprecatedParseBitcoinAccountID (std::string const& s); +deprecatedParseBitcoinAccountID(std::string const& s); // Compatibility with legacy code bool -deprecatedParseBase58 (AccountID& account, - Json::Value const& jv); +deprecatedParseBase58(AccountID& account, Json::Value const& jv); /** Parse AccountID from hexadecimal string @@ -76,17 +74,17 @@ deprecatedParseBase58 (AccountID& account, @return boost::none if a parse error occurs */ -template<> +template <> boost::optional -parseHex (std::string const& s); +parseHex(std::string const& s); /** Parse AccountID from hex or checked base58 string. @return boost::none if a parse error occurs */ -template<> +template <> boost::optional -parseHexOrBase58 (std::string const& s); +parseHexOrBase58(std::string const& s); /** Compute AccountID from public key. @@ -96,8 +94,8 @@ parseHexOrBase58 (std::string const& s); */ // VFALCO In PublicKey.h for now -//AccountID -//calcAccountID (PublicKey const& pk); +// AccountID +// calcAccountID (PublicKey const& pk); /** A special account that's used as the "issuer" for XRP. */ AccountID const& @@ -113,28 +111,27 @@ noAccount(); */ // DEPRECATED bool -to_issuer (AccountID&, std::string const&); +to_issuer(AccountID&, std::string const&); // DEPRECATED Should be checking the currency or native flag -inline -bool +inline bool isXRP(AccountID const& c) { return c == beast::zero; } // DEPRECATED -inline -std::string -to_string (AccountID const& account) +inline std::string +to_string(AccountID const& account) { return toBase58(account); } // DEPRECATED -inline std::ostream& operator<< (std::ostream& os, AccountID const& x) +inline std::ostream& +operator<<(std::ostream& os, AccountID const& x) { - os << to_string (x); + os << to_string(x); return os; } @@ -152,17 +149,15 @@ class AccountIDCache private: std::mutex mutable mutex_; std::size_t capacity_; - hash_map mutable m0_; - hash_map mutable m1_; + hash_map mutable m0_; + hash_map mutable m1_; public: AccountIDCache(AccountIDCache const&) = delete; - AccountIDCache& operator= (AccountIDCache const&) = delete; + AccountIDCache& + operator=(AccountIDCache const&) = delete; - explicit - AccountIDCache (std::size_t capacity); + explicit AccountIDCache(std::size_t capacity); /** Return ripple::toBase58 for the AccountID @@ -173,10 +168,10 @@ public: copy for correctness. */ std::string - toBase58 (AccountID const&) const; + toBase58(AccountID const&) const; }; -} // ripple +} // namespace ripple //------------------------------------------------------------------------------ @@ -185,11 +180,11 @@ namespace std { // DEPRECATED // VFALCO Use beast::uhash or a hardened container template <> -struct hash : ripple::AccountID::hasher +struct hash : ripple::AccountID::hasher { explicit hash() = default; }; -} // std +} // namespace std #endif diff --git a/src/ripple/protocol/AmountConversions.h b/src/ripple/protocol/AmountConversions.h index 86e7c36e80..899d313fbc 100644 --- a/src/ripple/protocol/AmountConversions.h +++ b/src/ripple/protocol/AmountConversions.h @@ -26,85 +26,82 @@ namespace ripple { -inline -STAmount -toSTAmount (IOUAmount const& iou, Issue const& iss) +inline STAmount +toSTAmount(IOUAmount const& iou, Issue const& iss) { bool const isNeg = iou.signum() < 0; - std::uint64_t const umant = isNeg ? - iou.mantissa () : iou.mantissa (); - return STAmount (iss, umant, iou.exponent (), /*native*/ false, isNeg, - STAmount::unchecked ()); + std::uint64_t const umant = isNeg ? -iou.mantissa() : iou.mantissa(); + return STAmount( + iss, + umant, + iou.exponent(), + /*native*/ false, + isNeg, + STAmount::unchecked()); } -inline -STAmount -toSTAmount (IOUAmount const& iou) +inline STAmount +toSTAmount(IOUAmount const& iou) { - return toSTAmount (iou, noIssue ()); + return toSTAmount(iou, noIssue()); } -inline -STAmount -toSTAmount (XRPAmount const& xrp) +inline STAmount +toSTAmount(XRPAmount const& xrp) { bool const isNeg = xrp.signum() < 0; - std::uint64_t const umant = isNeg ? - xrp.drops () : xrp.drops (); - return STAmount (umant, isNeg); + std::uint64_t const umant = isNeg ? -xrp.drops() : xrp.drops(); + return STAmount(umant, isNeg); } -inline -STAmount -toSTAmount (XRPAmount const& xrp, Issue const& iss) +inline STAmount +toSTAmount(XRPAmount const& xrp, Issue const& iss) { - assert (isXRP(iss.account) && isXRP(iss.currency)); - return toSTAmount (xrp); + assert(isXRP(iss.account) && isXRP(iss.currency)); + return toSTAmount(xrp); } template T -toAmount (STAmount const& amt) +toAmount(STAmount const& amt) { static_assert(sizeof(T) == -1, "Must use specialized function"); return T(0); } template <> -inline -STAmount -toAmount (STAmount const& amt) +inline STAmount +toAmount(STAmount const& amt) { return amt; } template <> -inline -IOUAmount -toAmount (STAmount const& amt) +inline IOUAmount +toAmount(STAmount const& amt) { - assert (amt.mantissa () < std::numeric_limits::max ()); - bool const isNeg = amt.negative (); + assert(amt.mantissa() < std::numeric_limits::max()); + bool const isNeg = amt.negative(); std::int64_t const sMant = - isNeg ? - std::int64_t (amt.mantissa ()) : amt.mantissa (); + isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa(); - assert (! isXRP (amt)); - return IOUAmount (sMant, amt.exponent ()); + assert(!isXRP(amt)); + return IOUAmount(sMant, amt.exponent()); } template <> -inline -XRPAmount -toAmount (STAmount const& amt) +inline XRPAmount +toAmount(STAmount const& amt) { - assert (amt.mantissa () < std::numeric_limits::max ()); - bool const isNeg = amt.negative (); + assert(amt.mantissa() < std::numeric_limits::max()); + bool const isNeg = amt.negative(); std::int64_t const sMant = - isNeg ? - std::int64_t (amt.mantissa ()) : amt.mantissa (); + isNeg ? -std::int64_t(amt.mantissa()) : amt.mantissa(); - assert (isXRP (amt)); - return XRPAmount (sMant); + assert(isXRP(amt)); + return XRPAmount(sMant); } - -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/Book.h b/src/ripple/protocol/Book.h index 445691299e..8a0867fe3e 100644 --- a/src/ripple/protocol/Book.h +++ b/src/ripple/protocol/Book.h @@ -35,77 +35,75 @@ public: Issue in; Issue out; - Book () + Book() { } - Book (Issue const& in_, Issue const& out_) - : in (in_) - , out (out_) + Book(Issue const& in_, Issue const& out_) : in(in_), out(out_) { } }; bool -isConsistent (Book const& book); +isConsistent(Book const& book); std::string -to_string (Book const& book); +to_string(Book const& book); std::ostream& -operator<< (std::ostream& os, Book const& x); +operator<<(std::ostream& os, Book const& x); template void -hash_append (Hasher& h, Book const& b) +hash_append(Hasher& h, Book const& b) { using beast::hash_append; hash_append(h, b.in, b.out); } Book -reversed (Book const& book); +reversed(Book const& book); /** Ordered comparison. */ int -compare (Book const& lhs, Book const& rhs); +compare(Book const& lhs, Book const& rhs); /** Equality comparison. */ /** @{ */ bool -operator== (Book const& lhs, Book const& rhs); +operator==(Book const& lhs, Book const& rhs); bool -operator!= (Book const& lhs, Book const& rhs); +operator!=(Book const& lhs, Book const& rhs); /** @} */ /** Strict weak ordering. */ /** @{ */ bool -operator< (Book const& lhs,Book const& rhs); +operator<(Book const& lhs, Book const& rhs); bool -operator> (Book const& lhs, Book const& rhs); +operator>(Book const& lhs, Book const& rhs); bool -operator>= (Book const& lhs, Book const& rhs); +operator>=(Book const& lhs, Book const& rhs); bool -operator<= (Book const& lhs, Book const& rhs); +operator<=(Book const& lhs, Book const& rhs); /** @} */ -} +} // namespace ripple //------------------------------------------------------------------------------ namespace std { template <> -struct hash - : private boost::base_from_member , 0> - , private boost::base_from_member , 1> +struct hash + : private boost::base_from_member, 0>, + private boost::base_from_member, 1> { private: - using currency_hash_type = boost::base_from_member < - std::hash , 0>; - using issuer_hash_type = boost::base_from_member < - std::hash , 1>; + using currency_hash_type = + boost::base_from_member, 0>; + using issuer_hash_type = + boost::base_from_member, 1>; public: explicit hash() = default; @@ -113,12 +111,13 @@ public: using value_type = std::size_t; using argument_type = ripple::Issue; - value_type operator() (argument_type const& value) const + value_type + operator()(argument_type const& value) const { - value_type result (currency_hash_type::member (value.currency)); - if (!isXRP (value.currency)) - boost::hash_combine (result, - issuer_hash_type::member (value.account)); + value_type result(currency_hash_type::member(value.currency)); + if (!isXRP(value.currency)) + boost::hash_combine( + result, issuer_hash_type::member(value.account)); return result; } }; @@ -126,10 +125,10 @@ public: //------------------------------------------------------------------------------ template <> -struct hash +struct hash { private: - using hasher = std::hash ; + using hasher = std::hash; hasher m_hasher; @@ -139,42 +138,41 @@ public: using value_type = std::size_t; using argument_type = ripple::Book; - value_type operator() (argument_type const& value) const + value_type + operator()(argument_type const& value) const { - value_type result (m_hasher (value.in)); - boost::hash_combine (result, m_hasher (value.out)); + value_type result(m_hasher(value.in)); + boost::hash_combine(result, m_hasher(value.out)); return result; } }; -} +} // namespace std //------------------------------------------------------------------------------ namespace boost { template <> -struct hash - : std::hash +struct hash : std::hash { explicit hash() = default; - using Base = std::hash ; + using Base = std::hash; // VFALCO NOTE broken in vs2012 - //using Base::Base; // inherit ctors + // using Base::Base; // inherit ctors }; template <> -struct hash - : std::hash +struct hash : std::hash { explicit hash() = default; - using Base = std::hash ; + using Base = std::hash; // VFALCO NOTE broken in vs2012 - //using Base::Base; // inherit ctors + // using Base::Base; // inherit ctors }; -} +} // namespace boost #endif diff --git a/src/ripple/protocol/BuildInfo.h b/src/ripple/protocol/BuildInfo.h index 505f0cdc9e..7f81131911 100644 --- a/src/ripple/protocol/BuildInfo.h +++ b/src/ripple/protocol/BuildInfo.h @@ -25,7 +25,6 @@ namespace ripple { - /** Versioning information for this build. */ // VFALCO The namespace is deprecated namespace BuildInfo { @@ -44,8 +43,8 @@ getVersionString(); std::string const& getFullVersionString(); -} // BuildInfo (DEPRECATED) +} // namespace BuildInfo -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/ErrorCodes.h b/src/ripple/protocol/ErrorCodes.h index 0f7d9526a0..a3fb9e590b 100644 --- a/src/ripple/protocol/ErrorCodes.h +++ b/src/ripple/protocol/ErrorCodes.h @@ -37,115 +37,114 @@ namespace ripple { // // Please only append to this table. Do not "fill-in" gaps and do not re-use // or repurpose error code values. -enum error_code_i -{ +enum error_code_i { // -1 represents codes not listed in this enumeration - rpcUNKNOWN = -1, + rpcUNKNOWN = -1, - rpcSUCCESS = 0, + rpcSUCCESS = 0, - rpcBAD_SYNTAX = 1, - rpcJSON_RPC = 2, - rpcFORBIDDEN = 3, + rpcBAD_SYNTAX = 1, + rpcJSON_RPC = 2, + rpcFORBIDDEN = 3, // Misc failure // unused 4, // unused 5, - rpcNO_PERMISSION = 6, - rpcNO_EVENTS = 7, + rpcNO_PERMISSION = 6, + rpcNO_EVENTS = 7, // unused 8, - rpcTOO_BUSY = 9, - rpcSLOW_DOWN = 10, - rpcHIGH_FEE = 11, - rpcNOT_ENABLED = 12, - rpcNOT_READY = 13, - rpcAMENDMENT_BLOCKED = 14, + rpcTOO_BUSY = 9, + rpcSLOW_DOWN = 10, + rpcHIGH_FEE = 11, + rpcNOT_ENABLED = 12, + rpcNOT_READY = 13, + rpcAMENDMENT_BLOCKED = 14, // Networking - rpcNO_CLOSED = 15, - rpcNO_CURRENT = 16, - rpcNO_NETWORK = 17, + rpcNO_CLOSED = 15, + rpcNO_CURRENT = 16, + rpcNO_NETWORK = 17, // Ledger state // unused 18, - rpcACT_NOT_FOUND = 19, + rpcACT_NOT_FOUND = 19, // unused 20, - rpcLGR_NOT_FOUND = 21, - rpcLGR_NOT_VALIDATED = 22, - rpcMASTER_DISABLED = 23, + rpcLGR_NOT_FOUND = 21, + rpcLGR_NOT_VALIDATED = 22, + rpcMASTER_DISABLED = 23, // unused 24, // unused 25, // unused 26, // unused 27, // unused 28, - rpcTXN_NOT_FOUND = 29, + rpcTXN_NOT_FOUND = 29, // unused 30, // Malformed command - rpcINVALID_PARAMS = 31, - rpcUNKNOWN_COMMAND = 32, - rpcNO_PF_REQUEST = 33, + rpcINVALID_PARAMS = 31, + rpcUNKNOWN_COMMAND = 32, + rpcNO_PF_REQUEST = 33, // Bad parameter - rpcACT_BITCOIN = 34, - rpcACT_MALFORMED = 35, - rpcALREADY_MULTISIG = 36, - rpcALREADY_SINGLE_SIG = 37, + rpcACT_BITCOIN = 34, + rpcACT_MALFORMED = 35, + rpcALREADY_MULTISIG = 36, + rpcALREADY_SINGLE_SIG = 37, // unused 38, // unused 39, - rpcBAD_FEATURE = 40, - rpcBAD_ISSUER = 41, - rpcBAD_MARKET = 42, - rpcBAD_SECRET = 43, - rpcBAD_SEED = 44, - rpcCHANNEL_MALFORMED = 45, + rpcBAD_FEATURE = 40, + rpcBAD_ISSUER = 41, + rpcBAD_MARKET = 42, + rpcBAD_SECRET = 43, + rpcBAD_SEED = 44, + rpcCHANNEL_MALFORMED = 45, rpcCHANNEL_AMT_MALFORMED = 46, - rpcCOMMAND_MISSING = 47, - rpcDST_ACT_MALFORMED = 48, - rpcDST_ACT_MISSING = 49, - rpcDST_ACT_NOT_FOUND = 50, - rpcDST_AMT_MALFORMED = 51, - rpcDST_AMT_MISSING = 52, - rpcDST_ISR_MALFORMED = 53, + rpcCOMMAND_MISSING = 47, + rpcDST_ACT_MALFORMED = 48, + rpcDST_ACT_MISSING = 49, + rpcDST_ACT_NOT_FOUND = 50, + rpcDST_AMT_MALFORMED = 51, + rpcDST_AMT_MISSING = 52, + rpcDST_ISR_MALFORMED = 53, // unused 54, // unused 55, // unused 56, - rpcLGR_IDXS_INVALID = 57, - rpcLGR_IDX_MALFORMED = 58, + rpcLGR_IDXS_INVALID = 57, + rpcLGR_IDX_MALFORMED = 58, // unused 59, // unused 60, // unused 61, - rpcPUBLIC_MALFORMED = 62, - rpcSIGNING_MALFORMED = 63, - rpcSENDMAX_MALFORMED = 64, - rpcSRC_ACT_MALFORMED = 65, - rpcSRC_ACT_MISSING = 66, - rpcSRC_ACT_NOT_FOUND = 67, + rpcPUBLIC_MALFORMED = 62, + rpcSIGNING_MALFORMED = 63, + rpcSENDMAX_MALFORMED = 64, + rpcSRC_ACT_MALFORMED = 65, + rpcSRC_ACT_MISSING = 66, + rpcSRC_ACT_NOT_FOUND = 67, // unused 68, - rpcSRC_CUR_MALFORMED = 69, - rpcSRC_ISR_MALFORMED = 70, - rpcSTREAM_MALFORMED = 71, - rpcATX_DEPRECATED = 72, + rpcSRC_CUR_MALFORMED = 69, + rpcSRC_ISR_MALFORMED = 70, + rpcSTREAM_MALFORMED = 71, + rpcATX_DEPRECATED = 72, // Internal error (should never happen) - rpcINTERNAL = 73, // Generic internal error. - rpcNOT_IMPL = 74, - rpcNOT_SUPPORTED = 75, - rpcBAD_KEY_TYPE = 76, - rpcDB_DESERIALIZATION = 77, - rpcEXCESSIVE_LGR_RANGE = 78, - rpcINVALID_LGR_RANGE = 79, - rpcLAST = rpcINVALID_LGR_RANGE // rpcLAST should always equal the last code.= + rpcINTERNAL = 73, // Generic internal error. + rpcNOT_IMPL = 74, + rpcNOT_SUPPORTED = 75, + rpcBAD_KEY_TYPE = 76, + rpcDB_DESERIALIZATION = 77, + rpcEXCESSIVE_LGR_RANGE = 78, + rpcINVALID_LGR_RANGE = 79, + rpcLAST = + rpcINVALID_LGR_RANGE // rpcLAST should always equal the last code.= }; /** Codes returned in the `warnings` array of certain RPC commands. These values need to remain stable. */ -enum warning_code_i -{ - warnRPC_UNSUPPORTED_MAJORITY = 1001, - warnRPC_AMENDMENT_BLOCKED = 1002, +enum warning_code_i { + warnRPC_UNSUPPORTED_MAJORITY = 1001, + warnRPC_AMENDMENT_BLOCKED = 1002, }; //------------------------------------------------------------------------------ @@ -158,18 +157,18 @@ namespace RPC { struct ErrorInfo { // Default ctor needed to produce an empty std::array during constexpr eval. - constexpr ErrorInfo () - : code (rpcUNKNOWN) - , token ("unknown") - , message ("An unknown error code.") - { } + constexpr ErrorInfo() + : code(rpcUNKNOWN), token("unknown"), message("An unknown error code.") + { + } - constexpr ErrorInfo (error_code_i code_, char const* token_, + constexpr ErrorInfo( + error_code_i code_, + char const* token_, char const* message_) - : code (code_) - , token (token_) - , message (message_) - { } + : code(code_), token(token_), message(message_) + { + } error_code_i code; Json::StaticString token; @@ -177,139 +176,158 @@ struct ErrorInfo }; /** Returns an ErrorInfo that reflects the error code. */ -ErrorInfo const& get_error_info (error_code_i code); +ErrorInfo const& +get_error_info(error_code_i code); /** Add or update the json update to reflect the error code. */ /** @{ */ template -void inject_error (error_code_i code, JsonValue& json) +void +inject_error(error_code_i code, JsonValue& json) { - ErrorInfo const& info (get_error_info (code)); - json [jss::error] = info.token; - json [jss::error_code] = info.code; - json [jss::error_message] = info.message; + ErrorInfo const& info(get_error_info(code)); + json[jss::error] = info.token; + json[jss::error_code] = info.code; + json[jss::error_message] = info.message; } template -void inject_error (int code, JsonValue& json) +void +inject_error(int code, JsonValue& json) { - inject_error (error_code_i (code), json); + inject_error(error_code_i(code), json); } template -void inject_error ( - error_code_i code, std::string const& message, JsonValue& json) +void +inject_error(error_code_i code, std::string const& message, JsonValue& json) { - ErrorInfo const& info (get_error_info (code)); - json [jss::error] = info.token; - json [jss::error_code] = info.code; - json [jss::error_message] = message; + ErrorInfo const& info(get_error_info(code)); + json[jss::error] = info.token; + json[jss::error_code] = info.code; + json[jss::error_message] = message; } /** @} */ /** Returns a new json object that reflects the error code. */ /** @{ */ -Json::Value make_error (error_code_i code); -Json::Value make_error (error_code_i code, std::string const& message); +Json::Value +make_error(error_code_i code); +Json::Value +make_error(error_code_i code, std::string const& message); /** @} */ /** Returns a new json object that indicates invalid parameters. */ /** @{ */ -inline Json::Value make_param_error (std::string const& message) +inline Json::Value +make_param_error(std::string const& message) { - return make_error (rpcINVALID_PARAMS, message); + return make_error(rpcINVALID_PARAMS, message); } -inline std::string missing_field_message (std::string const& name) +inline std::string +missing_field_message(std::string const& name) { return "Missing field '" + name + "'."; } -inline Json::Value missing_field_error (std::string const& name) +inline Json::Value +missing_field_error(std::string const& name) { - return make_param_error (missing_field_message (name)); + return make_param_error(missing_field_message(name)); } -inline Json::Value missing_field_error (Json::StaticString name) +inline Json::Value +missing_field_error(Json::StaticString name) { - return missing_field_error (std::string (name)); + return missing_field_error(std::string(name)); } -inline std::string object_field_message (std::string const& name) +inline std::string +object_field_message(std::string const& name) { return "Invalid field '" + name + "', not object."; } -inline Json::Value object_field_error (std::string const& name) +inline Json::Value +object_field_error(std::string const& name) { - return make_param_error (object_field_message (name)); + return make_param_error(object_field_message(name)); } -inline Json::Value object_field_error (Json::StaticString name) +inline Json::Value +object_field_error(Json::StaticString name) { - return object_field_error (std::string (name)); + return object_field_error(std::string(name)); } -inline std::string invalid_field_message (std::string const& name) +inline std::string +invalid_field_message(std::string const& name) { return "Invalid field '" + name + "'."; } -inline std::string invalid_field_message (Json::StaticString name) +inline std::string +invalid_field_message(Json::StaticString name) { - return invalid_field_message (std::string(name)); + return invalid_field_message(std::string(name)); } -inline Json::Value invalid_field_error (std::string const& name) +inline Json::Value +invalid_field_error(std::string const& name) { - return make_param_error (invalid_field_message (name)); + return make_param_error(invalid_field_message(name)); } -inline Json::Value invalid_field_error (Json::StaticString name) +inline Json::Value +invalid_field_error(Json::StaticString name) { - return invalid_field_error (std::string (name)); + return invalid_field_error(std::string(name)); } -inline std::string expected_field_message ( - std::string const& name, std::string const& type) +inline std::string +expected_field_message(std::string const& name, std::string const& type) { return "Invalid field '" + name + "', not " + type + "."; } -inline std::string expected_field_message ( - Json::StaticString name, std::string const& type) +inline std::string +expected_field_message(Json::StaticString name, std::string const& type) { - return expected_field_message (std::string (name), type); + return expected_field_message(std::string(name), type); } -inline Json::Value expected_field_error ( - std::string const& name, std::string const& type) +inline Json::Value +expected_field_error(std::string const& name, std::string const& type) { - return make_param_error (expected_field_message (name, type)); + return make_param_error(expected_field_message(name, type)); } -inline Json::Value expected_field_error ( - Json::StaticString name, std::string const& type) +inline Json::Value +expected_field_error(Json::StaticString name, std::string const& type) { - return expected_field_error (std::string (name), type); + return expected_field_error(std::string(name), type); } -inline Json::Value not_validator_error () +inline Json::Value +not_validator_error() { - return make_param_error ("not a validator"); + return make_param_error("not a validator"); } /** @} */ /** Returns `true` if the json contains an rpc error specification. */ -bool contains_error (Json::Value const& json); +bool +contains_error(Json::Value const& json); -} // RPC +} // namespace RPC /** Returns a single string with the contents of an RPC error. */ -std::string rpcErrorString(Json::Value const& jv); +std::string +rpcErrorString(Json::Value const& jv); -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/Feature.h b/src/ripple/protocol/Feature.h index 180f02bdd8..a325de3096 100644 --- a/src/ripple/protocol/Feature.h +++ b/src/ripple/protocol/Feature.h @@ -69,15 +69,14 @@ namespace detail { class FeatureCollections { - static constexpr char const* const featureNames[] = - { - "MultiSign", // Unconditionally supported. + static constexpr char const* const featureNames[] = { + "MultiSign", // Unconditionally supported. "Tickets", - "TrustSetAuth", // Unconditionally supported. - "FeeEscalation", // Unconditionally supported. + "TrustSetAuth", // Unconditionally supported. + "FeeEscalation", // Unconditionally supported. "OwnerPaysFee", "PayChan", - "Flow", // Unconditionally supported. + "Flow", // Unconditionally supported. "CompareTakerFlowCross", "FlowCross", "CryptoConditions", @@ -110,7 +109,8 @@ class FeatureCollections // fixQualityUpperBound should be activated before FlowCross "fixQualityUpperBound", "RequireFullyCanonicalSig", - "fix1781", // XRPEndpointSteps should be included in the circular payment check + "fix1781", // XRPEndpointSteps should be included in the circular + // payment check }; std::vector features; @@ -120,9 +120,10 @@ class FeatureCollections public: FeatureCollections(); - static constexpr std::size_t numFeatures() + static constexpr std::size_t + numFeatures() { - return sizeof (featureNames) / sizeof (featureNames[0]); + return sizeof(featureNames) / sizeof(featureNames[0]); } boost::optional @@ -137,12 +138,12 @@ public: /** Amendments that this server supports, but doesn't enable by default */ std::vector const& -supportedAmendments (); +supportedAmendments(); -} // detail +} // namespace detail boost::optional -getRegisteredFeature (std::string const& name); +getRegisteredFeature(std::string const& name); size_t featureToBitsetIndex(uint256 const& f); @@ -155,8 +156,9 @@ class FeatureBitset { using base = std::bitset; - template - void initFromFeatures(uint256 const& f, Fs&&... fs) + template + void + initFromFeatures(uint256 const& f, Fs&&... fs) { set(f); if constexpr (sizeof...(fs) > 0) @@ -168,49 +170,47 @@ public: using base::operator==; using base::operator!=; - using base::test; using base::all; using base::any; - using base::none; using base::count; - using base::size; - using base::set; - using base::reset; using base::flip; + using base::none; + using base::reset; + using base::set; + using base::size; + using base::test; using base::operator[]; using base::to_string; - using base::to_ulong; using base::to_ullong; + using base::to_ulong; FeatureBitset() = default; - explicit - FeatureBitset(base const& b) - : base(b) + explicit FeatureBitset(base const& b) : base(b) { } - template - explicit - FeatureBitset(uint256 const& f, Fs&&... fs) + template + explicit FeatureBitset(uint256 const& f, Fs&&... fs) { initFromFeatures(f, std::forward(fs)...); } template - explicit - FeatureBitset(Col const& fs) + explicit FeatureBitset(Col const& fs) { for (auto const& f : fs) set(featureToBitsetIndex(f)); } - auto operator[](uint256 const& f) + auto + operator[](uint256 const& f) { return base::operator[](featureToBitsetIndex(f)); } - auto operator[](uint256 const& f) const + auto + operator[](uint256 const& f) const { return base::operator[](featureToBitsetIndex(f)); } @@ -236,119 +236,98 @@ public: return *this; } - FeatureBitset& operator&=(FeatureBitset const& rhs) + FeatureBitset& + operator&=(FeatureBitset const& rhs) { base::operator&=(rhs); return *this; } - FeatureBitset& operator|=(FeatureBitset const& rhs) + FeatureBitset& + operator|=(FeatureBitset const& rhs) { base::operator|=(rhs); return *this; } - FeatureBitset operator~() const + FeatureBitset + operator~() const { return FeatureBitset{base::operator~()}; } - friend - FeatureBitset operator&( - FeatureBitset const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator&(FeatureBitset const& lhs, FeatureBitset const& rhs) { - return FeatureBitset{static_cast(lhs) & - static_cast(rhs)}; + return FeatureBitset{ + static_cast(lhs) & static_cast(rhs)}; } - friend - FeatureBitset operator&( - FeatureBitset const& lhs, - uint256 const& rhs) + friend FeatureBitset + operator&(FeatureBitset const& lhs, uint256 const& rhs) { return lhs & FeatureBitset{rhs}; } - friend - FeatureBitset operator&( - uint256 const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator&(uint256 const& lhs, FeatureBitset const& rhs) { return FeatureBitset{lhs} & rhs; } - friend - FeatureBitset operator|( - FeatureBitset const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator|(FeatureBitset const& lhs, FeatureBitset const& rhs) { - return FeatureBitset{static_cast(lhs) | - static_cast(rhs)}; + return FeatureBitset{ + static_cast(lhs) | static_cast(rhs)}; } - friend - FeatureBitset operator|( - FeatureBitset const& lhs, - uint256 const& rhs) + friend FeatureBitset + operator|(FeatureBitset const& lhs, uint256 const& rhs) { return lhs | FeatureBitset{rhs}; } - friend - FeatureBitset operator|( - uint256 const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator|(uint256 const& lhs, FeatureBitset const& rhs) { return FeatureBitset{lhs} | rhs; } - friend - FeatureBitset operator^( - FeatureBitset const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator^(FeatureBitset const& lhs, FeatureBitset const& rhs) { - return FeatureBitset{static_cast(lhs) ^ - static_cast(rhs)}; + return FeatureBitset{ + static_cast(lhs) ^ static_cast(rhs)}; } - friend - FeatureBitset operator^( - FeatureBitset const& lhs, - uint256 const& rhs) + friend FeatureBitset + operator^(FeatureBitset const& lhs, uint256 const& rhs) { - return lhs ^ FeatureBitset{rhs}; + return lhs ^ FeatureBitset { rhs }; } - friend - FeatureBitset operator^( - uint256 const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator^(uint256 const& lhs, FeatureBitset const& rhs) { return FeatureBitset{lhs} ^ rhs; } // set difference - friend - FeatureBitset operator-( - FeatureBitset const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator-(FeatureBitset const& lhs, FeatureBitset const& rhs) { return lhs & ~rhs; } - friend - FeatureBitset operator-( - FeatureBitset const& lhs, - uint256 const& rhs) + friend FeatureBitset + operator-(FeatureBitset const& lhs, uint256 const& rhs) { return lhs - FeatureBitset{rhs}; } - friend - FeatureBitset operator-( - uint256 const& lhs, - FeatureBitset const& rhs) + friend FeatureBitset + operator-(uint256 const& lhs, FeatureBitset const& rhs) { return FeatureBitset{lhs} - rhs; } @@ -403,6 +382,6 @@ extern uint256 const retiredFix1512; extern uint256 const retiredFix1523; extern uint256 const retiredFix1528; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/HashPrefix.h b/src/ripple/protocol/HashPrefix.h index 2142b8a6f4..ec080c2d55 100644 --- a/src/ripple/protocol/HashPrefix.h +++ b/src/ripple/protocol/HashPrefix.h @@ -27,17 +27,15 @@ namespace ripple { namespace detail { -constexpr -std::uint32_t +constexpr std::uint32_t make_hash_prefix(char a, char b, char c) { - return - (static_cast(a) << 24) + + return (static_cast(a) << 24) + (static_cast(b) << 16) + (static_cast(c) << 8); } -} +} // namespace detail /** Prefix for hashing functions. @@ -53,37 +51,36 @@ make_hash_prefix(char a, char b, char c) @note Hash prefixes are part of the protocol; you cannot, arbitrarily, change the type or the value of any of these without causing breakage. */ -enum class HashPrefix : std::uint32_t -{ +enum class HashPrefix : std::uint32_t { /** transaction plus signature to give transaction ID */ - transactionID = detail::make_hash_prefix('T', 'X', 'N'), + transactionID = detail::make_hash_prefix('T', 'X', 'N'), /** transaction plus metadata */ - txNode = detail::make_hash_prefix('S', 'N', 'D'), + txNode = detail::make_hash_prefix('S', 'N', 'D'), /** account state */ - leafNode = detail::make_hash_prefix('M', 'L', 'N'), + leafNode = detail::make_hash_prefix('M', 'L', 'N'), /** inner node in V1 tree */ - innerNode = detail::make_hash_prefix('M', 'I', 'N'), + innerNode = detail::make_hash_prefix('M', 'I', 'N'), /** ledger master data for signing */ - ledgerMaster = detail::make_hash_prefix('L', 'W', 'R'), + ledgerMaster = detail::make_hash_prefix('L', 'W', 'R'), /** inner transaction to sign */ - txSign = detail::make_hash_prefix('S', 'T', 'X'), + txSign = detail::make_hash_prefix('S', 'T', 'X'), /** inner transaction to multi-sign */ - txMultiSign = detail::make_hash_prefix('S', 'M', 'T'), + txMultiSign = detail::make_hash_prefix('S', 'M', 'T'), /** validation for signing */ - validation = detail::make_hash_prefix('V', 'A', 'L'), + validation = detail::make_hash_prefix('V', 'A', 'L'), /** proposal for signing */ - proposal = detail::make_hash_prefix('P', 'R', 'P'), + proposal = detail::make_hash_prefix('P', 'R', 'P'), /** Manifest */ - manifest = detail::make_hash_prefix('M', 'A', 'N'), + manifest = detail::make_hash_prefix('M', 'A', 'N'), /** Payment Channel Claim */ paymentChannelClaim = detail::make_hash_prefix('C', 'L', 'M'), @@ -91,13 +88,12 @@ enum class HashPrefix : std::uint32_t template void -hash_append (Hasher& h, HashPrefix const& hp) noexcept +hash_append(Hasher& h, HashPrefix const& hp) noexcept { using beast::hash_append; - hash_append(h, - static_cast(hp)); + hash_append(h, static_cast(hp)); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Indexes.h b/src/ripple/protocol/Indexes.h index 959ebf02d8..b8b1bef6c6 100644 --- a/src/ripple/protocol/Indexes.h +++ b/src/ripple/protocol/Indexes.h @@ -20,78 +20,81 @@ #ifndef RIPPLE_PROTOCOL_INDEXES_H_INCLUDED #define RIPPLE_PROTOCOL_INDEXES_H_INCLUDED +#include +#include #include #include #include #include #include #include -#include -#include #include namespace ripple { // get the index of the node that holds the last 256 ledgers uint256 -getLedgerHashIndex (); +getLedgerHashIndex(); // Get the index of the node that holds the set of 256 ledgers that includes // this ledger's hash (or the first ledger after it if it's not a multiple // of 256). uint256 -getLedgerHashIndex (std::uint32_t desiredLedgerIndex); +getLedgerHashIndex(std::uint32_t desiredLedgerIndex); // get the index of the node that holds the enabled amendments uint256 -getLedgerAmendmentIndex (); +getLedgerAmendmentIndex(); // get the index of the node that holds the fee schedule uint256 -getLedgerFeeIndex (); +getLedgerFeeIndex(); uint256 -getGeneratorIndex (AccountID const& uGeneratorID); +getGeneratorIndex(AccountID const& uGeneratorID); uint256 -getBookBase (Book const& book); +getBookBase(Book const& book); uint256 -getOfferIndex (AccountID const& account, std::uint32_t uSequence); +getOfferIndex(AccountID const& account, std::uint32_t uSequence); uint256 -getOwnerDirIndex (AccountID const& account); +getOwnerDirIndex(AccountID const& account); uint256 -getDirNodeIndex (uint256 const& uDirRoot, const std::uint64_t uNodeIndex); +getDirNodeIndex(uint256 const& uDirRoot, const std::uint64_t uNodeIndex); uint256 -getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir = 0); +getQualityIndex(uint256 const& uBase, const std::uint64_t uNodeDir = 0); uint256 -getQualityNext (uint256 const& uBase); +getQualityNext(uint256 const& uBase); // VFALCO This name could be better std::uint64_t -getQuality (uint256 const& uBase); +getQuality(uint256 const& uBase); uint256 -getTicketIndex (AccountID const& account, std::uint32_t uSequence); +getTicketIndex(AccountID const& account, std::uint32_t uSequence); uint256 -getRippleStateIndex (AccountID const& a, AccountID const& b, Currency const& currency); +getRippleStateIndex( + AccountID const& a, + AccountID const& b, + Currency const& currency); uint256 -getRippleStateIndex (AccountID const& a, Issue const& issue); +getRippleStateIndex(AccountID const& a, Issue const& issue); uint256 -getSignerListIndex (AccountID const& account); +getSignerListIndex(AccountID const& account); uint256 -getCheckIndex (AccountID const& account, std::uint32_t uSequence); +getCheckIndex(AccountID const& account, std::uint32_t uSequence); uint256 -getDepositPreauthIndex (AccountID const& owner, AccountID const& preauthorized); +getDepositPreauthIndex(AccountID const& owner, AccountID const& preauthorized); //------------------------------------------------------------------------------ @@ -111,32 +114,37 @@ struct account_t { explicit account_t() = default; - Keylet operator()(AccountID const& id) const; + Keylet + operator()(AccountID const& id) const; }; -static account_t const account {}; +static account_t const account{}; /** The amendment table */ struct amendments_t { explicit amendments_t() = default; - Keylet operator()() const; + Keylet + operator()() const; }; -static amendments_t const amendments {}; +static amendments_t const amendments{}; /** Any item that can be in an owner dir. */ -Keylet child (uint256 const& key); +Keylet +child(uint256 const& key); /** Skip list */ struct skip_t { explicit skip_t() = default; - Keylet operator()() const; + Keylet + operator()() const; - Keylet operator()(LedgerIndex ledger) const; + Keylet + operator()(LedgerIndex ledger) const; }; -static skip_t const skip {}; +static skip_t const skip{}; /** The ledger fees */ struct fees_t @@ -144,161 +152,178 @@ struct fees_t explicit fees_t() = default; // VFALCO This could maybe be constexpr - Keylet operator()() const; + Keylet + operator()() const; }; -static fees_t const fees {}; +static fees_t const fees{}; /** The beginning of an order book */ struct book_t { explicit book_t() = default; - Keylet operator()(Book const& b) const; + Keylet + operator()(Book const& b) const; }; -static book_t const book {}; +static book_t const book{}; /** A trust line */ struct line_t { explicit line_t() = default; - Keylet operator()(AccountID const& id0, - AccountID const& id1, Currency const& currency) const; + Keylet + operator()( + AccountID const& id0, + AccountID const& id1, + Currency const& currency) const; - Keylet operator()(AccountID const& id, - Issue const& issue) const; + Keylet + operator()(AccountID const& id, Issue const& issue) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltRIPPLE_STATE, key }; + return {ltRIPPLE_STATE, key}; } }; -static line_t const line {}; +static line_t const line{}; /** An offer from an account */ struct offer_t { explicit offer_t() = default; - Keylet operator()(AccountID const& id, - std::uint32_t seq) const; + Keylet + operator()(AccountID const& id, std::uint32_t seq) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltOFFER, key }; + return {ltOFFER, key}; } }; -static offer_t const offer {}; +static offer_t const offer{}; /** The initial directory page for a specific quality */ struct quality_t { explicit quality_t() = default; - Keylet operator()(Keylet const& k, - std::uint64_t q) const; + Keylet + operator()(Keylet const& k, std::uint64_t q) const; }; -static quality_t const quality {}; +static quality_t const quality{}; /** The directory for the next lower quality */ struct next_t { explicit next_t() = default; - Keylet operator()(Keylet const& k) const; + Keylet + operator()(Keylet const& k) const; }; -static next_t const next {}; +static next_t const next{}; /** A ticket belonging to an account */ struct ticket_t { explicit ticket_t() = default; - Keylet operator()(AccountID const& id, - std::uint32_t seq) const; + Keylet + operator()(AccountID const& id, std::uint32_t seq) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltTICKET, key }; + return {ltTICKET, key}; } }; -static ticket_t const ticket {}; +static ticket_t const ticket{}; /** A SignerList */ struct signers_t { explicit signers_t() = default; - Keylet operator()(AccountID const& id) const; + Keylet + operator()(AccountID const& id) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltSIGNER_LIST, key }; + return {ltSIGNER_LIST, key}; } }; -static signers_t const signers {}; +static signers_t const signers{}; /** A Check */ struct check_t { explicit check_t() = default; - Keylet operator()(AccountID const& id, - std::uint32_t seq) const; + Keylet + operator()(AccountID const& id, std::uint32_t seq) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltCHECK, key }; + return {ltCHECK, key}; } }; -static check_t const check {}; +static check_t const check{}; /** A DepositPreauth */ struct depositPreauth_t { explicit depositPreauth_t() = default; - Keylet operator()(AccountID const& owner, - AccountID const& preauthorized) const; + Keylet + operator()(AccountID const& owner, AccountID const& preauthorized) const; - Keylet operator()(uint256 const& key) const + Keylet + operator()(uint256 const& key) const { - return { ltDEPOSIT_PREAUTH, key }; + return {ltDEPOSIT_PREAUTH, key}; } }; -static depositPreauth_t const depositPreauth {}; +static depositPreauth_t const depositPreauth{}; //------------------------------------------------------------------------------ /** Any ledger entry */ -Keylet unchecked(uint256 const& key); +Keylet +unchecked(uint256 const& key); /** The root page of an account's directory */ -Keylet ownerDir (AccountID const& id); +Keylet +ownerDir(AccountID const& id); /** A page in a directory */ /** @{ */ -Keylet page (uint256 const& root, std::uint64_t index); -Keylet page (Keylet const& root, std::uint64_t index); +Keylet +page(uint256 const& root, std::uint64_t index); +Keylet +page(Keylet const& root, std::uint64_t index); /** @} */ // DEPRECATED -inline -Keylet page (uint256 const& key) +inline Keylet +page(uint256 const& key) { - return { ltDIR_NODE, key }; + return {ltDIR_NODE, key}; } /** An escrow entry */ Keylet -escrow (AccountID const& source, std::uint32_t seq); +escrow(AccountID const& source, std::uint32_t seq); /** A PaymentChannel */ Keylet -payChan (AccountID const& source, AccountID const& dst, std::uint32_t seq); +payChan(AccountID const& source, AccountID const& dst, std::uint32_t seq); -} // keylet +} // namespace keylet -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/InnerObjectFormats.h b/src/ripple/protocol/InnerObjectFormats.h index 29297563a5..08d390bac7 100644 --- a/src/ripple/protocol/InnerObjectFormats.h +++ b/src/ripple/protocol/InnerObjectFormats.h @@ -25,21 +25,23 @@ namespace ripple { /** Manages the list of known inner object formats. -*/ -class InnerObjectFormats : public KnownFormats + */ +class InnerObjectFormats : public KnownFormats { private: /** Create the object. This will load the object with all the known inner object formats. */ - InnerObjectFormats (); + InnerObjectFormats(); public: - static InnerObjectFormats const& getInstance (); + static InnerObjectFormats const& + getInstance(); - SOTemplate const* findSOTemplateBySField (SField const& sField) const; + SOTemplate const* + findSOTemplateBySField(SField const& sField) const; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Issue.h b/src/ripple/protocol/Issue.h index 9832fe4aa3..11c45c0136 100644 --- a/src/ripple/protocol/Issue.h +++ b/src/ripple/protocol/Issue.h @@ -37,24 +37,23 @@ public: Currency currency; AccountID account; - Issue () + Issue() { } - Issue (Currency const& c, AccountID const& a) - : currency (c), account (a) + Issue(Currency const& c, AccountID const& a) : currency(c), account(a) { } }; bool -isConsistent (Issue const& ac); +isConsistent(Issue const& ac); std::string -to_string (Issue const& ac); +to_string(Issue const& ac); std::ostream& -operator<< (std::ostream& os, Issue const& x); +operator<<(std::ostream& os, Issue const& x); template void @@ -69,44 +68,46 @@ hash_append(Hasher& h, Issue const& r) if the currency is not XRP. */ int -compare (Issue const& lhs, Issue const& rhs); +compare(Issue const& lhs, Issue const& rhs); /** Equality comparison. */ /** @{ */ bool -operator== (Issue const& lhs, Issue const& rhs); +operator==(Issue const& lhs, Issue const& rhs); bool -operator!= (Issue const& lhs, Issue const& rhs); +operator!=(Issue const& lhs, Issue const& rhs); /** @} */ /** Strict weak ordering. */ /** @{ */ bool -operator< (Issue const& lhs, Issue const& rhs); +operator<(Issue const& lhs, Issue const& rhs); bool -operator> (Issue const& lhs, Issue const& rhs); +operator>(Issue const& lhs, Issue const& rhs); bool -operator>= (Issue const& lhs, Issue const& rhs); +operator>=(Issue const& lhs, Issue const& rhs); bool -operator<= (Issue const& lhs, Issue const& rhs); +operator<=(Issue const& lhs, Issue const& rhs); /** @} */ //------------------------------------------------------------------------------ /** Returns an asset specifier that represents XRP. */ -inline Issue const& xrpIssue () +inline Issue const& +xrpIssue() { - static Issue issue {xrpCurrency(), xrpAccount()}; + static Issue issue{xrpCurrency(), xrpAccount()}; return issue; } /** Returns an asset specifier that represents no account and currency. */ -inline Issue const& noIssue () +inline Issue const& +noIssue() { - static Issue issue {noCurrency(), noAccount()}; + static Issue issue{noCurrency(), noAccount()}; return issue; } -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/KeyType.h b/src/ripple/protocol/KeyType.h index cb96f4a7a5..5a3c6b5d04 100644 --- a/src/ripple/protocol/KeyType.h +++ b/src/ripple/protocol/KeyType.h @@ -20,20 +20,18 @@ #ifndef RIPPLE_PROTOCOL_KEYTYPE_H_INCLUDED #define RIPPLE_PROTOCOL_KEYTYPE_H_INCLUDED -#include #include +#include namespace ripple { -enum class KeyType -{ +enum class KeyType { secp256k1 = 0, - ed25519 = 1, + ed25519 = 1, }; -inline -boost::optional -keyTypeFromString (std::string const& s) +inline boost::optional +keyTypeFromString(std::string const& s) { if (s == "secp256k1") return KeyType::secp256k1; @@ -44,9 +42,8 @@ keyTypeFromString (std::string const& s) return {}; } -inline -char const* -to_string (KeyType type) +inline char const* +to_string(KeyType type) { if (type == KeyType::secp256k1) return "secp256k1"; @@ -58,12 +55,12 @@ to_string (KeyType type) } template -inline -Stream& operator<<(Stream& s, KeyType type) +inline Stream& +operator<<(Stream& s, KeyType type) { return s << to_string(type); } -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/Keylet.h b/src/ripple/protocol/Keylet.h index 13e899441f..a1b2e4267f 100644 --- a/src/ripple/protocol/Keylet.h +++ b/src/ripple/protocol/Keylet.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_PROTOCOL_KEYLET_H_INCLUDED #define RIPPLE_PROTOCOL_KEYLET_H_INCLUDED -#include #include +#include namespace ripple { @@ -40,18 +40,15 @@ struct Keylet LedgerEntryType type; uint256 key; - Keylet (LedgerEntryType type_, - uint256 const& key_) - : type(type_) - , key(key_) + Keylet(LedgerEntryType type_, uint256 const& key_) : type(type_), key(key_) { } /** Returns true if the SLE matches the type */ bool - check (STLedgerEntry const&) const; + check(STLedgerEntry const&) const; }; -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/KnownFormats.h b/src/ripple/protocol/KnownFormats.h index 2500836b60..07781f843e 100644 --- a/src/ripple/protocol/KnownFormats.h +++ b/src/ripple/protocol/KnownFormats.h @@ -40,40 +40,42 @@ class KnownFormats { public: /** A known format. - */ + */ class Item { public: - Item (char const* name, + Item( + char const* name, KeyType type, std::initializer_list uniqueFields, std::initializer_list commonFields) - : soTemplate_ (uniqueFields, commonFields) - , name_ (name) - , type_ (type) + : soTemplate_(uniqueFields, commonFields), name_(name), type_(type) { // Verify that KeyType is appropriate. - static_assert ( + static_assert( std::is_enum::value || - std::is_integral::value, + std::is_integral::value, "KnownFormats KeyType must be integral or enum."); } /** Retrieve the name of the format. - */ - std::string const& getName () const + */ + std::string const& + getName() const { return name_; } /** Retrieve the transaction type this format represents. - */ - KeyType getType () const + */ + KeyType + getType() const { return type_; } - SOTemplate const& getSOTemplate() const + SOTemplate const& + getSOTemplate() const { return soTemplate_; } @@ -88,15 +90,16 @@ public: Derived classes will load the object with all the known formats. */ - KnownFormats () = default; + KnownFormats() = default; /** Destroy the known formats object. The defined formats are deleted. */ - virtual ~KnownFormats () = default; + virtual ~KnownFormats() = default; KnownFormats(KnownFormats const&) = delete; - KnownFormats& operator=(KnownFormats const&) = delete; + KnownFormats& + operator=(KnownFormats const&) = delete; /** Retrieve the type for a format specified by name. @@ -105,21 +108,23 @@ public: @param name The name of the type. @return The type. */ - KeyType findTypeByName (std::string const& name) const + KeyType + findTypeByName(std::string const& name) const { - Item const* const result = findByName (name); + Item const* const result = findByName(name); if (result != nullptr) - return result->getType (); - Throw ("Unknown format name"); - return {}; // Silence compiler warning. + return result->getType(); + Throw("Unknown format name"); + return {}; // Silence compiler warning. } /** Retrieve a format based on its type. - */ - Item const* findByType (KeyType type) const + */ + Item const* + findByType(KeyType type) const { - auto const itr = types_.find (type); + auto const itr = types_.find(type); if (itr == types_.end()) return nullptr; return itr->second; @@ -127,10 +132,11 @@ public: protected: /** Retrieve a format based on its name. - */ - Item const* findByName (std::string const& name) const + */ + Item const* + findByName(std::string const& name) const { - auto const itr = names_.find (name); + auto const itr = names_.find(name); if (itr == names_.end()) return nullptr; return itr->second; @@ -145,12 +151,14 @@ protected: @return The created format. */ - Item const& add (char const* name, KeyType type, + Item const& + add(char const* name, + KeyType type, std::initializer_list uniqueFields, std::initializer_list commonFields = {}) { - formats_.emplace_front (name, type, uniqueFields, commonFields); - Item const& item {formats_.front()}; + formats_.emplace_front(name, type, uniqueFields, commonFields); + Item const& item{formats_.front()}; names_[name] = &item; types_[type] = &item; @@ -168,6 +176,6 @@ private: boost::container::flat_map types_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/LedgerFormats.h b/src/ripple/protocol/LedgerFormats.h index 66a27d9b90..d65aee0e46 100644 --- a/src/ripple/protocol/LedgerFormats.h +++ b/src/ripple/protocol/LedgerFormats.h @@ -33,8 +33,7 @@ namespace ripple { @ingroup protocol */ // Used as the type of a transaction or the type of a ledger entry. -enum LedgerEntryType -{ +enum LedgerEntryType { /** Special type, anything This is used when the type in the Keylet is unknown, such as when building metadata. @@ -45,13 +44,13 @@ enum LedgerEntryType This is used when the type in the Keylet is unknown, such as when iterating */ - ltCHILD = -2, + ltCHILD = -2, - ltINVALID = -1, + ltINVALID = -1, //--------------------------------------------------------------------------- - ltACCOUNT_ROOT = 'a', + ltACCOUNT_ROOT = 'a', /** Directory node. @@ -62,117 +61,119 @@ enum LedgerEntryType (There's a little more information than this, see the template) */ - ltDIR_NODE = 'd', + ltDIR_NODE = 'd', - ltRIPPLE_STATE = 'r', + ltRIPPLE_STATE = 'r', - ltTICKET = 'T', + ltTICKET = 'T', - ltSIGNER_LIST = 'S', + ltSIGNER_LIST = 'S', - ltOFFER = 'o', + ltOFFER = 'o', - ltLEDGER_HASHES = 'h', + ltLEDGER_HASHES = 'h', - ltAMENDMENTS = 'f', + ltAMENDMENTS = 'f', - ltFEE_SETTINGS = 's', + ltFEE_SETTINGS = 's', - ltESCROW = 'u', + ltESCROW = 'u', // Simple unidirection xrp channel - ltPAYCHAN = 'x', + ltPAYCHAN = 'x', - ltCHECK = 'C', + ltCHECK = 'C', - ltDEPOSIT_PREAUTH = 'p', + ltDEPOSIT_PREAUTH = 'p', // No longer used or supported. Left here to prevent accidental // reassignment of the ledger type. - ltNICKNAME = 'n', + ltNICKNAME = 'n', - ltNotUsed01 = 'c', + ltNotUsed01 = 'c', }; /** @ingroup protocol */ // Used as a prefix for computing ledger indexes (keys). -enum LedgerNameSpace -{ - spaceAccount = 'a', - spaceDirNode = 'd', - spaceGenerator = 'g', - spaceRipple = 'r', - spaceOffer = 'o', // Entry for an offer. - spaceOwnerDir = 'O', // Directory of things owned by an account. - spaceBookDir = 'B', // Directory of order books. - spaceContract = 'c', - spaceSkipList = 's', - spaceEscrow = 'u', - spaceAmendment = 'f', - spaceFee = 'e', - spaceTicket = 'T', - spaceSignerList = 'S', - spaceXRPUChannel = 'x', - spaceCheck = 'C', +enum LedgerNameSpace { + spaceAccount = 'a', + spaceDirNode = 'd', + spaceGenerator = 'g', + spaceRipple = 'r', + spaceOffer = 'o', // Entry for an offer. + spaceOwnerDir = 'O', // Directory of things owned by an account. + spaceBookDir = 'B', // Directory of order books. + spaceContract = 'c', + spaceSkipList = 's', + spaceEscrow = 'u', + spaceAmendment = 'f', + spaceFee = 'e', + spaceTicket = 'T', + spaceSignerList = 'S', + spaceXRPUChannel = 'x', + spaceCheck = 'C', spaceDepositPreauth = 'p', // No longer used or supported. Left here to reserve the space and // avoid accidental reuse of the space. - spaceNickname = 'n', + spaceNickname = 'n', }; /** @ingroup protocol */ -enum LedgerSpecificFlags -{ +enum LedgerSpecificFlags { // ltACCOUNT_ROOT - lsfPasswordSpent = 0x00010000, // True, if password set fee is spent. - lsfRequireDestTag = 0x00020000, // True, to require a DestinationTag for payments. - lsfRequireAuth = 0x00040000, // True, to require a authorization to hold IOUs. - lsfDisallowXRP = 0x00080000, // True, to disallow sending XRP. - lsfDisableMaster = 0x00100000, // True, force regular key - lsfNoFreeze = 0x00200000, // True, cannot freeze ripple states - lsfGlobalFreeze = 0x00400000, // True, all assets frozen - lsfDefaultRipple = 0x00800000, // True, trust lines allow rippling by default - lsfDepositAuth = 0x01000000, // True, all deposits require authorization + lsfPasswordSpent = 0x00010000, // True, if password set fee is spent. + lsfRequireDestTag = + 0x00020000, // True, to require a DestinationTag for payments. + lsfRequireAuth = + 0x00040000, // True, to require a authorization to hold IOUs. + lsfDisallowXRP = 0x00080000, // True, to disallow sending XRP. + lsfDisableMaster = 0x00100000, // True, force regular key + lsfNoFreeze = 0x00200000, // True, cannot freeze ripple states + lsfGlobalFreeze = 0x00400000, // True, all assets frozen + lsfDefaultRipple = + 0x00800000, // True, trust lines allow rippling by default + lsfDepositAuth = 0x01000000, // True, all deposits require authorization // ltOFFER - lsfPassive = 0x00010000, - lsfSell = 0x00020000, // True, offer was placed as a sell. + lsfPassive = 0x00010000, + lsfSell = 0x00020000, // True, offer was placed as a sell. // ltRIPPLE_STATE - lsfLowReserve = 0x00010000, // True, if entry counts toward reserve. - lsfHighReserve = 0x00020000, - lsfLowAuth = 0x00040000, - lsfHighAuth = 0x00080000, - lsfLowNoRipple = 0x00100000, - lsfHighNoRipple = 0x00200000, - lsfLowFreeze = 0x00400000, // True, low side has set freeze flag - lsfHighFreeze = 0x00800000, // True, high side has set freeze flag + lsfLowReserve = 0x00010000, // True, if entry counts toward reserve. + lsfHighReserve = 0x00020000, + lsfLowAuth = 0x00040000, + lsfHighAuth = 0x00080000, + lsfLowNoRipple = 0x00100000, + lsfHighNoRipple = 0x00200000, + lsfLowFreeze = 0x00400000, // True, low side has set freeze flag + lsfHighFreeze = 0x00800000, // True, high side has set freeze flag // ltSIGNER_LIST - lsfOneOwnerCount = 0x00010000, // True, uses only one OwnerCount + lsfOneOwnerCount = 0x00010000, // True, uses only one OwnerCount }; //------------------------------------------------------------------------------ /** Holds the list of known ledger entry formats. -*/ -class LedgerFormats : public KnownFormats + */ +class LedgerFormats : public KnownFormats { private: /** Create the object. This will load the object with all the known ledger formats. */ - LedgerFormats (); + LedgerFormats(); public: - static LedgerFormats const& getInstance (); + static LedgerFormats const& + getInstance(); }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/PayChan.h b/src/ripple/protocol/PayChan.h index 5957668e7e..3dae968dfc 100644 --- a/src/ripple/protocol/PayChan.h +++ b/src/ripple/protocol/PayChan.h @@ -20,25 +20,24 @@ #ifndef RIPPLE_PROTOCOL_PAYCHAN_H_INCLUDED #define RIPPLE_PROTOCOL_PAYCHAN_H_INCLUDED -#include #include +#include #include #include namespace ripple { -inline -void -serializePayChanAuthorization ( +inline void +serializePayChanAuthorization( Serializer& msg, uint256 const& key, XRPAmount const& amt) { - msg.add32 (HashPrefix::paymentChannelClaim); - msg.add256 (key); - msg.add64 (amt.drops ()); + msg.add32(HashPrefix::paymentChannelClaim); + msg.add256(key); + msg.add64(amt.drops()); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Protocol.h b/src/ripple/protocol/Protocol.h index f3238a9b1b..12609bef13 100644 --- a/src/ripple/protocol/Protocol.h +++ b/src/ripple/protocol/Protocol.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_PROTOCOL_PROTOCOL_H_INCLUDED #define RIPPLE_PROTOCOL_PROTOCOL_H_INCLUDED -#include #include +#include #include namespace ripple { @@ -64,6 +64,6 @@ using TxID = uint256; using TxSeq = std::uint32_t; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/PublicKey.h b/src/ripple/protocol/PublicKey.h index e4662decdd..34c3d2ecd0 100644 --- a/src/ripple/protocol/PublicKey.h +++ b/src/ripple/protocol/PublicKey.h @@ -23,8 +23,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -60,22 +60,22 @@ class PublicKey { protected: std::size_t size_ = 0; - std::uint8_t buf_[33]; // should be large enough + std::uint8_t buf_[33]; // should be large enough public: using const_iterator = std::uint8_t const*; PublicKey() = default; - PublicKey (PublicKey const& other); - PublicKey& operator= (PublicKey const& other); + PublicKey(PublicKey const& other); + PublicKey& + operator=(PublicKey const& other); /** Create a public key. Preconditions: publicKeyType(slice) != boost::none */ - explicit - PublicKey (Slice const& slice); + explicit PublicKey(Slice const& slice); std::uint8_t const* data() const noexcept @@ -122,7 +122,7 @@ public: Slice slice() const noexcept { - return { buf_, size_ }; + return {buf_, size_}; } operator Slice() const noexcept @@ -132,80 +132,67 @@ public: }; /** Print the public key to a stream. -*/ + */ std::ostream& operator<<(std::ostream& os, PublicKey const& pk); -inline -bool -operator== (PublicKey const& lhs, - PublicKey const& rhs) +inline bool +operator==(PublicKey const& lhs, PublicKey const& rhs) { return lhs.size() == rhs.size() && std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0; } -inline -bool -operator< (PublicKey const& lhs, - PublicKey const& rhs) +inline bool +operator<(PublicKey const& lhs, PublicKey const& rhs) { return std::lexicographical_compare( - lhs.data(), lhs.data() + lhs.size(), - rhs.data(), rhs.data() + rhs.size()); + lhs.data(), + lhs.data() + lhs.size(), + rhs.data(), + rhs.data() + rhs.size()); } template void -hash_append (Hasher& h, - PublicKey const& pk) +hash_append(Hasher& h, PublicKey const& pk) { h(pk.data(), pk.size()); } -template<> +template <> struct STExchange { explicit STExchange() = default; using value_type = PublicKey; - static - void - get (boost::optional& t, - STBlob const& u) + static void + get(boost::optional& t, STBlob const& u) { - t.emplace (Slice(u.data(), u.size())); + t.emplace(Slice(u.data(), u.size())); } - static - std::unique_ptr - set (SField const& f, PublicKey const& t) + static std::unique_ptr + set(SField const& f, PublicKey const& t) { - return std::make_unique( - f, t.data(), t.size()); + return std::make_unique(f, t.data(), t.size()); } }; //------------------------------------------------------------------------------ -inline -std::string -toBase58 (TokenType type, PublicKey const& pk) +inline std::string +toBase58(TokenType type, PublicKey const& pk) { - return base58EncodeToken( - type, pk.data(), pk.size()); + return base58EncodeToken(type, pk.data(), pk.size()); } -template<> +template <> boost::optional -parseBase58 (TokenType type, std::string const& s); +parseBase58(TokenType type, std::string const& s); -enum class ECDSACanonicality -{ - canonical, - fullyCanonical -}; +enum class ECDSACanonicality { canonical, fullyCanonical }; /** Determines the canonicality of a signature. @@ -233,7 +220,7 @@ enum class ECDSACanonicality no verification cryptography is performed. */ boost::optional -ecdsaCanonicality (Slice const& sig); +ecdsaCanonicality(Slice const& sig); /** Returns the type of public key. @@ -242,19 +229,19 @@ ecdsaCanonicality (Slice const& sig); */ /** @{ */ boost::optional -publicKeyType (Slice const& slice); +publicKeyType(Slice const& slice); -inline -boost::optional -publicKeyType (PublicKey const& publicKey) +inline boost::optional +publicKeyType(PublicKey const& publicKey) { - return publicKeyType (publicKey.slice()); + return publicKeyType(publicKey.slice()); } /** @} */ /** Verify a secp256k1 signature on the digest of a message. */ bool -verifyDigest (PublicKey const& publicKey, +verifyDigest( + PublicKey const& publicKey, uint256 const& digest, Slice const& sig, bool mustBeFullyCanonical = true); @@ -264,20 +251,21 @@ verifyDigest (PublicKey const& publicKey, SHA512-Half, and the resulting digest is signed. */ bool -verify (PublicKey const& publicKey, +verify( + PublicKey const& publicKey, Slice const& m, Slice const& sig, bool mustBeFullyCanonical = true); /** Calculate the 160-bit node ID from a node public key. */ NodeID -calcNodeID (PublicKey const&); +calcNodeID(PublicKey const&); // VFALCO This belongs in AccountID.h but // is here because of header issues AccountID -calcAccountID (PublicKey const& pk); +calcAccountID(PublicKey const& pk); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Quality.h b/src/ripple/protocol/Quality.h index 36b7275e62..27026b6740 100644 --- a/src/ripple/protocol/Quality.h +++ b/src/ripple/protocol/Quality.h @@ -40,20 +40,16 @@ namespace ripple { For offers, "in" is always TakerPays and "out" is always TakerGets. */ -template +template struct TAmounts { TAmounts() = default; - TAmounts (beast::Zero, beast::Zero) - : in (beast::zero) - , out (beast::zero) + TAmounts(beast::Zero, beast::Zero) : in(beast::zero), out(beast::zero) { } - TAmounts (In const& in_, Out const& out_) - : in (in_) - , out (out_) + TAmounts(In const& in_, Out const& out_) : in(in_), out(out_) { } @@ -64,14 +60,16 @@ struct TAmounts return in <= beast::zero || out <= beast::zero; } - TAmounts& operator+=(TAmounts const& rhs) + TAmounts& + operator+=(TAmounts const& rhs) { in += rhs.in; out += rhs.out; return *this; } - TAmounts& operator-=(TAmounts const& rhs) + TAmounts& + operator-=(TAmounts const& rhs) { in -= rhs.in; out -= rhs.out; @@ -84,22 +82,18 @@ struct TAmounts using Amounts = TAmounts; -template +template bool -operator== ( - TAmounts const& lhs, - TAmounts const& rhs) noexcept +operator==(TAmounts const& lhs, TAmounts const& rhs) noexcept { return lhs.in == rhs.in && lhs.out == rhs.out; } -template +template bool -operator!= ( - TAmounts const& lhs, - TAmounts const& rhs) noexcept +operator!=(TAmounts const& lhs, TAmounts const& rhs) noexcept { - return ! (lhs == rhs); + return !(lhs == rhs); } //------------------------------------------------------------------------------ @@ -123,28 +117,27 @@ public: static const int maxTickSize = 16; private: - // This has the same representation as STAmount, see the comment on the STAmount. - // However, this class does not alway use the canonical representation. In particular, - // the increment and decrement operators may cause a non-canonical representation. + // This has the same representation as STAmount, see the comment on the + // STAmount. However, this class does not alway use the canonical + // representation. In particular, the increment and decrement operators may + // cause a non-canonical representation. value_type m_value; public: Quality() = default; /** Create a quality from the integer encoding of an STAmount */ - explicit - Quality (std::uint64_t value); + explicit Quality(std::uint64_t value); /** Create a quality from the ratio of two amounts. */ - explicit - Quality (Amounts const& amount); + explicit Quality(Amounts const& amount); /** Create a quality from the ratio of two amounts. */ - template - Quality (Out const& out, In const& in) - : Quality (Amounts (toSTAmount (in), - toSTAmount (out))) - {} + template + Quality(Out const& out, In const& in) + : Quality(Amounts(toSTAmount(in), toSTAmount(out))) + { + } /** Advances to the next higher quality level. */ /** @{ */ @@ -152,7 +145,7 @@ public: operator++(); Quality - operator++ (int); + operator++(int); /** @} */ /** Advances to the next lower quality level. */ @@ -161,42 +154,43 @@ public: operator--(); Quality - operator-- (int); + operator--(int); /** @} */ /** Returns the quality as STAmount. */ STAmount - rate () const + rate() const { - return amountFromQuality (m_value); + return amountFromQuality(m_value); } /** Returns the quality rounded up to the specified number of decimal digits. */ Quality - round (int tickSize) const; + round(int tickSize) const; /** Returns the scaled amount with in capped. Math is avoided if the result is exact. The output is clamped to prevent money creation. */ Amounts - ceil_in (Amounts const& amount, STAmount const& limit) const; + ceil_in(Amounts const& amount, STAmount const& limit) const; - template + template TAmounts - ceil_in (TAmounts const& amount, In const& limit) const + ceil_in(TAmounts const& amount, In const& limit) const { if (amount.in <= limit) return amount; // Use the existing STAmount implementation for now, but consider // replacing with code specific to IOUAMount and XRPAmount - Amounts stAmt (toSTAmount (amount.in), toSTAmount (amount.out)); - STAmount stLim (toSTAmount (limit)); - auto const stRes = ceil_in (stAmt, stLim); - return TAmounts (toAmount (stRes.in), toAmount (stRes.out)); + Amounts stAmt(toSTAmount(amount.in), toSTAmount(amount.out)); + STAmount stLim(toSTAmount(limit)); + auto const stRes = ceil_in(stAmt, stLim); + return TAmounts( + toAmount(stRes.in), toAmount(stRes.out)); } /** Returns the scaled amount with out capped. @@ -204,79 +198,73 @@ public: to prevent money creation. */ Amounts - ceil_out (Amounts const& amount, STAmount const& limit) const; + ceil_out(Amounts const& amount, STAmount const& limit) const; - template + template TAmounts - ceil_out (TAmounts const& amount, Out const& limit) const + ceil_out(TAmounts const& amount, Out const& limit) const { if (amount.out <= limit) return amount; // Use the existing STAmount implementation for now, but consider // replacing with code specific to IOUAMount and XRPAmount - Amounts stAmt (toSTAmount (amount.in), toSTAmount (amount.out)); - STAmount stLim (toSTAmount (limit)); - auto const stRes = ceil_out (stAmt, stLim); - return TAmounts (toAmount (stRes.in), toAmount (stRes.out)); + Amounts stAmt(toSTAmount(amount.in), toSTAmount(amount.out)); + STAmount stLim(toSTAmount(limit)); + auto const stRes = ceil_out(stAmt, stLim); + return TAmounts( + toAmount(stRes.in), toAmount(stRes.out)); } /** Returns `true` if lhs is lower quality than `rhs`. Lower quality means the taker receives a worse deal. Higher quality is better for the taker. */ - friend - bool - operator< (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator<(Quality const& lhs, Quality const& rhs) noexcept { return lhs.m_value > rhs.m_value; } - friend - bool - operator> (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator>(Quality const& lhs, Quality const& rhs) noexcept { return lhs.m_value < rhs.m_value; } - friend - bool - operator<= (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator<=(Quality const& lhs, Quality const& rhs) noexcept { return !(lhs > rhs); } - friend - bool - operator>= (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator>=(Quality const& lhs, Quality const& rhs) noexcept { return !(lhs < rhs); } - friend - bool - operator== (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator==(Quality const& lhs, Quality const& rhs) noexcept { return lhs.m_value == rhs.m_value; } - friend - bool - operator!= (Quality const& lhs, Quality const& rhs) noexcept + friend bool + operator!=(Quality const& lhs, Quality const& rhs) noexcept { - return ! (lhs == rhs); + return !(lhs == rhs); } - friend - std::ostream& - operator<< (std::ostream& os, Quality const& quality) + friend std::ostream& + operator<<(std::ostream& os, Quality const& quality) { os << quality.m_value; return os; } - // return the relative distance (relative error) between two qualities. This is used for testing only. - // relative distance is abs(a-b)/min(a,b) + // return the relative distance (relative error) between two qualities. This + // is used for testing only. relative distance is abs(a-b)/min(a,b) friend double relativeDistance(Quality const& q1, Quality const& q2) { @@ -314,8 +302,8 @@ public: @param rhs The second leg of the path: intermediate to output. */ Quality -composed_quality (Quality const& lhs, Quality const& rhs); +composed_quality(Quality const& lhs, Quality const& rhs); -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/Rate.h b/src/ripple/protocol/Rate.h index cbb4894ab7..a982596e51 100644 --- a/src/ripple/protocol/Rate.h +++ b/src/ripple/protocol/Rate.h @@ -34,73 +34,57 @@ namespace ripple { For example, a transfer rate of 1% is represented as 1,010,000,000. */ -struct Rate - : private boost::totally_ordered +struct Rate : private boost::totally_ordered { std::uint32_t value; - Rate () = delete; + Rate() = delete; - explicit - Rate (std::uint32_t rate) - : value (rate) + explicit Rate(std::uint32_t rate) : value(rate) { } }; -inline -bool -operator== (Rate const& lhs, Rate const& rhs) noexcept +inline bool +operator==(Rate const& lhs, Rate const& rhs) noexcept { return lhs.value == rhs.value; } -inline -bool -operator< (Rate const& lhs, Rate const& rhs) noexcept +inline bool +operator<(Rate const& lhs, Rate const& rhs) noexcept { return lhs.value < rhs.value; } -inline -std::ostream& -operator<< (std::ostream& os, Rate const& rate) +inline std::ostream& +operator<<(std::ostream& os, Rate const& rate) { os << rate.value; return os; } STAmount -multiply ( - STAmount const& amount, - Rate const& rate); +multiply(STAmount const& amount, Rate const& rate); STAmount -multiplyRound ( - STAmount const& amount, - Rate const& rate, - bool roundUp); +multiplyRound(STAmount const& amount, Rate const& rate, bool roundUp); STAmount -multiplyRound ( +multiplyRound( STAmount const& amount, Rate const& rate, Issue const& issue, bool roundUp); STAmount -divide ( - STAmount const& amount, - Rate const& rate); +divide(STAmount const& amount, Rate const& rate); STAmount -divideRound ( - STAmount const& amount, - Rate const& rate, - bool roundUp); +divideRound(STAmount const& amount, Rate const& rate, bool roundUp); STAmount -divideRound ( +divideRound( STAmount const& amount, Rate const& rate, Issue const& issue, @@ -109,6 +93,6 @@ divideRound ( /** A transfer rate signifying a 1:1 exchange */ extern Rate const parityRate; -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/SField.h b/src/ripple/protocol/SField.h index cb784b2581..3ad477899c 100644 --- a/src/ripple/protocol/SField.h +++ b/src/ripple/protocol/SField.h @@ -49,12 +49,11 @@ template class STInteger; class STVector256; -enum SerializedTypeID -{ +enum SerializedTypeID { // special types - STI_UNKNOWN = -2, - STI_DONE = -1, - STI_NOTPRESENT = 0, + STI_UNKNOWN = -2, + STI_DONE = -1, + STI_NOTPRESENT = 0, // // types (common) STI_UINT16 = 1, @@ -79,21 +78,19 @@ enum SerializedTypeID // cannot be serialized inside other types STI_TRANSACTION = 10001, STI_LEDGERENTRY = 10002, - STI_VALIDATION = 10003, - STI_METADATA = 10004, + STI_VALIDATION = 10003, + STI_METADATA = 10004, }; // constexpr -inline -int +inline int field_code(SerializedTypeID id, int index) { return (safe_cast(id) << 16) | index; } // constexpr -inline -int +inline int field_code(int id, int index) { return (id << 16) | index; @@ -112,91 +109,105 @@ field_code(int id, int index) class SField { public: - enum - { - sMD_Never = 0x00, - sMD_ChangeOrig = 0x01, // original value when it changes - sMD_ChangeNew = 0x02, // new value when it changes - sMD_DeleteFinal = 0x04, // final value when it is deleted - sMD_Create = 0x08, // value when it's created - sMD_Always = 0x10, // value when node containing it is affected at all - sMD_Default = sMD_ChangeOrig | sMD_ChangeNew | sMD_DeleteFinal | sMD_Create + enum { + sMD_Never = 0x00, + sMD_ChangeOrig = 0x01, // original value when it changes + sMD_ChangeNew = 0x02, // new value when it changes + sMD_DeleteFinal = 0x04, // final value when it is deleted + sMD_Create = 0x08, // value when it's created + sMD_Always = 0x10, // value when node containing it is affected at all + sMD_Default = + sMD_ChangeOrig | sMD_ChangeNew | sMD_DeleteFinal | sMD_Create }; - enum class IsSigning : unsigned char - { - no, - yes - }; - static IsSigning const notSigning = IsSigning::no; + enum class IsSigning : unsigned char { no, yes }; + static IsSigning const notSigning = IsSigning::no; - int const fieldCode; // (type<<16)|index - SerializedTypeID const fieldType; // STI_* - int const fieldValue; // Code number for protocol - std::string const fieldName; - int const fieldMeta; - int const fieldNum; - IsSigning const signingField; + int const fieldCode; // (type<<16)|index + SerializedTypeID const fieldType; // STI_* + int const fieldValue; // Code number for protocol + std::string const fieldName; + int const fieldMeta; + int const fieldNum; + IsSigning const signingField; Json::StaticString const jsonName; SField(SField const&) = delete; - SField& operator=(SField const&) = delete; + SField& + operator=(SField const&) = delete; SField(SField&&) = delete; - SField& operator=(SField&&) = delete; + SField& + operator=(SField&&) = delete; public: - struct private_access_tag_t; // public, but still an implementation detail + struct private_access_tag_t; // public, but still an implementation detail // These constructors can only be called from SField.cpp - SField (private_access_tag_t, SerializedTypeID tid, int fv, - const char* fn, int meta = sMD_Default, + SField( + private_access_tag_t, + SerializedTypeID tid, + int fv, + const char* fn, + int meta = sMD_Default, IsSigning signing = IsSigning::yes); - explicit SField (private_access_tag_t, int fc); + explicit SField(private_access_tag_t, int fc); - static const SField& getField (int fieldCode); - static const SField& getField (std::string const& fieldName); - static const SField& getField (int type, int value) + static const SField& + getField(int fieldCode); + static const SField& + getField(std::string const& fieldName); + static const SField& + getField(int type, int value) { - return getField (field_code (type, value)); + return getField(field_code(type, value)); } - static const SField& getField (SerializedTypeID type, int value) + static const SField& + getField(SerializedTypeID type, int value) { - return getField (field_code (type, value)); + return getField(field_code(type, value)); } - std::string const& getName () const + std::string const& + getName() const { return fieldName; } - bool hasName () const + bool + hasName() const { return fieldCode > 0; } - Json::StaticString const& getJsonName () const + Json::StaticString const& + getJsonName() const { return jsonName; } - bool isGeneric () const + bool + isGeneric() const { return fieldCode == 0; } - bool isInvalid () const + bool + isInvalid() const { return fieldCode == -1; } - bool isUseful () const + bool + isUseful() const { return fieldCode > 0; } - bool isKnown () const + bool + isKnown() const { return fieldType != STI_UNKNOWN; } - bool isBinary () const + bool + isBinary() const { return fieldValue < 256; } @@ -205,50 +216,60 @@ public: // should be discarded during serialization,like 'hash'. // You cannot serialize an object's hash inside that object, // but you can have it in the JSON representation. - bool isDiscardable () const + bool + isDiscardable() const { return fieldValue > 256; } - int getCode () const + int + getCode() const { return fieldCode; } - int getNum () const + int + getNum() const { return fieldNum; } - static int getNumFields () + static int + getNumFields() { return num; } - bool isSigningField () const + bool + isSigningField() const { return signingField == IsSigning::yes; } - bool shouldMeta (int c) const + bool + shouldMeta(int c) const { return (fieldMeta & c) != 0; } - bool shouldInclude (bool withSigningField) const + bool + shouldInclude(bool withSigningField) const { return (fieldValue < 256) && (withSigningField || (signingField == IsSigning::yes)); } - bool operator== (const SField& f) const + bool + operator==(const SField& f) const { return fieldCode == f.fieldCode; } - bool operator!= (const SField& f) const + bool + operator!=(const SField& f) const { return fieldCode != f.fieldCode; } - static int compare (const SField& f1, const SField& f2); + static int + compare(const SField& f1, const SField& f2); private: static int num; @@ -262,14 +283,11 @@ struct TypedField : SField using type = T; template - explicit - TypedField (Args&&... args) - : SField(std::forward(args)...) + explicit TypedField(Args&&... args) : SField(std::forward(args)...) { } - TypedField (TypedField&& u) - : SField(std::move(u)) + TypedField(TypedField&& u) : SField(std::move(u)) { } }; @@ -280,16 +298,13 @@ struct OptionaledField { TypedField const* f; - explicit - OptionaledField (TypedField const& f_) - : f (&f_) + explicit OptionaledField(TypedField const& f_) : f(&f_) { } }; template -inline -OptionaledField +inline OptionaledField operator~(TypedField const& f) { return OptionaledField(f); @@ -387,7 +402,6 @@ extern SF_U64 const sfHighNode; extern SF_U64 const sfDestinationNode; extern SF_U64 const sfCookie; - // 128-bit extern SF_U128 const sfEmailHash; @@ -503,6 +517,6 @@ extern SField const sfMajorities; //------------------------------------------------------------------------------ -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/SOTemplate.h b/src/ripple/protocol/SOTemplate.h index dd46f93f05..45a10bbab3 100644 --- a/src/ripple/protocol/SOTemplate.h +++ b/src/ripple/protocol/SOTemplate.h @@ -29,12 +29,11 @@ namespace ripple { /** Kind of element in each entry of an SOTemplate. */ -enum SOEStyle -{ - soeINVALID = -1, - soeREQUIRED = 0, // required - soeOPTIONAL = 1, // optional, may be present with default value - soeDEFAULT = 2, // optional, if present, must not have default value +enum SOEStyle { + soeINVALID = -1, + soeREQUIRED = 0, // required + soeOPTIONAL = 1, // optional, may be present with default value + soeDEFAULT = 2, // optional, if present, must not have default value }; //------------------------------------------------------------------------------ @@ -44,23 +43,24 @@ class SOElement { // Use std::reference_wrapper so SOElement can be stored in a std::vector. std::reference_wrapper sField_; - SOEStyle style_; + SOEStyle style_; public: - SOElement (SField const& fieldName, SOEStyle style) - : sField_ (fieldName) - , style_ (style) + SOElement(SField const& fieldName, SOEStyle style) + : sField_(fieldName), style_(style) { - if (! sField_.get().isUseful()) - Throw ("SField in SOElement must be useful."); + if (!sField_.get().isUseful()) + Throw("SField in SOElement must be useful."); } - SField const& sField () const + SField const& + sField() const { return sField_.get(); } - SOEStyle style () const + SOEStyle + style() const { return style_; } @@ -78,44 +78,52 @@ public: // Copying vectors is expensive. Make this a move-only type until // there is motivation to change that. SOTemplate(SOTemplate&& other) = default; - SOTemplate& operator=(SOTemplate&& other) = default; + SOTemplate& + operator=(SOTemplate&& other) = default; /** Create a template populated with all fields. After creating the template fields cannot be added, modified, or removed. */ - SOTemplate (std::initializer_list uniqueFields, + SOTemplate( + std::initializer_list uniqueFields, std::initializer_list commonFields = {}); /* Provide for the enumeration of fields */ - std::vector::const_iterator begin() const + std::vector::const_iterator + begin() const { return elements_.cbegin(); } - std::vector::const_iterator cbegin() const + std::vector::const_iterator + cbegin() const { return begin(); } - std::vector::const_iterator end() const + std::vector::const_iterator + end() const { return elements_.cend(); } - std::vector::const_iterator cend() const + std::vector::const_iterator + cend() const { return end(); } /** The number of entries in this template */ - std::size_t size () const + std::size_t + size() const { - return elements_.size (); + return elements_.size(); } /** Retrieve the position of a named field. */ - int getIndex (SField const&) const; + int + getIndex(SField const&) const; SOEStyle style(SField const& sf) const @@ -125,9 +133,9 @@ public: private: std::vector elements_; - std::vector indices_; // field num -> index + std::vector indices_; // field num -> index }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STAccount.h b/src/ripple/protocol/STAccount.h index 1cb39f77b4..a7f655f916 100644 --- a/src/ripple/protocol/STAccount.h +++ b/src/ripple/protocol/STAccount.h @@ -26,8 +26,7 @@ namespace ripple { -class STAccount final - : public STBase +class STAccount final : public STBase { private: // The original implementation of STAccount kept the value in an STBlob. @@ -40,61 +39,63 @@ private: public: using value_type = AccountID; - STAccount (); - STAccount (SField const& n); - STAccount (SField const& n, Buffer&& v); - STAccount (SerialIter& sit, SField const& name); - STAccount (SField const& n, AccountID const& v); + STAccount(); + STAccount(SField const& n); + STAccount(SField const& n, Buffer&& v); + STAccount(SerialIter& sit, SField const& name); + STAccount(SField const& n, AccountID const& v); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { - return emplace (n, buf, *this); + return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { - return emplace (n, buf, std::move(*this)); + return emplace(n, buf, std::move(*this)); } - SerializedTypeID getSType () const override + SerializedTypeID + getSType() const override { return STI_ACCOUNT; } - std::string getText () const override; + std::string + getText() const override; void - add (Serializer& s) const override + add(Serializer& s) const override { - assert (fName->isBinary ()); - assert (fName->fieldType == STI_ACCOUNT); + assert(fName->isBinary()); + assert(fName->fieldType == STI_ACCOUNT); // Preserve the serialization behavior of an STBlob: // o If we are default (all zeros) serialize as an empty blob. // o Otherwise serialize 160 bits. int const size = isDefault() ? 0 : uint160::bytes; - s.addVL (value_.data(), size); + s.addVL(value_.data(), size); } bool - isEquivalent (const STBase& t) const override + isEquivalent(const STBase& t) const override { auto const* const tPtr = dynamic_cast(&t); return tPtr && (default_ == tPtr->default_) && (value_ == tPtr->value_); } bool - isDefault () const override + isDefault() const override { return default_; } STAccount& - operator= (AccountID const& value) + operator=(AccountID const& value) { - setValue (value); + setValue(value); return *this; } @@ -104,13 +105,14 @@ public: return value_; } - void setValue (AccountID const& v) + void + setValue(AccountID const& v) { value_ = v; default_ = false; } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STAmount.h b/src/ripple/protocol/STAmount.h index 841961bb4a..1f70f5f022 100644 --- a/src/ripple/protocol/STAmount.h +++ b/src/ripple/protocol/STAmount.h @@ -22,10 +22,10 @@ #include #include -#include -#include -#include #include +#include +#include +#include namespace ripple { @@ -39,19 +39,18 @@ namespace ripple { // Wire form: // High 8 bits are (offset+142), legal range is, 80 to 22 inclusive // Low 56 bits are value, legal range is 10^15 to (10^16 - 1) inclusive -class STAmount - : public STBase +class STAmount : public STBase { public: using mantissa_type = std::uint64_t; using exponent_type = int; - using rep = std::pair ; + using rep = std::pair; private: Issue mIssue; mantissa_type mValue; exponent_type mOffset; - bool mIsNative; // A shorthand for isXRP(mIssue). + bool mIsNative; // A shorthand for isXRP(mIssue). bool mIsNegative; public: @@ -61,14 +60,14 @@ public: static const int cMaxOffset = 80; // Maximum native value supported by the code - static const std::uint64_t cMinValue = 1000000000000000ull; - static const std::uint64_t cMaxValue = 9999999999999999ull; - static const std::uint64_t cMaxNative = 9000000000000000000ull; + static const std::uint64_t cMinValue = 1000000000000000ull; + static const std::uint64_t cMaxValue = 9999999999999999ull; + static const std::uint64_t cMaxNative = 9000000000000000000ull; // Max native value on network. static const std::uint64_t cMaxNativeN = 100000000000000000ull; - static const std::uint64_t cNotNative = 0x8000000000000000ull; - static const std::uint64_t cPosNative = 0x4000000000000000ull; + static const std::uint64_t cNotNative = 0x8000000000000000ull; + static const std::uint64_t cPosNative = 0x4000000000000000ull; static std::uint64_t const uRateOne; @@ -81,53 +80,77 @@ public: }; // Do not call canonicalize - STAmount (SField const& name, Issue const& issue, - mantissa_type mantissa, exponent_type exponent, - bool native, bool negative, unchecked); + STAmount( + SField const& name, + Issue const& issue, + mantissa_type mantissa, + exponent_type exponent, + bool native, + bool negative, + unchecked); - STAmount (Issue const& issue, - mantissa_type mantissa, exponent_type exponent, - bool native, bool negative, unchecked); + STAmount( + Issue const& issue, + mantissa_type mantissa, + exponent_type exponent, + bool native, + bool negative, + unchecked); // Call canonicalize - STAmount (SField const& name, Issue const& issue, - mantissa_type mantissa, exponent_type exponent, - bool native, bool negative); + STAmount( + SField const& name, + Issue const& issue, + mantissa_type mantissa, + exponent_type exponent, + bool native, + bool negative); - STAmount (SField const& name, std::int64_t mantissa); + STAmount(SField const& name, std::int64_t mantissa); - STAmount (SField const& name, - std::uint64_t mantissa = 0, bool negative = false); + STAmount( + SField const& name, + std::uint64_t mantissa = 0, + bool negative = false); - STAmount (SField const& name, Issue const& issue, - std::uint64_t mantissa = 0, int exponent = 0, bool negative = false); + STAmount( + SField const& name, + Issue const& issue, + std::uint64_t mantissa = 0, + int exponent = 0, + bool negative = false); - explicit - STAmount (std::uint64_t mantissa = 0, bool negative = false); + explicit STAmount(std::uint64_t mantissa = 0, bool negative = false); - STAmount (Issue const& issue, std::uint64_t mantissa = 0, int exponent = 0, + STAmount( + Issue const& issue, + std::uint64_t mantissa = 0, + int exponent = 0, bool negative = false); // VFALCO Is this needed when we have the previous signature? - STAmount (Issue const& issue, std::uint32_t mantissa, int exponent = 0, + STAmount( + Issue const& issue, + std::uint32_t mantissa, + int exponent = 0, bool negative = false); - STAmount (Issue const& issue, std::int64_t mantissa, int exponent = 0); + STAmount(Issue const& issue, std::int64_t mantissa, int exponent = 0); - STAmount (Issue const& issue, int mantissa, int exponent = 0); + STAmount(Issue const& issue, int mantissa, int exponent = 0); // Legacy support for new-style amounts - STAmount (IOUAmount const& amount, Issue const& issue); - STAmount (XRPAmount const& amount); + STAmount(IOUAmount const& amount, Issue const& issue); + STAmount(XRPAmount const& amount); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } @@ -135,12 +158,13 @@ public: //-------------------------------------------------------------------------- private: - static - std::unique_ptr - construct (SerialIter&, SField const& name); + static std::unique_ptr + construct(SerialIter&, SField const& name); - void set (std::int64_t v); - void canonicalize(); + void + set(std::int64_t v); + void + canonicalize(); public: //-------------------------------------------------------------------------- @@ -149,15 +173,43 @@ public: // //-------------------------------------------------------------------------- - int exponent() const noexcept { return mOffset; } - bool native() const noexcept { return mIsNative; } - bool negative() const noexcept { return mIsNegative; } - std::uint64_t mantissa() const noexcept { return mValue; } - Issue const& issue() const { return mIssue; } + int + exponent() const noexcept + { + return mOffset; + } + bool + native() const noexcept + { + return mIsNative; + } + bool + negative() const noexcept + { + return mIsNegative; + } + std::uint64_t + mantissa() const noexcept + { + return mValue; + } + Issue const& + issue() const + { + return mIssue; + } // These three are deprecated - Currency const& getCurrency() const { return mIssue.currency; } - AccountID const& getIssuer() const { return mIssue.account; } + Currency const& + getCurrency() const + { + return mIssue.currency; + } + AccountID const& + getIssuer() const + { + return mIssue.account; + } int signum() const noexcept @@ -169,11 +221,11 @@ public: STAmount zeroed() const { - return STAmount (mIssue); + return STAmount(mIssue); } void - setJson (Json::Value&) const; + setJson(Json::Value&) const; STAmount const& value() const noexcept @@ -192,18 +244,21 @@ public: return *this != beast::zero; } - STAmount& operator+= (STAmount const&); - STAmount& operator-= (STAmount const&); + STAmount& + operator+=(STAmount const&); + STAmount& + operator-=(STAmount const&); - STAmount& operator= (beast::Zero) + STAmount& operator=(beast::Zero) { clear(); return *this; } - STAmount& operator= (XRPAmount const& amount) + STAmount& + operator=(XRPAmount const& amount) { - *this = STAmount (amount); + *this = STAmount(amount); return *this; } @@ -213,13 +268,15 @@ public: // //-------------------------------------------------------------------------- - void negate() + void + negate() { if (*this != beast::zero) mIsNegative = !mIsNegative; } - void clear() + void + clear() { // The -100 is used to allow 0 to sort less than a small positive values // which have a negative exponent. @@ -229,25 +286,29 @@ public: } // Zero while copying currency and issuer. - void clear (STAmount const& saTmpl) + void + clear(STAmount const& saTmpl) { - clear (saTmpl.mIssue); + clear(saTmpl.mIssue); } - void clear (Issue const& issue) + void + clear(Issue const& issue) { setIssue(issue); clear(); } - void setIssuer (AccountID const& uIssuer) + void + setIssuer(AccountID const& uIssuer) { mIssue.account = uIssuer; setIssue(mIssue); } /** Set the Issue for this amount and update mIsNative. */ - void setIssue (Issue const& issue); + void + setIssue(Issue const& issue); //-------------------------------------------------------------------------- // @@ -267,14 +328,13 @@ public: std::string getText() const override; - Json::Value - getJson (JsonOptions) const override; + Json::Value getJson(JsonOptions) const override; void - add (Serializer& s) const override; + add(Serializer& s) const override; bool - isEquivalent (const STBase& t) const override; + isEquivalent(const STBase& t) const override; bool isDefault() const override @@ -282,8 +342,10 @@ public: return (mValue == 0) && mIsNative; } - XRPAmount xrp () const; - IOUAmount iou () const; + XRPAmount + xrp() const; + IOUAmount + iou() const; }; //------------------------------------------------------------------------------ @@ -294,22 +356,21 @@ public: // VFALCO TODO The parameter type should be Quality not uint64_t STAmount -amountFromQuality (std::uint64_t rate); +amountFromQuality(std::uint64_t rate); STAmount -amountFromString (Issue const& issue, std::string const& amount); +amountFromString(Issue const& issue, std::string const& amount); STAmount -amountFromJson (SField const& name, Json::Value const& v); +amountFromJson(SField const& name, Json::Value const& v); bool -amountFromJsonNoThrow (STAmount& result, Json::Value const& jvSource); +amountFromJsonNoThrow(STAmount& result, Json::Value const& jvSource); // IOUAmount and XRPAmount define toSTAmount, defining this // trivial conversion here makes writing generic code easier -inline -STAmount const& -toSTAmount (STAmount const& a) +inline STAmount const& +toSTAmount(STAmount const& a) { return a; } @@ -320,11 +381,10 @@ toSTAmount (STAmount const& a) // //------------------------------------------------------------------------------ -inline -bool -isLegalNet (STAmount const& value) +inline bool +isLegalNet(STAmount const& value) { - return ! value.native() || (value.mantissa() <= STAmount::cMaxNativeN); + return !value.native() || (value.mantissa() <= STAmount::cMaxNativeN); } //------------------------------------------------------------------------------ @@ -333,38 +393,37 @@ isLegalNet (STAmount const& value) // //------------------------------------------------------------------------------ -bool operator== (STAmount const& lhs, STAmount const& rhs); -bool operator< (STAmount const& lhs, STAmount const& rhs); - -inline bool -operator!= (STAmount const& lhs, STAmount const& rhs) +operator==(STAmount const& lhs, STAmount const& rhs); +bool +operator<(STAmount const& lhs, STAmount const& rhs); + +inline bool +operator!=(STAmount const& lhs, STAmount const& rhs) { return !(lhs == rhs); } -inline -bool -operator> (STAmount const& lhs, STAmount const& rhs) +inline bool +operator>(STAmount const& lhs, STAmount const& rhs) { return rhs < lhs; } -inline -bool -operator<= (STAmount const& lhs, STAmount const& rhs) +inline bool +operator<=(STAmount const& lhs, STAmount const& rhs) { return !(rhs < lhs); } -inline -bool -operator>= (STAmount const& lhs, STAmount const& rhs) +inline bool +operator>=(STAmount const& lhs, STAmount const& rhs) { return !(lhs < rhs); } -STAmount operator- (STAmount const& value); +STAmount +operator-(STAmount const& value); //------------------------------------------------------------------------------ // @@ -372,37 +431,46 @@ STAmount operator- (STAmount const& value); // //------------------------------------------------------------------------------ -STAmount operator+ (STAmount const& v1, STAmount const& v2); -STAmount operator- (STAmount const& v1, STAmount const& v2); +STAmount +operator+(STAmount const& v1, STAmount const& v2); +STAmount +operator-(STAmount const& v1, STAmount const& v2); STAmount -divide (STAmount const& v1, STAmount const& v2, Issue const& issue); +divide(STAmount const& v1, STAmount const& v2, Issue const& issue); STAmount -multiply (STAmount const& v1, STAmount const& v2, Issue const& issue); +multiply(STAmount const& v1, STAmount const& v2, Issue const& issue); // multiply, or divide rounding result in specified direction STAmount -mulRound (STAmount const& v1, STAmount const& v2, - Issue const& issue, bool roundUp); +mulRound( + STAmount const& v1, + STAmount const& v2, + Issue const& issue, + bool roundUp); STAmount -divRound (STAmount const& v1, STAmount const& v2, - Issue const& issue, bool roundUp); +divRound( + STAmount const& v1, + STAmount const& v2, + Issue const& issue, + bool roundUp); // Someone is offering X for Y, what is the rate? // Rate: smaller is better, the taker wants the most out: in/out // VFALCO TODO Return a Quality object std::uint64_t -getRate (STAmount const& offerOut, STAmount const& offerIn); +getRate(STAmount const& offerOut, STAmount const& offerIn); //------------------------------------------------------------------------------ -inline bool isXRP(STAmount const& amount) +inline bool +isXRP(STAmount const& amount) { - return isXRP (amount.issue().currency); + return isXRP(amount.issue().currency); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STArray.h b/src/ripple/protocol/STArray.h index 3cd13505dd..419aa0e0ee 100644 --- a/src/ripple/protocol/STArray.h +++ b/src/ripple/protocol/STArray.h @@ -25,17 +25,12 @@ namespace ripple { -class STArray final - : public STBase - , public CountedObject +class STArray final : public STBase, public CountedObject { private: using list_type = std::vector; - enum - { - reserveSize = 8 - }; + enum { reserveSize = 8 }; list_type v_; @@ -43,47 +38,63 @@ public: // Read-only iteration class Items; - static char const* getCountedObjectName () { return "STArray"; } + static char const* + getCountedObjectName() + { + return "STArray"; + } using size_type = list_type::size_type; using iterator = list_type::iterator; using const_iterator = list_type::const_iterator; STArray(); - STArray (STArray&&); - STArray (STArray const&) = default; - STArray (SField const& f, int n); - STArray (SerialIter& sit, SField const& f, int depth = 0); - explicit STArray (int n); - explicit STArray (SField const& f); - STArray& operator= (STArray const&) = default; - STArray& operator= (STArray&&); + STArray(STArray&&); + STArray(STArray const&) = default; + STArray(SField const& f, int n); + STArray(SerialIter& sit, SField const& f, int depth = 0); + explicit STArray(int n); + explicit STArray(SField const& f); + STArray& + operator=(STArray const&) = default; + STArray& + operator=(STArray&&); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } - STObject& operator[] (std::size_t j) + STObject& + operator[](std::size_t j) { return v_[j]; } - STObject const& operator[] (std::size_t j) const + STObject const& + operator[](std::size_t j) const { return v_[j]; } - STObject& back() { return v_.back(); } + STObject& + back() + { + return v_.back(); + } - STObject const& back() const { return v_.back(); } + STObject const& + back() const + { + return v_.back(); + } template void @@ -92,86 +103,107 @@ public: v_.emplace_back(std::forward(args)...); } - void push_back (STObject const& object) + void + push_back(STObject const& object) { v_.push_back(object); } - void push_back (STObject&& object) + void + push_back(STObject&& object) { v_.push_back(std::move(object)); } - iterator begin () + iterator + begin() { - return v_.begin (); + return v_.begin(); } - iterator end () + iterator + end() { - return v_.end (); + return v_.end(); } - const_iterator begin () const + const_iterator + begin() const { - return v_.begin (); + return v_.begin(); } - const_iterator end () const + const_iterator + end() const { - return v_.end (); + return v_.end(); } - size_type size () const + size_type + size() const { - return v_.size (); + return v_.size(); } - bool empty () const + bool + empty() const { - return v_.empty (); + return v_.empty(); } - void clear () + void + clear() { - v_.clear (); + v_.clear(); } - void reserve (std::size_t n) + void + reserve(std::size_t n) { - v_.reserve (n); + v_.reserve(n); } - void swap (STArray & a) noexcept + void + swap(STArray& a) noexcept { - v_.swap (a.v_); + v_.swap(a.v_); } - virtual std::string getFullText () const override; - virtual std::string getText () const override; + virtual std::string + getFullText() const override; + virtual std::string + getText() const override; - virtual Json::Value getJson (JsonOptions index) const override; - virtual void add (Serializer & s) const override; + virtual Json::Value + getJson(JsonOptions index) const override; + virtual void + add(Serializer& s) const override; - void sort (bool (*compare) (const STObject & o1, const STObject & o2)); + void + sort(bool (*compare)(const STObject& o1, const STObject& o2)); - bool operator== (const STArray & s) const + bool + operator==(const STArray& s) const { return v_ == s.v_; } - bool operator!= (const STArray & s) const + bool + operator!=(const STArray& s) const { return v_ != s.v_; } - virtual SerializedTypeID getSType () const override + virtual SerializedTypeID + getSType() const override { return STI_ARRAY; } - virtual bool isEquivalent (const STBase & t) const override; - virtual bool isDefault () const override + virtual bool + isEquivalent(const STBase& t) const override; + virtual bool + isDefault() const override { - return v_.empty (); + return v_.empty(); } }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STBase.h b/src/ripple/protocol/STBase.h index dfbee47ed6..4eb7b7cd9e 100644 --- a/src/ripple/protocol/STBase.h +++ b/src/ripple/protocol/STBase.h @@ -23,18 +23,15 @@ #include #include #include -#include #include +#include #include +#include #include #include -#include namespace ripple { -enum class JsonOptions { - none = 0, - include_date = 1 -}; +enum class JsonOptions { none = 0, include_date = 1 }; // VFALCO TODO fix this restriction on copy assignment. // @@ -67,27 +64,27 @@ class STBase public: STBase(); - explicit - STBase (SField const& n); + explicit STBase(SField const& n); virtual ~STBase() = default; STBase(const STBase& t) = default; - STBase& operator= (const STBase& t); + STBase& + operator=(const STBase& t); - bool operator== (const STBase& t) const; - bool operator!= (const STBase& t) const; + bool + operator==(const STBase& t) const; + bool + operator!=(const STBase& t) const; - virtual - STBase* - copy (std::size_t n, void* buf) const + virtual STBase* + copy(std::size_t n, void* buf) const { return emplace(n, buf, *this); } - virtual - STBase* - move (std::size_t n, void* buf) + virtual STBase* + move(std::size_t n, void* buf) { return emplace(n, buf, std::move(*this)); } @@ -96,9 +93,9 @@ public: D& downcast() { - D* ptr = dynamic_cast (this); + D* ptr = dynamic_cast(this); if (ptr == nullptr) - Throw (); + Throw(); return *ptr; } @@ -106,71 +103,63 @@ public: D const& downcast() const { - D const * ptr = dynamic_cast (this); + D const* ptr = dynamic_cast(this); if (ptr == nullptr) - Throw (); + Throw(); return *ptr; } - virtual - SerializedTypeID + virtual SerializedTypeID getSType() const; - virtual - std::string + virtual std::string getFullText() const; - virtual - std::string + virtual std::string getText() const; - virtual - Json::Value - getJson (JsonOptions /*options*/) const; + virtual Json::Value getJson(JsonOptions /*options*/) const; - virtual - void - add (Serializer& s) const; + virtual void + add(Serializer& s) const; - virtual - bool - isEquivalent (STBase const& t) const; + virtual bool + isEquivalent(STBase const& t) const; - virtual - bool + virtual bool isDefault() const; /** A STBase is a field. This sets the name. */ void - setFName (SField const& n); + setFName(SField const& n); SField const& getFName() const; void - addFieldID (Serializer& s) const; + addFieldID(Serializer& s) const; protected: SField const* fName; template - static - STBase* + static STBase* emplace(std::size_t n, void* buf, T&& val) { using U = std::decay_t; if (sizeof(U) > n) return new U(std::forward(val)); - return new(buf) U(std::forward(val)); + return new (buf) U(std::forward(val)); } }; //------------------------------------------------------------------------------ -std::ostream& operator<< (std::ostream& out, const STBase& t); +std::ostream& +operator<<(std::ostream& out, const STBase& t); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STBitString.h b/src/ripple/protocol/STBitString.h index 4cdca94dd7..a4a847d0b0 100644 --- a/src/ripple/protocol/STBitString.h +++ b/src/ripple/protocol/STBitString.h @@ -20,87 +20,85 @@ #ifndef RIPPLE_PROTOCOL_STBITSTRING_H_INCLUDED #define RIPPLE_PROTOCOL_STBITSTRING_H_INCLUDED -#include #include +#include namespace ripple { template -class STBitString final - : public STBase +class STBitString final : public STBase { public: using value_type = base_uint; - STBitString () = default; + STBitString() = default; - STBitString (SField const& n) - : STBase (n) - { } - - STBitString (const value_type& v) - : value_ (v) - { } - - STBitString (SField const& n, const value_type& v) - : STBase (n), value_ (v) - { } - - STBitString (SField const& n, const char* v) - : STBase (n) + STBitString(SField const& n) : STBase(n) { - value_.SetHex (v); } - STBitString (SField const& n, std::string const& v) - : STBase (n) + STBitString(const value_type& v) : value_(v) { - value_.SetHex (v); } - STBitString (SerialIter& sit, SField const& name) + STBitString(SField const& n, const value_type& v) : STBase(n), value_(v) + { + } + + STBitString(SField const& n, const char* v) : STBase(n) + { + value_.SetHex(v); + } + + STBitString(SField const& n, std::string const& v) : STBase(n) + { + value_.SetHex(v); + } + + STBitString(SerialIter& sit, SField const& name) : STBitString(name, sit.getBitString()) { } STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } SerializedTypeID - getSType () const override; + getSType() const override; std::string - getText () const override + getText() const override { - return to_string (value_); + return to_string(value_); } bool - isEquivalent (const STBase& t) const override + isEquivalent(const STBase& t) const override { - const STBitString* v = dynamic_cast (&t); + const STBitString* v = dynamic_cast(&t); return v && (value_ == v->value_); } void - add (Serializer& s) const override + add(Serializer& s) const override { - assert (fName->isBinary ()); - assert (fName->fieldType == getSType()); - s.addBitString (value_); + assert(fName->isBinary()); + assert(fName->fieldType == getSType()); + s.addBitString(value_); } template - void setValue (base_uint const& v) + void + setValue(base_uint const& v) { value_ = v; } @@ -111,13 +109,13 @@ public: return value_; } - operator value_type () const + operator value_type() const { return value_; } bool - isDefault () const override + isDefault() const override { return value_ == beast::zero; } @@ -131,29 +129,26 @@ using STHash160 = STBitString<160>; using STHash256 = STBitString<256>; template <> -inline -SerializedTypeID -STHash128::getSType () const +inline SerializedTypeID +STHash128::getSType() const { return STI_HASH128; } template <> -inline -SerializedTypeID -STHash160::getSType () const +inline SerializedTypeID +STHash160::getSType() const { return STI_HASH160; } template <> -inline -SerializedTypeID -STHash256::getSType () const +inline SerializedTypeID +STHash256::getSType() const { return STI_HASH256; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STBlob.h b/src/ripple/protocol/STBlob.h index 1c069bd923..2afdc912df 100644 --- a/src/ripple/protocol/STBlob.h +++ b/src/ripple/protocol/STBlob.h @@ -30,45 +30,39 @@ namespace ripple { // variable length byte string -class STBlob - : public STBase +class STBlob : public STBase { public: using value_type = Slice; - STBlob () = default; - STBlob (STBlob const& rhs) - :STBase(rhs) - , value_ (rhs.data (), rhs.size ()) + STBlob() = default; + STBlob(STBlob const& rhs) : STBase(rhs), value_(rhs.data(), rhs.size()) { } - STBlob (SField const& f, - void const* data, std::size_t size) - : STBase(f), value_ (data, size) + STBlob(SField const& f, void const* data, std::size_t size) + : STBase(f), value_(data, size) { } - STBlob (SField const& f, Buffer&& b) - : STBase(f), value_(std::move (b)) + STBlob(SField const& f, Buffer&& b) : STBase(f), value_(std::move(b)) { } - STBlob (SField const& n) - : STBase (n) + STBlob(SField const& n) : STBase(n) { } - STBlob (SerialIter&, SField const& name = sfGeneric); + STBlob(SerialIter&, SField const& name = sfGeneric); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } @@ -82,30 +76,29 @@ public: std::uint8_t const* data() const { - return reinterpret_cast< - std::uint8_t const*>(value_.data()); + return reinterpret_cast(value_.data()); } SerializedTypeID - getSType () const override + getSType() const override { return STI_VL; } std::string - getText () const override; + getText() const override; void - add (Serializer& s) const override + add(Serializer& s) const override { - assert (fName->isBinary ()); - assert ((fName->fieldType == STI_VL) || - (fName->fieldType == STI_ACCOUNT)); - s.addVL (value_.data (), value_.size ()); + assert(fName->isBinary()); + assert( + (fName->fieldType == STI_VL) || (fName->fieldType == STI_ACCOUNT)); + s.addVL(value_.data(), value_.size()); } STBlob& - operator= (Slice const& slice) + operator=(Slice const& slice) { value_ = Buffer(slice.data(), slice.size()); return *this; @@ -118,31 +111,31 @@ public: } STBlob& - operator= (Buffer&& buffer) + operator=(Buffer&& buffer) { value_ = std::move(buffer); return *this; } void - setValue (Buffer&& b) + setValue(Buffer&& b) { - value_ = std::move (b); + value_ = std::move(b); } bool - isEquivalent (const STBase& t) const override; + isEquivalent(const STBase& t) const override; bool - isDefault () const override + isDefault() const override { - return value_.empty (); + return value_.empty(); } private: Buffer value_; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STExchange.h b/src/ripple/protocol/STExchange.h index 8020024727..64392bc049 100644 --- a/src/ripple/protocol/STExchange.h +++ b/src/ripple/protocol/STExchange.h @@ -20,14 +20,14 @@ #ifndef RIPPLE_PROTOCOL_STEXCHANGE_H_INCLUDED #define RIPPLE_PROTOCOL_STEXCHANGE_H_INCLUDED +#include #include -#include #include +#include #include #include #include #include -#include #include #include #include @@ -40,7 +40,6 @@ namespace ripple { template struct STExchange; - template struct STExchange, T> { @@ -48,20 +47,16 @@ struct STExchange, T> using value_type = U; - static - void - get (boost::optional& t, - STInteger const& u) + static void + get(boost::optional& t, STInteger const& u) { t = u.value(); } - static - std::unique_ptr> - set (SField const& f, T const& t) + static std::unique_ptr> + set(SField const& f, T const& t) { - return std::make_unique< - STInteger>(f, t); + return std::make_unique>(f, t); } }; @@ -72,21 +67,16 @@ struct STExchange using value_type = Slice; - static - void - get (boost::optional& t, - STBlob const& u) + static void + get(boost::optional& t, STBlob const& u) { - t.emplace (u.data(), u.size()); + t.emplace(u.data(), u.size()); } - static - std::unique_ptr - set (TypedField const& f, - Slice const& t) + static std::unique_ptr + set(TypedField const& f, Slice const& t) { - return std::make_unique( - f, t.data(), t.size()); + return std::make_unique(f, t.data(), t.size()); } }; @@ -97,31 +87,22 @@ struct STExchange using value_type = Buffer; - static - void - get (boost::optional& t, - STBlob const& u) + static void + get(boost::optional& t, STBlob const& u) { - t.emplace ( - u.data(), u.size()); + t.emplace(u.data(), u.size()); } - static - std::unique_ptr - set (TypedField const& f, - Buffer const& t) + static std::unique_ptr + set(TypedField const& f, Buffer const& t) { - return std::make_unique( - f, t.data(), t.size()); + return std::make_unique(f, t.data(), t.size()); } - static - std::unique_ptr - set (TypedField const& f, - Buffer&& t) + static std::unique_ptr + set(TypedField const& f, Buffer&& t) { - return std::make_unique( - f, std::move(t)); + return std::make_unique(f, std::move(t)); } }; @@ -131,32 +112,26 @@ struct STExchange /** @{ */ template boost::optional -get (STObject const& st, - TypedField const& f) +get(STObject const& st, TypedField const& f) { boost::optional t; - STBase const* const b = - st.peekAtPField(f); - if (! b) + STBase const* const b = st.peekAtPField(f); + if (!b) return t; auto const id = b->getSType(); if (id == STI_NOTPRESENT) return t; - auto const u = - dynamic_cast(b); + auto const u = dynamic_cast(b); // This should never happen - if (! u) - Throw ( - "Wrong field type"); + if (!u) + Throw("Wrong field type"); STExchange::get(t, *u); return t; } template -boost::optional::value_type> -get (STObject const& st, - TypedField const& f) +boost::optional::value_type> +get(STObject const& st, TypedField const& f) { return get(st, f); } @@ -165,45 +140,39 @@ get (STObject const& st, /** Set a field value in an STObject. */ template void -set (STObject& st, - TypedField const& f, T&& t) +set(STObject& st, TypedField const& f, T&& t) { - st.set(STExchange::type>::set( - f, std::forward(t))); + st.set(STExchange::type>::set( + f, std::forward(t))); } /** Set a blob field using an init function. */ template void -set (STObject& st, - TypedField const& f, - std::size_t size, Init&& init) +set(STObject& st, TypedField const& f, std::size_t size, Init&& init) { - st.set(std::make_unique( - f, size, init)); + st.set(std::make_unique(f, size, init)); } /** Set a blob field from data. */ template void -set (STObject& st, +set(STObject& st, TypedField const& f, - void const* data, std::size_t size) + void const* data, + std::size_t size) { - st.set(std::make_unique( - f, data, size)); + st.set(std::make_unique(f, data, size)); } /** Remove a field in an STObject. */ template void -erase (STObject& st, - TypedField const& f) +erase(STObject& st, TypedField const& f) { st.makeFieldAbsent(f); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STInteger.h b/src/ripple/protocol/STInteger.h index f52ec135b2..e70e516c36 100644 --- a/src/ripple/protocol/STInteger.h +++ b/src/ripple/protocol/STInteger.h @@ -25,85 +25,83 @@ namespace ripple { template -class STInteger - : public STBase +class STInteger : public STBase { public: using value_type = Integer; - explicit - STInteger (Integer v) - : value_ (v) - { } + explicit STInteger(Integer v) : value_(v) + { + } - STInteger (SField const& n, Integer v = 0) - : STBase (n), value_ (v) - { } + STInteger(SField const& n, Integer v = 0) : STBase(n), value_(v) + { + } STInteger(SerialIter& sit, SField const& name); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } SerializedTypeID - getSType () const override; + getSType() const override; - Json::Value - getJson (JsonOptions) const override; + Json::Value getJson(JsonOptions) const override; std::string - getText () const override; + getText() const override; void - add (Serializer& s) const override + add(Serializer& s) const override { - assert (fName->isBinary ()); - assert (fName->fieldType == getSType ()); - s.addInteger (value_); + assert(fName->isBinary()); + assert(fName->fieldType == getSType()); + s.addInteger(value_); } - STInteger& operator= (value_type const& v) + STInteger& + operator=(value_type const& v) { value_ = v; return *this; } - value_type value() const noexcept + value_type + value() const noexcept { return value_; } void - setValue (Integer v) + setValue(Integer v) { value_ = v; } - operator - Integer () const + operator Integer() const { return value_; } - virtual - bool isDefault () const override + virtual bool + isDefault() const override { return value_ == 0; } bool - isEquivalent (const STBase& t) const override + isEquivalent(const STBase& t) const override { - const STInteger* v = dynamic_cast (&t); + const STInteger* v = dynamic_cast(&t); return v && (value_ == v->value_); } @@ -111,11 +109,11 @@ private: Integer value_; }; -using STUInt8 = STInteger; +using STUInt8 = STInteger; using STUInt16 = STInteger; using STUInt32 = STInteger; using STUInt64 = STInteger; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STLedgerEntry.h b/src/ripple/protocol/STLedgerEntry.h index 6fb98bb6a5..1ddb012201 100644 --- a/src/ripple/protocol/STLedgerEntry.h +++ b/src/ripple/protocol/STLedgerEntry.h @@ -27,56 +27,62 @@ namespace ripple { class Invariants_test; -class STLedgerEntry final - : public STObject - , public CountedObject +class STLedgerEntry final : public STObject, public CountedObject { - friend Invariants_test; // this test wants access to the private type_ + friend Invariants_test; // this test wants access to the private type_ public: - static char const* getCountedObjectName () { return "STLedgerEntry"; } + static char const* + getCountedObjectName() + { + return "STLedgerEntry"; + } using pointer = std::shared_ptr; - using ref = const std::shared_ptr&; + using ref = const std::shared_ptr&; /** Create an empty object with the given key and type. */ - explicit - STLedgerEntry (Keylet const& k); + explicit STLedgerEntry(Keylet const& k); - STLedgerEntry (LedgerEntryType type, - uint256 const& key) + STLedgerEntry(LedgerEntryType type, uint256 const& key) : STLedgerEntry(Keylet(type, key)) { } - STLedgerEntry (SerialIter & sit, uint256 const& index); + STLedgerEntry(SerialIter& sit, uint256 const& index); STLedgerEntry(SerialIter&& sit, uint256 const& index) - : STLedgerEntry(sit, index) {} + : STLedgerEntry(sit, index) + { + } - STLedgerEntry (STObject const& object, uint256 const& index); + STLedgerEntry(STObject const& object, uint256 const& index); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } - SerializedTypeID getSType () const override + SerializedTypeID + getSType() const override { return STI_LEDGERENTRY; } - std::string getFullText () const override; + std::string + getFullText() const override; - std::string getText () const override; + std::string + getText() const override; - Json::Value getJson (JsonOptions options) const override; + Json::Value + getJson(JsonOptions options) const override; /** Returns the 'key' (or 'index') of this item. The key identifies this entry's position in @@ -88,25 +94,29 @@ public: return key_; } - LedgerEntryType getType () const + LedgerEntryType + getType() const { return type_; } // is this a ledger entry that can be threaded - bool isThreadedType() const; + bool + isThreadedType() const; - bool thread ( + bool + thread( uint256 const& txID, std::uint32_t ledgerSeq, - uint256 & prevTxID, - std::uint32_t & prevLedgerID); + uint256& prevTxID, + std::uint32_t& prevLedgerID); private: /* Make STObject comply with the template for this SLE type Can throw */ - void setSLEType (); + void + setSLEType(); private: uint256 key_; @@ -115,6 +125,6 @@ private: using SLE = STLedgerEntry; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STObject.h b/src/ripple/protocol/STObject.h index 385cf20c76..ad1fb32cf1 100644 --- a/src/ripple/protocol/STObject.h +++ b/src/ripple/protocol/STObject.h @@ -20,16 +20,16 @@ #ifndef RIPPLE_PROTOCOL_STOBJECT_H_INCLUDED #define RIPPLE_PROTOCOL_STOBJECT_H_INCLUDED -#include -#include #include #include #include +#include +#include #include +#include #include #include #include -#include #include #include #include @@ -48,9 +48,7 @@ throwFieldNotFound(SField const& field) Throw("Field not found: " + field.getName()); } -class STObject - : public STBase - , public CountedObject +class STObject : public STBase, public CountedObject { private: // Proxy value for a STBase derived class @@ -58,60 +56,60 @@ private: class Proxy { protected: - using value_type = - typename T::value_type; + using value_type = typename T::value_type; STObject* st_; SOEStyle style_; TypedField const* f_; - Proxy (Proxy const&) = default; - Proxy (STObject* st, TypedField const* f); - value_type value() const; - T const* find() const; + Proxy(Proxy const&) = default; + Proxy(STObject* st, TypedField const* f); + value_type + value() const; + T const* + find() const; template - void assign (U&& u); + void + assign(U&& u); }; template class ValueProxy : private Proxy { private: - using value_type = - typename T::value_type; + using value_type = typename T::value_type; public: ValueProxy(ValueProxy const&) = default; - ValueProxy& operator= (ValueProxy const&) = delete; + ValueProxy& + operator=(ValueProxy const&) = delete; template - std::enable_if_t< - std::is_assignable_v, - ValueProxy&> - operator= (U&& u); + std::enable_if_t, ValueProxy&> + operator=(U&& u); operator value_type() const; private: friend class STObject; - ValueProxy (STObject* st, TypedField const* f); + ValueProxy(STObject* st, TypedField const* f); }; template class OptionalProxy : private Proxy { private: - using value_type = - typename T::value_type; + using value_type = typename T::value_type; - using optional_type = boost::optional< - typename std::decay::type>; + using optional_type = + boost::optional::type>; public: OptionalProxy(OptionalProxy const&) = default; - OptionalProxy& operator= (OptionalProxy const&) = delete; + OptionalProxy& + operator=(OptionalProxy const&) = delete; /** Returns `true` if the field is set. @@ -126,7 +124,8 @@ private: STObject::FieldErr if !engaged() */ - value_type operator*() const; + value_type + operator*() const; operator optional_type() const; @@ -134,100 +133,93 @@ private: optional_type operator~() const; - friend bool operator==( - OptionalProxy const& lhs, - boost::none_t) noexcept + friend bool + operator==(OptionalProxy const& lhs, boost::none_t) noexcept { - return ! lhs.engaged(); + return !lhs.engaged(); } - friend bool operator==( - boost::none_t, - OptionalProxy const& rhs) noexcept + friend bool + operator==(boost::none_t, OptionalProxy const& rhs) noexcept { return rhs == boost::none; } - friend bool operator==( - OptionalProxy const& lhs, - optional_type const& rhs) noexcept + friend bool + operator==(OptionalProxy const& lhs, optional_type const& rhs) noexcept { - if (! lhs.engaged()) - return ! rhs; - if (! rhs) + if (!lhs.engaged()) + return !rhs; + if (!rhs) return false; return *lhs == *rhs; } - friend bool operator==( - optional_type const& lhs, - OptionalProxy const& rhs) noexcept + friend bool + operator==(optional_type const& lhs, OptionalProxy const& rhs) noexcept { return rhs == lhs; } - friend bool operator==( - OptionalProxy const& lhs, - OptionalProxy const& rhs) noexcept + friend bool + operator==(OptionalProxy const& lhs, OptionalProxy const& rhs) noexcept { if (lhs.engaged() != rhs.engaged()) return false; - return ! lhs.engaged() || *lhs == *rhs; + return !lhs.engaged() || *lhs == *rhs; } - friend bool operator!=( - OptionalProxy const& lhs, - boost::none_t) noexcept + friend bool + operator!=(OptionalProxy const& lhs, boost::none_t) noexcept { - return ! (lhs == boost::none); + return !(lhs == boost::none); } - friend bool operator!=(boost::none_t, - OptionalProxy const& rhs) noexcept + friend bool + operator!=(boost::none_t, OptionalProxy const& rhs) noexcept { - return ! (rhs == boost::none); + return !(rhs == boost::none); } - friend bool operator!=( - OptionalProxy const& lhs, - optional_type const& rhs) noexcept + friend bool + operator!=(OptionalProxy const& lhs, optional_type const& rhs) noexcept { - return ! (lhs == rhs); + return !(lhs == rhs); } - friend bool operator!=( - optional_type const& lhs, - OptionalProxy const& rhs) noexcept + friend bool + operator!=(optional_type const& lhs, OptionalProxy const& rhs) noexcept { - return ! (lhs == rhs); + return !(lhs == rhs); } - friend bool operator!=( - OptionalProxy const& lhs, - OptionalProxy const& rhs) noexcept + friend bool + operator!=(OptionalProxy const& lhs, OptionalProxy const& rhs) noexcept { - return ! (lhs == rhs); + return !(lhs == rhs); } - OptionalProxy& operator= (boost::none_t const&); - OptionalProxy& operator= (optional_type&& v); - OptionalProxy& operator= (optional_type const& v); + OptionalProxy& + operator=(boost::none_t const&); + OptionalProxy& + operator=(optional_type&& v); + OptionalProxy& + operator=(optional_type const& v); template - std::enable_if_t< - std::is_assignable_v, - OptionalProxy&> - operator= (U&& u); + std::enable_if_t, OptionalProxy&> + operator=(U&& u); private: friend class STObject; - OptionalProxy (STObject* st, - TypedField const* f); + OptionalProxy(STObject* st, TypedField const* f); - bool engaged() const noexcept; + bool + engaged() const noexcept; - void disengage(); + void + disengage(); optional_type optional_value() const; @@ -241,16 +233,13 @@ private: using result_type = STBase; STBase const& - operator() (detail::STVar const& e) const + operator()(detail::STVar const& e) const { return e.get(); } }; - enum - { - reserveSize = 20 - }; + enum { reserveSize = 20 }; using list_type = std::vector; @@ -258,113 +247,139 @@ private: SOTemplate const* mType; public: - using iterator = boost::transform_iterator< - Transform, STObject::list_type::const_iterator>; + using iterator = boost:: + transform_iterator; class FieldErr : public std::runtime_error { using std::runtime_error::runtime_error; }; - static char const* getCountedObjectName () { return "STObject"; } + static char const* + getCountedObjectName() + { + return "STObject"; + } STObject(STObject&&); STObject(STObject const&) = default; - STObject (const SOTemplate & type, SField const& name); - STObject (const SOTemplate& type, - SerialIter& sit, SField const& name) noexcept (false); - STObject (SerialIter& sit, - SField const& name, int depth = 0) noexcept (false); - STObject (SerialIter&& sit, SField const& name) noexcept (false) + STObject(const SOTemplate& type, SField const& name); + STObject( + const SOTemplate& type, + SerialIter& sit, + SField const& name) noexcept(false); + STObject(SerialIter& sit, SField const& name, int depth = 0) noexcept( + false); + STObject(SerialIter&& sit, SField const& name) noexcept(false) : STObject(sit, name) { } - STObject& operator= (STObject const&) = default; - STObject& operator= (STObject&& other); + STObject& + operator=(STObject const&) = default; + STObject& + operator=(STObject&& other); - explicit STObject (SField const& name); + explicit STObject(SField const& name); virtual ~STObject(); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } - iterator begin() const + iterator + begin() const { return iterator(v_.begin()); } - iterator end() const + iterator + end() const { return iterator(v_.end()); } - bool empty() const + bool + empty() const { return v_.empty(); } - void reserve (std::size_t n) + void + reserve(std::size_t n) { - v_.reserve (n); + v_.reserve(n); } - void applyTemplate (const SOTemplate & type) noexcept (false); + void + applyTemplate(const SOTemplate& type) noexcept(false); - void applyTemplateFromSField (SField const&) noexcept (false); + void + applyTemplateFromSField(SField const&) noexcept(false); - bool isFree () const + bool + isFree() const { return mType == nullptr; } - void set (const SOTemplate&); - bool set (SerialIter& u, int depth = 0); + void + set(const SOTemplate&); + bool + set(SerialIter& u, int depth = 0); - virtual SerializedTypeID getSType () const override + virtual SerializedTypeID + getSType() const override { return STI_OBJECT; } - virtual bool isEquivalent (const STBase & t) const override; - virtual bool isDefault () const override + virtual bool + isEquivalent(const STBase& t) const override; + virtual bool + isDefault() const override { return v_.empty(); } - virtual void add (Serializer & s) const override + virtual void + add(Serializer& s) const override { - add (s, withAllFields); // just inner elements + add(s, withAllFields); // just inner elements } - void addWithoutSigningFields (Serializer & s) const + void + addWithoutSigningFields(Serializer& s) const { - add (s, omitSigningFields); + add(s, omitSigningFields); } // VFALCO NOTE does this return an expensive copy of an object with a // dynamic buffer? // VFALCO TODO Remove this function and fix the few callers. - Serializer getSerializer () const + Serializer + getSerializer() const { Serializer s; - add (s, withAllFields); + add(s, withAllFields); return s; } - virtual std::string getFullText () const override; - virtual std::string getText () const override; + virtual std::string + getFullText() const override; + virtual std::string + getText() const override; // TODO(tom): options should be an enum. - virtual Json::Value getJson (JsonOptions options) const override; + virtual Json::Value + getJson(JsonOptions options) const override; template std::size_t @@ -374,61 +389,88 @@ public: return v_.size() - 1; } - int getCount () const + int + getCount() const { - return v_.size (); + return v_.size(); } - bool setFlag (std::uint32_t); - bool clearFlag (std::uint32_t); + bool setFlag(std::uint32_t); + bool clearFlag(std::uint32_t); bool isFlag(std::uint32_t) const; - std::uint32_t getFlags () const; + std::uint32_t + getFlags() const; - uint256 getHash (HashPrefix prefix) const; - uint256 getSigningHash (HashPrefix prefix) const; + uint256 + getHash(HashPrefix prefix) const; + uint256 + getSigningHash(HashPrefix prefix) const; - const STBase& peekAtIndex (int offset) const + const STBase& + peekAtIndex(int offset) const { return v_[offset].get(); } - STBase& getIndex(int offset) + STBase& + getIndex(int offset) { return v_[offset].get(); } - const STBase* peekAtPIndex (int offset) const + const STBase* + peekAtPIndex(int offset) const { return &v_[offset].get(); } - STBase* getPIndex (int offset) + STBase* + getPIndex(int offset) { return &v_[offset].get(); } - int getFieldIndex (SField const& field) const; - SField const& getFieldSType (int index) const; + int + getFieldIndex(SField const& field) const; + SField const& + getFieldSType(int index) const; - const STBase& peekAtField (SField const& field) const; - STBase& getField (SField const& field); - const STBase* peekAtPField (SField const& field) const; - STBase* getPField (SField const& field, bool createOkay = false); + const STBase& + peekAtField(SField const& field) const; + STBase& + getField(SField const& field); + const STBase* + peekAtPField(SField const& field) const; + STBase* + getPField(SField const& field, bool createOkay = false); // these throw if the field type doesn't match, or return default values // if the field is optional but not present - unsigned char getFieldU8 (SField const& field) const; - std::uint16_t getFieldU16 (SField const& field) const; - std::uint32_t getFieldU32 (SField const& field) const; - std::uint64_t getFieldU64 (SField const& field) const; - uint128 getFieldH128 (SField const& field) const; + unsigned char + getFieldU8(SField const& field) const; + std::uint16_t + getFieldU16(SField const& field) const; + std::uint32_t + getFieldU32(SField const& field) const; + std::uint64_t + getFieldU64(SField const& field) const; + uint128 + getFieldH128(SField const& field) const; - uint160 getFieldH160 (SField const& field) const; - uint256 getFieldH256 (SField const& field) const; - AccountID getAccountID (SField const& field) const; + uint160 + getFieldH160(SField const& field) const; + uint256 + getFieldH256(SField const& field) const; + AccountID + getAccountID(SField const& field) const; - Blob getFieldVL (SField const& field) const; - STAmount const& getFieldAmount (SField const& field) const; - STPathSet const& getFieldPathSet (SField const& field) const; - const STVector256& getFieldV256 (SField const& field) const; - const STArray& getFieldArray (SField const& field) const; + Blob + getFieldVL(SField const& field) const; + STAmount const& + getFieldAmount(SField const& field) const; + STPathSet const& + getFieldPathSet(SField const& field) const; + const STVector256& + getFieldV256(SField const& field) const; + const STArray& + getFieldArray(SField const& field) const; /** Return the value of a field. @@ -437,7 +479,7 @@ public: STObject::FieldErr if the field is not present. */ - template + template typename T::value_type operator[](TypedField const& f) const; @@ -445,7 +487,7 @@ public: @return boost::none if the field is not present. */ - template + template boost::optional> operator[](OptionaledField const& of) const; @@ -456,7 +498,7 @@ public: STObject::FieldErr if the field is not present. */ - template + template ValueProxy operator[](TypedField const& f); @@ -465,7 +507,7 @@ public: The return value equals boost::none if the field is not present. */ - template + template OptionalProxy operator[](OptionaledField const& of); @@ -473,103 +515,128 @@ public: if the field already exists, it is replaced. */ void - set (std::unique_ptr v); + set(std::unique_ptr v); - void setFieldU8 (SField const& field, unsigned char); - void setFieldU16 (SField const& field, std::uint16_t); - void setFieldU32 (SField const& field, std::uint32_t); - void setFieldU64 (SField const& field, std::uint64_t); - void setFieldH128 (SField const& field, uint128 const&); - void setFieldH256 (SField const& field, uint256 const& ); - void setFieldVL (SField const& field, Blob const&); - void setFieldVL (SField const& field, Slice const&); + void + setFieldU8(SField const& field, unsigned char); + void + setFieldU16(SField const& field, std::uint16_t); + void + setFieldU32(SField const& field, std::uint32_t); + void + setFieldU64(SField const& field, std::uint64_t); + void + setFieldH128(SField const& field, uint128 const&); + void + setFieldH256(SField const& field, uint256 const&); + void + setFieldVL(SField const& field, Blob const&); + void + setFieldVL(SField const& field, Slice const&); - void setAccountID (SField const& field, AccountID const&); + void + setAccountID(SField const& field, AccountID const&); - void setFieldAmount (SField const& field, STAmount const&); - void setFieldPathSet (SField const& field, STPathSet const&); - void setFieldV256 (SField const& field, STVector256 const& v); - void setFieldArray (SField const& field, STArray const& v); + void + setFieldAmount(SField const& field, STAmount const&); + void + setFieldPathSet(SField const& field, STPathSet const&); + void + setFieldV256(SField const& field, STVector256 const& v); + void + setFieldArray(SField const& field, STArray const& v); template - void setFieldH160 (SField const& field, base_uint<160, Tag> const& v) + void + setFieldH160(SField const& field, base_uint<160, Tag> const& v) { - STBase* rf = getPField (field, true); + STBase* rf = getPField(field, true); - if (! rf) + if (!rf) throwFieldNotFound(field); - if (rf->getSType () == STI_NOTPRESENT) - rf = makeFieldPresent (field); + if (rf->getSType() == STI_NOTPRESENT) + rf = makeFieldPresent(field); using Bits = STBitString<160>; - if (auto cf = dynamic_cast (rf)) - cf->setValue (v); + if (auto cf = dynamic_cast(rf)) + cf->setValue(v); else - Throw ("Wrong field type"); + Throw("Wrong field type"); } - STObject& peekFieldObject (SField const& field); - STArray& peekFieldArray (SField const& field); + STObject& + peekFieldObject(SField const& field); + STArray& + peekFieldArray(SField const& field); - bool isFieldPresent (SField const& field) const; - STBase* makeFieldPresent (SField const& field); - void makeFieldAbsent (SField const& field); - bool delField (SField const& field); - void delField (int index); + bool + isFieldPresent(SField const& field) const; + STBase* + makeFieldPresent(SField const& field); + void + makeFieldAbsent(SField const& field); + bool + delField(SField const& field); + void + delField(int index); - bool hasMatchingEntry (const STBase&); + bool + hasMatchingEntry(const STBase&); - bool operator== (const STObject & o) const; - bool operator!= (const STObject & o) const + bool + operator==(const STObject& o) const; + bool + operator!=(const STObject& o) const { - return ! (*this == o); + return !(*this == o); } private: - enum WhichFields : bool - { + enum WhichFields : bool { // These values are carefully chosen to do the right thing if passed // to SField::shouldInclude (bool) omitSigningFields = false, withAllFields = true }; - void add (Serializer & s, WhichFields whichFields) const; + void + add(Serializer& s, WhichFields whichFields) const; // Sort the entries in an STObject into the order that they will be // serialized. Note: they are not sorted into pointer value order, they // are sorted by SField::fieldCode. static std::vector - getSortedFields ( - STObject const& objToSort, WhichFields whichFields); + getSortedFields(STObject const& objToSort, WhichFields whichFields); // Implementation for getting (most) fields that return by value. // // The remove_cv and remove_reference are necessitated by the STBitString // types. Their value() returns by const ref. We return those types // by value. - template ().value ())>::type >::type > - V getFieldByValue (SField const& field) const + template < + typename T, + typename V = typename std::remove_cv().value())>::type>::type> + V + getFieldByValue(SField const& field) const { - const STBase* rf = peekAtPField (field); + const STBase* rf = peekAtPField(field); - if (! rf) + if (!rf) throwFieldNotFound(field); - SerializedTypeID id = rf->getSType (); + SerializedTypeID id = rf->getSType(); if (id == STI_NOTPRESENT) - return V (); // optional field not present + return V(); // optional field not present - const T* cf = dynamic_cast (rf); + const T* cf = dynamic_cast(rf); - if (! cf) - Throw ("Wrong field type"); + if (!cf) + Throw("Wrong field type"); - return cf->value (); + return cf->value(); } // Implementations for getting (most) fields that return by const reference. @@ -578,84 +645,88 @@ private: // obvious to return. So we insist on having the call provide an // 'empty' value we return in that circumstance. template - V const& getFieldByConstRef (SField const& field, V const& empty) const + V const& + getFieldByConstRef(SField const& field, V const& empty) const { - const STBase* rf = peekAtPField (field); + const STBase* rf = peekAtPField(field); - if (! rf) + if (!rf) throwFieldNotFound(field); - SerializedTypeID id = rf->getSType (); + SerializedTypeID id = rf->getSType(); if (id == STI_NOTPRESENT) - return empty; // optional field not present + return empty; // optional field not present - const T* cf = dynamic_cast (rf); + const T* cf = dynamic_cast(rf); - if (! cf) - Throw ("Wrong field type"); + if (!cf) + Throw("Wrong field type"); return *cf; } // Implementation for setting most fields with a setValue() method. template - void setFieldUsingSetValue (SField const& field, V value) + void + setFieldUsingSetValue(SField const& field, V value) { static_assert(!std::is_lvalue_reference::value, ""); - STBase* rf = getPField (field, true); + STBase* rf = getPField(field, true); - if (! rf) + if (!rf) throwFieldNotFound(field); - if (rf->getSType () == STI_NOTPRESENT) - rf = makeFieldPresent (field); + if (rf->getSType() == STI_NOTPRESENT) + rf = makeFieldPresent(field); - T* cf = dynamic_cast (rf); + T* cf = dynamic_cast(rf); - if (! cf) - Throw ("Wrong field type"); + if (!cf) + Throw("Wrong field type"); - cf->setValue (std::move (value)); + cf->setValue(std::move(value)); } // Implementation for setting fields using assignment template - void setFieldUsingAssignment (SField const& field, T const& value) + void + setFieldUsingAssignment(SField const& field, T const& value) { - STBase* rf = getPField (field, true); + STBase* rf = getPField(field, true); - if (! rf) + if (!rf) throwFieldNotFound(field); - if (rf->getSType () == STI_NOTPRESENT) - rf = makeFieldPresent (field); + if (rf->getSType() == STI_NOTPRESENT) + rf = makeFieldPresent(field); - T* cf = dynamic_cast (rf); + T* cf = dynamic_cast(rf); - if (! cf) - Throw ("Wrong field type"); + if (!cf) + Throw("Wrong field type"); (*cf) = value; } // Implementation for peeking STObjects and STArrays template - T& peekField (SField const& field) + T& + peekField(SField const& field) { - STBase* rf = getPField (field, true); + STBase* rf = getPField(field, true); - if (! rf) + if (!rf) throwFieldNotFound(field); - if (rf->getSType () == STI_NOTPRESENT) - rf = makeFieldPresent (field); + if (rf->getSType() == STI_NOTPRESENT) + rf = makeFieldPresent(field); - T* cf = dynamic_cast (rf); + T* cf = dynamic_cast(rf); - if (! cf) - Throw ("Wrong field type"); + if (!cf) + Throw("Wrong field type"); return *cf; } @@ -664,15 +735,13 @@ private: //------------------------------------------------------------------------------ template -STObject::Proxy::Proxy (STObject* st, TypedField const* f) - : st_ (st) - , f_ (f) +STObject::Proxy::Proxy(STObject* st, TypedField const* f) : st_(st), f_(f) { if (st_->mType) { // STObject has associated template - if (! st_->peekAtPField(*f_)) - Throw ( + if (!st_->peekAtPField(*f_)) + Throw( "Template field error '" + this->f_->getName() + "'"); style_ = st_->mType->style(*f_); } @@ -684,25 +753,22 @@ STObject::Proxy::Proxy (STObject* st, TypedField const* f) template auto -STObject::Proxy::value() const -> - value_type +STObject::Proxy::value() const -> value_type { auto const t = find(); if (t) return t->value(); if (style_ != soeDEFAULT) - Throw ( + Throw( "Missing field '" + this->f_->getName() + "'"); return value_type{}; } template -inline -T const* +inline T const* STObject::Proxy::find() const { - return dynamic_cast( - st_->peekAtPField(*f_)); + return dynamic_cast(st_->peekAtPField(*f_)); } template @@ -710,19 +776,16 @@ template void STObject::Proxy::assign(U&& u) { - if (style_ == soeDEFAULT && - u == value_type{}) + if (style_ == soeDEFAULT && u == value_type{}) { st_->makeFieldAbsent(*f_); return; } T* t; if (style_ == soeINVALID) - t = dynamic_cast( - st_->getPField(*f_, true)); + t = dynamic_cast(st_->getPField(*f_, true)); else - t = dynamic_cast( - st_->makeFieldPresent(*f_)); + t = dynamic_cast(st_->makeFieldPresent(*f_)); assert(t); *t = std::forward(u); } @@ -731,10 +794,8 @@ STObject::Proxy::assign(U&& u) template template -std::enable_if_t< - std::is_assignable_v, - STObject::ValueProxy&> -STObject::ValueProxy::operator= (U&& u) +std::enable_if_t, STObject::ValueProxy&> +STObject::ValueProxy::operator=(U&& u) { this->assign(std::forward(u)); return *this; @@ -747,8 +808,7 @@ STObject::ValueProxy::operator value_type() const } template -STObject::ValueProxy::ValueProxy( - STObject* st, TypedField const* f) +STObject::ValueProxy::ValueProxy(STObject* st, TypedField const* f) : Proxy(st, f) { } @@ -763,15 +823,14 @@ STObject::OptionalProxy::operator bool() const noexcept template auto -STObject::OptionalProxy::operator*() const -> - value_type +STObject::OptionalProxy::operator*() const -> value_type { return this->value(); } template -STObject::OptionalProxy::operator - typename STObject::OptionalProxy::optional_type() const +STObject::OptionalProxy::operator typename STObject::OptionalProxy< + T>::optional_type() const { return optional_value(); } @@ -785,8 +844,7 @@ STObject::OptionalProxy::operator~() const template auto -STObject::OptionalProxy::operator=(boost::none_t const&) -> - OptionalProxy& +STObject::OptionalProxy::operator=(boost::none_t const&) -> OptionalProxy& { disengage(); return *this; @@ -794,8 +852,7 @@ STObject::OptionalProxy::operator=(boost::none_t const&) -> template auto -STObject::OptionalProxy::operator=(optional_type&& v) -> - OptionalProxy& +STObject::OptionalProxy::operator=(optional_type&& v) -> OptionalProxy& { if (v) this->assign(std::move(*v)); @@ -806,8 +863,7 @@ STObject::OptionalProxy::operator=(optional_type&& v) -> template auto -STObject::OptionalProxy::operator=(optional_type const& v) -> - OptionalProxy& +STObject::OptionalProxy::operator=(optional_type const& v) -> OptionalProxy& { if (v) this->assign(*v); @@ -818,9 +874,7 @@ STObject::OptionalProxy::operator=(optional_type const& v) -> template template -std::enable_if_t< - std::is_assignable_v, - STObject::OptionalProxy&> +std::enable_if_t, STObject::OptionalProxy&> STObject::OptionalProxy::operator=(U&& u) { this->assign(std::forward(u)); @@ -828,8 +882,7 @@ STObject::OptionalProxy::operator=(U&& u) } template -STObject::OptionalProxy::OptionalProxy( - STObject* st, TypedField const* f) +STObject::OptionalProxy::OptionalProxy(STObject* st, TypedField const* f) : Proxy(st, f) { } @@ -838,17 +891,15 @@ template bool STObject::OptionalProxy::engaged() const noexcept { - return this->style_ == soeDEFAULT - || this->find() != nullptr; + return this->style_ == soeDEFAULT || this->find() != nullptr; } template void STObject::OptionalProxy::disengage() { - if (this->style_ == soeREQUIRED || - this->style_ == soeDEFAULT) - Throw ( + if (this->style_ == soeREQUIRED || this->style_ == soeDEFAULT) + Throw( "Template field error '" + this->f_->getName() + "'"); if (this->style_ == soeINVALID) this->st_->delField(*this->f_); @@ -858,60 +909,54 @@ STObject::OptionalProxy::disengage() template auto -STObject::OptionalProxy::optional_value() const -> - optional_type +STObject::OptionalProxy::optional_value() const -> optional_type { - if (! engaged()) + if (!engaged()) return boost::none; return this->value(); } //------------------------------------------------------------------------------ -template +template typename T::value_type STObject::operator[](TypedField const& f) const { auto const b = peekAtPField(f); - if (! b) + if (!b) // This is a free object (no constraints) // with no template - Throw ( - "Missing field '" + f.getName() + "'"); - auto const u = - dynamic_cast(b); - if (! u) + Throw("Missing field '" + f.getName() + "'"); + auto const u = dynamic_cast(b); + if (!u) { assert(mType); assert(b->getSType() == STI_NOTPRESENT); - if(mType->style(f) == soeOPTIONAL) - Throw ( - "Missing field '" + f.getName() + "'"); + if (mType->style(f) == soeOPTIONAL) + Throw("Missing field '" + f.getName() + "'"); assert(mType->style(f) == soeDEFAULT); // Handle the case where value_type is a // const reference, otherwise we return // the address of a temporary. - static std::decay_t< - typename T::value_type> const dv{}; + static std::decay_t const dv{}; return dv; } return u->value(); } -template +template boost::optional> STObject::operator[](OptionaledField const& of) const { auto const b = peekAtPField(*of.f); - if (! b) + if (!b) return boost::none; - auto const u = - dynamic_cast(b); - if (! u) + auto const u = dynamic_cast(b); + if (!u) { assert(mType); assert(b->getSType() == STI_NOTPRESENT); - if(mType->style(*of.f) == soeOPTIONAL) + if (mType->style(*of.f) == soeOPTIONAL) return boost::none; assert(mType->style(*of.f) == soeDEFAULT); return typename T::value_type{}; @@ -919,24 +964,20 @@ STObject::operator[](OptionaledField const& of) const return u->value(); } -template -inline -auto -STObject::operator[](TypedField const& f) -> - ValueProxy +template +inline auto +STObject::operator[](TypedField const& f) -> ValueProxy { return ValueProxy(this, &f); } -template -inline -auto -STObject::operator[](OptionaledField const& of) -> - OptionalProxy +template +inline auto +STObject::operator[](OptionaledField const& of) -> OptionalProxy { return OptionalProxy(this, of.f); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STParsedJSON.h b/src/ripple/protocol/STParsedJSON.h index c4de513997..0d38618cb3 100644 --- a/src/ripple/protocol/STParsedJSON.h +++ b/src/ripple/protocol/STParsedJSON.h @@ -38,15 +38,16 @@ public: @param name The name of the JSON field, used in diagnostics. @param json The JSON-RPC to parse. */ - STParsedJSONObject (std::string const& name, Json::Value const& json); + STParsedJSONObject(std::string const& name, Json::Value const& json); - STParsedJSONObject () = delete; - STParsedJSONObject (STParsedJSONObject const&) = delete; - STParsedJSONObject& operator= (STParsedJSONObject const&) = delete; - ~STParsedJSONObject () = default; + STParsedJSONObject() = delete; + STParsedJSONObject(STParsedJSONObject const&) = delete; + STParsedJSONObject& + operator=(STParsedJSONObject const&) = delete; + ~STParsedJSONObject() = default; /** The STObject if the parse was successful. */ - boost::optional object; + boost::optional object; /** On failure, an appropriate set of error values. */ Json::Value error; @@ -65,22 +66,21 @@ public: @param name The name of the JSON field, used in diagnostics. @param json The JSON-RPC to parse. */ - STParsedJSONArray (std::string const& name, Json::Value const& json); + STParsedJSONArray(std::string const& name, Json::Value const& json); - STParsedJSONArray () = delete; - STParsedJSONArray (STParsedJSONArray const&) = delete; - STParsedJSONArray& operator= (STParsedJSONArray const&) = delete; - ~STParsedJSONArray () = default; + STParsedJSONArray() = delete; + STParsedJSONArray(STParsedJSONArray const&) = delete; + STParsedJSONArray& + operator=(STParsedJSONArray const&) = delete; + ~STParsedJSONArray() = default; /** The STArray if the parse was successful. */ - boost::optional array; + boost::optional array; /** On failure, an appropriate set of error values. */ Json::Value error; }; - - -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STPathSet.h b/src/ripple/protocol/STPathSet.h index 66230ce9c7..dce226a01a 100644 --- a/src/ripple/protocol/STPathSet.h +++ b/src/ripple/protocol/STPathSet.h @@ -33,30 +33,29 @@ namespace ripple { class STPathElement { public: - enum Type - { - typeNone = 0x00, - typeAccount = 0x01, // Rippling through an account (vs taking an offer). - typeCurrency = 0x10, // Currency follows. - typeIssuer = 0x20, // Issuer follows. - typeBoundary = 0xFF, // Boundary between alternate paths. + enum Type { + typeNone = 0x00, + typeAccount = + 0x01, // Rippling through an account (vs taking an offer). + typeCurrency = 0x10, // Currency follows. + typeIssuer = 0x20, // Issuer follows. + typeBoundary = 0xFF, // Boundary between alternate paths. typeAll = typeAccount | typeCurrency | typeIssuer, - // Combination of all types. + // Combination of all types. }; private: - static - std::size_t - get_hash (STPathElement const& element); + static std::size_t + get_hash(STPathElement const& element); public: STPathElement( boost::optional const& account, boost::optional const& currency, boost::optional const& issuer) - : mType (typeNone) + : mType(typeNone) { - if (! account) + if (!account) { is_offer_ = true; } @@ -81,14 +80,19 @@ public: assert(mIssuerID != noAccount()); } - hash_value_ = get_hash (*this); + hash_value_ = get_hash(*this); } - STPathElement ( - AccountID const& account, Currency const& currency, - AccountID const& issuer, bool forceCurrency = false) - : mType (typeNone), mAccountID (account), mCurrencyID (currency) - , mIssuerID (issuer), is_offer_ (isXRP(mAccountID)) + STPathElement( + AccountID const& account, + Currency const& currency, + AccountID const& issuer, + bool forceCurrency = false) + : mType(typeNone) + , mAccountID(account) + , mCurrencyID(currency) + , mIssuerID(issuer) + , is_offer_(isXRP(mAccountID)) { if (!is_offer_) mType |= typeAccount; @@ -99,95 +103,98 @@ public: if (!isXRP(issuer)) mType |= typeIssuer; - hash_value_ = get_hash (*this); + hash_value_ = get_hash(*this); } - STPathElement ( - unsigned int uType, AccountID const& account, Currency const& currency, + STPathElement( + unsigned int uType, + AccountID const& account, + Currency const& currency, AccountID const& issuer) - : mType (uType), mAccountID (account), mCurrencyID (currency) - , mIssuerID (issuer), is_offer_ (isXRP(mAccountID)) + : mType(uType) + , mAccountID(account) + , mCurrencyID(currency) + , mIssuerID(issuer) + , is_offer_(isXRP(mAccountID)) { - hash_value_ = get_hash (*this); + hash_value_ = get_hash(*this); } - STPathElement () - : mType (typeNone), is_offer_ (true) + STPathElement() : mType(typeNone), is_offer_(true) { - hash_value_ = get_hash (*this); + hash_value_ = get_hash(*this); } STPathElement(STPathElement const&) = default; - STPathElement& operator=(STPathElement const&) = default; + STPathElement& + operator=(STPathElement const&) = default; auto - getNodeType () const + getNodeType() const { return mType; } bool - isOffer () const + isOffer() const { return is_offer_; } bool - isAccount () const + isAccount() const { - return !isOffer (); + return !isOffer(); } bool - hasIssuer () const + hasIssuer() const { - return getNodeType () & STPathElement::typeIssuer; + return getNodeType() & STPathElement::typeIssuer; } bool - hasCurrency () const + hasCurrency() const { - return getNodeType () & STPathElement::typeCurrency; + return getNodeType() & STPathElement::typeCurrency; } bool - isNone () const + isNone() const { - return getNodeType () == STPathElement::typeNone; + return getNodeType() == STPathElement::typeNone; } // Nodes are either an account ID or a offer prefix. Offer prefixs denote a // class of offers. AccountID const& - getAccountID () const + getAccountID() const { return mAccountID; } Currency const& - getCurrency () const + getCurrency() const { return mCurrencyID; } AccountID const& - getIssuerID () const + getIssuerID() const { return mIssuerID; } bool - operator== (const STPathElement& t) const + operator==(const STPathElement& t) const { return (mType & typeAccount) == (t.mType & typeAccount) && - hash_value_ == t.hash_value_ && - mAccountID == t.mAccountID && - mCurrencyID == t.mCurrencyID && - mIssuerID == t.mIssuerID; + hash_value_ == t.hash_value_ && mAccountID == t.mAccountID && + mCurrencyID == t.mCurrencyID && mIssuerID == t.mIssuerID; } bool - operator!= (const STPathElement& t) const + operator!=(const STPathElement& t) const { return !operator==(t); } @@ -205,88 +212,93 @@ private: class STPath { public: - STPath () = default; + STPath() = default; - STPath (std::vector p) - : mPath (std::move(p)) - { } + STPath(std::vector p) : mPath(std::move(p)) + { + } std::vector::size_type - size () const + size() const { - return mPath.size (); - } - - bool empty() const - { - return mPath.empty (); - } - - void - push_back (STPathElement const& e) - { - mPath.push_back (e); - } - - template - void - emplace_back (Args&&... args) - { - mPath.emplace_back (std::forward (args)...); + return mPath.size(); } bool - hasSeen ( - AccountID const& account, Currency const& currency, + empty() const + { + return mPath.empty(); + } + + void + push_back(STPathElement const& e) + { + mPath.push_back(e); + } + + template + void + emplace_back(Args&&... args) + { + mPath.emplace_back(std::forward(args)...); + } + + bool + hasSeen( + AccountID const& account, + Currency const& currency, AccountID const& issuer) const; - Json::Value - getJson (JsonOptions) const; + Json::Value getJson(JsonOptions) const; std::vector::const_iterator - begin () const + begin() const { - return mPath.begin (); + return mPath.begin(); } std::vector::const_iterator - end () const + end() const { - return mPath.end (); + return mPath.end(); } bool - operator== (STPath const& t) const + operator==(STPath const& t) const { return mPath == t.mPath; } std::vector::const_reference - back () const + back() const { - return mPath.back (); + return mPath.back(); } std::vector::const_reference - front () const + front() const { - return mPath.front (); + return mPath.front(); } - STPathElement& operator[](int i) + STPathElement& + operator[](int i) { return mPath[i]; } - const STPathElement& operator[](int i) const + const STPathElement& + operator[](int i) const { return mPath[i]; } - void reserve(size_t s) + void + reserve(size_t s) { mPath.reserve(s); } + private: std::vector mPath; }; @@ -294,38 +306,36 @@ private: //------------------------------------------------------------------------------ // A set of zero or more payment paths -class STPathSet final - : public STBase +class STPathSet final : public STBase { public: - STPathSet () = default; + STPathSet() = default; - STPathSet (SField const& n) - : STBase (n) - { } + STPathSet(SField const& n) : STBase(n) + { + } - STPathSet (SerialIter& sit, SField const& name); + STPathSet(SerialIter& sit, SField const& name); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } void - add (Serializer& s) const override; + add(Serializer& s) const override; - Json::Value - getJson (JsonOptions) const override; + Json::Value getJson(JsonOptions) const override; SerializedTypeID - getSType () const override + getSType() const override { return STI_PATHSET; } @@ -334,67 +344,68 @@ public: assembleAdd(STPath const& base, STPathElement const& tail); bool - isEquivalent (const STBase& t) const override; + isEquivalent(const STBase& t) const override; bool - isDefault () const override + isDefault() const override { - return value.empty (); + return value.empty(); } // std::vector like interface: std::vector::const_reference - operator[] (std::vector::size_type n) const + operator[](std::vector::size_type n) const { return value[n]; } std::vector::reference - operator[] (std::vector::size_type n) + operator[](std::vector::size_type n) { return value[n]; } std::vector::const_iterator - begin () const + begin() const { - return value.begin (); + return value.begin(); } std::vector::const_iterator - end () const + end() const { - return value.end (); + return value.end(); } std::vector::size_type - size () const + size() const { - return value.size (); + return value.size(); } bool - empty () const + empty() const { - return value.empty (); + return value.empty(); } void - push_back (STPath const& e) + push_back(STPath const& e) { - value.push_back (e); + value.push_back(e); } template - void emplace_back (Args&&... args) + void + emplace_back(Args&&... args) { - value.emplace_back (std::forward (args)...); + value.emplace_back(std::forward(args)...); } private: std::vector value; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STTx.h b/src/ripple/protocol/STTx.h index ed3bc5b483..f4e631d7fc 100644 --- a/src/ripple/protocol/STTx.h +++ b/src/ripple/protocol/STTx.h @@ -21,8 +21,8 @@ #define RIPPLE_PROTOCOL_STTX_H_INCLUDED #include -#include #include +#include #include #include #include @@ -30,8 +30,7 @@ namespace ripple { -enum TxnSql : char -{ +enum TxnSql : char { txnSqlNew = 'N', txnSqlConflict = 'C', txnSqlHeld = 'H', @@ -40,26 +39,31 @@ enum TxnSql : char txnSqlUnknown = 'U' }; -class STTx final - : public STObject - , public CountedObject +class STTx final : public STObject, public CountedObject { public: - static char const* getCountedObjectName () { return "STTx"; } + static char const* + getCountedObjectName() + { + return "STTx"; + } static std::size_t const minMultiSigners = 1; static std::size_t const maxMultiSigners = 8; public: - STTx () = delete; - STTx& operator= (STTx const& other) = delete; + STTx() = delete; + STTx& + operator=(STTx const& other) = delete; - STTx (STTx const& other) = default; + STTx(STTx const& other) = default; - explicit STTx (SerialIter& sit) noexcept (false); - explicit STTx (SerialIter&& sit) noexcept (false) : STTx(sit) {} + explicit STTx(SerialIter& sit) noexcept(false); + explicit STTx(SerialIter&& sit) noexcept(false) : STTx(sit) + { + } - explicit STTx (STObject&& object) noexcept (false); + explicit STTx(STObject&& object) noexcept(false); /** Constructs a transaction. @@ -67,88 +71,93 @@ public: any fields that the callback function adds to the object that's passed in. */ - STTx ( - TxType type, - std::function assembler); + STTx(TxType type, std::function assembler); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } // STObject functions. - SerializedTypeID getSType () const override + SerializedTypeID + getSType() const override { return STI_TRANSACTION; } - std::string getFullText () const override; + std::string + getFullText() const override; // Outer transaction functions / signature functions. - Blob getSignature () const; + Blob + getSignature() const; - uint256 getSigningHash () const; + uint256 + getSigningHash() const; - TxType getTxnType () const + TxType + getTxnType() const { return tx_type_; } - Blob getSigningPubKey () const + Blob + getSigningPubKey() const { - return getFieldVL (sfSigningPubKey); + return getFieldVL(sfSigningPubKey); } - std::uint32_t getSequence () const + std::uint32_t + getSequence() const { - return getFieldU32 (sfSequence); + return getFieldU32(sfSequence); } - void setSequence (std::uint32_t seq) + void + setSequence(std::uint32_t seq) { - return setFieldU32 (sfSequence, seq); + return setFieldU32(sfSequence, seq); } boost::container::flat_set getMentionedAccounts() const; - uint256 getTransactionID () const + uint256 + getTransactionID() const { return tid_; } - Json::Value getJson (JsonOptions options) const override; - Json::Value getJson (JsonOptions options, bool binary) const; + Json::Value + getJson(JsonOptions options) const override; + Json::Value + getJson(JsonOptions options, bool binary) const; - void sign ( - PublicKey const& publicKey, - SecretKey const& secretKey); + void + sign(PublicKey const& publicKey, SecretKey const& secretKey); /** Check the signature. @return `true` if valid signature. If invalid, the error message string. */ - enum class RequireFullyCanonicalSig : bool - { - no, - yes - }; + enum class RequireFullyCanonicalSig : bool { no, yes }; std::pair checkSign(RequireFullyCanonicalSig requireCanonicalSig) const; // SQL Functions with metadata. - static - std::string const& - getMetaSQLInsertReplaceHeader (); + static std::string const& + getMetaSQLInsertReplaceHeader(); - std::string getMetaSQL ( - std::uint32_t inLedger, std::string const& escapedMetaData) const; + std::string + getMetaSQL(std::uint32_t inLedger, std::string const& escapedMetaData) + const; - std::string getMetaSQL ( + std::string + getMetaSQL( Serializer rawTxn, std::uint32_t inLedger, char status, @@ -156,16 +165,17 @@ public: private: std::pair - checkSingleSign (RequireFullyCanonicalSig requireCanonicalSig) const; + checkSingleSign(RequireFullyCanonicalSig requireCanonicalSig) const; std::pair - checkMultiSign (RequireFullyCanonicalSig requireCanonicalSig) const; + checkMultiSign(RequireFullyCanonicalSig requireCanonicalSig) const; uint256 tid_; TxType tx_type_; }; -bool passesLocalChecks (STObject const& st, std::string&); +bool +passesLocalChecks(STObject const& st, std::string&); /** Sterilize a transaction. @@ -175,11 +185,12 @@ bool passesLocalChecks (STObject const& st, std::string&); the transaction's digest, are all computed. */ std::shared_ptr -sterilize (STTx const& stx); +sterilize(STTx const& stx); /** Check whether a transaction is a pseudo-transaction */ -bool isPseudoTx(STObject const& tx); +bool +isPseudoTx(STObject const& tx); -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STValidation.h b/src/ripple/protocol/STValidation.h index d5583308e5..35a313e934 100644 --- a/src/ripple/protocol/STValidation.h +++ b/src/ripple/protocol/STValidation.h @@ -76,18 +76,16 @@ public: if (publicKeyType(makeSlice(spk)) != KeyType::secp256k1) { - JLOG (debugLog().error()) - << "Invalid public key in validation" - << getJson (JsonOptions::none); - Throw ("Invalid public key in validation"); + JLOG(debugLog().error()) << "Invalid public key in validation" + << getJson(JsonOptions::none); + Throw("Invalid public key in validation"); } - if (checkSignature && !isValid ()) + if (checkSignature && !isValid()) { - JLOG (debugLog().error()) - << "Invalid signature in validation" - << getJson (JsonOptions::none); - Throw ("Invalid signature in validation"); + JLOG(debugLog().error()) << "Invalid signature in validation" + << getJson(JsonOptions::none); + Throw("Invalid signature in validation"); } mNodeID = lookupNodeID(PublicKey(makeSlice(spk))); @@ -119,7 +117,8 @@ public: @param nodeID ID corresponding to node's public master key @param isFull Whether the validation is full or partial @param fee FeeSettings to include in the validation - @param amendments If not empty, the amendments to include in this validation + @param amendments If not empty, the amendments to include in this + validation @note The fee and amendment settings are only set if not boost::none. Typically, the amendments and fees are set for validations of flag @@ -212,7 +211,6 @@ public: getSignature() const; private: - static SOTemplate const& getFormat(); @@ -221,6 +219,6 @@ private: NetClock::time_point mSeen = {}; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/STVector256.h b/src/ripple/protocol/STVector256.h index 5856bcf5ac..0a4b90b09c 100644 --- a/src/ripple/protocol/STVector256.h +++ b/src/ripple/protocol/STVector256.h @@ -20,120 +20,118 @@ #ifndef RIPPLE_PROTOCOL_STVECTOR256_H_INCLUDED #define RIPPLE_PROTOCOL_STVECTOR256_H_INCLUDED +#include #include #include -#include namespace ripple { -class STVector256 - : public STBase +class STVector256 : public STBase { public: using value_type = std::vector const&; - STVector256 () = default; + STVector256() = default; - explicit STVector256 (SField const& n) - : STBase (n) - { } + explicit STVector256(SField const& n) : STBase(n) + { + } - explicit STVector256 (std::vector const& vector) - : mValue (vector) - { } + explicit STVector256(std::vector const& vector) : mValue(vector) + { + } - STVector256 (SField const& n, std::vector const& vector) - : STBase (n), mValue (vector) - { } + STVector256(SField const& n, std::vector const& vector) + : STBase(n), mValue(vector) + { + } - STVector256 (SerialIter& sit, SField const& name); + STVector256(SerialIter& sit, SField const& name); STBase* - copy (std::size_t n, void* buf) const override + copy(std::size_t n, void* buf) const override { return emplace(n, buf, *this); } STBase* - move (std::size_t n, void* buf) override + move(std::size_t n, void* buf) override { return emplace(n, buf, std::move(*this)); } SerializedTypeID - getSType () const override + getSType() const override { return STI_VECTOR256; } void - add (Serializer& s) const override; + add(Serializer& s) const override; - Json::Value - getJson (JsonOptions) const override; + Json::Value getJson(JsonOptions) const override; bool - isEquivalent (const STBase& t) const override; + isEquivalent(const STBase& t) const override; bool - isDefault () const override + isDefault() const override { - return mValue.empty (); + return mValue.empty(); } STVector256& - operator= (std::vector const& v) + operator=(std::vector const& v) { mValue = v; return *this; } STVector256& - operator= (std::vector&& v) + operator=(std::vector&& v) { mValue = std::move(v); return *this; } void - setValue (const STVector256& v) + setValue(const STVector256& v) { mValue = v.mValue; } /** Retrieve a copy of the vector we contain */ - explicit - operator std::vector () const + explicit operator std::vector() const { return mValue; } std::size_t - size () const + size() const { - return mValue.size (); + return mValue.size(); } void - resize (std::size_t n) + resize(std::size_t n) { - return mValue.resize (n); + return mValue.resize(n); } bool - empty () const + empty() const { - return mValue.empty (); + return mValue.empty(); } std::vector::reference - operator[] (std::vector::size_type n) + operator[](std::vector::size_type n) { return mValue[n]; } std::vector::const_reference - operator[] (std::vector::size_type n) const + operator[](std::vector::size_type n) const { return mValue[n]; } @@ -157,51 +155,51 @@ public: } void - push_back (uint256 const& v) + push_back(uint256 const& v) { - mValue.push_back (v); + mValue.push_back(v); } std::vector::iterator begin() { - return mValue.begin (); + return mValue.begin(); } std::vector::const_iterator begin() const { - return mValue.begin (); + return mValue.begin(); } std::vector::iterator end() { - return mValue.end (); + return mValue.end(); } std::vector::const_iterator end() const { - return mValue.end (); + return mValue.end(); } std::vector::iterator - erase (std::vector::iterator position) + erase(std::vector::iterator position) { - return mValue.erase (position); + return mValue.erase(position); } void - clear () noexcept + clear() noexcept { - return mValue.clear (); + return mValue.clear(); } private: std::vector mValue; }; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/SecretKey.h b/src/ripple/protocol/SecretKey.h index a9a77db364..00dbdd9999 100644 --- a/src/ripple/protocol/SecretKey.h +++ b/src/ripple/protocol/SecretKey.h @@ -42,13 +42,14 @@ public: using const_iterator = std::uint8_t const*; SecretKey() = default; - SecretKey (SecretKey const&) = default; - SecretKey& operator= (SecretKey const&) = default; + SecretKey(SecretKey const&) = default; + SecretKey& + operator=(SecretKey const&) = default; ~SecretKey(); - SecretKey (std::array const& data); - SecretKey (Slice const& slice); + SecretKey(std::array const& data); + SecretKey(Slice const& slice); std::uint8_t const* data() const @@ -95,21 +96,17 @@ public: } }; -inline -bool -operator== (SecretKey const& lhs, - SecretKey const& rhs) +inline bool +operator==(SecretKey const& lhs, SecretKey const& rhs) { return lhs.size() == rhs.size() && std::memcmp(lhs.data(), rhs.data(), rhs.size()) == 0; } -inline -bool -operator!= (SecretKey const& lhs, - SecretKey const& rhs) +inline bool +operator!=(SecretKey const& lhs, SecretKey const& rhs) { - return ! (lhs == rhs); + return !(lhs == rhs); } //------------------------------------------------------------------------------ @@ -117,14 +114,12 @@ operator!= (SecretKey const& lhs, /** Parse a secret key */ template <> boost::optional -parseBase58 (TokenType type, std::string const& s); +parseBase58(TokenType type, std::string const& s); -inline -std::string -toBase58 (TokenType type, SecretKey const& sk) +inline std::string +toBase58(TokenType type, SecretKey const& sk) { - return base58EncodeToken( - type, sk.data(), sk.size()); + return base58EncodeToken(type, sk.data(), sk.size()); } /** Create a secret key using secure random numbers. */ @@ -133,11 +128,11 @@ randomSecretKey(); /** Generate a new secret key deterministically. */ SecretKey -generateSecretKey (KeyType type, Seed const& seed); +generateSecretKey(KeyType type, Seed const& seed); /** Derive the public key from a secret key. */ PublicKey -derivePublicKey (KeyType type, SecretKey const& sk); +derivePublicKey(KeyType type, SecretKey const& sk); /** Generate a key pair deterministically. @@ -148,11 +143,11 @@ derivePublicKey (KeyType type, SecretKey const& sk); corresponding to ordinal 0 for the generator. */ std::pair -generateKeyPair (KeyType type, Seed const& seed); +generateKeyPair(KeyType type, Seed const& seed); /** Create a key pair using secure random numbers. */ std::pair -randomKeyPair (KeyType type); +randomKeyPair(KeyType type); /** Generate a signature for a message digest. This can only be used with secp256k1 since Ed25519's @@ -161,15 +156,12 @@ randomKeyPair (KeyType type); */ /** @{ */ Buffer -signDigest (PublicKey const& pk, SecretKey const& sk, - uint256 const& digest); +signDigest(PublicKey const& pk, SecretKey const& sk, uint256 const& digest); -inline -Buffer -signDigest (KeyType type, SecretKey const& sk, - uint256 const& digest) +inline Buffer +signDigest(KeyType type, SecretKey const& sk, uint256 const& digest) { - return signDigest (derivePublicKey(type, sk), sk, digest); + return signDigest(derivePublicKey(type, sk), sk, digest); } /** @} */ @@ -179,18 +171,15 @@ signDigest (KeyType type, SecretKey const& sk, */ /** @{ */ Buffer -sign (PublicKey const& pk, - SecretKey const& sk, Slice const& message); +sign(PublicKey const& pk, SecretKey const& sk, Slice const& message); -inline -Buffer -sign (KeyType type, SecretKey const& sk, - Slice const& message) +inline Buffer +sign(KeyType type, SecretKey const& sk, Slice const& message) { - return sign (derivePublicKey(type, sk), sk, message); + return sign(derivePublicKey(type, sk), sk, message); } /** @} */ -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Seed.h b/src/ripple/protocol/Seed.h index ebe346e0cb..2bad213f03 100644 --- a/src/ripple/protocol/Seed.h +++ b/src/ripple/protocol/Seed.h @@ -20,8 +20,8 @@ #ifndef RIPPLE_PROTOCOL_SEED_H_INCLUDED #define RIPPLE_PROTOCOL_SEED_H_INCLUDED -#include #include +#include #include #include #include @@ -39,8 +39,9 @@ public: Seed() = delete; - Seed (Seed const&) = default; - Seed& operator= (Seed const&) = default; + Seed(Seed const&) = default; + Seed& + operator=(Seed const&) = default; /** Destroy the seed. The buffer will first be securely erased. @@ -49,8 +50,8 @@ public: /** Construct a seed */ /** @{ */ - explicit Seed (Slice const& slice); - explicit Seed (uint128 const& seed); + explicit Seed(Slice const& slice); + explicit Seed(uint128 const& seed); /** @} */ std::uint8_t const* @@ -108,29 +109,28 @@ randomSeed(); the string (e.g. hex or base58). */ Seed -generateSeed (std::string const& passPhrase); +generateSeed(std::string const& passPhrase); /** Parse a Base58 encoded string into a seed */ template <> boost::optional -parseBase58 (std::string const& s); +parseBase58(std::string const& s); /** Attempt to parse a string as a seed */ boost::optional -parseGenericSeed (std::string const& str); +parseGenericSeed(std::string const& str); /** Encode a Seed in RFC1751 format */ std::string -seedAs1751 (Seed const& seed); +seedAs1751(Seed const& seed); /** Format a seed as a Base58 string */ -inline -std::string -toBase58 (Seed const& seed) +inline std::string +toBase58(Seed const& seed) { return base58EncodeToken(TokenType::FamilySeed, seed.data(), seed.size()); } -} +} // namespace ripple #endif diff --git a/src/ripple/protocol/Serializer.h b/src/ripple/protocol/Serializer.h index 8249ecf60a..4f4b53633d 100644 --- a/src/ripple/protocol/Serializer.h +++ b/src/ripple/protocol/Serializer.h @@ -20,12 +20,12 @@ #ifndef RIPPLE_PROTOCOL_SERIALIZER_H_INCLUDED #define RIPPLE_PROTOCOL_SERIALIZER_H_INCLUDED -#include #include -#include #include -#include #include +#include +#include +#include #include #include #include @@ -38,7 +38,7 @@ namespace ripple { -class CKey; // forward declaration +class CKey; // forward declaration class Serializer { @@ -47,13 +47,12 @@ private: Blob mData; public: - explicit - Serializer (int n = 256) + explicit Serializer(int n = 256) { - mData.reserve (n); + mData.reserve(n); } - Serializer (void const* data, std::size_t size) + Serializer(void const* data, std::size_t size) { mData.resize(size); @@ -64,7 +63,8 @@ public: } } - Slice slice() const noexcept + Slice + slice() const noexcept { return Slice(mData.data(), mData.size()); } @@ -82,50 +82,68 @@ public: } // assemble functions - int add8 (unsigned char byte); - int add16 (std::uint16_t); - int add32 (std::uint32_t); // ledger indexes, account sequence, timestamps - int add32 (HashPrefix); - int add64 (std::uint64_t); // native currency amounts - int add128 (const uint128&); // private key generators - int add256 (uint256 const& ); // transaction and ledger hashes + int + add8(unsigned char byte); + int add16(std::uint16_t); + int add32(std::uint32_t); // ledger indexes, account sequence, timestamps + int add32(HashPrefix); + int add64(std::uint64_t); // native currency amounts + int + add128(const uint128&); // private key generators + int + add256(uint256 const&); // transaction and ledger hashes template int addInteger(Integer); template - int addBitString(base_uint const& v) { - int ret = mData.size (); - mData.insert (mData.end (), v.begin (), v.end ()); + int + addBitString(base_uint const& v) + { + int ret = mData.size(); + mData.insert(mData.end(), v.begin(), v.end()); return ret; } // TODO(tom): merge with add128 and add256. template - int add160 (base_uint<160, Tag> const& i) + int + add160(base_uint<160, Tag> const& i) { return addBitString<160, Tag>(i); } - int addRaw (Blob const& vector); - int addRaw (const void* ptr, int len); - int addRaw (const Serializer& s); - int addZeros (size_t uBytes); + int + addRaw(Blob const& vector); + int + addRaw(const void* ptr, int len); + int + addRaw(const Serializer& s); + int + addZeros(size_t uBytes); - int addVL (Blob const& vector); - int addVL (Slice const& slice); - template - int addVL (Iter begin, Iter end, int len); - int addVL (const void* ptr, int len); + int + addVL(Blob const& vector); + int + addVL(Slice const& slice); + template + int + addVL(Iter begin, Iter end, int len); + int + addVL(const void* ptr, int len); // disassemble functions - bool get8 (int&, int offset) const; - bool get256 (uint256&, int offset) const; + bool + get8(int&, int offset) const; + bool + get256(uint256&, int offset) const; template - bool getInteger(Integer& number, int offset) { + bool + getInteger(Integer& number, int offset) + { static const auto bytes = sizeof(Integer); - if ((offset + bytes) > mData.size ()) + if ((offset + bytes) > mData.size()) return false; number = 0; @@ -140,157 +158,196 @@ public: } template - bool getBitString(base_uint& data, int offset) const { - auto success = (offset + (Bits / 8)) <= mData.size (); + bool + getBitString(base_uint& data, int offset) const + { + auto success = (offset + (Bits / 8)) <= mData.size(); if (success) - memcpy (data.begin (), & (mData.front ()) + offset, (Bits / 8)); + memcpy(data.begin(), &(mData.front()) + offset, (Bits / 8)); return success; } // TODO(tom): merge with get128 and get256. template - bool get160 (base_uint<160, Tag>& o, int offset) const + bool + get160(base_uint<160, Tag>& o, int offset) const { return getBitString<160, Tag>(o, offset); } - bool getRaw (Blob&, int offset, int length) const; - Blob getRaw (int offset, int length) const; + bool + getRaw(Blob&, int offset, int length) const; + Blob + getRaw(int offset, int length) const; - bool getVL (Blob& objectVL, int offset, int& length) const; - bool getVLLength (int& length, int offset) const; + bool + getVL(Blob& objectVL, int offset, int& length) const; + bool + getVLLength(int& length, int offset) const; - int addFieldID (int type, int name); - int addFieldID (SerializedTypeID type, int name) + int + addFieldID(int type, int name); + int + addFieldID(SerializedTypeID type, int name) { - return addFieldID (safe_cast (type), name); + return addFieldID(safe_cast(type), name); } // DEPRECATED - uint256 getSHA512Half() const; + uint256 + getSHA512Half() const; // totality functions - Blob const& peekData () const + Blob const& + peekData() const { return mData; } - Blob getData () const + Blob + getData() const { return mData; } - Blob& modData () + Blob& + modData() { return mData; } - int getDataLength () const + int + getDataLength() const { - return mData.size (); + return mData.size(); } - const void* getDataPtr () const + const void* + getDataPtr() const { return mData.data(); } - void* getDataPtr () + void* + getDataPtr() { return mData.data(); } - int getLength () const + int + getLength() const { - return mData.size (); + return mData.size(); } - std::string getString () const + std::string + getString() const { - return std::string (static_cast (getDataPtr ()), size ()); + return std::string(static_cast(getDataPtr()), size()); } - void secureErase () + void + secureErase() { beast::secure_erase(mData.data(), mData.size()); - mData.clear (); + mData.clear(); } - void erase () + void + erase() { - mData.clear (); + mData.clear(); } - bool chop (int num); + bool + chop(int num); // vector-like functions - Blob ::iterator begin () + Blob ::iterator + begin() { - return mData.begin (); + return mData.begin(); } - Blob ::iterator end () + Blob ::iterator + end() { - return mData.end (); + return mData.end(); } - Blob ::const_iterator begin () const + Blob ::const_iterator + begin() const { - return mData.begin (); + return mData.begin(); } - Blob ::const_iterator end () const + Blob ::const_iterator + end() const { - return mData.end (); + return mData.end(); } - void reserve (size_t n) + void + reserve(size_t n) { - mData.reserve (n); + mData.reserve(n); } - void resize (size_t n) + void + resize(size_t n) { - mData.resize (n); + mData.resize(n); } - size_t capacity () const + size_t + capacity() const { - return mData.capacity (); + return mData.capacity(); } - bool operator== (Blob const& v) + bool + operator==(Blob const& v) { return v == mData; } - bool operator!= (Blob const& v) + bool + operator!=(Blob const& v) { return v != mData; } - bool operator== (const Serializer& v) + bool + operator==(const Serializer& v) { return v.mData == mData; } - bool operator!= (const Serializer& v) + bool + operator!=(const Serializer& v) { return v.mData != mData; } - std::string getHex () const + std::string + getHex() const { std::stringstream h; for (unsigned char const& element : mData) { - h << - std::setw (2) << - std::hex << - std::setfill ('0') << - safe_cast(element); + h << std::setw(2) << std::hex << std::setfill('0') + << safe_cast(element); } - return h.str (); + return h.str(); } - static int decodeLengthLength (int b1); - static int decodeVLLength (int b1); - static int decodeVLLength (int b1, int b2); - static int decodeVLLength (int b1, int b2, int b3); + static int + decodeLengthLength(int b1); + static int + decodeVLLength(int b1); + static int + decodeVLLength(int b1, int b2); + static int + decodeVLLength(int b1, int b2, int b3); + private: - static int lengthVL (int length) + static int + lengthVL(int length) { - return length + encodeLengthLength (length); + return length + encodeLengthLength(length); } - static int encodeLengthLength (int length); // length to encode length - int addEncoded (int length); + static int + encodeLengthLength(int length); // length to encode length + int + addEncoded(int length); }; -template -int Serializer::addVL(Iter begin, Iter end, int len) +template +int +Serializer::addVL(Iter begin, Iter end, int len) { int ret = addEncoded(len); for (; begin != end; ++begin) @@ -316,20 +373,17 @@ private: std::size_t used_ = 0; public: - SerialIter (void const* data, - std::size_t size) noexcept; + SerialIter(void const* data, std::size_t size) noexcept; - SerialIter (Slice const& slice) - : SerialIter(slice.data(), slice.size()) + SerialIter(Slice const& slice) : SerialIter(slice.data(), slice.size()) { } // Infer the size of the data based on the size of the passed array. - template - explicit SerialIter (std::uint8_t const (&data)[N]) - : SerialIter(&data[0], N) + template + explicit SerialIter(std::uint8_t const (&data)[N]) : SerialIter(&data[0], N) { - static_assert (N > 0, ""); + static_assert(N > 0, ""); } std::size_t @@ -383,33 +437,34 @@ public: } void - getFieldID (int& type, int& name); + getFieldID(int& type, int& name); // Returns the size of the VL if the // next object is a VL. Advances the iterator // to the beginning of the VL. int - getVLDataLength (); + getVLDataLength(); Slice - getSlice (std::size_t bytes); + getSlice(std::size_t bytes); // VFALCO DEPRECATED Returns a copy Blob - getRaw (int size); + getRaw(int size); // VFALCO DEPRECATED Returns a copy Blob getVL(); void - skip (int num); + skip(int num); Buffer getVLBuffer(); - template - T getRawHelper (int size); + template + T + getRawHelper(int size); }; template @@ -417,17 +472,16 @@ base_uint SerialIter::getBitString() { base_uint u; - auto const n = Bits/8; + auto const n = Bits / 8; if (remain_ < n) - Throw ( - "invalid SerialIter getBitString"); - std::memcpy (u.begin(), p_, n); + Throw("invalid SerialIter getBitString"); + std::memcpy(u.begin(), p_, n); p_ += n; used_ += n; remain_ -= n; return u; } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/Sign.h b/src/ripple/protocol/Sign.h index 86559958ce..92aaff7c3a 100644 --- a/src/ripple/protocol/Sign.h +++ b/src/ripple/protocol/Sign.h @@ -22,8 +22,8 @@ #include #include -#include #include +#include #include namespace ripple { @@ -40,9 +40,12 @@ namespace ripple { @note If a signature already exists, it is overwritten. */ void -sign (STObject& st, HashPrefix const& prefix, - KeyType type, SecretKey const& sk, - SF_Blob const& sigField = sfSignature); +sign( + STObject& st, + HashPrefix const& prefix, + KeyType type, + SecretKey const& sk, + SF_Blob const& sigField = sfSignature); /** Returns `true` if STObject contains valid signature @@ -53,13 +56,15 @@ sign (STObject& st, HashPrefix const& prefix, If not specified the value defaults to `sfSignature`. */ bool -verify (STObject const& st, HashPrefix const& prefix, +verify( + STObject const& st, + HashPrefix const& prefix, PublicKey const& pk, - SF_Blob const& sigField = sfSignature); + SF_Blob const& sigField = sfSignature); /** Return a Serializer suitable for computing a multisigning TxnSignature. */ Serializer -buildMultiSigningData (STObject const& obj, AccountID const& signingID); +buildMultiSigningData(STObject const& obj, AccountID const& signingID); /** Break the multi-signing hash computation into 2 parts for optimization. @@ -74,14 +79,14 @@ buildMultiSigningData (STObject const& obj, AccountID const& signingID); signer's unique data. */ Serializer -startMultiSigningData (STObject const& obj); +startMultiSigningData(STObject const& obj); inline void -finishMultiSigningData (AccountID const& signingID, Serializer& s) +finishMultiSigningData(AccountID const& signingID, Serializer& s) { - s.add160 (signingID); + s.add160(signingID); } -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/SystemParameters.h b/src/ripple/protocol/SystemParameters.h index 6e96467c3a..dcadaab247 100644 --- a/src/ripple/protocol/SystemParameters.h +++ b/src/ripple/protocol/SystemParameters.h @@ -29,9 +29,8 @@ namespace ripple { // Various protocol and system specific constant globals. /* The name of the system. */ -static inline -std::string const& -systemName () +static inline std::string const& +systemName() { static std::string const name = "ripple"; return name; @@ -40,31 +39,26 @@ systemName () /** Configure the native currency. */ /** Number of drops in the genesis account. */ -constexpr -XRPAmount -INITIAL_XRP{100'000'000'000 * DROPS_PER_XRP }; +constexpr XRPAmount INITIAL_XRP{100'000'000'000 * DROPS_PER_XRP}; /** Returns true if the amount does not exceed the initial XRP in existence. */ -inline -bool isLegalAmount (XRPAmount const& amount) +inline bool +isLegalAmount(XRPAmount const& amount) { return amount <= INITIAL_XRP; } /* The currency code for the native currency. */ -static inline -std::string const& -systemCurrencyCode () +static inline std::string const& +systemCurrencyCode() { static std::string const code = "XRP"; return code; } /** The XRP ledger network's earliest allowed sequence */ -static -std::uint32_t constexpr -XRP_LEDGER_EARLIEST_SEQ {32570}; +static std::uint32_t constexpr XRP_LEDGER_EARLIEST_SEQ{32570}; -} // ripple +} // namespace ripple #endif diff --git a/src/ripple/protocol/TER.h b/src/ripple/protocol/TER.h index 3c8181682f..dcb31bfd8a 100644 --- a/src/ripple/protocol/TER.h +++ b/src/ripple/protocol/TER.h @@ -38,19 +38,17 @@ using TERUnderlyingType = int; //------------------------------------------------------------------------------ -enum TELcodes : TERUnderlyingType -{ +enum TELcodes : TERUnderlyingType { // Note: Range is stable. // Exact numbers are used in ripple-binary-codec: // https://github.com/ripple/ripple-binary-codec/blob/master/src/enums/definitions.json // Use tokens. - // -399 .. -300: L Local error (transaction fee inadequate, exceeds local limit) - // Only valid during non-consensus processing. - // Implications: + // -399 .. -300: L Local error (transaction fee inadequate, exceeds local + // limit) Only valid during non-consensus processing. Implications: // - Not forwarded // - No fee check - telLOCAL_ERROR = -399, + telLOCAL_ERROR = -399, telBAD_DOMAIN, telBAD_PATH_COUNT, telBAD_PUBLIC_KEY, @@ -67,8 +65,7 @@ enum TELcodes : TERUnderlyingType //------------------------------------------------------------------------------ -enum TEMcodes : TERUnderlyingType -{ +enum TEMcodes : TERUnderlyingType { // Note: Range is stable. // Exact numbers are used in ripple-binary-codec: // https://github.com/ripple/ripple-binary-codec/blob/master/src/enums/definitions.json @@ -82,7 +79,7 @@ enum TEMcodes : TERUnderlyingType // - Not forwarded // - Reject // - Cannot succeed in any imagined ledger. - temMALFORMED = -299, + temMALFORMED = -299, temBAD_AMOUNT, temBAD_CURRENCY, @@ -124,8 +121,7 @@ enum TEMcodes : TERUnderlyingType //------------------------------------------------------------------------------ -enum TEFcodes : TERUnderlyingType -{ +enum TEFcodes : TERUnderlyingType { // Note: Range is stable. // Exact numbers are used in ripple-binary-codec: // https://github.com/ripple/ripple-binary-codec/blob/master/src/enums/definitions.json @@ -143,7 +139,7 @@ enum TEFcodes : TERUnderlyingType // - Not applied // - Not forwarded // - Could succeed in an imagined ledger. - tefFAILURE = -199, + tefFAILURE = -199, tefALREADY, tefBAD_ADD_AUTH, tefBAD_AUTH, @@ -151,7 +147,7 @@ enum TEFcodes : TERUnderlyingType tefCREATED, tefEXCEPTION, tefINTERNAL, - tefNO_AUTH_REQUIRED, // Can't set auth if auth is not required. + tefNO_AUTH_REQUIRED, // Can't set auth if auth is not required. tefPAST_SEQ, tefWRONG_PRIOR, tefMASTER_DISABLED, @@ -166,8 +162,7 @@ enum TEFcodes : TERUnderlyingType //------------------------------------------------------------------------------ -enum TERcodes : TERUnderlyingType -{ +enum TERcodes : TERUnderlyingType { // Note: Range is stable. // Exact numbers are used in ripple-binary-codec: // https://github.com/ripple/ripple-binary-codec/blob/master/src/enums/definitions.json @@ -189,24 +184,23 @@ enum TERcodes : TERUnderlyingType // - Might succeed later // - Hold // - Makes hole in sequence which jams transactions. - terRETRY = -99, - terFUNDS_SPENT, // This is a free transaction, so don't burden network. - terINSUF_FEE_B, // Can't pay fee, therefore don't burden network. - terNO_ACCOUNT, // Can't pay fee, therefore don't burden network. - terNO_AUTH, // Not authorized to hold IOUs. - terNO_LINE, // Internal flag. - terOWNERS, // Can't succeed with non-zero owner count. - terPRE_SEQ, // Can't pay fee, no point in forwarding, so don't - // burden network. - terLAST, // Process after all other transactions - terNO_RIPPLE, // Rippling not allowed - terQUEUED // Transaction is being held in TxQ until fee drops + terRETRY = -99, + terFUNDS_SPENT, // This is a free transaction, so don't burden network. + terINSUF_FEE_B, // Can't pay fee, therefore don't burden network. + terNO_ACCOUNT, // Can't pay fee, therefore don't burden network. + terNO_AUTH, // Not authorized to hold IOUs. + terNO_LINE, // Internal flag. + terOWNERS, // Can't succeed with non-zero owner count. + terPRE_SEQ, // Can't pay fee, no point in forwarding, so don't + // burden network. + terLAST, // Process after all other transactions + terNO_RIPPLE, // Rippling not allowed + terQUEUED // Transaction is being held in TxQ until fee drops }; //------------------------------------------------------------------------------ -enum TEScodes : TERUnderlyingType -{ +enum TEScodes : TERUnderlyingType { // Note: Exact number must stay stable. This code is stored by value // in metadata for historic transactions. @@ -216,13 +210,12 @@ enum TEScodes : TERUnderlyingType // Implications: // - Applied // - Forwarded - tesSUCCESS = 0 + tesSUCCESS = 0 }; //------------------------------------------------------------------------------ -enum TECcodes : TERUnderlyingType -{ +enum TECcodes : TERUnderlyingType { // Note: Exact numbers must stay stable. These codes are stored by // value in metadata for historic transactions. @@ -243,104 +236,131 @@ enum TECcodes : TERUnderlyingType // Otherwise, treated as terRETRY. // // DO NOT CHANGE THESE NUMBERS: They appear in ledger meta data. - tecCLAIM = 100, - tecPATH_PARTIAL = 101, - tecUNFUNDED_ADD = 102, - tecUNFUNDED_OFFER = 103, - tecUNFUNDED_PAYMENT = 104, - tecFAILED_PROCESSING = 105, - tecDIR_FULL = 121, - tecINSUF_RESERVE_LINE = 122, - tecINSUF_RESERVE_OFFER = 123, - tecNO_DST = 124, - tecNO_DST_INSUF_XRP = 125, - tecNO_LINE_INSUF_RESERVE = 126, - tecNO_LINE_REDUNDANT = 127, - tecPATH_DRY = 128, - tecUNFUNDED = 129, // Deprecated, old ambiguous unfunded. - tecNO_ALTERNATIVE_KEY = 130, - tecNO_REGULAR_KEY = 131, - tecOWNERS = 132, - tecNO_ISSUER = 133, - tecNO_AUTH = 134, - tecNO_LINE = 135, - tecINSUFF_FEE = 136, - tecFROZEN = 137, - tecNO_TARGET = 138, - tecNO_PERMISSION = 139, - tecNO_ENTRY = 140, - tecINSUFFICIENT_RESERVE = 141, - tecNEED_MASTER_KEY = 142, - tecDST_TAG_NEEDED = 143, - tecINTERNAL = 144, - tecOVERSIZE = 145, - tecCRYPTOCONDITION_ERROR = 146, - tecINVARIANT_FAILED = 147, - tecEXPIRED = 148, - tecDUPLICATE = 149, - tecKILLED = 150, - tecHAS_OBLIGATIONS = 151, - tecTOO_SOON = 152, + tecCLAIM = 100, + tecPATH_PARTIAL = 101, + tecUNFUNDED_ADD = 102, + tecUNFUNDED_OFFER = 103, + tecUNFUNDED_PAYMENT = 104, + tecFAILED_PROCESSING = 105, + tecDIR_FULL = 121, + tecINSUF_RESERVE_LINE = 122, + tecINSUF_RESERVE_OFFER = 123, + tecNO_DST = 124, + tecNO_DST_INSUF_XRP = 125, + tecNO_LINE_INSUF_RESERVE = 126, + tecNO_LINE_REDUNDANT = 127, + tecPATH_DRY = 128, + tecUNFUNDED = 129, // Deprecated, old ambiguous unfunded. + tecNO_ALTERNATIVE_KEY = 130, + tecNO_REGULAR_KEY = 131, + tecOWNERS = 132, + tecNO_ISSUER = 133, + tecNO_AUTH = 134, + tecNO_LINE = 135, + tecINSUFF_FEE = 136, + tecFROZEN = 137, + tecNO_TARGET = 138, + tecNO_PERMISSION = 139, + tecNO_ENTRY = 140, + tecINSUFFICIENT_RESERVE = 141, + tecNEED_MASTER_KEY = 142, + tecDST_TAG_NEEDED = 143, + tecINTERNAL = 144, + tecOVERSIZE = 145, + tecCRYPTOCONDITION_ERROR = 146, + tecINVARIANT_FAILED = 147, + tecEXPIRED = 148, + tecDUPLICATE = 149, + tecKILLED = 150, + tecHAS_OBLIGATIONS = 151, + tecTOO_SOON = 152, }; //------------------------------------------------------------------------------ // For generic purposes, a free function that returns the value of a TE*codes. -constexpr TERUnderlyingType TERtoInt (TELcodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TELcodes v) +{ + return safe_cast(v); +} -constexpr TERUnderlyingType TERtoInt (TEMcodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TEMcodes v) +{ + return safe_cast(v); +} -constexpr TERUnderlyingType TERtoInt (TEFcodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TEFcodes v) +{ + return safe_cast(v); +} -constexpr TERUnderlyingType TERtoInt (TERcodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TERcodes v) +{ + return safe_cast(v); +} -constexpr TERUnderlyingType TERtoInt (TEScodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TEScodes v) +{ + return safe_cast(v); +} -constexpr TERUnderlyingType TERtoInt (TECcodes v) -{ return safe_cast(v); } +constexpr TERUnderlyingType +TERtoInt(TECcodes v) +{ + return safe_cast(v); +} //------------------------------------------------------------------------------ // Template class that is specific to selected ranges of error codes. The // Trait tells std::enable_if which ranges are allowed. -template class Trait> +template