cmd/ethereum, cmd/mist, core, eth, javascript, xeth: fixes for new p2p API

This commit is contained in:
Felix Lange 2015-02-05 03:16:16 +01:00
parent 8e8ec8f5f8
commit 56f777b2fc
16 changed files with 87 additions and 126 deletions

View File

@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
) )
@ -61,13 +62,11 @@ func main() {
utils.InitConfig(VmType, ConfigFile, Datadir, "ETH") utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
ethereum, err := eth.New(&eth.Config{ ethereum, err := eth.New(&eth.Config{
Name: ClientIdentifier, Name: p2p.MakeName(ClientIdentifier, Version),
Version: Version,
KeyStore: KeyStore, KeyStore: KeyStore,
DataDir: Datadir, DataDir: Datadir,
LogFile: LogFile, LogFile: LogFile,
LogLevel: LogLevel, LogLevel: LogLevel,
Identifier: Identifier,
MaxPeers: MaxPeer, MaxPeers: MaxPeer,
Port: OutboundPort, Port: OutboundPort,
NATType: PMPGateway, NATType: PMPGateway,

View File

@ -844,6 +844,7 @@ ApplicationWindow {
minimumHeight: 50 minimumHeight: 50
title: "Connect to peer" title: "Connect to peer"
ComboBox { ComboBox {
id: addrField id: addrField
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
@ -872,6 +873,17 @@ ApplicationWindow {
} }
} }
ComboBox {
id: nodeidField
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.right: addPeerButton.left
anchors.leftMargin: 10
anchors.rightMargin: 10
editable: true
}
Button { Button {
id: addPeerButton id: addPeerButton
anchors.right: parent.right anchors.right: parent.right
@ -879,7 +891,7 @@ ApplicationWindow {
anchors.rightMargin: 10 anchors.rightMargin: 10
text: "Add" text: "Add"
onClicked: { onClicked: {
eth.connectToPeer(addrField.currentText) eth.connectToPeer(addrField.currentText, nodeidField.currentText)
addPeerWin.visible = false addPeerWin.visible = false
} }
} }

View File

@ -32,18 +32,6 @@ Rectangle {
width: 500 width: 500
} }
Label {
text: "Client ID"
}
TextField {
text: gui.getCustomIdentifier()
width: 500
placeholderText: "Anonymous"
onTextChanged: {
gui.setCustomIdentifier(text)
}
}
TextArea { TextArea {
objectName: "statsPane" objectName: "statsPane"
width: parent.width width: parent.width

View File

@ -64,15 +64,6 @@ func (gui *Gui) Transact(recipient, value, gas, gasPrice, d string) (string, err
return gui.xeth.Transact(recipient, value, gas, gasPrice, data) return gui.xeth.Transact(recipient, value, gas, gasPrice, data)
} }
func (gui *Gui) SetCustomIdentifier(customIdentifier string) {
gui.clientIdentity.SetCustomIdentifier(customIdentifier)
gui.config.Save("id", customIdentifier)
}
func (gui *Gui) GetCustomIdentifier() string {
return gui.clientIdentity.GetCustomIdentifier()
}
// functions that allow Gui to implement interface guilogger.LogSystem // functions that allow Gui to implement interface guilogger.LogSystem
func (gui *Gui) SetLogLevel(level logger.LogLevel) { func (gui *Gui) SetLogLevel(level logger.LogLevel) {
gui.logLevel = level gui.logLevel = level

View File

@ -41,7 +41,6 @@ import (
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/ui/qt/qwhisper" "github.com/ethereum/go-ethereum/ui/qt/qwhisper"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
"github.com/obscuren/qml" "github.com/obscuren/qml"
@ -77,9 +76,8 @@ type Gui struct {
xeth *xeth.XEth xeth *xeth.XEth
Session string Session string
clientIdentity *p2p.SimpleClientIdentity config *ethutil.ConfigManager
config *ethutil.ConfigManager
plugins map[string]plugin plugins map[string]plugin
@ -87,7 +85,7 @@ type Gui struct {
} }
// Create GUI, but doesn't start it // Create GUI, but doesn't start it
func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIdentity *p2p.SimpleClientIdentity, session string, logLevel int) *Gui { func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, session string, logLevel int) *Gui {
db, err := ethdb.NewLDBDatabase("tx_database") db, err := ethdb.NewLDBDatabase("tx_database")
if err != nil { if err != nil {
panic(err) panic(err)
@ -95,15 +93,14 @@ func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIden
xeth := xeth.New(ethereum) xeth := xeth.New(ethereum)
gui := &Gui{eth: ethereum, gui := &Gui{eth: ethereum,
txDb: db, txDb: db,
xeth: xeth, xeth: xeth,
logLevel: logger.LogLevel(logLevel), logLevel: logger.LogLevel(logLevel),
Session: session, Session: session,
open: false, open: false,
clientIdentity: clientIdentity, config: config,
config: config, plugins: make(map[string]plugin),
plugins: make(map[string]plugin), serviceEvents: make(chan ServEv, 1),
serviceEvents: make(chan ServEv, 1),
} }
data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "plugins.json")) data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "plugins.json"))
json.Unmarshal([]byte(data), &gui.plugins) json.Unmarshal([]byte(data), &gui.plugins)

View File

@ -52,13 +52,11 @@ func run() error {
config := utils.InitConfig(VmType, ConfigFile, Datadir, "ETH") config := utils.InitConfig(VmType, ConfigFile, Datadir, "ETH")
ethereum, err := eth.New(&eth.Config{ ethereum, err := eth.New(&eth.Config{
Name: ClientIdentifier, Name: p2p.MakeName(ClientIdentifier, Version),
Version: Version,
KeyStore: KeyStore, KeyStore: KeyStore,
DataDir: Datadir, DataDir: Datadir,
LogFile: LogFile, LogFile: LogFile,
LogLevel: LogLevel, LogLevel: LogLevel,
Identifier: Identifier,
MaxPeers: MaxPeer, MaxPeers: MaxPeer,
Port: OutboundPort, Port: OutboundPort,
NATType: PMPGateway, NATType: PMPGateway,
@ -79,7 +77,7 @@ func run() error {
utils.StartWebSockets(ethereum, WsPort) utils.StartWebSockets(ethereum, WsPort)
} }
gui := NewWindow(ethereum, config, ethereum.ClientIdentity().(*p2p.SimpleClientIdentity), KeyRing, LogLevel) gui := NewWindow(ethereum, config, KeyRing, LogLevel)
utils.RegisterInterrupt(func(os.Signal) { utils.RegisterInterrupt(func(os.Signal) {
gui.Stop() gui.Stop()

View File

@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/event/filter"
"github.com/ethereum/go-ethereum/javascript" "github.com/ethereum/go-ethereum/javascript"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
"github.com/obscuren/qml" "github.com/obscuren/qml"
) )
@ -142,8 +143,13 @@ func (ui *UiLib) Connect(button qml.Object) {
} }
} }
func (ui *UiLib) ConnectToPeer(addr string) { func (ui *UiLib) ConnectToPeer(addr string, hexid string) {
if err := ui.eth.SuggestPeer(addr); err != nil { id, err := discover.HexID(hexid)
if err != nil {
guilogger.Errorf("bad node ID: %v", err)
return
}
if err := ui.eth.SuggestPeer(addr, id); err != nil {
guilogger.Infoln(err) guilogger.Infoln(err)
} }
} }

View File

@ -122,12 +122,10 @@ func exit(err error) {
} }
func StartEthereum(ethereum *eth.Ethereum, SeedNode string) { func StartEthereum(ethereum *eth.Ethereum, SeedNode string) {
clilogger.Infof("Starting %s", ethereum.ClientIdentity()) clilogger.Infoln("Starting ", ethereum.Name())
err := ethereum.Start(SeedNode) if err := ethereum.Start(SeedNode); err != nil {
if err != nil {
exit(err) exit(err)
} }
RegisterInterrupt(func(sig os.Signal) { RegisterInterrupt(func(sig os.Signal) {
ethereum.Stop() ethereum.Stop()
logger.Flush() logger.Flush()

View File

@ -34,7 +34,6 @@ type EthManager interface {
IsListening() bool IsListening() bool
Peers() []*p2p.Peer Peers() []*p2p.Peer
KeyManager() *crypto.KeyManager KeyManager() *crypto.KeyManager
ClientIdentity() p2p.ClientIdentity
Db() ethutil.Database Db() ethutil.Database
EventMux() *event.TypeMux EventMux() *event.TypeMux
} }

View File

@ -9,7 +9,6 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/p2p"
) )
// Implement our EthTest Manager // Implement our EthTest Manager
@ -54,13 +53,6 @@ func (tm *TestManager) TxPool() *TxPool {
func (tm *TestManager) EventMux() *event.TypeMux { func (tm *TestManager) EventMux() *event.TypeMux {
return tm.eventMux return tm.eventMux
} }
func (tm *TestManager) Broadcast(msgType p2p.Msg, data []interface{}) {
fmt.Println("Broadcast not implemented")
}
func (tm *TestManager) ClientIdentity() p2p.ClientIdentity {
return nil
}
func (tm *TestManager) KeyManager() *crypto.KeyManager { func (tm *TestManager) KeyManager() *crypto.KeyManager {
return nil return nil
} }

View File

@ -12,20 +12,19 @@ import (
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
ethlogger "github.com/ethereum/go-ethereum/logger" ethlogger "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/pow/ezp"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper" "github.com/ethereum/go-ethereum/whisper"
) )
type Config struct { type Config struct {
Name string Name string
Version string KeyStore string
Identifier string DataDir string
KeyStore string LogFile string
DataDir string LogLevel int
LogFile string KeyRing string
LogLevel int
KeyRing string
MaxPeers int MaxPeers int
Port string Port string
@ -66,8 +65,7 @@ type Ethereum struct {
WsServer rpc.RpcServer WsServer rpc.RpcServer
keyManager *crypto.KeyManager keyManager *crypto.KeyManager
clientIdentity p2p.ClientIdentity logger ethlogger.LogSystem
logger ethlogger.LogSystem
synclock sync.Mutex synclock sync.Mutex
syncGroup sync.WaitGroup syncGroup sync.WaitGroup
@ -103,21 +101,17 @@ func New(config *Config) (*Ethereum, error) {
// Initialise the keyring // Initialise the keyring
keyManager.Init(config.KeyRing, 0, false) keyManager.Init(config.KeyRing, 0, false)
// Create a new client id for this instance. This will help identifying the node on the network
clientId := p2p.NewSimpleClientIdentity(config.Name, config.Version, config.Identifier, keyManager.PublicKey())
saveProtocolVersion(db) saveProtocolVersion(db)
//ethutil.Config.Db = db //ethutil.Config.Db = db
eth := &Ethereum{ eth := &Ethereum{
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
quit: make(chan bool), quit: make(chan bool),
db: db, db: db,
keyManager: keyManager, keyManager: keyManager,
clientIdentity: clientId, blacklist: p2p.NewBlacklist(),
blacklist: p2p.NewBlacklist(), eventMux: &event.TypeMux{},
eventMux: &event.TypeMux{}, logger: logger,
logger: logger,
} }
eth.chainManager = core.NewChainManager(db, eth.EventMux()) eth.chainManager = core.NewChainManager(db, eth.EventMux())
@ -132,21 +126,23 @@ func New(config *Config) (*Ethereum, error) {
ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool) ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool)
protocols := []p2p.Protocol{ethProto, eth.whisper.Protocol()} protocols := []p2p.Protocol{ethProto, eth.whisper.Protocol()}
nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway) nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway)
if err != nil { if err != nil {
return nil, err return nil, err
} }
netprv, err := crypto.GenerateKey()
eth.net = &p2p.Server{ if err != nil {
Identity: clientId, return nil, fmt.Errorf("could not generate server key: %v", err)
MaxPeers: config.MaxPeers, }
Protocols: protocols, eth.net = &p2p.Server{
Blacklist: eth.blacklist, PrivateKey: netprv,
NAT: nat, Name: config.Name,
NoDial: !config.Dial, MaxPeers: config.MaxPeers,
Protocols: protocols,
Blacklist: eth.blacklist,
NAT: nat,
NoDial: !config.Dial,
} }
if len(config.Port) > 0 { if len(config.Port) > 0 {
eth.net.ListenAddr = ":" + config.Port eth.net.ListenAddr = ":" + config.Port
} }
@ -162,8 +158,8 @@ func (s *Ethereum) Logger() ethlogger.LogSystem {
return s.logger return s.logger
} }
func (s *Ethereum) ClientIdentity() p2p.ClientIdentity { func (s *Ethereum) Name() string {
return s.clientIdentity return s.net.Name
} }
func (s *Ethereum) ChainManager() *core.ChainManager { func (s *Ethereum) ChainManager() *core.ChainManager {
@ -241,26 +237,17 @@ func (s *Ethereum) Start(seedNode string) error {
s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{}) s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{})
go s.blockBroadcastLoop() go s.blockBroadcastLoop()
// TODO: read peers here
if len(seedNode) > 0 {
logger.Infof("Connect to seed node %v", seedNode)
if err := s.SuggestPeer(seedNode); err != nil {
logger.Infoln(err)
}
}
logger.Infoln("Server started") logger.Infoln("Server started")
return nil return nil
} }
func (self *Ethereum) SuggestPeer(addr string) error { func (self *Ethereum) SuggestPeer(addr string, id discover.NodeID) error {
netaddr, err := net.ResolveTCPAddr("tcp", addr) netaddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil { if err != nil {
logger.Errorf("couldn't resolve %s:", addr, err) logger.Errorf("couldn't resolve %s:", addr, err)
return err return err
} }
self.net.SuggestPeer(netaddr.IP, netaddr.Port, id)
self.net.SuggestPeer(netaddr.IP, netaddr.Port, nil)
return nil return nil
} }

View File

@ -92,13 +92,14 @@ func EthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool)
// the main loop that handles incoming messages // the main loop that handles incoming messages
// note RemovePeer in the post-disconnect hook // note RemovePeer in the post-disconnect hook
func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) { func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) {
id := peer.ID()
self := &ethProtocol{ self := &ethProtocol{
txPool: txPool, txPool: txPool,
chainManager: chainManager, chainManager: chainManager,
blockPool: blockPool, blockPool: blockPool,
rw: rw, rw: rw,
peer: peer, peer: peer,
id: fmt.Sprintf("%x", peer.Identity().Pubkey()[:8]), id: fmt.Sprintf("%x", id[:8]),
} }
err = self.handleStatus() err = self.handleStatus()
if err == nil { if err == nil {

View File

@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
ethlogger "github.com/ethereum/go-ethereum/logger" ethlogger "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
) )
var sys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel)) var sys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel))
@ -128,26 +129,11 @@ func (self *testBlockPool) RemovePeer(peerId string) {
} }
} }
// TODO: refactor this into p2p/client_identity
type peerId struct {
pubkey []byte
}
func (self *peerId) String() string {
return "test peer"
}
func (self *peerId) Pubkey() (pubkey []byte) {
pubkey = self.pubkey
if len(pubkey) == 0 {
pubkey = crypto.GenerateNewKeyPair().PublicKey
self.pubkey = pubkey
}
return
}
func testPeer() *p2p.Peer { func testPeer() *p2p.Peer {
return p2p.NewPeer(&peerId{}, []p2p.Cap{}) var id discover.NodeID
pk := crypto.GenerateNewKeyPair().PublicKey
copy(id[:], pk)
return p2p.NewPeer(id, "test peer", []p2p.Cap{})
} }
type ethProtocolTester struct { type ethProtocolTester struct {

View File

@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
"github.com/obscuren/otto" "github.com/obscuren/otto"
@ -201,8 +202,15 @@ func (self *JSRE) addPeer(call otto.FunctionCall) otto.Value {
if err != nil { if err != nil {
return otto.FalseValue() return otto.FalseValue()
} }
self.ethereum.SuggestPeer(host) idstr, err := call.Argument(0).ToString()
if err != nil {
return otto.FalseValue()
}
id, err := discover.HexID(idstr)
if err != nil {
return otto.FalseValue()
}
self.ethereum.SuggestPeer(host, id)
return otto.TrueValue() return otto.TrueValue()
} }

View File

@ -215,7 +215,7 @@ func NewPeer(peer *p2p.Peer) *Peer {
return &Peer{ return &Peer{
ref: peer, ref: peer,
Ip: fmt.Sprintf("%v", peer.RemoteAddr()), Ip: fmt.Sprintf("%v", peer.RemoteAddr()),
Version: fmt.Sprintf("%v", peer.Identity()), Version: fmt.Sprintf("%v", peer.ID()),
Caps: fmt.Sprintf("%v", caps), Caps: fmt.Sprintf("%v", caps),
} }
} }

View File

@ -31,7 +31,6 @@ type Backend interface {
IsListening() bool IsListening() bool
Peers() []*p2p.Peer Peers() []*p2p.Peer
KeyManager() *crypto.KeyManager KeyManager() *crypto.KeyManager
ClientIdentity() p2p.ClientIdentity
Db() ethutil.Database Db() ethutil.Database
EventMux() *event.TypeMux EventMux() *event.TypeMux
Whisper() *whisper.Whisper Whisper() *whisper.Whisper