forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBanManagerImpl.cpp
103 lines (91 loc) · 2.68 KB
/
BanManagerImpl.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
102
103
// Copyright 2016 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 "overlay/BanManagerImpl.h"
#include "crypto/KeyUtils.h"
#include "crypto/SecretKey.h"
#include "database/Database.h"
#include "main/Application.h"
#include "util/Logging.h"
namespace stellar
{
using namespace std;
std::unique_ptr<BanManager>
BanManager::create(Application& app)
{
return std::make_unique<BanManagerImpl>(app);
}
BanManagerImpl::BanManagerImpl(Application& app) : mApp(app)
{
}
BanManagerImpl::~BanManagerImpl()
{
}
void
BanManagerImpl::banNode(NodeID nodeID)
{
auto nodeIDString = KeyUtils::toStrKey(nodeID);
auto timer = mApp.getDatabase().getInsertTimer("ban");
auto prep = mApp.getDatabase().getPreparedStatement(
"INSERT INTO ban (nodeid) "
"SELECT :n WHERE NOT EXISTS (SELECT 1 FROM ban WHERE nodeid = :n)");
auto& st = prep.statement();
st.exchange(soci::use(nodeIDString));
st.define_and_bind();
st.execute(true);
}
void
BanManagerImpl::unbanNode(NodeID nodeID)
{
auto nodeIDString = KeyUtils::toStrKey(nodeID);
auto timer = mApp.getDatabase().getDeleteTimer("ban");
auto prep = mApp.getDatabase().getPreparedStatement(
"DELETE FROM ban WHERE nodeid = :n;");
auto& st = prep.statement();
st.exchange(soci::use(nodeIDString));
st.define_and_bind();
st.execute(true);
}
bool
BanManagerImpl::isBanned(NodeID nodeID)
{
auto nodeIDString = KeyUtils::toStrKey(nodeID);
auto timer = mApp.getDatabase().getSelectTimer("ban");
auto prep = mApp.getDatabase().getPreparedStatement(
"SELECT count(*) FROM ban WHERE nodeid = :n");
uint32_t count;
auto& st = prep.statement();
st.exchange(soci::into(count));
st.exchange(soci::use(nodeIDString));
st.define_and_bind();
st.execute(true);
return count == 1;
}
std::vector<std::string>
BanManagerImpl::getBans()
{
std::vector<std::string> result;
std::string nodeIDString;
auto timer = mApp.getDatabase().getSelectTimer("ban");
auto prep =
mApp.getDatabase().getPreparedStatement("SELECT nodeid FROM ban");
auto& st = prep.statement();
st.exchange(soci::into(nodeIDString));
st.define_and_bind();
st.execute(true);
while (st.got_data())
{
result.push_back(nodeIDString);
st.fetch();
}
return result;
}
void
BanManager::dropAll(Database& db)
{
db.getSession() << "DROP TABLE IF EXISTS ban";
db.getSession() << "CREATE TABLE ban ("
"nodeid CHARACTER(56) NOT NULL PRIMARY KEY"
")";
}
}