2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-04-08 16:43:55 +03:00
|
|
|
package utils
|
2015-04-09 11:26:26 +03:00
|
|
|
|
2015-04-08 16:43:55 +03:00
|
|
|
import (
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
"errors"
|
2015-04-09 11:26:26 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
"math/big"
|
2015-04-09 11:26:26 +03:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2015-08-06 17:18:32 +03:00
|
|
|
"path"
|
2015-04-09 11:26:26 +03:00
|
|
|
"strings"
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
"gopkg.in/urfave/cli.v1"
|
2015-04-08 16:43:55 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Custom type which is registered in the flags library which cli uses for
|
|
|
|
// argument parsing. This allows us to expand Value to an absolute path when
|
|
|
|
// the argument is parsed
|
|
|
|
type DirectoryString struct {
|
2015-04-09 11:26:26 +03:00
|
|
|
Value string
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:06:27 +03:00
|
|
|
func (self *DirectoryString) String() string {
|
2015-04-09 11:26:26 +03:00
|
|
|
return self.Value
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:06:27 +03:00
|
|
|
func (self *DirectoryString) Set(value string) error {
|
2015-04-09 11:26:26 +03:00
|
|
|
self.Value = expandPath(value)
|
|
|
|
return nil
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom cli.Flag type which expand the received string to an absolute path.
|
|
|
|
// e.g. ~/.ethereum -> /home/username/.ethereum
|
|
|
|
type DirectoryFlag struct {
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
Name string
|
|
|
|
Value DirectoryString
|
|
|
|
Usage string
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self DirectoryFlag) String() string {
|
2017-01-09 13:16:06 +03:00
|
|
|
fmtString := "%s %v\t%v"
|
2015-04-09 11:26:26 +03:00
|
|
|
if len(self.Value.Value) > 0 {
|
|
|
|
fmtString = "%s \"%v\"\t%v"
|
|
|
|
}
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
return fmt.Sprintf(fmtString, prefixedNames(self.Name), self.Value.Value, self.Usage)
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func eachName(longName string, fn func(string)) {
|
2015-04-09 11:26:26 +03:00
|
|
|
parts := strings.Split(longName, ",")
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
fn(name)
|
|
|
|
}
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
2015-04-09 11:26:26 +03:00
|
|
|
|
2015-04-08 16:43:55 +03:00
|
|
|
// called by cli library, grabs variable from environment (if in env)
|
|
|
|
// and adds variable to flag set for parsing.
|
|
|
|
func (self DirectoryFlag) Apply(set *flag.FlagSet) {
|
2015-04-09 11:26:26 +03:00
|
|
|
eachName(self.Name, func(name string) {
|
2015-04-09 21:06:27 +03:00
|
|
|
set.Var(&self.Value, self.Name, self.Usage)
|
2015-04-09 11:26:26 +03:00
|
|
|
})
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
2017-02-27 00:21:51 +03:00
|
|
|
// BigFlag is a command line flag that accepts 256 bit big integers in decimal or
|
|
|
|
// hexadecimal syntax.
|
|
|
|
type BigFlag struct {
|
|
|
|
Name string
|
|
|
|
Value *big.Int
|
|
|
|
Usage string
|
|
|
|
}
|
|
|
|
|
|
|
|
// bigValue turns *big.Int into a flag.Value
|
|
|
|
type bigValue big.Int
|
|
|
|
|
|
|
|
func (b *bigValue) String() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return (*big.Int)(b).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bigValue) Set(s string) error {
|
|
|
|
int, ok := math.ParseBig256(s)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid integer syntax")
|
|
|
|
}
|
|
|
|
*b = (bigValue)(*int)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f BigFlag) GetName() string {
|
|
|
|
return f.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f BigFlag) String() string {
|
|
|
|
fmtString := "%s %v\t%v"
|
|
|
|
if f.Value != nil {
|
|
|
|
fmtString = "%s \"%v\"\t%v"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(fmtString, prefixedNames(f.Name), f.Value, f.Usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f BigFlag) Apply(set *flag.FlagSet) {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
set.Var((*bigValue)(f.Value), f.Name, f.Usage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GlobalBig returns the value of a BigFlag from the global flag set.
|
|
|
|
func GlobalBig(ctx *cli.Context, name string) *big.Int {
|
|
|
|
val := ctx.GlobalGeneric(name)
|
|
|
|
if val == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return (*big.Int)(val.(*bigValue))
|
|
|
|
}
|
|
|
|
|
2015-04-08 16:43:55 +03:00
|
|
|
func prefixFor(name string) (prefix string) {
|
2015-04-09 11:26:26 +03:00
|
|
|
if len(name) == 1 {
|
|
|
|
prefix = "-"
|
|
|
|
} else {
|
|
|
|
prefix = "--"
|
|
|
|
}
|
2015-04-08 16:43:55 +03:00
|
|
|
|
2015-04-09 11:26:26 +03:00
|
|
|
return
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func prefixedNames(fullName string) (prefixed string) {
|
2015-04-09 11:26:26 +03:00
|
|
|
parts := strings.Split(fullName, ",")
|
|
|
|
for i, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
prefixed += prefixFor(name) + name
|
|
|
|
if i < len(parts)-1 {
|
|
|
|
prefixed += ", "
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-10 21:33:17 +03:00
|
|
|
func (self DirectoryFlag) GetName() string {
|
2015-04-09 11:26:26 +03:00
|
|
|
return self.Name
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *DirectoryFlag) Set(value string) {
|
2015-04-09 11:26:26 +03:00
|
|
|
self.Value.Value = value
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expands a file path
|
|
|
|
// 1. replace tilde with users home dir
|
|
|
|
// 2. expands embedded environment variables
|
|
|
|
// 3. cleans the path, e.g. /a/b/../c -> /a/c
|
|
|
|
// Note, it has limitations, e.g. ~someuser/tmp will not be expanded
|
|
|
|
func expandPath(p string) string {
|
2015-04-09 11:26:26 +03:00
|
|
|
if strings.HasPrefix(p, "~/") || strings.HasPrefix(p, "~\\") {
|
2016-09-16 12:53:50 +03:00
|
|
|
if home := homeDir(); home != "" {
|
|
|
|
p = home + p[1:]
|
2015-04-09 11:26:26 +03:00
|
|
|
}
|
|
|
|
}
|
2015-08-06 17:18:32 +03:00
|
|
|
return path.Clean(os.ExpandEnv(p))
|
2015-04-08 16:43:55 +03:00
|
|
|
}
|
2016-09-16 12:53:50 +03:00
|
|
|
|
|
|
|
func homeDir() string {
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
|
|
return home
|
|
|
|
}
|
|
|
|
if usr, err := user.Current(); err == nil {
|
|
|
|
return usr.HomeDir
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|