Skip to content

Commit c1fe4e5

Browse files
introduce vm ExitCode (#50)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent e1235a2 commit c1fe4e5

File tree

8 files changed

+403
-0
lines changed

8 files changed

+403
-0
lines changed

core/common/enum.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_COMMON_ENUM_HPP
7+
#define CPP_FILECOIN_CORE_COMMON_ENUM_HPP
8+
9+
namespace fc::common {
10+
11+
/**
12+
* @brief Convert enum class value as integer
13+
* Usage:
14+
* Given an enum class
15+
* @code
16+
* enum class Enumerator {
17+
* one = 1,
18+
* eleven = 11,
19+
* hundred = 100
20+
* }
21+
* @nocode
22+
* Can be converted as:
23+
* @code
24+
* int v = to_int(Enumerator::eleven); // v == 11
25+
* @nocode
26+
*
27+
* @tparam Enumeration - enum type
28+
* @param value - to convert
29+
* @return integer value of enum
30+
*/
31+
template <typename Enumeration>
32+
auto to_int(Enumeration const value) ->
33+
typename std::underlying_type<Enumeration>::type {
34+
return static_cast<typename std::underlying_type<Enumeration>::type>(value);
35+
}
36+
37+
} // namespace fc::common
38+
39+
#endif // CPP_FILECOIN_CORE_COMMON_ENUM_HPP

core/vm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
#
55

66
add_subdirectory(actor)
7+
add_subdirectory(exit_code)

core/vm/exit_code/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(exit_code
7+
impl/exit_code.cpp
8+
)
9+
target_link_libraries(exit_code
10+
)

core/vm/exit_code/exit_code.hpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_VM_EXITCODE_EXITCODE_HPP
7+
#define CPP_FILECOIN_CORE_VM_EXITCODE_EXITCODE_HPP
8+
9+
#include <string>
10+
11+
namespace fc::vm::exit_code {
12+
13+
/**
14+
* @brief System error codes
15+
*/
16+
enum class ErrorCode {
17+
18+
/**
19+
* Successful exit code
20+
*/
21+
kSuccess = 0,
22+
23+
// System error codes
24+
25+
/**
26+
* Represents a failure to find an actor
27+
*/
28+
kActorNotFound,
29+
30+
/**
31+
* Represents a failure to find the code for a particular actor in the VM
32+
* registry.
33+
*/
34+
kActorCodeNotFound,
35+
36+
/**
37+
* Represents a failure to find a method in an actor
38+
*/
39+
kInvalidMethod,
40+
41+
/**
42+
* Indicates that a method was called with the incorrect number of
43+
* arguments, or that its arguments did not satisfy its preconditions
44+
*/
45+
kInvalidArgumentsSystem,
46+
47+
/**
48+
* Represents a failure to apply a message, as it did not carry sufficient
49+
*/
50+
kInsufficientFundsSystem,
51+
52+
/**
53+
* Represents a message invocation out of sequence. This happens when
54+
* message.CallSeqNum is not exactly actor.CallSeqNum + 1
55+
*/
56+
kInvalidCallSeqNum,
57+
58+
/**
59+
* Returned when the execution of an actor method (including its subcalls)
60+
* uses more gas than initially allocated
61+
*/
62+
kOutOfGas,
63+
64+
/**
65+
* Returned when an actor method invocation makes a call to the runtime that
66+
* does not satisfy its preconditions
67+
*/
68+
kRuntimeAPIError,
69+
70+
/**
71+
* Returned when an actor method invocation calls rt.Assert with a false
72+
* condition
73+
*/
74+
kRuntimeAssertFailure,
75+
76+
/**
77+
* Returned when an actor method's Send call has returned with a failure
78+
* error code (and the Send call did not specify to ignore errors)
79+
*/
80+
kMethodSubcallError,
81+
82+
// User defined error codes
83+
84+
kInsufficientFundsUser,
85+
kInvalidArgumentsUser,
86+
kInconsistentStateUser,
87+
88+
kInvalidSectorPacking,
89+
kSealVerificationFailed,
90+
kPoStVerificationFailed,
91+
kDeadlineExceeded,
92+
kInsufficientPledgeCollateral,
93+
};
94+
95+
/**
96+
* @brief Virtual machine exit code
97+
*/
98+
class ExitCode {
99+
public:
100+
/**
101+
* @brief Create exit code
102+
* @param exit_code - code to set
103+
*/
104+
explicit ExitCode(ErrorCode error_code);
105+
106+
/**
107+
* @brief Create successful exit code
108+
* @return Successful exit code
109+
*/
110+
static ExitCode makeOkExitCode();
111+
112+
/**
113+
* @brief Create error exit code
114+
* @param exit_code - code to set
115+
*/
116+
static ExitCode makeErrorExitCode(ErrorCode error_code);
117+
118+
/**
119+
* @brief ensure if exit code is error code
120+
* @param exit_code - to check
121+
* @return exit_code if error code is present or ExitCode with
122+
* SystemError::kRuntimeAPIError set otherwise
123+
*/
124+
static ExitCode ensureErrorCode(const ExitCode &exit_code);
125+
126+
/**
127+
* @brief Check if exit code is Success
128+
* @return true if no error
129+
*/
130+
bool isSuccess() const;
131+
132+
/**
133+
* @brief Check if exit code is System or User defined error
134+
* @return true if error
135+
*/
136+
bool isError() const;
137+
138+
/**
139+
* @brief Check if state update is allowed
140+
* @return true if is successful
141+
*/
142+
bool allowsStateUpdate() const;
143+
144+
std::string toString() const;
145+
146+
bool operator==(const ExitCode &other) const;
147+
148+
private:
149+
ErrorCode exit_code_;
150+
};
151+
152+
/**
153+
* @brief Runtime error with text message
154+
*/
155+
class RuntimeError {
156+
public:
157+
RuntimeError(ExitCode exit_code, std::string error_message);
158+
explicit RuntimeError(ExitCode exit_code);
159+
160+
std::string toString() const;
161+
162+
private:
163+
ExitCode exit_code_;
164+
std::string error_message_;
165+
};
166+
167+
} // namespace fc::vm::exit_code
168+
169+
#endif // CPP_FILECOIN_CORE_VM_EXITCODE_EXITCODE_HPP

core/vm/exit_code/impl/exit_code.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "vm/exit_code/exit_code.hpp"
7+
8+
#include <sstream>
9+
10+
#include "common/enum.hpp"
11+
12+
using fc::common::to_int;
13+
using fc::vm::exit_code::ErrorCode;
14+
using fc::vm::exit_code::ExitCode;
15+
using fc::vm::exit_code::RuntimeError;
16+
17+
ExitCode::ExitCode(ErrorCode error_code) : exit_code_{error_code} {}
18+
19+
ExitCode ExitCode::makeOkExitCode() {
20+
return ExitCode(ErrorCode::kSuccess);
21+
}
22+
23+
ExitCode ExitCode::makeErrorExitCode(ErrorCode error_code) {
24+
return ExitCode(error_code);
25+
}
26+
27+
ExitCode ExitCode::ensureErrorCode(const ExitCode &exit_code) {
28+
if (exit_code.isSuccess()) return ExitCode(ErrorCode::kRuntimeAPIError);
29+
return exit_code;
30+
}
31+
32+
bool ExitCode::isSuccess() const {
33+
return exit_code_ == ErrorCode::kSuccess;
34+
}
35+
36+
bool ExitCode::isError() const {
37+
return !isSuccess();
38+
}
39+
40+
bool ExitCode::allowsStateUpdate() const {
41+
return isSuccess();
42+
}
43+
44+
std::string ExitCode::toString() const {
45+
if (exit_code_ == ErrorCode::kSuccess) return "Success";
46+
return "ErrorCode " + std::to_string(to_int(exit_code_));
47+
}
48+
49+
bool ExitCode::operator==(const ExitCode &other) const {
50+
return exit_code_ == other.exit_code_;
51+
}
52+
53+
RuntimeError::RuntimeError(ExitCode exit_code, std::string error_message)
54+
: exit_code_(exit_code), error_message_(std::move(error_message)) {}
55+
56+
RuntimeError::RuntimeError(ExitCode exit_code) : exit_code_(exit_code) {}
57+
58+
std::string RuntimeError::toString() const {
59+
std::stringstream res;
60+
res << "Runtime error: '" << exit_code_.toString();
61+
if (!error_message_.empty()) res << " (\"" << error_message_ << "\")";
62+
return res.str();
63+
}

test/core/vm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
#
55

66
add_subdirectory(actor)
7+
add_subdirectory(exit_code)

test/core/vm/exit_code/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
addtest(exit_code_test
7+
exit_code_test.cpp
8+
)
9+
target_link_libraries(exit_code_test
10+
exit_code
11+
)

0 commit comments

Comments
 (0)