-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
164 lines (135 loc) · 4.22 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2019 The go-vnt Authors
// This file is part of the go-vnt library.
//
// The go-vnt library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-vnt library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>.
package vntp2p
import (
"context"
"crypto/rand"
"sync"
"time"
util "github.com/ipfs/go-ipfs-util"
dht "github.com/libp2p/go-libp2p-kad-dht"
peer "github.com/libp2p/go-libp2p-peer"
routing "github.com/libp2p/go-libp2p-routing"
"github.com/vntchain/go-vnt/log"
)
const (
refreshInterval = 30 * time.Second
searchTimeOut = 1 * time.Minute
)
type DhtTable interface {
Start(ctx context.Context) error
Lookup(ctx context.Context, targetID NodeID) []*NodeID
Update(ctx context.Context, id peer.ID) error
RandomPeer() []peer.ID
GetDhtTable() *dht.IpfsDHT
}
type VNTDht struct {
mutex sync.Mutex
table *dht.IpfsDHT
self peer.ID
}
func NewDHTTable(dht *dht.IpfsDHT, id peer.ID) *VNTDht {
return &VNTDht{
table: dht,
self: id,
}
}
func (vdht *VNTDht) Start(ctx context.Context) error {
var bootStrapConfig = dht.DefaultBootstrapConfig
bootStrapConfig.Period = time.Duration(refreshInterval)
bootStrapConfig.Timeout = time.Duration(searchTimeOut)
proc, err := vdht.table.BootstrapWithConfig(bootStrapConfig)
if err != nil {
log.Debug("Start refresh k-bucket error", "error", err)
return err
}
// wait till ctx or dht.Context exits.
// we have to do it this way to satisfy the Routing interface (contexts)
go func() {
defer proc.Close()
select {
case <-ctx.Done():
case <-vdht.table.Context().Done():
}
}()
return nil
}
func (vdht *VNTDht) Update(ctx context.Context, id peer.ID) error {
vdht.table.Update(ctx, id)
return nil
}
func randomID() peer.ID {
id := make([]byte, 16)
rand.Read(id)
id = util.Hash(id)
// var aid NodeID
// copy(aid, id)
return peer.ID(id)
}
func (vdht *VNTDht) Lookup(ctx context.Context, targetID NodeID) []*NodeID {
// vdht.table.GetClosestPeers(vdht.Context, )
results := vdht.lookup(ctx, targetID.PeerID())
nodeids := []*NodeID{}
for _, result := range results {
nodeid := PeerIDtoNodeID(result)
nodeids = append(nodeids, &nodeid)
}
return nodeids
}
func (vdht *VNTDht) lookup(ctx context.Context, targetid peer.ID) []peer.ID {
// 开始搜寻
// fmt.Println("Begin Lookup")
cctx, cancel := context.WithTimeout(ctx, searchTimeOut)
defer cancel()
runQuery := func(ctxs context.Context, id peer.ID) {
p, err := vdht.table.FindPeer(ctxs, id)
if err == routing.ErrNotFound {
} else if err != nil {
log.Debug("lookup peer occurs error", "error", err)
} else {
log.Debug("lookup peer find peer", "id", id.ToString(), "peer", p.ID.ToString())
}
}
runQuery(cctx, targetid)
return nil
}
func (vdht *VNTDht) doRefresh(ctx context.Context, done chan struct{}) {
// defer close(done)
// Load nodes from the database and insert
// them. This should yield a few previously seen nodes that are
// (hopefully) still alive.
// tab.loadSeedNodes(true)
// Run self lookup to discover new neighbor nodes.
// fmt.Println("@@@CCCC find my self", vdht.self)
vdht.lookup(ctx, vdht.self)
// The Kademlia paper specifies that the bucket refresh should
// perform a lookup in the least recently used bucket. We cannot
// adhere to this because the findnode target is a 512bit value
// (not hash-sized) and it is not easily possible to generate a
// sha3 preimage that falls into a chosen bucket.
// We perform a few lookups with a random target instead.
for i := 0; i < 3; i++ {
target := randomID()
// fmt.Println("random id: ", target)
vdht.lookup(ctx, target)
}
}
func (vdht *VNTDht) RandomPeer() []peer.ID {
return vdht.table.GetRandomPeers()
}
func (vdht *VNTDht) GetDhtTable() *dht.IpfsDHT {
return vdht.table
}