You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.5 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package blockchain
  17. import "math/big"
  18. import "github.com/arnaucode/derosuite/config"
  19. // this file implements the logic to calculate fees dynamicallly
  20. // get mininum size of block
  21. func Get_Block_Minimum_Size(version uint64) uint64 {
  22. return config.CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE
  23. }
  24. // get maximum size of TX
  25. func Get_Transaction_Maximum_Size() uint64 {
  26. return config.CRYPTONOTE_MAX_TX_SIZE
  27. }
  28. // get per KB fees which is dependent of block reward and median_block_size
  29. //
  30. func (chain *Blockchain) Get_Dynamic_per_kb_fee(block_reward, median_block_size, version uint64) uint64 {
  31. fee_per_kb_base := config.DYNAMIC_FEE_PER_KB_BASE_FEE_V5
  32. min_block_size := Get_Block_Minimum_Size(version)
  33. if median_block_size < min_block_size {
  34. median_block_size = min_block_size
  35. }
  36. var unscaled_fee_per_kb big.Int
  37. unscaled_fee_per_kb.SetUint64((fee_per_kb_base * min_block_size) / median_block_size)
  38. unscaled_fee_per_kb.Mul(&unscaled_fee_per_kb, big.NewInt(int64(block_reward))) // block reward is always positive
  39. unscaled_fee_per_kb.Div(&unscaled_fee_per_kb, big.NewInt(int64(config.DYNAMIC_FEE_PER_KB_BASE_BLOCK_REWARD)))
  40. if !unscaled_fee_per_kb.IsUint64() {
  41. panic("Get_Dynamic_per_kb_fee has issues, Need to fix it urgently\n")
  42. }
  43. lo := unscaled_fee_per_kb.Uint64()
  44. // we need to quantise it 8 decimal // make 4 lower digits 0
  45. mask := uint64(10000)
  46. qlo := (lo + mask - 1) / mask * mask
  47. //logger.Infof("dynamic fee lo= %d qlo=%d", lo, qlo)
  48. return qlo
  49. }
  50. // this will give the dynamic fee at any specfic height
  51. func (chain *Blockchain) Get_Dynamic_Fee_Rate(height uint64) uint64 {
  52. var base_reward, median_block_size uint64
  53. // sanitize height
  54. if height >= chain.Get_Height() {
  55. height = chain.Load_Height_for_BL_ID(chain.Get_Top_ID())
  56. }
  57. block_id_at_height, _ := chain.Load_BL_ID_at_Height(height)
  58. median_block_size = chain.Get_Median_BlockSize_At_Block(block_id_at_height)
  59. base_reward = chain.Load_Block_Reward(block_id_at_height)
  60. return chain.Get_Dynamic_per_kb_fee(base_reward, median_block_size, 0)
  61. }
  62. // get the tx fee
  63. // this function assumes that dynamic fees has been calculated as per requirement
  64. // for every part of 1KB multiply by dynamic_fee_per_kb
  65. // the dynamic fee should be calculated on the basis of the current top
  66. func (chain *Blockchain) Calculate_TX_fee(dynamic_fee_per_kb uint64, tx_size uint64) uint64 {
  67. size_in_kb := tx_size / 1024
  68. if (tx_size % 1024) != 0 { // for any part there of, use a full KB fee
  69. size_in_kb += 1
  70. }
  71. needed_fee := size_in_kb * dynamic_fee_per_kb
  72. return needed_fee
  73. }