forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCPDriver.cpp
101 lines (86 loc) · 2.55 KB
/
SCPDriver.cpp
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
// Copyright 2014 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "SCPDriver.h"
#include <algorithm>
#include "crypto/Hex.h"
#include "crypto/KeyUtils.h"
#include "crypto/SHA.h"
#include "crypto/SecretKey.h"
#include "xdrpp/marshal.h"
namespace stellar
{
std::string
SCPDriver::getValueString(Value const& v) const
{
uint256 valueHash = sha256(xdr::xdr_to_opaque(v));
return hexAbbrev(valueHash);
}
std::string
SCPDriver::toStrKey(PublicKey const& pk, bool fullKey) const
{
return fullKey ? KeyUtils::toStrKey(pk) : toShortString(pk);
}
std::string
SCPDriver::toShortString(PublicKey const& pk) const
{
return KeyUtils::toShortString(pk);
}
// values used to switch hash function between priority and neighborhood checks
static const uint32 hash_N = 1;
static const uint32 hash_P = 2;
static const uint32 hash_K = 3;
static uint64
hashHelper(uint64 slotIndex, Value const& prev,
std::function<void(SHA256*)> extra)
{
auto h = SHA256::create();
h->add(xdr::xdr_to_opaque(slotIndex));
h->add(xdr::xdr_to_opaque(prev));
extra(h.get());
uint256 t = h->finish();
uint64 res = 0;
for (size_t i = 0; i < sizeof(res); i++)
{
res = (res << 8) | t[i];
}
return res;
}
uint64
SCPDriver::computeHashNode(uint64 slotIndex, Value const& prev, bool isPriority,
int32_t roundNumber, NodeID const& nodeID)
{
return hashHelper(slotIndex, prev, [&](SHA256* h) {
h->add(xdr::xdr_to_opaque(isPriority ? hash_P : hash_N));
h->add(xdr::xdr_to_opaque(roundNumber));
h->add(xdr::xdr_to_opaque(nodeID));
});
}
uint64
SCPDriver::computeValueHash(uint64 slotIndex, Value const& prev,
int32_t roundNumber, Value const& value)
{
return hashHelper(slotIndex, prev, [&](SHA256* h) {
h->add(xdr::xdr_to_opaque(hash_K));
h->add(xdr::xdr_to_opaque(roundNumber));
h->add(xdr::xdr_to_opaque(value));
});
}
static const int MAX_TIMEOUT_SECONDS = (30 * 60);
std::chrono::milliseconds
SCPDriver::computeTimeout(uint32 roundNumber)
{
// straight linear timeout
// starting at 1 second and capping at MAX_TIMEOUT_SECONDS
int timeoutInSeconds;
if (roundNumber > MAX_TIMEOUT_SECONDS)
{
timeoutInSeconds = MAX_TIMEOUT_SECONDS;
}
else
{
timeoutInSeconds = (int)roundNumber;
}
return std::chrono::seconds(timeoutInSeconds);
}
}