|
| 1 | +package rhp |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | + |
| 7 | + rhp2 "go.sia.tech/core/rhp/v2" |
| 8 | + "go.sia.tech/core/types" |
| 9 | + "lukechampine.com/frand" |
| 10 | +) |
| 11 | + |
| 12 | +func TestValidateContractRenewal(t *testing.T) { |
| 13 | + hostKey, renterKey := types.NewPrivateKeyFromSeed(frand.Bytes(32)).PublicKey(), types.NewPrivateKeyFromSeed(frand.Bytes(32)).PublicKey() |
| 14 | + hostAddress, renterAddress := types.StandardUnlockHash(hostKey), types.StandardUnlockHash(renterKey) |
| 15 | + hostCollateral := types.NewCurrency64(frand.Uint64n(math.MaxUint64)) |
| 16 | + renterAllowance := types.NewCurrency64(frand.Uint64n(math.MaxUint64)) |
| 17 | + |
| 18 | + settings := rhp2.HostSettings{ |
| 19 | + MaxDuration: math.MaxUint64, |
| 20 | + MaxCollateral: types.NewCurrency(math.MaxUint64, math.MaxUint64), |
| 21 | + Address: hostAddress, |
| 22 | + } |
| 23 | + |
| 24 | + existing := types.FileContractRevision{ |
| 25 | + ParentID: types.FileContractID{1}, |
| 26 | + UnlockConditions: types.UnlockConditions{ |
| 27 | + PublicKeys: []types.UnlockKey{renterKey.UnlockKey(), hostKey.UnlockKey()}, |
| 28 | + SignaturesRequired: 2, |
| 29 | + }, |
| 30 | + FileContract: types.FileContract{ |
| 31 | + RevisionNumber: frand.Uint64n(math.MaxUint64), |
| 32 | + Filesize: frand.Uint64n(math.MaxUint64), |
| 33 | + FileMerkleRoot: frand.Entropy256(), |
| 34 | + WindowStart: 100, |
| 35 | + WindowEnd: 300, |
| 36 | + Payout: types.ZeroCurrency, // not validated here |
| 37 | + UnlockHash: types.Hash256(types.UnlockConditions{ |
| 38 | + PublicKeys: []types.UnlockKey{renterKey.UnlockKey(), hostKey.UnlockKey()}, |
| 39 | + SignaturesRequired: 2, |
| 40 | + }.UnlockHash()), |
| 41 | + ValidProofOutputs: []types.SiacoinOutput{ |
| 42 | + {Address: renterAddress, Value: renterAllowance}, |
| 43 | + {Address: hostAddress, Value: hostCollateral}, |
| 44 | + }, |
| 45 | + MissedProofOutputs: []types.SiacoinOutput{ |
| 46 | + {Address: renterAddress, Value: renterAllowance}, |
| 47 | + {Address: hostAddress, Value: hostCollateral}, |
| 48 | + {Address: types.VoidAddress, Value: types.ZeroCurrency}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + renewal := types.FileContract{ |
| 54 | + Filesize: existing.Filesize, |
| 55 | + FileMerkleRoot: existing.FileMerkleRoot, |
| 56 | + WindowStart: existing.WindowStart + 100, |
| 57 | + WindowEnd: existing.WindowEnd + 100, |
| 58 | + ValidProofOutputs: []types.SiacoinOutput{ |
| 59 | + {Address: renterAddress, Value: renterAllowance}, |
| 60 | + {Address: hostAddress, Value: hostCollateral}, |
| 61 | + }, |
| 62 | + MissedProofOutputs: []types.SiacoinOutput{ |
| 63 | + {Address: renterAddress, Value: renterAllowance}, |
| 64 | + {Address: hostAddress, Value: hostCollateral}, |
| 65 | + {Address: types.VoidAddress, Value: types.ZeroCurrency}, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + // bad renter key |
| 70 | + badRenterKey := types.NewPrivateKeyFromSeed(frand.Bytes(32)).PublicKey().UnlockKey() |
| 71 | + renewal.UnlockHash = types.Hash256(contractUnlockConditions(hostKey.UnlockKey(), badRenterKey).UnlockHash()) |
| 72 | + _, _, _, err := validateContractRenewal(existing, renewal, hostKey.UnlockKey(), renterKey.UnlockKey(), types.ZeroCurrency, types.ZeroCurrency, 0, settings) |
| 73 | + if err == nil || err.Error() != "incorrect unlock hash" { |
| 74 | + t.Fatalf("expected unlock hash error, got %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + // bad host key |
| 78 | + badHostKey := types.NewPrivateKeyFromSeed(frand.Bytes(32)).PublicKey().UnlockKey() |
| 79 | + renewal.UnlockHash = types.Hash256(contractUnlockConditions(badHostKey, renterKey.UnlockKey()).UnlockHash()) |
| 80 | + _, _, _, err = validateContractRenewal(existing, renewal, hostKey.UnlockKey(), renterKey.UnlockKey(), types.ZeroCurrency, types.ZeroCurrency, 0, settings) |
| 81 | + if err == nil || err.Error() != "incorrect unlock hash" { |
| 82 | + t.Fatalf("expected unlock hash error, got %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + // original keys |
| 86 | + renewal.UnlockHash = types.Hash256(contractUnlockConditions(hostKey.UnlockKey(), renterKey.UnlockKey()).UnlockHash()) |
| 87 | + _, _, _, err = validateContractRenewal(existing, renewal, hostKey.UnlockKey(), renterKey.UnlockKey(), types.ZeroCurrency, types.ZeroCurrency, 0, settings) |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + |
| 92 | + // different renter key, same host key |
| 93 | + newRenterKey := types.NewPrivateKeyFromSeed(frand.Bytes(32)).PublicKey().UnlockKey() |
| 94 | + renewal.UnlockHash = types.Hash256(contractUnlockConditions(hostKey.UnlockKey(), newRenterKey).UnlockHash()) |
| 95 | + _, _, _, err = validateContractRenewal(existing, renewal, hostKey.UnlockKey(), newRenterKey, types.ZeroCurrency, types.ZeroCurrency, 0, settings) |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | +} |
0 commit comments