Skip to content

Commit

Permalink
Update btcwire path import paths to new location.
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh committed Feb 5, 2015
1 parent 979d676 commit 03433da
Show file tree
Hide file tree
Showing 55 changed files with 1,005 additions and 1,005 deletions.
66 changes: 33 additions & 33 deletions addrmgr/addrmanager.go
Expand Up @@ -23,7 +23,7 @@ import (
"sync/atomic"
"time"

"github.com/btcsuite/btcwire"
"github.com/btcsuite/btcd/wire"
)

// AddrManager provides a concurrency safe address manager for caching potential
Expand Down Expand Up @@ -66,7 +66,7 @@ type serializedAddrManager struct {
}

type localAddress struct {
na *btcwire.NetAddress
na *wire.NetAddress
score AddressPriority
}

Expand Down Expand Up @@ -161,7 +161,7 @@ const (

// updateAddress is a helper function to either update an address already known
// to the address manager, or to add the address if not already known.
func (a *AddrManager) updateAddress(netAddr, srcAddr *btcwire.NetAddress) {
func (a *AddrManager) updateAddress(netAddr, srcAddr *wire.NetAddress) {
// Filter out non-routable addresses. Note that non-routable
// also includes invalid and local addresses.
if !IsRoutable(netAddr) {
Expand Down Expand Up @@ -292,15 +292,15 @@ func (a *AddrManager) pickTried(bucket int) *list.Element {
return oldestElem
}

func (a *AddrManager) getNewBucket(netAddr, srcAddr *btcwire.NetAddress) int {
func (a *AddrManager) getNewBucket(netAddr, srcAddr *wire.NetAddress) int {
// bitcoind:
// doublesha256(key + sourcegroup + int64(doublesha256(key + group + sourcegroup))%bucket_per_source_group) % num_new_buckets

data1 := []byte{}
data1 = append(data1, a.key[:]...)
data1 = append(data1, []byte(GroupKey(netAddr))...)
data1 = append(data1, []byte(GroupKey(srcAddr))...)
hash1 := btcwire.DoubleSha256(data1)
hash1 := wire.DoubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 %= newBucketsPerGroup
var hashbuf [8]byte
Expand All @@ -310,17 +310,17 @@ func (a *AddrManager) getNewBucket(netAddr, srcAddr *btcwire.NetAddress) int {
data2 = append(data2, GroupKey(srcAddr)...)
data2 = append(data2, hashbuf[:]...)

hash2 := btcwire.DoubleSha256(data2)
hash2 := wire.DoubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % newBucketCount)
}

func (a *AddrManager) getTriedBucket(netAddr *btcwire.NetAddress) int {
func (a *AddrManager) getTriedBucket(netAddr *wire.NetAddress) int {
// bitcoind hashes this as:
// doublesha256(key + group + truncate_to_64bits(doublesha256(key)) % buckets_per_group) % num_buckets
data1 := []byte{}
data1 = append(data1, a.key[:]...)
data1 = append(data1, []byte(NetAddressKey(netAddr))...)
hash1 := btcwire.DoubleSha256(data1)
hash1 := wire.DoubleSha256(data1)
hash64 := binary.LittleEndian.Uint64(hash1)
hash64 %= triedBucketsPerGroup
var hashbuf [8]byte
Expand All @@ -330,7 +330,7 @@ func (a *AddrManager) getTriedBucket(netAddr *btcwire.NetAddress) int {
data2 = append(data2, GroupKey(netAddr)...)
data2 = append(data2, hashbuf[:]...)

hash2 := btcwire.DoubleSha256(data2)
hash2 := wire.DoubleSha256(data2)
return int(binary.LittleEndian.Uint64(hash2) % triedBucketCount)
}

Expand Down Expand Up @@ -521,8 +521,8 @@ func (a *AddrManager) deserializePeers(filePath string) error {
return nil
}

// DeserializeNetAddress converts a given address string to a *btcwire.NetAddress
func (a *AddrManager) DeserializeNetAddress(addr string) (*btcwire.NetAddress, error) {
// DeserializeNetAddress converts a given address string to a *wire.NetAddress
func (a *AddrManager) DeserializeNetAddress(addr string) (*wire.NetAddress, error) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
Expand All @@ -532,7 +532,7 @@ func (a *AddrManager) DeserializeNetAddress(addr string) (*btcwire.NetAddress, e
return nil, err
}

return a.HostToNetAddress(host, uint16(port), btcwire.SFNodeNetwork)
return a.HostToNetAddress(host, uint16(port), wire.SFNodeNetwork)
}

// Start begins the core address handler which manages a pool of known
Expand Down Expand Up @@ -570,7 +570,7 @@ func (a *AddrManager) Stop() error {
// AddAddresses adds new addresses to the address manager. It enforces a max
// number of addresses and silently ignores duplicate addresses. It is
// safe for concurrent access.
func (a *AddrManager) AddAddresses(addrs []*btcwire.NetAddress, srcAddr *btcwire.NetAddress) {
func (a *AddrManager) AddAddresses(addrs []*wire.NetAddress, srcAddr *wire.NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()

Expand All @@ -582,23 +582,23 @@ func (a *AddrManager) AddAddresses(addrs []*btcwire.NetAddress, srcAddr *btcwire
// AddAddress adds a new address to the address manager. It enforces a max
// number of addresses and silently ignores duplicate addresses. It is
// safe for concurrent access.
func (a *AddrManager) AddAddress(addr, srcAddr *btcwire.NetAddress) {
func (a *AddrManager) AddAddress(addr, srcAddr *wire.NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()

a.updateAddress(addr, srcAddr)
}

// AddAddressByIP adds an address where we are given an ip:port and not a
// btcwire.NetAddress.
// wire.NetAddress.
func (a *AddrManager) AddAddressByIP(addrIP string) error {
// Split IP and port
addr, portStr, err := net.SplitHostPort(addrIP)
if err != nil {
return err
}
// Put it in btcwire.Netaddress
var na btcwire.NetAddress
// Put it in wire.Netaddress
var na wire.NetAddress
na.Timestamp = time.Now()
na.IP = net.ParseIP(addr)
if na.IP == nil {
Expand Down Expand Up @@ -637,14 +637,14 @@ func (a *AddrManager) NeedMoreAddresses() bool {

// AddressCache returns the current address cache. It must be treated as
// read-only (but since it is a copy now, this is not as dangerous).
func (a *AddrManager) AddressCache() []*btcwire.NetAddress {
func (a *AddrManager) AddressCache() []*wire.NetAddress {
a.mtx.Lock()
defer a.mtx.Unlock()
if a.nNew+a.nTried == 0 {
return nil
}

allAddr := make([]*btcwire.NetAddress, a.nNew+a.nTried)
allAddr := make([]*wire.NetAddress, a.nNew+a.nTried)
i := 0
// Iteration order is undefined here, but we randomise it anyway.
for _, v := range a.addrIndex {
Expand Down Expand Up @@ -688,7 +688,7 @@ func (a *AddrManager) reset() {
// HostToNetAddress returns a netaddress given a host address. If the address is
// a tor .onion address this will be taken care of. else if the host is not an
// IP address it will be resolved (via tor if required).
func (a *AddrManager) HostToNetAddress(host string, port uint16, services btcwire.ServiceFlag) (*btcwire.NetAddress, error) {
func (a *AddrManager) HostToNetAddress(host string, port uint16, services wire.ServiceFlag) (*wire.NetAddress, error) {
// tor address is 16 char base32 + ".onion"
var ip net.IP
if len(host) == 22 && host[16:] == ".onion" {
Expand All @@ -713,13 +713,13 @@ func (a *AddrManager) HostToNetAddress(host string, port uint16, services btcwir
ip = ips[0]
}

return btcwire.NewNetAddressIPPort(ip, port, services), nil
return wire.NewNetAddressIPPort(ip, port, services), nil
}

// ipString returns a string for the ip from the provided NetAddress. If the
// ip is in the range used for tor addresses then it will be transformed into
// the relevant .onion address.
func ipString(na *btcwire.NetAddress) string {
func ipString(na *wire.NetAddress) string {
if IsOnionCatTor(na) {
// We know now that na.IP is long enogh.
base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
Expand All @@ -731,7 +731,7 @@ func ipString(na *btcwire.NetAddress) string {

// NetAddressKey returns a string key in the form of ip:port for IPv4 addresses
// or [ip]:port for IPv6 addresses.
func NetAddressKey(na *btcwire.NetAddress) string {
func NetAddressKey(na *wire.NetAddress) string {
port := strconv.FormatUint(uint64(na.Port), 10)

return net.JoinHostPort(ipString(na), port)
Expand Down Expand Up @@ -820,13 +820,13 @@ func (a *AddrManager) GetAddress(class string, newBias int) *KnownAddress {
}
}

func (a *AddrManager) find(addr *btcwire.NetAddress) *KnownAddress {
func (a *AddrManager) find(addr *wire.NetAddress) *KnownAddress {
return a.addrIndex[NetAddressKey(addr)]
}

// Attempt increases the given address' attempt counter and updates
// the last attempt time.
func (a *AddrManager) Attempt(addr *btcwire.NetAddress) {
func (a *AddrManager) Attempt(addr *wire.NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()

Expand All @@ -844,7 +844,7 @@ func (a *AddrManager) Attempt(addr *btcwire.NetAddress) {
// Connected Marks the given address as currently connected and working at the
// current time. The address must already be known to AddrManager else it will
// be ignored.
func (a *AddrManager) Connected(addr *btcwire.NetAddress) {
func (a *AddrManager) Connected(addr *wire.NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()

Expand All @@ -867,7 +867,7 @@ func (a *AddrManager) Connected(addr *btcwire.NetAddress) {
// Good marks the given address as good. To be called after a successful
// connection and version exchange. If the address is unknown to the address
// manager it will be ignored.
func (a *AddrManager) Good(addr *btcwire.NetAddress) {
func (a *AddrManager) Good(addr *wire.NetAddress) {
a.mtx.Lock()
defer a.mtx.Unlock()

Expand Down Expand Up @@ -955,7 +955,7 @@ func (a *AddrManager) Good(addr *btcwire.NetAddress) {

// AddLocalAddress adds na to the list of known local addresses to advertise
// with the given priority.
func (a *AddrManager) AddLocalAddress(na *btcwire.NetAddress, priority AddressPriority) error {
func (a *AddrManager) AddLocalAddress(na *wire.NetAddress, priority AddressPriority) error {
if !IsRoutable(na) {
return fmt.Errorf("address %s is not routable", na.IP)
}
Expand All @@ -980,7 +980,7 @@ func (a *AddrManager) AddLocalAddress(na *btcwire.NetAddress, priority AddressPr

// getReachabilityFrom returns the relative reachability of the provided local
// address to the provided remote address.
func getReachabilityFrom(localAddr, remoteAddr *btcwire.NetAddress) int {
func getReachabilityFrom(localAddr, remoteAddr *wire.NetAddress) int {
const (
Unreachable = 0
Default = iota
Expand Down Expand Up @@ -1059,13 +1059,13 @@ func getReachabilityFrom(localAddr, remoteAddr *btcwire.NetAddress) int {

// GetBestLocalAddress returns the most appropriate local address to use
// for the given remote address.
func (a *AddrManager) GetBestLocalAddress(remoteAddr *btcwire.NetAddress) *btcwire.NetAddress {
func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.NetAddress {
a.lamtx.Lock()
defer a.lamtx.Unlock()

bestreach := 0
var bestscore AddressPriority
var bestAddress *btcwire.NetAddress
var bestAddress *wire.NetAddress
for _, la := range a.localAddresses {
reach := getReachabilityFrom(la.na, remoteAddr)
if reach > bestreach ||
Expand All @@ -1083,9 +1083,9 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *btcwire.NetAddress) *btcwi
remoteAddr.Port)

// Send something unroutable if nothing suitable.
bestAddress = &btcwire.NetAddress{
bestAddress = &wire.NetAddress{
Timestamp: time.Now(),
Services: btcwire.SFNodeNetwork,
Services: wire.SFNodeNetwork,
Port: 0,
}
if !IsIPv4(remoteAddr) && !IsOnionCatTor(remoteAddr) {
Expand Down

0 comments on commit 03433da

Please sign in to comment.