-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer.cpp
161 lines (145 loc) · 4.97 KB
/
peer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <fstream>
#include <string>
#include <opendht.h>
#include <chrono>
#include <readline/readline.h>
#include <readline/history.h>
#include "blockchain/blockchain.hpp"
#include "blockchain/litechain.h"
void asciiPrint();
void printHelp();
int main(int argc,char **argv){
if(argc < 2){
std::cerr<<"Format: ./binary /path/to/conf/file"<<std::endl;
std::cout<<"Run {./binary generate} to generate config file "<<std::endl;
return -1;
}
NodeConf conf;
std::cout<<argv[1]<<std::endl;
try{
std::string arg = argv[1];
if(arg == "generate"){
json c;
c["MAINNET"] = false;
c["MAINNET_ADDR"] = "";
c["MAINNET_PORT"] = "";
c["TESTNET_ADDR"] = "";
c["TESTNET_PORT"] = "";
c["PORT"] = "";
c["NODE_ID"] = "";
std::ofstream out("conf.json");
out<<std::setw(4)<<c;
out.close();
return 0;
}
if(argc<4){
std::cerr<<"./binary {conf} {identity} {nodes}"<<std::endl;
return -1;
}
std::ifstream conf_file(argv[1]);
json parsed;
conf_file >> parsed;
conf.node_id = parsed["NODE_ID"];
conf.main = parsed["MAINNET"];
conf.mainnet_addr = parsed["MAINNET_ADDR"];
conf.testnet_addr = parsed["TESTNET_ADDR"];
conf.port = parsed["PORT"];
conf.testnet_port = parsed["TESTNET_PORT"];
conf.mainnet_port = parsed["MAINNET_PORT"];
}catch(const exception& e){
std::cerr<<"Error reading config file:: "<<e.what()<<std::endl;
return -1;
}
std::shared_ptr<Logger> logger = std::make_shared<Logger>();
std::shared_ptr<Identity> id = std::make_shared<Identity>(argv[2]);
std::unique_ptr<Blockchain> blockchain = std::make_unique<Blockchain>(conf,id,logger);
blockchain->AddKnownNodes(argv[3]);
// auto b = new Blockchain(conf,logger);
std::unique_ptr<Litechain> lite = std::make_unique<Litechain>(argv[1],argv[2]);
lite->AddKnownNodes(argv[3]);
asciiPrint();
while(true){
const char* line_read = readline((conf.main?"[MAINNET]:>>":"[TESTNET]:>>"));
if (line_read && *line_read)
add_history(line_read);
std::string line;
if(line_read == nullptr){
std::cin >> line;
}else{
line = line_read;
}
if (!line.empty() && line[0] == '\0')
break;
if(line == "start"){
logger->Debug("Starting blockchain");
blockchain->Start();
}
if(line == "startlite"){
logger->Debug("Starting Litechain");
lite->Start();
}
if(line == "init"){
nlohmann::json obj;
std::ifstream conf_file("/home/emok/PycharmProjects/electionbackends/pubkey");
conf_file >> obj;
obj["timestamp"] = 124453622;
lite->InitChain(obj.dump());
}
if(line == "litetx"){
nlohmann::json obj;
obj["tx"] = "data...";
obj["timestamp"] = 122314241;
lite->BroadcastTX(obj.dump());
}
if(line == "getbx"){
std::cout<<lite->GetBlock(0,"PARENT",false)<<std::endl;
}
if(line == "h" || line == "help"){
printHelp();
continue;
}
if(line == "status"){
logger->Info((blockchain->IsRunning()?"Running.":"Inactive."));
continue;
}
if(line=="routing"){
logger->Info(blockchain->DHTRoutingTable());
continue;
}
if(line=="nodes"){
blockchain->DHTNodes();
// logger->Info(nodes.toString());
continue;
}
if(line == "tx"){
blockchain->BroadcastTransaction();
continue;
}
if(line == "debug"){
blockchain->PrintNodes();
continue;
}
if(line == "viewtx"){
blockchain->PrintTX();
continue;
}
if(line == "viewbx"){
blockchain->PrintBlocks();
continue;
}
}
}
void printHelp(){
std::cout<<"h - print this message"<<std::endl;
std::cout<<"start - start block chain"<<std::endl;
// std::cout<<"h - print this message"<<std::endl;
}
void asciiPrint(){
std::cout<<R"( _______ _____ _____ _____ __ __ _ ____ _____ _____ ____ _ _ ___ ____ _____
| ____\ \ / / _ \_ _| ____| | \/ | / \ / ___|_ _| ____| _ \| \ | |/ _ \| _ \| ____|
| _| \ \ / / | | || | | _| | |\/| | / _ \ \___ \ | | | _| | |_) | \| | | | | | | | _|
| |___ \ V /| |_| || | | |___ | | | |/ ___ \ ___) || | | |___| _ <| |\ | |_| | |_| | |___
|_____| \_/ \___/ |_| |_____| |_| |_/_/ \_\____/ |_| |_____|_| \_\_| \_|\___/|____/|_____|
)"<<endl;
}