-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalization_keeper.go
75 lines (64 loc) · 2.63 KB
/
finalization_keeper.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package executor
import (
"context"
"fmt"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/pulse"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/pkg/errors"
)
// FinalizationKeeper check how far from each other last finalized pulse and current one
// and if distance is more than limit it stops network
type FinalizationKeeper interface {
OnPulse(ctx context.Context, current insolar.PulseNumber) error
}
type FinalizationKeeperDefault struct {
jetKeeper JetKeeper
networkStopper insolar.TerminationHandler
limit int
pulseCalculator pulse.Calculator
}
func NewFinalizationKeeperDefault(jk JetKeeper, pc pulse.Calculator, limit int) *FinalizationKeeperDefault {
return &FinalizationKeeperDefault{
jetKeeper: jk,
limit: limit,
pulseCalculator: pc,
}
}
func (f *FinalizationKeeperDefault) OnPulse(ctx context.Context, current insolar.PulseNumber) error {
logger := inslogger.FromContext(ctx)
bottomLevel, err := f.pulseCalculator.Backwards(ctx, current, f.limit)
if err != nil {
if err == pulse.ErrNotFound {
logger.Debug("finalizationKeeper: possibly we started not so long ago. Do nothing. Current pulse: ", current)
return nil
}
return errors.Wrap(err, "Can't get old pulse")
}
lastConfirmedPulse := f.jetKeeper.TopSyncPulse()
if current < lastConfirmedPulse {
return errors.New(fmt.Sprintf("Current pulse ( %d ) is less than last confirmed ( %d )", current, lastConfirmedPulse))
}
if lastConfirmedPulse <= bottomLevel.PulseNumber {
// f.networkStopper.Leave(ctx, 0)
panic(fmt.Sprintf("it shouldn't be called. lastConfirmedPulse:%v, bottomLevel.PulseNumber:%v", lastConfirmedPulse, bottomLevel.PulseNumber))
return errors.New(fmt.Sprintf("last finalized pulse falls behind too much. Stop node. bottomLevel.PulseNumber: %d, last confirmed: %d", bottomLevel.PulseNumber, lastConfirmedPulse))
}
logger.Debugf("FinalizationKeeper: everything is ok. Current pulse: %d, last confirmed: %d, limit: %d", current, lastConfirmedPulse, f.limit)
return nil
}