Skip to content

Commit f371b9c

Browse files
authored
Small refactoring 2 (#157)
Signed-off-by: turuslan <[email protected]>
1 parent 416cbc0 commit f371b9c

25 files changed

+277
-292
lines changed

core/api/make.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ namespace fc::api {
160160
result.message = message;
161161
auto maybe_result = env.applyImplicitMessage(message);
162162
if (maybe_result) {
163-
result.receipt = {
164-
VMExitCode::Ok, maybe_result.value().return_value, 0};
163+
result.receipt = {VMExitCode::Ok, maybe_result.value(), 0};
165164
} else {
166165
if (isVMExitCode(maybe_result.error())) {
167166
auto ret_code =

core/codec/cbor/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(cbor
1010
cbor_resolve.cpp
1111
)
1212
target_link_libraries(cbor
13+
buffer
1314
cid
1415
outcome
1516
tinycbor

core/codec/cbor/cbor.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@
99
#include "codec/cbor/cbor_decode_stream.hpp"
1010
#include "codec/cbor/cbor_encode_stream.hpp"
1111
#include "codec/cbor/cbor_resolve.hpp"
12+
#include "common/buffer.hpp"
1213

1314
namespace fc::codec::cbor {
15+
using common::Buffer;
16+
1417
/**
1518
* @brief CBOR encoding to byte-vector
1619
* @tparam Type to be encoded
1720
* @param arg data to be encoded
1821
* @return encoded data
1922
*/
2023
template <typename T>
21-
outcome::result<std::vector<uint8_t>> encode(const T &arg) {
24+
outcome::result<Buffer> encode(const T &arg) {
2225
try {
2326
CborEncodeStream encoder;
2427
encoder << arg;
25-
return encoder.data();
28+
return Buffer{encoder.data()};
2629
} catch (std::system_error &e) {
2730
return outcome::failure(e.code());
2831
}

core/storage/chain/impl/chain_store_impl.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ namespace fc::storage::blockchain {
116116
for (auto &b : block_headers) {
117117
OUTCOME_TRY(data, codec::cbor::encode(b));
118118
OUTCOME_TRY(cid, common::getCidOf(data));
119-
OUTCOME_TRY(
120-
block_service_->set(std::move(cid), common::Buffer{std::move(data)}));
119+
OUTCOME_TRY(block_service_->set(std::move(cid), std::move(data)));
121120
}
122121

123122
return outcome::success();

core/storage/ipld/ipld_block.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace fc::storage::ipld {
7676
static common::Buffer serialize(const T &entity) {
7777
auto data = codec::cbor::encode(entity);
7878
BOOST_ASSERT(data.has_value());
79-
return common::Buffer{std::move(data.value())};
79+
return std::move(data.value());
8080
}
8181
};
8282
} // namespace fc::storage::ipld

core/vm/actor/actor_encoding.hpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ namespace fc::vm::actor {
5252
if constexpr (std::is_same_v<T, None>) {
5353
return T{};
5454
}
55-
return codec::cbor::decode<T>(result.return_value);
55+
return codec::cbor::decode<T>(result);
5656
}
5757

5858
template <typename T>
5959
outcome::result<InvocationOutput> encodeActorReturn(const T &result) {
60-
OUTCOME_TRY(encoded, codec::cbor::encode(result));
61-
return InvocationOutput{Buffer{encoded}};
60+
auto maybe_encoded = codec::cbor::encode(result);
61+
if (!maybe_encoded) {
62+
return VMExitCode::ENCODE_ACTOR_RESULT_ERROR;
63+
}
64+
return std::move(maybe_encoded.value());
6265
}
6366
} // namespace fc::vm::actor
6467

core/vm/actor/builtin/account/account_actor.cpp

+6-27
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,13 @@
88
#include "vm/exit_code/exit_code.hpp"
99

1010
namespace fc::vm::actor::builtin::account {
11-
12-
outcome::result<Actor> AccountActor::create(
13-
const std::shared_ptr<StateTree> &state_tree, const Address &address) {
14-
if (!address.isKeyType()) {
11+
ACTOR_METHOD_IMPL(Construct) {
12+
OUTCOME_TRY(runtime.validateImmediateCallerIs(kSystemActorAddress));
13+
if (!params.isKeyType()) {
1514
return VMExitCode::ACCOUNT_ACTOR_CREATE_WRONG_ADDRESS_TYPE;
1615
}
17-
OUTCOME_TRY(state,
18-
state_tree->getStore()->setCbor(AccountActorState{address}));
19-
Actor actor{kAccountCodeCid, ActorSubstateCID{state}, 0, 0};
20-
OUTCOME_TRY(state_tree->registerNewAddress(address, actor));
21-
return actor;
22-
}
23-
24-
outcome::result<Address> AccountActor::resolveToKeyAddress(
25-
const std::shared_ptr<StateTree> &state_tree, const Address &address) {
26-
if (address.isKeyType()) {
27-
return address;
28-
}
29-
auto maybe_actor = state_tree->get(address);
30-
if (!maybe_actor) {
31-
return VMExitCode::ACCOUNT_ACTOR_RESOLVE_NOT_FOUND;
32-
}
33-
auto actor = maybe_actor.value();
34-
if (actor.code != kAccountCodeCid) {
35-
return VMExitCode::ACCOUNT_ACTOR_RESOLVE_NOT_ACCOUNT_ACTOR;
36-
}
37-
OUTCOME_TRY(account_actor_state,
38-
state_tree->getStore()->getCbor<AccountActorState>(actor.head));
39-
return account_actor_state.address;
16+
OUTCOME_TRY(runtime.commitState(AccountActorState{params}));
17+
return outcome::success();
4018
}
4119

4220
ACTOR_METHOD_IMPL(PubkeyAddress) {
@@ -45,6 +23,7 @@ namespace fc::vm::actor::builtin::account {
4523
}
4624

4725
const ActorExports exports{
26+
exportMethod<Construct>(),
4827
exportMethod<PubkeyAddress>(),
4928
};
5029
} // namespace fc::vm::actor::builtin::account

core/vm/actor/builtin/account/account_actor.hpp

+3-13
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,9 @@ namespace fc::vm::actor::builtin::account {
2222
};
2323
CBOR_TUPLE(AccountActorState, address)
2424

25-
/// Account actors represent actors without code
26-
struct AccountActor {
27-
/// Create account actor from BLS or Secp256k1 address
28-
static outcome::result<Actor> create(
29-
const std::shared_ptr<StateTree> &state_tree, const Address &address);
30-
/**
31-
* Get BLS address of account actor from ID address
32-
* @param state_tree state tree
33-
* @param address id address to be resolved to key address
34-
* @returns key address associated with id address
35-
*/
36-
static outcome::result<Address> resolveToKeyAddress(
37-
const std::shared_ptr<StateTree> &state_tree, const Address &address);
25+
struct Construct : ActorMethodBase<1> {
26+
using Params = Address;
27+
ACTOR_METHOD_DECL();
3828
};
3929

4030
struct PubkeyAddress : ActorMethodBase<2> {

core/vm/actor/builtin/miner/miner_actor.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace fc::vm::actor::builtin::miner {
9292
kStoragePowerAddress,
9393
{
9494
.event_epoch = event_epoch,
95-
.payload = Buffer{payload2},
95+
.payload = payload2,
9696
},
9797
0));
9898
return outcome::success();
@@ -148,7 +148,7 @@ namespace fc::vm::actor::builtin::miner {
148148
.randomness = runtime.getRandomness(
149149
DomainSeparationTag::PoStDST,
150150
state.post_state.proving_period_start,
151-
Buffer{seed}),
151+
seed),
152152
.candidates = params.candidates,
153153
.proofs = params.proofs,
154154
.eligible_sectors = sectors,

core/vm/exit_code/exit_code.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ namespace fc::vm {
4242
ErrPlaceholder = 1000,
4343

4444
DECODE_ACTOR_PARAMS_ERROR,
45+
ENCODE_ACTOR_RESULT_ERROR,
46+
47+
SEND_TRANSFER_INSUFFICIENT,
4548

4649
ACCOUNT_ACTOR_CREATE_WRONG_ADDRESS_TYPE,
4750
ACCOUNT_ACTOR_RESOLVE_NOT_FOUND,

core/vm/exit_code/impl/exit_code.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ namespace fc::vm {
5454

5555
case E::DECODE_ACTOR_PARAMS_ERROR:
5656
return E{1};
57+
case E::ENCODE_ACTOR_RESULT_ERROR:
58+
return E{2};
59+
60+
case E::SEND_TRANSFER_INSUFFICIENT:
61+
return E{1};
5762

5863
case E::ACCOUNT_ACTOR_CREATE_WRONG_ADDRESS_TYPE:
5964
return E::ErrIllegalArgument;

core/vm/runtime/env.hpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,33 @@ namespace fc::vm::runtime {
3131
TokenAmount &penalty);
3232

3333
outcome::result<InvocationOutput> applyImplicitMessage(
34-
UnsignedMessage message) {
35-
OUTCOME_TRY(from, state_tree->get(message.from));
36-
message.nonce = from.nonce;
37-
GasAmount gas_used{0};
38-
return send(gas_used, message.from, message);
39-
}
40-
41-
outcome::result<InvocationOutput> send(GasAmount &gas_used,
42-
const Address &origin,
43-
const UnsignedMessage &message);
34+
UnsignedMessage message);
4435

4536
std::shared_ptr<RandomnessProvider> randomness_provider;
4637
std::shared_ptr<StateTree> state_tree;
4738
std::shared_ptr<Invoker> invoker;
4839
ChainEpoch chain_epoch;
4940
};
41+
42+
struct Execution : std::enable_shared_from_this<Execution> {
43+
static std::shared_ptr<Execution> make(std::shared_ptr<Env> env,
44+
const UnsignedMessage &message);
45+
46+
outcome::result<void> chargeGas(GasAmount amount);
47+
48+
outcome::result<Actor> tryCreateAccountActor(const Address &address);
49+
50+
outcome::result<InvocationOutput> sendWithRevert(
51+
const UnsignedMessage &message);
52+
53+
outcome::result<InvocationOutput> send(const UnsignedMessage &message);
54+
55+
std::shared_ptr<Env> env;
56+
std::shared_ptr<StateTree> state_tree;
57+
GasAmount gas_used;
58+
GasAmount gas_limit;
59+
Address origin;
60+
};
5061
} // namespace fc::vm::runtime
5162

5263
#endif // FILECOIN_CORE_VM_RUNTIME_ENV_HPP

core/vm/runtime/gas_cost.hpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ namespace fc::vm::runtime {
2626
* up to but excluding any actual processing by the VM.
2727
* This is the cost a block producer burns when including an invalid message.
2828
*/
29-
inline static const GasAmount kOnChainMessageBaseGasCost{
30-
kGasAmountPlaceholder};
29+
inline static const GasAmount kOnChainMessageBaseGasCost{0};
3130
inline static const GasAmount kOnChainMessagePerByteGasCharge{2};
3231

32+
inline static const GasAmount kOnChainReturnValuePerByteCost{8};
33+
3334
/**
3435
* Gas cost charged to the originator of a non-nil return value produced by an
3536
* on-chain message is given by:
@@ -52,14 +53,14 @@ namespace fc::vm::runtime {
5253
* by any nonzero currency amount. Accounts for writing receiver's new balance
5354
* (the sender's state is already accounted for).
5455
*/
55-
inline static const GasAmount kSendTransferFundsGasCost{10};
56+
inline static const GasAmount kSendTransferFundsGasCost{5};
5657

5758
/**
5859
* Gas cost charged, in addition to SendBase, if a message invokes a method on
5960
* the receiver. Accounts for the cost of loading receiver code and method
6061
* dispatch.
6162
*/
62-
inline static const GasAmount kSendInvokeMethodGasCost{5};
63+
inline static const GasAmount kSendInvokeMethodGasCost{10};
6364

6465
/**
6566
* Gas cost (Base + len * PerByte) for any Get operation to the IPLD store in
@@ -89,7 +90,7 @@ namespace fc::vm::runtime {
8990
* Gas cost for creating a new actor (via InitActor's Exec method). Actor
9091
* sub-state is charged separately.
9192
*/
92-
inline static const GasAmount kExecNewActorGasCost{kGasAmountPlaceholder};
93+
inline static const GasAmount kCreateActorGasCost{40 + 500};
9394

9495
/**
9596
* Gas cost for deleting an actor.

0 commit comments

Comments
 (0)