-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathLedgerHistory.h
126 lines (99 loc) · 4.11 KB
/
LedgerHistory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_LEDGERHISTORY_H
#define RIPPLE_LEDGERHISTORY_H
#include <beast/insight/Event.h>
namespace ripple {
// VFALCO TODO Rename to OldLedgers ?
/** Retains historical ledgers. */
class LedgerHistory : beast::LeakChecked <LedgerHistory>
{
public:
explicit
LedgerHistory (beast::insight::Collector::ptr const& collector);
/** Track a ledger
@return `true` if the ledger was already tracked
*/
bool addLedger (Ledger::pointer ledger, bool validated);
/** Get the ledgers_by_hash cache hit rate
@return the hit rate
*/
float getCacheHitRate ()
{
return m_ledgers_by_hash.getHitRate ();
}
/** Get a ledger given its squence number
@param ledgerIndex The sequence number of the desired ledger
*/
Ledger::pointer getLedgerBySeq (LedgerIndex ledgerIndex);
/** 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);
/** Retrieve a ledger given its hash
@param ledgerHash The hash of the requested ledger
@return The ledger requested
*/
Ledger::pointer getLedgerByHash (LedgerHash const& ledgerHash);
/** Set the history cache's paramters
@param size The target size of the cache
@param age The target age of the cache, in seconds
*/
void tune (int size, int age);
/** Remove stale cache entries
*/
void sweep ()
{
m_ledgers_by_hash.sweep ();
m_consensus_validated.sweep ();
}
/** Report that we have locally built a particular ledger
*/
void builtLedger (Ledger::ref);
/** Report that we have validated a particular ledger
*/
void validatedLedger (Ledger::ref);
/** Repair a hash to index mapping
@param ledgerIndex The index whose mapping is to be repaired
@param ledgerHash The hash it is to be mapped to
@return `true` if the mapping was repaired
*/
bool fixIndex(LedgerIndex ledgerIndex, LedgerHash const& ledgerHash);
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
@param valid The hash of the ledger we deemed fully valid
*/
void handleMismatch (LedgerHash const& built, LedgerHash const& valid);
beast::insight::Collector::ptr collector_;
beast::insight::Counter mismatch_counter_;
typedef TaggedCache <LedgerHash, Ledger> LedgersByHash;
LedgersByHash m_ledgers_by_hash;
// Maps ledger indexes to the corresponding hashes
// For debug and logging purposes
// 1) The hash of a ledger with that index we build
// 2) The hash of a ledger with that index we validated
typedef TaggedCache <LedgerIndex,
std::pair< LedgerHash, LedgerHash >> ConsensusValidated;
ConsensusValidated m_consensus_validated;
// Maps ledger indexes to the corresponding hash.
std::map <LedgerIndex, LedgerHash> mLedgersByIndex; // validated ledgers
};
} // ripple
#endif