1 // Copyright 2015 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
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.
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.
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/>.
23 "github.com/bytom/common"
26 type Summer interface {
31 func Sum(slice Summer) (sum *big.Int) {
34 for i := 0; i < slice.Len(); i++ {
35 sum.Add(sum, slice.Sum(i))
44 type VectorsBy func(v1, v2 Vector) bool
46 func (self VectorsBy) Sort(vectors []Vector) {
54 type vectorSorter struct {
56 by func(v1, v2 Vector) bool
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] }
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 }
66 type vectorSummer struct {
68 by func(v Vector) *big.Int
71 type VectorSum func(v Vector) *big.Int
73 func (v VectorSum) Sum(vectors []Vector) *big.Int {
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]) }
84 func GasSum(v Vector) *big.Int { return v.Gas }
86 var etherInWei = new(big.Rat).SetInt(common.String2Big("1000000000000000000"))
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)
95 return GP.Mul(GP, etherInWei).Num()