Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:add justseen node #44

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/discover/portalwire"
"github.com/ethereum/go-ethereum/p2p/enode"
Expand All @@ -18,6 +19,11 @@ import (
)

func main() {
glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, true))
slogVerbosity := log.FromLegacyLevel(5)
glogger.Verbosity(slogVerbosity)
log.SetDefault(log.NewLogger(glogger))

var privateKey *ecdsa.PrivateKey
var err error
privateKeyHex := os.Getenv("HIVE_CLIENT_PRIVATE_KEY")
Expand Down
12 changes: 9 additions & 3 deletions p2p/discover/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func (d *DiscV5API) AddEnr(enr string) (bool, error) {
return false, err
}

d.DiscV5.tab.addSeenNode(wrapNode(n))
wn := wrapNode(n)
wn.livenessChecks++
d.DiscV5.tab.addVerifiedNode(wn)
return true, nil
}

Expand Down Expand Up @@ -225,7 +227,9 @@ func (p *PortalAPI) HistoryAddEnr(enr string) (bool, error) {
return false, err
}

p.portalProtocol.table.addSeenNode(wrapNode(n))
wn := wrapNode(n)
wn.livenessChecks++
p.portalProtocol.table.addVerifiedNode(wn)
return true, nil
}

Expand All @@ -237,7 +241,9 @@ func (p *PortalAPI) AddEnrs(enrs []string) bool {
continue
}

p.portalProtocol.table.addSeenNode(wrapNode(n))
wn := wrapNode(n)
wn.livenessChecks++
p.portalProtocol.table.addVerifiedNode(wn)
}

return true
Expand Down
25 changes: 19 additions & 6 deletions p2p/discover/portal_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"math/big"
"math/rand"
"net"
"os"
"sort"
"time"

Expand Down Expand Up @@ -202,12 +201,12 @@ func NewPortalProtocol(config *PortalProtocolConfig, protocolId string, privateK
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
localNode.SetStaticIP(ipnet.IP)
break
}
}
}
closeCtx, cancelCloseCtx := context.WithCancel(context.Background())

log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelTrace, true)))
protocol := &PortalProtocol{
protocolId: protocolId,
ListenAddr: config.ListenAddr,
Expand Down Expand Up @@ -248,6 +247,9 @@ func (p *PortalProtocol) Start() error {
for i := 0; i < concurrentOffers; i++ {
go p.offerWorker()
}

// wait for the routing table to be initialized
<-p.table.initDone
return nil
}

Expand Down Expand Up @@ -516,6 +518,7 @@ func (p *PortalProtocol) processOffer(target *enode.Node, resp []byte, request *
}

p.log.Trace("Received accept response", "id", target.ID(), "accept", accept)
p.setJustSeen(target)

var contentKeyLen int
if request.Kind == TransientOfferRequestKind {
Expand Down Expand Up @@ -636,6 +639,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte,
}

p.log.Trace("Received content response", "id", target.ID(), "content", content)
p.setJustSeen(target)
return resp[1], content.Content, nil
case portalwire.ContentConnIdSelector:
connIdMsg := &portalwire.ConnectionId{}
Expand All @@ -645,6 +649,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte,
}

p.log.Trace("Received content response", "id", target.ID(), "connIdMsg", connIdMsg)
p.setJustSeen(target)
connctx, conncancel := context.WithTimeout(p.closeCtx, defaultUTPConnectTimeout)
laddr := p.utp.Addr().(*utp.Addr)
raddr := &utp.Addr{IP: target.IP(), Port: target.UDP()}
Expand Down Expand Up @@ -686,7 +691,7 @@ func (p *PortalProtocol) processContent(target *enode.Node, resp []byte) (byte,
}

p.log.Trace("Received content response", "id", target.ID(), "enrs", enrs)

p.setJustSeen(target)
nodes := p.filterNodes(target, enrs.Enrs, nil)
return resp[1], nodes, nil
default:
Expand All @@ -705,12 +710,18 @@ func (p *PortalProtocol) processNodes(target *enode.Node, resp []byte, distances
return nil, err
}

p.table.addVerifiedNode(wrapNode(target))
p.setJustSeen(target)
nodes := p.filterNodes(target, nodesResp.Enrs, distances)

return nodes, nil
}

func (p *PortalProtocol) setJustSeen(target *enode.Node) {
wn := wrapNode(target)
wn.livenessChecks++
p.table.addVerifiedNode(wn)
}

func (p *PortalProtocol) filterNodes(target *enode.Node, enrs [][]byte, distances []uint) []*enode.Node {
var (
nodes []*enode.Node
Expand Down Expand Up @@ -761,9 +772,9 @@ func (p *PortalProtocol) processPong(target *enode.Node, resp []byte) (*portalwi
}

p.log.Trace("Received pong response", "id", target.ID(), "pong", pong, "customPayload", customPayload)
p.setJustSeen(target)

p.radiusCache.Set([]byte(target.ID().String()), customPayload.Radius)
p.table.addVerifiedNode(wrapNode(target))
return pong, nil
}

Expand Down Expand Up @@ -1308,7 +1319,9 @@ func (p *PortalProtocol) lookupWorker(destNode *node, target enode.ID) ([]*node,
}
for _, n := range r {
if n.ID() != p.Self().ID() {
nodes.push(wrapNode(n), portalFindnodesResultLimit)
wn := wrapNode(n)
p.table.addSeenNode(wn)
nodes.push(wn, portalFindnodesResultLimit)
}
}
return nodes.entries, err
Expand Down