common.go 3.5 KB

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