decred.org/dcrdex@v1.0.5/client/asset/btc/electrum/network_example_test.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  //go:build electrumlive
     5  
     6  package electrum
     7  
     8  import (
     9  	"context"
    10  	"crypto/tls"
    11  	"crypto/x509"
    12  	"encoding/hex"
    13  	"net"
    14  	"sort"
    15  	"strings"
    16  	"testing"
    17  	"time"
    18  
    19  	"github.com/davecgh/go-spew/spew"
    20  	"github.com/gcash/bchd/wire"
    21  )
    22  
    23  func TestServerConn(t *testing.T) {
    24  	ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
    25  	defer cancel()
    26  	addr := "electrum.ltc.xurious.com:51002"
    27  	host, _, err := net.SplitHostPort(addr)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	rootCAs, _ := x509.SystemCertPool()
    32  	tlsConfig := &tls.Config{
    33  		InsecureSkipVerify: true,
    34  		RootCAs:            rootCAs,
    35  		// MinVersion:         tls.VersionTLS12,
    36  		ServerName: host,
    37  	}
    38  	opts := &ConnectOpts{
    39  		TLSConfig:   tlsConfig,
    40  		DebugLogger: StdoutPrinter,
    41  	}
    42  	sc, err := ConnectServer(ctx, addr, opts)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	t.Log(sc.proto)
    47  
    48  	banner, err := sc.Banner(ctx)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	t.Log(banner)
    53  
    54  	feats, err := sc.Features(ctx)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	spew.Dump(feats)
    59  	if feats.Genesis != "4966625a4b2851d9fdee139e56211a0d88575f59ed816ff5e6a63deb4e3e29a0" { // Litecoin Testnet genesis block hash
    60  		t.Fatalf("wrong genesis hash: %v", feats.Genesis)
    61  	}
    62  
    63  	txres, err := sc.GetTransaction(ctx, "f4b5fca9e2fa3abfe22ea82eece8e28cdcf3e03bb11c63c327b5c719bfa2df6f")
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	spew.Dump(txres)
    68  
    69  	var startHeight, count uint32 = 2319765, 11
    70  	hdrsRes, err := sc.BlockHeaders(ctx, startHeight, count) // [2319765, 2319776)
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	hdrReader := hex.NewDecoder(strings.NewReader(hdrsRes.HexConcat))
    76  	var lastHdr *wire.BlockHeader
    77  	timestamps := make([]int64, 0, hdrsRes.Count)
    78  	for i := uint32(0); i < hdrsRes.Count; i++ {
    79  		hdr := &wire.BlockHeader{}
    80  		err = hdr.Deserialize(hdrReader)
    81  		if err != nil {
    82  			if i > 0 {
    83  				t.Fatalf("Failed to deserialize header for block %d: %v",
    84  					startHeight+i, err)
    85  				break // we have at least one time stamp, work with it
    86  			}
    87  			t.Fatal(err)
    88  		}
    89  		timestamps = append(timestamps, hdr.Timestamp.Unix())
    90  		if i == count-1 && count == hdrsRes.Count {
    91  			lastHdr = hdr
    92  		}
    93  	}
    94  	spew.Dump(lastHdr)
    95  
    96  	sort.Slice(timestamps, func(i, j int) bool {
    97  		return timestamps[i] < timestamps[j]
    98  	})
    99  
   100  	medianTimestamp := timestamps[len(timestamps)/2]
   101  	t.Logf("Median time for block %d: %v", startHeight+hdrsRes.Count-1, time.Unix(medianTimestamp, 0))
   102  
   103  	/*
   104  			hdrRes, err := sc.SubscribeHeaders(ctx)
   105  			spew.Dump(hdrRes)
   106  
   107  			height := uint32(60000)
   108  		out:
   109  			for {
   110  				select {
   111  				case <-ctx.Done():
   112  					break out
   113  				case <-time.After(5 * time.Second):
   114  					hdr, err := sc.BlockHeader(ctx, height)
   115  					if err != nil {
   116  						t.Log(err)
   117  						break out
   118  					}
   119  					t.Log(hdr)
   120  				}
   121  				height++
   122  			}
   123  	*/
   124  	sc.Shutdown()
   125  
   126  	<-sc.Done()
   127  }