diff options
Diffstat (limited to 'xeth/hexface.go')
-rw-r--r-- | xeth/hexface.go | 150 |
1 files changed, 61 insertions, 89 deletions
diff --git a/xeth/hexface.go b/xeth/hexface.go index 808ae7d39..3eb535724 100644 --- a/xeth/hexface.go +++ b/xeth/hexface.go @@ -11,27 +11,52 @@ import ( "github.com/ethereum/go-ethereum/state" ) +// to resolve the import cycle +type Backend interface { + BlockProcessor() *core.BlockProcessor + ChainManager() *core.ChainManager + Coinbase() []byte + KeyManager() *crypto.KeyManager + IsMining() bool + IsListening() bool + PeerCount() int + Db() ethutil.Database + TxPool() *core.TxPool +} + type JSXEth struct { - *XEth + eth Backend + blockProcessor *core.BlockProcessor + chainManager *core.ChainManager + world *State } -func NewJSXEth(eth core.EthManager) *JSXEth { - return &JSXEth{New(eth)} +func NewJSXEth(eth Backend) *JSXEth { + xeth := &JSXEth{ + eth: eth, + blockProcessor: eth.BlockProcessor(), + chainManager: eth.ChainManager(), + } + xeth.world = NewState(xeth) + + return xeth } +func (self *JSXEth) State() *State { return self.world } + func (self *JSXEth) BlockByHash(strHash string) *JSBlock { hash := fromHex(strHash) - block := self.obj.ChainManager().GetBlock(hash) + block := self.chainManager.GetBlock(hash) return NewJSBlock(block) } func (self *JSXEth) BlockByNumber(num int32) *JSBlock { if num == -1 { - return NewJSBlock(self.obj.ChainManager().CurrentBlock()) + return NewJSBlock(self.chainManager.CurrentBlock()) } - return NewJSBlock(self.obj.ChainManager().GetBlockByNumber(uint64(num))) + return NewJSBlock(self.chainManager.GetBlockByNumber(uint64(num))) } func (self *JSXEth) Block(v interface{}) *JSBlock { @@ -46,43 +71,32 @@ func (self *JSXEth) Block(v interface{}) *JSBlock { return nil } -func (self *JSXEth) Key() *JSKey { - return NewJSKey(self.obj.KeyManager().KeyPair()) -} - func (self *JSXEth) Accounts() []string { - return []string{toHex(self.obj.KeyManager().Address())} + return []string{toHex(self.eth.KeyManager().Address())} } +/* func (self *JSXEth) StateObject(addr string) *JSObject { - object := &Object{self.World().safeGet(fromHex(addr))} + object := &Object{self.State().safeGet(fromHex(addr))} return NewJSObject(object) } +*/ func (self *JSXEth) PeerCount() int { - return self.obj.PeerCount() -} - -func (self *JSXEth) Peers() []JSPeer { - var peers []JSPeer - for _, peer := range self.obj.Peers() { - peers = append(peers, *NewJSPeer(peer)) - } - - return peers + return self.eth.PeerCount() } func (self *JSXEth) IsMining() bool { - return self.obj.IsMining() + return self.eth.IsMining() } func (self *JSXEth) IsListening() bool { - return self.obj.IsListening() + return self.eth.IsListening() } -func (self *JSXEth) CoinBase() string { - return toHex(self.obj.KeyManager().Address()) +func (self *JSXEth) Coinbase() string { + return toHex(self.eth.KeyManager().Address()) } func (self *JSXEth) NumberToHuman(balance string) string { @@ -92,25 +106,25 @@ func (self *JSXEth) NumberToHuman(balance string) string { } func (self *JSXEth) StorageAt(addr, storageAddr string) string { - storage := self.World().SafeGet(fromHex(addr)).Storage(fromHex(storageAddr)) + storage := self.State().SafeGet(addr).StorageString(storageAddr) return toHex(storage.Bytes()) } func (self *JSXEth) BalanceAt(addr string) string { - return self.World().SafeGet(fromHex(addr)).Balance().String() + return self.State().SafeGet(addr).Balance().String() } func (self *JSXEth) TxCountAt(address string) int { - return int(self.World().SafeGet(fromHex(address)).Nonce) + return int(self.State().SafeGet(address).Nonce) } func (self *JSXEth) CodeAt(address string) string { - return toHex(self.World().SafeGet(fromHex(address)).Code) + return toHex(self.State().SafeGet(address).Code) } func (self *JSXEth) IsContract(address string) bool { - return len(self.World().SafeGet(fromHex(address)).Code) > 0 + return len(self.State().SafeGet(address).Code) > 0 } func (self *JSXEth) SecretToAddress(key string) string { @@ -123,15 +137,7 @@ func (self *JSXEth) SecretToAddress(key string) string { } func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) { - ret, err := self.ExecuteObject(&Object{ - self.World().safeGet(fromHex(addr))}, - fromHex(data), - ethutil.NewValue(value), - ethutil.NewValue(gas), - ethutil.NewValue(price), - ) - - return toHex(ret), err + return "", nil } type KeyVal struct { @@ -141,7 +147,7 @@ type KeyVal struct { func (self *JSXEth) EachStorage(addr string) string { var values []KeyVal - object := self.World().SafeGet(fromHex(addr)) + object := self.State().SafeGet(addr) it := object.Trie().Iterator() for it.Next() { values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)}) @@ -178,62 +184,28 @@ func (self *JSXEth) FromNumber(str string) string { } func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { - var ( - to []byte - value = ethutil.NewValue(valueStr) - gas = ethutil.NewValue(gasStr) - gasPrice = ethutil.NewValue(gasPriceStr) - data []byte - ) - - data = fromHex(codeStr) - - to = fromHex(toStr) - - keyPair, err := crypto.NewKeyPairFromSec([]byte(fromHex(key))) - if err != nil { - return "", err - } - - tx, err := self.XEth.Transact(keyPair, to, value, gas, gasPrice, data) - if err != nil { - return "", err - } - if types.IsContractAddr(to) { - return toHex(core.AddressFromMessage(tx)), nil - } - - return toHex(tx.Hash()), nil + return "", nil } -func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) { - tx := types.NewTransactionFromBytes(fromHex(txStr)) - err := self.obj.TxPool().Add(tx) - if err != nil { - return nil, err +func ToJSMessages(messages state.Messages) *ethutil.List { + var msgs []JSMessage + for _, m := range messages { + msgs = append(msgs, NewJSMessage(m)) } - return NewJSReciept(core.MessageCreatesContract(tx), core.AddressFromMessage(tx), tx.Hash(), tx.From()), nil + return ethutil.NewList(msgs) } -func (self *JSXEth) CompileMutan(code string) string { - data, err := self.XEth.CompileMutan(code) +func (self *JSXEth) PushTx(encodedTx string) (string, error) { + tx := types.NewTransactionFromBytes(fromHex(encodedTx)) + err := self.eth.TxPool().Add(tx) if err != nil { - return err.Error() + return "", err } - return toHex(data) -} - -func (self *JSXEth) FindInConfig(str string) string { - return toHex(self.World().Config().Get(str).Address()) -} - -func ToJSMessages(messages state.Messages) *ethutil.List { - var msgs []JSMessage - for _, m := range messages { - msgs = append(msgs, NewJSMessage(m)) + if tx.To() == nil { + addr := core.AddressFromMessage(tx) + return toHex(addr), nil } - - return ethutil.NewList(msgs) + return toHex(tx.Hash()), nil } |