mirror of https://github.com/ethereum/go-ethereum
Merge pull request #485 from ethersphere/frontier/nodeadmin.js
Frontier console node admin interfacepull/522/head
commit
786a58d8b0
@ -0,0 +1,259 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"net/http" |
||||
"os" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/cmd/utils" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
"github.com/ethereum/go-ethereum/state" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
"github.com/obscuren/otto" |
||||
) |
||||
|
||||
/* |
||||
node admin bindings |
||||
*/ |
||||
|
||||
func (js *jsre) adminBindings() { |
||||
js.re.Set("admin", struct{}{}) |
||||
t, _ := js.re.Get("admin") |
||||
admin := t.Object() |
||||
admin.Set("suggestPeer", js.suggestPeer) |
||||
admin.Set("startRPC", js.startRPC) |
||||
admin.Set("startMining", js.startMining) |
||||
admin.Set("stopMining", js.stopMining) |
||||
admin.Set("nodeInfo", js.nodeInfo) |
||||
admin.Set("peers", js.peers) |
||||
admin.Set("newAccount", js.newAccount) |
||||
admin.Set("unlock", js.unlock) |
||||
admin.Set("import", js.importChain) |
||||
admin.Set("export", js.exportChain) |
||||
admin.Set("dumpBlock", js.dumpBlock) |
||||
} |
||||
|
||||
func (js *jsre) startMining(call otto.FunctionCall) otto.Value { |
||||
_, err := call.Argument(0).ToInteger() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
// threads now ignored
|
||||
err = js.ethereum.StartMining() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) stopMining(call otto.FunctionCall) otto.Value { |
||||
js.ethereum.StopMining() |
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) startRPC(call otto.FunctionCall) otto.Value { |
||||
addr, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
port, err := call.Argument(1).ToInteger() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
dataDir := js.ethereum.DataDir |
||||
|
||||
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", addr, port)) |
||||
if err != nil { |
||||
fmt.Printf("Can't listen on %s:%d: %v", addr, port, err) |
||||
return otto.FalseValue() |
||||
} |
||||
go http.Serve(l, rpc.JSONRPC(xeth.New(js.ethereum, nil), dataDir)) |
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) suggestPeer(call otto.FunctionCall) otto.Value { |
||||
nodeURL, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
err = js.ethereum.SuggestPeer(nodeURL) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) unlock(call otto.FunctionCall) otto.Value { |
||||
addr, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
seconds, err := call.Argument(2).ToInteger() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
arg := call.Argument(1) |
||||
var passphrase string |
||||
if arg.IsUndefined() { |
||||
fmt.Println("Please enter a passphrase now.") |
||||
passphrase, err = readPassword("Passphrase: ", true) |
||||
if err != nil { |
||||
utils.Fatalf("%v", err) |
||||
} |
||||
} else { |
||||
passphrase, err = arg.ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
} |
||||
am := js.ethereum.AccountManager() |
||||
// err := am.Unlock(common.FromHex(split[0]), split[1])
|
||||
// if err != nil {
|
||||
// utils.Fatalf("Unlock account failed '%v'", err)
|
||||
// }
|
||||
err = am.TimedUnlock(common.FromHex(addr), passphrase, time.Duration(seconds)*time.Second) |
||||
if err != nil { |
||||
fmt.Printf("Unlock account failed '%v'\n", err) |
||||
return otto.FalseValue() |
||||
} |
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) newAccount(call otto.FunctionCall) otto.Value { |
||||
arg := call.Argument(0) |
||||
var passphrase string |
||||
if arg.IsUndefined() { |
||||
fmt.Println("The new account will be encrypted with a passphrase.") |
||||
fmt.Println("Please enter a passphrase now.") |
||||
auth, err := readPassword("Passphrase: ", true) |
||||
if err != nil { |
||||
utils.Fatalf("%v", err) |
||||
} |
||||
confirm, err := readPassword("Repeat Passphrase: ", false) |
||||
if err != nil { |
||||
utils.Fatalf("%v", err) |
||||
} |
||||
if auth != confirm { |
||||
utils.Fatalf("Passphrases did not match.") |
||||
} |
||||
passphrase = auth |
||||
} else { |
||||
var err error |
||||
passphrase, err = arg.ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
} |
||||
acct, err := js.ethereum.AccountManager().NewAccount(passphrase) |
||||
if err != nil { |
||||
fmt.Printf("Could not create the account: %v", err) |
||||
return otto.UndefinedValue() |
||||
} |
||||
return js.re.ToVal(common.Bytes2Hex(acct.Address)) |
||||
} |
||||
|
||||
func (js *jsre) nodeInfo(call otto.FunctionCall) otto.Value { |
||||
return js.re.ToVal(js.ethereum.NodeInfo()) |
||||
} |
||||
|
||||
func (js *jsre) peers(call otto.FunctionCall) otto.Value { |
||||
return js.re.ToVal(js.ethereum.PeersInfo()) |
||||
} |
||||
|
||||
func (js *jsre) importChain(call otto.FunctionCall) otto.Value { |
||||
if len(call.ArgumentList) == 0 { |
||||
fmt.Println("err: require file name") |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
fn, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
var fh *os.File |
||||
fh, err = os.OpenFile(fn, os.O_RDONLY, os.ModePerm) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
defer fh.Close() |
||||
|
||||
var blocks types.Blocks |
||||
if err = rlp.Decode(fh, &blocks); err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
js.ethereum.ChainManager().Reset() |
||||
if err = js.ethereum.ChainManager().InsertChain(blocks); err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) exportChain(call otto.FunctionCall) otto.Value { |
||||
if len(call.ArgumentList) == 0 { |
||||
fmt.Println("err: require file name") |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
fn, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
data := js.ethereum.ChainManager().Export() |
||||
if err := common.WriteFile(fn, data); err != nil { |
||||
fmt.Println(err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (js *jsre) dumpBlock(call otto.FunctionCall) otto.Value { |
||||
var block *types.Block |
||||
if len(call.ArgumentList) > 0 { |
||||
if call.Argument(0).IsNumber() { |
||||
num, _ := call.Argument(0).ToInteger() |
||||
block = js.ethereum.ChainManager().GetBlockByNumber(uint64(num)) |
||||
} else if call.Argument(0).IsString() { |
||||
hash, _ := call.Argument(0).ToString() |
||||
block = js.ethereum.ChainManager().GetBlock(common.Hex2Bytes(hash)) |
||||
} else { |
||||
fmt.Println("invalid argument for dump. Either hex string or number") |
||||
} |
||||
|
||||
} else { |
||||
block = js.ethereum.ChainManager().CurrentBlock() |
||||
} |
||||
if block == nil { |
||||
fmt.Println("block not found") |
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
statedb := state.New(block.Root(), js.ethereum.StateDb()) |
||||
dump := statedb.RawDump() |
||||
return js.re.ToVal(dump) |
||||
|
||||
} |
@ -0,0 +1,252 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"github.com/obscuren/otto" |
||||
"os" |
||||
"path" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/accounts" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/eth" |
||||
) |
||||
|
||||
var port = 30300 |
||||
|
||||
func testJEthRE(t *testing.T) (repl *jsre, ethereum *eth.Ethereum, err error) { |
||||
os.RemoveAll("/tmp/eth/") |
||||
err = os.MkdirAll("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/", os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("%v", err) |
||||
return |
||||
} |
||||
err = os.MkdirAll("/tmp/eth/data", os.ModePerm) |
||||
if err != nil { |
||||
t.Errorf("%v", err) |
||||
return |
||||
} |
||||
// FIXME: this does not work ATM
|
||||
ks := crypto.NewKeyStorePlain("/tmp/eth/keys") |
||||
common.WriteFile("/tmp/eth/keys/e273f01c99144c438695e10f24926dc1f9fbf62d/e273f01c99144c438695e10f24926dc1f9fbf62d", |
||||
[]byte(`{"Id":"RhRXD+fNRKS4jx+7ZfEsNA==","Address":"4nPwHJkUTEOGleEPJJJtwfn79i0=","PrivateKey":"h4ACVpe74uIvi5Cg/2tX/Yrm2xdr3J7QoMbMtNX2CNc="}`)) |
||||
|
||||
port++ |
||||
ethereum, err = eth.New(ð.Config{ |
||||
DataDir: "/tmp/eth", |
||||
AccountManager: accounts.NewManager(ks), |
||||
Port: fmt.Sprintf("%d", port), |
||||
MaxPeers: 10, |
||||
Name: "test", |
||||
}) |
||||
|
||||
if err != nil { |
||||
t.Errorf("%v", err) |
||||
return |
||||
} |
||||
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") |
||||
repl = newJSRE(ethereum, assetPath) |
||||
return |
||||
} |
||||
|
||||
func TestNodeInfo(t *testing.T) { |
||||
repl, ethereum, err := testJEthRE(t) |
||||
if err != nil { |
||||
t.Errorf("error creating jsre, got %v", err) |
||||
return |
||||
} |
||||
err = ethereum.Start() |
||||
if err != nil { |
||||
t.Errorf("error starting ethereum: %v", err) |
||||
return |
||||
} |
||||
defer ethereum.Stop() |
||||
|
||||
val, err := repl.re.Run("admin.nodeInfo()") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
exp, err := val.Export() |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
nodeInfo, ok := exp.(*eth.NodeInfo) |
||||
if !ok { |
||||
t.Errorf("expected nodeInfo, got %v", err) |
||||
} |
||||
exp = "test" |
||||
got := nodeInfo.Name |
||||
if exp != got { |
||||
t.Errorf("expected %v, got %v", exp, got) |
||||
} |
||||
exp = 30301 |
||||
port := nodeInfo.DiscPort |
||||
if exp != port { |
||||
t.Errorf("expected %v, got %v", exp, port) |
||||
} |
||||
exp = 30301 |
||||
port = nodeInfo.TCPPort |
||||
if exp != port { |
||||
t.Errorf("expected %v, got %v", exp, port) |
||||
} |
||||
} |
||||
|
||||
func TestAccounts(t *testing.T) { |
||||
repl, ethereum, err := testJEthRE(t) |
||||
if err != nil { |
||||
t.Errorf("error creating jsre, got %v", err) |
||||
return |
||||
} |
||||
err = ethereum.Start() |
||||
if err != nil { |
||||
t.Errorf("error starting ethereum: %v", err) |
||||
return |
||||
} |
||||
defer ethereum.Stop() |
||||
|
||||
val, err := repl.re.Run("eth.coinbase") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
|
||||
pp, err := repl.re.PrettyPrint(val) |
||||
if err != nil { |
||||
t.Errorf("%v", err) |
||||
} |
||||
|
||||
if !val.IsString() { |
||||
t.Errorf("incorrect type, expected string, got %v: %v", val, pp) |
||||
} |
||||
strVal, _ := val.ToString() |
||||
expected := "0xe273f01c99144c438695e10f24926dc1f9fbf62d" |
||||
if strVal != expected { |
||||
t.Errorf("incorrect result, expected %s, got %v", expected, strVal) |
||||
} |
||||
|
||||
val, err = repl.re.Run(`admin.newAccount("password")`) |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
addr, err := val.ToString() |
||||
if err != nil { |
||||
t.Errorf("expected string, got %v", err) |
||||
} |
||||
|
||||
val, err = repl.re.Run("eth.accounts") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
exp, err := val.Export() |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
addrs, ok := exp.([]string) |
||||
if !ok { |
||||
t.Errorf("expected []string, got %v", err) |
||||
} |
||||
if len(addrs) != 2 || (addr != addrs[0][2:] && addr != addrs[1][2:]) { |
||||
t.Errorf("expected addrs == [<default>, <new>], got %v (%v)", addrs, addr) |
||||
} |
||||
|
||||
} |
||||
|
||||
func TestBlockChain(t *testing.T) { |
||||
repl, ethereum, err := testJEthRE(t) |
||||
if err != nil { |
||||
t.Errorf("error creating jsre, got %v", err) |
||||
return |
||||
} |
||||
err = ethereum.Start() |
||||
if err != nil { |
||||
t.Errorf("error starting ethereum: %v", err) |
||||
return |
||||
} |
||||
defer ethereum.Stop() |
||||
|
||||
// should get current block
|
||||
val0, err := repl.re.Run("admin.dumpBlock()") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
|
||||
fn := "/tmp/eth/data/blockchain.0" |
||||
_, err = repl.re.Run("admin.export(\"" + fn + "\")") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
if _, err = os.Stat(fn); err != nil { |
||||
t.Errorf("expected no error on file, got %v", err) |
||||
} |
||||
|
||||
_, err = repl.re.Run("admin.import(\"" + fn + "\")") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
|
||||
var val1 otto.Value |
||||
|
||||
// should get current block
|
||||
val1, err = repl.re.Run("admin.dumpBlock()") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
|
||||
// FIXME: neither != , nor reflect.DeepEqual works, doing string comparison
|
||||
v0 := fmt.Sprintf("%v", val0) |
||||
v1 := fmt.Sprintf("%v", val1) |
||||
if v0 != v1 { |
||||
t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0) |
||||
} |
||||
} |
||||
|
||||
func TestMining(t *testing.T) { |
||||
repl, ethereum, err := testJEthRE(t) |
||||
if err != nil { |
||||
t.Errorf("error creating jsre, got %v", err) |
||||
return |
||||
} |
||||
err = ethereum.Start() |
||||
if err != nil { |
||||
t.Errorf("error starting ethereum: %v", err) |
||||
return |
||||
} |
||||
defer ethereum.Stop() |
||||
|
||||
val, err := repl.re.Run("eth.mining") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
var mining bool |
||||
mining, err = val.ToBoolean() |
||||
if err != nil { |
||||
t.Errorf("expected boolean, got %v", err) |
||||
} |
||||
if mining { |
||||
t.Errorf("expected false (not mining), got true") |
||||
} |
||||
|
||||
} |
||||
|
||||
func TestRPC(t *testing.T) { |
||||
repl, ethereum, err := testJEthRE(t) |
||||
if err != nil { |
||||
t.Errorf("error creating jsre, got %v", err) |
||||
return |
||||
} |
||||
err = ethereum.Start() |
||||
if err != nil { |
||||
t.Errorf("error starting ethereum: %v", err) |
||||
return |
||||
} |
||||
defer ethereum.Stop() |
||||
|
||||
val, err := repl.re.Run(`admin.startRPC("127.0.0.1", 5004)`) |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
success, _ := val.ToBoolean() |
||||
if !success { |
||||
t.Errorf("expected true (started), got false") |
||||
} |
||||
} |
File diff suppressed because one or more lines are too long
@ -1,103 +0,0 @@ |
||||
package javascript |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"os" |
||||
"path" |
||||
"path/filepath" |
||||
|
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
"github.com/obscuren/otto" |
||||
) |
||||
|
||||
var jsrelogger = logger.NewLogger("JSRE") |
||||
|
||||
type JSRE struct { |
||||
Vm *otto.Otto |
||||
xeth *xeth.XEth |
||||
|
||||
objectCb map[string][]otto.Value |
||||
} |
||||
|
||||
func (jsre *JSRE) LoadExtFile(path string) { |
||||
result, err := ioutil.ReadFile(path) |
||||
if err == nil { |
||||
jsre.Vm.Run(result) |
||||
} else { |
||||
jsrelogger.Infoln("Could not load file:", path) |
||||
} |
||||
} |
||||
|
||||
func (jsre *JSRE) LoadIntFile(file string) { |
||||
assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") |
||||
jsre.LoadExtFile(path.Join(assetPath, file)) |
||||
} |
||||
|
||||
func NewJSRE(xeth *xeth.XEth) *JSRE { |
||||
re := &JSRE{ |
||||
otto.New(), |
||||
xeth, |
||||
make(map[string][]otto.Value), |
||||
} |
||||
|
||||
// Init the JS lib
|
||||
re.Vm.Run(jsLib) |
||||
|
||||
// Load extra javascript files
|
||||
re.LoadIntFile("bignumber.min.js") |
||||
|
||||
re.Bind("eth", &JSEthereum{re.xeth, re.Vm}) |
||||
|
||||
re.initStdFuncs() |
||||
|
||||
jsrelogger.Infoln("started") |
||||
|
||||
return re |
||||
} |
||||
|
||||
func (self *JSRE) Bind(name string, v interface{}) { |
||||
self.Vm.Set(name, v) |
||||
} |
||||
|
||||
func (self *JSRE) Run(code string) (otto.Value, error) { |
||||
return self.Vm.Run(code) |
||||
} |
||||
|
||||
func (self *JSRE) initStdFuncs() { |
||||
t, _ := self.Vm.Get("eth") |
||||
eth := t.Object() |
||||
eth.Set("require", self.require) |
||||
} |
||||
|
||||
func (self *JSRE) Require(file string) error { |
||||
if len(filepath.Ext(file)) == 0 { |
||||
file += ".js" |
||||
} |
||||
|
||||
fh, err := os.Open(file) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
content, _ := ioutil.ReadAll(fh) |
||||
self.Run("exports = {};(function() {" + string(content) + "})();") |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (self *JSRE) require(call otto.FunctionCall) otto.Value { |
||||
file, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
return otto.UndefinedValue() |
||||
} |
||||
if err := self.Require(file); err != nil { |
||||
fmt.Println("err:", err) |
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
t, _ := self.Vm.Get("exports") |
||||
|
||||
return t |
||||
} |
@ -1,94 +0,0 @@ |
||||
package javascript |
||||
|
||||
import ( |
||||
"fmt" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/state" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
"github.com/obscuren/otto" |
||||
) |
||||
|
||||
type JSStateObject struct { |
||||
*xeth.Object |
||||
eth *JSEthereum |
||||
} |
||||
|
||||
func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value { |
||||
cb := call.Argument(0) |
||||
|
||||
it := self.Object.Trie().Iterator() |
||||
for it.Next() { |
||||
cb.Call(self.eth.toVal(self), self.eth.toVal(common.Bytes2Hex(it.Key)), self.eth.toVal(common.Bytes2Hex(it.Value))) |
||||
} |
||||
|
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
// The JSEthereum object attempts to wrap the PEthereum object and returns
|
||||
// meaningful javascript objects
|
||||
type JSBlock struct { |
||||
*xeth.Block |
||||
eth *JSEthereum |
||||
} |
||||
|
||||
func (self *JSBlock) GetTransaction(hash string) otto.Value { |
||||
return self.eth.toVal(self.Block.GetTransaction(hash)) |
||||
} |
||||
|
||||
type JSLog struct { |
||||
Address string `json:address` |
||||
Topics []string `json:topics` |
||||
Number int32 `json:number` |
||||
Data string `json:data` |
||||
} |
||||
|
||||
func NewJSLog(log state.Log) JSLog { |
||||
return JSLog{ |
||||
Address: common.Bytes2Hex(log.Address()), |
||||
Topics: nil, //common.Bytes2Hex(log.Address()),
|
||||
Number: 0, |
||||
Data: common.Bytes2Hex(log.Data()), |
||||
} |
||||
} |
||||
|
||||
type JSEthereum struct { |
||||
*xeth.XEth |
||||
vm *otto.Otto |
||||
} |
||||
|
||||
func (self *JSEthereum) Block(v interface{}) otto.Value { |
||||
if number, ok := v.(int64); ok { |
||||
return self.toVal(&JSBlock{self.XEth.BlockByNumber(number), self}) |
||||
} else if hash, ok := v.(string); ok { |
||||
return self.toVal(&JSBlock{self.XEth.BlockByHash(hash), self}) |
||||
} |
||||
|
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
func (self *JSEthereum) GetStateObject(addr string) otto.Value { |
||||
return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self}) |
||||
} |
||||
|
||||
func (self *JSEthereum) Transact(fromStr, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value { |
||||
r, err := self.XEth.Transact(fromStr, recipient, valueStr, gasStr, gasPriceStr, dataStr) |
||||
if err != nil { |
||||
fmt.Println(err) |
||||
|
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
return self.toVal(r) |
||||
} |
||||
|
||||
func (self *JSEthereum) toVal(v interface{}) otto.Value { |
||||
result, err := self.vm.ToValue(v) |
||||
|
||||
if err != nil { |
||||
fmt.Println("Value unknown:", err) |
||||
|
||||
return otto.UndefinedValue() |
||||
} |
||||
|
||||
return result |
||||
} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,115 @@ |
||||
package jsre |
||||
|
||||
import ( |
||||
"fmt" |
||||
"github.com/obscuren/otto" |
||||
"io/ioutil" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
/* |
||||
JSRE is a generic JS runtime environment embedding the otto JS interpreter. |
||||
It provides some helper functions to |
||||
- load code from files |
||||
- run code snippets |
||||
- require libraries |
||||
- bind native go objects |
||||
*/ |
||||
type JSRE struct { |
||||
assetPath string |
||||
vm *otto.Otto |
||||
} |
||||
|
||||
func New(assetPath string) *JSRE { |
||||
re := &JSRE{ |
||||
assetPath, |
||||
otto.New(), |
||||
} |
||||
|
||||
// load prettyprint func definition
|
||||
re.vm.Run(pp_js) |
||||
re.vm.Set("loadScript", re.loadScript) |
||||
|
||||
return re |
||||
} |
||||
|
||||
// Exec(file) loads and runs the contents of a file
|
||||
// if a relative path is given, the jsre's assetPath is used
|
||||
func (self *JSRE) Exec(file string) error { |
||||
return self.exec(common.AbsolutePath(self.assetPath, file)) |
||||
} |
||||
|
||||
func (self *JSRE) exec(path string) error { |
||||
code, err := ioutil.ReadFile(path) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
_, err = self.vm.Run(code) |
||||
return err |
||||
} |
||||
|
||||
func (self *JSRE) Bind(name string, v interface{}) (err error) { |
||||
self.vm.Set(name, v) |
||||
return |
||||
} |
||||
|
||||
func (self *JSRE) Run(code string) (otto.Value, error) { |
||||
return self.vm.Run(code) |
||||
} |
||||
|
||||
func (self *JSRE) Get(ns string) (otto.Value, error) { |
||||
return self.vm.Get(ns) |
||||
} |
||||
|
||||
func (self *JSRE) Set(ns string, v interface{}) error { |
||||
return self.vm.Set(ns, v) |
||||
} |
||||
|
||||
func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value { |
||||
file, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
return otto.FalseValue() |
||||
} |
||||
if err := self.Exec(file); err != nil { |
||||
fmt.Println("err:", err) |
||||
return otto.FalseValue() |
||||
} |
||||
|
||||
return otto.TrueValue() |
||||
} |
||||
|
||||
func (self *JSRE) PrettyPrint(v interface{}) (val otto.Value, err error) { |
||||
var method otto.Value |
||||
v, err = self.vm.ToValue(v) |
||||
if err != nil { |
||||
return |
||||
} |
||||
method, err = self.vm.Get("prettyPrint") |
||||
if err != nil { |
||||
return |
||||
} |
||||
return method.Call(method, v) |
||||
} |
||||
|
||||
func (self *JSRE) ToVal(v interface{}) otto.Value { |
||||
result, err := self.vm.ToValue(v) |
||||
if err != nil { |
||||
fmt.Println("Value unknown:", err) |
||||
return otto.UndefinedValue() |
||||
} |
||||
return result |
||||
} |
||||
|
||||
func (self *JSRE) Eval(code string) (s string, err error) { |
||||
var val otto.Value |
||||
val, err = self.Run(code) |
||||
if err != nil { |
||||
return |
||||
} |
||||
val, err = self.PrettyPrint(val) |
||||
if err != nil { |
||||
return |
||||
} |
||||
return fmt.Sprintf("%v", val), nil |
||||
} |
@ -0,0 +1,84 @@ |
||||
package jsre |
||||
|
||||
import ( |
||||
"github.com/obscuren/otto" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
type testNativeObjectBinding struct { |
||||
toVal func(interface{}) otto.Value |
||||
} |
||||
|
||||
type msg struct { |
||||
Msg string |
||||
} |
||||
|
||||
func (no *testNativeObjectBinding) TestMethod(call otto.FunctionCall) otto.Value { |
||||
m, err := call.Argument(0).ToString() |
||||
if err != nil { |
||||
return otto.UndefinedValue() |
||||
} |
||||
return no.toVal(&msg{m}) |
||||
} |
||||
|
||||
func TestExec(t *testing.T) { |
||||
jsre := New("/tmp") |
||||
|
||||
common.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) |
||||
err := jsre.Exec("test.js") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
val, err := jsre.Run("msg") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
if !val.IsString() { |
||||
t.Errorf("expected string value, got %v", val) |
||||
} |
||||
exp := "testMsg" |
||||
got, _ := val.ToString() |
||||
if exp != got { |
||||
t.Errorf("expected '%v', got '%v'", exp, got) |
||||
} |
||||
} |
||||
|
||||
func TestBind(t *testing.T) { |
||||
jsre := New("/tmp") |
||||
|
||||
jsre.Bind("no", &testNativeObjectBinding{jsre.ToVal}) |
||||
|
||||
val, err := jsre.Run(`no.testMethod("testMsg")`) |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
pp, err := jsre.PrettyPrint(val) |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
t.Logf("no: %v", pp) |
||||
} |
||||
|
||||
func TestLoadScript(t *testing.T) { |
||||
jsre := New("/tmp") |
||||
|
||||
common.WriteFile("/tmp/test.js", []byte(`msg = "testMsg"`)) |
||||
_, err := jsre.Run(`loadScript("test.js")`) |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
val, err := jsre.Run("msg") |
||||
if err != nil { |
||||
t.Errorf("expected no error, got %v", err) |
||||
} |
||||
if !val.IsString() { |
||||
t.Errorf("expected string value, got %v", val) |
||||
} |
||||
exp := "testMsg" |
||||
got, _ := val.ToString() |
||||
if exp != got { |
||||
t.Errorf("expected '%v', got '%v'", exp, got) |
||||
} |
||||
} |
@ -0,0 +1,43 @@ |
||||
package rpc |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
// "fmt"
|
||||
"github.com/obscuren/otto" |
||||
) |
||||
|
||||
type Jeth struct { |
||||
ethApi *EthereumApi |
||||
toVal func(interface{}) otto.Value |
||||
} |
||||
|
||||
func NewJeth(ethApi *EthereumApi, toVal func(interface{}) otto.Value) *Jeth { |
||||
return &Jeth{ethApi, toVal} |
||||
} |
||||
|
||||
func (self *Jeth) err(code int, msg string, id interface{}) otto.Value { |
||||
rpcerr := &RpcErrorObject{code, msg} |
||||
rpcresponse := &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: rpcerr} |
||||
return self.toVal(rpcresponse) |
||||
} |
||||
|
||||
func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { |
||||
reqif, err := call.Argument(0).Export() |
||||
if err != nil { |
||||
return self.err(-32700, err.Error(), nil) |
||||
} |
||||
|
||||
jsonreq, err := json.Marshal(reqif) |
||||
|
||||
var req RpcRequest |
||||
err = json.Unmarshal(jsonreq, &req) |
||||
|
||||
var respif interface{} |
||||
err = self.ethApi.GetRequestReply(&req, &respif) |
||||
if err != nil { |
||||
return self.err(-32603, err.Error(), req.Id) |
||||
} |
||||
rpcresponse := &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: req.Id, Result: respif} |
||||
response = self.toVal(rpcresponse) |
||||
return |
||||
} |
Loading…
Reference in new issue