gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/contractor/contracts_test.go (about)

     1  package contractor
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/SkynetLabs/skyd/skymodules"
     7  	"go.sia.tech/siad/crypto"
     8  	"go.sia.tech/siad/types"
     9  )
    10  
    11  // TestPubkeysToContractIDMap tests updating the contractor's
    12  // pubKeysToContractID map
    13  func TestPubkeysToContractIDMap(t *testing.T) {
    14  	if testing.Short() {
    15  		t.SkipNow()
    16  	}
    17  	t.Parallel()
    18  	h, c, _, cf, err := newTestingTrio(t.Name())
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer tryClose(cf, t)
    23  
    24  	// The contractor shouldn't have any contracts formed so the pubkey map
    25  	// should be empty
    26  	c.mu.Lock()
    27  	pubKeyMapLen := len(c.pubKeysToContractID)
    28  	c.mu.Unlock()
    29  	if pubKeyMapLen != 0 {
    30  		t.Fatal("pubkey map is not empty")
    31  	}
    32  
    33  	// acquire the contract maintenance lock for the duration of the test. This
    34  	// prevents theadedContractMaintenance from running.
    35  	c.maintenanceLock.Lock()
    36  	defer c.maintenanceLock.Unlock()
    37  
    38  	// get the host's entry from the db
    39  	pk := h.PublicKey()
    40  	hostEntry, ok, err := c.staticHDB.Host(pk)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	if !ok {
    45  		t.Fatal("no entry for host in db")
    46  	}
    47  
    48  	// set an allowance but don't use SetAllowance to avoid automatic contract
    49  	// formation.
    50  	c.mu.Lock()
    51  	c.allowance = skymodules.DefaultAllowance
    52  	a := skymodules.DefaultAllowance
    53  	c.mu.Unlock()
    54  
    55  	// form a contract with the host
    56  	_, contract, err := c.managedNewContract(hostEntry, a, types.SiacoinPrecision.Mul64(50), c.blockHeight+100)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	// Call managedUpdatePubKeyToContractIDMap
    62  	c.managedUpdatePubKeyToContractIDMap()
    63  
    64  	// Check pubkey map
    65  	c.mu.Lock()
    66  	pubKeyMapLen = len(c.pubKeysToContractID)
    67  	fcid, ok := c.pubKeysToContractID[pk.String()]
    68  	c.mu.Unlock()
    69  	if !ok {
    70  		t.Fatal("Host Pubkey and contract not in map")
    71  	}
    72  	if fcid != contract.ID {
    73  		t.Error("Wrong FileContractID found in map")
    74  	}
    75  	if pubKeyMapLen != 1 {
    76  		t.Fatal("pubkey map should have 1 entry")
    77  	}
    78  
    79  	// Add a slice of contracts
    80  	_, pubkey := crypto.GenerateKeyPair()
    81  	spk := types.SiaPublicKey{
    82  		Algorithm: types.SignatureEd25519,
    83  		Key:       pubkey[:],
    84  	}
    85  	c2 := skymodules.RenterContract{
    86  		ID:            types.FileContractID{'1'},
    87  		HostPublicKey: spk,
    88  	}
    89  	contracts := []skymodules.RenterContract{c2, contract}
    90  	c.mu.Lock()
    91  	c.updatePubKeyToContractIDMap(contracts)
    92  	c.mu.Unlock()
    93  
    94  	// Check pubkey map
    95  	c.mu.Lock()
    96  	pubKeyMapLen = len(c.pubKeysToContractID)
    97  	fcid, ok = c.pubKeysToContractID[pk.String()]
    98  	fcid2, ok2 := c.pubKeysToContractID[spk.String()]
    99  	c.mu.Unlock()
   100  	if !ok {
   101  		t.Fatal("Original Host Pubkey and contract not in map")
   102  	}
   103  	if fcid != contract.ID {
   104  		t.Error("Wrong FileContractID found in map")
   105  	}
   106  	if !ok2 {
   107  		t.Fatal("Second Host Pubkey and contract not in map")
   108  	}
   109  	if fcid2 != c2.ID {
   110  		t.Error("Wrong second FileContractID found in map")
   111  	}
   112  	if pubKeyMapLen != 2 {
   113  		t.Fatal("pubkey map should have 2 entries")
   114  	}
   115  }
   116  
   117  // TestTryAddContractToPubKeyMap tests the tryAddContractToPubKeyMap method
   118  func TestTryAddContractToPubKeyMap(t *testing.T) {
   119  	// Create minimum Contractor
   120  	c := &Contractor{
   121  		renewedTo:           make(map[types.FileContractID]types.FileContractID),
   122  		pubKeysToContractID: make(map[string]types.FileContractID),
   123  	}
   124  
   125  	// Create minimum contracts
   126  	_, pk := crypto.GenerateKeyPair()
   127  	spk := types.SiaPublicKey{
   128  		Algorithm: types.SignatureEd25519,
   129  		Key:       pk[:],
   130  	}
   131  	c1 := skymodules.RenterContract{
   132  		ID:            types.FileContractID{'1'},
   133  		HostPublicKey: spk,
   134  	}
   135  	_, pk = crypto.GenerateKeyPair()
   136  	spk = types.SiaPublicKey{
   137  		Algorithm: types.SignatureEd25519,
   138  		Key:       pk[:],
   139  	}
   140  	c2 := skymodules.RenterContract{
   141  		ID:            types.FileContractID{'2'},
   142  		HostPublicKey: spk,
   143  	}
   144  
   145  	// Add c1 to renewTo map
   146  	c.renewedTo[c1.ID] = c2.ID
   147  
   148  	// Try and add c1 to pubKey map, should not be added
   149  	c.tryAddContractToPubKeyMap(c1)
   150  	if len(c.pubKeysToContractID) != 0 {
   151  		t.Error("PubKey map should be empty")
   152  	}
   153  
   154  	// Adding c2 should work
   155  	c.tryAddContractToPubKeyMap(c2)
   156  	if len(c.pubKeysToContractID) != 1 {
   157  		t.Error("PubKey map should have 1 entry")
   158  	}
   159  	// Can't test the case of a pubkey already in the pubkey map as that results
   160  	// in a Critical log
   161  }