github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/wallet/transactions_test.go (about)

     1  package wallet
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/NebulousLabs/Sia/types"
     7  )
     8  
     9  // TestIntegrationTransactions checks that the transaction history is being
    10  // correctly recorded and extended.
    11  func TestIntegrationTransactions(t *testing.T) {
    12  	if testing.Short() {
    13  		t.SkipNow()
    14  	}
    15  	wt, err := createWalletTester("TestIntegrationTransactions")
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	defer wt.closeWt()
    20  
    21  	// Creating the wallet tester results in blocks being mined until the miner
    22  	// has money, which means types.MaturityDelay+1 blocks are created, and
    23  	// each block is going to have a transaction (the miner payout) going to
    24  	// the wallet.
    25  	txns, err := wt.wallet.Transactions(0, 100)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if len(txns) != int(types.MaturityDelay+1) {
    30  		t.Error("unexpected transaction history length")
    31  	}
    32  	sendTxns, err := wt.wallet.SendSiacoins(types.NewCurrency64(5000), types.UnlockHash{})
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	// No more confirmed transactions have been added.
    37  	txns, err = wt.wallet.Transactions(0, 100)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if len(txns) != int(types.MaturityDelay+1) {
    42  		t.Error("unexpected transaction history length")
    43  	}
    44  	// Two transactions added to unconfirmed pool - 1 to fund the exact output,
    45  	// and 1 to hold the exact output.
    46  	if len(wt.wallet.UnconfirmedTransactions()) != 2 {
    47  		t.Error("was expecting 4 unconfirmed transactions")
    48  	}
    49  
    50  	b, _ := wt.miner.FindBlock()
    51  	err = wt.cs.AcceptBlock(b)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	// A confirmed transaction was added for the miner payout, and the 2
    56  	// transactions that were previously unconfirmed.
    57  	txns, err = wt.wallet.Transactions(0, 100)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	if len(txns) != int(types.MaturityDelay+2+2) {
    62  		t.Error("unexpected transaction history length")
    63  	}
    64  
    65  	// Try getting a partial history for just the previous block.
    66  	txns, err = wt.wallet.Transactions(types.MaturityDelay+3, types.MaturityDelay+3)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	// The partial should include one transaction for a block, and 2 for the
    71  	// send that occurred.
    72  	if len(txns) != 3 {
    73  		t.Error(len(txns))
    74  	}
    75  
    76  	// Check that transactions can be queried individually.
    77  	txn, exists := wt.wallet.Transaction(sendTxns[0].ID())
    78  	if !exists {
    79  		t.Fatal("unable to query transaction")
    80  	}
    81  	if txn.TransactionID != sendTxns[0].ID() {
    82  		t.Error("wrong transaction was fetched")
    83  	}
    84  	_, exists = wt.wallet.Transaction(types.TransactionID{})
    85  	if exists {
    86  		t.Error("able to query a nonexisting transction")
    87  	}
    88  }
    89  
    90  // TestIntegrationAddressTransactions checks grabbing the history for a single
    91  // address.
    92  func TestIntegrationAddressTransactions(t *testing.T) {
    93  	if testing.Short() {
    94  		t.SkipNow()
    95  	}
    96  	wt, err := createWalletTester("TestTransactionHistory")
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	defer wt.closeWt()
   101  
   102  	// Grab an address and send it money.
   103  	uc, err := wt.wallet.NextAddress()
   104  	addr := uc.UnlockHash()
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	_, err = wt.wallet.SendSiacoins(types.NewCurrency64(5005), addr)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	// Check the confirmed balance of the address.
   114  	addrHist := wt.wallet.AddressTransactions(addr)
   115  	if len(addrHist) != 0 {
   116  		t.Error("address should be empty - no confirmed transactions")
   117  	}
   118  	if len(wt.wallet.AddressUnconfirmedTransactions(addr)) == 0 {
   119  		t.Error("addresses unconfirmed transactions should not be empty")
   120  	}
   121  	b, _ := wt.miner.FindBlock()
   122  	err = wt.cs.AcceptBlock(b)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	addrHist = wt.wallet.AddressTransactions(addr)
   127  	if len(addrHist) == 0 {
   128  		t.Error("address history should have some transactions")
   129  	}
   130  	if len(wt.wallet.AddressUnconfirmedTransactions(addr)) != 0 {
   131  		t.Error("addresses unconfirmed transactions should be empty")
   132  	}
   133  }