Skip to content

Commit 5d78d0c

Browse files
ekovalevkamilsa
authored andcommitted
Use Message with strong types (#74)
Signed-off-by: Eugene Kovalev <[email protected]>
1 parent 060bfeb commit 5d78d0c

File tree

5 files changed

+43
-13
lines changed

5 files changed

+43
-13
lines changed

core/common/buffer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ namespace fc::common {
224224
* @return reference to stream
225225
*/
226226
template <class Stream,
227-
typename = std::enable_if_t<Stream::is_encoder_stream>>
227+
typename = std::enable_if_t<Stream::is_cbor_encoder_stream>>
228228
Stream &operator<<(Stream &s, const Buffer &buffer) {
229229
return s << buffer.toVector();
230230
}
@@ -237,7 +237,7 @@ namespace fc::common {
237237
* @return reference to stream
238238
*/
239239
template <class Stream,
240-
typename = std::enable_if_t<Stream::is_decoder_stream>>
240+
typename = std::enable_if_t<Stream::is_cbor_decoder_stream>>
241241
Stream &operator>>(Stream &s, Buffer &buffer) {
242242
std::vector<uint8_t> data;
243243
s >> data;

core/vm/actor/actor.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,37 @@ namespace fc::vm::actor {
2828
*/
2929
struct MethodNumber {
3030
uint64_t method_number;
31+
32+
inline bool operator==(const MethodNumber &other) const {
33+
return method_number == other.method_number;
34+
}
3135
};
3236

37+
template <class Stream,
38+
typename = std::enable_if_t<
39+
std::remove_reference<Stream>::type::is_cbor_encoder_stream>>
40+
Stream &operator<<(Stream &&s, const MethodNumber &method) noexcept {
41+
return s << method.method_number;
42+
}
43+
44+
template <class Stream,
45+
typename = std::enable_if_t<
46+
std::remove_reference<Stream>::type::is_cbor_decoder_stream>>
47+
Stream &operator>>(Stream &&s, MethodNumber &method) noexcept {
48+
return s >> method.method_number;
49+
}
50+
3351
/**
3452
* MethodParams is serialized parameters to the method call
3553
*/
36-
class MethodParams : public Buffer {};
54+
class MethodParams : public Buffer {
55+
using Buffer::Buffer;
56+
57+
public:
58+
inline bool operator==(const MethodParams &other) const {
59+
return Buffer::operator==(static_cast<const Buffer &>(other));
60+
}
61+
};
3762

3863
/**
3964
* CodeID identifies an actor's code (either one of the builtin actors, or, in

core/vm/message/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ add_library(message
1212
target_link_libraries(message
1313
Boost::boost
1414
address
15+
buffer
1516
logger
1617
keystore
1718
outcome

core/vm/message/message.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "crypto/signature/signature.hpp"
1313
#include "primitives/address/address.hpp"
1414
#include "primitives/big_int.hpp"
15+
#include "vm/actor/actor.hpp"
1516

1617
namespace fc::vm::message {
1718

@@ -24,6 +25,8 @@ namespace fc::vm::message {
2425
VERIFICATION_FAILURE
2526
};
2627

28+
using actor::MethodNumber;
29+
using actor::MethodParams;
2730
using crypto::signature::Signature;
2831
using primitives::BigInt;
2932
using primitives::address::Address;
@@ -42,8 +45,8 @@ namespace fc::vm::message {
4245
BigInt gasPrice;
4346
BigInt gasLimit;
4447

45-
uint64_t method;
46-
std::vector<uint8_t> params;
48+
MethodNumber method;
49+
MethodParams params;
4750

4851
/**
4952
* @brief Message equality operator

test/core/vm/message/message_test.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ using fc::vm::message::cid;
4545
using fc::vm::message::MessageError;
4646
using fc::vm::message::MessageSigner;
4747
using fc::vm::message::MessageSignerImpl;
48+
using fc::vm::message::MethodParams;
4849
using fc::vm::message::SignedMessage;
4950
using fc::vm::message::UnsignedMessage;
5051

@@ -59,14 +60,14 @@ UnsignedMessage makeMessage(Address const &from,
5960
Address const &to,
6061
uint64_t nonce) {
6162
return UnsignedMessage{
62-
to, // to Address
63-
from, // from Address
64-
nonce, // nonce
65-
BigInt(1), // transfer value
66-
BigInt(0), // gasPrice
67-
BigInt(1), // gasLimit
68-
0, // method num
69-
""_unhex // method params
63+
to, // to Address
64+
from, // from Address
65+
nonce, // nonce
66+
BigInt(1), // transfer value
67+
BigInt(0), // gasPrice
68+
BigInt(1), // gasLimit
69+
0, // method num
70+
MethodParams{""_unhex} // method params
7071
};
7172
}
7273

0 commit comments

Comments
 (0)