gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/host/update_test.go (about)

     1  package host
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"gitlab.com/NebulousLabs/fastrand"
     9  	"gitlab.com/SiaPrime/SiaPrime/crypto"
    10  	"gitlab.com/SiaPrime/SiaPrime/modules"
    11  	"gitlab.com/SiaPrime/SiaPrime/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  	defer ht.Close()
    25  
    26  	// create a file contract
    27  	fc := types.FileContract{
    28  		WindowStart:        types.MaturityDelay + 3,
    29  		WindowEnd:          1000,
    30  		Payout:             types.NewCurrency64(1),
    31  		UnlockHash:         types.UnlockConditions{}.UnlockHash(),
    32  		ValidProofOutputs:  []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}},
    33  		MissedProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}, {Value: types.NewCurrency64(0)}},
    34  	}
    35  	txnBuilder, err := ht.wallet.StartTransaction()
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	err = txnBuilder.FundSiacoins(fc.Payout)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	txnBuilder.AddFileContract(fc)
    44  	signedTxnSet, err := txnBuilder.Sign(true)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	fcid := signedTxnSet[len(signedTxnSet)-1].FileContractID(0)
    49  
    50  	// generate data
    51  	const dataSize = 777
    52  	data := fastrand.Bytes(dataSize)
    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  	defer ht.Close()
   117  
   118  	// Check that the host's persistent variables have incorporated the first
   119  	// few blocks.
   120  	if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 {
   121  		t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly")
   122  	}
   123  	oldChange := ht.host.recentChange
   124  	oldHeight := ht.host.blockHeight
   125  
   126  	// Corrupt the variables and perform a rescan to see if they reset
   127  	// correctly.
   128  	ht.host.recentChange[0]++
   129  	ht.host.blockHeight += 100e3
   130  	ht.cs.Unsubscribe(ht.host)
   131  	err = ht.host.initRescan()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	if oldChange != ht.host.recentChange || oldHeight != ht.host.blockHeight {
   136  		t.Error("consensus tracking variables were not reset correctly after rescan")
   137  	}
   138  }
   139  
   140  // TestIntegrationAutoRescan checks that a rescan is triggered during New if
   141  // the consensus set becomes desynchronized.
   142  func TestIntegrationAutoRescan(t *testing.T) {
   143  	if testing.Short() {
   144  		t.SkipNow()
   145  	}
   146  	t.Parallel()
   147  	ht, err := newHostTester("TestIntegrationAutoRescan")
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	defer ht.Close()
   152  
   153  	// Check that the host's persistent variables have incorporated the first
   154  	// few blocks.
   155  	if ht.host.recentChange == (modules.ConsensusChangeID{}) || ht.host.blockHeight == 0 {
   156  		t.Fatal("host variables do not indicate that the host is tracking the consensus set correctly")
   157  	}
   158  	oldChange := ht.host.recentChange
   159  	oldHeight := ht.host.blockHeight
   160  
   161  	// Corrupt the variables, then close the host.
   162  	ht.host.recentChange[0]++
   163  	ht.host.blockHeight += 100e3
   164  	err = ht.host.Close() // host saves upon closing
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  
   169  	// Create a new host and check that the persist variables have correctly
   170  	// reset.
   171  	h, err := New(ht.cs, ht.gateway, ht.tpool, ht.wallet, "localhost:0", filepath.Join(ht.persistDir, modules.HostDir))
   172  	if err != nil {
   173  		t.Fatal(err)
   174  	}
   175  	if oldChange != h.recentChange || oldHeight != h.blockHeight {
   176  		t.Error("consensus tracking variables were not reset correctly after rescan")
   177  	}
   178  
   179  	// Set ht.host to 'h' so that the 'ht.Close()' method will close everything
   180  	// cleanly.
   181  	ht.host = h
   182  }