common.go 3.2 KB

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