diff options
author | Felix Lange <fjl@twurst.com> | 2015-02-05 10:16:16 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-02-06 07:03:59 +0800 |
commit | 56f777b2fc77275bc636562b66a08b19afe2ec56 (patch) | |
tree | c9a34bd23e13adb0d850957b9a66cd748d34b3e3 /eth | |
parent | 8e8ec8f5f8974aafeac5e4a9ead76878fe22cefd (diff) | |
download | go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.tar.gz go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.tar.zst go-tangerine-56f777b2fc77275bc636562b66a08b19afe2ec56.zip |
cmd/ethereum, cmd/mist, core, eth, javascript, xeth: fixes for new p2p API
Diffstat (limited to 'eth')
-rw-r--r-- | eth/backend.go | 73 | ||||
-rw-r--r-- | eth/protocol.go | 3 | ||||
-rw-r--r-- | eth/protocol_test.go | 24 |
3 files changed, 37 insertions, 63 deletions
diff --git a/eth/backend.go b/eth/backend.go index 43e757435..08052c15d 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -12,20 +12,19 @@ import ( "github.com/ethereum/go-ethereum/event" ethlogger "github.com/ethereum/go-ethereum/logger" "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/rpc" "github.com/ethereum/go-ethereum/whisper" ) type Config struct { - Name string - Version string - Identifier string - KeyStore string - DataDir string - LogFile string - LogLevel int - KeyRing string + Name string + KeyStore string + DataDir string + LogFile string + LogLevel int + KeyRing string MaxPeers int Port string @@ -66,8 +65,7 @@ type Ethereum struct { WsServer rpc.RpcServer keyManager *crypto.KeyManager - clientIdentity p2p.ClientIdentity - logger ethlogger.LogSystem + logger ethlogger.LogSystem synclock sync.Mutex syncGroup sync.WaitGroup @@ -103,21 +101,17 @@ func New(config *Config) (*Ethereum, error) { // Initialise the keyring 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) //ethutil.Config.Db = db eth := &Ethereum{ - shutdownChan: make(chan bool), - quit: make(chan bool), - db: db, - keyManager: keyManager, - clientIdentity: clientId, - blacklist: p2p.NewBlacklist(), - eventMux: &event.TypeMux{}, - logger: logger, + shutdownChan: make(chan bool), + quit: make(chan bool), + db: db, + keyManager: keyManager, + blacklist: p2p.NewBlacklist(), + eventMux: &event.TypeMux{}, + logger: logger, } 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) protocols := []p2p.Protocol{ethProto, eth.whisper.Protocol()} - nat, err := p2p.ParseNAT(config.NATType, config.PMPGateway) if err != nil { return nil, err } - + netprv, err := crypto.GenerateKey() + if err != nil { + return nil, fmt.Errorf("could not generate server key: %v", err) + } eth.net = &p2p.Server{ - Identity: clientId, - MaxPeers: config.MaxPeers, - Protocols: protocols, - Blacklist: eth.blacklist, - NAT: nat, - NoDial: !config.Dial, + PrivateKey: netprv, + Name: config.Name, + MaxPeers: config.MaxPeers, + Protocols: protocols, + Blacklist: eth.blacklist, + NAT: nat, + NoDial: !config.Dial, } - if len(config.Port) > 0 { eth.net.ListenAddr = ":" + config.Port } @@ -162,8 +158,8 @@ func (s *Ethereum) Logger() ethlogger.LogSystem { return s.logger } -func (s *Ethereum) ClientIdentity() p2p.ClientIdentity { - return s.clientIdentity +func (s *Ethereum) Name() string { + return s.net.Name } func (s *Ethereum) ChainManager() *core.ChainManager { @@ -241,26 +237,17 @@ func (s *Ethereum) Start(seedNode string) error { s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{}) 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") 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) if err != nil { logger.Errorf("couldn't resolve %s:", addr, err) return err } - - self.net.SuggestPeer(netaddr.IP, netaddr.Port, nil) + self.net.SuggestPeer(netaddr.IP, netaddr.Port, id) return nil } diff --git a/eth/protocol.go b/eth/protocol.go index d7a7fa910..fb694c877 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -92,13 +92,14 @@ func EthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool) // the main loop that handles incoming messages // note RemovePeer in the post-disconnect hook func runEthProtocol(txPool txPool, chainManager chainManager, blockPool blockPool, peer *p2p.Peer, rw p2p.MsgReadWriter) (err error) { + id := peer.ID() self := ðProtocol{ txPool: txPool, chainManager: chainManager, blockPool: blockPool, rw: rw, peer: peer, - id: fmt.Sprintf("%x", peer.Identity().Pubkey()[:8]), + id: fmt.Sprintf("%x", id[:8]), } err = self.handleStatus() if err == nil { diff --git a/eth/protocol_test.go b/eth/protocol_test.go index 1fe6d8f6b..a91806a1c 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -14,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/ethutil" ethlogger "github.com/ethereum/go-ethereum/logger" "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)) @@ -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 { - 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 { |