Skip to content

Commit 2ed0bc1

Browse files
add Runtime::commitState<T> (#107)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent e2d69bd commit 2ed0bc1

File tree

6 files changed

+30
-47
lines changed

6 files changed

+30
-47
lines changed

core/vm/actor/builtin/init/init_actor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ namespace fc::vm::actor::builtin::init {
4848
kConstructorMethodNumber,
4949
exec_params.params,
5050
message.value));
51-
OUTCOME_TRY(new_state, store->setCbor(init_actor));
52-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{new_state}));
51+
OUTCOME_TRY(runtime.commitState(init_actor));
5352
return InvocationOutput{Buffer{primitives::address::encode(id_address)}};
5453
}
5554

core/vm/actor/builtin/multisig/multisig_actor.cpp

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::construct(
158158
state.initial_balance = runtime.getValueReceived();
159159
}
160160

161-
// commit state
162-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
163-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
164-
161+
OUTCOME_TRY(runtime.commitState(state));
165162
return fc::outcome::success();
166163
}
167164

@@ -189,11 +186,9 @@ fc::outcome::result<InvocationOutput> MultiSigActor::propose(
189186
// approve pending tx
190187
OUTCOME_TRY(state.approveTransaction(actor, runtime, tx_number));
191188

192-
// commit state
193-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
194-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
195-
196189
OUTCOME_TRY(encoded_result, codec::cbor::encode(tx_number));
190+
191+
OUTCOME_TRY(runtime.commitState(state));
197192
return InvocationOutput{Buffer{encoded_result}};
198193
}
199194

@@ -211,10 +206,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::approve(
211206
OUTCOME_TRY(
212207
state.approveTransaction(actor, runtime, tx_params.transaction_number));
213208

214-
// commit state
215-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
216-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
217-
209+
OUTCOME_TRY(runtime.commitState(state));
218210
return fc::outcome::success();
219211
}
220212

@@ -239,10 +231,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::cancel(
239231
return VMExitCode::MULTISIG_ACTOR_FORBIDDEN;
240232
}
241233

242-
// commit state
243-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
244-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
245-
234+
OUTCOME_TRY(runtime.commitState(state));
246235
return fc::outcome::success();
247236
}
248237

@@ -264,10 +253,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::addSigner(
264253
state.signers.push_back(add_signer_params.signer);
265254
if (add_signer_params.increase_threshold) state.threshold++;
266255

267-
// commit state
268-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
269-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
270-
256+
OUTCOME_TRY(runtime.commitState(state));
271257
return fc::outcome::success();
272258
}
273259

@@ -296,10 +282,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::removeSigner(
296282
if (state.threshold < 1 || state.signers.size() < state.threshold)
297283
return VMExitCode::MULTISIG_ACTOR_ILLEGAL_ARGUMENT;
298284

299-
// commit state
300-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
301-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
302-
285+
OUTCOME_TRY(runtime.commitState(state));
303286
return fc::outcome::success();
304287
}
305288

@@ -324,10 +307,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::swapSigner(
324307
return VMExitCode::MULTISIG_ACTOR_NOT_FOUND;
325308
*old_signer = swap_signer_params.new_signer;
326309

327-
// commit state
328-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
329-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
330-
310+
OUTCOME_TRY(runtime.commitState(state));
331311
return fc::outcome::success();
332312
}
333313

@@ -349,10 +329,7 @@ fc::outcome::result<InvocationOutput> MultiSigActor::changeThreshold(
349329

350330
state.threshold = change_threshold_params.new_threshold;
351331

352-
// commit state
353-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
354-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
355-
332+
OUTCOME_TRY(runtime.commitState(state));
356333
return fc::outcome::success();
357334
}
358335

core/vm/actor/builtin/payment_channel/payment_channel_actor.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ fc::outcome::result<InvocationOutput> PaymentChannelActor::construct(
3333
PaymentChannelActorState state{
3434
runtime.getImmediateCaller(), construct_params.to, 0, 0, 0, {}};
3535

36-
// commit state
37-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
38-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
39-
36+
OUTCOME_TRY(runtime.commitState(state));
4037
return fc::outcome::success();
4138
}
4239

core/vm/actor/builtin/reward/reward_actor.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ namespace fc::vm::actor::builtin::reward {
107107
OUTCOME_TRY(empty_mmap_cid, empty_mmap.flush());
108108
State empty_state{.reward_total = 0, .reward_map = empty_mmap_cid};
109109

110-
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(empty_state));
111-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
110+
OUTCOME_TRY(runtime.commitState(empty_state));
112111
return outcome::success();
113112
}
114113

@@ -143,11 +142,10 @@ namespace fc::vm::actor::builtin::reward {
143142
.amount_withdrawn = 0};
144143
OUTCOME_TRY(state.addReward(
145144
runtime.getIpfsDatastore(), reward_params.miner, new_reward));
146-
OUTCOME_TRY(new_state_cid, runtime.getIpfsDatastore()->setCbor(state));
147-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{new_state_cid}));
148145
}
149146
OUTCOME_TRY(runtime.send(
150147
kBurntFundsActorAddress, kSendMethodNumber, MethodParams{}, penalty));
148+
OUTCOME_TRY(runtime.commitState(state));
151149
return outcome::success();
152150
}
153151

@@ -163,11 +161,9 @@ namespace fc::vm::actor::builtin::reward {
163161
OUTCOME_TRY(state, store->getCbor<State>(state_cid));
164162
OUTCOME_TRY(withdrawn,
165163
state.withdrawReward(store, owner, runtime.getCurrentEpoch()));
166-
OUTCOME_TRY(new_state_cid, runtime.getIpfsDatastore()->setCbor(state));
167-
OUTCOME_TRY(runtime.commit(ActorSubstateCID{new_state_cid}));
168164
OUTCOME_TRY(
169165
runtime.send(owner, kSendMethodNumber, MethodParams{}, withdrawn));
170-
166+
OUTCOME_TRY(runtime.commitState(state));
171167
return outcome::success();
172168
}
173169

core/vm/runtime/runtime.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,21 @@ namespace fc::vm::runtime {
138138
/// Get current actor state
139139
virtual ActorSubstateCID getCurrentActorState() = 0;
140140

141-
/// Update actor state
141+
/// Update actor state CID
142142
virtual outcome::result<void> commit(const ActorSubstateCID &new_state) = 0;
143+
144+
/**
145+
* Commit actor state
146+
* @tparam T - POD state type
147+
* @param state - actor state structure
148+
* @return error in case of failure
149+
*/
150+
template <typename T>
151+
outcome::result<void> commitState(const T &state) {
152+
OUTCOME_TRY(state_cid, getIpfsDatastore()->setCbor(state));
153+
OUTCOME_TRY(commit(ActorSubstateCID{state_cid}));
154+
return outcome::success();
155+
}
143156
};
144157

145158
} // namespace fc::vm::runtime

test/core/vm/actor/builtin/init/init_actor_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ TEST(InitActorExecText, ExecSuccess) {
112112
.WillOnce(testing::Return(std::cref(message)));
113113

114114
EXPECT_CALL(runtime, getIpfsDatastore())
115-
.WillOnce(testing::Return(state_tree->getStore()));
115+
.Times(2)
116+
.WillRepeatedly(testing::Return(state_tree->getStore()));
116117

117118
EXPECT_CALL(runtime,
118119
send(id_address,

0 commit comments

Comments
 (0)