BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
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  */
25 
26 #include <fc/io/raw.hpp>
27 
28 #define SECONDS_PER_DAY (60 * 60 * 24)
29 
30 namespace graphene { namespace protocol {
31 
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 
38  uint32_t fee_per_kb )const
39  {
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 
50  FC_ASSERT( fee.amount >= 0, "Fee amount should not be negative" );
51  }
52 
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 
62  FC_ASSERT( fee.amount >= 0 , "Fee amount should not be negative");
63  }
64 
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 
#define SECONDS_PER_DAY
Definition: htlc.cpp:28
Definition: api.cpp:48
extension< additional_options_type > extensions
Definition: htlc.hpp:72
size_t pack_size(const T &v)
Definition: raw.hpp:757
microseconds days(int64_t d)
Definition: time.hpp:38
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
share_type calculate_fee(const fee_parameters_type &fee_params) const
Definition: htlc.cpp:65
safe< int64_t > share_type
Definition: types.hpp:309
share_type calculate_fee(const fee_parameters_type &fee_params, uint32_t fee_per_kb) const
Definition: htlc.cpp:37
static uint64_t calculate_data_fee(uint64_t bytes, uint64_t price_per_kbyte)
Definition: operations.cpp:33
share_type calculate_fee(const fee_parameters_type &fee_params) const
Definition: htlc.cpp:53