dist.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package math
  17. import (
  18. "math/big"
  19. "sort"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. type Summer interface {
  23. Sum(i int) *big.Int
  24. Len() int
  25. }
  26. func Sum(slice Summer) (sum *big.Int) {
  27. sum = new(big.Int)
  28. for i := 0; i < slice.Len(); i++ {
  29. sum.Add(sum, slice.Sum(i))
  30. }
  31. return
  32. }
  33. type Vector struct {
  34. Gas, Price *big.Int
  35. }
  36. type VectorsBy func(v1, v2 Vector) bool
  37. func (self VectorsBy) Sort(vectors []Vector) {
  38. bs := vectorSorter{
  39. vectors: vectors,
  40. by: self,
  41. }
  42. sort.Sort(bs)
  43. }
  44. type vectorSorter struct {
  45. vectors []Vector
  46. by func(v1, v2 Vector) bool
  47. }
  48. func (v vectorSorter) Len() int { return len(v.vectors) }
  49. func (v vectorSorter) Less(i, j int) bool { return v.by(v.vectors[i], v.vectors[j]) }
  50. func (v vectorSorter) Swap(i, j int) { v.vectors[i], v.vectors[j] = v.vectors[j], v.vectors[i] }
  51. func PriceSort(v1, v2 Vector) bool { return v1.Price.Cmp(v2.Price) < 0 }
  52. func GasSort(v1, v2 Vector) bool { return v1.Gas.Cmp(v2.Gas) < 0 }
  53. type vectorSummer struct {
  54. vectors []Vector
  55. by func(v Vector) *big.Int
  56. }
  57. type VectorSum func(v Vector) *big.Int
  58. func (v VectorSum) Sum(vectors []Vector) *big.Int {
  59. vs := vectorSummer{
  60. vectors: vectors,
  61. by: v,
  62. }
  63. return Sum(vs)
  64. }
  65. func (v vectorSummer) Len() int { return len(v.vectors) }
  66. func (v vectorSummer) Sum(i int) *big.Int { return v.by(v.vectors[i]) }
  67. func GasSum(v Vector) *big.Int { return v.Gas }
  68. var etherInWei = new(big.Rat).SetInt(common.String2Big("1000000000000000000"))
  69. func GasPrice(bp, gl, ep *big.Int) *big.Int {
  70. BP := new(big.Rat).SetInt(bp)
  71. GL := new(big.Rat).SetInt(gl)
  72. EP := new(big.Rat).SetInt(ep)
  73. GP := new(big.Rat).Quo(BP, GL)
  74. GP = GP.Quo(GP, EP)
  75. return GP.Mul(GP, etherInWei).Num()
  76. }