common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package common
  2. import (
  3. "fmt"
  4. "math/big"
  5. "os"
  6. "os/user"
  7. "path"
  8. "path/filepath"
  9. "runtime"
  10. "time"
  11. "github.com/kardianos/osext"
  12. )
  13. // MakeName creates a node name that follows the ethereum convention
  14. // for such names. It adds the operation system name and Go runtime version
  15. // the name.
  16. func MakeName(name, version string) string {
  17. return fmt.Sprintf("%s/v%s/%s/%s", name, version, runtime.GOOS, runtime.Version())
  18. }
  19. func DefaultAssetPath() string {
  20. var assetPath string
  21. pwd, _ := os.Getwd()
  22. srcdir := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist")
  23. // If the current working directory is the go-ethereum dir
  24. // assume a debug build and use the source directory as
  25. // asset directory.
  26. if pwd == srcdir {
  27. assetPath = path.Join(pwd, "assets")
  28. } else {
  29. switch runtime.GOOS {
  30. case "darwin":
  31. // Get Binary Directory
  32. exedir, _ := osext.ExecutableFolder()
  33. assetPath = filepath.Join(exedir, "../Resources")
  34. case "linux":
  35. assetPath = "/usr/share/mist"
  36. case "windows":
  37. assetPath = "./assets"
  38. default:
  39. assetPath = "."
  40. }
  41. }
  42. // Check if the assetPath exists. If not, try the source directory
  43. // This happens when binary is run from outside cmd/mist directory
  44. if _, err := os.Stat(assetPath); os.IsNotExist(err) {
  45. assetPath = path.Join(srcdir, "assets")
  46. }
  47. return assetPath
  48. }
  49. func DefaultDataDir() string {
  50. usr, _ := user.Current()
  51. if runtime.GOOS == "darwin" {
  52. return path.Join(usr.HomeDir, "Library/Ethereum")
  53. } else if runtime.GOOS == "windows" {
  54. return path.Join(usr.HomeDir, "AppData/Roaming/Ethereum")
  55. } else {
  56. return path.Join(usr.HomeDir, ".ethereum")
  57. }
  58. }
  59. func ToHex(b []byte) string {
  60. hex := Bytes2Hex(b)
  61. // Prefer output of "0x0" instead of "0x"
  62. if len(hex) == 0 {
  63. hex = "0"
  64. }
  65. return "0x" + hex
  66. }
  67. func FromHex(s string) []byte {
  68. if len(s) > 1 {
  69. if s[0:2] == "0x" {
  70. s = s[2:]
  71. }
  72. if len(s)%2 == 1 {
  73. s = "0" + s
  74. }
  75. return Hex2Bytes(s)
  76. }
  77. return nil
  78. }
  79. func IsWindows() bool {
  80. return runtime.GOOS == "windows"
  81. }
  82. func WindonizePath(path string) string {
  83. if string(path[0]) == "/" && IsWindows() {
  84. path = path[1:]
  85. }
  86. return path
  87. }
  88. // The different number of units
  89. var (
  90. Douglas = BigPow(10, 42)
  91. Einstein = BigPow(10, 21)
  92. Ether = BigPow(10, 18)
  93. Finney = BigPow(10, 15)
  94. Szabo = BigPow(10, 12)
  95. Shannon = BigPow(10, 9)
  96. Babbage = BigPow(10, 6)
  97. Ada = BigPow(10, 3)
  98. Wei = big.NewInt(1)
  99. )
  100. //
  101. // Currency to string
  102. // Returns a string representing a human readable format
  103. func CurrencyToString(num *big.Int) string {
  104. var (
  105. fin *big.Int = num
  106. denom string = "Wei"
  107. )
  108. switch {
  109. case num.Cmp(Douglas) >= 0:
  110. fin = new(big.Int).Div(num, Douglas)
  111. denom = "Douglas"
  112. case num.Cmp(Einstein) >= 0:
  113. fin = new(big.Int).Div(num, Einstein)
  114. denom = "Einstein"
  115. case num.Cmp(Ether) >= 0:
  116. fin = new(big.Int).Div(num, Ether)
  117. denom = "Ether"
  118. case num.Cmp(Finney) >= 0:
  119. fin = new(big.Int).Div(num, Finney)
  120. denom = "Finney"
  121. case num.Cmp(Szabo) >= 0:
  122. fin = new(big.Int).Div(num, Szabo)
  123. denom = "Szabo"
  124. case num.Cmp(Shannon) >= 0:
  125. fin = new(big.Int).Div(num, Shannon)
  126. denom = "Shannon"
  127. case num.Cmp(Babbage) >= 0:
  128. fin = new(big.Int).Div(num, Babbage)
  129. denom = "Babbage"
  130. case num.Cmp(Ada) >= 0:
  131. fin = new(big.Int).Div(num, Ada)
  132. denom = "Ada"
  133. }
  134. // TODO add comment clarifying expected behavior
  135. if len(fin.String()) > 5 {
  136. return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
  137. }
  138. return fmt.Sprintf("%v %s", fin, denom)
  139. }
  140. // Common big integers often used
  141. var (
  142. Big1 = big.NewInt(1)
  143. Big2 = big.NewInt(2)
  144. Big3 = big.NewInt(3)
  145. Big0 = big.NewInt(0)
  146. BigTrue = Big1
  147. BigFalse = Big0
  148. Big32 = big.NewInt(32)
  149. Big256 = big.NewInt(0xff)
  150. Big257 = big.NewInt(257)
  151. )
  152. func Bench(pre string, cb func()) {
  153. start := time.Now()
  154. cb()
  155. fmt.Println(pre, ": took:", time.Since(start))
  156. }