Skip to content

Commit 57e46ce

Browse files
authored
[FIL-78] CronActor (#57)
* Cron Actor implementation Signed-off-by: artyom-yurin <[email protected]>
1 parent e275235 commit 57e46ce

12 files changed

+254
-0
lines changed

core/vm/actor/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
add_library(actor
77
actor.cpp
88
init_actor.cpp
9+
cron_actor.cpp
10+
cron_actor_error.cpp
911
)
1012
target_link_libraries(actor
1113
address

core/vm/actor/actor.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ namespace fc::vm::actor {
5555
kInitCodeCid, kPaymentChannelCodeCid;
5656

5757
inline static const auto kInitAddress = Address::makeFromId(0);
58+
inline static const auto kStoragePowerAddress = Address::makeFromId(2);
59+
inline static const auto kCronAddress = Address::makeFromId(4);
5860
} // namespace fc::vm::actor
5961

6062
#endif // CPP_FILECOIN_CORE_VM_ACTOR_ACTOR_HPP

core/vm/actor/cron_actor.cpp

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+
#include "vm/actor/cron_actor.hpp"
7+
#include "vm/actor/cron_actor_error.hpp"
8+
9+
std::vector<fc::vm::actor::CronTableEntry> fc::vm::actor::CronActor::entries = {
10+
{kStoragePowerAddress, SpaMethods::CHECK_PROOF_SUBMISSIONS}};
11+
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+
}
19+
20+
for (const auto &entry : entries) {
21+
OUTCOME_TRY(vmctx.send(entry.to_addr, entry.method_num, BigInt(0), {}));
22+
}
23+
return outcome::success();
24+
}

core/vm/actor/cron_actor.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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_CRON_ACTOR_HPP
7+
#define CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_HPP
8+
9+
#include "vm/actor/actor.hpp"
10+
#include "vm/actor/storage_power_actor.hpp"
11+
#include "vm/vm_context.hpp"
12+
13+
namespace fc::vm::actor {
14+
15+
struct CronTableEntry {
16+
Address to_addr;
17+
uint64_t method_num;
18+
};
19+
20+
struct CronActor {
21+
/**
22+
* Entries is a set of actors (and corresponding methods) to call during
23+
* EpochTick
24+
*/
25+
static std::vector<CronTableEntry> entries;
26+
27+
/**
28+
* @brief EpochTick executes built-in periodic actions, run at every Epoch.
29+
* @param actor from Lotus(doesn't use)
30+
* @param vmctx is virtual machine context
31+
* @param params from Lotus(doesn't use)
32+
* @return success or error
33+
*/
34+
static outcome::result<void> EpochTick(Actor &actor,
35+
vm::VMContext &vmctx,
36+
const std::vector<uint8_t> &params);
37+
};
38+
39+
} // namespace fc::vm::actor
40+
41+
#endif // CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_HPP

core/vm/actor/cron_actor_error.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "vm/actor/cron_actor_error.hpp"
7+
8+
OUTCOME_CPP_DEFINE_CATEGORY(fc::vm::actor, CronActorError, e) {
9+
using fc::vm::actor::CronActorError;
10+
11+
switch (e) {
12+
case (CronActorError::WRONG_CALL):
13+
return "CronActor: EpochTick is only callable as a part of tipset state "
14+
"computation";
15+
case (CronActorError::UNKNOWN):
16+
return "CronActor: unknown error";
17+
}
18+
}

core/vm/actor/cron_actor_error.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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_CRON_ACTOR_ERROR_HPP
7+
#define CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_ERROR_HPP
8+
9+
#include "common/outcome.hpp"
10+
11+
namespace fc::vm::actor {
12+
13+
/**
14+
* @brief CronActor returns these types of errors
15+
*/
16+
enum class CronActorError {
17+
WRONG_CALL = 1,
18+
19+
UNKNOWN = 1000
20+
};
21+
22+
} // namespace fc::vm::actor
23+
24+
OUTCOME_HPP_DECLARE_ERROR(fc::vm::actor, CronActorError);
25+
26+
#endif // CPP_FILECOIN_CORE_VM_ACTOR_CRON_ACTOR_ERROR_HPP

core/vm/actor/storage_power_actor.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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_STORAGE_POWER_ACTOR_HPP
7+
#define CPP_FILECOIN_CORE_VM_ACTOR_STORAGE_POWER_ACTOR_HPP
8+
9+
namespace fc::vm::actor {
10+
11+
enum SpaMethods {
12+
CONSTRUCTOR = 1,
13+
CREATE_STORAGE_MINER,
14+
ARBITRATE_CONSENSUS_FAULT,
15+
UPDATE_STORAGE,
16+
GET_TOTAL_STORAGE,
17+
POWER_LOOKUP,
18+
IS_VALID_MINER,
19+
PLEDGE_COLLATERAL_FOR_SIZE,
20+
CHECK_PROOF_SUBMISSIONS
21+
};
22+
23+
} // namespace fc::vm::actor
24+
25+
#endif // CPP_FILECOIN_CORE_VM_ACTOR_STORAGE_POWER_ACTOR_HPP

core/vm/vm_context.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_VM_CONTEXT_HPP
7+
#define CPP_FILECOIN_CORE_VM_CONTEXT_HPP
8+
9+
namespace fc::vm {
10+
11+
/**
12+
* TODO(artyom-yurin): it is temp structure, will be removed after "runtime"
13+
* would be implemented
14+
*/
15+
struct Message {
16+
primitives::address::Address from;
17+
};
18+
19+
class VMContext {
20+
public:
21+
virtual ~VMContext() = default;
22+
23+
virtual outcome::result<void> send(primitives::address::Address to,
24+
uint64_t method,
25+
actor::BigInt value,
26+
std::vector<uint8_t> params) = 0;
27+
virtual Message message() = 0;
28+
};
29+
} // namespace fc::vm
30+
31+
#endif // CPP_FILECOIN_CORE_VM_CONTEXT_HPP

test/core/vm/CMakeLists.txt

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

6+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
7+
${PROJECT_SOURCE_DIR}/core
8+
)
9+
610
add_subdirectory(actor)
711
add_subdirectory(exit_code)
812
add_subdirectory(state)

test/core/vm/actor/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ target_link_libraries(init_actor_test
1919
hexutil
2020
ipfs_datastore_in_memory
2121
)
22+
23+
addtest(cron_actor_test
24+
cron_actor_test.cpp
25+
)
26+
target_link_libraries(cron_actor_test
27+
actor
28+
hexutil
29+
)

0 commit comments

Comments
 (0)