-
Notifications
You must be signed in to change notification settings - Fork 462
/
randomness.go
313 lines (262 loc) · 11.5 KB
/
randomness.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package chain
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"math/rand"
"github.com/filecoin-project/venus/pkg/beacon"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
"github.com/minio/blake2b-simd"
"github.com/pkg/errors"
)
type RandomSeed []byte
var _ RandomnessSource = (*GenesisRandomnessSource)(nil)
// A sampler for use when computing genesis state (the state that the genesis block points to as parent state).
// There is no chain to sample a seed from.
type GenesisRandomnessSource struct {
vrf types.VRFPi
}
func NewGenesisRandomnessSource(vrf types.VRFPi) *GenesisRandomnessSource {
return &GenesisRandomnessSource{vrf: vrf}
}
func (g *GenesisRandomnessSource) ChainGetRandomnessFromBeacon(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) ChainGetRandomnessFromTickets(ctx context.Context, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) GetChainRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch * 1000))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) GetChainRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch * 1000))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) GetBeaconRandomnessV3(ctx context.Context, pers crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) GetBeaconRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
func (g *GenesisRandomnessSource) GetBeaconRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
out := make([]byte, 32)
_, _ = rand.New(rand.NewSource(int64(randEpoch))).Read(out) //nolint
return out, nil
}
// Computes a random seed from raw ticket bytes.
// A randomness seed is the VRF digest of the minimum ticket of the tipset at or before the requested epoch
func MakeRandomSeed(rawVRFProof types.VRFPi) (RandomSeed, error) {
digest := rawVRFProof.Digest()
return digest[:], nil
}
///// GetRandomnessFromTickets derivation /////
// RandomnessSource provides randomness to actors.
type RandomnessSource interface {
GetChainRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error)
GetChainRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error)
GetBeaconRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error)
GetBeaconRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error)
GetBeaconRandomnessV3(ctx context.Context, pers crypto.DomainSeparationTag, filecoinEpoch abi.ChainEpoch, entropy []byte) ([]byte, error)
}
type TipSetByHeight interface {
GetTipSet(context.Context, types.TipSetKey) (*types.TipSet, error)
GetTipSetByHeight(context.Context, *types.TipSet, abi.ChainEpoch, bool) (*types.TipSet, error)
}
var _ RandomnessSource = (*ChainRandomnessSource)(nil)
type NetworkVersionGetter func(context.Context, abi.ChainEpoch) network.Version
// A randomness source that seeds computations with a sample drawn from a chain epoch.
type ChainRandomnessSource struct { //nolint
reader TipSetByHeight
head types.TipSetKey
beacon beacon.Schedule
networkVersionGetter NetworkVersionGetter
}
func NewChainRandomnessSource(reader TipSetByHeight, head types.TipSetKey, beacon beacon.Schedule, networkVersionGetter NetworkVersionGetter) RandomnessSource {
return &ChainRandomnessSource{reader: reader, head: head, beacon: beacon, networkVersionGetter: networkVersionGetter}
}
func (c *ChainRandomnessSource) GetBeaconRandomnessTipset(ctx context.Context, randEpoch abi.ChainEpoch, lookback bool) (*types.TipSet, error) {
ts, err := c.reader.GetTipSet(ctx, c.head)
if err != nil {
return nil, err
}
if randEpoch > ts.Height() {
return nil, fmt.Errorf("cannot draw randomness from the future")
}
searchHeight := randEpoch
if searchHeight < 0 {
searchHeight = 0
}
randTS, err := c.reader.GetTipSetByHeight(ctx, ts, searchHeight, lookback)
if err != nil {
return nil, err
}
return randTS, nil
}
// Draws a ticket from the chain identified by `head` and the highest tipset with height <= `epoch`.
// If `head` is empty (as when processing the pre-genesis state or the genesis block), the seed derived from
// a fixed genesis ticket.
// Note that this may produce the same value for different, neighbouring epochs when the epoch references a round
// in which no blocks were produced (an empty tipset or "null block"). A caller desiring a unique see for each epoch
// should blend in some distinguishing value (such as the epoch itself) into a hash of this ticket.
func (c *ChainRandomnessSource) GetChainRandomness(ctx context.Context, epoch abi.ChainEpoch, lookback bool) (types.Ticket, error) {
if !c.head.IsEmpty() {
start, err := c.reader.GetTipSet(ctx, c.head)
if err != nil {
return types.Ticket{}, err
}
if epoch > start.Height() {
return types.Ticket{}, fmt.Errorf("cannot draw randomness from the future")
}
searchHeight := epoch
if searchHeight < 0 {
searchHeight = 0
}
// Note: it is not an error to have epoch > start.Height(); in the case of a run of null blocks the
// sought-after height may be after the base (last non-empty) tipset.
// It's also not an error for the requested epoch to be negative.
tip, err := c.reader.GetTipSetByHeight(ctx, start, searchHeight, lookback)
if err != nil {
return types.Ticket{}, err
}
return *tip.MinTicket(), nil
}
return types.Ticket{}, fmt.Errorf("cannot get ticket for empty tipset")
}
// network v0-12
func (c *ChainRandomnessSource) GetChainRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
ticket, err := c.GetChainRandomness(ctx, round, true)
if err != nil {
return nil, err
}
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
return DrawRandomness(ticket.VRFProof, pers, round, entropy)
}
// network v13 and on
func (c *ChainRandomnessSource) GetChainRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
ticket, err := c.GetChainRandomness(ctx, round, false)
if err != nil {
return nil, err
}
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
return DrawRandomness(ticket.VRFProof, pers, round, entropy)
}
// network v0-12
func (c *ChainRandomnessSource) GetBeaconRandomnessV1(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
randTS, err := c.GetBeaconRandomnessTipset(ctx, round, true)
if err != nil {
return nil, err
}
be, err := FindLatestDRAND(ctx, randTS, c.reader)
if err != nil {
return nil, err
}
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
return DrawRandomness(be.Data, pers, round, entropy)
}
// network v13
func (c *ChainRandomnessSource) GetBeaconRandomnessV2(ctx context.Context, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
randTS, err := c.GetBeaconRandomnessTipset(ctx, round, false)
if err != nil {
return nil, err
}
be, err := FindLatestDRAND(ctx, randTS, c.reader)
if err != nil {
return nil, err
}
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
return DrawRandomness(be.Data, pers, round, entropy)
}
// network v14 and on
func (c *ChainRandomnessSource) GetBeaconRandomnessV3(ctx context.Context, pers crypto.DomainSeparationTag, filecoinEpoch abi.ChainEpoch, entropy []byte) ([]byte, error) {
if filecoinEpoch < 0 {
return c.GetBeaconRandomnessV2(ctx, pers, filecoinEpoch, entropy)
}
be, err := c.extractBeaconEntryForEpoch(ctx, filecoinEpoch)
if err != nil {
log.Errorf("failed to get beacon entry as expected: %s", err)
return nil, err
}
return DrawRandomness(be.Data, pers, filecoinEpoch, entropy)
}
func (c *ChainRandomnessSource) extractBeaconEntryForEpoch(ctx context.Context, filecoinEpoch abi.ChainEpoch) (*types.BeaconEntry, error) {
randTS, err := c.GetBeaconRandomnessTipset(ctx, filecoinEpoch, false)
if err != nil {
return nil, err
}
nv := c.networkVersionGetter(ctx, filecoinEpoch)
round := c.beacon.BeaconForEpoch(filecoinEpoch).MaxBeaconRoundForEpoch(nv, filecoinEpoch)
for i := 0; i < 20; i++ {
cbe := randTS.Blocks()[0].BeaconEntries
for _, v := range cbe {
if v.Round == round {
return &v, nil
}
}
next, err := c.reader.GetTipSet(ctx, randTS.Parents())
if err != nil {
return nil, fmt.Errorf("failed to load parents when searching back for beacon entry: %w", err)
}
randTS = next
}
return nil, fmt.Errorf("didn't find beacon for round %d (epoch %d)", round, filecoinEpoch)
}
//BlendEntropy get randomness with chain value. sha256(buf(tag, seed, epoch, entropy))
func BlendEntropy(tag crypto.DomainSeparationTag, seed RandomSeed, epoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {
buffer := bytes.Buffer{}
err := binary.Write(&buffer, binary.BigEndian, int64(tag))
if err != nil {
return nil, errors.Wrap(err, "failed to write tag for randomness")
}
_, err = buffer.Write(seed)
if err != nil {
return nil, errors.Wrap(err, "failed to write seed for randomness")
}
err = binary.Write(&buffer, binary.BigEndian, int64(epoch))
if err != nil {
return nil, errors.Wrap(err, "failed to write epoch for randomness")
}
_, err = buffer.Write(entropy)
if err != nil {
return nil, errors.Wrap(err, "failed to write entropy for randomness")
}
bufHash := blake2b.Sum256(buffer.Bytes())
return bufHash[:], nil
}
func DrawRandomness(rbase []byte, pers crypto.DomainSeparationTag, round abi.ChainEpoch, entropy []byte) ([]byte, error) {
h := blake2b.New256()
if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil {
return nil, fmt.Errorf("deriving randomness: %s", err)
}
VRFDigest := blake2b.Sum256(rbase)
_, err := h.Write(VRFDigest[:])
if err != nil {
return nil, fmt.Errorf("hashing VRFDigest: %s", err)
}
if err := binary.Write(h, binary.BigEndian, round); err != nil {
return nil, fmt.Errorf("deriving randomness: %s", err)
}
_, err = h.Write(entropy)
if err != nil {
return nil, fmt.Errorf("hashing entropy: %s", err)
}
return h.Sum(nil), nil
}