implement block (by hash and by number) , no arg same as -1 = current head + tests

This commit is contained in:
zelig 2015-03-09 19:57:59 +07:00
parent ea6f2124d9
commit 1df8ad2a77
3 changed files with 109 additions and 65 deletions

View file

@ -3,6 +3,8 @@ package javascript
import ( import (
"fmt" "fmt"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
"github.com/obscuren/otto" "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)) // return self.eth.toVal(self.Block.GetTransaction(hash))
// } // }
// type JSLog struct { type JSLog struct {
// Address string `json:address` Address string `json:address`
// Topics []string `json:topics` Topics []string `json:topics`
// Number int32 `json:number` Number int32 `json:number`
// Data string `json:data` Data string `json:data`
// } }
// func NewJSLog(log state.Log) JSLog { func NewJSLog(log state.Log) JSLog {
// return JSLog{ return JSLog{
// Address: ethutil.Bytes2Hex(log.Address()), Address: ethutil.Bytes2Hex(log.Address()),
// Topics: nil, //ethutil.Bytes2Hex(log.Address()), Topics: nil, //ethutil.Bytes2Hex(log.Address()),
// Number: 0, Number: 0,
// Data: ethutil.Bytes2Hex(log.Data()), Data: ethutil.Bytes2Hex(log.Data()),
// } }
// } }
// type JSEthereum struct { func (self *jeth) Block(call otto.FunctionCall) otto.Value {
// *xeth.XEth var block *xeth.Block
// vm *otto.Otto 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 { return self.toVal(block)
// 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()
// }
// func (self *JSEthereum) GetStateObject(addr string) otto.Value { // func (self *JSEthereum) GetStateObject(addr string) otto.Value {
// return self.toVal(&JSStateObject{self.XEth.State().SafeGet(addr), self}) // 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) // 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
// }

View file

@ -1,8 +1,11 @@
package javascript package javascript
import ( import (
"fmt"
"github.com/obscuren/otto"
"os" "os"
"path" "path"
// "reflect"
"testing" "testing"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
@ -10,13 +13,19 @@ import (
) )
func TestJEthRE(t *testing.T) { func TestJEthRE(t *testing.T) {
os.RemoveAll("/tmp/eth/")
err := os.MkdirAll("/tmp/eth/data/", os.ModePerm) err := os.MkdirAll("/tmp/eth/data/", os.ModePerm)
if err != nil { if err != nil {
t.Errorf("%v", err) t.Errorf("%v", err)
return return
} }
err = ethutil.WriteFile("/tmp/eth/default.prv", []byte("946d0fe6dd95ef5476dd1fd204626b59c973bd72ffac4a108827ef488465cc68"))
if err != nil {
t.Errorf("%v", err)
return
}
ethereum, err := eth.New(&eth.Config{ ethereum, err := eth.New(&eth.Config{
DataDir: "/tmp/eth", DataDir: "/tmp/eth",
KeyStore: "file", KeyStore: "file",
@ -26,12 +35,6 @@ func TestJEthRE(t *testing.T) {
return 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") assetPath := path.Join(os.Getenv("GOPATH"), "src", "github.com", "ethereum", "go-ethereum", "cmd", "mist", "assets", "ext")
jethre := NewJEthRE(ethereum, assetPath) jethre := NewJEthRE(ethereum, assetPath)
@ -54,11 +57,44 @@ func TestJEthRE(t *testing.T) {
t.Errorf("incorrect result, expected %s, got %v", expected, strVal) 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 // should get current block
// val, err = jethre.Run("eth.getBlock()") val0, err = jethre.Run("eth.block()")
// if err != nil { if err != nil {
// t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
// } }
if !val.IsObject() {
t.Errorf("expected object")
}
fn := "/tmp/eth/data/blockchain.0" fn := "/tmp/eth/data/blockchain.0"
val, err = jethre.Run("eth.export(\"" + fn + "\")") val, err = jethre.Run("eth.export(\"" + fn + "\")")
@ -78,17 +114,20 @@ func TestJEthRE(t *testing.T) {
t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
} }
// var val0 otto.Value var val1 otto.Value
// should get current block // should get current block
// val0, err = jethre.Run("eth.getBlock()") val1, err = jethre.Run("eth.block()")
// if err != nil { if err != nil {
// t.Errorf("expected no error, got %v", err) t.Errorf("expected no error, got %v", err)
// } }
// if v0 != v1 { // FIXME: neither != , nor reflect.DeepEqual works, doing string comparison
// t.Errorf("expected same head after export-import, got %v (!=%v)", v1, v0) 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() ethereum.Start()
// FIXME: // FIXME:
@ -147,4 +186,5 @@ func TestJEthRE(t *testing.T) {
if mining { if mining {
t.Errorf("expected false (not mining), got true") t.Errorf("expected false (not mining), got true")
} }
} }

View file

@ -82,19 +82,24 @@ func (self *XEth) State() *State { return self.state }
func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) Whisper() *Whisper { return self.whisper }
func (self *XEth) Miner() *miner.Miner { return self.miner } 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) hash := fromHex(strHash)
block := self.chainManager.GetBlock(hash) b := self.chainManager.GetBlock(hash)
if b != nil {
return NewBlock(block) block = NewBlock(b)
}
return
} }
func (self *XEth) BlockByNumber(num int32) *Block { func (self *XEth) BlockByNumber(num int32) (block *Block) {
if num == -1 { if num == -1 {
return NewBlock(self.chainManager.CurrentBlock()) return NewBlock(self.chainManager.CurrentBlock())
} }
b := self.chainManager.GetBlockByNumber(uint64(num))
return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) if b != nil {
block = NewBlock(b)
}
return
} }
// func (self *XEth) DumpBlockByHash(strHash string) (state.World, error) { // func (self *XEth) DumpBlockByHash(strHash string) (state.World, error) {