diff options
author | Felix Lange <fjl@twurst.com> | 2015-03-18 20:39:01 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-03-18 20:39:01 +0800 |
commit | d15f90645d5b2e90c441140972d9fe6ecd753643 (patch) | |
tree | b1f024c3c01f25b67bc25ed0452a9b584dad984f /eth | |
parent | a59dd393e71cc52b1f96973aef884af619166f38 (diff) | |
parent | 0a1eeca41e6ba5920ba65d9b41654768299bc7e3 (diff) | |
download | go-tangerine-d15f90645d5b2e90c441140972d9fe6ecd753643.tar.gz go-tangerine-d15f90645d5b2e90c441140972d9fe6ecd753643.tar.zst go-tangerine-d15f90645d5b2e90c441140972d9fe6ecd753643.zip |
Merge remote-tracking branch 'ethereum/conversion' into conversion
Diffstat (limited to 'eth')
-rw-r--r-- | eth/backend.go | 78 | ||||
-rw-r--r-- | eth/protocol.go | 61 |
2 files changed, 101 insertions, 38 deletions
diff --git a/eth/backend.go b/eth/backend.go index 42f2b5808..c26bdceb5 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -10,11 +10,11 @@ import ( "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/blockpool" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/miner" @@ -219,6 +219,64 @@ func New(config *Config) (*Ethereum, error) { return eth, nil } +type NodeInfo struct { + Name string + NodeUrl string + NodeID string + IP string + DiscPort int // UDP listening port for discovery protocol + TCPPort int // TCP listening port for RLPx + Td string + ListenAddr string +} + +func (s *Ethereum) NodeInfo() *NodeInfo { + node := s.net.Self() + + return &NodeInfo{ + Name: s.Name(), + NodeUrl: node.String(), + NodeID: node.ID.String(), + IP: node.IP.String(), + DiscPort: node.DiscPort, + TCPPort: node.TCPPort, + ListenAddr: s.net.ListenAddr, + Td: s.ChainManager().Td().String(), + } +} + +type PeerInfo struct { + ID string + Name string + Caps string + RemoteAddress string + LocalAddress string +} + +func newPeerInfo(peer *p2p.Peer) *PeerInfo { + var caps []string + for _, cap := range peer.Caps() { + caps = append(caps, cap.String()) + } + return &PeerInfo{ + ID: peer.ID().String(), + Name: peer.Name(), + Caps: strings.Join(caps, ", "), + RemoteAddress: peer.RemoteAddr().String(), + LocalAddress: peer.LocalAddr().String(), + } +} + +// PeersInfo returns an array of PeerInfo objects describing connected peers +func (s *Ethereum) PeersInfo() (peersinfo []*PeerInfo) { + for _, peer := range s.net.Peers() { + if peer != nil { + peersinfo = append(peersinfo, newPeerInfo(peer)) + } + } + return +} + func (s *Ethereum) ResetWithGenesisBlock(gb *types.Block) { s.chainManager.ResetWithGenesisBlock(gb) s.pow.UpdateCache(true) @@ -230,7 +288,7 @@ func (s *Ethereum) StartMining() error { servlogger.Errorf("Cannot start mining without coinbase: %v\n", err) return fmt.Errorf("no coinbase: %v", err) } - s.miner.Start(cb) + s.miner.Start(common.BytesToAddress(cb)) return nil } @@ -246,11 +304,12 @@ func (s *Ethereum) TxPool() *core.TxPool { return s.txPool } func (s *Ethereum) BlockPool() *blockpool.BlockPool { return s.blockPool } func (s *Ethereum) Whisper() *whisper.Whisper { return s.whisper } func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } -func (s *Ethereum) BlockDb() common.Database { return s.blockDb } -func (s *Ethereum) StateDb() common.Database { return s.stateDb } -func (s *Ethereum) ExtraDb() common.Database { return s.extraDb } +func (s *Ethereum) BlockDb() common.Database { return s.blockDb } +func (s *Ethereum) StateDb() common.Database { return s.stateDb } +func (s *Ethereum) ExtraDb() common.Database { return s.extraDb } func (s *Ethereum) IsListening() bool { return true } // Always listening func (s *Ethereum) PeerCount() int { return s.net.PeerCount() } +func (s *Ethereum) PeerInfo() 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 } @@ -262,9 +321,11 @@ func (s *Ethereum) Start() error { ProtocolVersion: ProtocolVersion, }) - err := s.net.Start() - if err != nil { - return err + if s.net.MaxPeers > 0 { + err := s.net.Start() + if err != nil { + return err + } } // Start services @@ -311,6 +372,7 @@ func (s *Ethereum) Stop() { // Close the database defer s.blockDb.Close() defer s.stateDb.Close() + defer s.extraDb.Close() s.txSub.Unsubscribe() // quits txBroadcastLoop s.blockSub.Unsubscribe() // quits blockBroadcastLoop diff --git a/eth/protocol.go b/eth/protocol.go index e368bbec5..15a8e1831 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -1,13 +1,12 @@ package eth import ( - "bytes" "fmt" "math/big" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/errs" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rlp" @@ -76,15 +75,15 @@ type txPool interface { } type chainManager interface { - GetBlockHashesFromHash(hash []byte, amount uint64) (hashes [][]byte) - GetBlock(hash []byte) (block *types.Block) - Status() (td *big.Int, currentBlock []byte, genesisBlock []byte) + GetBlockHashesFromHash(hash common.Hash, amount uint64) (hashes []common.Hash) + GetBlock(hash common.Hash) (block *types.Block) + Status() (td *big.Int, currentBlock common.Hash, genesisBlock common.Hash) } type blockPool interface { - AddBlockHashes(next func() ([]byte, bool), peerId string) + AddBlockHashes(next func() (common.Hash, bool), peerId string) AddBlock(block *types.Block, peerId string) - AddPeer(td *big.Int, currentBlock []byte, peerId string, requestHashes func([]byte) error, requestBlocks func([][]byte) error, peerError func(*errs.Error)) (best bool) + AddPeer(td *big.Int, currentBlock common.Hash, peerId string, requestHashes func(common.Hash) error, requestBlocks func([]common.Hash) error, peerError func(*errs.Error)) (best bool) RemovePeer(peerId string) } @@ -95,7 +94,7 @@ type newBlockMsgData struct { } type getBlockHashesMsgData struct { - Hash []byte + Hash common.Hash Amount uint64 } @@ -167,7 +166,7 @@ func (self *ethProtocol) handle() error { } for _, tx := range txs { jsonlogger.LogJson(&logger.EthTxReceived{ - TxHash: common.Bytes2Hex(tx.Hash()), + TxHash: tx.Hash().Hex(), RemoteId: self.peer.ID().String(), }) } @@ -183,7 +182,7 @@ func (self *ethProtocol) handle() error { request.Amount = maxHashes } hashes := self.chainManager.GetBlockHashesFromHash(request.Hash, request.Amount) - return p2p.EncodeMsg(self.rw, BlockHashesMsg, common.ByteSliceToInterface(hashes)...) + return p2p.EncodeMsg(self.rw, BlockHashesMsg, rlp.Flat(hashes)) case BlockHashesMsg: msgStream := rlp.NewStream(msg.Payload) @@ -192,14 +191,16 @@ func (self *ethProtocol) handle() error { } var i int - iter := func() (hash []byte, ok bool) { - hash, err := msgStream.Bytes() + iter := func() (hash common.Hash, ok bool) { + var h common.Hash + err := msgStream.Decode(&h) if err == rlp.EOL { - return nil, false + return common.Hash{}, false } else if err != nil { self.protoError(ErrDecode, "msg %v: after %v hashes : %v", msg, i, err) - return nil, false + return common.Hash{}, false } + i++ return hash, true } @@ -215,14 +216,14 @@ func (self *ethProtocol) handle() error { var i int for { i++ - var hash []byte - if err := msgStream.Decode(&hash); err != nil { - if err == rlp.EOL { - break - } else { - return self.protoError(ErrDecode, "msg %v: %v", msg, err) - } + var hash common.Hash + err := msgStream.Decode(&hash) + if err == rlp.EOL { + break + } else { + return self.protoError(ErrDecode, "msg %v: %v", msg, err) } + block := self.chainManager.GetBlock(hash) if block != nil { blocks = append(blocks, block) @@ -259,10 +260,10 @@ func (self *ethProtocol) handle() error { _, chainHead, _ := self.chainManager.Status() jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{ - BlockHash: common.Bytes2Hex(hash), + BlockHash: hash.Hex(), BlockNumber: request.Block.Number(), // this surely must be zero - ChainHeadHash: common.Bytes2Hex(chainHead), - BlockPrevHash: common.Bytes2Hex(request.Block.ParentHash()), + ChainHeadHash: chainHead.Hex(), + BlockPrevHash: request.Block.ParentHash().Hex(), RemoteId: self.peer.ID().String(), }) // to simplify backend interface adding a new block @@ -282,8 +283,8 @@ type statusMsgData struct { ProtocolVersion uint32 NetworkId uint32 TD *big.Int - CurrentBlock []byte - GenesisBlock []byte + CurrentBlock common.Hash + GenesisBlock common.Hash } func (self *ethProtocol) statusMsg() p2p.Msg { @@ -325,7 +326,7 @@ func (self *ethProtocol) handleStatus() error { _, _, genesisBlock := self.chainManager.Status() - if !bytes.Equal(status.GenesisBlock, genesisBlock) { + if status.GenesisBlock != genesisBlock { return self.protoError(ErrGenesisBlockMismatch, "%x (!= %x)", status.GenesisBlock, genesisBlock) } @@ -344,14 +345,14 @@ func (self *ethProtocol) handleStatus() error { return nil } -func (self *ethProtocol) requestBlockHashes(from []byte) error { +func (self *ethProtocol) requestBlockHashes(from common.Hash) error { self.peer.Debugf("fetching hashes (%d) %x...\n", maxHashes, from[0:4]) return p2p.EncodeMsg(self.rw, GetBlockHashesMsg, interface{}(from), uint64(maxHashes)) } -func (self *ethProtocol) requestBlocks(hashes [][]byte) error { +func (self *ethProtocol) requestBlocks(hashes []common.Hash) error { self.peer.Debugf("fetching %v blocks", len(hashes)) - return p2p.EncodeMsg(self.rw, GetBlocksMsg, common.ByteSliceToInterface(hashes)...) + return p2p.EncodeMsg(self.rw, GetBlocksMsg, rlp.Flat(hashes)) } func (self *ethProtocol) protoError(code int, format string, params ...interface{}) (err *errs.Error) { |