rippled
protocol/impl/LedgerHeader.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2023 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/protocol/LedgerHeader.h>
21 
22 namespace ripple {
23 
24 void
25 addRaw(LedgerHeader const& info, Serializer& s, bool includeHash)
26 {
27  s.add32(info.seq);
28  s.add64(info.drops.drops());
29  s.addBitString(info.parentHash);
30  s.addBitString(info.txHash);
31  s.addBitString(info.accountHash);
32  s.add32(info.parentCloseTime.time_since_epoch().count());
33  s.add32(info.closeTime.time_since_epoch().count());
34  s.add8(info.closeTimeResolution.count());
35  s.add8(info.closeFlags);
36 
37  if (includeHash)
38  s.addBitString(info.hash);
39 }
40 
41 LedgerHeader
42 deserializeHeader(Slice data, bool hasHash)
43 {
44  SerialIter sit(data.data(), data.size());
45 
46  LedgerHeader header;
47 
48  header.seq = sit.get32();
49  header.drops = sit.get64();
50  header.parentHash = sit.get256();
51  header.txHash = sit.get256();
52  header.accountHash = sit.get256();
53  header.parentCloseTime =
57  header.closeFlags = sit.get8();
58 
59  if (hasHash)
60  header.hash = sit.get256();
61 
62  return header;
63 }
64 
65 LedgerHeader
66 deserializePrefixedHeader(Slice data, bool hasHash)
67 {
68  return deserializeHeader(data + 4, hasHash);
69 }
70 
71 } // namespace ripple
ripple::LedgerHeader::closeFlags
int closeFlags
Definition: LedgerHeader.h:63
ripple::Slice
An immutable linear range of bytes.
Definition: Slice.h:44
ripple::XRPAmount::drops
constexpr value_type drops() const
Returns the number of drops.
Definition: XRPAmount.h:172
ripple::LedgerHeader::parentHash
uint256 parentHash
Definition: LedgerHeader.h:52
ripple::Serializer::add8
int add8(unsigned char i)
Definition: Serializer.cpp:166
std::chrono::duration
ripple::LedgerHeader::seq
LedgerIndex seq
Definition: LedgerHeader.h:41
ripple::LedgerHeader::accountHash
uint256 accountHash
Definition: LedgerHeader.h:51
ripple::Serializer::add64
int add64(std::uint64_t i)
Definition: Serializer.cpp:60
ripple::LedgerHeader::txHash
uint256 txHash
Definition: LedgerHeader.h:50
ripple::LedgerHeader::hash
uint256 hash
Definition: LedgerHeader.h:49
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::LedgerHeader::parentCloseTime
NetClock::time_point parentCloseTime
Definition: LedgerHeader.h:42
ripple::SerialIter::get8
unsigned char get8()
Definition: Serializer.cpp:362
ripple::SerialIter::get256
uint256 get256()
Definition: Serializer.h:377
ripple::SerialIter::get64
std::uint64_t get64()
Definition: Serializer.cpp:399
std::chrono::time_point
ripple::SerialIter
Definition: Serializer.h:311
ripple::LedgerHeader::closeTime
NetClock::time_point closeTime
Definition: LedgerHeader.h:72
ripple::LedgerHeader
Information about the notional ledger backing the view.
Definition: LedgerHeader.h:33
ripple::Serializer
Definition: Serializer.h:40
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Serializer::addBitString
int addBitString(base_uint< Bits, Tag > const &v)
Definition: Serializer.h:98
ripple::addRaw
void addRaw(LedgerHeader const &info, Serializer &s, bool includeHash)
Definition: protocol/impl/LedgerHeader.cpp:25
ripple::LedgerHeader::drops
XRPAmount drops
Definition: LedgerHeader.h:54
ripple::LedgerHeader::closeTimeResolution
NetClock::duration closeTimeResolution
Definition: LedgerHeader.h:66
ripple::deserializePrefixedHeader
LedgerHeader deserializePrefixedHeader(Slice data, bool hasHash)
Deserialize a ledger header (prefixed with 4 bytes) from a byte array.
Definition: protocol/impl/LedgerHeader.cpp:66
ripple::deserializeHeader
LedgerHeader deserializeHeader(Slice data, bool hasHash)
Deserialize a ledger header from a byte array.
Definition: protocol/impl/LedgerHeader.cpp:42
std::chrono::duration::count
T count(T... args)
ripple::Serializer::add32
int add32(std::uint32_t i)
Definition: Serializer.cpp:38
ripple::SerialIter::get32
std::uint32_t get32()
Definition: Serializer.cpp:386