-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_digester.go
176 lines (144 loc) · 3.97 KB
/
process_digester.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cmds
import (
"context"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/launch/config"
"github.com/spikeekips/mitum/launch/pm"
"github.com/spikeekips/mitum/launch/process"
"github.com/spikeekips/mitum/storage"
"github.com/spikeekips/mitum/storage/blockdata"
"github.com/spikeekips/mitum/storage/blockdata/localfs"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/logging"
"github.com/soonkuk/mitum-blocksign/digest"
"github.com/spikeekips/mitum-currency/currency"
)
const (
ProcessNameDigester = "digester"
ProcessNameStartDigester = "start_digester"
HookNameDigesterFollowUp = "followup_digester"
)
var (
ProcessorDigester pm.Process
ProcessorStartDigester pm.Process
)
func init() {
if i, err := pm.NewProcess(
ProcessNameDigester,
[]string{ProcessNameDigestDatabase},
ProcessDigester,
); err != nil {
panic(err)
} else {
ProcessorDigester = i
}
if i, err := pm.NewProcess(
ProcessNameStartDigester,
[]string{ProcessNameDigester},
ProcessStartDigester,
); err != nil {
panic(err)
} else {
ProcessorStartDigester = i
}
}
func ProcessDigester(ctx context.Context) (context.Context, error) {
var log *logging.Logging
if err := config.LoadLogContextValue(ctx, &log); err != nil {
return ctx, err
}
var st *digest.Database
if err := LoadDigestDatabaseContextValue(ctx, &st); err != nil {
if errors.Is(err, util.ContextValueNotFoundError) {
return ctx, nil
}
return ctx, err
}
di := digest.NewDigester(st, nil)
_ = di.SetLogging(log)
return context.WithValue(ctx, ContextValueDigester, di), nil
}
func ProcessStartDigester(ctx context.Context) (context.Context, error) {
var di *digest.Digester
if err := LoadDigesterContextValue(ctx, &di); err != nil {
if errors.Is(err, util.ContextValueNotFoundError) {
return ctx, nil
}
return ctx, err
}
return ctx, di.Start()
}
func HookDigesterFollowUp(ctx context.Context) (context.Context, error) {
var log *logging.Logging
if err := config.LoadLogContextValue(ctx, &log); err != nil {
return ctx, err
}
log.Log().Debug().Msg("digester trying to follow up")
var mst storage.Database
if err := process.LoadDatabaseContextValue(ctx, &mst); err != nil {
return ctx, err
}
var st *digest.Database
if err := LoadDigestDatabaseContextValue(ctx, &st); err != nil {
if errors.Is(err, util.ContextValueNotFoundError) {
return ctx, nil
}
return ctx, err
}
switch m, found, err := mst.LastManifest(); {
case err != nil:
return ctx, err
case !found:
log.Log().Debug().Msg("last manifest not found")
case m.Height() > st.LastBlock():
log.Log().Info().
Int64("last_manifest", m.Height().Int64()).
Int64("last_block", st.LastBlock().Int64()).
Msg("new blocks found to digest")
if err := digestFollowup(ctx, m.Height()); err != nil {
log.Log().Error().Err(err).Msg("failed to follow up")
return ctx, err
}
log.Log().Info().Msg("digested new blocks")
default:
log.Log().Info().Msg("digested blocks is up-to-dated")
}
return ctx, nil
}
func digestFollowup(ctx context.Context, height base.Height) error {
var st *digest.Database
if err := LoadDigestDatabaseContextValue(ctx, &st); err != nil {
return err
}
var blockData *localfs.BlockData
{
var bd blockdata.BlockData
if err := process.LoadBlockDataContextValue(ctx, &bd); err != nil {
return err
} else if i, ok := bd.(*localfs.BlockData); !ok {
return errors.Errorf("other block data, %T not yet supported", bd)
} else {
blockData = i
}
}
var cp *currency.CurrencyPool
if err := LoadCurrencyPoolContextValue(ctx, &cp); err != nil {
return err
}
if height <= st.LastBlock() {
return nil
}
lastBlock := st.LastBlock()
if lastBlock < base.PreGenesisHeight {
lastBlock = base.PreGenesisHeight
}
for i := lastBlock; i <= height; i++ {
if _, blk, err := localfs.LoadBlock(blockData, i); err != nil {
return err
} else if err := digest.DigestBlock(st, blk); err != nil {
return err
}
}
return nil
}