-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
107 lines (98 loc) · 3.38 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package jiffy
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-shipyard/telefil"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/host"
)
type (
// Option represents a configurable parameter in Motion service.
Option func(*options) error
options struct {
h host.Host
fil *telefil.Telefil
wallet Wallet
replicatorSpPicker func(context.Context, *Piece) ([]address.Address, error)
replicatorInterval *time.Ticker
replicatorVerificationInterval *time.Ticker
segmentorStoreDir string
segmentorChunkSizeBytes int64
segmentorMaxTotalSizeBytes int64
dealProviderCollateralPicker func(min, max abi.TokenAmount) abi.TokenAmount
dealPricePerEpochPicker func(pieceSize abi.PaddedPieceSize, start, end abi.ChainEpoch) abi.TokenAmount
dealVerified bool
dealSkipIPNIAnnounce bool
dealRemoveUnsealedCopy bool
dealOffline bool
dealPricePerGiBEpoch abi.TokenAmount
dealPricePerGiB abi.TokenAmount
dealPricePerDeal abi.TokenAmount
dealStartDelay abi.ChainEpoch
dealDuration abi.ChainEpoch
}
)
func newOptions(o ...Option) (*options, error) {
opts := options{
dealProviderCollateralPicker: func(min, max abi.TokenAmount) abi.TokenAmount { return min },
dealStartDelay: builtin.EpochsInDay * 4,
dealDuration: builtin.EpochsInYear,
dealOffline: true,
segmentorMaxTotalSizeBytes: 31 * GiB,
segmentorChunkSizeBytes: 1 * MiB,
dealVerified: true,
replicatorInterval: time.NewTicker(1 * time.Hour),
replicatorVerificationInterval: time.NewTicker(1 * time.Hour),
}
for _, apply := range o {
if err := apply(&opts); err != nil {
return nil, err
}
}
if opts.wallet == nil {
return nil, errors.New("wallet must be specified")
}
if opts.h == nil {
var err error
if opts.h, err = libp2p.New(); err != nil {
return nil, err
}
}
if opts.segmentorStoreDir == "" {
userHome, err := os.UserHomeDir()
if err != nil {
return nil, err
}
opts.segmentorStoreDir = filepath.Join(userHome, ".jiffy", "segments")
}
if opts.replicatorSpPicker == nil {
return nil, fmt.Errorf("storage provider picker must be set or at least one storage provider must be configured")
}
if opts.fil == nil {
var err error
if opts.fil, err = telefil.New(); err != nil {
return nil, err
}
}
if opts.dealPricePerEpochPicker == nil {
opts.dealPricePerEpochPicker = func(pieceSize abi.PaddedPieceSize, start, end abi.ChainEpoch) abi.TokenAmount {
// TODO maybe pass the draft deal proposal for a more sophisticated price picking e.g. per Provider?
durationEpochs := big.NewInt(int64(end - start))
pieceSizeInGiB := big.NewInt(int64(pieceSize / GiB))
var perGiB, perGiBEpoch abi.TokenAmount
perGiB.Mul(opts.dealPricePerGiB.Int, pieceSizeInGiB.Int)
perGiBEpoch.Mul(opts.dealPricePerGiBEpoch.Int, new(big.Int).Mul(pieceSizeInGiB.Int, durationEpochs.Int))
return big.Max(big.Max(perGiB, perGiBEpoch), opts.dealPricePerDeal)
}
}
return &opts, nil
}
// TODO add With* option setting