From 6d41402dcee01e4e1d03f461b5566df3dfe8e080 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 15:36:39 +0100 Subject: [PATCH 01/11] Backend no longer needed to resolve import cycle --- xeth/xeth.go | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 98f5f5b36..3cf3b6d61 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -15,12 +15,10 @@ import ( "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/miner" - "github.com/ethereum/go-ethereum/p2p" - "github.com/ethereum/go-ethereum/whisper" ) var ( @@ -30,30 +28,6 @@ var ( defaultGas = big.NewInt(90000) //500000 ) -// to resolve the import cycle -type Backend interface { - BlockProcessor() *core.BlockProcessor - ChainManager() *core.ChainManager - AccountManager() *accounts.Manager - TxPool() *core.TxPool - PeerCount() int - IsListening() bool - Peers() []*p2p.Peer - BlockDb() common.Database - StateDb() common.Database - ExtraDb() common.Database - EventMux() *event.TypeMux - Whisper() *whisper.Whisper - Miner() *miner.Miner - - IsMining() bool - StartMining() error - StopMining() - Version() string - ProtocolVersion() int - NetworkId() int -} - // Frontend should be implemented by users of XEth. Its methods are // called whenever XEth makes a decision that requires user input. type Frontend interface { @@ -82,7 +56,7 @@ func (dummyFrontend) UnlockAccount([]byte) bool { return false func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true } type XEth struct { - eth Backend + eth *eth.Ethereum blockProcessor *core.BlockProcessor chainManager *core.ChainManager accountManager *accounts.Manager @@ -110,7 +84,7 @@ type XEth struct { // New creates an XEth that uses the given frontend. // If a nil Frontend is provided, a default frontend which // confirms all transactions will be used. -func New(eth Backend, frontend Frontend) *XEth { +func New(eth *eth.Ethereum, frontend Frontend) *XEth { xeth := &XEth{ eth: eth, blockProcessor: eth.BlockProcessor(), @@ -195,7 +169,7 @@ func (self *XEth) AtStateNum(num int64) *XEth { return self.WithState(st) } -func (self *XEth) Backend() Backend { return self.eth } +func (self *XEth) Backend() *eth.Ethereum { return self.eth } func (self *XEth) WithState(statedb *state.StateDB) *XEth { xeth := &XEth{ eth: self.eth, From ad420d099aec2122396e5ab8d05e5e3d4e0eb139 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 16:01:44 +0100 Subject: [PATCH 02/11] rename eth to backend --- xeth/state.go | 2 +- xeth/xeth.go | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/xeth/state.go b/xeth/state.go index b0f2ce019..669cf91e6 100644 --- a/xeth/state.go +++ b/xeth/state.go @@ -29,7 +29,7 @@ func (self *State) SafeGet(addr string) *Object { func (self *State) safeGet(addr string) *state.StateObject { object := self.state.GetStateObject(common.HexToAddress(addr)) if object == nil { - object = state.NewStateObject(common.HexToAddress(addr), self.xeth.eth.StateDb()) + object = state.NewStateObject(common.HexToAddress(addr), self.xeth.backend.StateDb()) } return object diff --git a/xeth/xeth.go b/xeth/xeth.go index 3cf3b6d61..1176fef94 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -56,7 +56,7 @@ func (dummyFrontend) UnlockAccount([]byte) bool { return false func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true } type XEth struct { - eth *eth.Ethereum + backend *eth.Ethereum blockProcessor *core.BlockProcessor chainManager *core.ChainManager accountManager *accounts.Manager @@ -86,7 +86,7 @@ type XEth struct { // confirms all transactions will be used. func New(eth *eth.Ethereum, frontend Frontend) *XEth { xeth := &XEth{ - eth: eth, + backend: eth, blockProcessor: eth.BlockProcessor(), chainManager: eth.ChainManager(), accountManager: eth.AccountManager(), @@ -169,10 +169,10 @@ func (self *XEth) AtStateNum(num int64) *XEth { return self.WithState(st) } -func (self *XEth) Backend() *eth.Ethereum { return self.eth } +func (self *XEth) Backend() *eth.Ethereum { return self.backend } func (self *XEth) WithState(statedb *state.StateDB) *XEth { xeth := &XEth{ - eth: self.eth, + backend: self.backend, blockProcessor: self.blockProcessor, chainManager: self.chainManager, whisper: self.whisper, @@ -200,7 +200,7 @@ func (self *XEth) EthBlockByHash(strHash string) *types.Block { } func (self *XEth) EthTransactionByHash(hash string) *types.Transaction { - data, _ := self.eth.ExtraDb().Get(common.FromHex(hash)) + data, _ := self.backend.ExtraDb().Get(common.FromHex(hash)) if len(data) != 0 { return types.NewTransactionFromBytes(data) } @@ -247,7 +247,7 @@ func (self *XEth) Block(v interface{}) *Block { func (self *XEth) Accounts() []string { // TODO: check err? - accounts, _ := self.eth.AccountManager().Accounts() + accounts, _ := self.backend.AccountManager().Accounts() accountAddresses := make([]string, len(accounts)) for i, ac := range accounts { accountAddresses[i] = common.ToHex(ac.Address) @@ -256,31 +256,31 @@ func (self *XEth) Accounts() []string { } func (self *XEth) PeerCount() int { - return self.eth.PeerCount() + return self.backend.PeerCount() } func (self *XEth) IsMining() bool { - return self.eth.IsMining() + return self.backend.IsMining() } func (self *XEth) SetMining(shouldmine bool) bool { - ismining := self.eth.IsMining() + ismining := self.backend.IsMining() if shouldmine && !ismining { - err := self.eth.StartMining() + err := self.backend.StartMining() return err == nil } if ismining && !shouldmine { - self.eth.StopMining() + self.backend.StopMining() } - return self.eth.IsMining() + return self.backend.IsMining() } func (self *XEth) IsListening() bool { - return self.eth.IsListening() + return self.backend.IsListening() } func (self *XEth) Coinbase() string { - cb, _ := self.eth.AccountManager().Coinbase() + cb, _ := self.backend.AccountManager().Coinbase() return common.ToHex(cb) } @@ -517,7 +517,7 @@ func (self *XEth) FromNumber(str string) string { func (self *XEth) PushTx(encodedTx string) (string, error) { tx := types.NewTransactionFromBytes(common.FromHex(encodedTx)) - err := self.eth.TxPool().Add(tx) + err := self.backend.TxPool().Add(tx) if err != nil { return "", err } @@ -616,7 +616,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt if err := self.sign(tx, from, false); err != nil { return "", err } - if err := self.eth.TxPool().Add(tx); err != nil { + if err := self.backend.TxPool().Add(tx); err != nil { return "", err } From b9b7442b74a3a533f3b6eeefe6a1415d59eb8ccf Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 16:08:18 +0100 Subject: [PATCH 03/11] Remove redundant fields --- xeth/xeth.go | 57 ++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 1176fef94..7ec464609 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -56,12 +56,9 @@ func (dummyFrontend) UnlockAccount([]byte) bool { return false func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true } type XEth struct { - backend *eth.Ethereum - blockProcessor *core.BlockProcessor - chainManager *core.ChainManager - accountManager *accounts.Manager - state *State - whisper *Whisper + backend *eth.Ethereum + state *State + whisper *Whisper frontend Frontend @@ -86,24 +83,21 @@ type XEth struct { // confirms all transactions will be used. func New(eth *eth.Ethereum, frontend Frontend) *XEth { xeth := &XEth{ - backend: eth, - blockProcessor: eth.BlockProcessor(), - chainManager: eth.ChainManager(), - accountManager: eth.AccountManager(), - whisper: NewWhisper(eth.Whisper()), - quit: make(chan struct{}), - filterManager: filter.NewFilterManager(eth.EventMux()), - frontend: frontend, - logs: make(map[int]*logFilter), - messages: make(map[int]*whisperFilter), - agent: miner.NewRemoteAgent(), + backend: eth, + whisper: NewWhisper(eth.Whisper()), + quit: make(chan struct{}), + filterManager: filter.NewFilterManager(eth.EventMux()), + frontend: frontend, + logs: make(map[int]*logFilter), + messages: make(map[int]*whisperFilter), + agent: miner.NewRemoteAgent(), } eth.Miner().Register(xeth.agent) if frontend == nil { xeth.frontend = dummyFrontend{} } - xeth.state = NewState(xeth, xeth.chainManager.TransState()) + xeth.state = NewState(xeth, xeth.backend.ChainManager().TransState()) go xeth.start() go xeth.filterManager.Start() @@ -172,10 +166,7 @@ func (self *XEth) AtStateNum(num int64) *XEth { func (self *XEth) Backend() *eth.Ethereum { return self.backend } func (self *XEth) WithState(statedb *state.StateDB) *XEth { xeth := &XEth{ - backend: self.backend, - blockProcessor: self.blockProcessor, - chainManager: self.chainManager, - whisper: self.whisper, + backend: self.backend, } xeth.state = NewState(xeth, statedb) @@ -187,14 +178,14 @@ func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) BlockByHash(strHash string) *Block { hash := common.HexToHash(strHash) - block := self.chainManager.GetBlock(hash) + block := self.backend.ChainManager().GetBlock(hash) return NewBlock(block) } func (self *XEth) EthBlockByHash(strHash string) *types.Block { hash := common.HexToHash(strHash) - block := self.chainManager.GetBlock(hash) + block := self.backend.ChainManager().GetBlock(hash) return block } @@ -214,10 +205,10 @@ func (self *XEth) BlockByNumber(num int64) *Block { } if num == -1 { - return NewBlock(self.chainManager.CurrentBlock()) + return NewBlock(self.backend.ChainManager().CurrentBlock()) } - return NewBlock(self.chainManager.GetBlockByNumber(uint64(num))) + return NewBlock(self.backend.ChainManager().GetBlockByNumber(uint64(num))) } func (self *XEth) EthBlockByNumber(num int64) *types.Block { @@ -227,10 +218,10 @@ func (self *XEth) EthBlockByNumber(num int64) *types.Block { } if num == -1 { - return self.chainManager.CurrentBlock() + return self.backend.ChainManager().CurrentBlock() } - return self.chainManager.GetBlockByNumber(uint64(num)) + return self.backend.ChainManager().GetBlockByNumber(uint64(num)) } func (self *XEth) Block(v interface{}) *Block { @@ -530,7 +521,7 @@ func (self *XEth) PushTx(encodedTx string) (string, error) { } func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr string) (string, error) { - statedb := self.State().State() //self.chainManager.TransState() + statedb := self.State().State() //self.eth.ChainManager().TransState() msg := callmsg{ from: statedb.GetOrNewStateObject(common.HexToAddress(fromStr)), to: common.HexToAddress(toStr), @@ -547,8 +538,8 @@ func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr st msg.gasPrice = defaultGasPrice } - block := self.chainManager.CurrentBlock() - vmenv := core.NewEnv(statedb, self.chainManager, msg, block) + block := self.backend.ChainManager().CurrentBlock() + vmenv := core.NewEnv(statedb, self.backend.ChainManager(), msg, block) res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value) return common.ToHex(res), err @@ -609,7 +600,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt tx = types.NewTransactionMessage(to, value.BigInt(), gas, price, data) } - state := self.chainManager.TxState() + state := self.backend.ChainManager().TxState() nonce := state.NewNonce(from) tx.SetNonce(nonce) @@ -630,7 +621,7 @@ func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeSt } func (self *XEth) sign(tx *types.Transaction, from common.Address, didUnlock bool) error { - sig, err := self.accountManager.Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes()) + sig, err := self.backend.AccountManager().Sign(accounts.Account{Address: from.Bytes()}, tx.Hash().Bytes()) if err == accounts.ErrLocked { if didUnlock { return fmt.Errorf("sender account still locked after successful unlock") From 59b6b619a2d257b6165f9b18dcab566a16eee2a2 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 16:33:37 +0100 Subject: [PATCH 04/11] Don't expose backend directly --- rpc/api.go | 6 +++--- xeth/xeth.go | 23 +++++++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/rpc/api.go b/rpc/api.go index 4bc199176..37ab6e1d4 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -60,9 +60,9 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err } *reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data))) case "web3_clientVersion": - *reply = api.xeth().Backend().Version() + *reply = api.xeth().ClientVersion() case "net_version": - *reply = string(api.xeth().Backend().ProtocolVersion()) + *reply = api.xeth().NetworkVersion() case "net_listening": *reply = api.xeth().IsListening() case "net_peerCount": @@ -84,7 +84,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err case "eth_accounts": *reply = api.xeth().Accounts() case "eth_blockNumber": - v := api.xeth().Backend().ChainManager().CurrentBlock().Number() + v := api.xeth().CurrentBlock().Number() *reply = common.ToHex(v.Bytes()) case "eth_getBalance": args := new(GetBalanceArgs) diff --git a/xeth/xeth.go b/xeth/xeth.go index 7ec464609..b7d10cac3 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -143,7 +143,7 @@ func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice } func (self *XEth) RemoteMining() *miner.RemoteAgent { return self.agent } func (self *XEth) AtStateNum(num int64) *XEth { - chain := self.Backend().ChainManager() + chain := self.backend.ChainManager() var block *types.Block // -1 generally means "latest" @@ -156,14 +156,13 @@ func (self *XEth) AtStateNum(num int64) *XEth { var st *state.StateDB if block != nil { - st = state.New(block.Root(), self.Backend().StateDb()) + st = state.New(block.Root(), self.backend.StateDb()) } else { st = chain.State() } return self.WithState(st) } -func (self *XEth) Backend() *eth.Ethereum { return self.backend } func (self *XEth) WithState(statedb *state.StateDB) *XEth { xeth := &XEth{ backend: self.backend, @@ -224,6 +223,10 @@ func (self *XEth) EthBlockByNumber(num int64) *types.Block { return self.backend.ChainManager().GetBlockByNumber(uint64(num)) } +func (self *XEth) CurrentBlock() *types.Block { + return self.backend.ChainManager().CurrentBlock() +} + func (self *XEth) Block(v interface{}) *Block { if n, ok := v.(int32); ok { return self.BlockByNumber(int64(n)) @@ -254,6 +257,14 @@ func (self *XEth) IsMining() bool { return self.backend.IsMining() } +func (self *XEth) NetworkVersion() string { + return string(self.backend.ProtocolVersion()) +} + +func (self *XEth) ClientVersion() string { + return self.backend.Version() +} + func (self *XEth) SetMining(shouldmine bool) bool { ismining := self.backend.IsMining() if shouldmine && !ismining { @@ -314,7 +325,7 @@ func (self *XEth) SecretToAddress(key string) string { func (self *XEth) RegisterFilter(args *core.FilterOptions) int { var id int - filter := core.NewFilter(self.Backend()) + filter := core.NewFilter(self.backend) filter.SetOptions(args) filter.LogsCallback = func(logs state.Logs) { self.logMut.Lock() @@ -340,7 +351,7 @@ func (self *XEth) UninstallFilter(id int) bool { func (self *XEth) NewFilterString(word string) int { var id int - filter := core.NewFilter(self.Backend()) + filter := core.NewFilter(self.backend) switch word { case "pending": @@ -392,7 +403,7 @@ func (self *XEth) Logs(id int) state.Logs { } func (self *XEth) AllLogs(args *core.FilterOptions) state.Logs { - filter := core.NewFilter(self.Backend()) + filter := core.NewFilter(self.backend) filter.SetOptions(args) return filter.Find() From 65d553d367d698b26a9586bd709d561d00b4f5a8 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 16:34:28 +0100 Subject: [PATCH 05/11] Update internal calls to use CurrentBlock() --- xeth/xeth.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index b7d10cac3..92b48e8d8 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -204,7 +204,7 @@ func (self *XEth) BlockByNumber(num int64) *Block { } if num == -1 { - return NewBlock(self.backend.ChainManager().CurrentBlock()) + return NewBlock(self.CurrentBlock()) } return NewBlock(self.backend.ChainManager().GetBlockByNumber(uint64(num))) @@ -217,7 +217,7 @@ func (self *XEth) EthBlockByNumber(num int64) *types.Block { } if num == -1 { - return self.backend.ChainManager().CurrentBlock() + return self.CurrentBlock() } return self.backend.ChainManager().GetBlockByNumber(uint64(num)) @@ -549,7 +549,7 @@ func (self *XEth) Call(fromStr, toStr, valueStr, gasStr, gasPriceStr, dataStr st msg.gasPrice = defaultGasPrice } - block := self.backend.ChainManager().CurrentBlock() + block := self.CurrentBlock() vmenv := core.NewEnv(statedb, self.backend.ChainManager(), msg, block) res, err := vmenv.Call(msg.from, msg.to, msg.data, msg.gas, msg.gasPrice, msg.value) From 2106a63d60e261608dc71a30f07b6b905fc78f2f Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 16:42:33 +0100 Subject: [PATCH 06/11] Move Frontend interface to separate file --- xeth/frontend.go | 32 ++++++++++++++++++++++++++++++++ xeth/xeth.go | 27 --------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) create mode 100644 xeth/frontend.go diff --git a/xeth/frontend.go b/xeth/frontend.go new file mode 100644 index 000000000..8deb5c98c --- /dev/null +++ b/xeth/frontend.go @@ -0,0 +1,32 @@ +package xeth + +import ( + "github.com/ethereum/go-ethereum/core/types" +) + +// Frontend should be implemented by users of XEth. Its methods are +// called whenever XEth makes a decision that requires user input. +type Frontend interface { + // UnlockAccount is called when a transaction needs to be signed + // but the key corresponding to the transaction's sender is + // locked. + // + // It should unlock the account with the given address and return + // true if unlocking succeeded. + UnlockAccount(address []byte) bool + + // This is called for all transactions inititated through + // Transact. It should prompt the user to confirm the transaction + // and return true if the transaction was acknowledged. + // + // ConfirmTransaction is not used for Call transactions + // because they cannot change any state. + ConfirmTransaction(tx *types.Transaction) bool +} + +// dummyFrontend is a non-interactive frontend that allows all +// transactions but cannot not unlock any keys. +type dummyFrontend struct{} + +func (dummyFrontend) UnlockAccount([]byte) bool { return false } +func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true } diff --git a/xeth/xeth.go b/xeth/xeth.go index 92b48e8d8..565e96412 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -28,33 +28,6 @@ var ( defaultGas = big.NewInt(90000) //500000 ) -// Frontend should be implemented by users of XEth. Its methods are -// called whenever XEth makes a decision that requires user input. -type Frontend interface { - // UnlockAccount is called when a transaction needs to be signed - // but the key corresponding to the transaction's sender is - // locked. - // - // It should unlock the account with the given address and return - // true if unlocking succeeded. - UnlockAccount(address []byte) bool - - // This is called for all transactions inititated through - // Transact. It should prompt the user to confirm the transaction - // and return true if the transaction was acknowledged. - // - // ConfirmTransaction is not used for Call transactions - // because they cannot change any state. - ConfirmTransaction(tx *types.Transaction) bool -} - -// dummyFrontend is a non-interactive frontend that allows all -// transactions but cannot not unlock any keys. -type dummyFrontend struct{} - -func (dummyFrontend) UnlockAccount([]byte) bool { return false } -func (dummyFrontend) ConfirmTransaction(*types.Transaction) bool { return true } - type XEth struct { backend *eth.Ethereum state *State From 865f31018b7c4151a412cdc2466a912fb5869037 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 17:45:31 +0100 Subject: [PATCH 07/11] Shuffle --- xeth/xeth.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 565e96412..4bab92904 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -29,12 +29,12 @@ var ( ) type XEth struct { - backend *eth.Ethereum + backend *eth.Ethereum + frontend Frontend + state *State whisper *Whisper - frontend Frontend - quit chan struct{} filterManager *filter.FilterManager @@ -47,7 +47,6 @@ type XEth struct { // regmut sync.Mutex // register map[string][]*interface{} // TODO improve return type - // Miner agent agent *miner.RemoteAgent } @@ -57,10 +56,10 @@ type XEth struct { func New(eth *eth.Ethereum, frontend Frontend) *XEth { xeth := &XEth{ backend: eth, + frontend: frontend, whisper: NewWhisper(eth.Whisper()), quit: make(chan struct{}), filterManager: filter.NewFilterManager(eth.EventMux()), - frontend: frontend, logs: make(map[int]*logFilter), messages: make(map[int]*whisperFilter), agent: miner.NewRemoteAgent(), @@ -71,6 +70,7 @@ func New(eth *eth.Ethereum, frontend Frontend) *XEth { xeth.frontend = dummyFrontend{} } xeth.state = NewState(xeth, xeth.backend.ChainManager().TransState()) + go xeth.start() go xeth.filterManager.Start() @@ -144,6 +144,7 @@ func (self *XEth) WithState(statedb *state.StateDB) *XEth { xeth.state = NewState(xeth, statedb) return xeth } + func (self *XEth) State() *State { return self.state } func (self *XEth) Whisper() *Whisper { return self.whisper } From 82b5a8af49c7c56296e1d1a2303eb53d6f50734f Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 17:49:37 +0100 Subject: [PATCH 08/11] DRY up height logic --- xeth/xeth.go | 51 ++++++++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 4bab92904..28d1916b0 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -116,22 +116,13 @@ func (self *XEth) DefaultGasPrice() *big.Int { return defaultGasPrice } func (self *XEth) RemoteMining() *miner.RemoteAgent { return self.agent } func (self *XEth) AtStateNum(num int64) *XEth { - chain := self.backend.ChainManager() - var block *types.Block - - // -1 generally means "latest" - // -2 means "pending", which has no blocknum - if num < 0 { - num = chain.CurrentBlock().Number().Int64() - } - - block = chain.GetBlockByNumber(uint64(num)) + block := self.getBlockByHeight(num) var st *state.StateDB if block != nil { st = state.New(block.Root(), self.backend.StateDb()) } else { - st = chain.State() + st = self.backend.ChainManager().State() } return self.WithState(st) } @@ -149,6 +140,22 @@ func (self *XEth) State() *State { return self.state } func (self *XEth) Whisper() *Whisper { return self.whisper } +func (self *XEth) getBlockByHeight(height int64) *types.Block { + var num uint64 + + // -1 means "latest" + // -2 means "pending", which has no blocknum + if height <= -2 { + return &types.Block{} + } else if height == -1 { + num = self.CurrentBlock().NumberU64() + } else { + num = uint64(height) + } + + return self.backend.ChainManager().GetBlockByNumber(num) +} + func (self *XEth) BlockByHash(strHash string) *Block { hash := common.HexToHash(strHash) block := self.backend.ChainManager().GetBlock(hash) @@ -172,29 +179,11 @@ func (self *XEth) EthTransactionByHash(hash string) *types.Transaction { } func (self *XEth) BlockByNumber(num int64) *Block { - if num == -2 { - // "pending" is non-existant - return &Block{} - } - - if num == -1 { - return NewBlock(self.CurrentBlock()) - } - - return NewBlock(self.backend.ChainManager().GetBlockByNumber(uint64(num))) + return NewBlock(self.getBlockByHeight(num)) } func (self *XEth) EthBlockByNumber(num int64) *types.Block { - if num == -2 { - // "pending" is non-existant - return &types.Block{} - } - - if num == -1 { - return self.CurrentBlock() - } - - return self.backend.ChainManager().GetBlockByNumber(uint64(num)) + return self.getBlockByHeight(num) } func (self *XEth) CurrentBlock() *types.Block { From d41341f77d03a7ab8483cd9df4bc71ac08bbfc7c Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 24 Mar 2015 17:58:16 +0100 Subject: [PATCH 09/11] WithState -> withState --- xeth/xeth.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index 28d1916b0..8296e61bb 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -124,10 +124,11 @@ func (self *XEth) AtStateNum(num int64) *XEth { } else { st = self.backend.ChainManager().State() } - return self.WithState(st) + + return self.withState(st) } -func (self *XEth) WithState(statedb *state.StateDB) *XEth { +func (self *XEth) withState(statedb *state.StateDB) *XEth { xeth := &XEth{ backend: self.backend, } From c956bcb13c287baf1d4ae1b2c9ae83d43423d67f Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 25 Mar 2015 12:08:48 +0100 Subject: [PATCH 10/11] Move version to const and expose via Version() --- whisper/whisper.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/whisper/whisper.go b/whisper/whisper.go index dbd4fc85f..1d019aea5 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -16,8 +16,9 @@ import ( ) const ( - statusMsg = 0x0 - envelopesMsg = 0x01 + statusMsg = 0x0 + envelopesMsg = 0x01 + whisperVersion = 0x02 ) type MessageEvent struct { @@ -56,7 +57,7 @@ func New() *Whisper { // p2p whisper sub protocol handler whisper.protocol = p2p.Protocol{ Name: "shh", - Version: 2, + Version: uint(whisperVersion), Length: 2, Run: whisper.msgHandler, } @@ -64,6 +65,10 @@ func New() *Whisper { return whisper } +func (self *Whisper) Version() uint { + return self.protocol.Version +} + func (self *Whisper) Start() { wlogger.Infoln("Whisper started") go self.update() From 2b93843d86532db3d6b530daf15c04fde0b73eba Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 25 Mar 2015 12:09:55 +0100 Subject: [PATCH 11/11] Improve protocol version reporting --- eth/backend.go | 39 +++++++++++++++++++++------------------ rpc/api.go | 6 +++++- xeth/xeth.go | 12 ++++++++++-- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index 3f7f7c2cb..cf5bd2c92 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -138,11 +138,12 @@ type Ethereum struct { // logger logger.LogSystem - Mining bool - DataDir string - version string - protocolVersion int - networkId int + Mining bool + DataDir string + clientVersion string + ethVersionId int + netVersionId int + shhVersionId int } func New(config *Config) (*Ethereum, error) { @@ -177,16 +178,16 @@ func New(config *Config) (*Ethereum, error) { servlogger.Infof("Protocol Version: %v, Network Id: %v", config.ProtocolVersion, config.NetworkId) eth := &Ethereum{ - shutdownChan: make(chan bool), - blockDb: blockDb, - stateDb: stateDb, - extraDb: extraDb, - eventMux: &event.TypeMux{}, - accountManager: config.AccountManager, - DataDir: config.DataDir, - version: config.Name, // TODO should separate from Name - protocolVersion: config.ProtocolVersion, - networkId: config.NetworkId, + shutdownChan: make(chan bool), + blockDb: blockDb, + stateDb: stateDb, + extraDb: extraDb, + eventMux: &event.TypeMux{}, + accountManager: config.AccountManager, + DataDir: config.DataDir, + clientVersion: config.Name, // TODO should separate from Name + ethVersionId: config.ProtocolVersion, + netVersionId: config.NetworkId, } eth.chainManager = core.NewChainManager(blockDb, stateDb, eth.EventMux()) @@ -195,6 +196,7 @@ func New(config *Config) (*Ethereum, error) { eth.blockProcessor = core.NewBlockProcessor(stateDb, extraDb, eth.pow, eth.txPool, eth.chainManager, eth.EventMux()) eth.chainManager.SetProcessor(eth.blockProcessor) eth.whisper = whisper.New() + eth.shhVersionId = int(eth.whisper.Version()) eth.miner = miner.New(eth, eth.pow, config.MinerThreads) hasBlock := eth.chainManager.HasBlock @@ -324,9 +326,10 @@ func (s *Ethereum) IsListening() bool { return true } // Alwa func (s *Ethereum) PeerCount() int { return s.net.PeerCount() } func (s *Ethereum) Peers() []*p2p.Peer { return s.net.Peers() } func (s *Ethereum) MaxPeers() int { return s.net.MaxPeers } -func (s *Ethereum) Version() string { return s.version } -func (s *Ethereum) ProtocolVersion() int { return s.protocolVersion } -func (s *Ethereum) NetworkId() int { return s.networkId } +func (s *Ethereum) ClientVersion() string { return s.clientVersion } +func (s *Ethereum) EthVersion() int { return s.ethVersionId } +func (s *Ethereum) NetVersion() int { return s.netVersionId } +func (s *Ethereum) ShhVersion() int { return s.shhVersionId } // Start the ethereum func (s *Ethereum) Start() error { diff --git a/rpc/api.go b/rpc/api.go index 37ab6e1d4..aa5b54199 100644 --- a/rpc/api.go +++ b/rpc/api.go @@ -49,7 +49,7 @@ func (api *EthereumApi) Close() { } func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error { - // Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC + // Spec at https://github.com/ethereum/wiki/wiki/JSON-RPC rpclogger.Debugf("%s %s", req.Method, req.Params) switch req.Method { @@ -68,6 +68,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err case "net_peerCount": v := api.xeth().PeerCount() *reply = common.ToHex(big.NewInt(int64(v)).Bytes()) + case "eth_version": + *reply = api.xeth().EthVersion() case "eth_coinbase": // TODO handling of empty coinbase due to lack of accounts res := api.xeth().Coinbase() @@ -406,6 +408,8 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err res, _ := api.db.Get([]byte(args.Database + args.Key)) *reply = common.ToHex(res) + case "shh_version": + *reply = api.xeth().WhisperVersion() case "shh_post": args := new(WhisperMessageArgs) if err := json.Unmarshal(req.Params, &args); err != nil { diff --git a/xeth/xeth.go b/xeth/xeth.go index 8296e61bb..36c9979f4 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -221,12 +221,20 @@ func (self *XEth) IsMining() bool { return self.backend.IsMining() } +func (self *XEth) EthVersion() string { + return string(self.backend.EthVersion()) +} + func (self *XEth) NetworkVersion() string { - return string(self.backend.ProtocolVersion()) + return string(self.backend.NetVersion()) +} + +func (self *XEth) WhisperVersion() string { + return string(self.backend.ShhVersion()) } func (self *XEth) ClientVersion() string { - return self.backend.Version() + return self.backend.ClientVersion() } func (self *XEth) SetMining(shouldmine bool) bool {