From 5a374b3c89b53f2ccaf8de2954287be034c40a29 Mon Sep 17 00:00:00 2001 From: Gustavo Valverde Date: Tue, 27 Feb 2024 11:35:11 +0000 Subject: [PATCH] WIP: fixes to avoid build errors --- dnsseed/setup.go | 2 +- dnsseed/setup_test.go | 36 ++++++++++++++++++------------------ zcash/address_book.go | 22 ++++++++++++++-------- zcash/client.go | 8 ++++---- zcash/client_callbacks.go | 30 ++++++++++++++---------------- zcash/client_test.go | 6 +++--- 6 files changed, 54 insertions(+), 50 deletions(-) diff --git a/dnsseed/setup.go b/dnsseed/setup.go index 8522ae8..261f5d9 100644 --- a/dnsseed/setup.go +++ b/dnsseed/setup.go @@ -8,7 +8,7 @@ import ( "strconv" "time" - "github.com/caddyserver/caddy" + "github.com/coredns/caddy" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" clog "github.com/coredns/coredns/plugin/pkg/log" diff --git a/dnsseed/setup_test.go b/dnsseed/setup_test.go index fd80e8b..6afd110 100644 --- a/dnsseed/setup_test.go +++ b/dnsseed/setup_test.go @@ -4,60 +4,60 @@ import ( "testing" "time" - "github.com/caddyserver/caddy" + "github.com/coredns/caddy" "github.com/zcashfoundation/dnsseeder/zcash/network" ) // TestSetup tests the various things that should be parsed by setup. func TestSetup(t *testing.T) { tt := []struct { - config string + config string validConfig bool - magic network.Network - interval time.Duration - bootstrap []string - ttl uint32 + magic network.Network + interval time.Duration + bootstrap []string + ttl uint32 }{ {`dnsseed`, false, 0, 0, []string{}, 0}, {`dnsseed mainnet`, false, 0, 0, []string{}, 0}, {`dnsseed { }`, false, 0, 0, []string{}, 0}, {`dnsseed { network }`, false, 0, 0, []string{}, 0}, {`dnsseed { network mainnet }`, true, network.Mainnet, defaultUpdateInterval, []string{}, defaultTTL}, - {`dnsseed { + {`dnsseed { network testnet crawl_interval 15s bootstrap_peers - }`, + }`, false, 0, 0, []string{}, 0, }, - {`dnsseed { + {`dnsseed { network testnet crawl_interval bootstrap_peers 127.0.0.1:8233 - }`, + }`, false, 0, 0, []string{}, 0, }, - {`dnsseed { + {`dnsseed { network testnet crawl_interval 15s bootstrap_peers 127.0.0.1:8233 - }`, + }`, true, network.Testnet, time.Duration(15) * time.Second, []string{"127.0.0.1:8233"}, defaultTTL, }, - {`dnsseed { + {`dnsseed { network testnet crawl_interval 15s bootstrap_peers 127.0.0.1:8233 boop snoot every 15s - }`, + }`, false, 0, 0, []string{}, 0, }, - {`dnsseed { + {`dnsseed { network mainnet crawl_interval 30m bootstrap_peers 127.0.0.1:8233 127.0.0.2:8233 record_ttl 300 - }`, + }`, true, network.Mainnet, time.Duration(30) * time.Minute, []string{"127.0.0.1:8233", "127.0.0.2:8233"}, 300, }, } @@ -74,7 +74,7 @@ func TestSetup(t *testing.T) { // bad parse, as expected continue } - + if opts.networkMagic != test.magic { t.Errorf("Input: %s wrong network magic", test.config) } @@ -93,4 +93,4 @@ func TestSetup(t *testing.T) { t.Errorf("Input: %s wrong TTL", test.config) } } -} \ No newline at end of file +} diff --git a/zcash/address_book.go b/zcash/address_book.go index 794c2b1..0d557bd 100644 --- a/zcash/address_book.go +++ b/zcash/address_book.go @@ -11,13 +11,13 @@ import ( ) type Address struct { - netaddr *wire.NetAddress + netaddr *wire.NetAddressV2 lastUpdate time.Time } func (a *Address) String() string { portString := strconv.Itoa(int(a.netaddr.Port)) - return net.JoinHostPort(a.netaddr.IP.String(), portString) + return net.JoinHostPort(a.netaddr.Addr.String(), portString) } func (a *Address) asPeerKey() PeerKey { @@ -35,7 +35,7 @@ func (a *Address) fromPeerKey(s PeerKey) (*Address, error) { return nil, err } - na := wire.NewNetAddressTimestamp( + na := wire.NetAddressV2FromBytes( time.Now(), 0, net.ParseIP(host), @@ -47,13 +47,13 @@ func (a *Address) fromPeerKey(s PeerKey) (*Address, error) { return a, nil } -func (a *Address) asNetAddress() *wire.NetAddress { +func (a *Address) asNetAddress() *wire.NetAddressV2 { newNA := *a.netaddr newNA.Timestamp = a.lastUpdate return &newNA } -func (a *Address) fromNetAddress(na *wire.NetAddress) (*Address, error) { +func (a *Address) fromNetAddress(na *wire.NetAddressV2) (*Address, error) { a.netaddr = na a.lastUpdate = na.Timestamp return a, nil @@ -222,6 +222,12 @@ func (bk *AddressBook) waitForAddresses(n int, done chan struct{}) { return } +// Validate is an address is an IPv4 address. +func (bk *AddressBook) Validate(s string) bool { + ip := net.ParseIP(s) + return ip.To4() != nil +} + // GetAddressList returns a slice of n valid addresses in random order. // If there aren't enough known addresses, it returns as many as we have. func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) []net.IP { @@ -236,12 +242,12 @@ func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) [] continue } - if v6 && v.netaddr.IP.To4() != nil { + if v6 && bk.Validate(v.netaddr.Addr.String()) { // skip IPv4 addresses if we're asked for v6 continue } - if !v6 && v.netaddr.IP.To4() == nil { + if !v6 && !bk.Validate(v.netaddr.Addr.String()) { // skip IPv6 addresses if we're asked for v4 continue } @@ -253,7 +259,7 @@ func (bk *AddressBook) shuffleAddressList(n int, v6 bool, defaultPort string) [] continue } - resp = append(resp, v.netaddr.IP) + resp = append(resp, net.IP(v.netaddr.Addr.String())) } mrand.Seed(time.Now().UnixNano()) diff --git a/zcash/client.go b/zcash/client.go index 2e8fc4c..d8147c3 100644 --- a/zcash/client.go +++ b/zcash/client.go @@ -446,7 +446,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) { next := <-refreshQueue na := next.netaddr - ipString := na.IP.String() + ipString := na.Addr.String() portString := strconv.Itoa(int(na.Port)) // Don't care about the peer individually, just that we can connect. @@ -454,7 +454,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) { if err != nil { if err != ErrRepeatConnection { - s.logger.Printf("Peer %s:%d unusable on refresh. Error: %s", na.IP, na.Port, err) + s.logger.Printf("Peer %s:%d unusable on refresh. Error: %s", na.Addr, na.Port, err) // Blacklist the peer. We might try to connect again later. // This would deadlock if enqueueAddrs still holds the RLock, // hence the awkward channel allocation above. @@ -467,7 +467,7 @@ func (s *Seeder) RefreshAddresses(disconnect bool) { s.DisconnectPeer(next.asPeerKey()) } - s.logger.Printf("Validated %s", na.IP) + s.logger.Printf("Validated %s", na.Addr) } wg.Done() }() @@ -503,7 +503,7 @@ func (s *Seeder) RetryBlacklist() { next := <-blacklistQueue na := next.netaddr - ip := na.IP.String() + ip := na.Addr.String() port := strconv.Itoa(int(na.Port)) // Call internal connect directly to avoid being blocked diff --git a/zcash/client_callbacks.go b/zcash/client_callbacks.go index e650c44..1535afe 100644 --- a/zcash/client_callbacks.go +++ b/zcash/client_callbacks.go @@ -54,8 +54,8 @@ func (s *Seeder) onAddr(p *peer.Peer, msg *wire.MsgAddr) { // By checking if we know them before adding to the queue, we create // the end condition for the crawler thread: it will time out after // not processing any new addresses. - if s.addrBook.IsKnown(peerKeyFromNA(na)) { - s.logger.Printf("Already knew about %s:%d", na.IP, na.Port) + if s.addrBook.IsKnown(peerKeyFromNAV2(na)) { + s.logger.Printf("Already knew about %s:%d", na.Addr.String(), na.Port) continue } _, denied := DeniedPorts[na.Port] @@ -71,26 +71,24 @@ func (s *Seeder) onAddr(p *peer.Peer, msg *wire.MsgAddr) { // If other address type is received, the address is ignored. func (s *Seeder) onAddrV2(p *peer.Peer, msg *wire.MsgAddrV2) { if len(msg.AddrList) == 0 { - s.logger.Printf("Got empty addrv2 message from peer %s. Disconnecting.", p.Addr()) + s.logger.Printf("Got empty addr message from peer %s. Disconnecting.", p.Addr()) s.DisconnectPeer(peerKeyFromPeer(p)) return } - s.logger.Printf("Got %d addrv2s from peer %s", len(msg.AddrList), p.Addr()) + s.logger.Printf("Got %d addrs from peer %s", len(msg.AddrList), p.Addr()) for _, na := range msg.AddrList { - if na.NetworkID == wire.NIIPV4 || na.NetworkID == wire.NIIPV6 { - // By checking if we know them before adding to the queue, we create - // the end condition for the crawler thread: it will time out after - // not processing any new addresses. - if s.addrBook.IsKnown(peerKeyFromNAV2(na)) { - s.logger.Printf("Already knew about %s:%d", na.IP, na.Port) - continue - } - _, denied := DeniedPorts[na.Port] - if !denied { - s.addrQueue <- &na.NetAddress - } + // By checking if we know them before adding to the queue, we create + // the end condition for the crawler thread: it will time out after + // not processing any new addresses. + if s.addrBook.IsKnown(peerKeyFromNAV2(na)) { + s.logger.Printf("Already knew about %s:%d", na.Addr.String(), na.Port) + continue + } + _, denied := DeniedPorts[na.Port] + if !denied { + s.addrQueue <- na } } } diff --git a/zcash/client_test.go b/zcash/client_test.go index 7984375..74a4fbf 100644 --- a/zcash/client_test.go +++ b/zcash/client_test.go @@ -70,9 +70,9 @@ func startMockLoop() error { if atomic.LoadUint32(&useAddrV2) != 0 { cache := make([]*wire.NetAddressV2, 0, 1) - addrv2_1 := wire.NewNetAddressV2NetAddress(addr) - addrv2_2 := wire.NewNetAddressV2NetAddress(addr2) - addrv2_3 := wire.NewNetAddressV2NetAddress(addr3) + addrv2_1 := wire.NetAddressV2FromBytes(addr.Timestamp, addr.Services, addr.IP.To16(), addr.Port) + addrv2_2 := wire.NetAddressV2FromBytes(addr2.Timestamp, addr2.Services, addr2.IP.To16(), addr2.Port) + addrv2_3 := wire.NetAddressV2FromBytes(addr3.Timestamp, addr3.Services, addr3.IP.To16(), addr3.Port) cache = append(cache, addrv2_1, addrv2_2, addrv2_3) _, err := p.PushAddrV2Msg(cache) if err != nil {