rippled
Loading...
Searching...
No Matches
libxrpl/protocol/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 <xrpl/basics/Slice.h>
21#include <xrpl/basics/chrono.h>
22#include <xrpl/protocol/LedgerHeader.h>
23#include <xrpl/protocol/Serializer.h>
24
25namespace ripple {
26
27void
28addRaw(LedgerHeader const& info, Serializer& s, bool includeHash)
29{
30 s.add32(info.seq);
31 s.add64(info.drops.drops());
33 s.addBitString(info.txHash);
35 s.add32(info.parentCloseTime.time_since_epoch().count());
36 s.add32(info.closeTime.time_since_epoch().count());
38 s.add8(info.closeFlags);
39
40 if (includeHash)
41 s.addBitString(info.hash);
42}
43
44LedgerHeader
45deserializeHeader(Slice data, bool hasHash)
46{
47 SerialIter sit(data.data(), data.size());
48
49 LedgerHeader header;
50
51 header.seq = sit.get32();
52 header.drops = sit.get64();
53 header.parentHash = sit.get256();
54 header.txHash = sit.get256();
55 header.accountHash = sit.get256();
56 header.parentCloseTime =
60 header.closeFlags = sit.get8();
61
62 if (hasHash)
63 header.hash = sit.get256();
64
65 return header;
66}
67
68LedgerHeader
70{
71 return deserializeHeader(data + 4, hasHash);
72}
73
74} // namespace ripple
std::uint32_t get32()
unsigned char get8()
std::uint64_t get64()
int addBitString(base_uint< Bits, Tag > const &v)
Definition Serializer.h:131
int add8(unsigned char i)
An immutable linear range of bytes.
Definition Slice.h:46
constexpr value_type drops() const
Returns the number of drops.
Definition XRPAmount.h:177
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
LedgerHeader deserializePrefixedHeader(Slice data, bool hasHash=false)
Deserialize a ledger header (prefixed with 4 bytes) from a byte array.
void addRaw(LedgerHeader const &, Serializer &, bool includeHash=false)
LedgerHeader deserializeHeader(Slice data, bool hasHash=false)
Deserialize a ledger header from a byte array.
Information about the notional ledger backing the view.
NetClock::time_point closeTime
NetClock::duration closeTimeResolution
NetClock::time_point parentCloseTime
T time_since_epoch(T... args)