common.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 FromHex(s string) []byte {
  60. if len(s) > 1 {
  61. if s[0:2] == "0x" {
  62. s = s[2:]
  63. }
  64. if len(s)%2 == 1 {
  65. s = "0" + s
  66. }
  67. return Hex2Bytes(s)
  68. }
  69. return nil
  70. }
  71. func IsWindows() bool {
  72. return runtime.GOOS == "windows"
  73. }
  74. func WindonizePath(path string) string {
  75. if string(path[0]) == "/" && IsWindows() {
  76. path = path[1:]
  77. }
  78. return path
  79. }
  80. // The different number of units
  81. var (
  82. Douglas = BigPow(10, 42)
  83. Einstein = BigPow(10, 21)
  84. Ether = BigPow(10, 18)
  85. Finney = BigPow(10, 15)
  86. Szabo = BigPow(10, 12)
  87. Shannon = BigPow(10, 9)
  88. Babbage = BigPow(10, 6)
  89. Ada = BigPow(10, 3)
  90. Wei = big.NewInt(1)
  91. )
  92. //
  93. // Currency to string
  94. // Returns a string representing a human readable format
  95. func CurrencyToString(num *big.Int) string {
  96. var (
  97. fin *big.Int = num
  98. denom string = "Wei"
  99. )
  100. switch {
  101. case num.Cmp(Douglas) >= 0:
  102. fin = new(big.Int).Div(num, Douglas)
  103. denom = "Douglas"
  104. case num.Cmp(Einstein) >= 0:
  105. fin = new(big.Int).Div(num, Einstein)
  106. denom = "Einstein"
  107. case num.Cmp(Ether) >= 0:
  108. fin = new(big.Int).Div(num, Ether)
  109. denom = "Ether"
  110. case num.Cmp(Finney) >= 0:
  111. fin = new(big.Int).Div(num, Finney)
  112. denom = "Finney"
  113. case num.Cmp(Szabo) >= 0:
  114. fin = new(big.Int).Div(num, Szabo)
  115. denom = "Szabo"
  116. case num.Cmp(Shannon) >= 0:
  117. fin = new(big.Int).Div(num, Shannon)
  118. denom = "Shannon"
  119. case num.Cmp(Babbage) >= 0:
  120. fin = new(big.Int).Div(num, Babbage)
  121. denom = "Babbage"
  122. case num.Cmp(Ada) >= 0:
  123. fin = new(big.Int).Div(num, Ada)
  124. denom = "Ada"
  125. }
  126. // TODO add comment clarifying expected behavior
  127. if len(fin.String()) > 5 {
  128. return fmt.Sprintf("%sE%d %s", fin.String()[0:5], len(fin.String())-5, denom)
  129. }
  130. return fmt.Sprintf("%v %s", fin, denom)
  131. }
  132. // Common big integers often used
  133. var (
  134. Big1 = big.NewInt(1)
  135. Big2 = big.NewInt(2)
  136. Big3 = big.NewInt(3)
  137. Big0 = big.NewInt(0)
  138. BigTrue = Big1
  139. BigFalse = Big0
  140. Big32 = big.NewInt(32)
  141. Big256 = big.NewInt(0xff)
  142. Big257 = big.NewInt(257)
  143. )
  144. func Bench(pre string, cb func()) {
  145. start := time.Now()
  146. cb()
  147. fmt.Println(pre, ": took:", time.Since(start))
  148. }