quorum/ethereum/javascript_console.go

189 lines
3.8 KiB
Go
Raw Normal View History

2014-05-15 11:45:19 -07:00
package main
import (
"fmt"
"github.com/ethereum/eth-go"
2014-05-19 03:15:03 -07:00
"github.com/ethereum/eth-go/ethchain"
2014-05-15 11:45:19 -07:00
"github.com/ethereum/eth-go/ethpub"
2014-05-19 03:15:03 -07:00
"github.com/ethereum/eth-go/ethutil"
2014-05-15 11:45:19 -07:00
"github.com/robertkrimen/otto"
)
2014-05-17 06:15:46 -07:00
type Repl interface {
Start()
2014-05-19 07:32:45 -07:00
Stop()
2014-05-17 06:15:46 -07:00
}
type JSRE struct {
2014-05-19 03:15:03 -07:00
ethereum *eth.Ethereum
vm *otto.Otto
lib *ethpub.PEthereum
blockChan chan ethutil.React
changeChan chan ethutil.React
quitChan chan bool
objectCb map[string][]otto.Value
2014-05-15 11:45:19 -07:00
}
2014-05-17 06:15:46 -07:00
func NewJSRE(ethereum *eth.Ethereum) *JSRE {
2014-05-19 03:15:03 -07:00
re := &JSRE{
ethereum,
otto.New(),
ethpub.NewPEthereum(ethereum),
make(chan ethutil.React, 1),
make(chan ethutil.React, 1),
make(chan bool),
make(map[string][]otto.Value),
}
2014-05-19 07:32:45 -07:00
// Init the JS lib
re.vm.Run(jsLib)
2014-05-19 03:15:03 -07:00
// We have to make sure that, whoever calls this, calls "Stop"
go re.mainLoop()
2014-05-17 06:15:46 -07:00
re.Bind("eth", &JSEthereum{re.lib, re.vm})
2014-05-19 03:15:03 -07:00
t, _ := re.vm.Get("eth")
t.Object().Set("watch", func(call otto.FunctionCall) otto.Value {
addr, _ := call.Argument(0).ToString()
cb := call.Argument(1)
re.objectCb[addr] = append(re.objectCb[addr], cb)
event := "object:" + string(ethutil.FromHex(addr))
ethereum.Reactor().Subscribe(event, re.changeChan)
return otto.UndefinedValue()
})
2014-05-17 06:15:46 -07:00
return re
}
2014-05-19 03:15:03 -07:00
func (self *JSRE) Stop() {
// Kill the main loop
self.quitChan <- true
close(self.blockChan)
close(self.quitChan)
close(self.changeChan)
}
func (self *JSRE) mainLoop() {
// Subscribe to events
reactor := self.ethereum.Reactor()
reactor.Subscribe("newBlock", self.blockChan)
out:
for {
select {
case <-self.quitChan:
break out
case block := <-self.blockChan:
if _, ok := block.Resource.(*ethchain.Block); ok {
}
case object := <-self.changeChan:
if stateObject, ok := object.Resource.(*ethchain.StateObject); ok {
for _, cb := range self.objectCb[ethutil.Hex(stateObject.Address())] {
val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject))
cb.Call(cb, val)
}
} else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok {
fmt.Println(storageObject)
}
}
}
}
2014-05-17 06:15:46 -07:00
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)
2014-05-15 11:45:19 -07:00
}
2014-05-17 06:15:46 -07:00
type JSRepl struct {
re *JSRE
2014-05-19 04:04:31 -07:00
prompt string
2014-05-17 06:15:46 -07:00
}
func NewJSRepl(ethereum *eth.Ethereum) *JSRepl {
2014-05-19 04:04:31 -07:00
return &JSRepl{re: NewJSRE(ethereum), prompt: "> "}
2014-05-17 06:15:46 -07:00
}
2014-05-15 11:45:19 -07:00
2014-05-17 06:15:46 -07:00
func (self *JSRepl) Start() {
self.read()
2014-05-15 11:45:19 -07:00
}
2014-05-19 07:32:45 -07:00
func (self *JSRepl) Stop() {
self.re.Stop()
}
2014-05-17 06:15:46 -07:00
func (self *JSRepl) parseInput(code string) {
2014-05-15 13:15:14 -07:00
defer func() {
if r := recover(); r != nil {
fmt.Println("[native] error", r)
}
}()
2014-05-17 06:15:46 -07:00
value, err := self.re.Run(code)
2014-05-15 11:45:19 -07:00
if err != nil {
fmt.Println(err)
return
}
2014-05-19 07:32:45 -07:00
self.PrintValue(value)
2014-05-15 11:45:19 -07:00
}
2014-05-17 06:15:46 -07:00
// The JSEthereum object attempts to wrap the PEthereum object and returns
// meaningful javascript objects
type JSEthereum struct {
2014-05-15 13:15:14 -07:00
*ethpub.PEthereum
vm *otto.Otto
2014-05-15 11:45:19 -07:00
}
2014-05-17 06:15:46 -07:00
func (self *JSEthereum) GetKey() otto.Value {
2014-05-15 13:15:14 -07:00
return self.toVal(self.PEthereum.GetKey())
}
2014-05-17 06:15:46 -07:00
func (self *JSEthereum) GetStateObject(addr string) otto.Value {
2014-05-15 13:15:14 -07:00
return self.toVal(self.PEthereum.GetStateObject(addr))
}
2014-05-17 06:15:46 -07:00
func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value {
2014-05-15 13:15:14 -07:00
r, err := self.PEthereum.Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr)
2014-05-15 11:45:19 -07:00
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
2014-05-15 13:15:14 -07:00
return self.toVal(r)
}
2014-05-15 11:45:19 -07:00
2014-05-17 06:15:46 -07:00
func (self *JSEthereum) Create(key, valueStr, gasStr, gasPriceStr, initStr, bodyStr string) otto.Value {
2014-05-15 13:15:14 -07:00
r, err := self.PEthereum.Create(key, valueStr, gasStr, gasPriceStr, initStr, bodyStr)
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
return self.toVal(r)
2014-05-15 11:45:19 -07:00
}
2014-05-17 06:15:46 -07:00
func (self *JSEthereum) toVal(v interface{}) otto.Value {
2014-05-15 13:15:14 -07:00
result, err := self.vm.ToValue(v)
2014-05-15 11:45:19 -07:00
2014-05-15 13:15:14 -07:00
if err != nil {
fmt.Println(err)
return otto.UndefinedValue()
}
return result
2014-05-15 11:45:19 -07:00
}