From a3610f9188f9980e2d5d0d023e9865b54b71f18f Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Mon, 2 Dec 2024 14:12:18 +0100 Subject: [PATCH] v4: TestRPCLatestRevision --- rhp/v4/rpc_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/rhp/v4/rpc_test.go b/rhp/v4/rpc_test.go index 8b8be64..33af137 100644 --- a/rhp/v4/rpc_test.go +++ b/rhp/v4/rpc_test.go @@ -321,6 +321,84 @@ func TestFormContractBasis(t *testing.T) { } } +func TestRPCLatestRevision(t *testing.T) { + n, genesis := testutil.V2Network() + hostKey, renterKey := types.GeneratePrivateKey(), types.GeneratePrivateKey() + cm, s, w := startTestNode(t, n, genesis) + + // fund the wallet + mineAndSync(t, cm, w.Address(), int(n.MaturityDelay+20), w) + + sr := testutil.NewEphemeralSettingsReporter() + sr.Update(proto4.HostSettings{ + Release: "test", + AcceptingContracts: true, + WalletAddress: w.Address(), + MaxCollateral: types.Siacoins(10000), + MaxContractDuration: 1000, + RemainingStorage: 100 * proto4.SectorSize, + TotalStorage: 100 * proto4.SectorSize, + Prices: proto4.HostPrices{ + ContractPrice: types.Siacoins(1).Div64(5), // 0.2 SC + StoragePrice: types.NewCurrency64(100), // 100 H / byte / block + IngressPrice: types.NewCurrency64(100), // 100 H / byte + EgressPrice: types.NewCurrency64(100), // 100 H / byte + Collateral: types.NewCurrency64(200), + }, + }) + ss := testutil.NewEphemeralSectorStore() + c := testutil.NewEphemeralContractor(cm) + + transport := testRenterHostPair(t, hostKey, cm, s, w, c, sr, ss, zap.NewNop()) + + settings, err := rhp4.RPCSettings(context.Background(), transport) + if err != nil { + t.Fatal(err) + } + fundAndSign := &fundAndSign{w, renterKey} + + result, err := rhp4.RPCFormContract(context.Background(), transport, cm, fundAndSign, cm.TipState(), settings.Prices, hostKey.PublicKey(), settings.WalletAddress, proto4.RPCFormContractParams{ + RenterPublicKey: renterKey.PublicKey(), + RenterAddress: w.Address(), + Allowance: types.Siacoins(1), + Collateral: types.Siacoins(1), + ProofHeight: cm.Tip().Height + 50, + }) + if err != nil { + t.Fatal(err) + } + + // validate contract signatures + contract := result.Contract.Revision + contractSigHash := cm.TipState().ContractSigHash(contract) + if !hostKey.PublicKey().VerifyHash(contractSigHash, contract.HostSignature) { + t.Fatal("host signature verification failed") + } else if !renterKey.PublicKey().VerifyHash(contractSigHash, contract.RenterSignature) { + t.Fatal("renter signature verification failed") + } + + // verify the transaction set is valid + if known, err := cm.AddV2PoolTransactions(result.FormationSet.Basis, result.FormationSet.Transactions); err != nil { + t.Fatal(err) + } else if !known { + t.Fatal("expected transaction set to be known") + } + + // fetch revision + contract, err = rhp4.RPCLatestRevision(context.Background(), transport, result.Contract.ID) + if err != nil { + t.Fatal(err) + } + + // validate contract signatures + contractSigHash = cm.TipState().ContractSigHash(contract) + if !hostKey.PublicKey().VerifyHash(contractSigHash, contract.HostSignature) { + t.Fatal("host signature verification failed") + } else if !renterKey.PublicKey().VerifyHash(contractSigHash, contract.RenterSignature) { + t.Fatal("renter signature verification failed") + } +} + func TestRPCRefresh(t *testing.T) { n, genesis := testutil.V2Network() hostKey, renterKey := types.GeneratePrivateKey(), types.GeneratePrivateKey()