Skip to content

Commit 7cef939

Browse files
authored
feat: add methods for first and last block (#183)
* feat: add methods for first and last block * fix: typo * fix: add missing paths * fix: add missing mocks
1 parent 2728ec8 commit 7cef939

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

src/api/blocks/blocks.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ std::string Blocks::get(const char* blockId) {
1919

2020
/**/
2121

22+
std::string Blocks::first() {
23+
return http_->get(paths::Blocks::first(this->host_).c_str());
24+
}
25+
26+
/**/
27+
28+
std::string Blocks::last() {
29+
return http_->get(paths::Blocks::last(this->host_).c_str());
30+
}
31+
32+
/**/
33+
2234
std::string Blocks::all(const char* const query) {
2335
return http_->get(paths::Blocks::all(this->host_, query).c_str());
2436
}

src/api/paths.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ std::string Blocks::get(Host& newHost, const char* blockId) {
7474

7575
////////////////////////////////////////////////////////////////////////////////
7676

77+
std::string Blocks::first(Host& newHost) {
78+
std::string url;
79+
url.reserve(URL_MAX_LEN);
80+
url += newHost.toString().c_str();
81+
url += Blocks::base();
82+
url += "/first";
83+
return url;
84+
}
85+
86+
////////////////////////////////////////////////////////////////////////////////
87+
88+
std::string Blocks::last(Host& newHost) {
89+
std::string url;
90+
url.reserve(URL_MAX_LEN);
91+
url += newHost.toString().c_str();
92+
url += Blocks::base();
93+
url += "/last";
94+
return url;
95+
}
96+
97+
////////////////////////////////////////////////////////////////////////////////
98+
7799
std::string Blocks::all(Host& newHost, const char* const query) {
78100
std::string url;
79101
url.reserve(URL_MAX_LEN);

src/include/cpp-client/api/blocks/blocks.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class IBlocks : public Base {
2424
public:
2525
virtual ~IBlocks() {}
2626
virtual std::string get(const char* const blockId) = 0;
27+
virtual std::string first() = 0;
28+
virtual std::string last() = 0;
2729
virtual std::string all(const char* const query) = 0;
2830
virtual std::string transactions(const char* const blockId) = 0;
2931
virtual std::string search(const std::map<std::string, std::string>& bodyParameters, const char* const query) = 0;
@@ -39,6 +41,8 @@ class Blocks : public IBlocks {
3941
Blocks(Host& host, IHTTP& http) : IBlocks(host, http) {}
4042

4143
std::string get(const char* const blockId) override;
44+
std::string first() override;
45+
std::string last() override;
4246
std::string all(const char* const query) override;
4347
std::string transactions(const char* const blockId) override;
4448
std::string search(const std::map<std::string, std::string>& bodyParameters, const char* const query) override;

src/include/cpp-client/api/paths.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct Blockchain {
3838
struct Blocks { // NOLINT
3939
static std::string base();
4040
static std::string get(Host& newHost, const char* const blockId);
41+
static std::string first(Host& newHost);
42+
static std::string last(Host& newHost);
4143
static std::string all(Host& newHost, const char* const query = DEFAULT_QUERY);
4244
static std::string transactions(Host& newHost, const char* const blockId);
4345
static std::pair<std::string, std::string> search(Host& newHost,

test/api/blocks.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,101 @@ TEST(api, test_block) { // NOLINT
6666

6767
/**/
6868

69+
TEST(api, test_block_first) { // NOLINT
70+
Ark::Client::Connection<MockApi> connection(tIp, tPort);
71+
72+
const std::string expected_response = R"({
73+
"data": {
74+
"id": "13114381566690093367",
75+
"version": 0,
76+
"height": 1,
77+
"previous": "0",
78+
"forged": {
79+
"reward": "0",
80+
"fee": "0",
81+
"total": "0",
82+
"amount": "12500000000000000"
83+
},
84+
"payload": {
85+
"hash": "2a44f340d76ffc3df204c5f38cd355b7496c9065a1ade2ef92071436bd72e867",
86+
"length": 11395
87+
},
88+
"generator": {
89+
"address": "D6Z26L69gdk9qYmTv5uzk3uGepigtHY4ax",
90+
"publicKey": "03d3fdad9c5b25bf8880e6b519eb3611a5c0b31adebc8455f0e096175b28321aff"
91+
},
92+
"signature": "3044022035694a9b99a9236655c658eb07fc3b02ce5edcc24b76424a7287c54ed3822b0602203621e92defb360490610f763d85e94c2db2807a4bd7756cc8a6a585463ef7bae",
93+
"confirmations": 4346566,
94+
"transactions": 52,
95+
"timestamp": {
96+
"epoch": 0,
97+
"unix": 1490101200,
98+
"human": "2017-03-21T13:00:00.000Z"
99+
}
100+
}
101+
})";
102+
103+
EXPECT_CALL(connection.api.blocks, first())
104+
.Times(1)
105+
.WillOnce(Return(expected_response));
106+
107+
const auto block = connection.api.blocks.first();
108+
109+
auto responseMatches = strcmp(expected_response.c_str(),
110+
block.c_str()) == 0;
111+
ASSERT_TRUE(responseMatches);
112+
}
113+
114+
/**/
115+
116+
TEST(api, test_block_last) { // NOLINT
117+
Ark::Client::Connection<MockApi> connection(tIp, tPort);
118+
119+
const std::string expected_response = R"({
120+
"data": {
121+
"id": "5768900e27621b95dc201fc4e6ce16e72a39e0c625a1c659a23e83eac69605e0",
122+
"version": 0,
123+
"height": 4346647,
124+
"previous": "4deae70324c919d3a79f0f065cfa085a475a7245bcd04d4e558d3205658fc706",
125+
"forged": {
126+
"reward": "200000000",
127+
"fee": "10000000",
128+
"total": "210000000",
129+
"amount": "900000000"
130+
},
131+
"payload": {
132+
"hash": "b48f23356cc70d13b0a0c0ba6811be0144a9fc673cc9f723f036187c3bb67f2d",
133+
"length": 32
134+
},
135+
"generator": {
136+
"username": "genesis_40",
137+
"address": "D8xN3Nsa3KfC3H68Ek9xnkfdSwzv8Kkh3q",
138+
"publicKey": "026a423b3323de175dd82788c7eab57850c6a37ea6a470308ebadd7007baf8ceb3"
139+
},
140+
"signature": "304402203b6251780c8baead56882aa5aee188f85f8941a1bca272ef6ff3bf9d26cfeed602207634d5268a130937603b774a7282e44065370785dc4b809cf0e98f0e56252bcc",
141+
"confirmations": 0,
142+
"transactions": 1,
143+
"timestamp": {
144+
"epoch": 92536208,
145+
"unix": 1582637408,
146+
"human": "2020-02-25T13:30:08.000Z"
147+
}
148+
}
149+
})";
150+
151+
EXPECT_CALL(connection.api.blocks, last())
152+
.Times(1)
153+
.WillOnce(Return(expected_response));
154+
155+
const auto block = connection.api.blocks.last();
156+
157+
auto responseMatches = strcmp(expected_response.c_str(),
158+
block.c_str()) == 0;
159+
ASSERT_TRUE(responseMatches);
160+
}
161+
162+
/**/
163+
69164
TEST(api, test_block_transactions) { // NOLINT
70165
Ark::Client::Connection<MockApi> connection(tIp, tPort);
71166

test/api/paths.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ TEST(paths, test_blocks) {
3333
const auto get = paths::Blocks::get(testHost, "58328125061111756");
3434
ASSERT_STREQ("0.0.0.0:4003/api/blocks/58328125061111756", get.c_str());
3535

36+
const auto first = paths::Blocks::first(testHost);
37+
ASSERT_STREQ("0.0.0.0:4003/api/blocks/first", first.c_str());
38+
39+
const auto last = paths::Blocks::last(testHost);
40+
ASSERT_STREQ("0.0.0.0:4003/api/blocks/last", last.c_str());
41+
3642
const auto all = paths::Blocks::all(testHost, "?limit=1&page=5");
3743
ASSERT_STREQ("0.0.0.0:4003/api/blocks?limit=1&page=5", all.c_str());
3844

test/mocks/mock_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class MockBlocks : public Ark::Client::api::IBlocks { // NOLINT
3131
MockBlocks(Ark::Client::Host& host, Ark::Client::IHTTP& http) : IBlocks(host, http) {}
3232

3333
MOCK_METHOD1(get, std::string(const char* const));
34+
MOCK_METHOD0(first, std::string());
35+
MOCK_METHOD0(last, std::string());
3436
MOCK_METHOD1(all, std::string(const char* const));
3537
MOCK_METHOD1(transactions, std::string(const char* const));
3638
MOCK_METHOD2(search, std::string(const std::map<std::string, std::string>&, const char* const));

0 commit comments

Comments
 (0)