"github.com/vapor/common"
"github.com/vapor/crypto/ed25519"
+ "github.com/vapor/crypto/sha3pool"
"github.com/vapor/p2p/netutil"
)
node *Node
}
+func hash(target []byte) common.Hash {
+ var h [32]byte
+ sha3pool.Sum256(h[:], target)
+ return common.BytesToHash(h[:])
+}
+
func newNetwork(conn transport, ourPubkey ed25519.PublicKey, dbPath string, netrestrict *netutil.Netlist) (*Network, error) {
var ourID NodeID
copy(ourID[:], ourPubkey[:nodeIDBits])
// Recompute cpy.sha because the node might not have been
// created by NewNode or ParseNode.
cpy := *n
- cpy.sha = common.BytesToHash(n.ID[:])
+ cpy.sha = hash(n.ID[:])
nursery = append(nursery, &cpy)
}
net.reqRefresh(nursery)
// Resolve searches for a specific node with the given ID.
// It returns nil if the node could not be found.
func (net *Network) Resolve(targetID NodeID) *Node {
- result := net.lookup(common.BytesToHash(targetID[:]), true)
+ result := net.lookup(hash(targetID[:]), true)
for _, n := range result {
if n.ID == targetID {
return n
//
// The local node may be included in the result.
func (net *Network) Lookup(targetID NodeID) []*Node {
- return net.lookup(common.BytesToHash(targetID[:]), false)
+ return net.lookup(hash(targetID[:]), false)
}
func (net *Network) lookup(target common.Hash, stopOnMatch bool) []*Node {
// Satisfy queries against the local node directly.
if q.remote == net.tab.self {
log.WithFields(log.Fields{"module": logModule}).Debug("findnodeQuery self")
- closest := net.tab.closest(common.BytesToHash(q.target[:]), bucketSize)
+ closest := net.tab.closest(q.target, bucketSize)
q.reply <- closest.entries
return true
func (net *Network) handleQueryEvent(n *Node, ev nodeEvent, pkt *ingressPacket) (*nodeState, error) {
switch ev {
case findnodePacket:
- target := common.BytesToHash(pkt.data.(*findnode).Target[:])
- results := net.tab.closest(target, bucketSize).entries
+ results := net.tab.closest(hash(pkt.data.(*findnode).Target[:]), bucketSize).entries
net.conn.sendNeighbours(n, results)
return n.state, nil
case neighborsPacket: