diff options
author | obscuren <geffobscura@gmail.com> | 2015-02-21 01:13:46 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-02-21 01:13:46 +0800 |
commit | bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca (patch) | |
tree | 46ab5943fd5e26198067aeec4a44287452eb2a32 /javascript | |
parent | 771bfe9e78f9952002a71cccc8d41c8c544fdfcb (diff) | |
parent | d586a633ff005ac01c9f1eb33552d147cf6c883e (diff) | |
download | dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.gz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.bz2 dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.lz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.xz dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.tar.zst dexon-bd7ebbcd5b77ce4fdd471b44f0acda80f2b3ceca.zip |
Merge branch 'release/0.9.0'
Diffstat (limited to 'javascript')
-rw-r--r-- | javascript/javascript_runtime.go | 25 | ||||
-rw-r--r-- | javascript/types.go | 98 |
2 files changed, 46 insertions, 77 deletions
diff --git a/javascript/javascript_runtime.go b/javascript/javascript_runtime.go index a26f0154e..0aa0f73e2 100644 --- a/javascript/javascript_runtime.go +++ b/javascript/javascript_runtime.go @@ -7,10 +7,10 @@ import ( "path" "path/filepath" - "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" @@ -24,7 +24,7 @@ var jsrelogger = logger.NewLogger("JSRE") type JSRE struct { ethereum *eth.Ethereum Vm *otto.Otto - pipe *xeth.JSXEth + pipe *xeth.XEth events event.Subscription @@ -49,7 +49,7 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re := &JSRE{ ethereum, otto.New(), - xeth.NewJSXEth(ethereum), + xeth.New(ethereum), nil, make(map[string][]otto.Value), } @@ -58,8 +58,7 @@ func NewJSRE(ethereum *eth.Ethereum) *JSRE { re.Vm.Run(jsLib) // Load extra javascript files - re.LoadIntFile("string.js") - re.LoadIntFile("big.js") + re.LoadIntFile("bignumber.min.js") // Subscribe to events mux := ethereum.EventMux() @@ -129,10 +128,9 @@ func (self *JSRE) initStdFuncs() { */ func (self *JSRE) dump(call otto.FunctionCall) otto.Value { - var state *state.StateDB + var block *types.Block if len(call.ArgumentList) > 0 { - var block *types.Block if call.Argument(0).IsNumber() { num, _ := call.Argument(0).ToInteger() block = self.ethereum.ChainManager().GetBlockByNumber(uint64(num)) @@ -149,12 +147,12 @@ func (self *JSRE) dump(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() } - state = block.State() } else { - state = self.ethereum.ChainManager().State() + block = self.ethereum.ChainManager().CurrentBlock() } - v, _ := self.Vm.ToValue(state.Dump()) + statedb := state.New(block.Root(), self.ethereum.Db()) + v, _ := self.Vm.ToValue(statedb.Dump()) return v } @@ -199,12 +197,13 @@ func (self *JSRE) watch(call otto.FunctionCall) otto.Value { } func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value { - host, err := call.Argument(0).ToString() + nodeURL, err := call.Argument(0).ToString() if err != nil { return otto.FalseValue() } - self.ethereum.ConnectToPeer(host) - + if err := self.ethereum.SuggestPeer(nodeURL); err != nil { + return otto.FalseValue() + } return otto.TrueValue() } diff --git a/javascript/types.go b/javascript/types.go index d5acaecce..17f1b739e 100644 --- a/javascript/types.go +++ b/javascript/types.go @@ -3,7 +3,7 @@ package javascript import ( "fmt" - "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/ui" @@ -12,17 +12,17 @@ import ( ) type JSStateObject struct { - *xeth.JSObject + *xeth.Object eth *JSEthereum } func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value { cb := call.Argument(0) - self.JSObject.EachStorage(func(key string, value *ethutil.Value) { - value.Decode() - cb.Call(self.eth.toVal(self), self.eth.toVal(key), self.eth.toVal(ethutil.Bytes2Hex(value.Bytes()))) - }) + it := self.Object.Trie().Iterator() + for it.Next() { + cb.Call(self.eth.toVal(self), self.eth.toVal(ethutil.Bytes2Hex(it.Key)), self.eth.toVal(ethutil.Bytes2Hex(it.Value))) + } return otto.UndefinedValue() } @@ -30,82 +30,52 @@ func (self *JSStateObject) EachStorage(call otto.FunctionCall) otto.Value { // The JSEthereum object attempts to wrap the PEthereum object and returns // meaningful javascript objects type JSBlock struct { - *xeth.JSBlock + *xeth.Block eth *JSEthereum } func (self *JSBlock) GetTransaction(hash string) otto.Value { - return self.eth.toVal(self.JSBlock.GetTransaction(hash)) + return self.eth.toVal(self.Block.GetTransaction(hash)) } -type JSMessage struct { - To string `json:"to"` - From string `json:"from"` - Input string `json:"input"` - Output string `json:"output"` - Path int `json:"path"` - Origin string `json:"origin"` - Timestamp int32 `json:"timestamp"` - Coinbase string `json:"coinbase"` - Block string `json:"block"` - Number int32 `json:"number"` +type JSLog struct { + Address string `json:address` + Topics []string `json:topics` + Number int32 `json:number` + Data string `json:data` } -func NewJSMessage(message *state.Message) JSMessage { - return JSMessage{ - To: ethutil.Bytes2Hex(message.To), - From: ethutil.Bytes2Hex(message.From), - Input: ethutil.Bytes2Hex(message.Input), - Output: ethutil.Bytes2Hex(message.Output), - Path: message.Path, - Origin: ethutil.Bytes2Hex(message.Origin), - Timestamp: int32(message.Timestamp), - Coinbase: ethutil.Bytes2Hex(message.Origin), - Block: ethutil.Bytes2Hex(message.Block), - Number: int32(message.Number.Int64()), +func NewJSLog(log state.Log) JSLog { + return JSLog{ + Address: ethutil.Bytes2Hex(log.Address()), + Topics: nil, //ethutil.Bytes2Hex(log.Address()), + Number: 0, + Data: ethutil.Bytes2Hex(log.Data()), } } type JSEthereum struct { - *xeth.JSXEth + *xeth.XEth vm *otto.Otto ethereum *eth.Ethereum } -func (self *JSEthereum) GetBlock(hash string) otto.Value { - return self.toVal(&JSBlock{self.JSXEth.BlockByHash(hash), self}) -} - -func (self *JSEthereum) GetPeers() otto.Value { - return self.toVal(self.JSXEth.Peers()) -} +func (self *JSEthereum) Block(v interface{}) otto.Value { + if number, ok := v.(int64); ok { + return self.toVal(&JSBlock{self.XEth.BlockByNumber(int32(number)), self}) + } else if hash, ok := v.(string); ok { + return self.toVal(&JSBlock{self.XEth.BlockByHash(hash), self}) + } -func (self *JSEthereum) GetKey() otto.Value { - return self.toVal(self.JSXEth.Key()) + return otto.UndefinedValue() } func (self *JSEthereum) GetStateObject(addr string) otto.Value { - return self.toVal(&JSStateObject{xeth.NewJSObject(self.JSXEth.World().SafeGet(ethutil.Hex2Bytes(addr))), self}) -} - -func (self *JSEthereum) Peers() otto.Value { - return self.toVal(self.JSXEth.Peers()) + return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self}) } func (self *JSEthereum) Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr string) otto.Value { - r, err := self.JSXEth.Transact(key, recipient, valueStr, gasStr, gasPriceStr, dataStr) - if err != nil { - fmt.Println(err) - - return otto.UndefinedValue() - } - - return self.toVal(r) -} - -func (self *JSEthereum) Create(key, valueStr, gasStr, gasPriceStr, scriptStr string) otto.Value { - r, err := self.JSXEth.Transact(key, "", valueStr, gasStr, gasPriceStr, scriptStr) - + r, err := self.XEth.Transact(recipient, valueStr, gasStr, gasPriceStr, dataStr) if err != nil { fmt.Println(err) @@ -130,13 +100,13 @@ func (self *JSEthereum) toVal(v interface{}) otto.Value { func (self *JSEthereum) Messages(object map[string]interface{}) otto.Value { filter := ui.NewFilterFromMap(object, self.ethereum) - messages := filter.Find() - var msgs []JSMessage - for _, m := range messages { - msgs = append(msgs, NewJSMessage(m)) + logs := filter.Find() + var jslogs []JSLog + for _, m := range logs { + jslogs = append(jslogs, NewJSLog(m)) } - v, _ := self.vm.ToValue(msgs) + v, _ := self.vm.ToValue(jslogs) return v } |