forked from filecoin-project/go-fil-markets
-
Notifications
You must be signed in to change notification settings - Fork 2
/
storedask.go
208 lines (171 loc) · 5.9 KB
/
storedask.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package storedask
import (
"bytes"
"context"
"sync"
"github.com/ipfs/go-datastore"
logging "github.com/ipfs/go-log/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
versioning "github.com/filecoin-project/go-ds-versioning/pkg"
versionedds "github.com/filecoin-project/go-ds-versioning/pkg/datastore"
"github.com/filecoin-project/go-ds-versioning/pkg/versioned"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/boost-gfm/storagemarket"
"github.com/filecoin-project/boost-gfm/storagemarket/impl/providerutils"
"github.com/filecoin-project/boost-gfm/storagemarket/migrations"
)
var log = logging.Logger("storedask")
// DefaultPrice is the default price for unverified deals (in attoFil / GiB / Epoch)
var DefaultPrice = abi.NewTokenAmount(500000000)
// DefaultVerifiedPrice is the default price for verified deals (in attoFil / GiB / Epoch)
var DefaultVerifiedPrice = abi.NewTokenAmount(50000000)
// DefaultDuration is the default number of epochs a storage ask is in effect for
const DefaultDuration abi.ChainEpoch = 1000000
// DefaultMinPieceSize is the minimum accepted piece size for data
const DefaultMinPieceSize abi.PaddedPieceSize = 256
// DefaultMaxPieceSize is the default maximum accepted size for pieces for deals
// TODO: It would be nice to default this to the miner's sector size
const DefaultMaxPieceSize abi.PaddedPieceSize = 1 << 20
// StoredAsk implements a persisted SignedStorageAsk that lasts through restarts
// It also maintains a cache of the current SignedStorageAsk in memory
type StoredAsk struct {
askLk sync.RWMutex
ask *storagemarket.SignedStorageAsk
ds datastore.Batching
dsKey datastore.Key
spn storagemarket.StorageProviderNode
actor address.Address
}
// NewStoredAsk returns a new instance of StoredAsk
// It will initialize a new SignedStorageAsk on disk if one is not set
// Otherwise it loads the current SignedStorageAsk from disk
func NewStoredAsk(ds datastore.Batching, dsKey datastore.Key, spn storagemarket.StorageProviderNode, actor address.Address,
opts ...storagemarket.StorageAskOption) (*StoredAsk, error) {
s := &StoredAsk{
spn: spn,
actor: actor,
dsKey: dsKey,
}
askMigrations, err := versioned.BuilderList{
versioned.NewVersionedBuilder(migrations.GetMigrateSignedStorageAsk0To1(s.sign), versioning.VersionKey("1")),
}.Build()
if err != nil {
return nil, err
}
versionedDs, migrateDs := versionedds.NewVersionedDatastore(ds, askMigrations, versioning.VersionKey("1"))
// TODO: this is a bit risky -- but this is just a single key so it's probably ok to run migrations in the constructor
err = migrateDs(context.TODO())
if err != nil {
return nil, err
}
s.ds = versionedDs
if err := s.tryLoadAsk(); err != nil {
return nil, err
}
if s.ask == nil {
// TODO: we should be fine with this state, and just say it means 'not actively accepting deals'
// for now... lets just set a price
if err := s.SetAsk(DefaultPrice, DefaultVerifiedPrice, DefaultDuration, opts...); err != nil {
return nil, xerrors.Errorf("failed setting a default price: %w", err)
}
}
return s, nil
}
// SetAsk configures the storage miner's ask with the provided prices (for unverified and verified deals),
// duration, and options. Any previously-existing ask is replaced. If no options are passed to configure
// MinPieceSize and MaxPieceSize, the previous ask's values will be used, if available.
// It also increments the sequence number on the ask
func (s *StoredAsk) SetAsk(price abi.TokenAmount, verifiedPrice abi.TokenAmount, duration abi.ChainEpoch, options ...storagemarket.StorageAskOption) error {
s.askLk.Lock()
defer s.askLk.Unlock()
var seqno uint64
minPieceSize := DefaultMinPieceSize
maxPieceSize := DefaultMaxPieceSize
if s.ask != nil {
seqno = s.ask.Ask.SeqNo + 1
minPieceSize = s.ask.Ask.MinPieceSize
maxPieceSize = s.ask.Ask.MaxPieceSize
}
ctx := context.TODO()
_, height, err := s.spn.GetChainHead(ctx)
if err != nil {
return err
}
ask := &storagemarket.StorageAsk{
Price: price,
VerifiedPrice: verifiedPrice,
Timestamp: height,
Expiry: height + duration,
Miner: s.actor,
SeqNo: seqno,
MinPieceSize: minPieceSize,
MaxPieceSize: maxPieceSize,
}
for _, option := range options {
option(ask)
}
sig, err := s.sign(ctx, ask)
if err != nil {
return err
}
return s.saveAsk(&storagemarket.SignedStorageAsk{
Ask: ask,
Signature: sig,
})
}
func (s *StoredAsk) sign(ctx context.Context, ask *storagemarket.StorageAsk) (*crypto.Signature, error) {
tok, _, err := s.spn.GetChainHead(ctx)
if err != nil {
return nil, err
}
return providerutils.SignMinerData(ctx, ask, s.actor, tok, s.spn.GetMinerWorkerAddress, s.spn.SignBytes)
}
// GetAsk returns the current signed storage ask, or nil if one does not exist.
func (s *StoredAsk) GetAsk() *storagemarket.SignedStorageAsk {
s.askLk.RLock()
defer s.askLk.RUnlock()
if s.ask == nil {
return nil
}
ask := *s.ask
return &ask
}
func (s *StoredAsk) tryLoadAsk() error {
s.askLk.Lock()
defer s.askLk.Unlock()
err := s.loadAsk()
if err != nil {
if xerrors.Is(err, datastore.ErrNotFound) {
log.Warn("no previous ask found, miner will not accept deals until a price is set")
return nil
}
return err
}
return nil
}
func (s *StoredAsk) loadAsk() error {
askb, err := s.ds.Get(context.TODO(), s.dsKey)
if err != nil {
return xerrors.Errorf("failed to load most recent ask from disk: %w", err)
}
var ssa storagemarket.SignedStorageAsk
if err := cborutil.ReadCborRPC(bytes.NewReader(askb), &ssa); err != nil {
return err
}
s.ask = &ssa
return nil
}
func (s *StoredAsk) saveAsk(a *storagemarket.SignedStorageAsk) error {
b, err := cborutil.Dump(a)
if err != nil {
return err
}
if err := s.ds.Put(context.TODO(), s.dsKey, b); err != nil {
return err
}
s.ask = a
return nil
}