Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v4): Fix panic when forming contracts with different basis than the host #145

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions rhp/v4/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rhp_test
import (
"bytes"
"context"
"math"
"net"
"reflect"
"strings"
Expand Down Expand Up @@ -266,10 +267,44 @@ func TestFormContractBasis(t *testing.T) {
n, genesis := testutil.V2Network()
hostKey, renterKey := types.GeneratePrivateKey(), types.GeneratePrivateKey()

cm, s, w := startTestNode(t, n, genesis)
cm, s, _ := startTestNode(t, n, genesis)

// create a separate wallet that doesn't automatically sync with the test node
wCS, wState, err := chain.NewDBStore(chain.NewMemDB(), n, genesis)
if err != nil {
t.Fatal(err)
}
wCM := chain.NewManager(wCS, wState)
wStore := testutil.NewEphemeralWalletStore()
w, err := wallet.NewSingleAddressWallet(renterKey, wCM, wStore)
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Fatal(err)
}

// fund the wallet
mineAndSync(t, cm, w.Address(), int(n.MaturityDelay+20), w)
mineAndSync(t, cm, w.Address(), int(n.MaturityDelay+20))

// manually sync wallet's chain manager until one block before the tip
blocks, _, err := cm.BlocksForHistory([]types.BlockID{genesis.ID()}, cm.Tip().Height-1)
if err != nil {
t.Fatal(err)
} else if err := wCM.AddBlocks(blocks); err != nil {
t.Fatal(err)
}
rus, aus, err := wCM.UpdatesSince(types.ChainIndex{}, math.MaxInt32)
if err != nil {
t.Fatal(err)
}
err = wStore.UpdateChainState(func(tx wallet.UpdateTx) error {
return w.UpdateChainState(tx, rus, aus)
})
if err != nil {
t.Fatal(err)
}
balance, err := w.Balance()
if err != nil {
t.Fatal(err)
}

sr := testutil.NewEphemeralSettingsReporter()
sr.Update(proto4.HostSettings{
Expand Down Expand Up @@ -299,12 +334,11 @@ func TestFormContractBasis(t *testing.T) {
}

fundAndSign := &fundAndSign{w, renterKey}
renterAllowance, hostCollateral := types.Siacoins(100), types.Siacoins(200)
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: renterAllowance,
Collateral: hostCollateral,
Allowance: balance.Confirmed.Mul64(96).Div64(100), // almost the whole balance to force as many inputs as possible
Collateral: types.ZeroCurrency,
ProofHeight: cm.Tip().Height + 50,
})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions rhp/v4/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,14 @@ func (s *Server) handleRPCFormContract(stream net.Conn) error {

// update renter input basis to reflect our funding basis
if basis != req.Basis {
hostInputs := formationTxn.SiacoinInputs[len(formationTxn.SiacoinInputs)-len(req.RenterInputs)]
formationTxn.SiacoinInputs = formationTxn.SiacoinInputs[:len(formationTxn.SiacoinInputs)-len(req.RenterInputs)]
hostInputs := formationTxn.SiacoinInputs[len(req.RenterInputs):]
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
formationTxn.SiacoinInputs = formationTxn.SiacoinInputs[:len(req.RenterInputs)]
txnset, err := s.chain.UpdateV2TransactionSet([]types.V2Transaction{formationTxn}, req.Basis, basis)
if err != nil {
return errorBadRequest("failed to update renter inputs from %q to %q: %v", req.Basis, basis, err)
}
formationTxn = txnset[0]
formationTxn.SiacoinInputs = append(formationTxn.SiacoinInputs, hostInputs)
formationTxn.SiacoinInputs = append(formationTxn.SiacoinInputs, hostInputs...)
n8maninger marked this conversation as resolved.
Show resolved Hide resolved
}

// read the renter's signatures
Expand All @@ -611,8 +611,8 @@ func (s *Server) handleRPCFormContract(stream net.Conn) error {
formationTxn.FileContracts[0].RenterSignature = renterSigResp.RenterContractSignature

// add the renter signatures to the transaction
for i := range formationTxn.SiacoinInputs[:len(req.RenterInputs)] {
formationTxn.SiacoinInputs[i].SatisfiedPolicy = renterSigResp.RenterSatisfiedPolicies[i]
for i, policy := range renterSigResp.RenterSatisfiedPolicies {
formationTxn.SiacoinInputs[i].SatisfiedPolicy = policy
}

// add our signature to the contract
Expand Down
Loading