diff options
Diffstat (limited to 'xeth/xeth.go')
-rw-r--r-- | xeth/xeth.go | 123 |
1 files changed, 104 insertions, 19 deletions
diff --git a/xeth/xeth.go b/xeth/xeth.go index 96e545b35..0e71a0ed9 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -12,8 +12,11 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/whisper" ) var pipelogger = logger.NewLogger("XETH") @@ -22,20 +25,24 @@ var pipelogger = logger.NewLogger("XETH") type Backend interface { BlockProcessor() *core.BlockProcessor ChainManager() *core.ChainManager - Coinbase() []byte - KeyManager() *crypto.KeyManager + TxPool() *core.TxPool + PeerCount() int IsMining() bool IsListening() bool - PeerCount() int + Peers() []*p2p.Peer + KeyManager() *crypto.KeyManager + ClientIdentity() p2p.ClientIdentity Db() ethutil.Database - TxPool() *core.TxPool + EventMux() *event.TypeMux + Whisper() *whisper.Whisper } type XEth struct { eth Backend blockProcessor *core.BlockProcessor chainManager *core.ChainManager - world *State + state *State + whisper *Whisper } func New(eth Backend) *XEth { @@ -43,13 +50,16 @@ func New(eth Backend) *XEth { eth: eth, blockProcessor: eth.BlockProcessor(), chainManager: eth.ChainManager(), + whisper: NewWhisper(eth.Whisper()), } - xeth.world = NewState(xeth) + xeth.state = NewState(xeth) return xeth } -func (self *XEth) State() *State { return self.world } +func (self *XEth) Backend() Backend { return self.eth } +func (self *XEth) State() *State { return self.state } +func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) BlockByHash(strHash string) *Block { hash := fromHex(strHash) @@ -82,14 +92,6 @@ func (self *XEth) Accounts() []string { return []string{toHex(self.eth.KeyManager().Address())} } -/* -func (self *XEth) StateObject(addr string) *Object { - object := &Object{self.State().safeGet(fromHex(addr))} - - return NewObject(object) -} -*/ - func (self *XEth) PeerCount() int { return self.eth.PeerCount() } @@ -190,10 +192,6 @@ func (self *XEth) FromNumber(str string) string { return ethutil.BigD(fromHex(str)).String() } -func (self *XEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { - return "", nil -} - func ToMessages(messages state.Messages) *ethutil.List { var msgs []Message for _, m := range messages { @@ -216,3 +214,90 @@ func (self *XEth) PushTx(encodedTx string) (string, error) { } return toHex(tx.Hash()), nil } + +func (self *XEth) Call(toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) { + if len(gasStr) == 0 { + gasStr = "100000" + } + if len(gasPriceStr) == 0 { + gasPriceStr = "1" + } + + var ( + statedb = self.chainManager.TransState() + key = self.eth.KeyManager().KeyPair() + from = state.NewStateObject(key.Address(), self.eth.Db()) + block = self.chainManager.CurrentBlock() + to = statedb.GetOrNewStateObject(fromHex(toStr)) + data = fromHex(dataStr) + gas = ethutil.Big(gasStr) + price = ethutil.Big(gasPriceStr) + value = ethutil.Big(valueStr) + ) + + msg := types.NewTransactionMessage(fromHex(toStr), value, gas, price, data) + msg.Sign(key.PrivateKey) + vmenv := core.NewEnv(statedb, self.chainManager, msg, block) + + res, err := vmenv.Call(from, to.Address(), data, gas, price, value) + if err != nil { + return "", err + } + + return toHex(res), nil +} + +func (self *XEth) Transact(toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { + + var ( + to []byte + value = ethutil.NewValue(valueStr) + gas = ethutil.NewValue(gasStr) + price = ethutil.NewValue(gasPriceStr) + data []byte + key = self.eth.KeyManager().KeyPair() + contractCreation bool + ) + + data = fromHex(codeStr) + to = fromHex(toStr) + if len(to) == 0 { + contractCreation = true + } + + var tx *types.Transaction + if contractCreation { + tx = types.NewContractCreationTx(value.BigInt(), gas.BigInt(), price.BigInt(), data) + } else { + tx = types.NewTransactionMessage(to, 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.eth.TxPool().Add(tx) + if err != nil { + return "", err + } + state.SetNonce(key.Address(), nonce+1) + + if contractCreation { + addr := core.AddressFromMessage(tx) + pipelogger.Infof("Contract addr %x\n", addr) + } + + if types.IsContractAddr(to) { + return toHex(core.AddressFromMessage(tx)), nil + } + + return toHex(tx.Hash()), nil +} |