-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dht | ||
|
||
import ( | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/multiformats/go-multiaddr" | ||
mh "github.com/multiformats/go-multihash" | ||
mhreg "github.com/multiformats/go-multihash/core" | ||
|
||
"github.com/plprobelab/go-kademlia/kad" | ||
"github.com/plprobelab/go-kademlia/key" | ||
) | ||
|
||
type peerID struct { | ||
peer.ID | ||
} | ||
|
||
var _ kad.NodeID[key.Key256] = (*peerID)(nil) | ||
|
||
func newPeerID(p peer.ID) *peerID { | ||
return &peerID{p} | ||
} | ||
|
||
func (id peerID) Key() key.Key256 { | ||
hasher, _ := mhreg.GetHasher(mh.SHA2_256) | ||
hasher.Write([]byte(id.ID)) | ||
return key.NewKey256(hasher.Sum(nil)) | ||
} | ||
|
||
func (id peerID) NodeID() kad.NodeID[key.Key256] { | ||
return &id | ||
} | ||
|
||
func (id peerID) String() string { | ||
return id.ID.String() | ||
} | ||
|
||
type addrInfo struct { | ||
peer.AddrInfo | ||
} | ||
|
||
var _ kad.NodeInfo[key.Key256, multiaddr.Multiaddr] = (*addrInfo)(nil) | ||
|
||
func newAddrInfo(ai peer.AddrInfo) *addrInfo { | ||
return &addrInfo{ | ||
AddrInfo: ai, | ||
} | ||
} | ||
|
||
func (ai addrInfo) Key() key.Key256 { | ||
return newPeerID(ai.AddrInfo.ID).Key() | ||
} | ||
|
||
func (ai addrInfo) ID() kad.NodeID[key.Key256] { | ||
return newPeerID(ai.AddrInfo.ID) | ||
} | ||
|
||
func (ai addrInfo) Addresses() []multiaddr.Multiaddr { | ||
addrs := make([]multiaddr.Multiaddr, len(ai.Addrs)) | ||
copy(addrs, ai.Addrs) | ||
return addrs | ||
} | ||
|
||
func (ai addrInfo) String() string { | ||
return ai.AddrInfo.ID.String() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dht_pb | ||
|
||
import ( | ||
"errors" | ||
|
||
pb "github.com/ipfs/boxo/bitswap/message/pb" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/multiformats/go-multiaddr" | ||
|
||
"github.com/plprobelab/go-kademlia/kad" | ||
Check failure on line 11 in pb/helpers.go GitHub Actions / ubuntu (go 1.19.x)
Check failure on line 11 in pb/helpers.go GitHub Actions / ubuntu (go 1.20.x)
Check failure on line 11 in pb/helpers.go GitHub Actions / macos (go 1.19.x)
Check failure on line 11 in pb/helpers.go GitHub Actions / macos (go 1.20.x)
Check failure on line 11 in pb/helpers.go GitHub Actions / All
Check failure on line 11 in pb/helpers.go GitHub Actions / windows (go 1.20.x)
|
||
"github.com/plprobelab/go-kademlia/key" | ||
Check failure on line 12 in pb/helpers.go GitHub Actions / ubuntu (go 1.19.x)
Check failure on line 12 in pb/helpers.go GitHub Actions / ubuntu (go 1.20.x)
Check failure on line 12 in pb/helpers.go GitHub Actions / macos (go 1.19.x)
Check failure on line 12 in pb/helpers.go GitHub Actions / macos (go 1.20.x)
Check failure on line 12 in pb/helpers.go GitHub Actions / All
Check failure on line 12 in pb/helpers.go GitHub Actions / windows (go 1.20.x)
|
||
) | ||
|
||
var ErrNoValidAddresses = errors.New("no valid addresses") | ||
|
||
var ( | ||
_ kad.Request[key.Key256, multiaddr.Multiaddr] = (*pb.Message)(nil) | ||
_ kad.Response[key.Key256, multiaddr.Multiaddr] = (*pb.Message)(nil) | ||
) | ||
|
||
func (m *Message) Target() key.Key256 { | ||
p, err := peer.IDFromBytes(m.GetKey()) | ||
if err != nil { | ||
return key.ZeroKey256() | ||
} | ||
return key.NewKey256([]byte(p.String())) | ||
} | ||
|
||
func (m *Message) EmptyResponse() kad.Response[key.Key256, multiaddr.Multiaddr] { | ||
return &Message{} | ||
} | ||
|
||
func (m *Message) CloserNodes() []kad.NodeInfo[key.Key256, multiaddr.Multiaddr] { | ||
closerPeers := m.GetCloserPeers() | ||
if closerPeers == nil { | ||
return []kad.NodeInfo[key.Key256, multiaddr.Multiaddr]{} | ||
} | ||
return ParsePeers(closerPeers) | ||
} | ||
|
||
func ParsePeers(pbps []*Message_Peer) []kad.NodeInfo[key.Key256, multiaddr.Multiaddr] { | ||
peers := make([]kad.NodeInfo[key.Key256, multiaddr.Multiaddr], 0, len(pbps)) | ||
for _, p := range pbps { | ||
pi, err := PBPeerToPeerInfo(p) | ||
if err == nil { | ||
peers = append(peers, pi) | ||
} | ||
} | ||
return peers | ||
} | ||
|
||
func PBPeerToPeerInfo(pbp *Message_Peer) (*peer.AddrInfo, error) { | ||
addrs := make([]multiaddr.Multiaddr, 0, len(pbp.Addrs)) | ||
for _, a := range pbp.Addrs { | ||
addr, err := multiaddr.NewMultiaddrBytes(a) | ||
if err == nil { | ||
addrs = append(addrs, addr) | ||
} | ||
} | ||
if len(addrs) == 0 { | ||
return nil, ErrNoValidAddresses | ||
} | ||
|
||
return kad.NewAddrInfo(peer.AddrInfo{ | ||
ID: peer.ID(pbp.Id), | ||
Addrs: addrs, | ||
}), nil | ||
} |