generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 36
Feature/code loading #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
30b0bbe
VMExitCode
turuslan 71ff94e
refactor cron actor
turuslan 4aee351
invoker
turuslan 3fea027
decode actor params
turuslan c5744c0
clang-tidy
turuslan 2339405
error consts
turuslan 5d16692
replace with consts
turuslan 2f7d2f7
comments
turuslan 18b9732
encode actor params
turuslan 7cac3fe
address codec warning
turuslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP | ||
#define CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP | ||
|
||
#include "codec/cbor/cbor.hpp" | ||
#include "common/buffer.hpp" | ||
#include "common/outcome.hpp" | ||
#include "vm/actor/actor.hpp" | ||
#include "vm/exit_code/exit_code.hpp" | ||
#include "vm/vm_context.hpp" | ||
|
||
namespace fc::vm::actor { | ||
using common::Buffer; | ||
|
||
/// Actor method signature | ||
using ActorMethod = std::function<outcome::result<Buffer>( | ||
const Actor &, VMContext &, gsl::span<const uint8_t>)>; | ||
|
||
/// Actor methods exported by number | ||
using ActorExports = std::map<uint64_t, ActorMethod>; | ||
|
||
constexpr VMExitCode DECODE_ACTOR_PARAMS_ERROR{1}; | ||
|
||
/// Decode actor params, raises appropriate error | ||
template <typename T> | ||
outcome::result<T> decodeActorParams(gsl::span<const uint8_t> params_bytes) { | ||
auto maybe_params = codec::cbor::decode<T>(params_bytes); | ||
if (!maybe_params) { | ||
return DECODE_ACTOR_PARAMS_ERROR; | ||
} | ||
return maybe_params; | ||
} | ||
|
||
constexpr VMExitCode ENCODE_ACTOR_PARAMS_ERROR{1}; | ||
|
||
/// Encode actor params, raises appropriate error | ||
template <typename T> | ||
outcome::result<std::vector<uint8_t>> encodeActorParams(const T ¶ms) { | ||
auto maybe_bytes = codec::cbor::encode(params); | ||
if (!maybe_bytes) { | ||
return ENCODE_ACTOR_PARAMS_ERROR; | ||
} | ||
return maybe_bytes; | ||
} | ||
} // namespace fc::vm::actor | ||
|
||
#endif // CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "vm/actor/invoker.hpp" | ||
|
||
#include "vm/actor/cron_actor.hpp" | ||
|
||
namespace fc::vm::actor { | ||
Invoker::Invoker() { | ||
builtin_[actor::kCronCodeCid] = actor::CronActor::exports; | ||
} | ||
|
||
outcome::result<Buffer> Invoker::invoke(const Actor &actor, | ||
VMContext &context, | ||
uint64_t method, | ||
gsl::span<const uint8_t> params) { | ||
if (actor.code == actor::kAccountCodeCid) { | ||
return CANT_INVOKE_ACCOUNT_ACTOR; | ||
} | ||
auto maybe_builtin_actor = builtin_.find(actor.code); | ||
if (maybe_builtin_actor == builtin_.end()) { | ||
return NO_CODE_OR_METHOD; | ||
} | ||
auto builtin_actor = maybe_builtin_actor->second; | ||
auto maybe_builtin_method = builtin_actor.find(method); | ||
if (maybe_builtin_method == builtin_actor.end()) { | ||
return NO_CODE_OR_METHOD; | ||
} | ||
return maybe_builtin_method->second(actor, context, params); | ||
} | ||
} // namespace fc::vm::actor |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP | ||
#define CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP | ||
|
||
#include "vm/actor/actor_method.hpp" | ||
|
||
namespace fc::vm::actor { | ||
/// Finds and loads actor code, invokes actor methods | ||
class Invoker { | ||
public: | ||
static constexpr VMExitCode CANT_INVOKE_ACCOUNT_ACTOR{254}; | ||
static constexpr VMExitCode NO_CODE_OR_METHOD{255}; | ||
|
||
Invoker(); | ||
outcome::result<Buffer> invoke(const Actor &actor, | ||
VMContext &context, | ||
uint64_t method, | ||
gsl::span<const uint8_t> params); | ||
|
||
private: | ||
std::map<CID, ActorExports> builtin_; | ||
}; | ||
} // namespace fc::vm::actor | ||
|
||
#endif // CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ add_library(exit_code | |
impl/exit_code.cpp | ||
) | ||
target_link_libraries(exit_code | ||
outcome | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright Soramitsu Co., Ltd. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "vm/actor/invoker.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
#include "testutil/cbor.hpp" | ||
#include "testutil/outcome.hpp" | ||
#include "vm/actor/cron_actor.hpp" | ||
#include "vm_context_mock.hpp" | ||
|
||
using fc::vm::VMExitCode; | ||
|
||
/// invoker returns error or invokes actor method | ||
TEST(InvokerTest, InvokeCron) { | ||
using namespace fc::vm::actor; | ||
|
||
Invoker invoker; | ||
fc::vm::MockVMContext vmc; | ||
|
||
EXPECT_OUTCOME_ERROR(Invoker::CANT_INVOKE_ACCOUNT_ACTOR, invoker.invoke({kAccountCodeCid}, vmc, 0, {})); | ||
EXPECT_OUTCOME_ERROR(Invoker::NO_CODE_OR_METHOD, invoker.invoke({kEmptyObjectCid}, vmc, 0, {})); | ||
EXPECT_OUTCOME_ERROR(Invoker::NO_CODE_OR_METHOD, invoker.invoke({kCronCodeCid}, vmc, 1000, {})); | ||
|
||
EXPECT_CALL(vmc, message()) | ||
.WillRepeatedly(testing::Return(fc::vm::Message{kInitAddress})); | ||
EXPECT_OUTCOME_ERROR(CronActor::WRONG_CALL, invoker.invoke({kCronCodeCid}, vmc, 2, {})); | ||
} | ||
|
||
/// decodeActorParams returns error or decoded params | ||
TEST(InvokerTest, DecodeActorParams) { | ||
using fc::vm::actor::decodeActorParams; | ||
|
||
// 80 is cbor empty list, not int | ||
EXPECT_OUTCOME_ERROR(fc::vm::actor::DECODE_ACTOR_PARAMS_ERROR, decodeActorParams<int>("80"_unhex)); | ||
EXPECT_OUTCOME_EQ(decodeActorParams<int>("03"_unhex), 3); | ||
} | ||
|
||
/// encodeActorParams returns error or encoded params | ||
TEST(InvokerTest, EncodeActorParams) { | ||
using fc::vm::actor::encodeActorParams; | ||
|
||
EXPECT_OUTCOME_ERROR(fc::vm::actor::ENCODE_ACTOR_PARAMS_ERROR, encodeActorParams(fc::common::kEmptyCid)); | ||
EXPECT_OUTCOME_EQ(encodeActorParams(3), "03"_unhex); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.