diff options
Diffstat (limited to 'xeth')
-rw-r--r-- | xeth/types.go | 24 | ||||
-rw-r--r-- | xeth/vm_env.go | 72 | ||||
-rw-r--r-- | xeth/whisper.go | 115 | ||||
-rw-r--r-- | xeth/world.go | 2 | ||||
-rw-r--r-- | xeth/xeth.go | 123 |
5 files changed, 240 insertions, 96 deletions
diff --git a/xeth/types.go b/xeth/types.go index 4d8543eb1..34caf5cbc 100644 --- a/xeth/types.go +++ b/xeth/types.go @@ -1,6 +1,7 @@ package xeth import ( + "bytes" "fmt" "strings" @@ -9,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/state" ) @@ -35,20 +37,34 @@ func NewObject(state *state.StateObject) *Object { func (self *Object) StorageString(str string) *ethutil.Value { if ethutil.IsHex(str) { - return self.Storage(ethutil.Hex2Bytes(str[2:])) + return self.storage(ethutil.Hex2Bytes(str[2:])) } else { - return self.Storage(ethutil.RightPadBytes([]byte(str), 32)) + return self.storage(ethutil.RightPadBytes([]byte(str), 32)) } } func (self *Object) StorageValue(addr *ethutil.Value) *ethutil.Value { - return self.Storage(addr.Bytes()) + return self.storage(addr.Bytes()) } -func (self *Object) Storage(addr []byte) *ethutil.Value { +func (self *Object) storage(addr []byte) *ethutil.Value { return self.StateObject.GetStorage(ethutil.BigD(addr)) } +func (self *Object) Storage() (storage map[string]string) { + storage = make(map[string]string) + + it := self.StateObject.Trie().Iterator() + for it.Next() { + var data []byte + rlp.Decode(bytes.NewReader(it.Value), &data) + storage[toHex(it.Key)] = toHex(data) + } + self.StateObject.Trie().PrintRoot() + + return +} + // Block interface exposed to QML type Block struct { //Transactions string `json:"transactions"` diff --git a/xeth/vm_env.go b/xeth/vm_env.go deleted file mode 100644 index 1470b9eaa..000000000 --- a/xeth/vm_env.go +++ /dev/null @@ -1,72 +0,0 @@ -package xeth - -import ( - "math/big" - - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/vm" -) - -type VMEnv struct { - chain *core.ChainManager - state *state.StateDB - block *types.Block - value *big.Int - sender []byte - - depth int -} - -func NewEnv(chain *core.ChainManager, state *state.StateDB, block *types.Block, value *big.Int, sender []byte) *VMEnv { - return &VMEnv{ - state: state, - block: block, - value: value, - sender: sender, - } -} - -func (self *VMEnv) Origin() []byte { return self.sender } -func (self *VMEnv) BlockNumber() *big.Int { return self.block.Number() } -func (self *VMEnv) PrevHash() []byte { return self.block.ParentHash() } -func (self *VMEnv) Coinbase() []byte { return self.block.Coinbase() } -func (self *VMEnv) Time() int64 { return self.block.Time() } -func (self *VMEnv) Difficulty() *big.Int { return self.block.Difficulty() } -func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit() } -func (self *VMEnv) Value() *big.Int { return self.value } -func (self *VMEnv) State() *state.StateDB { return self.state } -func (self *VMEnv) Depth() int { return self.depth } -func (self *VMEnv) SetDepth(i int) { self.depth = i } -func (self *VMEnv) GetHash(n uint64) []byte { - if block := self.chain.GetBlockByNumber(n); block != nil { - return block.Hash() - } - - return nil -} -func (self *VMEnv) AddLog(log state.Log) { - self.state.AddLog(log) -} -func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error { - return vm.Transfer(from, to, amount) -} - -func (self *VMEnv) vm(addr, data []byte, gas, price, value *big.Int) *core.Execution { - return core.NewExecution(self, addr, data, gas, price, value) -} - -func (self *VMEnv) Call(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { - exe := self.vm(addr, data, gas, price, value) - return exe.Call(addr, me) -} -func (self *VMEnv) CallCode(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error) { - exe := self.vm(me.Address(), data, gas, price, value) - return exe.Call(addr, me) -} - -func (self *VMEnv) Create(me vm.ContextRef, addr, data []byte, gas, price, value *big.Int) ([]byte, error, vm.ContextRef) { - exe := self.vm(addr, data, gas, price, value) - return exe.Create(me) -} diff --git a/xeth/whisper.go b/xeth/whisper.go new file mode 100644 index 000000000..d9c7e1614 --- /dev/null +++ b/xeth/whisper.go @@ -0,0 +1,115 @@ +package xeth + +import ( + "errors" + "time" + + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/whisper" +) + +var qlogger = logger.NewLogger("XSHH") + +type Whisper struct { + *whisper.Whisper +} + +func NewWhisper(w *whisper.Whisper) *Whisper { + return &Whisper{w} +} + +func (self *Whisper) Post(payload string, to, from string, topics []string, priority, ttl uint32) error { + if priority == 0 { + priority = 1000 + } + + if ttl == 0 { + ttl = 100 + } + + pk := crypto.ToECDSAPub(fromHex(from)) + if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 { + msg := whisper.NewMessage(fromHex(payload)) + envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{ + Ttl: time.Duration(ttl) * time.Second, + To: crypto.ToECDSAPub(fromHex(to)), + From: key, + Topics: whisper.TopicsFromString(topics...), + }) + + if err != nil { + return err + } + + if err := self.Whisper.Send(envelope); err != nil { + return err + } + } else { + return errors.New("unmatched pub / priv for seal") + } + + return nil +} + +func (self *Whisper) NewIdentity() string { + key := self.Whisper.NewIdentity() + + return toHex(crypto.FromECDSAPub(&key.PublicKey)) +} + +func (self *Whisper) HasIdentity(key string) bool { + return self.Whisper.HasIdentity(crypto.ToECDSAPub(fromHex(key))) +} + +func (self *Whisper) Watch(opts *Options) int { + filter := whisper.Filter{ + To: crypto.ToECDSAPub(fromHex(opts.To)), + From: crypto.ToECDSAPub(fromHex(opts.From)), + Topics: whisper.TopicsFromString(opts.Topics...), + } + + var i int + filter.Fn = func(msg *whisper.Message) { + opts.Fn(NewWhisperMessage(msg)) + } + + i = self.Whisper.Watch(filter) + + return i +} + +func (self *Whisper) Messages(id int) (messages []WhisperMessage) { + msgs := self.Whisper.Messages(id) + messages = make([]WhisperMessage, len(msgs)) + for i, message := range msgs { + messages[i] = NewWhisperMessage(message) + } + + return +} + +type Options struct { + To string + From string + Topics []string + Fn func(msg WhisperMessage) +} + +type WhisperMessage struct { + ref *whisper.Message + Payload string `json:"payload"` + To string `json:"to"` + From string `json:"from"` + Sent int64 `json:"sent"` +} + +func NewWhisperMessage(msg *whisper.Message) WhisperMessage { + return WhisperMessage{ + ref: msg, + Payload: toHex(msg.Payload), + From: toHex(crypto.FromECDSAPub(msg.Recover())), + To: toHex(crypto.FromECDSAPub(msg.To)), + Sent: msg.Sent, + } +} diff --git a/xeth/world.go b/xeth/world.go index cdceec50d..9cbdd9461 100644 --- a/xeth/world.go +++ b/xeth/world.go @@ -11,7 +11,7 @@ func NewState(xeth *XEth) *State { } func (self *State) State() *state.StateDB { - return self.xeth.chainManager.State() + return self.xeth.chainManager.TransState() } func (self *State) Get(addr string) *Object { 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 +} |