admin.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/ethereum/go-ethereum/cmd/utils"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core/state"
  9. "github.com/ethereum/go-ethereum/core/types"
  10. "github.com/ethereum/go-ethereum/logger/glog"
  11. "github.com/ethereum/go-ethereum/rlp"
  12. "github.com/ethereum/go-ethereum/rpc"
  13. "github.com/ethereum/go-ethereum/xeth"
  14. "github.com/robertkrimen/otto"
  15. )
  16. /*
  17. node admin bindings
  18. */
  19. func (js *jsre) adminBindings() {
  20. js.re.Set("admin", struct{}{})
  21. t, _ := js.re.Get("admin")
  22. admin := t.Object()
  23. admin.Set("suggestPeer", js.suggestPeer)
  24. admin.Set("startRPC", js.startRPC)
  25. admin.Set("nodeInfo", js.nodeInfo)
  26. admin.Set("peers", js.peers)
  27. admin.Set("newAccount", js.newAccount)
  28. admin.Set("unlock", js.unlock)
  29. admin.Set("import", js.importChain)
  30. admin.Set("export", js.exportChain)
  31. admin.Set("dumpBlock", js.dumpBlock)
  32. admin.Set("verbosity", js.verbosity)
  33. admin.Set("backtrace", js.backtrace)
  34. admin.Set("miner", struct{}{})
  35. t, _ = admin.Get("miner")
  36. miner := t.Object()
  37. miner.Set("start", js.startMining)
  38. miner.Set("stop", js.stopMining)
  39. miner.Set("hashrate", js.hashrate)
  40. miner.Set("setExtra", js.setExtra)
  41. }
  42. func (js *jsre) setExtra(call otto.FunctionCall) otto.Value {
  43. extra, err := call.Argument(0).ToString()
  44. if err != nil {
  45. fmt.Println(err)
  46. return otto.UndefinedValue()
  47. }
  48. if len(extra) > 1024 {
  49. fmt.Println("error: cannot exceed 1024 bytes")
  50. return otto.UndefinedValue()
  51. }
  52. js.ethereum.Miner().SetExtra([]byte(extra))
  53. return otto.UndefinedValue()
  54. }
  55. func (js *jsre) hashrate(otto.FunctionCall) otto.Value {
  56. return js.re.ToVal(js.ethereum.Miner().HashRate())
  57. }
  58. func (js *jsre) backtrace(call otto.FunctionCall) otto.Value {
  59. tracestr, err := call.Argument(0).ToString()
  60. if err != nil {
  61. fmt.Println(err)
  62. return otto.UndefinedValue()
  63. }
  64. glog.GetTraceLocation().Set(tracestr)
  65. return otto.UndefinedValue()
  66. }
  67. func (js *jsre) verbosity(call otto.FunctionCall) otto.Value {
  68. v, err := call.Argument(0).ToInteger()
  69. if err != nil {
  70. fmt.Println(err)
  71. return otto.UndefinedValue()
  72. }
  73. glog.SetV(int(v))
  74. return otto.UndefinedValue()
  75. }
  76. func (js *jsre) startMining(call otto.FunctionCall) otto.Value {
  77. _, err := call.Argument(0).ToInteger()
  78. if err != nil {
  79. fmt.Println(err)
  80. return otto.FalseValue()
  81. }
  82. // threads now ignored
  83. err = js.ethereum.StartMining()
  84. if err != nil {
  85. fmt.Println(err)
  86. return otto.FalseValue()
  87. }
  88. return otto.TrueValue()
  89. }
  90. func (js *jsre) stopMining(call otto.FunctionCall) otto.Value {
  91. js.ethereum.StopMining()
  92. return otto.TrueValue()
  93. }
  94. func (js *jsre) startRPC(call otto.FunctionCall) otto.Value {
  95. addr, err := call.Argument(0).ToString()
  96. if err != nil {
  97. fmt.Println(err)
  98. return otto.FalseValue()
  99. }
  100. port, err := call.Argument(1).ToInteger()
  101. if err != nil {
  102. fmt.Println(err)
  103. return otto.FalseValue()
  104. }
  105. config := rpc.RpcConfig{
  106. ListenAddress: addr,
  107. ListenPort: uint(port),
  108. // CorsDomain: ctx.GlobalString(RPCCORSDomainFlag.Name),
  109. }
  110. xeth := xeth.New(js.ethereum, nil)
  111. err = rpc.Start(xeth, config)
  112. if err != nil {
  113. fmt.Printf(err.Error())
  114. return otto.FalseValue()
  115. }
  116. return otto.TrueValue()
  117. }
  118. func (js *jsre) suggestPeer(call otto.FunctionCall) otto.Value {
  119. nodeURL, err := call.Argument(0).ToString()
  120. if err != nil {
  121. fmt.Println(err)
  122. return otto.FalseValue()
  123. }
  124. err = js.ethereum.SuggestPeer(nodeURL)
  125. if err != nil {
  126. fmt.Println(err)
  127. return otto.FalseValue()
  128. }
  129. return otto.TrueValue()
  130. }
  131. func (js *jsre) unlock(call otto.FunctionCall) otto.Value {
  132. addr, err := call.Argument(0).ToString()
  133. if err != nil {
  134. fmt.Println(err)
  135. return otto.FalseValue()
  136. }
  137. seconds, err := call.Argument(2).ToInteger()
  138. if err != nil {
  139. fmt.Println(err)
  140. return otto.FalseValue()
  141. }
  142. arg := call.Argument(1)
  143. var passphrase string
  144. if arg.IsUndefined() {
  145. fmt.Println("Please enter a passphrase now.")
  146. passphrase, err = readPassword("Passphrase: ", true)
  147. if err != nil {
  148. utils.Fatalf("%v", err)
  149. }
  150. } else {
  151. passphrase, err = arg.ToString()
  152. if err != nil {
  153. fmt.Println(err)
  154. return otto.FalseValue()
  155. }
  156. }
  157. am := js.ethereum.AccountManager()
  158. // err := am.Unlock(common.FromHex(split[0]), split[1])
  159. // if err != nil {
  160. // utils.Fatalf("Unlock account failed '%v'", err)
  161. // }
  162. err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second)
  163. if err != nil {
  164. fmt.Printf("Unlock account failed '%v'\n", err)
  165. return otto.FalseValue()
  166. }
  167. return otto.TrueValue()
  168. }
  169. func (js *jsre) newAccount(call otto.FunctionCall) otto.Value {
  170. arg := call.Argument(0)
  171. var passphrase string
  172. if arg.IsUndefined() {
  173. fmt.Println("The new account will be encrypted with a passphrase.")
  174. fmt.Println("Please enter a passphrase now.")
  175. auth, err := readPassword("Passphrase: ", true)
  176. if err != nil {
  177. utils.Fatalf("%v", err)
  178. }
  179. confirm, err := readPassword("Repeat Passphrase: ", false)
  180. if err != nil {
  181. utils.Fatalf("%v", err)
  182. }
  183. if auth != confirm {
  184. utils.Fatalf("Passphrases did not match.")
  185. }
  186. passphrase = auth
  187. } else {
  188. var err error
  189. passphrase, err = arg.ToString()
  190. if err != nil {
  191. fmt.Println(err)
  192. return otto.FalseValue()
  193. }
  194. }
  195. acct, err := js.ethereum.AccountManager().NewAccount(passphrase)
  196. if err != nil {
  197. fmt.Printf("Could not create the account: %v", err)
  198. return otto.UndefinedValue()
  199. }
  200. return js.re.ToVal(common.Bytes2Hex(acct.Address))
  201. }
  202. func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value {
  203. return js.re.ToVal(js.ethereum.NodeInfo())
  204. }
  205. func (js *jsre) peers(call otto.FunctionCall) otto.Value {
  206. return js.re.ToVal(js.ethereum.PeersInfo())
  207. }
  208. func (js *jsre) importChain(call otto.FunctionCall) otto.Value {
  209. if len(call.ArgumentList) == 0 {
  210. fmt.Println("err: require file name")
  211. return otto.FalseValue()
  212. }
  213. fn, err := call.Argument(0).ToString()
  214. if err != nil {
  215. fmt.Println(err)
  216. return otto.FalseValue()
  217. }
  218. var fh *os.File
  219. fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm)
  220. if err != nil {
  221. fmt.Println(err)
  222. return otto.FalseValue()
  223. }
  224. defer fh.Close()
  225. var blocks types.Blocks
  226. if err = rlp.Decode(fh, &blocks); err != nil {
  227. fmt.Println(err)
  228. return otto.FalseValue()
  229. }
  230. js.ethereum.ChainManager().Reset()
  231. if err = js.ethereum.ChainManager().InsertChain(blocks); err != nil {
  232. fmt.Println(err)
  233. return otto.FalseValue()
  234. }
  235. return otto.TrueValue()
  236. }
  237. func (js *jsre) exportChain(call otto.FunctionCall) otto.Value {
  238. if len(call.ArgumentList) == 0 {
  239. fmt.Println("err: require file name")
  240. return otto.FalseValue()
  241. }
  242. fn, err := call.Argument(0).ToString()
  243. if err != nil {
  244. fmt.Println(err)
  245. return otto.FalseValue()
  246. }
  247. if err := utils.ExportChain(js.ethereum.ChainManager(), fn); err != nil {
  248. fmt.Println(err)
  249. return otto.FalseValue()
  250. }
  251. return otto.TrueValue()
  252. }
  253. func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value {
  254. var block *types.Block
  255. if len(call.ArgumentList) > 0 {
  256. if call.Argument(0).IsNumber() {
  257. num, _ := call.Argument(0).ToInteger()
  258. block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num))
  259. } else if call.Argument(0).IsString() {
  260. hash, _ := call.Argument(0).ToString()
  261. block = js.ethereum.ChainManager().GetBlock(common.HexToHash(hash))
  262. } else {
  263. fmt.Println("invalid argument for dump. Either hex string or number")
  264. }
  265. } else {
  266. block = js.ethereum.ChainManager().CurrentBlock()
  267. }
  268. if block == nil {
  269. fmt.Println("block not found")
  270. return otto.UndefinedValue()
  271. }
  272. statedb := state.New(block.Root(), js.ethereum.StateDb())
  273. dump := statedb.RawDump()
  274. return js.re.ToVal(dump)
  275. }