/
consensus.go
176 lines (153 loc) · 6.57 KB
/
consensus.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 file
import (
"context"
coreCommon "github.com/oasisprotocol/oasis-core/go/common"
cmdCommon "github.com/oasisprotocol/nexus/cmd/common"
beacon "github.com/oasisprotocol/nexus/coreapi/v22.2.11/beacon/api"
consensus "github.com/oasisprotocol/nexus/coreapi/v22.2.11/consensus/api"
genesis "github.com/oasisprotocol/nexus/coreapi/v22.2.11/genesis/api"
"github.com/oasisprotocol/nexus/cache/kvstore"
"github.com/oasisprotocol/nexus/common"
"github.com/oasisprotocol/nexus/metrics"
"github.com/oasisprotocol/nexus/storage/oasis/connections"
"github.com/oasisprotocol/nexus/storage/oasis/nodeapi"
)
// FileConsensusApiLite provides access to the consensus API of an Oasis node.
// Since FileConsensusApiLite is backed by a file containing the cached responses
// to `ConsensusApiLite` calls, this data is inherently compatible with the
// current Nexus and can thus handle heights from both Cobalt/Damask.
type FileConsensusApiLite struct {
db kvstore.KVStore
consensusApi nodeapi.ConsensusApiLite
}
var _ nodeapi.ConsensusApiLite = (*FileConsensusApiLite)(nil)
func NewFileConsensusApiLite(cacheDir string, consensusApi nodeapi.ConsensusApiLite) (*FileConsensusApiLite, error) {
db, err := kvstore.OpenKVStore(
cmdCommon.RootLogger().WithModule("file-consensus-api-lite").With("runtime", "consensus"),
cacheDir,
common.Ptr(metrics.NewDefaultAnalysisMetrics("consensus")),
)
if err != nil {
return nil, err
}
return &FileConsensusApiLite{
db: db,
consensusApi: consensusApi,
}, nil
}
func (c *FileConsensusApiLite) Close() error {
// Close all resources and return the first encountered error, if any.
var firstErr error
if c.consensusApi != nil {
firstErr = c.consensusApi.Close()
}
if err := c.db.Close(); err != nil && firstErr == nil {
firstErr = err
}
return firstErr
}
func (c *FileConsensusApiLite) GetGenesisDocument(ctx context.Context, chainContext string) (*genesis.Document, error) {
return kvstore.GetFromCacheOrCall(
c.db, false,
kvstore.GenerateCacheKey("GetGenesisDocument", chainContext),
func() (*genesis.Document, error) { return c.consensusApi.GetGenesisDocument(ctx, chainContext) },
)
}
func (c *FileConsensusApiLite) StateToGenesis(ctx context.Context, height int64) (*genesis.Document, error) {
return kvstore.GetFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("StateToGenesis", height),
func() (*genesis.Document, error) { return c.consensusApi.StateToGenesis(ctx, height) },
)
}
func (c *FileConsensusApiLite) GetBlock(ctx context.Context, height int64) (*consensus.Block, error) {
return kvstore.GetFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetBlock", height),
func() (*consensus.Block, error) { return c.consensusApi.GetBlock(ctx, height) },
)
}
func (c *FileConsensusApiLite) GetTransactionsWithResults(ctx context.Context, height int64) ([]nodeapi.TransactionWithResults, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
// v2: Updated roothash events conversion to retain more data.
kvstore.GenerateCacheKey("GetTransactionsWithResults.v2", height),
func() ([]nodeapi.TransactionWithResults, error) {
return c.consensusApi.GetTransactionsWithResults(ctx, height)
},
)
}
func (c *FileConsensusApiLite) GetEpoch(ctx context.Context, height int64) (beacon.EpochTime, error) {
time, err := kvstore.GetFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetEpoch", height),
func() (*beacon.EpochTime, error) {
time, err := c.consensusApi.GetEpoch(ctx, height)
return &time, err
},
)
if err != nil {
return beacon.EpochInvalid, err
}
return *time, nil
}
func (c *FileConsensusApiLite) RegistryEvents(ctx context.Context, height int64) ([]nodeapi.Event, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("RegistryEvents", height),
func() ([]nodeapi.Event, error) { return c.consensusApi.RegistryEvents(ctx, height) },
)
}
func (c *FileConsensusApiLite) StakingEvents(ctx context.Context, height int64) ([]nodeapi.Event, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("StakingEvents", height),
func() ([]nodeapi.Event, error) { return c.consensusApi.StakingEvents(ctx, height) },
)
}
func (c *FileConsensusApiLite) GovernanceEvents(ctx context.Context, height int64) ([]nodeapi.Event, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GovernanceEvents", height),
func() ([]nodeapi.Event, error) { return c.consensusApi.GovernanceEvents(ctx, height) },
)
}
func (c *FileConsensusApiLite) RoothashEvents(ctx context.Context, height int64) ([]nodeapi.Event, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
// v2: Updated roothash events conversion to retain more data.
kvstore.GenerateCacheKey("RoothashEvents.v2", height),
func() ([]nodeapi.Event, error) { return c.consensusApi.RoothashEvents(ctx, height) },
)
}
func (c *FileConsensusApiLite) GetNodes(ctx context.Context, height int64) ([]nodeapi.Node, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetNodes", height),
func() ([]nodeapi.Node, error) { return c.consensusApi.GetNodes(ctx, height) },
)
}
func (c *FileConsensusApiLite) GetValidators(ctx context.Context, height int64) ([]nodeapi.Validator, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetValidators", height),
func() ([]nodeapi.Validator, error) { return c.consensusApi.GetValidators(ctx, height) },
)
}
func (c *FileConsensusApiLite) GetCommittees(ctx context.Context, height int64, runtimeID coreCommon.Namespace) ([]nodeapi.Committee, error) {
return kvstore.GetSliceFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetCommittee", height, runtimeID),
func() ([]nodeapi.Committee, error) { return c.consensusApi.GetCommittees(ctx, height, runtimeID) },
)
}
func (c *FileConsensusApiLite) GetProposal(ctx context.Context, height int64, proposalID uint64) (*nodeapi.Proposal, error) {
return kvstore.GetFromCacheOrCall(
c.db, height == consensus.HeightLatest,
kvstore.GenerateCacheKey("GetProposal", height, proposalID),
func() (*nodeapi.Proposal, error) { return c.consensusApi.GetProposal(ctx, height, proposalID) },
)
}
func (c *FileConsensusApiLite) GrpcConn() connections.GrpcConn {
return c.consensusApi.GrpcConn()
}