@@ -2,18 +2,15 @@ package pruner
2
2
3
3
import (
4
4
"context"
5
- "time"
6
5
7
6
"github.com/ipfs/go-datastore"
8
7
logging "github.com/ipfs/go-log/v2"
9
8
"go.uber.org/fx"
10
9
11
10
"github.com/celestiaorg/celestia-node/core"
12
- "github.com/celestiaorg/celestia-node/libs/fxutil"
13
11
"github.com/celestiaorg/celestia-node/nodebuilder/node"
14
12
modshare "github.com/celestiaorg/celestia-node/nodebuilder/share"
15
13
"github.com/celestiaorg/celestia-node/pruner"
16
- "github.com/celestiaorg/celestia-node/pruner/full"
17
14
"github.com/celestiaorg/celestia-node/share/availability"
18
15
fullavail "github.com/celestiaorg/celestia-node/share/availability/full"
19
16
"github.com/celestiaorg/celestia-node/share/availability/light"
@@ -23,12 +20,6 @@ import (
23
20
var log = logging .Logger ("module/pruner" )
24
21
25
22
func ConstructModule (tp node.Type , cfg * Config ) fx.Option {
26
- baseComponents := fx .Options (
27
- fx .Supply (cfg ),
28
- availWindow (tp , cfg .EnableService ),
29
- advertiseArchival (tp , cfg ),
30
- )
31
-
32
23
prunerService := fx .Options (
33
24
fx .Provide (fx .Annotate (
34
25
newPrunerService ,
@@ -44,49 +35,53 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
44
35
fx .Invoke (func (_ * pruner.Service ) {}),
45
36
)
46
37
38
+ baseComponents := fx .Options (
39
+ fx .Supply (cfg ),
40
+ // TODO @renaynay: move this to share module construction
41
+ fx .Supply (modshare .Window (availability .StorageWindow )),
42
+ advertiseArchival (tp , cfg ),
43
+ prunerService ,
44
+ )
45
+
47
46
switch tp {
48
47
case node .Light :
49
48
// LNs enforce pruning by default
50
49
return fx .Module ("prune" ,
51
50
baseComponents ,
52
- prunerService ,
53
51
// TODO(@walldiss @renaynay): remove conversion after Availability and Pruner interfaces are merged
54
52
// note this provide exists in pruner module to avoid cyclical imports
55
53
fx .Provide (func (la * light.ShareAvailability ) pruner.Pruner { return la }),
56
54
)
57
55
case node .Full :
58
- if cfg .EnableService {
59
- return fx .Module ("prune" ,
60
- baseComponents ,
61
- prunerService ,
62
- fxutil .ProvideAs (full .NewPruner , new (pruner.Pruner )),
63
- fx .Supply ([]fullavail.Option {}),
64
- )
56
+ fullAvailOpts := make ([]fullavail.Option , 0 )
57
+
58
+ if ! cfg .EnableService {
59
+ // populate archival mode opts
60
+ fullAvailOpts = []fullavail.Option {fullavail .WithArchivalMode ()}
65
61
}
62
+
66
63
return fx .Module ("prune" ,
67
64
baseComponents ,
68
- fx .Invoke (func (ctx context.Context , ds datastore.Batching ) error {
69
- return pruner .DetectPreviousRun (ctx , ds )
70
- }),
71
- fx .Supply ([]fullavail.Option {fullavail .WithArchivalMode ()}),
65
+ fx .Supply (fullAvailOpts ),
66
+ fx .Provide (func (fa * fullavail.ShareAvailability ) pruner.Pruner { return fa }),
67
+ convertToPruned (),
72
68
)
73
69
case node .Bridge :
74
- if cfg .EnableService {
75
- return fx .Module ("prune" ,
76
- baseComponents ,
77
- prunerService ,
78
- fxutil .ProvideAs (full .NewPruner , new (pruner.Pruner )),
79
- fx .Supply ([]fullavail.Option {}),
80
- fx .Supply ([]core.Option {}),
81
- )
70
+ coreOpts := make ([]core.Option , 0 )
71
+ fullAvailOpts := make ([]fullavail.Option , 0 )
72
+
73
+ if ! cfg .EnableService {
74
+ // populate archival mode opts
75
+ coreOpts = []core.Option {core .WithArchivalMode ()}
76
+ fullAvailOpts = []fullavail.Option {fullavail .WithArchivalMode ()}
82
77
}
78
+
83
79
return fx .Module ("prune" ,
84
80
baseComponents ,
85
- fx .Invoke (func (ctx context.Context , ds datastore.Batching ) error {
86
- return pruner .DetectPreviousRun (ctx , ds )
87
- }),
88
- fx .Supply ([]fullavail.Option {fullavail .WithArchivalMode ()}),
89
- fx .Supply ([]core.Option {core .WithArchivalMode ()}),
81
+ fx .Provide (func (fa * fullavail.ShareAvailability ) pruner.Pruner { return fa }),
82
+ fx .Supply (coreOpts ),
83
+ fx .Supply (fullAvailOpts ),
84
+ convertToPruned (),
90
85
)
91
86
default :
92
87
panic ("unknown node type" )
@@ -103,23 +98,39 @@ func advertiseArchival(tp node.Type, pruneCfg *Config) fx.Option {
103
98
})
104
99
}
105
100
106
- func availWindow (tp node.Type , pruneEnabled bool ) fx.Option {
107
- switch tp {
108
- case node .Light :
109
- // light nodes are still subject to sampling within window
110
- // even if pruning is not enabled.
111
- return fx .Provide (func () modshare.Window {
112
- return modshare .Window (availability .StorageWindow )
113
- })
114
- case node .Full , node .Bridge :
115
- return fx .Provide (func () modshare.Window {
116
- if pruneEnabled {
117
- return modshare .Window (availability .StorageWindow )
118
- }
119
- // implicitly disable pruning by setting the window to 0
120
- return modshare .Window (time .Duration (0 ))
121
- })
122
- default :
123
- panic ("unknown node type" )
124
- }
101
+ // convertToPruned checks if the node is being converted to an archival node
102
+ // to a pruned node.
103
+ func convertToPruned () fx.Option {
104
+ return fx .Invoke (func (
105
+ ctx context.Context ,
106
+ cfg * Config ,
107
+ ds datastore.Batching ,
108
+ p * pruner.Service ,
109
+ ) error {
110
+ lastPrunedHeight , err := p .LastPruned (ctx )
111
+ if err != nil {
112
+ return err
113
+ }
114
+
115
+ err = detectFirstRun (ctx , cfg , ds , lastPrunedHeight )
116
+ if err != nil {
117
+ return err
118
+ }
119
+
120
+ isArchival := ! cfg .EnableService
121
+ convert , err := fullavail .ConvertFromArchivalToPruned (ctx , ds , isArchival )
122
+ if err != nil {
123
+ return err
124
+ }
125
+
126
+ // if we convert the node from archival to pruned, we need to reset the checkpoint
127
+ // to ensure the node goes back and deletes *all* blocks older than the
128
+ // availability window, as archival "pruning" only trims the .q4 file,
129
+ // but retains the ODS.
130
+ if convert {
131
+ return p .ResetCheckpoint (ctx )
132
+ }
133
+
134
+ return nil
135
+ })
125
136
}
0 commit comments