Skip to content

Commit

Permalink
Merge pull request #830 from SiaFoundation/chris/v0.7.1
Browse files Browse the repository at this point in the history
v0.7.1
  • Loading branch information
ChrisSchinnerl authored Dec 15, 2023
2 parents 7407235 + 29cf82e commit 1449ad4
Show file tree
Hide file tree
Showing 18 changed files with 480 additions and 240 deletions.
6 changes: 3 additions & 3 deletions api/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ func (c Contract) RenterFunds() types.Currency {
}

// RemainingCollateral returns the remaining collateral in the contract.
func (c Contract) RemainingCollateral(s rhpv2.HostSettings) types.Currency {
if c.Revision.MissedHostPayout().Cmp(s.ContractPrice) < 0 {
func (c Contract) RemainingCollateral() types.Currency {
if c.Revision.MissedHostPayout().Cmp(c.ContractPrice) < 0 {
return types.ZeroCurrency
}
return c.Revision.MissedHostPayout().Sub(s.ContractPrice)
return c.Revision.MissedHostPayout().Sub(c.ContractPrice)
}
2 changes: 1 addition & 1 deletion autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Bus interface {
WalletDiscard(ctx context.Context, txn types.Transaction) error
WalletOutputs(ctx context.Context) (resp []wallet.SiacoinElement, err error)
WalletPending(ctx context.Context) (resp []types.Transaction, err error)
WalletRedistribute(ctx context.Context, outputs int, amount types.Currency) (id types.TransactionID, err error)
WalletRedistribute(ctx context.Context, outputs int, amount types.Currency) (ids []types.TransactionID, err error)
}

type Autopilot struct {
Expand Down
52 changes: 38 additions & 14 deletions autopilot/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type (
resolver *ipResolver
logger *zap.SugaredLogger

maintenanceTxnID types.TransactionID
maintenanceTxnIDs []types.TransactionID

revisionBroadcastInterval time.Duration
revisionLastBroadcast map[types.FileContractID]time.Time
Expand Down Expand Up @@ -579,9 +579,11 @@ func (c *contractor) performWalletMaintenance(ctx context.Context) error {
return nil
}
for _, txn := range pending {
if c.maintenanceTxnID == txn.ID() {
l.Debugf("wallet maintenance skipped, pending transaction found with id %v", c.maintenanceTxnID)
return nil
for _, mTxnID := range c.maintenanceTxnIDs {
if mTxnID == txn.ID() {
l.Debugf("wallet maintenance skipped, pending transaction found with id %v", mTxnID)
return nil
}
}
}

Expand All @@ -607,13 +609,13 @@ func (c *contractor) performWalletMaintenance(ctx context.Context) error {
}

// redistribute outputs
id, err := b.WalletRedistribute(ctx, int(outputs), amount)
ids, err := b.WalletRedistribute(ctx, int(outputs), amount)
if err != nil {
return fmt.Errorf("failed to redistribute wallet into %d outputs of amount %v, balance %v, err %v", outputs, amount, balance, err)
}

l.Debugf("wallet maintenance succeeded, tx %v", id)
c.maintenanceTxnID = id
l.Debugf("wallet maintenance succeeded, txns %v", ids)
c.maintenanceTxnIDs = ids
return nil
}

Expand Down Expand Up @@ -1441,10 +1443,15 @@ func (c *contractor) refreshContract(ctx context.Context, w Worker, ci contractI
}

// calculate the renter funds
renterFunds, err := c.refreshFundingEstimate(ctx, state.cfg, ci, state.fee)
if err != nil {
c.logger.Errorw(fmt.Sprintf("could not get refresh funding estimate, err: %v", err), "hk", hk, "fcid", fcid)
return api.ContractMetadata{}, true, err
var renterFunds types.Currency
if isOutOfFunds(state.cfg, ci.settings, ci.contract) {
renterFunds, err = c.refreshFundingEstimate(ctx, state.cfg, ci, state.fee)
if err != nil {
c.logger.Errorw(fmt.Sprintf("could not get refresh funding estimate, err: %v", err), "hk", hk, "fcid", fcid)
return api.ContractMetadata{}, true, err
}
} else {
renterFunds = rev.ValidRenterPayout() // don't increase funds
}

// check our budget
Expand All @@ -1453,14 +1460,30 @@ func (c *contractor) refreshContract(ctx context.Context, w Worker, ci contractI
return api.ContractMetadata{}, false, fmt.Errorf("insufficient budget: %s < %s", budget.String(), renterFunds.String())
}

// calculate the new collateral
expectedStorage := renterFundsToExpectedStorage(renterFunds, contract.EndHeight()-cs.BlockHeight, ci.priceTable)
unallocatedCollateral := rev.MissedHostPayout().Sub(contract.ContractPrice)
minNewCollateral := minNewCollateral(unallocatedCollateral)

// calculate the expected new collateral to determine the minNewCollateral.
// If the contract isn't below the min collateral, we don't enforce a
// minimum.
var minNewColl types.Currency
_, _, expectedNewCollateral := rhpv3.RenewalCosts(contract.Revision.FileContract, ci.priceTable, expectedStorage, contract.EndHeight())
if isBelowCollateralThreshold(expectedNewCollateral, unallocatedCollateral) {
minNewColl = minNewCollateral(unallocatedCollateral)
}

// renew the contract
resp, err := w.RHPRenew(ctx, contract.ID, contract.EndHeight(), hk, contract.SiamuxAddr, settings.Address, state.address, renterFunds, minNewCollateral, expectedStorage, settings.WindowSize)
resp, err := w.RHPRenew(ctx, contract.ID, contract.EndHeight(), hk, contract.SiamuxAddr, settings.Address, state.address, renterFunds, minNewColl, expectedStorage, settings.WindowSize)
if err != nil {
if strings.Contains(err.Error(), "new collateral is too low") {
c.logger.Debugw("refresh failed: contract wouldn't have enough collateral after refresh",
"hk", hk,
"fcid", fcid,
"unallocatedCollateral", unallocatedCollateral.String(),
"minNewCollateral", minNewColl.String(),
)
return api.ContractMetadata{}, true, err
}
c.logger.Errorw("refresh failed", zap.Error(err), "hk", hk, "fcid", fcid)
if strings.Contains(err.Error(), wallet.ErrInsufficientBalance.Error()) {
return api.ContractMetadata{}, false, err
Expand All @@ -1484,6 +1507,7 @@ func (c *contractor) refreshContract(ctx context.Context, w Worker, ci contractI
"fcid", refreshedContract.ID,
"renewedFrom", contract.ID,
"renterFunds", renterFunds.String(),
"minNewCollateral", minNewColl.String(),
"newCollateral", newCollateral.String(),
)
return refreshedContract, true, nil
Expand Down
2 changes: 1 addition & 1 deletion autopilot/hostfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func isOutOfCollateral(c api.Contract, s rhpv2.HostSettings, pt rhpv3.HostPriceT
expectedStorage = s.RemainingStorage
}
_, _, newCollateral := rhpv3.RenewalCosts(c.Revision.FileContract, pt, expectedStorage, c.EndHeight())
return isBelowCollateralThreshold(newCollateral, c.RemainingCollateral(s))
return isBelowCollateralThreshold(newCollateral, c.RemainingCollateral())
}

// isBelowCollateralThreshold returns true if the remainingCollateral is below a
Expand Down
29 changes: 19 additions & 10 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type (
Balance() (spendable, confirmed, unconfirmed types.Currency, _ error)
FundTransaction(cs consensus.State, txn *types.Transaction, amount types.Currency, useUnconfirmedTxns bool) ([]types.Hash256, error)
Height() uint64
Redistribute(cs consensus.State, outputs int, amount, feePerByte types.Currency, pool []types.Transaction) (types.Transaction, []types.Hash256, error)
ReleaseInputs(txn types.Transaction)
Redistribute(cs consensus.State, outputs int, amount, feePerByte types.Currency, pool []types.Transaction) ([]types.Transaction, []types.Hash256, error)
ReleaseInputs(txn ...types.Transaction)
SignTransaction(cs consensus.State, txn *types.Transaction, toSign []types.Hash256, cf types.CoveredFields) error
Transactions(before, since time.Time, offset, limit int) ([]wallet.Transaction, error)
UnspentOutputs() ([]wallet.SiacoinElement, error)
Expand Down Expand Up @@ -602,22 +602,27 @@ func (b *bus) walletRedistributeHandler(jc jape.Context) {
}

cs := b.cm.TipState()
txn, toSign, err := b.w.Redistribute(cs, wfr.Outputs, wfr.Amount, b.tp.RecommendedFee(), b.tp.Transactions())
txns, toSign, err := b.w.Redistribute(cs, wfr.Outputs, wfr.Amount, b.tp.RecommendedFee(), b.tp.Transactions())
if jc.Check("couldn't redistribute money in the wallet into the desired outputs", err) != nil {
return
}

err = b.w.SignTransaction(cs, &txn, toSign, types.CoveredFields{WholeTransaction: true})
if jc.Check("couldn't sign the transaction", err) != nil {
return
var ids []types.TransactionID
for i := 0; i < len(txns); i++ {
err = b.w.SignTransaction(cs, &txns[i], toSign, types.CoveredFields{WholeTransaction: true})
if jc.Check("couldn't sign the transaction", err) != nil {
b.w.ReleaseInputs(txns...)
return
}
ids = append(ids, txns[i].ID())
}

if jc.Check("couldn't broadcast the transaction", b.tp.AcceptTransactionSet([]types.Transaction{txn})) != nil {
b.w.ReleaseInputs(txn)
if jc.Check("couldn't broadcast the transaction", b.tp.AcceptTransactionSet(txns)) != nil {
b.w.ReleaseInputs(txns...)
return
}

jc.Encode(txn.ID())
jc.Encode(ids)
}

func (b *bus) walletDiscardHandler(jc jape.Context) {
Expand Down Expand Up @@ -2066,7 +2071,11 @@ func (b *bus) metricsHandlerGET(jc jape.Context) {
if jc.DecodeForm("n", &n) != nil {
return
} else if n == 0 {
jc.Error(errors.New("parameter 'n' is required"), http.StatusBadRequest)
if jc.Request.FormValue("n") == "" {
jc.Error(errors.New("parameter 'n' is required"), http.StatusBadRequest)
} else {
jc.Error(errors.New("'n' has to be greater than zero"), http.StatusBadRequest)
}
return
}

Expand Down
4 changes: 2 additions & 2 deletions bus/client/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func (c *Client) WalletPrepareRenew(ctx context.Context, revision types.FileCont
// WalletRedistribute broadcasts a transaction that redistributes the money in
// the wallet in the desired number of outputs of given amount. If the
// transaction was successfully broadcasted it will return the transaction ID.
func (c *Client) WalletRedistribute(ctx context.Context, outputs int, amount types.Currency) (id types.TransactionID, err error) {
func (c *Client) WalletRedistribute(ctx context.Context, outputs int, amount types.Currency) (ids []types.TransactionID, err error) {
req := api.WalletRedistributeRequest{
Amount: amount,
Outputs: outputs,
}

err = c.c.WithContext(ctx).POST("/wallet/redistribute", req, &id)
err = c.c.WithContext(ctx).POST("/wallet/redistribute", req, &ids)
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/renterd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func main() {

flag.Parse()

log.Println("renterd v0.6.0")
log.Println("renterd v0.7.1")
log.Println("Network", build.NetworkName())
if flag.Arg(0) == "version" {
log.Println("Commit:", githash)
Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ RUN if [ "$BUILD_RUN_GO_GENERATE" = "true" ] ; then go generate ./... ; fi
# Build renterd.
RUN --mount=type=cache,target=/root/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=1 go build -ldflags="-s -w" -tags="${BUILD_TAGS}" ./cmd/renterd
CGO_ENABLED=1 go build -ldflags='-s -w -linkmode external -extldflags "-static"' -tags="${BUILD_TAGS}" ./cmd/renterd

# Build image that will be used to run renterd.
FROM debian:bookworm-slim
FROM alpine:3
LABEL maintainer="The Sia Foundation <[email protected]>" \
org.opencontainers.image.description.vendor="The Sia Foundation" \
org.opencontainers.image.description="A renterd container - next-generation Sia renter" \
Expand Down
2 changes: 1 addition & 1 deletion docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

if [[ "$BUILD_TAGS" == *'testnet'* ]]; then
exec renterd -http=':9880' -s3.address=':7070' "$@"
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/google/go-cmp v0.6.0
github.com/gotd/contrib v0.19.0
github.com/klauspost/reedsolomon v1.11.8
github.com/klauspost/reedsolomon v1.12.0
github.com/minio/minio-go/v7 v7.0.65
github.com/montanaflynn/stats v0.7.1
gitlab.com/NebulousLabs/encoding v0.0.0-20200604091946-456c3dc907fe
Expand Down Expand Up @@ -36,7 +36,7 @@ require (

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
github.com/aws/aws-sdk-go v1.48.13 // indirect
github.com/aws/aws-sdk-go v1.49.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cloudflare/cloudflare-go v0.75.0 // indirect
github.com/dchest/threefish v0.0.0-20120919164726-3ecf4c494abf // indirect
Expand All @@ -48,7 +48,7 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down Expand Up @@ -86,16 +86,16 @@ require (
gitlab.com/NebulousLabs/threadgroup v0.0.0-20200608151952-38921fbef213 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.sia.tech/web v0.0.0-20231205212549-23f4fb47d5f6 // indirect
go.sia.tech/web v0.0.0-20231213145933-3f175a86abff // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 // indirect
google.golang.org/grpc v1.59.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/grpc v1.60.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
nhooyr.io/websocket v1.8.10 // indirect
Expand Down
Loading

0 comments on commit 1449ad4

Please sign in to comment.