Skip to content

Commit e8b69b6

Browse files
authored
Сode loading (#63)
Signed-off-by: turuslan <[email protected]>
1 parent e46d904 commit e8b69b6

17 files changed

+240
-69
lines changed

core/primitives/address/address_codec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ namespace fc::primitives::address {
181181

182182
OUTCOME_TRY(address, decode(buffer));
183183
address.network = net;
184-
return address;
184+
return std::move(address);
185185
}
186186

187187
std::vector<uint8_t> checksum(const Address &address) {

core/vm/actor/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ add_library(actor
77
actor.cpp
88
init_actor.cpp
99
cron_actor.cpp
10-
cron_actor_error.cpp
10+
invoker.cpp
1111
)
1212
target_link_libraries(actor
1313
address
1414
cid
15+
exit_code
1516
hamt
1617
)

core/vm/actor/actor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace fc::vm::actor {
2222
ContentIdentifier code{common::kEmptyCid};
2323
ContentIdentifier head{common::kEmptyCid};
2424
uint64_t nonce{};
25-
BigInt balance;
25+
BigInt balance{};
2626
};
2727

2828
bool operator==(const Actor &lhs, const Actor &rhs);

core/vm/actor/actor_method.hpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP
7+
#define CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP
8+
9+
#include "codec/cbor/cbor.hpp"
10+
#include "common/buffer.hpp"
11+
#include "common/outcome.hpp"
12+
#include "vm/actor/actor.hpp"
13+
#include "vm/exit_code/exit_code.hpp"
14+
#include "vm/vm_context.hpp"
15+
16+
namespace fc::vm::actor {
17+
using common::Buffer;
18+
19+
/// Actor method signature
20+
using ActorMethod = std::function<outcome::result<Buffer>(
21+
const Actor &, VMContext &, gsl::span<const uint8_t>)>;
22+
23+
/// Actor methods exported by number
24+
using ActorExports = std::map<uint64_t, ActorMethod>;
25+
26+
constexpr VMExitCode DECODE_ACTOR_PARAMS_ERROR{1};
27+
28+
/// Decode actor params, raises appropriate error
29+
template <typename T>
30+
outcome::result<T> decodeActorParams(gsl::span<const uint8_t> params_bytes) {
31+
auto maybe_params = codec::cbor::decode<T>(params_bytes);
32+
if (!maybe_params) {
33+
return DECODE_ACTOR_PARAMS_ERROR;
34+
}
35+
return maybe_params;
36+
}
37+
38+
constexpr VMExitCode ENCODE_ACTOR_PARAMS_ERROR{1};
39+
40+
/// Encode actor params, raises appropriate error
41+
template <typename T>
42+
outcome::result<std::vector<uint8_t>> encodeActorParams(const T &params) {
43+
auto maybe_bytes = codec::cbor::encode(params);
44+
if (!maybe_bytes) {
45+
return ENCODE_ACTOR_PARAMS_ERROR;
46+
}
47+
return maybe_bytes;
48+
}
49+
} // namespace fc::vm::actor
50+
51+
#endif // CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_METHOD_HPP

core/vm/actor/cron_actor.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44
*/
55

66
#include "vm/actor/cron_actor.hpp"
7-
#include "vm/actor/cron_actor_error.hpp"
87

9-
std::vector<fc::vm::actor::CronTableEntry> fc::vm::actor::CronActor::entries = {
10-
{kStoragePowerAddress, SpaMethods::CHECK_PROOF_SUBMISSIONS}};
8+
namespace fc::vm::actor {
9+
std::vector<CronTableEntry> CronActor::entries = {
10+
{kStoragePowerAddress, SpaMethods::CHECK_PROOF_SUBMISSIONS}};
1111

12-
fc::outcome::result<void> fc::vm::actor::CronActor::epochTick(
13-
fc::vm::actor::Actor &actor,
14-
fc::vm::VMContext &vmctx,
15-
const std::vector<uint8_t> &params) {
16-
if (!(vmctx.message().from == kCronAddress)) {
17-
return CronActorError::WRONG_CALL;
18-
}
12+
outcome::result<Buffer> CronActor::epochTick(
13+
const Actor &actor,
14+
vm::VMContext &vmctx,
15+
gsl::span<const uint8_t> params) {
16+
if (!(vmctx.message().from == kCronAddress)) {
17+
return WRONG_CALL;
18+
}
1919

20-
for (const auto &entry : entries) {
21-
OUTCOME_TRY(vmctx.send(entry.to_addr, entry.method_num, BigInt(0), {}));
20+
for (const auto &entry : entries) {
21+
OUTCOME_TRY(vmctx.send(entry.to_addr, entry.method_num, BigInt(0), {}));
22+
}
23+
return outcome::success();
2224
}
23-
return outcome::success();
24-
}
25+
26+
ActorExports CronActor::exports = {
27+
{2, ActorMethod(CronActor::epochTick)},
28+
};
29+
} // namespace fc::vm::actor

core/vm/actor/cron_actor.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
#ifndef CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_HPP
77
#define CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_HPP
88

9-
#include "vm/actor/actor.hpp"
9+
#include "vm/actor/actor_method.hpp"
1010
#include "vm/actor/storage_power_actor.hpp"
11-
#include "vm/vm_context.hpp"
1211

1312
namespace fc::vm::actor {
1413

1514
struct CronTableEntry {
1615
Address to_addr;
17-
uint64_t method_num;
16+
uint64_t method_num{};
1817
};
1918

2019
struct CronActor {
20+
static constexpr VMExitCode WRONG_CALL{1};
21+
2122
/**
2223
* Entries is a set of actors (and corresponding methods) to call during
2324
* EpochTick
@@ -31,9 +32,11 @@ namespace fc::vm::actor {
3132
* @param params from Lotus(doesn't use)
3233
* @return success or error
3334
*/
34-
static outcome::result<void> epochTick(Actor &actor,
35-
vm::VMContext &vmctx,
36-
const std::vector<uint8_t> &params);
35+
static outcome::result<Buffer> epochTick(const Actor &actor,
36+
VMContext &vmctx,
37+
gsl::span<const uint8_t> params);
38+
39+
static ActorExports exports;
3740
};
3841

3942
} // namespace fc::vm::actor

core/vm/actor/cron_actor_error.cpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

core/vm/actor/cron_actor_error.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

core/vm/actor/invoker.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "vm/actor/invoker.hpp"
7+
8+
#include "vm/actor/cron_actor.hpp"
9+
10+
namespace fc::vm::actor {
11+
Invoker::Invoker() {
12+
builtin_[actor::kCronCodeCid] = actor::CronActor::exports;
13+
}
14+
15+
outcome::result<Buffer> Invoker::invoke(const Actor &actor,
16+
VMContext &context,
17+
uint64_t method,
18+
gsl::span<const uint8_t> params) {
19+
if (actor.code == actor::kAccountCodeCid) {
20+
return CANT_INVOKE_ACCOUNT_ACTOR;
21+
}
22+
auto maybe_builtin_actor = builtin_.find(actor.code);
23+
if (maybe_builtin_actor == builtin_.end()) {
24+
return NO_CODE_OR_METHOD;
25+
}
26+
auto builtin_actor = maybe_builtin_actor->second;
27+
auto maybe_builtin_method = builtin_actor.find(method);
28+
if (maybe_builtin_method == builtin_actor.end()) {
29+
return NO_CODE_OR_METHOD;
30+
}
31+
return maybe_builtin_method->second(actor, context, params);
32+
}
33+
} // namespace fc::vm::actor

core/vm/actor/invoker.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP
7+
#define CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP
8+
9+
#include "vm/actor/actor_method.hpp"
10+
11+
namespace fc::vm::actor {
12+
/// Finds and loads actor code, invokes actor methods
13+
class Invoker {
14+
public:
15+
static constexpr VMExitCode CANT_INVOKE_ACCOUNT_ACTOR{254};
16+
static constexpr VMExitCode NO_CODE_OR_METHOD{255};
17+
18+
Invoker();
19+
outcome::result<Buffer> invoke(const Actor &actor,
20+
VMContext &context,
21+
uint64_t method,
22+
gsl::span<const uint8_t> params);
23+
24+
private:
25+
std::map<CID, ActorExports> builtin_;
26+
};
27+
} // namespace fc::vm::actor
28+
29+
#endif // CPP_FILECOIN_CORE_VM_ACTOR_INVOKER_HPP

0 commit comments

Comments
 (0)