-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfgmassif.go
131 lines (107 loc) · 3.39 KB
/
cfgmassif.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
package veracity
import (
"context"
"fmt"
"github.com/datatrails/go-datatrails-common/logger"
"github.com/datatrails/go-datatrails-merklelog/massifs"
"github.com/urfave/cli/v2"
)
const (
defaultMassifHeight = uint8(14)
)
// cfgMassifReader establishes the blob read only data accessor
// only azure blob storage is supported. Both emulated and produciton.
func cfgMassifReader(cmd *CmdCtx, cCtx *cli.Context) error {
var err error
if cmd.log == nil {
if err = cfgLogging(cmd, cCtx); err != nil {
return err
}
}
cmd.massifHeight = uint8(cCtx.Uint("height"))
if cmd.massifHeight == 0 {
cmd.massifHeight = defaultMassifHeight
}
localLog := cCtx.String("data-local")
remoteLog := cCtx.String("data-url")
if localLog != "" && remoteLog != "" {
return fmt.Errorf("can't use data-local and data-url at the same time")
}
if localLog == "" && remoteLog == "" {
// If we had no url and no local data supplied we default to the production data source.
reader, err := cfgReader(cmd, cCtx, true)
if err != nil {
return err
}
mr := massifs.NewMassifReader(logger.Sugar, reader)
cmd.massifReader = &mr
} else if localLog != "" {
codec, err := massifs.NewRootSignerCodec()
if err != nil {
return err
}
// This configures the dir cache and local reader for single tenant use,
// InReplicaMode is false, meaning tenant specific filesystem paths are
// not automatically derived.
cache, err := massifs.NewLogDirCache(
logger.Sugar,
NewFileOpener(),
massifs.WithDirCacheTenant(cCtx.String("tenant")), // may be empty string
massifs.WithDirCacheMassifLister(NewDirLister()),
massifs.WithDirCacheSealLister(NewDirLister()),
massifs.WithReaderOption(massifs.WithMassifHeight(cmd.massifHeight)),
massifs.WithReaderOption(massifs.WithCBORCodec(codec)),
)
if err != nil {
return err
}
mr, err := massifs.NewLocalReader(logger.Sugar, cache)
if err != nil {
return err
}
cmd.massifReader = &mr
} else {
// otherwise configure for reading from remote blobs
reader, err := cfgReader(cmd, cCtx, false)
if err != nil {
return err
}
mr := massifs.NewMassifReader(logger.Sugar, reader)
cmd.massifReader = &mr
}
return nil
}
// cfgMassif configures a massif reader and reads a massif
func cfgMassif(cmd *CmdCtx, cCtx *cli.Context) error {
var err error
if err = cfgMassifReader(cmd, cCtx); err != nil {
return err
}
tenant := CtxGetOneTenantOption(cCtx)
if tenant == "" {
return fmt.Errorf("tenant must be provided for this command")
}
ctx := context.Background()
mmrIndex := cCtx.Uint64("mmrindex")
massifIndex := cCtx.Uint64("massif")
// mmrIndex zero is always going to be massifIndex 0 so we treat this the
// same as though the massif option had been supplied as 0
if massifIndex == uint64(0) && mmrIndex == uint64(0) {
cmd.massif, err = cmd.massifReader.GetMassif(context.Background(), tenant, massifIndex)
return err
}
// now, if we have a non zero mmrIndex, use it to (re)compute the massifIndex
if mmrIndex > uint64(0) {
massifIndex = massifs.MassifIndexFromMMRIndex(cmd.massifHeight, mmrIndex)
cmd.massif, err = cmd.massifReader.GetMassif(context.Background(), tenant, massifIndex)
return err
}
// If massifIndex is not provided it will be zero here, and that is a good
// default.
massif, err := cmd.massifReader.GetMassif(ctx, tenant, massifIndex)
if err != nil {
return err
}
cmd.massif = massif
return nil
}