Merge pull request #2 from fiksn/master
Make the code work with latest btcsuite repo changes
This commit is contained in:
commit
601a21dec7
12
client.go
12
client.go
|
@ -1,19 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/btcsuite/btcwire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
btcnet btcwire.BitcoinNet // Bitcoin Network
|
btcnet wire.BitcoinNet // Bitcoin Network
|
||||||
pver uint32 // Protocl Version
|
pver uint32 // Protocl Version
|
||||||
userAgent string // User Agent
|
userAgent string // User Agent
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(userAgent string) *Client {
|
func NewClient(userAgent string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
btcnet: btcwire.MainNet,
|
btcnet: wire.MainNet,
|
||||||
pver: btcwire.ProtocolVersion,
|
pver: wire.ProtocolVersion,
|
||||||
userAgent: userAgent,
|
userAgent: userAgent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcwire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: Break Client/Peer/Crawler into separate modules.
|
// TODO: Break Client/Peer/Crawler into separate modules.
|
||||||
|
@ -24,7 +24,7 @@ type Crawler struct {
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Node *Peer
|
Node *Peer
|
||||||
Peers []*btcwire.NetAddress
|
Peers []*wire.NetAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCrawler(client *Client, seeds []string) *Crawler {
|
func NewCrawler(client *Client, seeds []string) *Crawler {
|
||||||
|
@ -80,7 +80,7 @@ func (c *Crawler) handleAddress(address string) *Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send getaddr.
|
// Send getaddr.
|
||||||
err = peer.WriteMessage(btcwire.NewMsgGetAddr())
|
err = peer.WriteMessage(wire.NewMsgGetAddr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warningf("[%s] GetAddr failed: %v", address, err)
|
logger.Warningf("[%s] GetAddr failed: %v", address, err)
|
||||||
return &r
|
return &r
|
||||||
|
@ -110,7 +110,7 @@ func (c *Crawler) handleAddress(address string) *Result {
|
||||||
}
|
}
|
||||||
|
|
||||||
switch tmsg := msg.(type) {
|
switch tmsg := msg.(type) {
|
||||||
case *btcwire.MsgAddr:
|
case *wire.MsgAddr:
|
||||||
r.Peers = append(r.Peers, tmsg.AddrList...)
|
r.Peers = append(r.Peers, tmsg.AddrList...)
|
||||||
|
|
||||||
if firstReceived == -1 {
|
if firstReceived == -1 {
|
||||||
|
|
|
@ -5,7 +5,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"github.com/btcsuite/btcwire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -14,7 +14,7 @@ import (
|
||||||
var onioncatrange = net.IPNet{IP: net.ParseIP("FD87:d87e:eb43::"),
|
var onioncatrange = net.IPNet{IP: net.ParseIP("FD87:d87e:eb43::"),
|
||||||
Mask: net.CIDRMask(48, 128)}
|
Mask: net.CIDRMask(48, 128)}
|
||||||
|
|
||||||
func Tor(na *btcwire.NetAddress) bool {
|
func Tor(na *wire.NetAddress) bool {
|
||||||
// bitcoind encodes a .onion address as a 16 byte number by decoding the
|
// bitcoind encodes a .onion address as a 16 byte number by decoding the
|
||||||
// address prior to the .onion (i.e. the key hash) base32 into a ten
|
// address prior to the .onion (i.e. the key hash) base32 into a ten
|
||||||
// byte number. it then stores the first 6 bytes of the address as
|
// byte number. it then stores the first 6 bytes of the address as
|
||||||
|
@ -29,7 +29,7 @@ func Tor(na *btcwire.NetAddress) bool {
|
||||||
// ipString returns a string for the ip from the provided NetAddress. If the
|
// 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
|
// ip is in the range used for tor addresses then it will be transformed into
|
||||||
// the relavent .onion address.
|
// the relavent .onion address.
|
||||||
func ipString(na *btcwire.NetAddress) string {
|
func ipString(na *wire.NetAddress) string {
|
||||||
if Tor(na) {
|
if Tor(na) {
|
||||||
// We know now that na.IP is long enogh.
|
// We know now that na.IP is long enogh.
|
||||||
base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
|
base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
|
||||||
|
@ -41,7 +41,7 @@ func ipString(na *btcwire.NetAddress) string {
|
||||||
|
|
||||||
// NetAddressKey returns a string key in the form of ip:port for IPv4 addresses
|
// NetAddressKey returns a string key in the form of ip:port for IPv4 addresses
|
||||||
// or [ip]:port for IPv6 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)
|
port := strconv.FormatUint(uint64(na.Port), 10)
|
||||||
addr := net.JoinHostPort(ipString(na), port)
|
addr := net.JoinHostPort(ipString(na), port)
|
||||||
return addr
|
return addr
|
||||||
|
|
18
peer.go
18
peer.go
|
@ -5,7 +5,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcwire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
|
@ -54,13 +54,13 @@ func (p *Peer) Handshake() error {
|
||||||
|
|
||||||
logger.Debugf("[%s] Starting handshake.", p.Address)
|
logger.Debugf("[%s] Starting handshake.", p.Address)
|
||||||
|
|
||||||
nonce, err := btcwire.RandomUint64()
|
nonce, err := wire.RandomUint64()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
p.nonce = nonce
|
p.nonce = nonce
|
||||||
|
|
||||||
msgVersion, err := btcwire.NewMsgVersionFromConn(p.conn, p.nonce, 0)
|
msgVersion, err := wire.NewMsgVersionFromConn(p.conn, p.nonce, 0)
|
||||||
msgVersion.UserAgent = p.client.userAgent
|
msgVersion.UserAgent = p.client.userAgent
|
||||||
msgVersion.DisableRelayTx = true
|
msgVersion.DisableRelayTx = true
|
||||||
if err := p.WriteMessage(msgVersion); err != nil {
|
if err := p.WriteMessage(msgVersion); err != nil {
|
||||||
|
@ -72,7 +72,7 @@ func (p *Peer) Handshake() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
vmsg, ok := msg.(*btcwire.MsgVersion)
|
vmsg, ok := msg.(*wire.MsgVersion)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Did not receive version message: %T", vmsg)
|
return fmt.Errorf("Did not receive version message: %T", vmsg)
|
||||||
}
|
}
|
||||||
|
@ -90,17 +90,17 @@ func (p *Peer) Handshake() error {
|
||||||
// accept external connections so we skip it.
|
// accept external connections so we skip it.
|
||||||
|
|
||||||
// Send verack.
|
// Send verack.
|
||||||
if err := p.WriteMessage(btcwire.NewMsgVerAck()); err != nil {
|
if err := p.WriteMessage(wire.NewMsgVerAck()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) WriteMessage(msg btcwire.Message) error {
|
func (p *Peer) WriteMessage(msg wire.Message) error {
|
||||||
return btcwire.WriteMessage(p.conn, msg, p.pver, p.client.btcnet)
|
return wire.WriteMessage(p.conn, msg, p.pver, p.client.btcnet)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Peer) ReadMessage() (btcwire.Message, []byte, error) {
|
func (p *Peer) ReadMessage() (wire.Message, []byte, error) {
|
||||||
return btcwire.ReadMessage(p.conn, p.pver, p.client.btcnet)
|
return wire.ReadMessage(p.conn, p.pver, p.client.btcnet)
|
||||||
}
|
}
|
||||||
|
|
4
seed.go
4
seed.go
|
@ -4,7 +4,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/btcsuite/btcnet"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetSeedsFromDNS(dnsSeeds []string) []string {
|
func GetSeedsFromDNS(dnsSeeds []string) []string {
|
||||||
|
@ -33,7 +33,7 @@ func GetSeedsFromDNS(dnsSeeds []string) []string {
|
||||||
seeds := []string{}
|
seeds := []string{}
|
||||||
for ips := range results {
|
for ips := range results {
|
||||||
for _, ip := range ips {
|
for _, ip := range ips {
|
||||||
seeds = append(seeds, net.JoinHostPort(ip.String(), btcnet.MainNetParams.DefaultPort))
|
seeds = append(seeds, net.JoinHostPort(ip.String(), chaincfg.MainNetParams.DefaultPort))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue