diff options
author | Sonic <sonic@cobinhood.com> | 2018-10-15 11:31:40 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@byzantine-lab.io> | 2019-06-12 17:23:39 +0800 |
commit | 7d27d334ed128bf528c8a7fc16674d0a29481087 (patch) | |
tree | c32e491a0b8b722788ef801e873dabf0256f9058 | |
parent | f4d15de52885a472ffb4ae5982662ec3f41ebebc (diff) | |
download | go-tangerine-7d27d334ed128bf528c8a7fc16674d0a29481087.tar.gz go-tangerine-7d27d334ed128bf528c8a7fc16674d0a29481087.tar.zst go-tangerine-7d27d334ed128bf528c8a7fc16674d0a29481087.zip |
dex: add self node meta after Start
-rw-r--r-- | dex/backend.go | 1 | ||||
-rw-r--r-- | dex/handler.go | 34 | ||||
-rw-r--r-- | dex/peer_test.go | 15 | ||||
-rw-r--r-- | dex/protocol.go | 3 | ||||
-rw-r--r-- | p2p/server.go | 4 |
5 files changed, 51 insertions, 6 deletions
diff --git a/dex/backend.go b/dex/backend.go index 0e475dec0..3ceef039b 100644 --- a/dex/backend.go +++ b/dex/backend.go @@ -196,6 +196,7 @@ func (s *Dexon) Start(srvr *p2p.Server) error { } // Start the networking layer and the light server if requested s.protocolManager.Start(srvr, maxPeers) + s.protocolManager.addSelfMeta() return nil } diff --git a/dex/handler.go b/dex/handler.go index 5613efdd8..a6a3627ec 100644 --- a/dex/handler.go +++ b/dex/handler.go @@ -33,6 +33,7 @@ import ( "github.com/dexon-foundation/dexon/consensus" "github.com/dexon-foundation/dexon/core" "github.com/dexon-foundation/dexon/core/types" + "github.com/dexon-foundation/dexon/crypto" "github.com/dexon-foundation/dexon/eth/downloader" "github.com/dexon-foundation/dexon/eth/fetcher" "github.com/dexon-foundation/dexon/ethdb" @@ -130,6 +131,7 @@ func NewProtocolManager( newPeerCh: make(chan *peer), noMorePeers: make(chan struct{}), txsyncCh: make(chan *txsync), + metasyncCh: make(chan *metasync), quitSync: make(chan struct{}), receiveCh: make(chan interface{}, 1024), } @@ -222,8 +224,6 @@ func (pm *ProtocolManager) Start(srvr p2pServer, maxPeers int) { pm.srvr = srvr pm.peers = newPeerSet(pm.gov, pm.srvr, pm.nodeTable) - // if our self in node set build the node info - // broadcast transactions pm.txsCh = make(chan core.NewTxsEvent, txChanSize) pm.txsSub = pm.txpool.SubscribeNewTxsEvent(pm.txsCh) @@ -247,6 +247,36 @@ func (pm *ProtocolManager) Start(srvr p2pServer, maxPeers int) { go pm.syncer() go pm.txsyncLoop() go pm.metasyncLoop() + +} + +func (pm *ProtocolManager) addSelfMeta() { + pm.nodeTable.Add([]*NodeMeta{pm.makeSelfNodeMeta()}) +} + +func (pm *ProtocolManager) makeSelfNodeMeta() *NodeMeta { + self := pm.srvr.Self() + meta := &NodeMeta{ + ID: self.ID(), + IP: self.IP(), + UDP: self.UDP(), + TCP: self.TCP(), + Timestamp: uint64(time.Now().Unix()), + } + + h := rlpHash([]interface{}{ + meta.ID, + meta.IP, + meta.UDP, + meta.TCP, + meta.Timestamp, + }) + sig, err := crypto.Sign(h[:], pm.srvr.GetPrivateKey()) + if err != nil { + panic(err) + } + meta.Sig = sig + return meta } func (pm *ProtocolManager) Stop() { diff --git a/dex/peer_test.go b/dex/peer_test.go index d0262e201..74989258a 100644 --- a/dex/peer_test.go +++ b/dex/peer_test.go @@ -6,13 +6,18 @@ import ( "testing" mapset "github.com/deckarep/golang-set" + "github.com/dexon-foundation/dexon/crypto" "github.com/dexon-foundation/dexon/p2p/discover" "github.com/dexon-foundation/dexon/p2p/enode" ) func TestPeerSetBuildAndForgetNotaryConn(t *testing.T) { self := discover.Node{ID: nodeID(0)} - server := newTestP2PServer(&self) + key, err := crypto.GenerateKey() + if err != nil { + t.Fatal(err) + } + server := newTestP2PServer(&self, key) table := newNodeTable() gov := &testGovernance{ @@ -49,7 +54,6 @@ func TestPeerSetBuildAndForgetNotaryConn(t *testing.T) { ps := newPeerSet(gov, server, table) peer1 := newDummyPeer(nodeID(1)) peer2 := newDummyPeer(nodeID(2)) - var err error err = ps.Register(peer1) if err != nil { t.Error(err) @@ -312,7 +316,11 @@ func TestPeerSetBuildAndForgetNotaryConn(t *testing.T) { func TestPeerSetBuildDKGConn(t *testing.T) { self := discover.Node{ID: nodeID(0)} - server := newTestP2PServer(&self) + key, err := crypto.GenerateKey() + if err != nil { + t.Fatal(err) + } + server := newTestP2PServer(&self, key) table := newNodeTable() gov := &testGovernance{} @@ -329,7 +337,6 @@ func TestPeerSetBuildDKGConn(t *testing.T) { ps := newPeerSet(gov, server, table) peer1 := newDummyPeer(nodeID(1)) peer2 := newDummyPeer(nodeID(2)) - var err error err = ps.Register(peer1) if err != nil { t.Error(err) diff --git a/dex/protocol.go b/dex/protocol.go index 3507965ae..1907d18c8 100644 --- a/dex/protocol.go +++ b/dex/protocol.go @@ -17,6 +17,7 @@ package dex import ( + "crypto/ecdsa" "fmt" "io" "math/big" @@ -134,6 +135,8 @@ type governance interface { type p2pServer interface { Self() *enode.Node + GetPrivateKey() *ecdsa.PrivateKey + AddDirectPeer(*enode.Node) RemoveDirectPeer(*enode.Node) diff --git a/p2p/server.go b/p2p/server.go index a58673342..8cd2863b3 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -441,6 +441,10 @@ func (srv *Server) tcpAddr(listener net.Listener) net.TCPAddr { return addr } +func (srv *Server) GetPrivateKey() *ecdsa.PrivateKey { + return srv.PrivateKey +} + // Stop terminates the server and all active peer connections. // It blocks until all active connections have been closed. func (srv *Server) Stop() { |