BitShares-Core
7.0.2
BitShares blockchain node software and command-line wallet software
libraries
protocol
htlc.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (c) 2018 jmjatlanta and contributors.
3
*
4
* The MIT License
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to deal
8
* in the Software without restriction, including without limitation the rights
9
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
* THE SOFTWARE.
23
*/
24
#include <
graphene/protocol/htlc.hpp
>
25
26
#include <
fc/io/raw.hpp
>
27
28
#define SECONDS_PER_DAY (60 * 60 * 24)
29
30
namespace
graphene
{
namespace
protocol {
31
32
void
htlc_create_operation::validate
()
const
{
33
FC_ASSERT
(
fee
.
amount
>= 0,
"Fee amount should not be negative"
);
34
FC_ASSERT
(
amount
.
amount
> 0,
"HTLC amount should be greater than zero"
);
35
}
36
37
share_type
htlc_create_operation::calculate_fee
(
const
fee_params_t
& fee_params,
38
uint32_t fee_per_kb )
const
39
{
40
uint64_t
days
= (
claim_period_seconds
+
SECONDS_PER_DAY
- 1 ) /
SECONDS_PER_DAY
;
41
// multiply with overflow check
42
share_type
total_fee = fee_params.
fee
;
43
total_fee +=
share_type
(fee_params.
fee_per_day
) *
days
;
44
if
(
extensions
.value.memo.valid())
45
total_fee +=
calculate_data_fee
(
fc::raw::pack_size
(
extensions
.value.memo), fee_per_kb);
46
return
total_fee;
47
}
48
49
void
htlc_redeem_operation::validate
()
const
{
50
FC_ASSERT
(
fee
.
amount
>= 0,
"Fee amount should not be negative"
);
51
}
52
53
share_type
htlc_redeem_operation::calculate_fee
(
const
fee_params_t
& fee_params )
const
54
{
55
uint64_t kb = (
preimage
.size() + 1023 ) / 1024;
56
uint64_t product = kb * fee_params.
fee_per_kb
;
57
FC_ASSERT
( kb == 0 || product / kb == fee_params.
fee_per_kb
,
"Fee calculation overflow"
);
58
return
fee_params.
fee
+ product;
59
}
60
61
void
htlc_extend_operation::validate
()
const
{
62
FC_ASSERT
(
fee
.
amount
>= 0 ,
"Fee amount should not be negative"
);
63
}
64
65
share_type
htlc_extend_operation::calculate_fee
(
const
fee_params_t
& fee_params )
const
66
{
67
uint32_t
days
= (
seconds_to_add
+
SECONDS_PER_DAY
- 1 ) /
SECONDS_PER_DAY
;
68
uint64_t per_day_fee = fee_params.
fee_per_day
*
days
;
69
FC_ASSERT
(
days
== 0 || per_day_fee /
days
== fee_params.
fee_per_day
,
"Fee calculation overflow"
);
70
return
fee_params.
fee
+ per_day_fee;
71
}
72
} }
73
74
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_create_operation::fee_params_t
)
75
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_create_operation::additional_options_type
)
76
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_redeem_operation::fee_params_t
)
77
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_extend_operation::fee_params_t
)
78
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_create_operation
)
79
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_redeem_operation
)
80
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_redeemed_operation
)
81
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_extend_operation
)
82
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
(
graphene::protocol::htlc_refund_operation
)
graphene::protocol::htlc_redeem_operation::fee_params_t
Definition:
htlc.hpp:92
htlc.hpp
graphene::protocol::htlc_create_operation::fee
asset fee
Definition:
htlc.hpp:53
graphene::protocol::htlc_create_operation::fee_params_t::fee
uint64_t fee
Definition:
htlc.hpp:48
graphene::protocol::htlc_redeem_operation
Definition:
htlc.hpp:90
graphene::protocol::htlc_extend_operation::fee_params_t::fee
uint64_t fee
Definition:
htlc.hpp:156
graphene::protocol::htlc_refund_operation
Definition:
htlc.hpp:187
graphene::protocol::htlc_redeem_operation::preimage
std::vector< char > preimage
Definition:
htlc.hpp:104
graphene::protocol::htlc_redeem_operation::validate
void validate() const
Definition:
htlc.cpp:49
graphene::protocol::htlc_extend_operation::fee_params_t
Definition:
htlc.hpp:155
graphene::protocol::htlc_extend_operation::validate
void validate() const
Definition:
htlc.cpp:61
graphene::protocol::htlc_redeemed_operation
Definition:
htlc.hpp:127
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition:
types.hpp:86
graphene::protocol::htlc_create_operation
Definition:
htlc.hpp:45
graphene::protocol::htlc_create_operation::fee_params_t::fee_per_day
uint64_t fee_per_day
Definition:
htlc.hpp:49
graphene::protocol::htlc_create_operation::calculate_fee
share_type calculate_fee(const fee_params_t &fee_params, uint32_t fee_per_kb) const
Definition:
htlc.cpp:37
graphene::protocol::base_operation::calculate_data_fee
static uint64_t calculate_data_fee(uint64_t bytes, uint64_t price_per_kbyte)
Definition:
operations.cpp:33
fc::days
microseconds days(int64_t d)
Definition:
time.hpp:38
graphene::protocol::htlc_redeem_operation::calculate_fee
share_type calculate_fee(const fee_params_t &fee_params) const
Definition:
htlc.cpp:53
SECONDS_PER_DAY
#define SECONDS_PER_DAY
Definition:
htlc.cpp:28
graphene::protocol::htlc_create_operation::amount
asset amount
Definition:
htlc.hpp:59
graphene::protocol::htlc_redeem_operation::fee
asset fee
Definition:
htlc.hpp:98
fc::raw::pack_size
size_t pack_size(const T &v)
Definition:
raw.hpp:757
graphene::protocol::share_type
safe< int64_t > share_type
Definition:
types.hpp:309
graphene::protocol::htlc_redeem_operation::fee_params_t::fee
uint64_t fee
Definition:
htlc.hpp:93
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition:
exception.hpp:345
graphene::protocol::htlc_redeem_operation::fee_params_t::fee_per_kb
uint64_t fee_per_kb
Definition:
htlc.hpp:94
graphene::protocol::htlc_create_operation::fee_params_t
Definition:
htlc.hpp:47
graphene::protocol::asset::amount
share_type amount
Definition:
asset.hpp:36
graphene::protocol::htlc_create_operation::validate
void validate() const
Definition:
htlc.cpp:32
graphene::protocol::htlc_extend_operation
Definition:
htlc.hpp:153
graphene::protocol::htlc_extend_operation::calculate_fee
share_type calculate_fee(const fee_params_t &fee_params) const
Definition:
htlc.cpp:65
graphene::protocol::htlc_extend_operation::fee
asset fee
Definition:
htlc.hpp:161
graphene::protocol::htlc_extend_operation::seconds_to_add
uint32_t seconds_to_add
Definition:
htlc.hpp:167
graphene::protocol::htlc_create_operation::extensions
extension< additional_options_type > extensions
Definition:
htlc.hpp:72
graphene::protocol::htlc_create_operation::additional_options_type
Definition:
htlc.hpp:68
graphene::protocol::htlc_extend_operation::fee_params_t::fee_per_day
uint64_t fee_per_day
Definition:
htlc.hpp:157
graphene
Definition:
api.cpp:48
graphene::protocol::htlc_create_operation::claim_period_seconds
uint32_t claim_period_seconds
Definition:
htlc.hpp:65
raw.hpp
fc::safe
Definition:
safe.hpp:26
Generated on Fri Dec 15 2023 06:12:43 for BitShares-Core by
1.8.17