github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/common/math/dist.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library 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 // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package math 18 19 import ( 20 "math/big" 21 "sort" 22 23 "github.com/atheioschain/go-atheios/common" 24 ) 25 26 type Summer interface { 27 Sum(i int) *big.Int 28 Len() int 29 } 30 31 func Sum(slice Summer) (sum *big.Int) { 32 sum = new(big.Int) 33 34 for i := 0; i < slice.Len(); i++ { 35 sum.Add(sum, slice.Sum(i)) 36 } 37 return 38 } 39 40 type Vector struct { 41 Gas, Price *big.Int 42 } 43 44 type VectorsBy func(v1, v2 Vector) bool 45 46 func (self VectorsBy) Sort(vectors []Vector) { 47 bs := vectorSorter{ 48 vectors: vectors, 49 by: self, 50 } 51 sort.Sort(bs) 52 } 53 54 type vectorSorter struct { 55 vectors []Vector 56 by func(v1, v2 Vector) bool 57 } 58 59 func (v vectorSorter) Len() int { return len(v.vectors) } 60 func (v vectorSorter) Less(i, j int) bool { return v.by(v.vectors[i], v.vectors[j]) } 61 func (v vectorSorter) Swap(i, j int) { v.vectors[i], v.vectors[j] = v.vectors[j], v.vectors[i] } 62 63 func PriceSort(v1, v2 Vector) bool { return v1.Price.Cmp(v2.Price) < 0 } 64 func GasSort(v1, v2 Vector) bool { return v1.Gas.Cmp(v2.Gas) < 0 } 65 66 type vectorSummer struct { 67 vectors []Vector 68 by func(v Vector) *big.Int 69 } 70 71 type VectorSum func(v Vector) *big.Int 72 73 func (v VectorSum) Sum(vectors []Vector) *big.Int { 74 vs := vectorSummer{ 75 vectors: vectors, 76 by: v, 77 } 78 return Sum(vs) 79 } 80 81 func (v vectorSummer) Len() int { return len(v.vectors) } 82 func (v vectorSummer) Sum(i int) *big.Int { return v.by(v.vectors[i]) } 83 84 func GasSum(v Vector) *big.Int { return v.Gas } 85 86 var etherInWei = new(big.Rat).SetInt(common.String2Big("1000000000000000000")) 87 88 func GasPrice(bp, gl, ep *big.Int) *big.Int { 89 BP := new(big.Rat).SetInt(bp) 90 GL := new(big.Rat).SetInt(gl) 91 EP := new(big.Rat).SetInt(ep) 92 GP := new(big.Rat).Quo(BP, GL) 93 GP = GP.Quo(GP, EP) 94 95 return GP.Mul(GP, etherInWei).Num() 96 }