github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/peer/example_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package peer_test
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  	"time"
    12  
    13  	"github.com/BlockABC/godash/chaincfg"
    14  	"github.com/BlockABC/godash/peer"
    15  	"github.com/BlockABC/godash/wire"
    16  )
    17  
    18  // mockRemotePeer creates a basic inbound peer listening on the simnet port for
    19  // use with Example_peerConnection.  It does not return until the listner is
    20  // active.
    21  func mockRemotePeer() error {
    22  	// Configure peer to act as a simnet node that offers no services.
    23  	peerCfg := &peer.Config{
    24  		UserAgentName:    "peer",  // User agent name to advertise.
    25  		UserAgentVersion: "1.0.0", // User agent version to advertise.
    26  		ChainParams:      &chaincfg.SimNetParams,
    27  	}
    28  
    29  	// Accept connections on the simnet port.
    30  	listener, err := net.Listen("tcp", "127.0.0.1:18555")
    31  	if err != nil {
    32  		return err
    33  	}
    34  	go func() {
    35  		conn, err := listener.Accept()
    36  		if err != nil {
    37  			fmt.Printf("Accept: error %v\n", err)
    38  			return
    39  		}
    40  
    41  		// Create and start the inbound peer.
    42  		p := peer.NewInboundPeer(peerCfg)
    43  		p.Connect(conn)
    44  	}()
    45  
    46  	return nil
    47  }
    48  
    49  // This example demonstrates the basic process for initializing and creating an
    50  // outbound peer.  Peers negotiate by exchanging version and verack messages.
    51  // For demonstration, a simple handler for version message is attached to the
    52  // peer.
    53  func Example_newOutboundPeer() {
    54  	// Ordinarily this will not be needed since the outbound peer will be
    55  	// connecting to a remote peer, however, since this example is executed
    56  	// and tested, a mock remote peer is needed to listen for the outbound
    57  	// peer.
    58  	if err := mockRemotePeer(); err != nil {
    59  		fmt.Printf("mockRemotePeer: unexpected error %v\n", err)
    60  		return
    61  	}
    62  
    63  	// Create an outbound peer that is configured to act as a simnet node
    64  	// that offers no services and has listeners for the version and verack
    65  	// messages.  The verack listener is used here to signal the code below
    66  	// when the handshake has been finished by signalling a channel.
    67  	verack := make(chan struct{})
    68  	peerCfg := &peer.Config{
    69  		UserAgentName:    "peer",  // User agent name to advertise.
    70  		UserAgentVersion: "1.0.0", // User agent version to advertise.
    71  		ChainParams:      &chaincfg.SimNetParams,
    72  		Services:         0,
    73  		Listeners: peer.MessageListeners{
    74  			OnVersion: func(p *peer.Peer, msg *wire.MsgVersion) {
    75  				fmt.Println("outbound: received version")
    76  			},
    77  			OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
    78  				verack <- struct{}{}
    79  			},
    80  		},
    81  	}
    82  	p, err := peer.NewOutboundPeer(peerCfg, "127.0.0.1:18555")
    83  	if err != nil {
    84  		fmt.Printf("NewOutboundPeer: error %v\n", err)
    85  		return
    86  	}
    87  
    88  	// Establish the connection to the peer address and mark it connected.
    89  	conn, err := net.Dial("tcp", p.Addr())
    90  	if err != nil {
    91  		fmt.Printf("net.Dial: error %v\n", err)
    92  		return
    93  	}
    94  	p.Connect(conn)
    95  
    96  	// Wait for the verack message or timeout in case of failure.
    97  	select {
    98  	case <-verack:
    99  	case <-time.After(time.Second * 1):
   100  		fmt.Printf("Example_peerConnection: verack timeout")
   101  	}
   102  
   103  	// Disconnect the peer.
   104  	p.Disconnect()
   105  	p.WaitForDisconnect()
   106  
   107  	// Output:
   108  	// outbound: received version
   109  }