diff --git a/jethre/jeth.go b/jethre/jeth.go index 2d02107736..0970d740e8 100644 --- a/jethre/jeth.go +++ b/jethre/jeth.go @@ -3,6 +3,8 @@ package javascript import ( "fmt" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/xeth" "github.com/obscuren/otto" ) @@ -155,36 +157,45 @@ func (self *jeth) Export(call otto.FunctionCall) 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` -// } +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: ethutil.Bytes2Hex(log.Address()), -// Topics: nil, //ethutil.Bytes2Hex(log.Address()), -// Number: 0, -// Data: ethutil.Bytes2Hex(log.Data()), -// } -// } +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.XEth -// vm *otto.Otto -// } +func (self *jeth) Block(call otto.FunctionCall) otto.Value { + var block *xeth.Block + if len(call.ArgumentList) > 0 { + if call.Argument(0).IsNumber() { + num, _ := call.Argument(0).ToInteger() + block = self.xeth.BlockByNumber(int32(num)) + } else if call.Argument(0).IsString() { + hash, _ := call.Argument(0).ToString() + block = self.xeth.BlockByHash(hash) + } else { + fmt.Println("invalid argument for dump. Either hex string or number") + return otto.UndefinedValue() + } + } else { + block = self.xeth.BlockByNumber(-1) + } + if block == nil { + fmt.Println("block not found") + return otto.UndefinedValue() + } -// 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}) -// } - -// return otto.UndefinedValue() -// } + return self.toVal(block) +} // func (self *JSEthereum) GetStateObject(addr string) otto.Value { // return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self}) @@ -200,15 +211,3 @@ func (self *jeth) Export(call otto.FunctionCall) otto.Value { // 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 -// } diff --git a/jethre/jethre_test.go b/jethre/jethre_test.go index 61ce130914..0513c3b7e6 100644 --- a/jethre/jethre_test.go +++ b/jethre/jethre_test.go @@ -1,8 +1,11 @@ package javascript import ( + "fmt" + "github.com/obscuren/otto" "os" "path" + // "reflect" "testing" "github.com/ethereum/go-ethereum/eth" @@ -10,13 +13,19 @@ import ( ) func TestJEthRE(t *testing.T) { - + os.RemoveAll("/tmp/eth/") err := os.MkdirAll("/tmp/eth/data/", os.ModePerm) if err != nil { t.Errorf("%v", err) return } + err = ethutil.WriteFile("/tmp/eth/default.prv", []byte("946d0fe6dd95ef5476dd1fd204626b59c973bd72ffac4a108827ef488465cc68")) + if err != nil { + t.Errorf("%v", err) + return + } + ethereum, err := eth.New(ð.Config{ DataDir: "/tmp/eth", KeyStore: "file", @@ -26,12 +35,6 @@ func TestJEthRE(t *testing.T) { return } - err = ethutil.WriteFile("/tmp/eth/default.prv", []byte("946d0fe6dd95ef5476dd1fd204626b59c973bd72ffac4a108827ef488465cc68")) - if err != nil { - t.Errorf("%v", err) - return - } - t.Logf("ethereum %#v", ethereum) assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext") jethre := NewJEthRE(ethereum, assetPath) @@ -54,11 +57,44 @@ func TestJEthRE(t *testing.T) { t.Errorf("incorrect result, expected %s, got %v", expected, strVal) } + // no block 1, error + val, err = jethre.Run("eth.block(10)") + if err != nil { + t.Errorf("expected no error, got %v", err) + } + pp, err = jethre.PrettyPrint(val) + if err != nil { + t.Errorf("%v", err) + } + // t.Logf("block is %v", pp) + + if val != otto.UndefinedValue() { + t.Errorf("expected undefined value, got %v", pp) + } + + val, err = jethre.Run(`eth.block("b5d6d8402156c5c1dfadaa4b87c676b5bcadb17ef9bc8e939606daaa0d35f55d")`) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + pp, err = jethre.PrettyPrint(val) + if err != nil { + t.Errorf("%v", err) + } + // t.Logf("block is %v", pp) + + if val == otto.UndefinedValue() { + t.Errorf("undefined value") + } + + var val0 otto.Value // should get current block - // val, err = jethre.Run("eth.getBlock()") - // if err != nil { - // t.Errorf("expected no error, got %v", err) - // } + val0, err = jethre.Run("eth.block()") + if err != nil { + t.Errorf("expected no error, got %v", err) + } + if !val.IsObject() { + t.Errorf("expected object") + } fn := "/tmp/eth/data/blockchain.0" val, err = jethre.Run("eth.export(\"" + fn + "\")") @@ -78,17 +114,20 @@ func TestJEthRE(t *testing.T) { t.Errorf("expected no error, got %v", err) } - // var val0 otto.Value + var val1 otto.Value // should get current block - // val0, err = jethre.Run("eth.getBlock()") - // if err != nil { - // t.Errorf("expected no error, got %v", err) - // } + val1, err = jethre.Run("eth.block()") + if err != nil { + t.Errorf("expected no error, got %v", err) + } - // if v0 != v1 { - // t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0) - // } + // 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) + } ethereum.Start() // FIXME: @@ -147,4 +186,5 @@ func TestJEthRE(t *testing.T) { if mining { t.Errorf("expected false (not mining), got true") } + } diff --git a/xeth/xeth.go b/xeth/xeth.go index 94081a3a91..999747a2b1 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -82,19 +82,24 @@ func (self *XEth) State() *State { return self.state } func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) Miner() *miner.Miner { return self.miner } -func (self *XEth) BlockByHash(strHash string) *Block { +func (self *XEth) BlockByHash(strHash string) (block *Block) { hash := fromHex(strHash) - block := self.chainManager.GetBlock(hash) - - return NewBlock(block) + b := self.chainManager.GetBlock(hash) + if b != nil { + block = NewBlock(b) + } + return } -func (self *XEth) BlockByNumber(num int32) *Block { +func (self *XEth) BlockByNumber(num int32) (block *Block) { if num == -1 { return NewBlock(self.chainManager.CurrentBlock()) } - - return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) + b := self.chainManager.GetBlockByNumber(uint64(num)) + if b != nil { + block = NewBlock(b) + } + return } // func (self *XEth) DumpBlockByHash(strHash string) (state.World, error) {