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

     1  package host
     2  
     3  import (
     4  	"crypto/rand"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/NebulousLabs/Sia/crypto"
    10  	"github.com/NebulousLabs/Sia/modules"
    11  	"github.com/NebulousLabs/Sia/types"
    12  )
    13  
    14  // TestStorageProof checks that the host can create and submit a storage proof.
    15  func TestStorageProof(t *testing.T) {
    16  	if testing.Short() {
    17  		t.SkipNow()
    18  	}
    19  	t.Parallel()
    20  	ht, err := newHostTester("TestStorageProof")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	// create a file contract
    26  	fc := types.FileContract{
    27  		WindowStart:        types.MaturityDelay + 3,
    28  		WindowEnd:          1000,
    29  		Payout:             types.NewCurrency64(1),
    30  		UnlockHash:         types.UnlockConditions{}.UnlockHash(),
    31  		ValidProofOutputs:  []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}},
    32  		MissedProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}},
    33  	}
    34  	txnBuilder := ht.wallet.StartTransaction()
    35  	err = txnBuilder.FundSiacoins(fc.Payout)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	txnBuilder.AddFileContract(fc)
    40  	signedTxnSet, err := txnBuilder.Sign(true)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	fcid := signedTxnSet[len(signedTxnSet)-1].FileContractID(0)
    45  
    46  	// generate data
    47  	const dataSize = 777
    48  	data := make([]byte, dataSize)
    49  	_, err = rand.Read(data)
    50  	if err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	root := crypto.MerkleRoot(data)
    54  	err = ioutil.WriteFile(filepath.Join(ht.host.persistDir, "foo"), data, 0777)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	// create revision
    60  	rev := types.FileContractRevision{
    61  		ParentID:              fcid,
    62  		UnlockConditions:      types.UnlockConditions{},
    63  		NewFileSize:           dataSize,
    64  		NewWindowStart:        fc.WindowStart,
    65  		NewFileMerkleRoot:     root,
    66  		NewWindowEnd:          fc.WindowEnd,
    67  		NewValidProofOutputs:  fc.ValidProofOutputs,
    68  		NewMissedProofOutputs: fc.MissedProofOutputs,
    69  		NewRevisionNumber:     1,
    70  	}
    71  	_ = types.Transaction{
    72  		FileContractRevisions: []types.FileContractRevision{rev},
    73  	}
    74  
    75  	/*
    76  		// create obligation
    77  		obligation := &contractObligation{
    78  			ID: fcid,
    79  			OriginTransaction: types.Transaction{
    80  				FileContracts: []types.FileContract{fc},
    81  			},
    82  			Path: filepath.Join(ht.host.persistDir, "foo"),
    83  		}
    84  		ht.host.obligationsByID[fcid] = obligation
    85  		ht.host.addActionItem(fc.WindowStart+1, obligation)
    86  
    87  		// submit both to tpool
    88  		err = ht.tpool.AcceptTransactionSet(append(signedTxnSet, revTxn))
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  		_, err = ht.miner.AddBlock()
    93  		if err != nil {
    94  			t.Fatal(err)
    95  		}
    96  
    97  		// storage proof will be submitted after mining one more block
    98  		_, err = ht.miner.AddBlock()
    99  		if err != nil {
   100  			t.Fatal(err)
   101  		}
   102  	*/
   103  }
   104  
   105  // TestInitRescan probes the initRescan function, verifying that it works in
   106  // the naive case. The rescan is triggered manually.
   107  func TestInitRescan(t *testing.T) {
   108  	if testing.Short() {
   109  		t.SkipNow()
   110  	}
   111  	t.Parallel()
   112  	ht, err := newHostTester("TestInitRescan")
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  
   117  	// Check that the host's persistent variables have incorporated the first
   118  	// few blocks.
   119  	if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 {
   120  		t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly")
   121  	}
   122  	oldChange := ht.host.recentChange
   123  	oldHeight := ht.host.blockHeight
   124  
   125  	// Corrupt the variables and perform a rescan to see if they reset
   126  	// correctly.
   127  	ht.host.recentChange[0]++
   128  	ht.host.blockHeight += 100e3
   129  	ht.cs.Unsubscribe(ht.host)
   130  	err = ht.host.initRescan()
   131  	if err != nil {
   132  		t.Fatal(err)
   133  	}
   134  	if oldChange != ht.host.recentChange || oldHeight != ht.host.blockHeight {
   135  		t.Error("consensus tracking variables were not reset correctly after rescan")
   136  	}
   137  }
   138  
   139  // TestIntegrationAutoRescan checks that a rescan is triggered during New if
   140  // the consensus set becomes desynchronized.
   141  func TestIntegrationAutoRescan(t *testing.T) {
   142  	if testing.Short() {
   143  		t.SkipNow()
   144  	}
   145  	t.Parallel()
   146  	ht, err := newHostTester("TestIntegrationAutoRescan")
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	// Check that the host's persistent variables have incorporated the first
   152  	// few blocks.
   153  	if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 {
   154  		t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly")
   155  	}
   156  	oldChange := ht.host.recentChange
   157  	oldHeight := ht.host.blockHeight
   158  
   159  	// Corrupt the variables, then close the host.
   160  	ht.host.recentChange[0]++
   161  	ht.host.blockHeight += 100e3
   162  	err = ht.host.Close() // host saves upon closing
   163  	if err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	// Create a new host and check that the persist variables have correctly
   168  	// reset.
   169  	h, err := New(ht.cs, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
   170  	if err != nil {
   171  		t.Fatal(err)
   172  	}
   173  	if oldChange != h.recentChange || oldHeight != h.blockHeight {
   174  		t.Error("consensus tracking variables were not reset correctly after rescan")
   175  	}
   176  }