diff options
Diffstat (limited to 'xeth')
-rw-r--r-- | xeth/config.go | 2 | ||||
-rw-r--r-- | xeth/hexface.go | 150 | ||||
-rw-r--r-- | xeth/world.go | 57 | ||||
-rw-r--r-- | xeth/xeth.go | 174 |
4 files changed, 77 insertions, 306 deletions
diff --git a/xeth/config.go b/xeth/config.go index ad0660d75..db6cce3bd 100644 --- a/xeth/config.go +++ b/xeth/config.go @@ -1,5 +1,6 @@ package xeth +/* import "github.com/ethereum/go-ethereum/ethutil" var cnfCtr = ethutil.Hex2Bytes("661005d2720d855f1d9976f88bb10c1a3398c77f") @@ -33,3 +34,4 @@ func (self *Config) Get(name string) *Object { func (self *Config) Exist() bool { return self.pipe.World().Get(cnfCtr) != nil } +*/ 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 } diff --git a/xeth/world.go b/xeth/world.go index 25c2f3eb8..60d4bbb57 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -1,63 +1,32 @@ package xeth -import ( - "github.com/ethereum/go-ethereum/p2p" - "github.com/ethereum/go-ethereum/state" -) - -type World struct { - pipe *XEth - cfg *Config -} - -func NewWorld(pipe *XEth) *World { - world := &World{pipe, nil} - world.cfg = &Config{pipe} +import "github.com/ethereum/go-ethereum/state" - return world +type State struct { + xeth *JSXEth } -func (self *XEth) World() *World { - return self.world +func NewState(xeth *JSXEth) *State { + return &State{xeth} } -func (self *World) State() *state.StateDB { - return self.pipe.chainManager.State() +func (self *State) State() *state.StateDB { + return self.xeth.chainManager.State() } -func (self *World) Get(addr []byte) *Object { - return &Object{self.State().GetStateObject(addr)} +func (self *State) Get(addr string) *Object { + return &Object{self.State().GetStateObject(fromHex(addr))} } -func (self *World) SafeGet(addr []byte) *Object { +func (self *State) SafeGet(addr string) *Object { return &Object{self.safeGet(addr)} } -func (self *World) safeGet(addr []byte) *state.StateObject { - object := self.State().GetStateObject(addr) +func (self *State) safeGet(addr string) *state.StateObject { + object := self.State().GetStateObject(fromHex(addr)) if object == nil { - object = state.NewStateObject(addr, self.pipe.obj.Db()) + object = state.NewStateObject(fromHex(addr), self.xeth.eth.Db()) } return object } - -func (self *World) Coinbase() *state.StateObject { - return nil -} - -func (self *World) IsMining() bool { - return self.pipe.obj.IsMining() -} - -func (self *World) IsListening() bool { - return self.pipe.obj.IsListening() -} - -func (self *World) Peers() []*p2p.Peer { - return self.pipe.obj.Peers() -} - -func (self *World) Config() *Config { - return self.cfg -} diff --git a/xeth/xeth.go b/xeth/xeth.go index 34d8ffd0a..92b9bc995 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -4,178 +4,6 @@ package xeth * eXtended ETHereum */ -import ( - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethutil" - "github.com/ethereum/go-ethereum/logger" - "github.com/ethereum/go-ethereum/state" -) +import "github.com/ethereum/go-ethereum/logger" var pipelogger = logger.NewLogger("XETH") - -type VmVars struct { - State *state.StateDB -} - -type XEth struct { - obj core.EthManager - blockProcessor *core.BlockProcessor - chainManager *core.ChainManager - world *World - - Vm VmVars -} - -func New(obj core.EthManager) *XEth { - pipe := &XEth{ - obj: obj, - blockProcessor: obj.BlockProcessor(), - chainManager: obj.ChainManager(), - } - pipe.world = NewWorld(pipe) - - return pipe -} - -/* - * State / Account accessors - */ -func (self *XEth) Balance(addr []byte) *ethutil.Value { - return ethutil.NewValue(self.World().safeGet(addr).Balance) -} - -func (self *XEth) Nonce(addr []byte) uint64 { - return self.World().safeGet(addr).Nonce -} - -func (self *XEth) Block(hash []byte) *types.Block { - return self.chainManager.GetBlock(hash) -} - -func (self *XEth) Storage(addr, storageAddr []byte) *ethutil.Value { - return self.World().safeGet(addr).GetStorage(ethutil.BigD(storageAddr)) -} - -func (self *XEth) Exists(addr []byte) bool { - return self.World().Get(addr) != nil -} - -// Converts the given private key to an address -func (self *XEth) ToAddress(priv []byte) []byte { - pair, err := crypto.NewKeyPairFromSec(priv) - if err != nil { - return nil - } - - return pair.Address() -} - -/* - * Execution helpers - */ -func (self *XEth) Execute(addr []byte, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { - return self.ExecuteObject(&Object{self.World().safeGet(addr)}, data, value, gas, price) -} - -func (self *XEth) ExecuteObject(object *Object, data []byte, value, gas, price *ethutil.Value) ([]byte, error) { - var ( - initiator = state.NewStateObject(self.obj.KeyManager().KeyPair().Address(), self.obj.Db()) - block = self.chainManager.CurrentBlock() - ) - - self.Vm.State = self.World().State().Copy() - - vmenv := NewEnv(self.chainManager, self.Vm.State, block, value.BigInt(), initiator.Address()) - return vmenv.Call(initiator, object.Address(), data, gas.BigInt(), price.BigInt(), value.BigInt()) -} - -/* - * Transactional methods - */ -func (self *XEth) TransactString(key *crypto.KeyPair, rec string, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { - // Check if an address is stored by this address - var hash []byte - addr := self.World().Config().Get("NameReg").StorageString(rec).Bytes() - if len(addr) > 0 { - hash = addr - } else if ethutil.IsHex(rec) { - hash = ethutil.Hex2Bytes(rec[2:]) - } else { - hash = ethutil.Hex2Bytes(rec) - } - - return self.Transact(key, hash, value, gas, price, data) -} - -func (self *XEth) Transact(key *crypto.KeyPair, to []byte, value, gas, price *ethutil.Value, data []byte) (*types.Transaction, error) { - var hash []byte - var contractCreation bool - if types.IsContractAddr(to) { - contractCreation = true - } else { - // Check if an address is stored by this address - addr := self.World().Config().Get("NameReg").Storage(to).Bytes() - if len(addr) > 0 { - hash = addr - } else { - hash = to - } - } - - var tx *types.Transaction - if contractCreation { - tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) - } else { - tx = types.NewTransactionMessage(hash, value.BigInt(), gas.BigInt(), price.BigInt(), data) - } - - state := self.chainManager.TransState() - nonce := state.GetNonce(key.Address()) - - tx.SetNonce(nonce) - tx.Sign(key.PrivateKey) - - // Do some pre processing for our "pre" events and hooks - block := self.chainManager.NewBlock(key.Address()) - coinbase := state.GetOrNewStateObject(key.Address()) - coinbase.SetGasPool(block.GasLimit()) - self.blockProcessor.ApplyTransactions(coinbase, state, block, types.Transactions{tx}, true) - - err := self.obj.TxPool().Add(tx) - if err != nil { - return nil, err - } - state.SetNonce(key.Address(), nonce+1) - - if contractCreation { - addr := core.AddressFromMessage(tx) - pipelogger.Infof("Contract addr %x\n", addr) - } - - return tx, nil -} - -func (self *XEth) PushTx(tx *types.Transaction) ([]byte, error) { - err := self.obj.TxPool().Add(tx) - if err != nil { - return nil, err - } - - if tx.To() == nil { - addr := core.AddressFromMessage(tx) - pipelogger.Infof("Contract addr %x\n", addr) - return addr, nil - } - return tx.Hash(), nil -} - -func (self *XEth) CompileMutan(code string) ([]byte, error) { - data, err := ethutil.Compile(code, false) - if err != nil { - return nil, err - } - - return data, nil -} |