common: remove CurrencyToString

Move denomination values to params instead.
This commit is contained in:
Felix Lange 2017-02-20 17:25:49 +01:00
parent 11539030cd
commit 8a8102e7ab
8 changed files with 66 additions and 102 deletions

View file

@ -196,7 +196,7 @@ var (
GasPriceFlag = cli.StringFlag{
Name: "gasprice",
Usage: "Minimal gas price to accept for mining a transactions",
Value: new(big.Int).Mul(big.NewInt(20), common.Shannon).String(),
Value: fmt.Sprint(uint64(20 * params.Shannon)),
}
ExtraDataFlag = cli.StringFlag{
Name: "extradata",
@ -385,12 +385,12 @@ var (
GpoMinGasPriceFlag = cli.StringFlag{
Name: "gpomin",
Usage: "Minimum suggested gas price",
Value: new(big.Int).Mul(big.NewInt(20), common.Shannon).String(),
Value: fmt.Sprint(uint64(20 * params.Shannon)),
}
GpoMaxGasPriceFlag = cli.StringFlag{
Name: "gpomax",
Usage: "Maximum suggested gas price",
Value: new(big.Int).Mul(big.NewInt(500), common.Shannon).String(),
Value: fmt.Sprint(uint64(500 * params.Shannon)),
}
GpoFullBlockRatioFlag = cli.IntFlag{
Name: "gpofull",

View file

@ -18,7 +18,6 @@ package common
import (
"fmt"
"math/big"
)
type StorageSize float64
@ -36,54 +35,3 @@ func (self StorageSize) String() string {
func (self StorageSize) Int64() int64 {
return int64(self)
}
// The different number of units
var (
Douglas = BigPow(10, 42)
Einstein = BigPow(10, 21)
Ether = BigPow(10, 18)
Finney = BigPow(10, 15)
Szabo = BigPow(10, 12)
Shannon = BigPow(10, 9)
Babbage = BigPow(10, 6)
Ada = BigPow(10, 3)
Wei = big.NewInt(1)
)
//
// Currency to string
// Returns a string representing a human readable format
func CurrencyToString(num *big.Int) string {
var (
fin *big.Int = num
denom string = "Wei"
)
switch {
case num.Cmp(Ether) >= 0:
fin = new(big.Int).Div(num, Ether)
denom = "Ether"
case num.Cmp(Finney) >= 0:
fin = new(big.Int).Div(num, Finney)
denom = "Finney"
case num.Cmp(Szabo) >= 0:
fin = new(big.Int).Div(num, Szabo)
denom = "Szabo"
case num.Cmp(Shannon) >= 0:
fin = new(big.Int).Div(num, Shannon)
denom = "Shannon"
case num.Cmp(Babbage) >= 0:
fin = new(big.Int).Div(num, Babbage)
denom = "Babbage"
case num.Cmp(Ada) >= 0:
fin = new(big.Int).Div(num, Ada)
denom = "Ada"
}
// TODO add comment clarifying expected behavior
if len(fin.String()) > 5 {
return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
}
return fmt.Sprintf("%v %s", fin, denom)
}

View file

@ -17,43 +17,22 @@
package common
import (
"math/big"
checker "gopkg.in/check.v1"
"testing"
)
type SizeSuite struct{}
var _ = checker.Suite(&SizeSuite{})
func (s *SizeSuite) TestStorageSizeString(c *checker.C) {
data1 := 2381273
data2 := 2192
data3 := 12
exp1 := "2.38 mB"
exp2 := "2.19 kB"
exp3 := "12.00 B"
c.Assert(StorageSize(data1).String(), checker.Equals, exp1)
c.Assert(StorageSize(data2).String(), checker.Equals, exp2)
c.Assert(StorageSize(data3).String(), checker.Equals, exp3)
func TestStorageSizeString(t *testing.T) {
tests := []struct {
size StorageSize
str string
}{
{2381273, "2.38 mB"},
{2192, "2.19 kB"},
{12, "12.00 B"},
}
func (s *SizeSuite) TestCommon(c *checker.C) {
ether := CurrencyToString(BigPow(10, 19))
finney := CurrencyToString(BigPow(10, 16))
szabo := CurrencyToString(BigPow(10, 13))
shannon := CurrencyToString(BigPow(10, 10))
babbage := CurrencyToString(BigPow(10, 7))
ada := CurrencyToString(BigPow(10, 4))
wei := CurrencyToString(big.NewInt(10))
c.Assert(ether, checker.Equals, "10 Ether")
c.Assert(finney, checker.Equals, "10 Finney")
c.Assert(szabo, checker.Equals, "10 Szabo")
c.Assert(shannon, checker.Equals, "10 Shannon")
c.Assert(babbage, checker.Equals, "10 Babbage")
c.Assert(ada, checker.Equals, "10 Ada")
c.Assert(wei, checker.Equals, "10 Wei")
for _, test := range tests {
if test.size.String() != test.str {
t.Errorf("%f: got %q, want %q", float64(test.size), test.size.String(), test.str)
}
}
}

View file

@ -29,6 +29,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/params"
)
func transaction(nonce uint64, gaslimit *big.Int, key *ecdsa.PrivateKey) *types.Transaction {
@ -65,7 +66,7 @@ func TestStateChangeDuringPoolReset(t *testing.T) {
)
// setup pool with 2 transaction in it
statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether))
statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
tx0 := transaction(0, big.NewInt(100000), key)
tx1 := transaction(1, big.NewInt(100000), key)
@ -82,7 +83,7 @@ func TestStateChangeDuringPoolReset(t *testing.T) {
statedb, _ = state.New(common.Hash{}, db)
// simulate that the new head block included tx0 and tx1
statedb.SetNonce(address, 2)
statedb.SetBalance(address, new(big.Int).Mul(common.Big1, common.Ether))
statedb.SetBalance(address, new(big.Int).SetUint64(params.Ether))
trigger = false
}
return stdb, nil

View file

@ -38,6 +38,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/syndtr/goleveldb/leveldb"
@ -45,9 +46,11 @@ import (
"golang.org/x/net/context"
)
const defaultGas = 90000
var emptyHex = "0x"
const (
defaultGas = 90000
defaultGasPrice = 50 * params.Shannon
emptyHex = "0x"
)
// PublicEthereumAPI provides an API to access Ethereum related information.
// It offers only methods that operate on public data that is freely available to anyone.
@ -597,7 +600,7 @@ func (s *PublicBlockChainAPI) doCall(ctx context.Context, args CallArgs, blockNr
gas = big.NewInt(50000000)
}
if gasPrice.BitLen() == 0 {
gasPrice = new(big.Int).Mul(big.NewInt(50), common.Shannon)
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
}
// Create new call message

View file

@ -561,7 +561,7 @@ func (env *Work) commitTransactions(mux *event.TypeMux, txs *types.TransactionsB
// Ignore any transactions (and accounts subsequently) with low gas limits
if tx.GasPrice().Cmp(gasPrice) < 0 && !env.ownedAccounts.Has(from) {
// Pop the current low-priced transaction without shifting in the next from the account
log.Info(fmt.Sprintf("Transaction (%x) below gas price (tx=%v ask=%v). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], common.CurrencyToString(tx.GasPrice()), common.CurrencyToString(gasPrice), from[:4]))
log.Info(fmt.Sprintf("Transaction (%x) below gas price (tx=%dwei ask=%dwei). All sequential txs from this address(%x) will be ignored\n", tx.Hash().Bytes()[:4], tx.GasPrice(), gasPrice, from[:4]))
env.lowGasTxs = append(env.lowGasTxs, tx)
txs.Pop()

View file

@ -24,7 +24,6 @@ import (
"math/big"
"path/filepath"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethstats"
@ -145,9 +144,9 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
LightMode: true,
DatabaseCache: config.EthereumDatabaseCache,
NetworkId: config.EthereumNetworkID,
GasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
GpoMinGasPrice: new(big.Int).Mul(big.NewInt(20), common.Shannon),
GpoMaxGasPrice: new(big.Int).Mul(big.NewInt(500), common.Shannon),
GasPrice: new(big.Int).SetUint64(20 * params.Shannon),
GpoMinGasPrice: new(big.Int).SetUint64(50 * params.Shannon),
GpoMaxGasPrice: new(big.Int).SetUint64(500 * params.Shannon),
GpoFullBlockRatio: 80,
GpobaseStepDown: 10,
GpobaseStepUp: 100,

34
params/denomination.go Normal file
View file

@ -0,0 +1,34 @@
// Copyright 2017 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package params
const (
// These are the multipliers for ether denominations.
// Example: To get the wei value of an amount in 'douglas', use
//
// new(big.Int).Mul(value, big.NewInt(params.Douglas))
//
Wei = 1
Ada = 1e3
Babbage = 1e6
Shannon = 1e9
Szabo = 1e12
Finney = 1e15
Ether = 1e18
Einstein = 1e21
Douglas = 1e42
)