rippled
ConsensusTransSetSF.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/ConsensusTransSetSF.h>
21 #include <ripple/app/ledger/TransactionMaster.h>
22 #include <ripple/app/main/Application.h>
23 #include <ripple/app/misc/NetworkOPs.h>
24 #include <ripple/app/misc/Transaction.h>
25 #include <ripple/basics/Log.h>
26 #include <ripple/core/JobQueue.h>
27 #include <ripple/nodestore/Database.h>
28 #include <ripple/protocol/HashPrefix.h>
29 #include <ripple/protocol/digest.h>
30 
31 namespace ripple {
32 
34  : app_(app), m_nodeCache(nodeCache), j_(app.journal("TransactionAcquire"))
35 {
36 }
37 
38 void
40  bool fromFilter,
41  SHAMapHash const& nodeHash,
43  Blob&& nodeData,
44  SHAMapNodeType type) const
45 {
46  if (fromFilter)
47  return;
48 
49  m_nodeCache.insert(nodeHash, nodeData);
50 
51  if ((type == SHAMapNodeType::tnTRANSACTION_NM) && (nodeData.size() > 16))
52  {
53  // this is a transaction, and we didn't have it
54  JLOG(j_.debug())
55  << "Node on our acquiring TX set is TXN we may not have";
56 
57  try
58  {
59  // skip prefix
60  Serializer s(nodeData.data() + 4, nodeData.size() - 4);
61  SerialIter sit(s.slice());
62  auto stx = std::make_shared<STTx const>(std::ref(sit));
63  assert(stx->getTransactionID() == nodeHash.as_uint256());
64  auto const pap = &app_;
65  app_.getJobQueue().addJob(jtTRANSACTION, "TXS->TXN", [pap, stx]() {
66  pap->getOPs().submitTransaction(stx);
67  });
68  }
69  catch (std::exception const& ex)
70  {
71  JLOG(j_.warn())
72  << "Fetched invalid transaction in proposed set. Exception: "
73  << ex.what();
74  }
75  }
76 }
77 
80 {
81  Blob nodeData;
82  if (m_nodeCache.retrieve(nodeHash, nodeData))
83  return nodeData;
84 
85  auto txn =
87 
88  if (txn)
89  {
90  // this is a transaction, and we have it
91  JLOG(j_.trace()) << "Node in our acquiring TX set is TXN we have";
92  Serializer s;
94  txn->getSTransaction()->add(s);
95  assert(sha512Half(s.slice()) == nodeHash.as_uint256());
96  nodeData = s.peekData();
97  return nodeData;
98  }
99 
100  return std::nullopt;
101 }
102 
103 } // namespace ripple
ripple::Application
Definition: Application.h:115
ripple::jtTRANSACTION
@ jtTRANSACTION
Definition: Job.h:63
ripple::TaggedCache::retrieve
bool retrieve(const key_type &key, T &data)
Definition: TaggedCache.h:439
ripple::TaggedCache< SHAMapHash, Blob >
std::exception
STL class.
beast::Journal::trace
Stream trace() const
Severity stream access functions.
Definition: Journal.h:309
std::vector< unsigned char >
ripple::SHAMapNodeType
SHAMapNodeType
Definition: SHAMapTreeNode.h:46
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
ripple::JobQueue::addJob
bool addJob(JobType type, std::string const &name, JobHandler &&jobHandler)
Adds a job to the JobQueue.
Definition: JobQueue.h:166
ripple::ConsensusTransSetSF::m_nodeCache
NodeCache & m_nodeCache
Definition: ConsensusTransSetSF.h:55
ripple::SHAMapHash
Definition: SHAMapHash.h:32
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
ripple::TaggedCache::insert
auto insert(key_type const &key, T const &value) -> std::enable_if_t<!IsKeyCache, ReturnType >
Insert the element into the container.
Definition: TaggedCache.h:411
ripple::TransactionMaster::fetch_from_cache
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
Definition: TransactionMaster.cpp:52
ripple::ConsensusTransSetSF::app_
Application & app_
Definition: ConsensusTransSetSF.h:54
ripple::ConsensusTransSetSF::getNode
std::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
Definition: ConsensusTransSetSF.cpp:79
ripple::Application::getJobQueue
virtual JobQueue & getJobQueue()=0
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
ripple::SerialIter
Definition: Serializer.h:310
ripple::HashPrefix::transactionID
@ transactionID
transaction plus signature to give transaction ID
std::uint32_t
ripple::Serializer
Definition: Serializer.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sha512Half
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition: digest.h:216
ripple::Serializer::peekData
Blob const & peekData() const
Definition: Serializer.h:168
std::optional
beast::Journal::debug
Stream debug() const
Definition: Journal.h:315
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::SHAMapHash::as_uint256
uint256 const & as_uint256() const
Definition: SHAMapHash.h:43
ripple::ConsensusTransSetSF::gotNode
void gotNode(bool fromFilter, SHAMapHash const &nodeHash, std::uint32_t ledgerSeq, Blob &&nodeData, SHAMapNodeType type) const override
Definition: ConsensusTransSetSF.cpp:39
ripple::ConsensusTransSetSF::ConsensusTransSetSF
ConsensusTransSetSF(Application &app, NodeCache &nodeCache)
Definition: ConsensusTransSetSF.cpp:33
ripple::ConsensusTransSetSF::j_
const beast::Journal j_
Definition: ConsensusTransSetSF.h:56
std::ref
T ref(T... args)
std::exception::what
T what(T... args)
ripple::Application::getMasterTransaction
virtual TransactionMaster & getMasterTransaction()=0