aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--accounts/account_manager.go4
-rw-r--r--rpc/api.go144
-rw-r--r--ui/frontend.go18
-rw-r--r--xeth/state.go (renamed from xeth/world.go)13
-rw-r--r--xeth/xeth.go22
5 files changed, 157 insertions, 44 deletions
diff --git a/accounts/account_manager.go b/accounts/account_manager.go
index 90fed1343..3e9fa7799 100644
--- a/accounts/account_manager.go
+++ b/accounts/account_manager.go
@@ -34,10 +34,12 @@ package accounts
import (
crand "crypto/rand"
+
"errors"
- "github.com/ethereum/go-ethereum/crypto"
"sync"
"time"
+
+ "github.com/ethereum/go-ethereum/crypto"
)
var ErrLocked = errors.New("account is locked; please request passphrase")
diff --git a/rpc/api.go b/rpc/api.go
index 908d37a80..3b4a914ef 100644
--- a/rpc/api.go
+++ b/rpc/api.go
@@ -9,6 +9,7 @@ For each request type, define the following:
package rpc
import (
+ "fmt"
"math/big"
"strings"
"sync"
@@ -19,8 +20,10 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil"
+ "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/event/filter"
"github.com/ethereum/go-ethereum/state"
+ "github.com/ethereum/go-ethereum/ui"
"github.com/ethereum/go-ethereum/xeth"
)
@@ -31,7 +34,10 @@ var (
)
type EthereumApi struct {
- xeth *xeth.XEth
+ eth *xeth.XEth
+ xethMu sync.RWMutex
+ mux *event.TypeMux
+
quit chan struct{}
filterManager *filter.FilterManager
@@ -52,7 +58,8 @@ type EthereumApi struct {
func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
db, _ := ethdb.NewLDBDatabase("dapps")
api := &EthereumApi{
- xeth: eth,
+ eth: eth,
+ mux: eth.Backend().EventMux(),
quit: make(chan struct{}),
filterManager: filter.NewFilterManager(eth.Backend().EventMux()),
logs: make(map[int]*logFilter),
@@ -67,9 +74,23 @@ func NewEthereumApi(eth *xeth.XEth) *EthereumApi {
func (self *EthereumApi) start() {
timer := time.NewTicker(filterTickerTime)
+ events := self.mux.Subscribe(core.ChainEvent{})
+
done:
for {
select {
+ case ev := <-events.Chan():
+ switch ev.(type) {
+ case core.ChainEvent:
+ if self.defaultBlockAge < 0 {
+ chain := self.xeth().Backend().ChainManager()
+ block := chain.GetBlockByNumber(chain.CurrentBlock().Number().Uint64() - uint64(self.defaultBlockAge))
+ if block != nil {
+ statedb := state.New(block.Root(), self.db)
+ self.useState(statedb)
+ }
+ }
+ }
case <-timer.C:
self.logMut.Lock()
self.messagesMut.Lock()
@@ -82,7 +103,7 @@ done:
for id, filter := range self.messages {
if time.Since(filter.timeout) > 20*time.Second {
- self.xeth.Whisper().Unwatch(id)
+ self.xeth().Whisper().Unwatch(id)
delete(self.messages, id)
}
}
@@ -130,7 +151,7 @@ func (self *EthereumApi) WatchTx(args string, reply *interface{}) error {
func (self *EthereumApi) NewFilter(args *FilterOptions, reply *interface{}) error {
var id int
- filter := core.NewFilter(self.xeth.Backend())
+ filter := core.NewFilter(self.xeth().Backend())
filter.SetOptions(toFilterOptions(args))
filter.LogsCallback = func(logs state.Logs) {
self.logMut.Lock()
@@ -155,7 +176,7 @@ func (self *EthereumApi) UninstallFilter(id int, reply *interface{}) error {
func (self *EthereumApi) NewFilterString(args string, reply *interface{}) error {
var id int
- filter := core.NewFilter(self.xeth.Backend())
+ filter := core.NewFilter(self.xeth().Backend())
callback := func(block *types.Block) {
self.logMut.Lock()
@@ -200,7 +221,7 @@ func (self *EthereumApi) Logs(id int, reply *interface{}) error {
}
func (self *EthereumApi) AllLogs(args *FilterOptions, reply *interface{}) error {
- filter := core.NewFilter(self.xeth.Backend())
+ filter := core.NewFilter(self.xeth().Backend())
filter.SetOptions(toFilterOptions(args))
*reply = toLogs(filter.Find())
@@ -211,13 +232,28 @@ func (self *EthereumApi) AllLogs(args *FilterOptions, reply *interface{}) error
func (p *EthereumApi) GetBlock(args *GetBlockArgs, reply *interface{}) error {
// This seems a bit precarious Maybe worth splitting to discrete functions
if len(args.Hash) > 0 {
- *reply = p.xeth.BlockByHash(args.Hash)
+ *reply = p.xeth().BlockByHash(args.Hash)
} else {
- *reply = p.xeth.BlockByNumber(args.BlockNumber)
+ *reply = p.xeth().BlockByNumber(args.BlockNumber)
}
return nil
}
+/*
+func unlockAccount(server, account *Account) bool {
+ pwd, status := server.PasswordDialog()
+ switch status {
+ case Ok:
+ if !account.Unlock([]byte(pwd)) {
+ return unlockAccount(account)
+ }
+ return true
+ default:
+ return false
+ }
+}
+*/
+
func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error {
if len(args.Gas) == 0 {
args.Gas = defaultGas.String()
@@ -228,18 +264,35 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) error {
}
// TODO if no_private_key then
- if _, exists := p.register[args.From]; exists {
- p.register[args.From] = append(p.register[args.From], args)
- } else {
- result, _ := p.xeth.Transact( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
- *reply = result
- }
+ //if _, exists := p.register[args.From]; exists {
+ // p.register[args.From] = append(p.register[args.From], args)
+ //} else {
+ /*
+ account := accounts.Get(fromHex(args.From))
+ if account != nil {
+ if account.Unlocked() {
+ if !unlockAccount(account) {
+ return
+ }
+ }
+
+ result, _ := account.Transact(fromHex(args.To), fromHex(args.Value), fromHex(args.Gas), fromHex(args.GasPrice), fromHex(args.Data))
+ if len(result) > 0 {
+ *reply = toHex(result)
+ }
+ } else if _, exists := p.register[args.From]; exists {
+ p.register[ags.From] = append(p.register[args.From], args)
+ }
+ */
+ result, _ := p.xeth().Transact( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
+ *reply = result
+ //}
return nil
}
func (p *EthereumApi) Call(args *NewTxArgs, reply *interface{}) error {
- result, err := p.xeth.Call( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
+ result, err := p.xeth().Call( /* TODO specify account */ args.To, args.Value, args.Gas, args.GasPrice, args.Data)
if err != nil {
return err
}
@@ -253,7 +306,7 @@ func (p *EthereumApi) PushTx(args *PushTxArgs, reply *interface{}) error {
if err != nil {
return err
}
- result, _ := p.xeth.PushTx(args.Tx)
+ result, _ := p.xeth().PushTx(args.Tx)
*reply = result
return nil
}
@@ -264,7 +317,7 @@ func (p *EthereumApi) GetStateAt(args *GetStateArgs, reply *interface{}) error {
return err
}
- state := p.xeth.State().SafeGet(args.Address)
+ state := p.xeth().State().SafeGet(args.Address)
value := state.StorageString(args.Key)
var hx string
@@ -286,37 +339,37 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) err
return err
}
- *reply = p.xeth.State().SafeGet(args.Address).Storage()
+ *reply = p.xeth().State().SafeGet(args.Address).Storage()
return nil
}
func (p *EthereumApi) GetPeerCount(reply *interface{}) error {
- *reply = p.xeth.PeerCount()
+ *reply = p.xeth().PeerCount()
return nil
}
func (p *EthereumApi) GetIsListening(reply *interface{}) error {
- *reply = p.xeth.IsListening()
+ *reply = p.xeth().IsListening()
return nil
}
func (p *EthereumApi) GetCoinbase(reply *interface{}) error {
- *reply = p.xeth.Coinbase()
+ *reply = p.xeth().Coinbase()
return nil
}
func (p *EthereumApi) Accounts(reply *interface{}) error {
- *reply = p.xeth.Accounts()
+ *reply = p.xeth().Accounts()
return nil
}
func (p *EthereumApi) GetIsMining(reply *interface{}) error {
- *reply = p.xeth.IsMining()
+ *reply = p.xeth().IsMining()
return nil
}
func (p *EthereumApi) SetMining(shouldmine bool, reply *interface{}) error {
- *reply = p.xeth.SetMining(shouldmine)
+ *reply = p.xeth().SetMining(shouldmine)
return nil
}
@@ -332,7 +385,7 @@ func (p *EthereumApi) SetDefaultBlockAge(defaultBlockAge int, reply *interface{}
}
func (p *EthereumApi) BlockNumber(reply *interface{}) error {
- *reply = p.xeth.Backend().ChainManager().CurrentBlock().Number()
+ *reply = p.xeth().Backend().ChainManager().CurrentBlock().Number()
return nil
}
@@ -341,7 +394,7 @@ func (p *EthereumApi) GetTxCountAt(args *GetTxCountArgs, reply *interface{}) err
if err != nil {
return err
}
- *reply = p.xeth.TxCountAt(args.Address)
+ *reply = p.xeth().TxCountAt(args.Address)
return nil
}
@@ -350,7 +403,7 @@ func (p *EthereumApi) GetBalanceAt(args *GetBalanceArgs, reply *interface{}) err
if err != nil {
return err
}
- state := p.xeth.State().SafeGet(args.Address)
+ state := p.xeth().State().SafeGet(args.Address)
*reply = toHex(state.Balance().Bytes())
return nil
}
@@ -360,7 +413,7 @@ func (p *EthereumApi) GetCodeAt(args *GetCodeAtArgs, reply *interface{}) error {
if err != nil {
return err
}
- *reply = p.xeth.CodeAt(args.Address)
+ *reply = p.xeth().CodeAt(args.Address)
return nil
}
@@ -407,7 +460,7 @@ func (p *EthereumApi) DbGet(args *DbArgs, reply *interface{}) error {
}
func (p *EthereumApi) NewWhisperIdentity(reply *interface{}) error {
- *reply = p.xeth.Whisper().NewIdentity()
+ *reply = p.xeth().Whisper().NewIdentity()
return nil
}
@@ -418,7 +471,7 @@ func (p *EthereumApi) NewWhisperFilter(args *xeth.Options, reply *interface{}) e
defer p.messagesMut.Unlock()
p.messages[id].add(msg) // = append(p.messages[id], msg)
}
- id = p.xeth.Whisper().Watch(args)
+ id = p.xeth().Whisper().Watch(args)
p.messages[id] = &whisperFilter{timeout: time.Now()}
*reply = id
return nil
@@ -436,7 +489,7 @@ func (self *EthereumApi) MessagesChanged(id int, reply *interface{}) error {
}
func (p *EthereumApi) WhisperPost(args *WhisperMessageArgs, reply *interface{}) error {
- err := p.xeth.Whisper().Post(args.Payload, args.To, args.From, args.Topic, args.Priority, args.Ttl)
+ err := p.xeth().Whisper().Post(args.Payload, args.To, args.From, args.Topic, args.Priority, args.Ttl)
if err != nil {
return err
}
@@ -446,12 +499,12 @@ func (p *EthereumApi) WhisperPost(args *WhisperMessageArgs, reply *interface{})
}
func (p *EthereumApi) HasWhisperIdentity(args string, reply *interface{}) error {
- *reply = p.xeth.Whisper().HasIdentity(args)
+ *reply = p.xeth().Whisper().HasIdentity(args)
return nil
}
func (p *EthereumApi) WhisperMessages(id int, reply *interface{}) error {
- *reply = p.xeth.Whisper().Messages(id)
+ *reply = p.xeth().Whisper().Messages(id)
return nil
}
@@ -655,3 +708,28 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
rpclogger.DebugDetailf("Reply: %T %s", reply, reply)
return nil
}
+
+func (self *EthereumApi) xeth() *xeth.XEth {
+ self.xethMu.RLock()
+ defer self.xethMu.RUnlock()
+
+ return self.eth
+}
+
+func (self *EthereumApi) useState(statedb *state.StateDB) {
+ self.xethMu.Lock()
+ defer self.xethMu.Unlock()
+
+ self.eth = self.xeth().UseState(statedb)
+}
+
+func t(f ui.Frontend) {
+ // Call the password dialog
+ ret, err := f.Call("PasswordDialog")
+ if err != nil {
+ fmt.Println(err)
+ }
+ // Get the first argument
+ t, _ := ret.Get(0)
+ fmt.Println("return:", t)
+}
diff --git a/ui/frontend.go b/ui/frontend.go
new file mode 100644
index 000000000..22dc64fdf
--- /dev/null
+++ b/ui/frontend.go
@@ -0,0 +1,18 @@
+package ui
+
+// ReturnInterface is returned by the Intercom interface when a method is called
+type ReturnInterface interface {
+ Get(i int) (interface{}, error)
+ Size() int
+}
+
+// Frontend is the basic interface for calling arbitrary methods on something that
+// implements a front end (GUI, CLI, etc)
+type Frontend interface {
+ // Checks whether a specific method is implemented
+ Supports(method string) bool
+ // Call calls the given method on interface it implements. This will return
+ // an error with errNotImplemented if the method hasn't been implemented
+ // and will return a ReturnInterface if it does.
+ Call(method string) (ReturnInterface, error)
+}
diff --git a/xeth/world.go b/xeth/state.go
index 9cbdd9461..e2562613c 100644
--- a/xeth/world.go
+++ b/xeth/state.go
@@ -3,19 +3,20 @@ package xeth
import "github.com/ethereum/go-ethereum/state"
type State struct {
- xeth *XEth
+ xeth *XEth
+ state *state.StateDB
}
-func NewState(xeth *XEth) *State {
- return &State{xeth}
+func NewState(xeth *XEth, statedb *state.StateDB) *State {
+ return &State{xeth, statedb}
}
func (self *State) State() *state.StateDB {
- return self.xeth.chainManager.TransState()
+ return self.state
}
func (self *State) Get(addr string) *Object {
- return &Object{self.State().GetStateObject(fromHex(addr))}
+ return &Object{self.state.GetStateObject(fromHex(addr))}
}
func (self *State) SafeGet(addr string) *Object {
@@ -23,7 +24,7 @@ func (self *State) SafeGet(addr string) *Object {
}
func (self *State) safeGet(addr string) *state.StateObject {
- object := self.State().GetStateObject(fromHex(addr))
+ object := self.state.GetStateObject(fromHex(addr))
if object == nil {
object = state.NewStateObject(fromHex(addr), self.xeth.eth.Db())
}
diff --git a/xeth/xeth.go b/xeth/xeth.go
index 2985ce982..d4c188fec 100644
--- a/xeth/xeth.go
+++ b/xeth/xeth.go
@@ -17,6 +17,7 @@ import (
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/whisper"
)
@@ -54,13 +55,26 @@ func New(eth Backend) *XEth {
whisper: NewWhisper(eth.Whisper()),
miner: eth.Miner(),
}
- xeth.state = NewState(xeth)
+ xeth.state = NewState(xeth, xeth.chainManager.TransState())
return xeth
}
-func (self *XEth) Backend() Backend { return self.eth }
-func (self *XEth) State() *State { return self.state }
+func (self *XEth) Backend() Backend { return self.eth }
+func (self *XEth) UseState(statedb *state.StateDB) *XEth {
+ xeth := &XEth{
+ eth: self.eth,
+ blockProcessor: self.blockProcessor,
+ chainManager: self.chainManager,
+ whisper: self.whisper,
+ miner: self.miner,
+ }
+
+ xeth.state = NewState(xeth, statedb)
+ return xeth
+}
+func (self *XEth) State() *State { return self.state }
+
func (self *XEth) Whisper() *Whisper { return self.whisper }
func (self *XEth) Miner() *miner.Miner { return self.miner }
@@ -229,7 +243,7 @@ func (self *XEth) Call(toStr, valueStr, gasStr, gasPriceStr, dataStr string) (st
}
var (
- statedb = self.chainManager.TransState()
+ statedb = self.State().State() //self.chainManager.TransState()
key = self.eth.KeyManager().KeyPair()
from = statedb.GetOrNewStateObject(key.Address())
block = self.chainManager.CurrentBlock()