-
Notifications
You must be signed in to change notification settings - Fork 151
/
node.go
114 lines (97 loc) · 3.59 KB
/
node.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
package coreapi
import (
"strings"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"github.com/iotaledger/hornet/v2/pkg/common"
"github.com/iotaledger/hornet/v2/pkg/tipselect"
iotago "github.com/iotaledger/iota.go/v3"
)
//nolint:unparam // even if the error is never used, the structure of all routes should be the same
func info() (*infoResponse, error) {
var blocksPerSecond, referencedBlocksPerSecond, referencedRate float64
lastConfirmedMilestoneMetric := deps.Tangle.LastConfirmedMilestoneMetric()
if lastConfirmedMilestoneMetric != nil {
blocksPerSecond = lastConfirmedMilestoneMetric.BPS
referencedBlocksPerSecond = lastConfirmedMilestoneMetric.RBPS
referencedRate = lastConfirmedMilestoneMetric.ReferencedRate
}
syncState := deps.SyncManager.SyncState()
// latest milestone
var latestMilestoneIndex = syncState.LatestMilestoneIndex
var latestMilestoneTimestamp uint32
var latestMilestoneIDHex string
cachedMilestoneLatest := deps.Storage.CachedMilestoneByIndexOrNil(latestMilestoneIndex) // milestone +1
if cachedMilestoneLatest != nil {
latestMilestoneTimestamp = cachedMilestoneLatest.Milestone().TimestampUnix()
latestMilestoneIDHex = cachedMilestoneLatest.Milestone().MilestoneIDHex()
cachedMilestoneLatest.Release(true) // milestone -1
}
// confirmed milestone index
var confirmedMilestoneIndex = syncState.ConfirmedMilestoneIndex
var confirmedMilestoneTimestamp uint32
var confirmedMilestoneIDHex string
cachedMilestoneConfirmed := deps.Storage.CachedMilestoneByIndexOrNil(confirmedMilestoneIndex) // milestone +1
if cachedMilestoneConfirmed != nil {
confirmedMilestoneTimestamp = cachedMilestoneConfirmed.Milestone().TimestampUnix()
confirmedMilestoneIDHex = cachedMilestoneConfirmed.Milestone().MilestoneIDHex()
cachedMilestoneConfirmed.Release(true) // milestone -1
}
// pruning index
var pruningIndex iotago.MilestoneIndex
snapshotInfo := deps.Storage.SnapshotInfo()
if snapshotInfo != nil {
pruningIndex = snapshotInfo.PruningIndex()
}
return &infoResponse{
Name: deps.AppInfo.Name,
Version: deps.AppInfo.Version,
Status: nodeStatus{
IsHealthy: deps.Tangle.IsNodeHealthy(syncState),
LatestMilestone: milestoneInfoResponse{
Index: latestMilestoneIndex,
Timestamp: latestMilestoneTimestamp,
MilestoneID: latestMilestoneIDHex,
},
ConfirmedMilestone: milestoneInfoResponse{
Index: confirmedMilestoneIndex,
Timestamp: confirmedMilestoneTimestamp,
MilestoneID: confirmedMilestoneIDHex,
},
PruningIndex: pruningIndex,
},
SupportedProtocolVersions: deps.ProtocolManager.SupportedVersions(),
ProtocolParameters: deps.ProtocolManager.Current(),
PendingProtocolParameters: deps.ProtocolManager.Pending(),
BaseToken: deps.BaseToken,
Metrics: nodeMetrics{
BlocksPerSecond: blocksPerSecond,
ReferencedBlocksPerSecond: referencedBlocksPerSecond,
ReferencedRate: referencedRate,
},
Features: features,
}, nil
}
func tips(c echo.Context) (*tipsResponse, error) {
allowSemiLazy := false
for query := range c.QueryParams() {
if strings.ToLower(query) == "allowsemilazy" {
allowSemiLazy = true
break
}
}
var tips iotago.BlockIDs
var err error
if !allowSemiLazy {
tips, err = deps.TipSelector.SelectNonLazyTips()
} else {
tips, err = deps.TipSelector.SelectTipsWithSemiLazyAllowed()
}
if err != nil {
if errors.Is(err, common.ErrNodeNotSynced) || errors.Is(err, tipselect.ErrNoTipsAvailable) {
return nil, errors.WithMessage(echo.ErrServiceUnavailable, err.Error())
}
return nil, err
}
return &tipsResponse{Tips: tips.ToHex()}, nil
}