-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathchain_v2.go
146 lines (126 loc) · 4.4 KB
/
chain_v2.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
package full
import (
"context"
"errors"
"go.uber.org/fx"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-f3"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/lf3"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
)
var _ ChainModuleAPIv2 = (*ChainModuleV2)(nil)
type ChainModuleAPIv2 interface {
ChainGetTipSet(context.Context, types.TipSetSelector) (*types.TipSet, error)
}
type ChainModuleV2 struct {
Chain *store.ChainStore
F3 lf3.F3Backend `optional:"true"`
fx.In
}
func (cm *ChainModuleV2) ChainGetTipSet(ctx context.Context, selector types.TipSetSelector) (*types.TipSet, error) {
if err := selector.Validate(); err != nil {
return nil, xerrors.Errorf("validating selector: %w", err)
}
// Get tipset by key.
if selector.Key != nil {
return cm.Chain.GetTipSetFromKey(ctx, *selector.Key)
}
// Get tipset by height.
if selector.Height != nil {
anchor, err := cm.getTipSetByAnchor(ctx, selector.Height.Anchor)
if err != nil {
return nil, xerrors.Errorf("getting anchor from tipset: %w", err)
}
return cm.Chain.GetTipsetByHeight(ctx, *selector.Height.At, anchor, selector.Height.Previous)
}
// Get tipset by tag, either latest or finalized.
if selector.Tag != nil {
return cm.getTipSetByTag(ctx, *selector.Tag)
}
return nil, xerrors.Errorf("no tipset found for selector")
}
func (cm *ChainModuleV2) getTipSetByTag(ctx context.Context, tag types.TipSetTag) (*types.TipSet, error) {
switch tag {
case types.TipSetTags.Latest:
return cm.Chain.GetHeaviestTipSet(), nil
case types.TipSetTags.Finalized:
return cm.getLatestFinalizedTipset(ctx)
case types.TipSetTags.Safe:
return cm.getLatestSafeTipSet(ctx)
default:
return nil, xerrors.Errorf("unknown tipset tag: %s", tag)
}
}
func (cm *ChainModuleV2) getLatestSafeTipSet(ctx context.Context) (*types.TipSet, error) {
finalized, err := cm.getLatestFinalizedTipset(ctx)
if err != nil {
return nil, xerrors.Errorf("getting latest finalized tipset: %w", err)
}
heaviest := cm.Chain.GetHeaviestTipSet()
switch {
case finalized == nil:
return heaviest, nil
case heaviest == nil:
return finalized, nil
case finalized.Height() >= heaviest.Height()-build.SafeHeightDistance:
return finalized, nil
default:
safeAt := max(0, heaviest.Height()-build.SafeHeightDistance)
return cm.Chain.GetTipsetByHeight(ctx, safeAt, heaviest, true)
}
}
func (cm *ChainModuleV2) getLatestFinalizedTipset(ctx context.Context) (*types.TipSet, error) {
if cm.F3 == nil {
// F3 is disabled; fall back to EC finality.
return cm.getECFinalized(ctx)
}
cert, err := cm.F3.GetLatestCert(ctx)
if err != nil {
if errors.Is(err, f3.ErrF3NotRunning) || errors.Is(err, api.ErrF3NotReady) {
// Only fall back to EC finality if F3 isn't running or not ready.
log.Debugw("F3 not running or not ready, falling back to EC finality", "err", err)
return cm.getECFinalized(ctx)
}
return nil, err
}
if cert == nil {
// No latest certificate. Fall back to EC finality.
return cm.getECFinalized(ctx)
}
// Extract the finalized tipeset from the certificate.
tsk, err := types.TipSetKeyFromBytes(cert.ECChain.Head().Key)
if err != nil {
return nil, xerrors.Errorf("decoding latest f3 cert tipset key: %w", err)
}
ts, err := cm.Chain.LoadTipSet(ctx, tsk)
if err != nil {
return nil, xerrors.Errorf("loading latest f3 cert tipset %s: %w", tsk, err)
}
return ts, nil
}
func (cm *ChainModuleV2) getTipSetByAnchor(ctx context.Context, anchor *types.TipSetAnchor) (*types.TipSet, error) {
switch {
case anchor == nil:
// No anchor specified. Fall back to finalized tipset.
return cm.getTipSetByTag(ctx, types.TipSetTags.Finalized)
case anchor.Key == nil && anchor.Tag == nil:
// Anchor is zero-valued. Fall back to heaviest tipset.
return cm.Chain.GetHeaviestTipSet(), nil
case anchor.Key != nil:
// Get tipset at the specified key.
return cm.Chain.GetTipSetFromKey(ctx, *anchor.Key)
case anchor.Tag != nil:
return cm.getTipSetByTag(ctx, *anchor.Tag)
default:
return nil, xerrors.Errorf("invalid anchor: %v", anchor)
}
}
func (cm *ChainModuleV2) getECFinalized(ctx context.Context) (*types.TipSet, error) {
head := cm.Chain.GetHeaviestTipSet()
finalizedHeight := head.Height() - policy.ChainFinality
return cm.Chain.GetTipsetByHeight(ctx, finalizedHeight, head, true)
}