@@ -6,55 +6,68 @@ import (
6
6
"fmt"
7
7
"strings"
8
8
9
+ "github.com/CESSProject/cess-bucket/node"
10
+ "github.com/CESSProject/cess-bucket/pkg/logger"
9
11
"github.com/CESSProject/p2p-go/core"
10
12
pubsub "github.com/libp2p/go-libp2p-pubsub"
11
13
"github.com/libp2p/go-libp2p/core/host"
12
14
"github.com/libp2p/go-libp2p/core/peer"
13
15
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
14
16
)
15
17
16
- func subscribe (ctx context.Context , bootnode string , h host. Host ) {
18
+ func subscribe (ctx context.Context , peernode * core. PeerNode , minerRecord node. MinerRecord , l logger. Logger ) {
17
19
18
20
var (
19
21
err error
20
22
room string
21
23
findpeer peer.AddrInfo
22
24
)
23
25
24
- gossipSub , err := pubsub .NewGossipSub (ctx , h )
26
+ gossipSub , err := pubsub .NewGossipSub (ctx , peernode . GetHost () )
25
27
if err != nil {
26
-
28
+ fmt . Printf ( "NewGossipSub: %v \n " , err )
27
29
return
28
30
}
29
-
31
+ bootnode := peernode . GetBootnode ()
30
32
if strings .Contains (bootnode , "12D3KooWRm2sQg65y2ZgCUksLsjWmKbBtZ4HRRsGLxbN76XTtC8T" ) {
31
33
room = fmt .Sprintf ("%s-12D3KooWRm2sQg65y2ZgCUksLsjWmKbBtZ4HRRsGLxbN76XTtC8T" , core .NetworkRoom )
32
34
} else if strings .Contains (bootnode , "12D3KooWEGeAp1MvvUrBYQtb31FE1LPg7aHsd1LtTXn6cerZTBBd" ) {
33
35
room = fmt .Sprintf ("%s-12D3KooWEGeAp1MvvUrBYQtb31FE1LPg7aHsd1LtTXn6cerZTBBd" , core .NetworkRoom )
34
36
} else if strings .Contains (bootnode , "12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S" ) {
35
37
room = fmt .Sprintf ("%s-12D3KooWGDk9JJ5F6UPNuutEKSbHrTXnF5eSn3zKaR27amgU6o9S" , core .NetworkRoom )
38
+ } else if strings .Contains (bootnode , "12D3KooWS8a18xoBzwkmUsgGBctNo6QCr6XCpUDR946mTBBUTe83" ) {
39
+ room = fmt .Sprintf ("%s-12D3KooWS8a18xoBzwkmUsgGBctNo6QCr6XCpUDR946mTBBUTe83" , core .NetworkRoom )
40
+ } else if strings .Contains (bootnode , "12D3KooWDWeiiqbpNGAqA5QbDTdKgTtwX8LCShWkTpcyxpRf2jA9" ) {
41
+ room = fmt .Sprintf ("%s-12D3KooWDWeiiqbpNGAqA5QbDTdKgTtwX8LCShWkTpcyxpRf2jA9" , core .NetworkRoom )
42
+ } else if strings .Contains (bootnode , "12D3KooWNcTWWuUWKhjTVDF1xZ38yCoHXoF4aDjnbjsNpeVwj33U" ) {
43
+ room = fmt .Sprintf ("%s-12D3KooWNcTWWuUWKhjTVDF1xZ38yCoHXoF4aDjnbjsNpeVwj33U" , core .NetworkRoom )
36
44
} else {
37
45
room = core .NetworkRoom
38
46
}
39
47
48
+ fmt .Printf ("room: %s\n " , room )
49
+
40
50
// setup local mDNS discovery
41
- if err := setupDiscovery (h ); err != nil {
51
+ if err := setupDiscovery (peernode .GetHost ()); err != nil {
52
+ fmt .Printf ("setupDiscovery: %v" , err )
42
53
return
43
54
}
44
55
45
56
// join the pubsub topic called librum
46
57
topic , err := gossipSub .Join (room )
47
58
if err != nil {
59
+ fmt .Printf ("Join: %v" , err )
48
60
return
49
61
}
50
62
51
63
// subscribe to topic
52
64
subscriber , err := topic .Subscribe ()
53
65
if err != nil {
66
+ fmt .Printf ("Subscribe: %v" , err )
54
67
return
55
68
}
56
69
57
- fmt .Println ("Join room: " , room )
70
+ fmt .Printf ("Join room: %s \n " , room )
58
71
59
72
for {
60
73
msg , err := subscriber .Next (ctx )
@@ -63,7 +76,7 @@ func subscribe(ctx context.Context, bootnode string, h host.Host) {
63
76
}
64
77
65
78
// only consider messages delivered by other peers
66
- if msg .ReceivedFrom == h .ID () {
79
+ if msg .ReceivedFrom == peernode . GetHost () .ID () {
67
80
continue
68
81
}
69
82
@@ -73,6 +86,7 @@ func subscribe(ctx context.Context, bootnode string, h host.Host) {
73
86
}
74
87
75
88
fmt .Println ("got peer: " , findpeer .ID .String ())
89
+ minerRecord .SavePeer (findpeer )
76
90
}
77
91
}
78
92
0 commit comments