OSDN Git Service

enable the fast sync server flag
authorpaladz <453256728@qq.com>
Mon, 25 Jun 2018 11:37:06 +0000 (19:37 +0800)
committerpaladz <453256728@qq.com>
Mon, 25 Jun 2018 11:37:06 +0000 (19:37 +0800)
consensus/general.go
consensus/network.go [deleted file]
consensus/server_flag.go [new file with mode: 0644]
consensus/server_flag_test.go [new file with mode: 0644]
netsync/fetcher.go
netsync/handle.go
netsync/peer.go

index f5ec658..93159f3 100644 (file)
@@ -106,7 +106,7 @@ var TestNetParams = Params{
        Bech32HRPSegwit: "tm",
 }
 
-// TestNetParams is the config for test-net
+// SoloNetParams is the config for test-net
 var SoloNetParams = Params{
        Name:            "solo",
        Bech32HRPSegwit: "sm",
diff --git a/consensus/network.go b/consensus/network.go
deleted file mode 100644 (file)
index 8fcc763..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-package consensus
-
-// ServiceFlag use uint64 to indicate what kind of server this node can provide.
-// one uint64 can represent 64 type of service flag
-type ServiceFlag uint64
-
-const (
-       // SFFullNode is a flag used to indicate a peer is a full node.
-       SFFullNode ServiceFlag = 1 << iota
-
-       DefaultServices = SFFullNode
-)
diff --git a/consensus/server_flag.go b/consensus/server_flag.go
new file mode 100644 (file)
index 0000000..5346026
--- /dev/null
@@ -0,0 +1,19 @@
+package consensus
+
+// ServiceFlag use uint64 to indicate what kind of server this node can provide.
+// one uint64 can represent 64 type of service flag
+type ServiceFlag uint64
+
+const (
+       // SFFullNode is a flag used to indicate a peer is a full node.
+       SFFullNode ServiceFlag = 1 << iota
+       // SFFastSync indicate peer support header first mode
+       SFFastSync
+       // DefaultServices is the server that this node support
+       DefaultServices = SFFullNode | SFFastSync
+)
+
+// IsEnable check does the flag support the input flag function
+func (f ServiceFlag) IsEnable(checkFlag ServiceFlag) bool {
+       return f&checkFlag == checkFlag
+}
diff --git a/consensus/server_flag_test.go b/consensus/server_flag_test.go
new file mode 100644 (file)
index 0000000..996e0a2
--- /dev/null
@@ -0,0 +1,38 @@
+package consensus
+
+import "testing"
+
+func TestIsEnable(t *testing.T) {
+       cases := []struct {
+               baseFlag   ServiceFlag
+               checkFlage ServiceFlag
+               result     bool
+       }{
+               {
+                       baseFlag:   SFFullNode,
+                       checkFlage: SFFullNode,
+                       result:     true,
+               },
+               {
+                       baseFlag:   SFFullNode,
+                       checkFlage: SFFastSync,
+                       result:     false,
+               },
+               {
+                       baseFlag:   SFFullNode | SFFastSync,
+                       checkFlage: SFFullNode,
+                       result:     true,
+               },
+               {
+                       baseFlag:   SFFullNode | SFFastSync,
+                       checkFlage: SFFastSync,
+                       result:     true,
+               },
+       }
+
+       for i, c := range cases {
+               if c.baseFlag.IsEnable(c.checkFlage) != c.result {
+                       t.Errorf("test case #%b got %b, want %b", i, c.baseFlag.IsEnable(c.checkFlage), c.result)
+               }
+       }
+}
index 0fe3e80..1333e87 100644 (file)
@@ -33,7 +33,6 @@ type Fetcher struct {
 
        // Block cache
        queue  *prque.Prque              // Queue containing the import operations (block number sorted)
-       queues map[string]int            // Per peer block counts to prevent memory exhaustion
        queued map[bc.Hash]*blockPending // Set of already queued blocks (to dedup imports)
 }
 
index 0060ab0..e7ae337 100644 (file)
@@ -24,8 +24,7 @@ import (
 
 //SyncManager Sync Manager is responsible for the business layer information synchronization
 type SyncManager struct {
-       networkID uint64
-       sw        *p2p.Switch
+       sw *p2p.Switch
 
        privKey     crypto.PrivKeyEd25519 // local node's p2p key
        chain       *core.Chain
index c48e5ee..870bc44 100644 (file)
@@ -1,11 +1,13 @@
 package netsync
 
 import (
+       "strconv"
        "sync"
 
        log "github.com/sirupsen/logrus"
        "gopkg.in/fatih/set.v0"
 
+       "github.com/bytom/consensus"
        "github.com/bytom/errors"
        "github.com/bytom/p2p"
        "github.com/bytom/p2p/trust"
@@ -27,6 +29,7 @@ const (
 type peer struct {
        mtx      sync.RWMutex
        version  int // Protocol version negotiated
+       services consensus.ServiceFlag
        id       string
        height   uint64
        hash     *bc.Hash
@@ -39,8 +42,16 @@ type peer struct {
 }
 
 func newPeer(height uint64, hash *bc.Hash, Peer *p2p.Peer) *peer {
+       services := consensus.SFFullNode
+       if len(Peer.Other) != 0 {
+               if serviceFlag, err := strconv.ParseUint(Peer.Other[0], 10, 64); err != nil {
+                       services = consensus.ServiceFlag(serviceFlag)
+               }
+       }
+
        return &peer{
                version:     defaultVersion,
+               services:    services,
                id:          Peer.Key,
                height:      height,
                hash:        hash,