-
Notifications
You must be signed in to change notification settings - Fork 0
/
availability.go
108 lines (92 loc) · 3.26 KB
/
availability.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
package light
import (
"context"
"errors"
"math"
ipldFormat "github.com/ipfs/go-ipld-format"
logging "github.com/ipfs/go-log/v2"
"github.com/furyaxyz/elysium-node/share"
"github.com/furyaxyz/elysium-node/share/getters"
)
var log = logging.Logger("share/light")
// ShareAvailability implements share.Availability using Data Availability Sampling technique.
// It is light because it does not require the downloading of all the data to verify
// its availability. It is assumed that there are a lot of lightAvailability instances
// on the network doing sampling over the same Root to collectively verify its availability.
type ShareAvailability struct {
getter share.Getter
params Parameters
}
// NewShareAvailability creates a new light Availability.
func NewShareAvailability(
getter share.Getter,
opts ...Option,
) *ShareAvailability {
params := DefaultParameters()
for _, opt := range opts {
opt(¶ms)
}
return &ShareAvailability{getter, params}
}
// SharesAvailable randomly samples `params.SampleAmount` amount of Shares committed to the given
// Root. This way SharesAvailable subjectively verifies that Shares are available.
func (la *ShareAvailability) SharesAvailable(ctx context.Context, dah *share.Root) error {
log.Debugw("Validate availability", "root", dah.String())
// We assume the caller of this method has already performed basic validation on the
// given dah/root. If for some reason this has not happened, the node should panic.
if err := dah.ValidateBasic(); err != nil {
log.Errorw("Availability validation cannot be performed on a malformed DataAvailabilityHeader",
"err", err)
panic(err)
}
samples, err := SampleSquare(len(dah.RowsRoots), int(la.params.SampleAmount))
if err != nil {
return err
}
// indicate to the share.Getter that a blockservice session should be created. This
// functionality is optional and must be supported by the used share.Getter.
ctx = getters.WithSession(ctx)
log.Debugw("starting sampling session", "root", dah.String())
errs := make(chan error, len(samples))
for _, s := range samples {
go func(s Sample) {
log.Debugw("fetching share", "root", dah.String(), "row", s.Row, "col", s.Col)
_, err := la.getter.GetShare(ctx, dah, s.Row, s.Col)
if err != nil {
log.Debugw("error fetching share", "root", dah.String(), "row", s.Row, "col", s.Col)
}
// we don't really care about Share bodies at this point
// it also means we now saved the Share in local storage
select {
case errs <- err:
case <-ctx.Done():
}
}(s)
}
for range samples {
var err error
select {
case err = <-errs:
case <-ctx.Done():
err = ctx.Err()
}
if err != nil {
if !errors.Is(err, context.Canceled) {
log.Errorw("availability validation failed", "root", dah.String(), "err", err.Error())
}
if ipldFormat.IsNotFound(err) || errors.Is(err, context.DeadlineExceeded) {
return share.ErrNotAvailable
}
return err
}
}
return nil
}
// ProbabilityOfAvailability calculates the probability that the
// data square is available based on the amount of samples collected
// (params.SampleAmount).
//
// Formula: 1 - (0.75 ** amount of samples)
func (la *ShareAvailability) ProbabilityOfAvailability(context.Context) float64 {
return 1 - math.Pow(0.75, float64(la.params.SampleAmount))
}