-
Notifications
You must be signed in to change notification settings - Fork 0
/
availability.go
88 lines (73 loc) · 2.39 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
package full
import (
"context"
"errors"
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/availability/discovery"
"github.com/furyaxyz/elysium-node/share/eds"
"github.com/furyaxyz/elysium-node/share/eds/byzantine"
)
var log = logging.Logger("share/full")
// ShareAvailability implements share.Availability using the full data square
// recovery technique. It is considered "full" because it is required
// to download enough shares to fully reconstruct the data square.
type ShareAvailability struct {
store *eds.Store
getter share.Getter
disc *discovery.Discovery
cancel context.CancelFunc
}
// NewShareAvailability creates a new full ShareAvailability.
func NewShareAvailability(
store *eds.Store,
getter share.Getter,
disc *discovery.Discovery,
) *ShareAvailability {
return &ShareAvailability{
store: store,
getter: getter,
disc: disc,
}
}
func (fa *ShareAvailability) Start(context.Context) error {
ctx, cancel := context.WithCancel(context.Background())
fa.cancel = cancel
go fa.disc.Advertise(ctx)
return nil
}
func (fa *ShareAvailability) Stop(context.Context) error {
fa.cancel()
return nil
}
// SharesAvailable reconstructs the data committed to the given Root by requesting
// enough Shares from the network.
func (fa *ShareAvailability) SharesAvailable(ctx context.Context, root *share.Root) error {
// 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 := root.ValidateBasic(); err != nil {
log.Errorw("Availability validation cannot be performed on a malformed DataAvailabilityHeader",
"err", err)
panic(err)
}
// a hack to avoid loading the whole EDS in mem if we store it already.
if fa.store != nil {
if ok, _ := fa.store.Has(ctx, root.Hash()); ok {
return nil
}
}
_, err := fa.getter.GetEDS(ctx, root)
if err != nil {
log.Errorw("availability validation failed", "root", root.String(), "err", err.Error())
var byzantineErr *byzantine.ErrByzantine
if ipldFormat.IsNotFound(err) || errors.Is(err, context.DeadlineExceeded) && !errors.As(err, &byzantineErr) {
return share.ErrNotAvailable
}
return err
}
return err
}
func (fa *ShareAvailability) ProbabilityOfAvailability(context.Context) float64 {
return 1
}