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_;
66  jtTRANSACTION, "TXS->TXN", [pap, stx](Job&) {
67  pap->getOPs().submitTransaction(stx);
68  });
69  }
70  catch (std::exception const&)
71  {
72  JLOG(j_.warn()) << "Fetched invalid transaction in proposed set";
73  }
74  }
75 }
76 
77 boost::optional<Blob>
79 {
80  Blob nodeData;
81  if (m_nodeCache.retrieve(nodeHash, nodeData))
82  return nodeData;
83 
84  auto txn =
86 
87  if (txn)
88  {
89  // this is a transaction, and we have it
90  JLOG(j_.trace()) << "Node in our acquiring TX set is TXN we have";
91  Serializer s;
93  txn->getSTransaction()->add(s);
94  assert(sha512Half(s.slice()) == nodeHash.as_uint256());
95  nodeData = s.peekData();
96  return nodeData;
97  }
98 
99  return boost::none;
100 }
101 
102 } // namespace ripple
ripple::Application
Definition: Application.h:97
ripple::jtTRANSACTION
@ jtTRANSACTION
Definition: Job.h:51
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:126
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::ConsensusTransSetSF::getNode
boost::optional< Blob > getNode(SHAMapHash const &nodeHash) const override
Definition: ConsensusTransSetSF.cpp:78
ripple::SHAMapHash
Definition: SHAMapTreeNode.h:47
ripple::SHAMapNodeType::tnTRANSACTION_NM
@ tnTRANSACTION_NM
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::Application::getJobQueue
virtual JobQueue & getJobQueue()=0
ripple::Serializer::slice
Slice slice() const noexcept
Definition: Serializer.h:63
ripple::Job
Definition: Job.h:82
ripple::SerialIter
Definition: Serializer.h:308
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::TaggedCache::insert
bool insert(key_type const &key, T const &value)
Insert the element into the container.
Definition: TaggedCache.h:433
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:166
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: SHAMapTreeNode.h:58
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)
ripple::TaggedCache::retrieve
bool retrieve(const key_type &key, T &data)
Definition: TaggedCache.h:445
ripple::Application::getMasterTransaction
virtual TransactionMaster & getMasterTransaction()=0