dist.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package math
  2. import (
  3. "math/big"
  4. "sort"
  5. "github.com/ethereum/go-ethereum/common"
  6. )
  7. type Summer interface {
  8. Sum(i int) *big.Int
  9. Len() int
  10. }
  11. func Sum(slice Summer) (sum *big.Int) {
  12. sum = new(big.Int)
  13. for i := 0; i < slice.Len(); i++ {
  14. sum.Add(sum, slice.Sum(i))
  15. }
  16. return
  17. }
  18. type Vector struct {
  19. Gas, Price *big.Int
  20. }
  21. type VectorsBy func(v1, v2 Vector) bool
  22. func (self VectorsBy) Sort(vectors []Vector) {
  23. bs := vectorSorter{
  24. vectors: vectors,
  25. by: self,
  26. }
  27. sort.Sort(bs)
  28. }
  29. type vectorSorter struct {
  30. vectors []Vector
  31. by func(v1, v2 Vector) bool
  32. }
  33. func (v vectorSorter) Len() int { return len(v.vectors) }
  34. func (v vectorSorter) Less(i, j int) bool { return v.by(v.vectors[i], v.vectors[j]) }
  35. func (v vectorSorter) Swap(i, j int) { v.vectors[i], v.vectors[j] = v.vectors[j], v.vectors[i] }
  36. func PriceSort(v1, v2 Vector) bool { return v1.Price.Cmp(v2.Price) < 0 }
  37. func GasSort(v1, v2 Vector) bool { return v1.Gas.Cmp(v2.Gas) < 0 }
  38. type vectorSummer struct {
  39. vectors []Vector
  40. by func(v Vector) *big.Int
  41. }
  42. type VectorSum func(v Vector) *big.Int
  43. func (v VectorSum) Sum(vectors []Vector) *big.Int {
  44. vs := vectorSummer{
  45. vectors: vectors,
  46. by: v,
  47. }
  48. return Sum(vs)
  49. }
  50. func (v vectorSummer) Len() int { return len(v.vectors) }
  51. func (v vectorSummer) Sum(i int) *big.Int { return v.by(v.vectors[i]) }
  52. func GasSum(v Vector) *big.Int { return v.Gas }
  53. var etherInWei = new(big.Rat).SetInt(common.String2Big("1000000000000000000"))
  54. func GasPrice(bp, gl, ep *big.Int) *big.Int {
  55. BP := new(big.Rat).SetInt(bp)
  56. GL := new(big.Rat).SetInt(gl)
  57. EP := new(big.Rat).SetInt(ep)
  58. GP := new(big.Rat).Quo(BP, GL)
  59. GP = GP.Quo(GP, EP)
  60. return GP.Mul(GP, etherInWei).Num()
  61. }