Skip to content

Commit 8fb01b2

Browse files
authored
butterflynet (#593)
Signed-off-by: turuslan <[email protected]>
1 parent c0e2ead commit 8fb01b2

File tree

19 files changed

+152
-126
lines changed

19 files changed

+152
-126
lines changed

core/api/storage_miner/storage_api.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "sector_storage/impl/remote_worker.hpp"
1313

1414
namespace fc::api {
15-
using common::HttpUri;
1615
using miner::kMinerVersion;
1716
using sector_storage::RemoteWorker;
1817

core/blockchain/block_validator/validator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "cbor_blake/memory.hpp"
1313
#include "common/error_text.hpp"
1414
#include "common/from_span.hpp"
15+
#include "common/prometheus/metrics.hpp"
16+
#include "common/prometheus/since.hpp"
1517
#include "crypto/bls/impl/bls_provider_impl.hpp"
1618
#include "primitives/block/rand.hpp"
1719
#include "primitives/tipset/chain.hpp"
@@ -48,6 +50,14 @@ namespace fc::blockchain::block_validator {
4850

4951
outcome::result<void> BlockValidator::validate(const TsBranchPtr &branch,
5052
const BlockHeader &block) {
53+
static auto &metricTime{prometheus::BuildHistogram()
54+
.Name("lotus_block_validation_ms")
55+
.Help("Duration for Block Validation in ms")
56+
.Register(prometheusRegistry())
57+
.Add({}, kDefaultPrometheusMsBuckets)};
58+
auto BOOST_OUTCOME_TRY_UNIQUE_NAME{
59+
gsl::finally([since{Since{}}] { metricTime.Observe(since.ms()); })};
60+
5161
OUTCOME_TRY(block_cbor, codec::cbor::encode(block));
5262
const auto block_cid{CbCid::hash(block_cbor)};
5363
storage::OneKey key{BytesIn{block_cid}, kv};

core/common/libp2p/soralog.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace fc {
3434
groups:
3535
- name: main
3636
sink: file
37-
level: debug
37+
level: info
3838
children:
3939
- name: libp2p
4040
)",

core/common/uri_parser/percent_encoding.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#include <gsl/gsl_util>
2929
#include <iomanip>
3030
#include <sstream>
31-
#include <stdexcept>
31+
32+
#include "common/error_text.hpp"
3233

3334
// unreserved
3435
static const std::string unreserved(
@@ -53,16 +54,17 @@ inline static bool need_encode(const char c) {
5354
return true;
5455
}
5556

56-
static int decodePercentEscapedByte(std::istringstream &iss) {
57+
static fc::outcome::result<int> decodePercentEscapedByte(
58+
std::istringstream &iss) {
5759
auto c = iss.get();
5860
if (c != '%') {
59-
throw std::runtime_error("Wrong token for start percent-encoded sequence");
61+
return ERROR_TEXT("Wrong token for start percent-encoded sequence");
6062
}
6163

6264
int result = 0;
6365

6466
if (iss.eof()) {
65-
throw std::runtime_error(
67+
return ERROR_TEXT(
6668
"Unxpected end of data during try parse percent-encoded symbol");
6769
}
6870
c = iss.get();
@@ -73,14 +75,14 @@ static int decodePercentEscapedByte(std::istringstream &iss) {
7375
} else if (c >= 'A' && c <= 'F') {
7476
result = 10 + c - 'A';
7577
} else if (c == -1) {
76-
throw std::runtime_error(
78+
return ERROR_TEXT(
7779
"Unxpected end of data during try parse percent-encoded symbol");
7880
} else {
79-
throw std::runtime_error("Wrong percent-encoded symbol");
81+
return ERROR_TEXT("Wrong percent-encoded symbol");
8082
}
8183

8284
if (iss.eof()) {
83-
throw std::runtime_error(
85+
return ERROR_TEXT(
8486
"Unxpected end of data during try parse percent-encoded symbol");
8587
}
8688
c = iss.get();
@@ -91,17 +93,18 @@ static int decodePercentEscapedByte(std::istringstream &iss) {
9193
} else if (c >= 'A' && c <= 'F') {
9294
result = (result << 4) | (10 + c - 'A');
9395
} else if (c == -1) {
94-
throw std::runtime_error(
96+
return ERROR_TEXT(
9597
"Unxpected end of data during try parse percent-encoded symbol");
9698
} else {
97-
throw std::runtime_error("Wrong percent-encoded symbol");
99+
return ERROR_TEXT("Wrong percent-encoded symbol");
98100
}
99101

100102
return result;
101103
}
102104

103-
static uint32_t decodePercentEscaped(std::istringstream &iss, bool &isUtf8) {
104-
auto c = decodePercentEscapedByte(iss);
105+
static fc::outcome::result<uint32_t> decodePercentEscaped(
106+
std::istringstream &iss, bool &isUtf8) {
107+
OUTCOME_TRY(c, decodePercentEscapedByte(iss));
105108

106109
if (!isUtf8) {
107110
return static_cast<uint32_t>(c);
@@ -140,7 +143,7 @@ static uint32_t decodePercentEscaped(std::istringstream &iss, bool &isUtf8) {
140143
return static_cast<uint32_t>(fc);
141144
}
142145
try {
143-
c = decodePercentEscapedByte(iss);
146+
OUTCOME_TRYA(c, decodePercentEscapedByte(iss));
144147
} catch (...) {
145148
iss.clear(std::istringstream::goodbit);
146149
iss.seekg(p);
@@ -157,15 +160,16 @@ static uint32_t decodePercentEscaped(std::istringstream &iss, bool &isUtf8) {
157160
return symbol;
158161
}
159162

160-
std::string PercentEncoding::decode(const std::string &input) {
163+
fc::outcome::result<std::string> PercentEncoding::decode(
164+
const std::string &input) {
161165
std::istringstream iss(input);
162166
std::ostringstream oss;
163167
bool isUtf8 = true;
164168

165169
while (!iss.eof()) {
166170
int c = iss.peek();
167171
if (c == '%') {
168-
auto symbol = decodePercentEscaped(iss, isUtf8);
172+
OUTCOME_TRY(symbol, decodePercentEscaped(iss, isUtf8));
169173

170174
if (symbol <= 0b0111'1111) // 7bit -> 1byte
171175
{

core/common/uri_parser/percent_encoding.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727

2828
#include <string>
2929

30+
#include "common/outcome.hpp"
31+
3032
class PercentEncoding final {
3133
public:
3234
static std::string encode(const std::string &input);
3335

34-
static std::string decode(std::string const &input);
36+
static fc::outcome::result<std::string> decode(std::string const &input);
3537
};

core/common/uri_parser/uri_parser.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
#include <cstring>
3131
#include <iomanip>
3232
#include <sstream>
33-
#include <stdexcept>
33+
34+
#include "common/error_text.hpp"
3435

3536
namespace fc::common {
36-
HttpUri::HttpUri(const std::string &uri) {
37-
try {
38-
parse(uri);
39-
} catch (const std::exception &exception) {
40-
throw std::runtime_error(std::string("Bad URI ← ") + exception.what());
41-
}
37+
inline const auto kOutOfData{ERROR_TEXT("HttpUri: Out of data")};
38+
39+
outcome::result<HttpUri> HttpUri::parse(const std::string &string) {
40+
HttpUri uri;
41+
OUTCOME_TRY(uri.parseThis(string));
42+
return uri;
4243
}
4344

44-
void HttpUri::parse(const std::string &string) {
45+
outcome::result<void> HttpUri::parseThis(const std::string &string) {
4546
const char *s = string.c_str();
4647
const auto *end = string.c_str() + string.size();
4748

@@ -51,7 +52,7 @@ namespace fc::common {
5152
s += 2;
5253
} else if (*s == '/') {
5354
scheme_ = Scheme::UNDEFINED;
54-
parsePath(s, end);
55+
OUTCOME_TRY(parsePath(s, end));
5556
} else if (string.length() >= 7 && strncasecmp(s, "http://", 7) == 0) {
5657
scheme_ = Scheme::HTTP;
5758
port_ = 80;
@@ -61,84 +62,86 @@ namespace fc::common {
6162
port_ = 443;
6263
s += 8;
6364
} else {
64-
throw std::runtime_error("Wrong scheme");
65+
return ERROR_TEXT("Wrong scheme");
6566
}
6667

6768
// host:
6869
while (*s != 0 && *s != ':' && *s != '/' && *s != '?' && *s != '#'
6970
&& isspace(*s) == 0) {
7071
if (isalnum(*s) == 0 && *s != '.' && *s != '-') {
71-
throw std::runtime_error("Wrong hostname");
72+
return ERROR_TEXT("Wrong hostname");
7273
}
7374
host_.push_back(static_cast<char>(tolower(*s)));
7475
if (++s > end) {
75-
throw std::runtime_error("Out of data");
76+
return kOutOfData;
7677
}
7778
}
7879

7980
// port:
8081
if (*s == ':') {
8182
if (++s > end) {
82-
throw std::runtime_error("Out of data");
83+
return kOutOfData;
8384
}
8485
uint64_t port = 0;
8586
while (*s != 0 && *s != '/' && *s != '?' && *s != '#'
8687
&& isspace(*s) == 0) {
8788
if (isdigit(*s) == 0) {
88-
throw std::runtime_error("Wrong port");
89+
return ERROR_TEXT("Wrong port");
8990
}
9091
port = port * 10 + *s - '0';
9192
if (port < 1 || port > 65535) {
92-
throw std::runtime_error("Wrong port");
93+
return ERROR_TEXT("Wrong port");
9394
}
9495
if (++s > end) {
95-
throw std::runtime_error("Out of data");
96+
return kOutOfData;
9697
}
9798
}
9899
port_ = static_cast<uint16_t>(port);
99100
}
100101

101102
// path:
102-
parsePath(s, end);
103+
OUTCOME_TRY(parsePath(s, end));
104+
return outcome::success();
103105
}
104106

105-
void HttpUri::parsePath(const char *s, const char *end) {
107+
outcome::result<void> HttpUri::parsePath(const char *s, const char *end) {
106108
if (*s == '/') {
107109
while (*s != 0 && *s != '?' && *s != '#' && isspace(*s) == 0) {
108110
path_.push_back(*s);
109111
if (++s > end) {
110-
throw std::runtime_error("Out of data");
112+
return kOutOfData;
111113
}
112114
}
113115
}
114116

115117
// query:
116118
if (*s == '?') {
117119
if (++s > end) {
118-
throw std::runtime_error("Out of data");
120+
return kOutOfData;
119121
}
120122
hasQuery_ = true;
121123
while (*s != 0 && *s != '#' && isspace(*s) == 0) {
122124
query_.push_back(*s);
123125
if (++s > end) {
124-
throw std::runtime_error("Out of data");
126+
return kOutOfData;
125127
}
126128
}
127129
}
128130

129131
// fragment:
130132
if (*s == '#') {
131133
if (++s > end) {
132-
throw std::runtime_error("Out of data");
134+
return kOutOfData;
133135
}
134136
hasFragment_ = true;
135137
while (*s != 0 && isspace(*s) == 0) {
136138
fragment_.push_back(*s);
137139
if (++s > end) {
138-
throw std::runtime_error("Out of data");
140+
return kOutOfData;
139141
}
140142
}
141143
}
144+
return outcome::success();
142145
}
143146

144147
const std::string &HttpUri::str() const {
@@ -171,7 +174,7 @@ namespace fc::common {
171174
return thisAsString_;
172175
}
173176

174-
std::string HttpUri::urldecode(const std::string &input_) {
177+
outcome::result<std::string> HttpUri::urldecode(const std::string &input_) {
175178
std::string input(input_);
176179
std::replace(input.begin(), input.end(), '+', ' ');
177180
return PercentEncoding::decode(input);

core/common/uri_parser/uri_parser.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@
2727

2828
#include <string>
2929

30+
#include "common/outcome.hpp"
31+
3032
namespace fc::common {
3133
class HttpUri final {
3234
public:
3335
enum class Scheme { UNDEFINED, HTTP, HTTPS };
3436

3537
HttpUri() = default;
3638

37-
explicit HttpUri(const std::string &uri);
38-
39-
virtual ~HttpUri() = default;
40-
41-
void parse(const std::string &string);
39+
static outcome::result<HttpUri> parse(const std::string &string);
40+
outcome::result<void> parseThis(const std::string &string);
4241

4342
const std::string &str() const;
4443

@@ -111,12 +110,12 @@ namespace fc::common {
111110
}
112111
}
113112

114-
static std::string urldecode(const std::string &input);
113+
static outcome::result<std::string> urldecode(const std::string &input);
115114

116115
static std::string urlencode(const std::string &input);
117116

118117
private:
119-
void parsePath(const char *s, const char *end);
118+
outcome::result<void> parsePath(const char *s, const char *end);
120119

121120
Scheme scheme_ = Scheme::UNDEFINED;
122121
std::string host_;

core/config/profile_config.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace fc::config {
3131
// one string, it's an error, and exception will be thrown.
3232
std::string const &value = get_single_string(values);
3333
if (value == "mainnet" || value == "2k" || value == "no-upgrades"
34-
|| value == "interopnet") {
34+
|| value == "interopnet" || value == "butterflynet") {
3535
v = boost::any(Profile{value});
3636
} else {
3737
throw validation_error(validation_error::invalid_option_value);
@@ -53,6 +53,8 @@ namespace fc::config {
5353
setParamsNoUpgrades();
5454
} else if (profile == "interopnet") {
5555
setParamsInteropnet();
56+
} else if (profile == "butterflynet") {
57+
setParamsButterfly();
5658
}
5759
}),
5860
"Network parameters profile configuration that defines network "

0 commit comments

Comments
 (0)