diff options
author | obscuren <geffobscura@gmail.com> | 2015-03-16 18:27:38 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-03-16 18:27:38 +0800 |
commit | b5234413611ce5984292f85a01de1f56c045b490 (patch) | |
tree | e6e0c6f7fe8358a2dc63cdea11ac66b4f59397f5 /common/math | |
parent | 0b8f66ed9ef177dc72442dd7ba337c6733e30344 (diff) | |
download | go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar.gz go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar.bz2 go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar.lz go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar.xz go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.tar.zst go-tangerine-b5234413611ce5984292f85a01de1f56c045b490.zip |
Moved ethutil => common
Diffstat (limited to 'common/math')
-rw-r--r-- | common/math/dist.go | 80 | ||||
-rw-r--r-- | common/math/dist_test.go | 66 |
2 files changed, 146 insertions, 0 deletions
diff --git a/common/math/dist.go b/common/math/dist.go new file mode 100644 index 000000000..0a0731971 --- /dev/null +++ b/common/math/dist.go @@ -0,0 +1,80 @@ +package math + +import ( + "math/big" + "sort" + + "github.com/ethereum/go-ethereum/common" +) + +type Summer interface { + Sum(i int) *big.Int + Len() int +} + +func Sum(slice Summer) (sum *big.Int) { + sum = new(big.Int) + + for i := 0; i < slice.Len(); i++ { + sum.Add(sum, slice.Sum(i)) + } + return +} + +type Vector struct { + Gas, Price *big.Int +} + +type VectorsBy func(v1, v2 Vector) bool + +func (self VectorsBy) Sort(vectors []Vector) { + bs := vectorSorter{ + vectors: vectors, + by: self, + } + sort.Sort(bs) +} + +type vectorSorter struct { + vectors []Vector + by func(v1, v2 Vector) bool +} + +func (v vectorSorter) Len() int { return len(v.vectors) } +func (v vectorSorter) Less(i, j int) bool { return v.by(v.vectors[i], v.vectors[j]) } +func (v vectorSorter) Swap(i, j int) { v.vectors[i], v.vectors[j] = v.vectors[j], v.vectors[i] } + +func PriceSort(v1, v2 Vector) bool { return v1.Price.Cmp(v2.Price) < 0 } +func GasSort(v1, v2 Vector) bool { return v1.Gas.Cmp(v2.Gas) < 0 } + +type vectorSummer struct { + vectors []Vector + by func(v Vector) *big.Int +} + +type VectorSum func(v Vector) *big.Int + +func (v VectorSum) Sum(vectors []Vector) *big.Int { + vs := vectorSummer{ + vectors: vectors, + by: v, + } + return Sum(vs) +} + +func (v vectorSummer) Len() int { return len(v.vectors) } +func (v vectorSummer) Sum(i int) *big.Int { return v.by(v.vectors[i]) } + +func GasSum(v Vector) *big.Int { return v.Gas } + +var etherInWei = new(big.Rat).SetInt(common.String2Big("1000000000000000000")) + +func GasPrice(bp, gl, ep *big.Int) *big.Int { + BP := new(big.Rat).SetInt(bp) + GL := new(big.Rat).SetInt(gl) + EP := new(big.Rat).SetInt(ep) + GP := new(big.Rat).Quo(BP, GL) + GP = GP.Quo(GP, EP) + + return GP.Mul(GP, etherInWei).Num() +} diff --git a/common/math/dist_test.go b/common/math/dist_test.go new file mode 100644 index 000000000..90e302f44 --- /dev/null +++ b/common/math/dist_test.go @@ -0,0 +1,66 @@ +package math + +import ( + "fmt" + "math/big" + "testing" +) + +type summer struct { + numbers []*big.Int +} + +func (s summer) Len() int { return len(s.numbers) } +func (s summer) Sum(i int) *big.Int { + return s.numbers[i] +} + +func TestSum(t *testing.T) { + summer := summer{numbers: []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}} + sum := Sum(summer) + if sum.Cmp(big.NewInt(6)) != 0 { + t.Errorf("not 6", sum) + } +} + +func TestDist(t *testing.T) { + var vectors = []Vector{ + Vector{big.NewInt(1000), big.NewInt(1234)}, + Vector{big.NewInt(500), big.NewInt(10023)}, + Vector{big.NewInt(1034), big.NewInt(1987)}, + Vector{big.NewInt(1034), big.NewInt(1987)}, + Vector{big.NewInt(8983), big.NewInt(1977)}, + Vector{big.NewInt(98382), big.NewInt(1887)}, + Vector{big.NewInt(12398), big.NewInt(1287)}, + Vector{big.NewInt(12398), big.NewInt(1487)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(12398), big.NewInt(128)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(1398), big.NewInt(187)}, + Vector{big.NewInt(12328), big.NewInt(1927)}, + Vector{big.NewInt(12398), big.NewInt(1987)}, + Vector{big.NewInt(22398), big.NewInt(1287)}, + Vector{big.NewInt(1370), big.NewInt(1981)}, + Vector{big.NewInt(12398), big.NewInt(1957)}, + Vector{big.NewInt(42198), big.NewInt(1987)}, + } + + VectorsBy(GasSort).Sort(vectors) + fmt.Println(vectors) + + BP := big.NewInt(15) + GL := big.NewInt(1000000) + EP := big.NewInt(100) + fmt.Println("BP", BP, "GL", GL, "EP", EP) + GP := GasPrice(BP, GL, EP) + fmt.Println("GP =", GP, "Wei per GU") + + S := len(vectors) / 4 + fmt.Println("L", len(vectors), "S", S) + for i := 1; i <= S*4; i += S { + fmt.Printf("T%d = %v\n", i, vectors[i]) + } + + g := VectorSum(GasSum).Sum(vectors) + fmt.Printf("G = ∑g* (%v)\n", g) +} |