1 Star 1 Fork 0

凡卡/libp2parea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
searchNode.go 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
凡卡 提交于 2023-11-29 11:01 . first commit
package unit
import (
"bytes"
"math/big"
"gitee.com/prestonTao/libp2parea"
"gitee.com/prestonTao/libp2parea/engine"
"gitee.com/prestonTao/libp2parea/nodeStore"
"gitee.com/prestonTao/utils"
)
/*
多次查询逻辑节点并对比结果是否正确
*/
func LoopSendSearch(areas []*libp2parea.Area) {
netid := areas[0].GetNetId()
logicIds := GetLogicNetID(&netid)
for i, logicOne := range logicIds {
wantId := globalSearchNode(areas, *logicOne)
for _, one := range areas {
engine.Log.Info("节点:%s 查找:%s want:%s", one.GetNetId().B58String(), logicOne.B58String(), wantId.B58String())
magneticId, err := one.SearchNetAddr(logicOne)
if err != nil {
engine.Log.Error("发送SearchNetAddr消息失败:%s", err.Error())
continue
}
if !bytes.Equal(wantId, *magneticId) {
engine.Log.Error("磁力节点不相等 %d:%s from:%s", i, magneticId.B58String(), one.GetNetId().B58String())
} else {
// engine.Log.Info("磁力节点 %d:%s", i, magnetic.B58String())
}
}
}
}
/*
得到保存数据的逻辑节点
@idStr id十六进制字符串
@return 4分之一节点
*/
func GetLogicNetID(id *nodeStore.AddressNet) (logicIds []*nodeStore.AddressNet) {
bas := nodeStore.BuildArithmeticSequence(100)
logicIds = make([]*nodeStore.AddressNet, 0, len(bas))
idInt := new(big.Int).SetBytes(*id)
for _, one := range bas {
bs := new(big.Int).Xor(idInt, one).Bytes()
newbs := utils.FullHighPositionZero(&bs, 32)
mh := nodeStore.AddressNet(*newbs)
logicIds = append(logicIds, &mh)
}
return
}
func globalSearchNode(areas []*libp2parea.Area, find nodeStore.AddressNet) nodeStore.AddressNet {
// findNetid := nodeStore.AddressFromB58String("ED7buQ4uJy5DSsYNUKmzxgmUVTDQKRxsbKPwtwgBre3W")
kad := nodeStore.NewKademlia(0)
// netBigIds := make([]*big.Int, 0)
for _, one := range areas {
kad.Add(new(big.Int).SetBytes(one.GetNetId()))
// netBigIds = append(netBigIds, new(big.Int).SetBytes(one.GetNetId()))
}
dstId := kad.Get(new(big.Int).SetBytes(find))
dstIdBs := dstId[0].Bytes()
dstIdBsP := utils.FullHighPositionZero(&dstIdBs, 32)
return nodeStore.AddressNet(*dstIdBsP)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/prestonTao/libp2parea.git
git@gitee.com:prestonTao/libp2parea.git
prestonTao
libp2parea
libp2parea
3aaa451ef873

搜索帮助