瀏覽代碼

Setup实现了一半

skyfffire 2 年之前
父節點
當前提交
2da1ac0414
共有 2 個文件被更改,包括 58 次插入14 次删除
  1. 36 13
      p2p/conn.go
  2. 22 1
      p2p/server.go

+ 36 - 13
p2p/conn.go

@@ -1,9 +1,32 @@
 package p2p
 package p2p
 
 
-import "net"
+import (
+	"blockchain-go/p2p/enode"
+	"net"
+)
 
 
 type connFlag int32
 type connFlag int32
 
 
+func (f connFlag) String() string {
+	s := ""
+	if f&trustedConn != 0 {
+		s += "-trusted"
+	}
+	if f&dynDialedConn != 0 {
+		s += "-dyndial"
+	}
+	if f&staticDialedConn != 0 {
+		s += "-staticdial"
+	}
+	if f&inboundConn != 0 {
+		s += "-inbound"
+	}
+	if s != "" {
+		s = s[1:]
+	}
+	return s
+}
+
 const (
 const (
 	dynDialedConn connFlag = 1 << iota
 	dynDialedConn connFlag = 1 << iota
 	staticDialedConn
 	staticDialedConn
@@ -15,19 +38,19 @@ const (
 // during the two handshakes.
 // during the two handshakes.
 type conn struct {
 type conn struct {
 	fd net.Conn
 	fd net.Conn
-	//transport
-	//node  *enode.Node
+	transport
+	node  *enode.Node
 	flags connFlag
 	flags connFlag
 	cont  chan error // The run loop uses cont to signal errors to SetupConn.
 	cont  chan error // The run loop uses cont to signal errors to SetupConn.
-	//caps  []Cap      // valid after the protocol handshake
-	name string // valid after the protocol handshake
+	caps  []Cap      // valid after the protocol handshake
+	name  string     // valid after the protocol handshake
 }
 }
 
 
-//func (c *conn) String() string {
-//    s := c.flags.String()
-//    if (c.node.ID() != enode.ID{}) {
-//        s += " " + c.node.ID().String()
-//    }
-//    s += " " + c.fd.RemoteAddr().String()
-//    return s
-//}
+func (c *conn) String() string {
+	s := c.flags.String()
+	if (c.node.ID() != enode.ID{}) {
+		s += " " + c.node.ID().String()
+	}
+	s += " " + c.fd.RemoteAddr().String()
+	return s
+}

+ 22 - 1
p2p/server.go

@@ -5,6 +5,8 @@ import (
 	"blockchain-go/p2p/enode"
 	"blockchain-go/p2p/enode"
 	"blockchain-go/p2p/enr"
 	"blockchain-go/p2p/enr"
 	"blockchain-go/p2p/nat"
 	"blockchain-go/p2p/nat"
+	"blockchain-go/p2p/rlpx"
+	"crypto/ecdsa"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"github.com/ethereum/go-ethereum/crypto"
 	"github.com/ethereum/go-ethereum/crypto"
@@ -186,12 +188,31 @@ func (server *Server) listenLoop() {
 			fmt.Printf("Accepted connection, addr: %v.", fd.RemoteAddr())
 			fmt.Printf("Accepted connection, addr: %v.", fd.RemoteAddr())
 		}
 		}
 		gopool.Submit(func() {
 		gopool.Submit(func() {
-			//server.setupConn(fd, inboundConn, nil)
+			server.SetupConn(fd, inboundConn, nil)
 			slots <- struct{}{}
 			slots <- struct{}{}
 		})
 		})
 	}
 	}
 }
 }
 
 
+func (server *Server) newRLPX(conn net.Conn, dialDest *ecdsa.PublicKey) transport {
+	return &rlpxTransport{conn: rlpx.NewConn(conn, dialDest)}
+}
+
+func (server *Server) SetupConn(fd net.Conn, flags connFlag, dialDesk *enode.Node) error {
+	c := &conn{fd: fd, flags: flags, cont: make(chan error)}
+	c.transport = server.newRLPX(fd, nil)
+
+	err := server.setupConn(c, nil)
+	if err != nil {
+		c.close(err)
+	}
+	return err
+}
+
+func (server *Server) setupConn(c *conn, dialDest *enode.Node) error {
+	return nil
+}
+
 // 配置节点发现逻辑
 // 配置节点发现逻辑
 func (server *Server) setupDiscovery() (err error) {
 func (server *Server) setupDiscovery() (err error) {
 	return nil
 	return nil