Skip to content

Commit 0adcb72

Browse files
Feature/payment channel actor (#91)
* init Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent dfa6fc8 commit 0adcb72

File tree

10 files changed

+560
-0
lines changed

10 files changed

+560
-0
lines changed

core/vm/actor/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ add_subdirectory(account)
77
add_subdirectory(cron)
88
add_subdirectory(init)
99
add_subdirectory(multisig)
10+
add_subdirectory(payment_channel)
1011
add_subdirectory(storage_power)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_library(payment_channel_actor
7+
payment_channel_actor.cpp
8+
)
9+
target_link_libraries(payment_channel_actor
10+
actor
11+
outcome
12+
signature
13+
)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "vm/actor/builtin/payment_channel/payment_channel_actor.hpp"
7+
8+
#include "vm/actor/actor_method.hpp"
9+
#include "vm/exit_code/exit_code.hpp"
10+
11+
using fc::outcome::result;
12+
using fc::primitives::address::Protocol;
13+
using fc::vm::actor::decodeActorParams;
14+
using fc::vm::actor::kAccountCodeCid;
15+
using fc::vm::actor::builtin::payment_channel::PaymentChannelActor;
16+
using fc::vm::runtime::InvocationOutput;
17+
using fc::vm::runtime::Runtime;
18+
19+
fc::outcome::result<InvocationOutput> PaymentChannelActor::construct(
20+
const Actor &actor, Runtime &runtime, const MethodParams &params) {
21+
if (actor.code != kAccountCodeCid)
22+
return VMExitCode::PAYMENT_CHANNEL_WRONG_CALLER;
23+
24+
OUTCOME_TRY(construct_params,
25+
decodeActorParams<ConstructParameteres>(params));
26+
if (construct_params.to.getProtocol() != Protocol::ID)
27+
return VMExitCode::PAYMENT_CHANNEL_ILLEGAL_ARGUMENT;
28+
OUTCOME_TRY(target_actor_code_id,
29+
runtime.getActorCodeID(construct_params.to));
30+
if (target_actor_code_id != kAccountCodeCid)
31+
return VMExitCode::PAYMENT_CHANNEL_ILLEGAL_ARGUMENT;
32+
33+
PaymentChannelActorState state{
34+
runtime.getImmediateCaller(), construct_params.to, 0, 0, 0, {}};
35+
36+
// commit state
37+
OUTCOME_TRY(state_cid, runtime.getIpfsDatastore()->setCbor(state));
38+
OUTCOME_TRY(runtime.commit(ActorSubstateCID{state_cid}));
39+
40+
return fc::outcome::success();
41+
}
42+
43+
fc::outcome::result<InvocationOutput> PaymentChannelActor::updateChannelState(
44+
const Actor &actor, Runtime &runtime, const MethodParams &params) {
45+
OUTCOME_TRY(state,
46+
runtime.getIpfsDatastore()->getCbor<PaymentChannelActorState>(
47+
actor.head));
48+
Address signer = runtime.getImmediateCaller();
49+
if (signer != state.from || signer != state.to) {
50+
return VMExitCode::PAYMENT_CHANNEL_WRONG_CALLER;
51+
}
52+
53+
OUTCOME_TRY(update_state_params,
54+
decodeActorParams<UpdateChannelStateParameters>(params));
55+
56+
// TODO (a.chernyshov) not implemented yet FIL-129
57+
58+
return fc::outcome::success();
59+
}
60+
61+
fc::outcome::result<InvocationOutput> PaymentChannelActor::settle(
62+
const Actor &actor, Runtime &runtime, const MethodParams &params) {
63+
64+
// TODO (a.chernyshov) not implemented yet FIL-129
65+
66+
return fc::outcome::success();
67+
}
68+
69+
fc::outcome::result<InvocationOutput> PaymentChannelActor::collect(
70+
const Actor &actor, Runtime &runtime, const MethodParams &params) {
71+
72+
// TODO (a.chernyshov) not implemented yet FIL-129
73+
74+
return fc::outcome::success();
75+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_HPP
7+
#define CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_HPP
8+
9+
#include "common/outcome.hpp"
10+
#include "primitives/address/address_codec.hpp"
11+
#include "vm/actor/builtin/payment_channel/payment_channel_actor_state.hpp"
12+
#include "vm/runtime/runtime.hpp"
13+
#include "vm/runtime/runtime_types.hpp"
14+
15+
namespace fc::vm::actor::builtin::payment_channel {
16+
17+
using fc::vm::runtime::InvocationOutput;
18+
using fc::vm::runtime::Runtime;
19+
20+
/**
21+
* Construct method parameters
22+
*/
23+
struct ConstructParameteres {
24+
/** Voucher receiver */
25+
Address to;
26+
};
27+
28+
/**
29+
* UpdateChannelState method parameters
30+
*/
31+
struct UpdateChannelStateParameters {
32+
SignedVoucher signed_voucher;
33+
Buffer secret;
34+
Buffer proof;
35+
};
36+
37+
class PaymentChannelActor {
38+
public:
39+
static outcome::result<InvocationOutput> construct(
40+
const Actor &actor, Runtime &runtime, const MethodParams &params);
41+
42+
static outcome::result<InvocationOutput> updateChannelState(
43+
const Actor &actor, Runtime &runtime, const MethodParams &params);
44+
45+
static outcome::result<InvocationOutput> settle(const Actor &actor,
46+
Runtime &runtime,
47+
const MethodParams &params);
48+
49+
static outcome::result<InvocationOutput> collect(
50+
const Actor &actor, Runtime &runtime, const MethodParams &params);
51+
};
52+
53+
/**
54+
* CBOR serialization of ConstructParameteres
55+
*/
56+
template <class Stream,
57+
typename = std::enable_if_t<
58+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
59+
Stream &operator<<(Stream &&s, const ConstructParameteres &construct_params) {
60+
return s << (s.list() << construct_params.to);
61+
}
62+
63+
/**
64+
* CBOR deserialization of ConstructParameteres
65+
*/
66+
template <class Stream,
67+
typename = std::enable_if_t<
68+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
69+
Stream &operator>>(Stream &&s, ConstructParameteres &construct_params) {
70+
s.list() >> construct_params.to;
71+
return s;
72+
}
73+
74+
/**
75+
* CBOR serialization of UpdateChannelStateParameters
76+
*/
77+
template <class Stream,
78+
typename = std::enable_if_t<
79+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
80+
Stream &operator<<(Stream &&s, const UpdateChannelStateParameters &params) {
81+
return s << (s.list() << params.signed_voucher << params.secret
82+
<< params.proof);
83+
}
84+
85+
/**
86+
* CBOR deserialization of UpdateChannelStateParameters
87+
*/
88+
template <class Stream,
89+
typename = std::enable_if_t<
90+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
91+
Stream &operator>>(Stream &&s, UpdateChannelStateParameters &params) {
92+
s.list() >> params.signed_voucher >> params.secret >> params.proof;
93+
return s;
94+
}
95+
96+
} // namespace fc::vm::actor::builtin::payment_channel
97+
98+
#endif // CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_HPP
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_STATE_HPP
7+
#define CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_STATE_HPP
8+
9+
#include "common/buffer.hpp"
10+
#include "crypto/signature/signature.hpp"
11+
#include "primitives/address/address.hpp"
12+
#include "primitives/big_int.hpp"
13+
#include "primitives/chain_epoch.hpp"
14+
#include "vm/actor/actor.hpp"
15+
16+
namespace fc::vm::actor::builtin::payment_channel {
17+
18+
using common::Buffer;
19+
using crypto::signature::Signature;
20+
using fc::vm::actor::MethodNumber;
21+
using primitives::BigInt;
22+
using primitives::ChainEpoch;
23+
using primitives::UBigInt;
24+
using primitives::address::Address;
25+
26+
struct LaneState {
27+
int64_t state{};
28+
BigInt redeem{};
29+
int64_t nonce{};
30+
31+
inline bool operator==(const LaneState &other) const {
32+
return state == other.state && redeem == other.redeem
33+
&& nonce == other.nonce;
34+
}
35+
};
36+
37+
struct PaymentChannelActorState {
38+
Address from;
39+
Address to;
40+
UBigInt to_send{};
41+
ChainEpoch settling_at;
42+
ChainEpoch min_settling_height;
43+
std::vector<LaneState> lanes{};
44+
};
45+
46+
struct Merge {
47+
uint64_t lane{};
48+
uint64_t nonce{};
49+
};
50+
51+
/**
52+
* Modular Verification method
53+
*/
54+
struct ModularVerificationParameter {
55+
Address actor;
56+
MethodNumber method;
57+
Buffer data;
58+
};
59+
60+
struct SignedVoucher {
61+
uint64_t time_lock{};
62+
Buffer secret_preimage;
63+
ModularVerificationParameter extra;
64+
uint64_t lane{};
65+
uint64_t nonce{};
66+
UBigInt amount;
67+
uint64_t min_close_height{};
68+
std::vector<Merge> merges{};
69+
Signature signature;
70+
};
71+
72+
/**
73+
* CBOR serialization of LaneState
74+
*/
75+
template <class Stream,
76+
typename = std::enable_if_t<
77+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
78+
Stream &operator<<(Stream &&s, const LaneState &state) {
79+
return s << (s.list() << state.state << state.redeem << state.nonce);
80+
}
81+
82+
/**
83+
* CBOR deserialization of LaneState
84+
*/
85+
template <class Stream,
86+
typename = std::enable_if_t<
87+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
88+
Stream &operator>>(Stream &&s, LaneState &state) {
89+
s.list() >> state.state >> state.redeem >> state.nonce;
90+
return s;
91+
}
92+
93+
/**
94+
* CBOR serialization of PaymentChannelActorState
95+
*/
96+
template <class Stream,
97+
typename = std::enable_if_t<
98+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
99+
Stream &operator<<(Stream &&s, const PaymentChannelActorState &state) {
100+
return s << (s.list() << state.from << state.to << state.to_send
101+
<< state.settling_at << state.min_settling_height
102+
<< state.lanes);
103+
}
104+
105+
/**
106+
* CBOR deserialization of PaymentChannelActorState
107+
*/
108+
template <class Stream,
109+
typename = std::enable_if_t<
110+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
111+
Stream &operator>>(Stream &&s, PaymentChannelActorState &state) {
112+
s.list() >> state.from >> state.to >> state.to_send >> state.settling_at
113+
>> state.min_settling_height >> state.lanes;
114+
return s;
115+
}
116+
117+
/**
118+
* CBOR serialization of ModularVerificationParameter
119+
*/
120+
template <class Stream,
121+
typename = std::enable_if_t<
122+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
123+
Stream &operator<<(Stream &&s, const ModularVerificationParameter &param) {
124+
return s << (s.list() << param.actor << param.method << param.data);
125+
}
126+
127+
/**
128+
* CBOR deserialization of ModularVerificationParameter
129+
*/
130+
template <class Stream,
131+
typename = std::enable_if_t<
132+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
133+
Stream &operator>>(Stream &&s, ModularVerificationParameter &param) {
134+
s.list() >> param.actor >> param.method >> param.data;
135+
return s;
136+
}
137+
138+
/**
139+
* CBOR serialization of Merge
140+
*/
141+
template <class Stream,
142+
typename = std::enable_if_t<
143+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
144+
Stream &operator<<(Stream &&s, const Merge &merge) {
145+
return s << (s.list() << merge.lane << merge.nonce);
146+
}
147+
148+
/**
149+
* CBOR deserialization of Merge
150+
*/
151+
template <class Stream,
152+
typename = std::enable_if_t<
153+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
154+
Stream &operator>>(Stream &&s, Merge &merge) {
155+
s.list() >> merge.lane >> merge.nonce;
156+
return s;
157+
}
158+
159+
/**
160+
* CBOR serialization of SignedVoucher
161+
*/
162+
template <class Stream,
163+
typename = std::enable_if_t<
164+
std::remove_reference_t<Stream>::is_cbor_encoder_stream>>
165+
Stream &operator<<(Stream &&s, const SignedVoucher &voucher) {
166+
return s << (s.list() << voucher.time_lock << voucher.secret_preimage
167+
<< voucher.extra << voucher.lane << voucher.nonce
168+
<< voucher.amount << voucher.min_close_height
169+
<< voucher.merges << voucher.signature);
170+
}
171+
172+
/**
173+
* CBOR deserialization of SignedVoucher
174+
*/
175+
template <class Stream,
176+
typename = std::enable_if_t<
177+
std::remove_reference_t<Stream>::is_cbor_decoder_stream>>
178+
Stream &operator>>(Stream &&s, SignedVoucher &voucher) {
179+
s.list() >> voucher.time_lock >> voucher.secret_preimage >> voucher.extra
180+
>> voucher.lane >> voucher.nonce >> voucher.amount
181+
>> voucher.min_close_height >> voucher.merges >> voucher.signature;
182+
return s;
183+
}
184+
185+
} // namespace fc::vm::actor::builtin::payment_channel
186+
#endif // CPP_FILECOIN_VM_ACTOR_BUILTIN_PAYMENT_CHANNEL_ACTOR_STATE_HPP

core/vm/exit_code/exit_code.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ namespace fc::vm {
3434
MULTISIG_ACTOR_INSUFFICIENT_FUNDS,
3535
MULTISIG_ACTOR_ILLEGAL_STATE,
3636

37+
PAYMENT_CHANNEL_WRONG_CALLER,
38+
PAYMENT_CHANNEL_ILLEGAL_ARGUMENT,
39+
3740
STORAGE_POWER_ACTOR_OUT_OF_BOUND,
3841
STORAGE_POWER_ACTOR_ALREADY_EXISTS,
3942

0 commit comments

Comments
 (0)