Skip to content

Commit

Permalink
fix handleRPCFormContract
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Dec 18, 2024
1 parent af7c48b commit b3acfca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
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)
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
6 changes: 3 additions & 3 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):]
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...)
}

// read the renter's signatures
Expand Down

0 comments on commit b3acfca

Please sign in to comment.