Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Eliminate nonsense logic during contract negotiation #3150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 7 additions & 14 deletions modules/host/negotiateformcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func contractCollateral(settings modules.HostExternalSettings, fc types.FileCont
}

// managedAddCollateral adds the host's collateral to the file contract
// transaction set, returning the new inputs and outputs that get added to the
// transaction set, returning the new inputs that get added to the
// transaction, as well as any new parents that get added to the transaction
// set. The builder that is used to add the collateral is also returned,
// because the new transaction has not yet been signed.
func (h *Host) managedAddCollateral(settings modules.HostExternalSettings, txnSet []types.Transaction) (builder modules.TransactionBuilder, newParents []types.Transaction, newInputs []types.SiacoinInput, newOutputs []types.SiacoinOutput, err error) {
func (h *Host) managedAddCollateral(settings modules.HostExternalSettings, txnSet []types.Transaction) (builder modules.TransactionBuilder, newParents []types.Transaction, newInputs []types.SiacoinInput, err error) {
txn := txnSet[len(txnSet)-1]
parents := txnSet[:len(txnSet)-1]
fc := txn.FileContracts[0]
Expand All @@ -45,22 +45,19 @@ func (h *Host) managedAddCollateral(settings modules.HostExternalSettings, txnSe
err = builder.FundSiacoins(hostPortion)
if err != nil {
builder.Drop()
return nil, nil, nil, nil, extendErr("could not add collateral: ", ErrorInternal(err.Error()))
return nil, nil, nil, extendErr("could not add collateral: ", ErrorInternal(err.Error()))
}

// Return which inputs and outputs have been added by the collateral call.
newParentIndices, newInputIndices, newOutputIndices, _ := builder.ViewAdded()
newParentIndices, newInputIndices, _, _ := builder.ViewAdded()
updatedTxn, updatedParents := builder.View()
for _, parentIndex := range newParentIndices {
newParents = append(newParents, updatedParents[parentIndex])
}
for _, inputIndex := range newInputIndices {
newInputs = append(newInputs, updatedTxn.SiacoinInputs[inputIndex])
}
for _, outputIndex := range newOutputIndices {
newOutputs = append(newOutputs, updatedTxn.SiacoinOutputs[outputIndex])
}
return builder, newParents, newInputs, newOutputs, nil
return builder, newParents, newInputs, nil
}

// managedRPCFormContract accepts a file contract from a renter, checks the
Expand Down Expand Up @@ -119,13 +116,13 @@ func (h *Host) managedRPCFormContract(conn net.Conn) error {
return extendErr("contract verification failed: ", err)
}
// The host adds collateral to the transaction.
txnBuilder, newParents, newInputs, newOutputs, err := h.managedAddCollateral(settings, txnSet)
txnBuilder, newParents, newInputs, err := h.managedAddCollateral(settings, txnSet)
if err != nil {
modules.WriteNegotiationRejection(conn, err) // Error ignored to preserve type in extendErr
return extendErr("failed to add collateral: ", err)
}
// The host indicates acceptance, and then sends any new parent
// transactions, inputs and outputs that were added to the transaction.
// transactions and inputs that were added to the transaction.
err = modules.WriteNegotiationAcceptance(conn)
if err != nil {
return extendErr("accepting verified contract failed: ", ErrorConnection(err.Error()))
Expand All @@ -138,10 +135,6 @@ func (h *Host) managedRPCFormContract(conn net.Conn) error {
if err != nil {
return extendErr("failed to write new inputs: ", ErrorConnection(err.Error()))
}
err = encoding.WriteObject(conn, newOutputs)
if err != nil {
return extendErr("failed to write new outputs: ", ErrorConnection(err.Error()))
}

// The renter will now send a negotiation response, followed by transaction
// signatures for the file contract transaction in the case of acceptance.
Expand Down
23 changes: 8 additions & 15 deletions modules/host/negotiaterenewcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func renewContractCollateral(so storageObligation, settings modules.HostExternal

// managedAddRenewCollateral adds the host's collateral to the renewed file
// contract.
func (h *Host) managedAddRenewCollateral(so storageObligation, settings modules.HostExternalSettings, txnSet []types.Transaction) (builder modules.TransactionBuilder, newParents []types.Transaction, newInputs []types.SiacoinInput, newOutputs []types.SiacoinOutput, err error) {
func (h *Host) managedAddRenewCollateral(so storageObligation, settings modules.HostExternalSettings, txnSet []types.Transaction) (builder modules.TransactionBuilder, newParents []types.Transaction, newInputs []types.SiacoinInput, err error) {
txn := txnSet[len(txnSet)-1]
parents := txnSet[:len(txnSet)-1]
fc := txn.FileContracts[0]
Expand All @@ -58,22 +58,19 @@ func (h *Host) managedAddRenewCollateral(so storageObligation, settings modules.
err = builder.FundSiacoins(hostPortion)
if err != nil {
builder.Drop()
return nil, nil, nil, nil, extendErr("could not add collateral: ", ErrorInternal(err.Error()))
return nil, nil, nil, extendErr("could not add collateral: ", ErrorInternal(err.Error()))
}

// Return which inputs and outputs have been added by the collateral call.
newParentIndices, newInputIndices, newOutputIndices, _ := builder.ViewAdded()
// Return which inputs have been added by the collateral call.
newParentIndices, newInputIndices, _, _ := builder.ViewAdded()
updatedTxn, updatedParents := builder.View()
for _, parentIndex := range newParentIndices {
newParents = append(newParents, updatedParents[parentIndex])
}
for _, inputIndex := range newInputIndices {
newInputs = append(newInputs, updatedTxn.SiacoinInputs[inputIndex])
}
for _, outputIndex := range newOutputIndices {
newOutputs = append(newOutputs, updatedTxn.SiacoinOutputs[outputIndex])
}
return builder, newParents, newInputs, newOutputs, nil
return builder, newParents, newInputs, nil
}

// managedRenewContract accepts a request to renew a file contract.
Expand Down Expand Up @@ -131,13 +128,13 @@ func (h *Host) managedRPCRenewContract(conn net.Conn) error {
modules.WriteNegotiationRejection(conn, err) // Error is ignored to preserve type for extendErr
return extendErr("verification of renewal failed: ", err)
}
txnBuilder, newParents, newInputs, newOutputs, err := h.managedAddRenewCollateral(so, settings, txnSet)
txnBuilder, newParents, newInputs, err := h.managedAddRenewCollateral(so, settings, txnSet)
if err != nil {
modules.WriteNegotiationRejection(conn, err) // Error is ignored to preserve type for extendErr
return extendErr("failed to add collateral: ", err)
}
// The host indicates acceptance, then sends the new parents, inputs, and
// outputs to the transaction.
// The host indicates acceptance, then sends the new parents and inputs to
// the transaction.
err = modules.WriteNegotiationAcceptance(conn)
if err != nil {
return extendErr("failed to write acceptance: ", ErrorConnection(err.Error()))
Expand All @@ -150,10 +147,6 @@ func (h *Host) managedRPCRenewContract(conn net.Conn) error {
if err != nil {
return extendErr("failed to write new inputs: ", ErrorConnection(err.Error()))
}
err = encoding.WriteObject(conn, newOutputs)
if err != nil {
return extendErr("failed to write new outputs: ", ErrorConnection(err.Error()))
}

// The renter will send a negotiation response, followed by transaction
// signatures for the file contract transaction in the case of acceptance.
Expand Down
7 changes: 0 additions & 7 deletions modules/renter/proto/formcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,18 @@ func (cs *ContractSet) FormContract(params ContractParams, txnBuilder transactio
// were added to the transaction.
var newParents []types.Transaction
var newInputs []types.SiacoinInput
var newOutputs []types.SiacoinOutput
if err = encoding.ReadObject(conn, &newParents, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added parents: " + err.Error())
}
if err = encoding.ReadObject(conn, &newInputs, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added inputs: " + err.Error())
}
if err = encoding.ReadObject(conn, &newOutputs, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added outputs: " + err.Error())
}

// Merge txnAdditions with txnSet.
txnBuilder.AddParents(newParents)
for _, input := range newInputs {
txnBuilder.AddSiacoinInput(input)
}
for _, output := range newOutputs {
txnBuilder.AddSiacoinOutput(output)
}

// Sign the txn.
signedTxnSet, err := txnBuilder.Sign(true)
Expand Down
9 changes: 1 addition & 8 deletions modules/renter/proto/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,22 @@ func (cs *ContractSet) Renew(oldContract *SafeContract, params ContractParams, t
if err = modules.ReadNegotiationAcceptance(conn); err != nil {
return modules.RenterContract{}, errors.New("host did not accept our proposed contract: " + err.Error())
}
// host now sends any new parent transactions, inputs and outputs that
// host now sends any new parent transactions and inputs that
// were added to the transaction
var newParents []types.Transaction
var newInputs []types.SiacoinInput
var newOutputs []types.SiacoinOutput
if err = encoding.ReadObject(conn, &newParents, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added parents: " + err.Error())
}
if err = encoding.ReadObject(conn, &newInputs, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added inputs: " + err.Error())
}
if err = encoding.ReadObject(conn, &newOutputs, types.BlockSizeLimit); err != nil {
return modules.RenterContract{}, errors.New("couldn't read the host's added outputs: " + err.Error())
}

// merge txnAdditions with txnSet
txnBuilder.AddParents(newParents)
for _, input := range newInputs {
txnBuilder.AddSiacoinInput(input)
}
for _, output := range newOutputs {
txnBuilder.AddSiacoinOutput(output)
}

// sign the txn
signedTxnSet, err := txnBuilder.Sign(true)
Expand Down