github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/transactionpool/transactionpool_test.go (about)

     1  package transactionpool
     2  
     3  import (
     4  	"crypto/rand"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/build"
     9  	"github.com/NebulousLabs/Sia/crypto"
    10  	"github.com/NebulousLabs/Sia/modules"
    11  	"github.com/NebulousLabs/Sia/modules/consensus"
    12  	"github.com/NebulousLabs/Sia/modules/gateway"
    13  	"github.com/NebulousLabs/Sia/modules/miner"
    14  	"github.com/NebulousLabs/Sia/modules/wallet"
    15  	"github.com/NebulousLabs/Sia/types"
    16  )
    17  
    18  // A tpoolTester is used during testing to initialize a transaction pool and
    19  // useful helper modules.
    20  type tpoolTester struct {
    21  	cs        modules.ConsensusSet
    22  	gateway   modules.Gateway
    23  	tpool     *TransactionPool
    24  	miner     modules.TestMiner
    25  	wallet    modules.Wallet
    26  	walletKey crypto.TwofishKey
    27  
    28  	persistDir string
    29  }
    30  
    31  // createTpoolTester returns a ready-to-use tpool tester, with all modules
    32  // initialized.
    33  func createTpoolTester(name string) (*tpoolTester, error) {
    34  	// Initialize the modules.
    35  	testdir := build.TempDir(modules.TransactionPoolDir, name)
    36  	g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  	cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	tp, err := New(cs, g, filepath.Join(testdir, modules.TransactionPoolDir))
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  	w, err := wallet.New(cs, tp, filepath.Join(testdir, modules.WalletDir))
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	var key crypto.TwofishKey
    53  	_, err = rand.Read(key[:])
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	_, err = w.Encrypt(key)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	err = w.Unlock(key)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	m, err := miner.New(cs, tp, w, filepath.Join(testdir, modules.MinerDir))
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	// Assebmle all of the objects in to a tpoolTester
    71  	tpt := &tpoolTester{
    72  		cs:        cs,
    73  		gateway:   g,
    74  		tpool:     tp,
    75  		miner:     m,
    76  		wallet:    w,
    77  		walletKey: key,
    78  
    79  		persistDir: testdir,
    80  	}
    81  
    82  	// Mine blocks until there is money in the wallet.
    83  	for i := types.BlockHeight(0); i <= types.MaturityDelay; i++ {
    84  		b, _ := tpt.miner.FindBlock()
    85  		err = tpt.cs.AcceptBlock(b)
    86  		if err != nil {
    87  			return nil, err
    88  		}
    89  	}
    90  
    91  	return tpt, nil
    92  }
    93  
    94  // TestIntegrationNewNilInputs tries to trigger a panic with nil inputs.
    95  func TestIntegrationNewNilInputs(t *testing.T) {
    96  	// Create a gateway and consensus set.
    97  	testdir := build.TempDir(modules.TransactionPoolDir, "TestNewNilInputs")
    98  	g, err := gateway.New("localhost:0", filepath.Join(testdir, modules.GatewayDir))
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	cs, err := consensus.New(g, filepath.Join(testdir, modules.ConsensusDir))
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	tpDir := filepath.Join(testdir, modules.TransactionPoolDir)
   107  
   108  	// Try all combinations of nil inputs.
   109  	_, err = New(nil, nil, tpDir)
   110  	if err == nil {
   111  		t.Error(err)
   112  	}
   113  	_, err = New(nil, g, tpDir)
   114  	if err != errNilCS {
   115  		t.Error(err)
   116  	}
   117  	_, err = New(cs, nil, tpDir)
   118  	if err != errNilGateway {
   119  		t.Error(err)
   120  	}
   121  	_, err = New(cs, g, tpDir)
   122  	if err != nil {
   123  		t.Error(err)
   124  	}
   125  }