decred.org/dcrdex@v1.0.5/server/asset/bch/live_test.go (about)

     1  //go:build bchlive
     2  
     3  // go test -v -tags bchlive -run UTXOStats
     4  // -----------------------------------
     5  // Grab the most recent block and iterate it's outputs, taking account of
     6  // how many UTXOs are found, how many are of an unknown type, etc.
     7  //
     8  // go test -v -tags bchlive -run P2SHStats
     9  // -----------------------------------------
    10  // For each output in the last block, check it's previous outpoint to see if
    11  // it's a P2SH or P2WSH. If so, takes statistics on the script types, including
    12  // for the redeem script.
    13  //
    14  // go test -v -tags bchlive -run LiveFees
    15  // ------------------------------------------
    16  // Test that fees rates are parsed without error and that a few historical fee
    17  // rates are correct
    18  //
    19  // go test -v -tags bchlive -run TestMedianFeeRates
    20  // ------------------------------------------
    21  // Test that a median fee rate can be calculated.
    22  
    23  package bch
    24  
    25  import (
    26  	"context"
    27  	"fmt"
    28  	"os"
    29  	"path/filepath"
    30  	"testing"
    31  
    32  	"decred.org/dcrdex/dex"
    33  	"decred.org/dcrdex/server/asset"
    34  	"decred.org/dcrdex/server/asset/btc"
    35  	"github.com/btcsuite/btcd/btcutil"
    36  )
    37  
    38  var (
    39  	bch *BCHBackend
    40  	ctx context.Context
    41  )
    42  
    43  func TestMain(m *testing.M) {
    44  	// Wrap everything for defers.
    45  	doIt := func() int {
    46  		logger := dex.StdOutLogger("BCHTEST", dex.LevelTrace)
    47  
    48  		// Since Bitcoin Cash's data dir is the same as Bitcoin, for dev, we'll
    49  		// just need to all agree on using ~/.bch instead of ~/.bitcoin.
    50  		homeDir := btcutil.AppDataDir("bch", false)
    51  		configPath := filepath.Join(homeDir, "bitcoin.conf")
    52  
    53  		maxFeeBlocks = 1000
    54  
    55  		dexAsset, err := NewBackend(&asset.BackendConfig{
    56  			AssetID:    BipID,
    57  			ConfigPath: configPath,
    58  			Logger:     logger,
    59  			Net:        dex.Mainnet,
    60  		})
    61  		if err != nil {
    62  			fmt.Printf("NewBackend error: %v\n", err)
    63  			return 1
    64  		}
    65  
    66  		var ok bool
    67  		bch, ok = dexAsset.(*BCHBackend)
    68  		if !ok {
    69  			fmt.Printf("Could not cast asset.Backend to *BCHBackend")
    70  			return 1
    71  		}
    72  
    73  		var cancel context.CancelFunc
    74  		ctx, cancel = context.WithCancel(context.Background())
    75  
    76  		wg, err := dexAsset.Connect(ctx)
    77  		if err != nil {
    78  			cancel()
    79  			fmt.Printf("Connect failed: %v", err)
    80  			return 1
    81  		}
    82  		defer func() {
    83  			cancel()
    84  			wg.Wait()
    85  		}()
    86  
    87  		return m.Run()
    88  	}
    89  
    90  	os.Exit(doIt())
    91  }
    92  
    93  func TestUTXOStats(t *testing.T) {
    94  	btc.LiveUTXOStats(bch.Backend, t)
    95  }
    96  
    97  func TestP2SHStats(t *testing.T) {
    98  	btc.LiveP2SHStats(bch.Backend, t, 100)
    99  }
   100  
   101  func TestLiveFees(t *testing.T) {
   102  	btc.LiveFeeRates(bch.Backend, t, map[string]uint64{
   103  		"bcf7ae875b585e00a61055372c1e99046b20f5fbfcd8659959afb6f428326bfa": 1,
   104  		"056762c6df7ae2d80d6ebfcf7d9e3764dc4ca915fc983798ab4999fb3a30538f": 5,
   105  		"dc3962fc4d2d7d99646cacc16a23cc49143ea9cfc43128ec986b61e9132b2726": 444,
   106  		"0de586d0c74780605c36c0f51dcd850d1772f41a92c549e3aa36f9e78e905284": 2604,
   107  	})
   108  }
   109  
   110  func TestMedianFeeRates(t *testing.T) {
   111  	btc.TestMedianFeesTheHardWay(bch.Backend, t)
   112  }