Skip to content

Commit dcb4d17

Browse files
Feature/message storage (#75)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent c6feeae commit dcb4d17

17 files changed

+436
-1
lines changed

core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55

6+
add_subdirectory(blockchain)
67
add_subdirectory(clock)
78
add_subdirectory(codec)
89
add_subdirectory(common)

core/blockchain/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_subdirectory(message_pool)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(message_pool
7+
impl/gas_price_scored_message_storage.cpp
8+
impl/message_pool_error.cpp)
9+
target_link_libraries(message_pool
10+
outcome
11+
message
12+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "blockchain/message_pool/impl/gas_price_scored_message_storage.hpp"
7+
8+
#include "blockchain/message_pool/message_pool_error.hpp"
9+
10+
using fc::blockchain::message_pool::GasPriceScoredMessageStorage;
11+
using fc::blockchain::message_pool::MessagePoolError;
12+
using fc::vm::message::SignedMessage;
13+
14+
fc::outcome::result<void> GasPriceScoredMessageStorage::put(
15+
const SignedMessage &message) {
16+
auto res = messages_.insert(message);
17+
if (!res.second) return MessagePoolError::MESSAGE_ALREADY_IN_POOL;
18+
return fc::outcome::success();
19+
}
20+
21+
void GasPriceScoredMessageStorage::remove(const SignedMessage &message) {
22+
messages_.erase(message);
23+
}
24+
25+
std::vector<SignedMessage> GasPriceScoredMessageStorage::getTopScored(
26+
size_t n) const {
27+
std::set<SignedMessage, decltype(compareGasFunctor)> score(
28+
messages_.begin(), messages_.end(), compareGasFunctor);
29+
return std::vector<SignedMessage>(
30+
score.begin(),
31+
(n < score.size()) ? std::next(score.begin(), n) : score.end());
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_GAS_PRICE_SCORED_MESSAGE_STORAGE_HPP
7+
#define CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_GAS_PRICE_SCORED_MESSAGE_STORAGE_HPP
8+
9+
#include "blockchain/message_pool/message_storage.hpp"
10+
11+
namespace fc::blockchain::message_pool {
12+
13+
using primitives::address::Address;
14+
using vm::message::SignedMessage;
15+
16+
/**
17+
* Compare messages by sender address and nonce
18+
*/
19+
auto compareMessagesFunctor = [](const SignedMessage &lhs,
20+
const SignedMessage &rhs) {
21+
return (lhs.message.from < rhs.message.from)
22+
|| ((lhs.message.from == rhs.message.from)
23+
&& (lhs.message.nonce < rhs.message.nonce));
24+
};
25+
26+
/**
27+
* Comparator based on gas price for scoring
28+
*/
29+
auto compareGasFunctor = [](const SignedMessage &lhs,
30+
const SignedMessage &rhs) {
31+
return (lhs.message.gasPrice > rhs.message.gasPrice)
32+
|| ((lhs.message.gasPrice == rhs.message.gasPrice)
33+
&& (compareMessagesFunctor(lhs, rhs)));
34+
};
35+
36+
/**
37+
* Caches pending messages and order by gas price.
38+
*/
39+
class GasPriceScoredMessageStorage : public MessageStorage {
40+
public:
41+
~GasPriceScoredMessageStorage() override = default;
42+
43+
/** \copydoc MessageStorage::put() */
44+
outcome::result<void> put(const SignedMessage &message) override;
45+
46+
/** \copydoc MessageStorage::remove() */
47+
void remove(const SignedMessage &message) override;
48+
49+
/** \copydoc MessageStorage::getTopScored() */
50+
std::vector<SignedMessage> getTopScored(size_t n) const override;
51+
52+
private:
53+
std::set<SignedMessage, decltype(compareMessagesFunctor)> messages_{
54+
compareMessagesFunctor};
55+
};
56+
57+
} // namespace fc::blockchain::message_pool
58+
59+
#endif // CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_GAS_PRICE_SCORED_MESSAGE_STORAGE_HPP
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "blockchain/message_pool/message_pool_error.hpp"
7+
8+
OUTCOME_CPP_DEFINE_CATEGORY(fc::blockchain::message_pool, MessagePoolError, e) {
9+
using fc::blockchain::message_pool::MessagePoolError;
10+
11+
switch (e) {
12+
case MessagePoolError::MESSAGE_ALREADY_IN_POOL:
13+
return "MessagePoolError: message is already in pool";
14+
}
15+
16+
return "unknown error";
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef FILECOIN_CORE_BLOCKCHAIN_MESSAGE_POOL_ERROR
7+
#define FILECOIN_CORE_BLOCKCHAIN_MESSAGE_POOL_ERROR
8+
9+
#include "common/outcome.hpp"
10+
11+
namespace fc::blockchain::message_pool {
12+
13+
/**
14+
* @brief Type of errors returned by MessagePool
15+
*/
16+
enum class MessagePoolError {
17+
MESSAGE_ALREADY_IN_POOL = 1,
18+
};
19+
20+
} // namespace fc::blockchain::message_pool
21+
22+
OUTCOME_HPP_DECLARE_ERROR(fc::blockchain::message_pool, MessagePoolError);
23+
24+
#endif // FILECOIN_CORE_BLOCKCHAIN_MESSAGE_POOL_ERROR
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_MESSAGE_STORAGE_HPP
7+
#define CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_MESSAGE_STORAGE_HPP
8+
9+
#include "common/outcome.hpp"
10+
#include "primitives/address/address.hpp"
11+
#include "vm/message/message.hpp"
12+
13+
namespace fc::blockchain::message_pool {
14+
15+
using primitives::address::Address;
16+
using vm::message::SignedMessage;
17+
18+
/**
19+
* Caches pending messages.
20+
*/
21+
class MessageStorage {
22+
public:
23+
virtual ~MessageStorage() = default;
24+
25+
/**
26+
* Add message
27+
* @param message - message to add
28+
* @return error code in case of error, or nothing otherwise
29+
*/
30+
virtual outcome::result<void> put(const SignedMessage &message) = 0;
31+
32+
/**
33+
* Remove message
34+
* @param message - message to remove
35+
* @return error code in case of error, or nothing otherwise
36+
*/
37+
virtual void remove(const SignedMessage &message) = 0;
38+
39+
/**
40+
* Get N top scored messages
41+
* @param n - how many messages return
42+
* @return no more than N top scored messages present in cache
43+
*/
44+
virtual std::vector<SignedMessage> getTopScored(size_t n) const = 0;
45+
};
46+
47+
} // namespace fc::blockchain::message_pool
48+
49+
#endif // CPP_FILECOIN_BLOCKCHAIN_MESSAGE_POOL_MESSAGE_STORAGE_HPP

test/core/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55

6+
add_subdirectory(blockchain)
67
add_subdirectory(clock)
8+
add_subdirectory(codec)
79
add_subdirectory(common)
810
add_subdirectory(crypto)
911
add_subdirectory(fslock)
1012
add_subdirectory(power)
1113
add_subdirectory(primitives)
1214
add_subdirectory(storage)
13-
add_subdirectory(codec)
1415
add_subdirectory(vm)

test/core/blockchain/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_subdirectory(message_pool)

0 commit comments

Comments
 (0)