common.go 3.82 KB
Newer Older
obscuren's avatar
obscuren committed
1
package common
obscuren's avatar
obscuren committed
2 3 4 5

import (
	"fmt"
	"math/big"
Taylor Gerring's avatar
Taylor Gerring committed
6
	"os"
Maran's avatar
Maran committed
7 8
	"os/user"
	"path"
Taylor Gerring's avatar
Taylor Gerring committed
9
	"path/filepath"
Maran's avatar
Maran committed
10
	"runtime"
11
	"time"
Taylor Gerring's avatar
Taylor Gerring committed
12 13

	"github.com/kardianos/osext"
obscuren's avatar
obscuren committed
14 15
)

16 17 18 19 20 21 22
// MakeName creates a node name that follows the ethereum convention
// for such names. It adds the operation system name and Go runtime version
// the name.
func MakeName(name, version string) string {
	return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
}

Taylor Gerring's avatar
Taylor Gerring committed
23 24
func DefaultAssetPath() string {
	var assetPath string
25 26 27
	pwd, _ := os.Getwd()
	srcdir := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")

Taylor Gerring's avatar
Taylor Gerring committed
28 29 30
	// If the current working directory is the go-ethereum dir
	// assume a debug build and use the source directory as
	// asset directory.
31
	if pwd == srcdir {
Taylor Gerring's avatar
Taylor Gerring committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
		assetPath = path.Join(pwd, "assets")
	} else {
		switch runtime.GOOS {
		case "darwin":
			// Get Binary Directory
			exedir, _ := osext.ExecutableFolder()
			assetPath = filepath.Join(exedir, "../Resources")
		case "linux":
			assetPath = "/usr/share/mist"
		case "windows":
			assetPath = "./assets"
		default:
			assetPath = "."
		}
46
	}
47

48 49 50 51
	// Check if the assetPath exists. If not, try the source directory
	// This happens when binary is run from outside cmd/mist directory
	if _, err := os.Stat(assetPath); os.IsNotExist(err) {
		assetPath = path.Join(srcdir, "assets")
Taylor Gerring's avatar
Taylor Gerring committed
52
	}
53

Taylor Gerring's avatar
Taylor Gerring committed
54 55 56
	return assetPath
}

Maran's avatar
Maran committed
57 58 59 60 61 62 63 64 65 66
func DefaultDataDir() string {
	usr, _ := user.Current()
	if runtime.GOOS == "darwin" {
		return path.Join(usr.HomeDir, "Library/Ethereum")
	} else if runtime.GOOS == "windows" {
		return path.Join(usr.HomeDir, "AppData/Roaming/Ethereum")
	} else {
		return path.Join(usr.HomeDir, ".ethereum")
	}
}
67

68 69 70 71 72 73 74 75 76
func ToHex(b []byte) string {
	hex := Bytes2Hex(b)
	// Prefer output of "0x0" instead of "0x"
	if len(hex) == 0 {
		hex = "0"
	}
	return "0x" + hex
}

77 78 79 80 81 82 83 84 85 86 87 88
func FromHex(s string) []byte {
	if len(s) > 1 {
		if s[0:2] == "0x" {
			s = s[2:]
		}
		if len(s)%2 == 1 {
			s = "0" + s
		}
		return Hex2Bytes(s)
	}
	return nil
}
Maran's avatar
Maran committed
89 90 91 92 93 94 95 96 97 98 99
func IsWindows() bool {
	return runtime.GOOS == "windows"
}

func WindonizePath(path string) string {
	if string(path[0]) == "/" && IsWindows() {
		path = path[1:]
	}
	return path
}

100
// The different number of units
obscuren's avatar
obscuren committed
101
var (
obscuren's avatar
obscuren committed
102 103 104 105 106 107 108 109 110
	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)
obscuren's avatar
obscuren committed
111 112
)

113
//
Maran's avatar
Maran committed
114
// Currency to string
115
// Returns a string representing a human readable format
obscuren's avatar
obscuren committed
116
func CurrencyToString(num *big.Int) string {
obscuren's avatar
obscuren committed
117 118 119 120 121
	var (
		fin   *big.Int = num
		denom string   = "Wei"
	)

obscuren's avatar
obscuren committed
122
	switch {
obscuren's avatar
obscuren committed
123
	case num.Cmp(Douglas) >= 0:
obscuren's avatar
obscuren committed
124 125
		fin = new(big.Int).Div(num, Douglas)
		denom = "Douglas"
obscuren's avatar
obscuren committed
126
	case num.Cmp(Einstein) >= 0:
obscuren's avatar
obscuren committed
127 128
		fin = new(big.Int).Div(num, Einstein)
		denom = "Einstein"
obscuren's avatar
obscuren committed
129
	case num.Cmp(Ether) >= 0:
obscuren's avatar
obscuren committed
130 131
		fin = new(big.Int).Div(num, Ether)
		denom = "Ether"
obscuren's avatar
obscuren committed
132
	case num.Cmp(Finney) >= 0:
obscuren's avatar
obscuren committed
133 134
		fin = new(big.Int).Div(num, Finney)
		denom = "Finney"
obscuren's avatar
obscuren committed
135
	case num.Cmp(Szabo) >= 0:
obscuren's avatar
obscuren committed
136 137
		fin = new(big.Int).Div(num, Szabo)
		denom = "Szabo"
Maran's avatar
Maran committed
138
	case num.Cmp(Shannon) >= 0:
obscuren's avatar
obscuren committed
139 140
		fin = new(big.Int).Div(num, Shannon)
		denom = "Shannon"
Maran's avatar
Maran committed
141
	case num.Cmp(Babbage) >= 0:
obscuren's avatar
obscuren committed
142 143
		fin = new(big.Int).Div(num, Babbage)
		denom = "Babbage"
Maran's avatar
Maran committed
144
	case num.Cmp(Ada) >= 0:
obscuren's avatar
obscuren committed
145 146 147 148
		fin = new(big.Int).Div(num, Ada)
		denom = "Ada"
	}

149
	// TODO add comment clarifying expected behavior
obscuren's avatar
obscuren committed
150 151
	if len(fin.String()) > 5 {
		return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
obscuren's avatar
obscuren committed
152 153
	}

obscuren's avatar
obscuren committed
154
	return fmt.Sprintf("%v %s", fin, denom)
obscuren's avatar
obscuren committed
155
}
156

157
// Common big integers often used
158
var (
159 160
	Big1     = big.NewInt(1)
	Big2     = big.NewInt(2)
obscuren's avatar
obscuren committed
161
	Big3     = big.NewInt(3)
162 163 164 165 166
	Big0     = big.NewInt(0)
	BigTrue  = Big1
	BigFalse = Big0
	Big32    = big.NewInt(32)
	Big256   = big.NewInt(0xff)
obscuren's avatar
obscuren committed
167
	Big257   = big.NewInt(257)
168
)
169 170 171 172 173 174

func Bench(pre string, cb func()) {
	start := time.Now()
	cb()
	fmt.Println(pre, ": took:", time.Since(start))
}