github.com/ZuluSpl0it/Sia@v1.3.7/modules/transactionpool/persist_test.go (about)

     1  package transactionpool
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/persist"
    10  	"github.com/NebulousLabs/Sia/types"
    11  
    12  	"github.com/coreos/bbolt"
    13  )
    14  
    15  // TestRescan triggers a rescan in the transaction pool, verifying that the
    16  // rescan code does not cause deadlocks or crashes.
    17  func TestRescan(t *testing.T) {
    18  	if testing.Short() {
    19  		t.SkipNow()
    20  	}
    21  
    22  	tpt, err := createTpoolTester(t.Name())
    23  	if err != nil {
    24  		t.Fatal(err)
    25  	}
    26  	defer tpt.Close()
    27  
    28  	// Create a valid transaction set using the wallet.
    29  	txns, err := tpt.wallet.SendSiacoins(types.NewCurrency64(100), types.UnlockHash{})
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	if len(tpt.tpool.transactionSets) != 1 {
    34  		t.Error("sending coins did not increase the transaction sets by 1")
    35  	}
    36  	// Mine the transaction into a block, so that it's in the consensus set.
    37  	_, err = tpt.miner.AddBlock()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	// Close the tpool, delete the persistence, then restart the tpool. The
    43  	// tpool should still recognize the transaction set as a duplicate.
    44  	persistDir := tpt.tpool.persistDir
    45  	err = tpt.tpool.Close()
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	err = os.RemoveAll(persistDir)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	tpt.tpool, err = New(tpt.cs, tpt.gateway, persistDir)
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	err = tpt.tpool.AcceptTransactionSet(txns)
    58  	if err != modules.ErrDuplicateTransactionSet {
    59  		t.Fatal("expecting modules.ErrDuplicateTransactionSet, got:", err)
    60  	}
    61  
    62  	// Close the tpool, corrupt the database, then restart the tpool. The tpool
    63  	// should still recognize the transaction set as a duplicate.
    64  	err = tpt.tpool.Close()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	db, err := persist.OpenDatabase(dbMetadata, filepath.Join(persistDir, dbFilename))
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  	err = db.Update(func(tx *bolt.Tx) error {
    73  		ccBytes := tx.Bucket(bucketRecentConsensusChange).Get(fieldRecentConsensusChange)
    74  		// copy the bytes due to bolt's mmap.
    75  		newCCBytes := make([]byte, len(ccBytes))
    76  		copy(newCCBytes, ccBytes)
    77  		newCCBytes[0]++
    78  		return tx.Bucket(bucketRecentConsensusChange).Put(fieldRecentConsensusChange, newCCBytes)
    79  	})
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  	err = db.Close()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	tpt.tpool, err = New(tpt.cs, tpt.gateway, persistDir)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  	err = tpt.tpool.AcceptTransactionSet(txns)
    92  	if err != modules.ErrDuplicateTransactionSet {
    93  		t.Fatal("expecting modules.ErrDuplicateTransactionSet, got:", err)
    94  	}
    95  }